• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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>
12 
13 #include <kms++/kms++.h>
14 #include <kms++/omap/omapkms++.h>
15 
16 extern "C" {
17 #include <omap_drmif.h>
18 }
19 
20 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
21 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
22 #define PAGE_SIZE 4096
23 
24 using namespace std;
25 
26 namespace kms
27 {
28 
OmapFramebuffer(OmapCard & card,uint32_t width,uint32_t height,const string & fourcc,Flags flags)29 OmapFramebuffer::OmapFramebuffer(OmapCard& card, uint32_t width, uint32_t height, const string& fourcc, Flags flags)
30 	: OmapFramebuffer(card, width, height, FourCCToPixelFormat(fourcc), flags)
31 {
32 }
33 
OmapFramebuffer(OmapCard & card,uint32_t width,uint32_t height,PixelFormat format,Flags flags)34 OmapFramebuffer::OmapFramebuffer(OmapCard& card, uint32_t width, uint32_t height, PixelFormat format, Flags flags)
35 	:Framebuffer(card, width, height), m_omap_card(card), m_format(format)
36 {
37 	Create(flags);
38 }
39 
~OmapFramebuffer()40 OmapFramebuffer::~OmapFramebuffer()
41 {
42 	Destroy();
43 }
44 
Create(Flags buffer_flags)45 void OmapFramebuffer::Create(Flags buffer_flags)
46 {
47 	const PixelFormatInfo& format_info = get_pixel_format_info(m_format);
48 
49 	m_num_planes = format_info.num_planes;
50 
51 	for (int i = 0; i < format_info.num_planes; ++i) {
52 		const PixelFormatPlaneInfo& pi = format_info.planes[i];
53 		FramebufferPlane& plane = m_planes[i];
54 
55 		uint32_t flags = OMAP_BO_SCANOUT | OMAP_BO_WC;
56 
57 #if defined(OMAP_BO_MEM_CONTIG)
58 		if (buffer_flags & Flags::MemContig)
59 			flags |= OMAP_BO_MEM_CONTIG;
60 		if (buffer_flags & Flags::MemTiler)
61 			flags |= OMAP_BO_MEM_TILER;
62 		if (buffer_flags & Flags::MemPin)
63 			flags |= OMAP_BO_MEM_PIN;
64 #endif
65 
66 		struct omap_bo* bo;
67 
68 		uint32_t stride;
69 
70 		if (!(buffer_flags & Flags::Tiled)) {
71 			stride = width() * pi.bitspp / 8;
72 
73 			uint32_t size = stride * height() / pi.ysub;
74 
75 			bo = omap_bo_new(m_omap_card.dev(), size, flags);
76 			if (!bo)
77 				throw invalid_argument(string("omap_bo_new failed: ") + strerror(errno));
78 		} else {
79 			unsigned bitspertiler;
80 
81 			switch (m_format) {
82 			case PixelFormat::NV12:
83 				bitspertiler = i == 0 ? 8 : 16; break;
84 			case PixelFormat::YUYV:
85 			case PixelFormat::UYVY:
86 				bitspertiler = 32; break;
87 			case PixelFormat::ARGB8888:
88 			case PixelFormat::XRGB8888:
89 				bitspertiler = 32; break;
90 			case PixelFormat::RGB565:
91 				bitspertiler = 16; break;
92 			default:
93 				throw invalid_argument("unimplemented format");
94 			}
95 
96 			switch (bitspertiler) {
97 			case 8: flags |= OMAP_BO_TILED_8; break;
98 			case 16: flags |= OMAP_BO_TILED_16; break;
99 			case 32: flags |= OMAP_BO_TILED_32; break;
100 			default:
101 				throw invalid_argument("bad bitspertiler");
102 			}
103 
104 			uint32_t width_tiler = width() * pi.bitspp / bitspertiler;
105 
106 			bo = omap_bo_new_tiled(m_omap_card.dev(), width_tiler, height(), flags);
107 			if (!bo)
108 				throw invalid_argument(string("omap_bo_new_tiled failed: ") + strerror(errno));
109 
110 			stride = round_up(width() * pi.bitspp / 8, PAGE_SIZE);
111 		}
112 
113 		plane.omap_bo = bo;
114 		plane.handle = omap_bo_handle(bo);
115 		plane.stride = stride;
116 		plane.size = omap_bo_size(bo);
117 		plane.offset = 0;
118 		plane.map = 0;
119 		plane.prime_fd = -1;
120 	}
121 
122 	/* create framebuffer object */
123 	uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
124 	uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
125 	uint32_t offsets[4] = { m_planes[0].offset, m_planes[1].offset };
126 	uint32_t id;
127 	int r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
128 			  bo_handles, pitches, offsets, &id, 0);
129 	if (r)
130 		throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
131 
132 	set_id(id);
133 }
134 
Destroy()135 void OmapFramebuffer::Destroy()
136 {
137 	/* delete framebuffer */
138 	drmModeRmFB(card().fd(), id());
139 
140 	for (uint i = 0; i < m_num_planes; ++i) {
141 		FramebufferPlane& plane = m_planes[i];
142 
143 		/* unmap buffer */
144 		if (plane.map)
145 			munmap(plane.map, plane.size);
146 
147 		omap_bo_del(plane.omap_bo);
148 
149 		if (plane.prime_fd >= 0)
150 			::close(plane.prime_fd);
151 	}
152 }
153 
map(unsigned plane)154 uint8_t* OmapFramebuffer::map(unsigned plane)
155 {
156 	FramebufferPlane& p = m_planes[plane];
157 
158 	if (p.map)
159 		return p.map;
160 
161 	p.map = (uint8_t*)omap_bo_map(p.omap_bo);
162 	if (p.map == MAP_FAILED)
163 		throw invalid_argument(string("mmap failed: ") + strerror(errno));
164 
165 	return p.map;
166 }
167 
prime_fd(unsigned int plane)168 int OmapFramebuffer::prime_fd(unsigned int plane)
169 {
170 	FramebufferPlane& p = m_planes[plane];
171 
172 	if (p.prime_fd >= 0)
173 		return p.prime_fd;
174 
175 	int fd = omap_bo_dmabuf(p.omap_bo);
176 	if (fd < 0)
177 		throw std::runtime_error("omap_bo_dmabuf failed\n");
178 
179 	p.prime_fd = fd;
180 
181 	return fd;
182 }
183 
184 }
185