]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blob - libkms++/dumbframebuffer.cpp
Add DrmObject::set_id()
[android/external-libkmsxx.git] / libkms++ / dumbframebuffer.cpp
2 #include <cstring>
3 #include <stdexcept>
4 #include <sys/mman.h>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7 #include <drm_fourcc.h>
8 #include <drm.h>
9 #include <drm_mode.h>
11 #include "kms++.h"
13 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
15 using namespace std;
17 namespace kms
18 {
20 DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const char* fourcc)
21         :Framebuffer(card, width, height)
22 {
23         uint32_t a, b, c, d;
24         a = fourcc[0];
25         b = fourcc[1];
26         c = fourcc[2];
27         d = fourcc[3];
29         uint32_t code = ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24));
31         Create(width, height, code);
32 }
34 DumbFramebuffer::~DumbFramebuffer()
35 {
36         Destroy();
37 }
39 void DumbFramebuffer::print_short() const
40 {
41         printf("DumbFramebuffer %d\n", id());
42 }
44 struct FormatPlaneInfo
45 {
46         uint8_t bitspp; /* bits per (macro) pixel */
47         uint8_t xsub;
48         uint8_t ysub;
49 };
51 struct FormatInfo
52 {
53         uint32_t format;
54         uint8_t num_planes;
55         struct FormatPlaneInfo planes[4];
56 };
58 static const FormatInfo format_info_array[] = {
59         /* YUV packed */
60         { DRM_FORMAT_UYVY, 1, { { 32, 2, 1 } }, },
61         { DRM_FORMAT_YUYV, 1, { { 32, 2, 1 } }, },
62         /* YUV semi-planar */
63         { DRM_FORMAT_NV12, 2, { { 8, 1, 1, }, { 16, 2, 2 } }, },
64         /* RGB16 */
65         { DRM_FORMAT_RGB565, 1, { { 16, 1, 1 } }, },
66         /* RGB32 */
67         { DRM_FORMAT_XRGB8888, 1, { { 32, 1, 1 } }, },
68 };
70 static const FormatInfo& find_format(uint32_t format)
71 {
72         for (uint i = 0; i < ARRAY_SIZE(format_info_array); ++i) {
73                 if (format == format_info_array[i].format)
74                         return format_info_array[i];
75         }
77         throw std::invalid_argument("foo");
78 }
80 void DumbFramebuffer::Create(uint32_t width, uint32_t height, uint32_t format)
81 {
82         int r;
84         m_format = format;
86         const FormatInfo& format_info = find_format(format);
88         m_num_planes = format_info.num_planes;
90         for (int i = 0; i < format_info.num_planes; ++i) {
91                 const FormatPlaneInfo& pi = format_info.planes[i];
92                 FramebufferPlane& plane = m_planes[i];
94                 /* create dumb buffer */
95                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
96                 creq.width = width / pi.xsub;
97                 creq.height = height / pi.ysub;
98                 creq.bpp = pi.bitspp;
99                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
100                 if (r)
101                         throw std::invalid_argument("foo");
103                 plane.handle = creq.handle;
104                 plane.stride = creq.pitch;
105                 plane.size = creq.height * creq.pitch;
107                 /*
108                 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
109                         i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
110                 */
112                 /* prepare buffer for memory mapping */
113                 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
114                 mreq.handle = plane.handle;
115                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
116                 if (r)
117                         throw std::invalid_argument("foo");
119                 /* perform actual memory mapping */
120                 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
121                                                   card().fd(), mreq.offset);
122                 if (plane.map == MAP_FAILED)
123                         throw std::invalid_argument("foo");
125                 /* clear the framebuffer to 0 */
126                 memset(plane.map, 0, plane.size);
127         }
129         /* create framebuffer object for the dumb-buffer */
130         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
131         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
132         uint32_t offsets[4] = { 0 };
133         uint32_t id;
134         r = drmModeAddFB2(card().fd(), width, height, format,
135                           bo_handles, pitches, offsets, &id, 0);
136         if (r)
137                 throw std::invalid_argument("foo");
139         set_id(id);
142 void DumbFramebuffer::Destroy()
144         /* delete framebuffer */
145         drmModeRmFB(card().fd(), id());
147         for (uint i = 0; i < m_num_planes; ++i) {
148                 FramebufferPlane& plane = m_planes[i];
150                 /* unmap buffer */
151                 munmap(plane.map, plane.size);
153                 /* delete dumb buffer */
154                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
155                 dreq.handle = plane.handle;
156                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
158         }
161 void DumbFramebuffer::clear()
163         for (unsigned i = 0; i < m_num_planes; ++i)
164                 memset(m_planes[i].map, 0, m_planes[i].size);