• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
22 
DumbFramebuffer(Card & card,uint32_t width,uint32_t height,const string & fourcc)23 DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const string& fourcc)
24 	:DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
25 {
26 }
27 
DumbFramebuffer(Card & card,uint32_t width,uint32_t height,PixelFormat format)28 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
29 	:Framebuffer(card, width, height), m_format(format)
30 {
31 	Create();
32 }
33 
~DumbFramebuffer()34 DumbFramebuffer::~DumbFramebuffer()
35 {
36 	Destroy();
37 }
38 
Create()39 void DumbFramebuffer::Create()
40 {
41 	int r;
42 
43 	const PixelFormatInfo& format_info = get_pixel_format_info(m_format);
44 
45 	m_num_planes = format_info.num_planes;
46 
47 	for (int i = 0; i < format_info.num_planes; ++i) {
48 		const PixelFormatPlaneInfo& pi = format_info.planes[i];
49 		FramebufferPlane& plane = m_planes[i];
50 
51 		/* create dumb buffer */
52 		struct drm_mode_create_dumb creq = drm_mode_create_dumb();
53 		creq.width = width();
54 		creq.height = height() / pi.ysub;
55 		creq.bpp = pi.bitspp;
56 		r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
57 		if (r)
58 			throw invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed: ") + strerror(errno));
59 
60 		plane.handle = creq.handle;
61 		plane.stride = creq.pitch;
62 		plane.size = creq.height * creq.pitch;
63 		plane.offset = 0;
64 		plane.map = 0;
65 		plane.prime_fd = -1;
66 	}
67 
68 	/* create framebuffer object for the dumb-buffer */
69 	uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
70 	uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
71 	uint32_t offsets[4] = {  m_planes[0].offset, m_planes[1].offset };
72 	uint32_t id;
73 	r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
74 			  bo_handles, pitches, offsets, &id, 0);
75 	if (r)
76 		throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
77 
78 	set_id(id);
79 }
80 
Destroy()81 void DumbFramebuffer::Destroy()
82 {
83 	/* delete framebuffer */
84 	drmModeRmFB(card().fd(), id());
85 
86 	for (uint i = 0; i < m_num_planes; ++i) {
87 		FramebufferPlane& plane = m_planes[i];
88 
89 		/* unmap buffer */
90 		if (plane.map)
91 			munmap(plane.map, plane.size);
92 
93 		/* delete dumb buffer */
94 		struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
95 		dreq.handle = plane.handle;
96 		drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
97 		if (plane.prime_fd >= 0)
98 			::close(plane.prime_fd);
99 	}
100 }
101 
map(unsigned plane)102 uint8_t* DumbFramebuffer::map(unsigned plane)
103 {
104 	FramebufferPlane& p = m_planes[plane];
105 
106 	if (p.map)
107 		return p.map;
108 
109 	/* prepare buffer for memory mapping */
110 	struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
111 	mreq.handle = p.handle;
112 	int r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
113 	if (r)
114 		throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed: ") + strerror(errno));
115 
116 	/* perform actual memory mapping */
117 	p.map = (uint8_t *)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
118 					  card().fd(), mreq.offset);
119 	if (p.map == MAP_FAILED)
120 		throw invalid_argument(string("mmap failed: ") + strerror(errno));
121 
122 	return p.map;
123 }
124 
prime_fd(unsigned int plane)125 int DumbFramebuffer::prime_fd(unsigned int plane)
126 {
127 	if (m_planes[plane].prime_fd >= 0)
128 		return m_planes[plane].prime_fd;
129 
130 	int r = drmPrimeHandleToFD(card().fd(), m_planes[plane].handle,
131 				   DRM_CLOEXEC | O_RDWR, &m_planes[plane].prime_fd);
132 	if (r)
133 		throw std::runtime_error("drmPrimeHandleToFD failed");
134 
135 	return m_planes[plane].prime_fd;
136 }
137 
138 }
139