add PixelFormat support to DumbFramebuffer
[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 string& fourcc)
21         :DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
22 {
23 }
25 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
26         :Framebuffer(card, width, height)
27 {
28         Create(width, height, (uint32_t)format);
29 }
31 DumbFramebuffer::~DumbFramebuffer()
32 {
33         Destroy();
34 }
36 void DumbFramebuffer::print_short() const
37 {
38         printf("DumbFramebuffer %d\n", id());
39 }
41 struct FormatPlaneInfo
42 {
43         uint8_t bitspp; /* bits per (macro) pixel */
44         uint8_t xsub;
45         uint8_t ysub;
46 };
48 struct FormatInfo
49 {
50         uint32_t format;
51         uint8_t num_planes;
52         struct FormatPlaneInfo planes[4];
53 };
55 static const FormatInfo format_info_array[] = {
56         /* YUV packed */
57         { DRM_FORMAT_UYVY, 1, { { 32, 2, 1 } }, },
58         { DRM_FORMAT_YUYV, 1, { { 32, 2, 1 } }, },
59         /* YUV semi-planar */
60         { DRM_FORMAT_NV12, 2, { { 8, 1, 1, }, { 16, 2, 2 } }, },
61         /* RGB16 */
62         { DRM_FORMAT_RGB565, 1, { { 16, 1, 1 } }, },
63         /* RGB32 */
64         { DRM_FORMAT_XRGB8888, 1, { { 32, 1, 1 } }, },
65 };
67 static const FormatInfo& find_format(uint32_t format)
68 {
69         for (uint i = 0; i < ARRAY_SIZE(format_info_array); ++i) {
70                 if (format == format_info_array[i].format)
71                         return format_info_array[i];
72         }
74         throw std::invalid_argument("foo");
75 }
77 void DumbFramebuffer::Create(uint32_t width, uint32_t height, uint32_t format)
78 {
79         int r;
81         m_format = format;
83         const FormatInfo& format_info = find_format(format);
85         m_num_planes = format_info.num_planes;
87         for (int i = 0; i < format_info.num_planes; ++i) {
88                 const FormatPlaneInfo& pi = format_info.planes[i];
89                 FramebufferPlane& plane = m_planes[i];
91                 /* create dumb buffer */
92                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
93                 creq.width = width / pi.xsub;
94                 creq.height = height / pi.ysub;
95                 creq.bpp = pi.bitspp;
96                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
97                 if (r)
98                         throw std::invalid_argument("foo");
100                 plane.handle = creq.handle;
101                 plane.stride = creq.pitch;
102                 plane.size = creq.height * creq.pitch;
104                 /*
105                 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
106                         i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
107                 */
109                 /* prepare buffer for memory mapping */
110                 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
111                 mreq.handle = plane.handle;
112                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
113                 if (r)
114                         throw std::invalid_argument("foo");
116                 /* perform actual memory mapping */
117                 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
118                                                   card().fd(), mreq.offset);
119                 if (plane.map == MAP_FAILED)
120                         throw std::invalid_argument("foo");
122                 /* clear the framebuffer to 0 */
123                 memset(plane.map, 0, plane.size);
124         }
126         /* create framebuffer object for the dumb-buffer */
127         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
128         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
129         uint32_t offsets[4] = { 0 };
130         uint32_t id;
131         r = drmModeAddFB2(card().fd(), width, height, format,
132                           bo_handles, pitches, offsets, &id, 0);
133         if (r)
134                 throw std::invalid_argument("foo");
136         set_id(id);
139 void DumbFramebuffer::Destroy()
141         /* delete framebuffer */
142         drmModeRmFB(card().fd(), id());
144         for (uint i = 0; i < m_num_planes; ++i) {
145                 FramebufferPlane& plane = m_planes[i];
147                 /* unmap buffer */
148                 munmap(plane.map, plane.size);
150                 /* delete dumb buffer */
151                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
152                 dreq.handle = plane.handle;
153                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
155         }
158 void DumbFramebuffer::clear()
160         for (unsigned i = 0; i < m_num_planes; ++i)
161                 memset(m_planes[i].map, 0, m_planes[i].size);