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