e493b9fc4ba61855805d8fb1d0169d0e7c2b902d
[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), m_format(format)
27 {
28         Create();
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         PixelFormat format;
51         uint8_t num_planes;
52         struct FormatPlaneInfo planes[4];
53 };
55 static const FormatInfo format_info_array[] = {
56         /* YUV packed */
57         { PixelFormat::UYVY, 1, { { 32, 2, 1 } }, },
58         { PixelFormat::YUYV, 1, { { 32, 2, 1 } }, },
59         /* YUV semi-planar */
60         { PixelFormat::NV12, 2, { { 8, 1, 1, }, { 16, 2, 2 } }, },
61         /* RGB16 */
62         { PixelFormat::RGB565, 1, { { 16, 1, 1 } }, },
63         /* RGB32 */
64         { PixelFormat::XRGB8888, 1, { { 32, 1, 1 } }, },
65 };
67 static const FormatInfo& find_format(PixelFormat 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()
78 {
79         int r;
81         const FormatInfo& format_info = find_format(m_format);
83         m_num_planes = format_info.num_planes;
85         for (int i = 0; i < format_info.num_planes; ++i) {
86                 const FormatPlaneInfo& pi = format_info.planes[i];
87                 FramebufferPlane& plane = m_planes[i];
89                 /* create dumb buffer */
90                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
91                 creq.width = width() / pi.xsub;
92                 creq.height = height() / pi.ysub;
93                 creq.bpp = pi.bitspp;
94                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
95                 if (r)
96                         throw std::invalid_argument("foo");
98                 plane.handle = creq.handle;
99                 plane.stride = creq.pitch;
100                 plane.size = creq.height * creq.pitch;
102                 /*
103                 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
104                         i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
105                 */
107                 /* prepare buffer for memory mapping */
108                 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
109                 mreq.handle = plane.handle;
110                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
111                 if (r)
112                         throw std::invalid_argument("foo");
114                 /* perform actual memory mapping */
115                 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
116                                                   card().fd(), mreq.offset);
117                 if (plane.map == MAP_FAILED)
118                         throw std::invalid_argument("foo");
120                 /* clear the framebuffer to 0 */
121                 memset(plane.map, 0, plane.size);
122         }
124         /* create framebuffer object for the dumb-buffer */
125         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
126         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
127         uint32_t offsets[4] = { 0 };
128         uint32_t id;
129         r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
130                           bo_handles, pitches, offsets, &id, 0);
131         if (r)
132                 throw std::invalid_argument("foo");
134         set_id(id);
137 void DumbFramebuffer::Destroy()
139         /* delete framebuffer */
140         drmModeRmFB(card().fd(), id());
142         for (uint i = 0; i < m_num_planes; ++i) {
143                 FramebufferPlane& plane = m_planes[i];
145                 /* unmap buffer */
146                 munmap(plane.map, plane.size);
148                 /* delete dumb buffer */
149                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
150                 dreq.handle = plane.handle;
151                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
153         }
156 void DumbFramebuffer::clear()
158         for (unsigned i = 0; i < m_num_planes; ++i)
159                 memset(m_planes[i].map, 0, m_planes[i].size);