f422081536acb883d65fc767e7cef65698ab0391
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 { PixelFormat::YVYU, { 1, { { 32, 2, 1 } }, } },
59 { PixelFormat::VYUY, { 1, { { 32, 2, 1 } }, } },
60 /* YUV semi-planar */
61 { PixelFormat::NV12, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
62 /* RGB16 */
63 { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } },
64 /* RGB32 */
65 { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } },
66 { PixelFormat::XBGR8888, { 1, { { 32, 1, 1 } }, } },
67 };
69 void DumbFramebuffer::Create()
70 {
71 int r;
73 const FormatInfo& format_info = format_info_array.at(m_format);
75 m_num_planes = format_info.num_planes;
77 for (int i = 0; i < format_info.num_planes; ++i) {
78 const FormatPlaneInfo& pi = format_info.planes[i];
79 FramebufferPlane& plane = m_planes[i];
81 /* create dumb buffer */
82 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
83 creq.width = width() / pi.xsub;
84 creq.height = height() / pi.ysub;
85 creq.bpp = pi.bitspp;
86 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
87 if (r)
88 throw std::invalid_argument("foo");
90 plane.handle = creq.handle;
91 plane.stride = creq.pitch;
92 plane.size = creq.height * creq.pitch;
94 /*
95 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
96 i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
97 */
99 /* prepare buffer for memory mapping */
100 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
101 mreq.handle = plane.handle;
102 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
103 if (r)
104 throw std::invalid_argument("foo");
106 /* perform actual memory mapping */
107 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
108 card().fd(), mreq.offset);
109 if (plane.map == MAP_FAILED)
110 throw std::invalid_argument("foo");
112 /* clear the framebuffer to 0 */
113 memset(plane.map, 0, plane.size);
114 }
116 /* create framebuffer object for the dumb-buffer */
117 uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
118 uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
119 uint32_t offsets[4] = { 0 };
120 uint32_t id;
121 r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
122 bo_handles, pitches, offsets, &id, 0);
123 if (r)
124 throw std::invalid_argument("foo");
126 set_id(id);
127 }
129 void DumbFramebuffer::Destroy()
130 {
131 /* delete framebuffer */
132 drmModeRmFB(card().fd(), id());
134 for (uint i = 0; i < m_num_planes; ++i) {
135 FramebufferPlane& plane = m_planes[i];
137 /* unmap buffer */
138 munmap(plane.map, plane.size);
140 /* delete dumb buffer */
141 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
142 dreq.handle = plane.handle;
143 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
145 }
146 }
148 void DumbFramebuffer::clear()
149 {
150 for (unsigned i = 0; i < m_num_planes; ++i)
151 memset(m_planes[i].map, 0, m_planes[i].size);
152 }
154 }