2 #include <cstring>
3 #include <stdexcept>
4 #include <sys/mman.h>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <drm_fourcc.h>
10 #include <drm.h>
11 #include <drm_mode.h>
13 #include "kms++.h"
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 using namespace std;
19 namespace kms
20 {
22 DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const string& fourcc)
23 :DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
24 {
25 }
27 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
28 :Framebuffer(card, width, height), m_format(format)
29 {
30 Create();
31 }
33 DumbFramebuffer::~DumbFramebuffer()
34 {
35 Destroy();
36 }
38 uint32_t DumbFramebuffer::prime_fd(unsigned int plane)
39 {
40 if (m_planes[plane].prime_fd >= 0)
41 return m_planes[plane].prime_fd;
43 int r = drmPrimeHandleToFD(card().fd(), m_planes[plane].handle,
44 DRM_CLOEXEC, &m_planes[plane].prime_fd);
45 if (r)
46 throw std::runtime_error("drmPrimeHandleToFD failed\n");
48 return m_planes[plane].prime_fd;
49 }
51 struct FormatPlaneInfo
52 {
53 uint8_t bitspp; /* bits per (macro) pixel */
54 uint8_t xsub;
55 uint8_t ysub;
56 };
58 struct FormatInfo
59 {
60 uint8_t num_planes;
61 struct FormatPlaneInfo planes[4];
62 };
64 static const map<PixelFormat, FormatInfo> format_info_array = {
65 /* YUV packed */
66 { PixelFormat::UYVY, { 1, { { 32, 2, 1 } }, } },
67 { PixelFormat::YUYV, { 1, { { 32, 2, 1 } }, } },
68 { PixelFormat::YVYU, { 1, { { 32, 2, 1 } }, } },
69 { PixelFormat::VYUY, { 1, { { 32, 2, 1 } }, } },
70 /* YUV semi-planar */
71 { PixelFormat::NV12, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
72 { PixelFormat::NV21, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
73 /* RGB16 */
74 { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } },
75 /* RGB32 */
76 { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } },
77 { PixelFormat::XBGR8888, { 1, { { 32, 1, 1 } }, } },
78 { PixelFormat::ARGB8888, { 1, { { 32, 1, 1 } }, } },
79 { PixelFormat::ABGR8888, { 1, { { 32, 1, 1 } }, } },
80 };
82 void DumbFramebuffer::Create()
83 {
84 int r;
86 const FormatInfo& format_info = format_info_array.at(m_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 invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed") + strerror(errno));
103 plane.handle = creq.handle;
104 plane.stride = creq.pitch;
105 plane.size = creq.height * creq.pitch;
106 plane.offset = 0;
108 /*
109 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
110 i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
111 */
113 /* prepare buffer for memory mapping */
114 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
115 mreq.handle = plane.handle;
116 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
117 if (r)
118 throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed") + strerror(errno));
120 /* perform actual memory mapping */
121 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
122 card().fd(), mreq.offset);
123 if (plane.map == MAP_FAILED)
124 throw invalid_argument(string("mmap failed: ") + strerror(errno));
126 /* clear the framebuffer to 0 */
127 memset(plane.map, 0, plane.size);
129 plane.prime_fd = -1;
130 }
132 /* create framebuffer object for the dumb-buffer */
133 uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
134 uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
135 uint32_t offsets[4] = { m_planes[0].offset, m_planes[1].offset };
136 uint32_t id;
137 r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
138 bo_handles, pitches, offsets, &id, 0);
139 if (r)
140 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
142 set_id(id);
143 }
145 void DumbFramebuffer::Destroy()
146 {
147 /* delete framebuffer */
148 drmModeRmFB(card().fd(), id());
150 for (uint i = 0; i < m_num_planes; ++i) {
151 FramebufferPlane& plane = m_planes[i];
153 /* unmap buffer */
154 munmap(plane.map, plane.size);
156 /* delete dumb buffer */
157 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
158 dreq.handle = plane.handle;
159 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
160 if (plane.prime_fd >= 0)
161 ::close(plane.prime_fd);
162 }
163 }
165 void DumbFramebuffer::clear()
166 {
167 for (unsigned i = 0; i < m_num_planes; ++i)
168 memset(m_planes[i].map, 0, m_planes[i].size);
169 }
171 }