1
2 #include <cstring>
3 #include <cerrno>
4 #include <stdexcept>
5 #include <sys/mman.h>
6 #include <xf86drm.h>
7 #include <xf86drmMode.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <drm_fourcc.h>
11 #include <drm.h>
12 #include <drm_mode.h>
13
14 #include <kms++/kms++.h>
15
16 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17
18 using namespace std;
19
20 namespace kms
21 {
DumbFramebuffer(Card & card,uint32_t width,uint32_t height,const string & fourcc)22 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, const string& fourcc)
23 : DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
24 {
25 }
26
DumbFramebuffer(Card & card,uint32_t width,uint32_t height,PixelFormat format)27 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
28 : Framebuffer(card, width, height), m_format(format)
29 {
30 int r;
31
32 const PixelFormatInfo& format_info = get_pixel_format_info(m_format);
33
34 m_num_planes = format_info.num_planes;
35
36 for (int i = 0; i < format_info.num_planes; ++i) {
37 const PixelFormatPlaneInfo& pi = format_info.planes[i];
38 FramebufferPlane& plane = m_planes.at(i);
39
40 /* create dumb buffer */
41 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
42 creq.width = width;
43 creq.height = height / pi.ysub;
44 /*
45 * For fully planar YUV buffers, the chroma planes don't combine
46 * U and V components, their width must thus be divided by the
47 * horizontal subsampling factor.
48 */
49 if (format_info.type == PixelColorType::YUV &&
50 format_info.num_planes == 3)
51 creq.width /= pi.xsub;
52 creq.bpp = pi.bitspp;
53 r = drmIoctl(card.fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
54 if (r)
55 throw invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed: ") + strerror(errno));
56
57 plane.handle = creq.handle;
58 plane.stride = creq.pitch;
59 plane.size = creq.height * creq.pitch;
60 plane.offset = 0;
61 plane.map = 0;
62 plane.prime_fd = -1;
63 }
64
65 /* create framebuffer object for the dumb-buffer */
66 uint32_t bo_handles[4] = {
67 m_planes[0].handle,
68 m_planes[1].handle,
69 m_planes[2].handle,
70 m_planes[3].handle,
71 };
72 uint32_t pitches[4] = {
73 m_planes[0].stride,
74 m_planes[1].stride,
75 m_planes[2].stride,
76 m_planes[3].stride,
77 };
78 uint32_t offsets[4] = {
79 m_planes[0].offset,
80 m_planes[1].offset,
81 m_planes[2].offset,
82 m_planes[3].offset,
83 };
84 uint32_t id;
85 r = drmModeAddFB2(card.fd(), width, height, (uint32_t)format,
86 bo_handles, pitches, offsets, &id, 0);
87 if (r)
88 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
89
90 set_id(id);
91 }
92
~DumbFramebuffer()93 DumbFramebuffer::~DumbFramebuffer()
94 {
95 /* delete framebuffer */
96 drmModeRmFB(card().fd(), id());
97
98 for (uint i = 0; i < m_num_planes; ++i) {
99 FramebufferPlane& plane = m_planes.at(i);
100
101 /* unmap buffer */
102 if (plane.map)
103 munmap(plane.map, plane.size);
104
105 /* delete dumb buffer */
106 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
107 dreq.handle = plane.handle;
108 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
109 if (plane.prime_fd >= 0)
110 ::close(plane.prime_fd);
111 }
112 }
113
map(unsigned plane)114 uint8_t* DumbFramebuffer::map(unsigned plane)
115 {
116 FramebufferPlane& p = m_planes.at(plane);
117
118 if (p.map)
119 return p.map;
120
121 /* prepare buffer for memory mapping */
122 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
123 mreq.handle = p.handle;
124 int r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
125 if (r)
126 throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed: ") + strerror(errno));
127
128 /* perform actual memory mapping */
129 p.map = (uint8_t*)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
130 card().fd(), mreq.offset);
131 if (p.map == MAP_FAILED)
132 throw invalid_argument(string("mmap failed: ") + strerror(errno));
133
134 return p.map;
135 }
136
prime_fd(unsigned int plane)137 int DumbFramebuffer::prime_fd(unsigned int plane)
138 {
139 if (m_planes.at(plane).prime_fd >= 0)
140 return m_planes.at(plane).prime_fd;
141
142 int r = drmPrimeHandleToFD(card().fd(), m_planes.at(plane).handle,
143 DRM_CLOEXEC | O_RDWR, &m_planes.at(plane).prime_fd);
144 if (r)
145 throw std::runtime_error("drmPrimeHandleToFD failed");
146
147 return m_planes.at(plane).prime_fd;
148 }
149
150 } // namespace kms
151