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 { PixelFormat::NV21, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
63 /* RGB16 */
64 { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } },
65 /* RGB32 */
66 { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } },
67 { PixelFormat::XBGR8888, { 1, { { 32, 1, 1 } }, } },
68 };
70 void DumbFramebuffer::Create()
71 {
72 int r;
74 const FormatInfo& format_info = format_info_array.at(m_format);
76 m_num_planes = format_info.num_planes;
78 for (int i = 0; i < format_info.num_planes; ++i) {
79 const FormatPlaneInfo& pi = format_info.planes[i];
80 FramebufferPlane& plane = m_planes[i];
82 /* create dumb buffer */
83 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
84 creq.width = width() / pi.xsub;
85 creq.height = height() / pi.ysub;
86 creq.bpp = pi.bitspp;
87 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
88 if (r)
89 throw std::invalid_argument("foo");
91 plane.handle = creq.handle;
92 plane.stride = creq.pitch;
93 plane.size = creq.height * creq.pitch;
95 /*
96 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
97 i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
98 */
100 /* prepare buffer for memory mapping */
101 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
102 mreq.handle = plane.handle;
103 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
104 if (r)
105 throw std::invalid_argument("foo");
107 /* perform actual memory mapping */
108 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
109 card().fd(), mreq.offset);
110 if (plane.map == MAP_FAILED)
111 throw std::invalid_argument("foo");
113 /* clear the framebuffer to 0 */
114 memset(plane.map, 0, plane.size);
115 }
117 /* create framebuffer object for the dumb-buffer */
118 uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
119 uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
120 uint32_t offsets[4] = { 0 };
121 uint32_t id;
122 r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
123 bo_handles, pitches, offsets, &id, 0);
124 if (r)
125 throw std::invalid_argument("foo");
127 set_id(id);
128 }
130 void DumbFramebuffer::Destroy()
131 {
132 /* delete framebuffer */
133 drmModeRmFB(card().fd(), id());
135 for (uint i = 0; i < m_num_planes; ++i) {
136 FramebufferPlane& plane = m_planes[i];
138 /* unmap buffer */
139 munmap(plane.map, plane.size);
141 /* delete dumb buffer */
142 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
143 dreq.handle = plane.handle;
144 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
146 }
147 }
149 void DumbFramebuffer::clear()
150 {
151 for (unsigned i = 0; i < m_num_planes; ++i)
152 memset(m_planes[i].map, 0, m_planes[i].size);
153 }
155 }