1 #include <chrono>
2 #include <cstdio>
3 #include <vector>
4 #include <memory>
5 #include <algorithm>
6 #include <poll.h>
8 #include <xf86drm.h>
9 #include <xf86drmMode.h>
10 #include <gbm.h>
12 #include <kms++/kms++.h>
13 #include <kms++util.h>
14 #include "cube-egl.h"
15 #include "cube-gles2.h"
17 using namespace kms;
18 using namespace std;
20 static int s_flip_pending;
21 static bool s_need_exit;
23 static bool s_support_planes;
25 class GbmDevice
26 {
27 public:
28 GbmDevice(Card& card)
29 {
30 m_dev = gbm_create_device(card.fd());
31 FAIL_IF(!m_dev, "failed to create gbm device");
32 }
34 ~GbmDevice()
35 {
36 gbm_device_destroy(m_dev);
37 }
39 GbmDevice(const GbmDevice& other) = delete;
40 GbmDevice& operator=(const GbmDevice& other) = delete;
42 struct gbm_device* handle() const { return m_dev; }
44 private:
45 struct gbm_device* m_dev;
46 };
48 class GbmSurface
49 {
50 public:
51 GbmSurface(GbmDevice& gdev, int width, int height)
52 {
53 m_surface = gbm_surface_create(gdev.handle(), width, height,
54 GBM_FORMAT_XRGB8888,
55 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
56 FAIL_IF(!m_surface, "failed to create gbm surface");
57 }
59 ~GbmSurface()
60 {
61 gbm_surface_destroy(m_surface);
62 }
64 GbmSurface(const GbmSurface& other) = delete;
65 GbmSurface& operator=(const GbmSurface& other) = delete;
67 bool has_free()
68 {
69 return gbm_surface_has_free_buffers(m_surface);
70 }
72 gbm_bo* lock_front_buffer()
73 {
74 return gbm_surface_lock_front_buffer(m_surface);
75 }
77 void release_buffer(gbm_bo *bo)
78 {
79 gbm_surface_release_buffer(m_surface, bo);
80 }
82 struct gbm_surface* handle() const { return m_surface; }
84 private:
85 struct gbm_surface* m_surface;
86 };
88 class GbmEglSurface
89 {
90 public:
91 GbmEglSurface(Card& card, GbmDevice& gdev, const EglState& egl, int width, int height)
92 : card(card), egl(egl), m_width(width), m_height(height),
93 bo_prev(0), bo_next(0)
94 {
95 gsurface = unique_ptr<GbmSurface>(new GbmSurface(gdev, width, height));
96 esurface = eglCreateWindowSurface(egl.display(), egl.config(), gsurface->handle(), NULL);
97 FAIL_IF(esurface == EGL_NO_SURFACE, "failed to create egl surface");
98 }
100 ~GbmEglSurface()
101 {
102 if (bo_next)
103 gsurface->release_buffer(bo_next);
104 eglDestroySurface(egl.display(), esurface);
105 }
107 void make_current()
108 {
109 FAIL_IF(!gsurface->has_free(), "No free buffers");
111 eglMakeCurrent(egl.display(), esurface, esurface, egl.context());
112 }
114 void swap_buffers()
115 {
116 eglSwapBuffers(egl.display(), esurface);
117 }
119 static void drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
120 {
121 auto fb = reinterpret_cast<Framebuffer*>(data);
122 delete fb;
123 }
125 static Framebuffer* drm_fb_get_from_bo(struct gbm_bo *bo, Card& card)
126 {
127 auto fb = reinterpret_cast<Framebuffer*>(gbm_bo_get_user_data(bo));
128 if (fb)
129 return fb;
131 uint32_t width = gbm_bo_get_width(bo);
132 uint32_t height = gbm_bo_get_height(bo);
133 uint32_t stride = gbm_bo_get_stride(bo);
134 uint32_t handle = gbm_bo_get_handle(bo).u32;
136 fb = new ExtFramebuffer(card, width, height, 24, 32, stride, handle);
138 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
140 return fb;
141 }
143 Framebuffer* lock_next()
144 {
145 bo_prev = bo_next;
146 bo_next = gsurface->lock_front_buffer();
147 FAIL_IF(!bo_next, "could not lock gbm buffer");
148 return drm_fb_get_from_bo(bo_next, card);
149 }
151 void free_prev()
152 {
153 if (bo_prev) {
154 gsurface->release_buffer(bo_prev);
155 bo_prev = 0;
156 }
157 }
159 uint32_t width() const { return m_width; }
160 uint32_t height() const { return m_height; }
162 private:
163 Card& card;
164 const EglState& egl;
166 unique_ptr<GbmSurface> gsurface;
167 EGLSurface esurface;
169 int m_width;
170 int m_height;
172 struct gbm_bo* bo_prev;
173 struct gbm_bo* bo_next;
174 };
176 class OutputHandler : private PageFlipHandlerBase
177 {
178 public:
179 OutputHandler(Card& card, GbmDevice& gdev, const EglState& egl, Connector* connector, Crtc* crtc, Videomode& mode, Plane* plane, float rotation_mult)
180 : m_frame_num(0), m_connector(connector), m_crtc(crtc), m_plane(plane), m_mode(mode),
181 m_rotation_mult(rotation_mult)
182 {
183 m_surface1 = unique_ptr<GbmEglSurface>(new GbmEglSurface(card, gdev, egl, mode.hdisplay, mode.vdisplay));
184 m_scene1 = unique_ptr<GlScene>(new GlScene());
185 m_scene1->set_viewport(m_surface1->width(), m_surface1->height());
187 if (m_plane) {
188 m_surface2 = unique_ptr<GbmEglSurface>(new GbmEglSurface(card, gdev, egl, 400, 400));
189 m_scene2 = unique_ptr<GlScene>(new GlScene());
190 m_scene2->set_viewport(m_surface2->width(), m_surface2->height());
191 }
192 }
194 OutputHandler(const OutputHandler& other) = delete;
195 OutputHandler& operator=(const OutputHandler& other) = delete;
197 void setup()
198 {
199 int ret;
201 m_surface1->make_current();
202 m_surface1->swap_buffers();
203 Framebuffer* fb = m_surface1->lock_next();
205 Framebuffer* planefb = 0;
207 if (m_plane) {
208 m_surface2->make_current();
209 m_surface2->swap_buffers();
210 planefb = m_surface2->lock_next();
211 }
214 ret = m_crtc->set_mode(m_connector, *fb, m_mode);
215 FAIL_IF(ret, "failed to set mode");
217 if (m_crtc->card().has_atomic()) {
218 Plane* root_plane = 0;
219 for (Plane* p : m_crtc->get_possible_planes()) {
220 if (p->crtc_id() == m_crtc->id()) {
221 root_plane = p;
222 break;
223 }
224 }
226 FAIL_IF(!root_plane, "No primary plane for crtc %d", m_crtc->id());
228 m_root_plane = root_plane;
229 }
231 if (m_plane) {
232 ret = m_crtc->set_plane(m_plane, *planefb,
233 0, 0, planefb->width(), planefb->height(),
234 0, 0, planefb->width(), planefb->height());
235 FAIL_IF(ret, "failed to set plane");
236 }
237 }
239 void start_flipping()
240 {
241 m_t1 = chrono::steady_clock::now();
242 queue_next();
243 }
245 private:
246 void handle_page_flip(uint32_t frame, double time)
247 {
248 ++m_frame_num;
250 if (m_frame_num % 100 == 0) {
251 auto t2 = chrono::steady_clock::now();
252 chrono::duration<float> fsec = t2 - m_t1;
253 printf("fps: %f\n", 100.0 / fsec.count());
254 m_t1 = t2;
255 }
257 s_flip_pending--;
259 m_surface1->free_prev();
260 if (m_plane)
261 m_surface2->free_prev();
263 if (s_need_exit)
264 return;
266 queue_next();
267 }
269 void queue_next()
270 {
271 m_surface1->make_current();
272 m_scene1->draw(m_frame_num * m_rotation_mult);
273 m_surface1->swap_buffers();
274 Framebuffer* fb = m_surface1->lock_next();
276 Framebuffer* planefb = 0;
278 if (m_plane) {
279 m_surface2->make_current();
280 m_scene2->draw(m_frame_num * m_rotation_mult * 2);
281 m_surface2->swap_buffers();
282 planefb = m_surface2->lock_next();
283 }
285 if (m_crtc->card().has_atomic()) {
286 int r;
288 AtomicReq req(m_crtc->card());
290 req.add(m_root_plane, "FB_ID", fb->id());
291 if (m_plane)
292 req.add(m_plane, "FB_ID", planefb->id());
294 r = req.test();
295 FAIL_IF(r, "atomic test failed");
297 r = req.commit(this);
298 FAIL_IF(r, "atomic commit failed");
299 } else {
300 int ret;
302 ret = m_crtc->page_flip(*fb, this);
303 FAIL_IF(ret, "failed to queue page flip");
305 if (m_plane) {
306 ret = m_crtc->set_plane(m_plane, *planefb,
307 0, 0, planefb->width(), planefb->height(),
308 0, 0, planefb->width(), planefb->height());
309 FAIL_IF(ret, "failed to set plane");
310 }
311 }
313 s_flip_pending++;
314 }
316 int m_frame_num;
317 chrono::steady_clock::time_point m_t1;
319 Connector* m_connector;
320 Crtc* m_crtc;
321 Plane* m_plane;
322 Videomode m_mode;
323 Plane* m_root_plane;
325 unique_ptr<GbmEglSurface> m_surface1;
326 unique_ptr<GbmEglSurface> m_surface2;
328 unique_ptr<GlScene> m_scene1;
329 unique_ptr<GlScene> m_scene2;
331 float m_rotation_mult;
332 };
334 void main_gbm()
335 {
336 Card card;
338 GbmDevice gdev(card);
339 EglState egl(gdev.handle());
341 vector<unique_ptr<OutputHandler>> outputs;
342 vector<Plane*> used_planes;
344 float rot_mult = 1;
346 for (auto pipe : card.get_connected_pipelines()) {
347 auto connector = pipe.connector;
348 auto crtc = pipe.crtc;
349 auto mode = connector->get_default_mode();
351 Plane* plane = 0;
353 if (s_support_planes) {
354 for (Plane* p : crtc->get_possible_planes()) {
355 if (find(used_planes.begin(), used_planes.end(), p) != used_planes.end())
356 continue;
358 if (p->plane_type() != PlaneType::Overlay)
359 continue;
361 plane = p;
362 break;
363 }
364 }
366 if (plane)
367 used_planes.push_back(plane);
369 auto out = new OutputHandler(card, gdev, egl, connector, crtc, mode, plane, rot_mult);
370 outputs.emplace_back(out);
372 rot_mult *= 1.33;
373 }
375 for (auto& out : outputs)
376 out->setup();
378 for (auto& out : outputs)
379 out->start_flipping();
381 struct pollfd fds[2] = { };
382 fds[0].fd = 0;
383 fds[0].events = POLLIN;
384 fds[1].fd = card.fd();
385 fds[1].events = POLLIN;
387 while (!s_need_exit || s_flip_pending) {
388 int r = poll(fds, ARRAY_SIZE(fds), -1);
389 FAIL_IF(r < 0, "poll error %d", r);
391 if (fds[0].revents)
392 s_need_exit = true;
394 if (fds[1].revents)
395 card.call_page_flip_handlers();
396 }
397 }