4e3b56bef804c52d87e89fb8b8c824c4c7f7c6e5
[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         uint8_t num_planes;
51         struct FormatPlaneInfo planes[4];
52 };
54 static const map<PixelFormat, FormatInfo> format_info_array = {
55         /* YUV packed */
56         { PixelFormat::UYVY, { 1, { { 32, 2, 1 } }, } },
57         { PixelFormat::YUYV, { 1, { { 32, 2, 1 } }, } },
58         /* YUV semi-planar */
59         { PixelFormat::NV12, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
60         /* RGB16 */
61         { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } },
62         /* RGB32 */
63         { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } },
64 };
66 void DumbFramebuffer::Create()
67 {
68         int r;
70         const FormatInfo& format_info = format_info_array.at(m_format);
72         m_num_planes = format_info.num_planes;
74         for (int i = 0; i < format_info.num_planes; ++i) {
75                 const FormatPlaneInfo& pi = format_info.planes[i];
76                 FramebufferPlane& plane = m_planes[i];
78                 /* create dumb buffer */
79                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
80                 creq.width = width() / pi.xsub;
81                 creq.height = height() / pi.ysub;
82                 creq.bpp = pi.bitspp;
83                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
84                 if (r)
85                         throw std::invalid_argument("foo");
87                 plane.handle = creq.handle;
88                 plane.stride = creq.pitch;
89                 plane.size = creq.height * creq.pitch;
91                 /*
92                 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
93                         i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
94                 */
96                 /* prepare buffer for memory mapping */
97                 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
98                 mreq.handle = plane.handle;
99                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
100                 if (r)
101                         throw std::invalid_argument("foo");
103                 /* perform actual memory mapping */
104                 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
105                                                   card().fd(), mreq.offset);
106                 if (plane.map == MAP_FAILED)
107                         throw std::invalid_argument("foo");
109                 /* clear the framebuffer to 0 */
110                 memset(plane.map, 0, plane.size);
111         }
113         /* create framebuffer object for the dumb-buffer */
114         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
115         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
116         uint32_t offsets[4] = { 0 };
117         uint32_t id;
118         r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
119                           bo_handles, pitches, offsets, &id, 0);
120         if (r)
121                 throw std::invalid_argument("foo");
123         set_id(id);
126 void DumbFramebuffer::Destroy()
128         /* delete framebuffer */
129         drmModeRmFB(card().fd(), id());
131         for (uint i = 0; i < m_num_planes; ++i) {
132                 FramebufferPlane& plane = m_planes[i];
134                 /* unmap buffer */
135                 munmap(plane.map, plane.size);
137                 /* delete dumb buffer */
138                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
139                 dreq.handle = plane.handle;
140                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
142         }
145 void DumbFramebuffer::clear()
147         for (unsigned i = 0; i < m_num_planes; ++i)
148                 memset(m_planes[i].map, 0, m_planes[i].size);