-
Notifications
You must be signed in to change notification settings - Fork 7
/
modeset-double-buffered.c
644 lines (558 loc) · 17.8 KB
/
modeset-double-buffered.c
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* modeset - DRM Double-Buffered Modesetting Example
*
* Written 2012 by David Herrmann <dh.herrmann@googlemail.com>
* Dedicated to the Public Domain.
*/
/*
* DRM Double-Buffered Modesetting Howto
* This example extends the modeset.c howto and introduces double-buffering.
* When drawing a new frame into a framebuffer, we should always draw into an
* unused buffer and not into the front buffer. If we draw into the front
* buffer, we might have drawn half the frame when the display-controller starts
* scanning out the next frame. Hence, we see flickering on the screen.
* The technique to avoid this is called double-buffering. We have two
* framebuffers, the front buffer which is currently used for scanout and a
* back-buffer that is used for drawing operations. When a frame is done, we
* simply swap both buffers.
* Swapping does not mean copying data, instead, only the pointers to the
* buffers are swapped.
*
* Please read modeset.c before reading this file as most of the functions stay
* the same. Only the differences are highlighted here.
* Also note that triple-buffering or any other number of buffers can be easily
* implemented by following the scheme here. However, in this example we limit
* the number of buffers to 2 so it is easier to follow.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
struct modeset_buf;
struct modeset_dev;
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
struct modeset_dev *dev);
static int modeset_create_fb(int fd, struct modeset_buf *buf);
static void modeset_destroy_fb(int fd, struct modeset_buf *buf);
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
struct modeset_dev *dev);
static int modeset_open(int *out, const char *node);
static int modeset_prepare(int fd);
static void modeset_draw(int fd);
static void modeset_cleanup(int fd);
/*
* modeset_open() stays the same as before.
*/
static int modeset_open(int *out, const char *node)
{
int fd, ret;
uint64_t has_dumb;
fd = open(node, O_RDWR | O_CLOEXEC);
if (fd < 0) {
ret = -errno;
fprintf(stderr, "cannot open '%s': %m\n", node);
return ret;
}
if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 ||
!has_dumb) {
fprintf(stderr, "drm device '%s' does not support dumb buffers\n",
node);
close(fd);
return -EOPNOTSUPP;
}
*out = fd;
return 0;
}
/*
* Previously, we used the modeset_dev objects to hold buffer informations, too.
* Technically, we could have split them but avoided this to make the
* example simpler.
* However, in this example we need 2 buffers. One back buffer and one front
* buffer. So we introduce a new structure modeset_buf which contains everything
* related to a single buffer. Each device now gets an array of two of these
* buffers.
* Each buffer consists of width, height, stride, size, handle, map and fb-id.
* They have the same meaning as before.
*
* Each device also gets a new integer field: front_buf. This field contains the
* index of the buffer that is currently used as front buffer / scanout buffer.
* In our example it can be 0 or 1. We flip it by using XOR:
* dev->front_buf ^= dev->front_buf
*
* Everything else stays the same.
*/
struct modeset_buf {
uint32_t width;
uint32_t height;
uint32_t stride;
uint32_t size;
uint32_t handle;
uint8_t *map;
uint32_t fb;
};
struct modeset_dev {
struct modeset_dev *next;
unsigned int front_buf;
struct modeset_buf bufs[2];
drmModeModeInfo mode;
uint32_t conn;
uint32_t crtc;
drmModeCrtc *saved_crtc;
};
static struct modeset_dev *modeset_list = NULL;
/*
* modeset_prepare() stays the same.
*/
static int modeset_prepare(int fd)
{
drmModeRes *res;
drmModeConnector *conn;
unsigned int i;
struct modeset_dev *dev;
int ret;
/* retrieve resources */
res = drmModeGetResources(fd);
if (!res) {
fprintf(stderr, "cannot retrieve DRM resources (%d): %m\n",
errno);
return -errno;
}
/* iterate all connectors */
for (i = 0; i < res->count_connectors; ++i) {
/* get information for each connector */
conn = drmModeGetConnector(fd, res->connectors[i]);
if (!conn) {
fprintf(stderr, "cannot retrieve DRM connector %u:%u (%d): %m\n",
i, res->connectors[i], errno);
continue;
}
/* create a device structure */
dev = malloc(sizeof(*dev));
memset(dev, 0, sizeof(*dev));
dev->conn = conn->connector_id;
/* call helper function to prepare this connector */
ret = modeset_setup_dev(fd, res, conn, dev);
if (ret) {
if (ret != -ENOENT) {
errno = -ret;
fprintf(stderr, "cannot setup device for connector %u:%u (%d): %m\n",
i, res->connectors[i], errno);
}
free(dev);
drmModeFreeConnector(conn);
continue;
}
/* free connector data and link device into global list */
drmModeFreeConnector(conn);
dev->next = modeset_list;
modeset_list = dev;
}
/* free resources again */
drmModeFreeResources(res);
return 0;
}
/*
* modeset_setup_dev() sets up all resources for a single device. It mostly
* stays the same, but one thing changes: We allocate two framebuffers instead
* of one. That is, we call modeset_create_fb() twice.
* We also copy the width/height information into both framebuffers so
* modeset_create_fb() can use them without requiring a pointer to modeset_dev.
*/
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
struct modeset_dev *dev)
{
int ret;
/* check if a monitor is connected */
if (conn->connection != DRM_MODE_CONNECTED) {
fprintf(stderr, "ignoring unused connector %u\n",
conn->connector_id);
return -ENOENT;
}
/* check if there is at least one valid mode */
if (conn->count_modes == 0) {
fprintf(stderr, "no valid mode for connector %u\n",
conn->connector_id);
return -EFAULT;
}
/* copy the mode information into our device structure and into both
* buffers */
memcpy(&dev->mode, &conn->modes[0], sizeof(dev->mode));
dev->bufs[0].width = conn->modes[0].hdisplay;
dev->bufs[0].height = conn->modes[0].vdisplay;
dev->bufs[1].width = conn->modes[0].hdisplay;
dev->bufs[1].height = conn->modes[0].vdisplay;
fprintf(stderr, "mode for connector %u is %ux%u\n",
conn->connector_id, dev->bufs[0].width, dev->bufs[0].height);
/* find a crtc for this connector */
ret = modeset_find_crtc(fd, res, conn, dev);
if (ret) {
fprintf(stderr, "no valid crtc for connector %u\n",
conn->connector_id);
return ret;
}
/* create framebuffer #1 for this CRTC */
ret = modeset_create_fb(fd, &dev->bufs[0]);
if (ret) {
fprintf(stderr, "cannot create framebuffer for connector %u\n",
conn->connector_id);
return ret;
}
/* create framebuffer #2 for this CRTC */
ret = modeset_create_fb(fd, &dev->bufs[1]);
if (ret) {
fprintf(stderr, "cannot create framebuffer for connector %u\n",
conn->connector_id);
modeset_destroy_fb(fd, &dev->bufs[0]);
return ret;
}
return 0;
}
/*
* modeset_find_crtc() stays the same.
*/
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
struct modeset_dev *dev)
{
drmModeEncoder *enc;
unsigned int i, j;
int32_t crtc;
struct modeset_dev *iter;
/* first try the currently conected encoder+crtc */
if (conn->encoder_id)
enc = drmModeGetEncoder(fd, conn->encoder_id);
else
enc = NULL;
if (enc) {
if (enc->crtc_id) {
crtc = enc->crtc_id;
for (iter = modeset_list; iter; iter = iter->next) {
if (iter->crtc == crtc) {
crtc = -1;
break;
}
}
if (crtc >= 0) {
drmModeFreeEncoder(enc);
dev->crtc = crtc;
return 0;
}
}
drmModeFreeEncoder(enc);
}
/* If the connector is not currently bound to an encoder or if the
* encoder+crtc is already used by another connector (actually unlikely
* but lets be safe), iterate all other available encoders to find a
* matching CRTC. */
for (i = 0; i < conn->count_encoders; ++i) {
enc = drmModeGetEncoder(fd, conn->encoders[i]);
if (!enc) {
fprintf(stderr, "cannot retrieve encoder %u:%u (%d): %m\n",
i, conn->encoders[i], errno);
continue;
}
/* iterate all global CRTCs */
for (j = 0; j < res->count_crtcs; ++j) {
/* check whether this CRTC works with the encoder */
if (!(enc->possible_crtcs & (1 << j)))
continue;
/* check that no other device already uses this CRTC */
crtc = res->crtcs[j];
for (iter = modeset_list; iter; iter = iter->next) {
if (iter->crtc == crtc) {
crtc = -1;
break;
}
}
/* we have found a CRTC, so save it and return */
if (crtc >= 0) {
drmModeFreeEncoder(enc);
dev->crtc = crtc;
return 0;
}
}
drmModeFreeEncoder(enc);
}
fprintf(stderr, "cannot find suitable CRTC for connector %u\n",
conn->connector_id);
return -ENOENT;
}
/*
* modeset_create_fb() is mostly the same as before. Buf instead of writing the
* fields of a modeset_dev, we now require a buffer pointer passed as @buf.
* Please note that buf->width and buf->height are initialized by
* modeset_setup_dev() so we can use them here.
*/
static int modeset_create_fb(int fd, struct modeset_buf *buf)
{
struct drm_mode_create_dumb creq;
struct drm_mode_destroy_dumb dreq;
struct drm_mode_map_dumb mreq;
int ret;
/* create dumb buffer */
memset(&creq, 0, sizeof(creq));
creq.width = buf->width;
creq.height = buf->height;
creq.bpp = 32;
ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
if (ret < 0) {
fprintf(stderr, "cannot create dumb buffer (%d): %m\n",
errno);
return -errno;
}
buf->stride = creq.pitch;
buf->size = creq.size;
buf->handle = creq.handle;
/* create framebuffer object for the dumb-buffer */
ret = drmModeAddFB(fd, buf->width, buf->height, 24, 32, buf->stride,
buf->handle, &buf->fb);
if (ret) {
fprintf(stderr, "cannot create framebuffer (%d): %m\n",
errno);
ret = -errno;
goto err_destroy;
}
/* prepare buffer for memory mapping */
memset(&mreq, 0, sizeof(mreq));
mreq.handle = buf->handle;
ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
if (ret) {
fprintf(stderr, "cannot map dumb buffer (%d): %m\n",
errno);
ret = -errno;
goto err_fb;
}
/* perform actual memory mapping */
buf->map = mmap(0, buf->size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, mreq.offset);
if (buf->map == MAP_FAILED) {
fprintf(stderr, "cannot mmap dumb buffer (%d): %m\n",
errno);
ret = -errno;
goto err_fb;
}
/* clear the framebuffer to 0 */
memset(buf->map, 0, buf->size);
return 0;
err_fb:
drmModeRmFB(fd, buf->fb);
err_destroy:
memset(&dreq, 0, sizeof(dreq));
dreq.handle = buf->handle;
drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
return ret;
}
/*
* modeset_destroy_fb() is a new function. It does exactly the reverse of
* modeset_create_fb() and destroys a single framebuffer. The modeset.c example
* used to do this directly in modeset_cleanup().
* We simply unmap the buffer, remove the drm-FB and destroy the memory buffer.
*/
static void modeset_destroy_fb(int fd, struct modeset_buf *buf)
{
struct drm_mode_destroy_dumb dreq;
/* unmap buffer */
munmap(buf->map, buf->size);
/* delete framebuffer */
drmModeRmFB(fd, buf->fb);
/* delete dumb buffer */
memset(&dreq, 0, sizeof(dreq));
dreq.handle = buf->handle;
drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
}
/*
* main() also stays almost exactly the same as before. We only need to change
* the way that we initially set the CRTCs. Instead of using the buffer
* information from modeset_dev, we now use dev->bufs[iter->front_buf] to get
* the current front-buffer and use this framebuffer for drmModeSetCrtc().
*/
int main(int argc, char **argv)
{
int ret, fd;
const char *card;
struct modeset_dev *iter;
struct modeset_buf *buf;
/* check which DRM device to open */
if (argc > 1)
card = argv[1];
else
card = "/dev/dri/card0";
fprintf(stderr, "using card '%s'\n", card);
/* open the DRM device */
ret = modeset_open(&fd, card);
if (ret)
goto out_return;
/* prepare all connectors and CRTCs */
ret = modeset_prepare(fd);
if (ret)
goto out_close;
/* perform actual modesetting on each found connector+CRTC */
for (iter = modeset_list; iter; iter = iter->next) {
iter->saved_crtc = drmModeGetCrtc(fd, iter->crtc);
buf = &iter->bufs[iter->front_buf];
ret = drmModeSetCrtc(fd, iter->crtc, buf->fb, 0, 0,
&iter->conn, 1, &iter->mode);
if (ret)
fprintf(stderr, "cannot set CRTC for connector %u (%d): %m\n",
iter->conn, errno);
}
/* draw some colors for 5seconds */
modeset_draw(fd);
/* cleanup everything */
modeset_cleanup(fd);
ret = 0;
out_close:
close(fd);
out_return:
if (ret) {
errno = -ret;
fprintf(stderr, "modeset failed with error %d: %m\n", errno);
} else {
fprintf(stderr, "exiting\n");
}
return ret;
}
/*
* A short helper function to compute a changing color value. No need to
* understand it.
*/
static uint8_t next_color(bool *up, uint8_t cur, unsigned int mod)
{
uint8_t next;
next = cur + (*up ? 1 : -1) * (rand() % mod);
if ((*up && next < cur) || (!*up && next > cur)) {
*up = !*up;
next = cur;
}
return next;
}
/*
* modeset_draw() is the place where things change. The render-logic is the same
* and we still draw a solid-color on the whole screen. However, we now have two
* buffers and need to flip between them.
*
* So before drawing into a framebuffer, we need to find the back-buffer.
* Remember, dev->font_buf is the index of the front buffer, so
* dev->front_buf ^ 1 is the index of the back buffer. We simply use
* dev->bufs[dev->front_buf ^ 1] to get the back-buffer and draw into it.
*
* After we finished drawing, we need to flip the buffers. We do this with the
* same call as we initially set the CRTC: drmModeSetCrtc(). However, we now
* pass the back-buffer as new framebuffer as we want to flip them.
* The only thing left to do is to change the dev->front_buf index to point to
* the new back-buffer (which was previously the front buffer).
* We then sleep for a short time period and start drawing again.
*
* If you run this example, you will notice that there is almost no flickering,
* anymore. The buffers are now swapped as a whole so each new frame shows
* always the whole new image. If you look carefully, you will notice that the
* modeset.c example showed many screen corruptions during redraw-cycles.
*
* However, this example is still not perfect. Imagine the display-controller is
* currently scanning out a new image and we call drmModeSetCrtc()
* simultaneously. It will then have the same effect as if we used a single
* buffer and we get some tearing. But, the chance that this happens is a lot
* less likely as with a single-buffer. This is because there is a long period
* between each frame called vertical-blank where the display-controller does
* not perform a scanout. If we swap the buffers in this period, we have the
* guarantee that there will be no tearing. See the modeset-vsync.c example if
* you want to know how you can guarantee that the swap takes place at a
* vertical-sync.
*/
static void modeset_draw(int fd)
{
uint8_t r, g, b;
bool r_up, g_up, b_up;
unsigned int i, j, k, off;
struct modeset_dev *iter;
struct modeset_buf *buf;
int ret;
srand(time(NULL));
r = rand() % 0xff;
g = rand() % 0xff;
b = rand() % 0xff;
r_up = g_up = b_up = true;
for (i = 0; i < 50; ++i) {
r = next_color(&r_up, r, 20);
g = next_color(&g_up, g, 10);
b = next_color(&b_up, b, 5);
for (iter = modeset_list; iter; iter = iter->next) {
buf = &iter->bufs[iter->front_buf ^ 1];
for (j = 0; j < buf->height; ++j) {
for (k = 0; k < buf->width; ++k) {
off = buf->stride * j + k * 4;
*(uint32_t*)&buf->map[off] =
(r << 16) | (g << 8) | b;
}
}
ret = drmModeSetCrtc(fd, iter->crtc, buf->fb, 0, 0,
&iter->conn, 1, &iter->mode);
if (ret)
fprintf(stderr, "cannot flip CRTC for connector %u (%d): %m\n",
iter->conn, errno);
else
iter->front_buf ^= 1;
}
usleep(100000);
}
}
/*
* modeset_cleanup() stays the same as before. But it now calls
* modeset_destroy_fb() instead of accessing the framebuffers directly.
*/
static void modeset_cleanup(int fd)
{
struct modeset_dev *iter;
while (modeset_list) {
/* remove from global list */
iter = modeset_list;
modeset_list = iter->next;
/* restore saved CRTC configuration */
drmModeSetCrtc(fd,
iter->saved_crtc->crtc_id,
iter->saved_crtc->buffer_id,
iter->saved_crtc->x,
iter->saved_crtc->y,
&iter->conn,
1,
&iter->saved_crtc->mode);
drmModeFreeCrtc(iter->saved_crtc);
/* destroy framebuffers */
modeset_destroy_fb(fd, &iter->bufs[1]);
modeset_destroy_fb(fd, &iter->bufs[0]);
/* free allocated memory */
free(iter);
}
}
/*
* This was a very short extension to the basic modesetting example that shows
* how double-buffering is implemented. Double-buffering is the de-facto
* standard in any graphics application so any other example will be based on
* this. It is important to understand the ideas behind it as the code is pretty
* easy and short compared to modeset.c.
*
* Double-buffering doesn't solve all problems. Vsync'ed page-flips solve most
* of the problems that still occur, but has problems on it's own (see
* modeset-vsync.c for a discussion).
*
* If you want more code, I can recommend reading the source-code of:
* - plymouth (which uses dumb-buffers like this example; very easy to understand)
* - kmscon (which uses libuterm to do this)
* - wayland (very sophisticated DRM renderer; hard to understand fully as it
* uses more complicated techniques like DRM planes)
* - xserver (very hard to understand as it is split across many files/projects)
*
* Any feedback is welcome. Feel free to use this code freely for your own
* documentation or projects.
*
* - Hosted on http://github.com/dvdhrm/docs
* - Written by David Herrmann <dh.herrmann@googlemail.com>
*/