forked from stschake/flutter_drm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrm-legacy.c
113 lines (93 loc) · 2.35 KB
/
drm-legacy.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
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include "common.h"
#include "drm-common.h"
static struct drm drm;
static void page_flip_handler(int fd, unsigned int frame,
unsigned int sec, unsigned int usec, void *data)
{
/* suppress 'unused parameter' warnings */
(void)fd, (void)frame, (void)sec, (void)usec;
int *waiting_for_flip = data;
*waiting_for_flip = 0;
}
static int legacy_run(const struct gbm *gbm, const struct egl *egl)
{
fd_set fds;
drmEventContext evctx = {
.version = 2,
.page_flip_handler = page_flip_handler,
};
struct gbm_bo *bo;
struct drm_fb *fb;
uint32_t i = 0;
int ret;
FD_ZERO(&fds);
FD_SET(0, &fds);
FD_SET(drm.fd, &fds);
eglSwapBuffers(egl->display, egl->surface);
bo = gbm_surface_lock_front_buffer(gbm->surface);
fb = drm_fb_get_from_bo(bo);
if (!fb) {
fprintf(stderr, "Failed to get a new framebuffer BO\n");
return -1;
}
/* set mode: */
ret = drmModeSetCrtc(drm.fd, drm.crtc_id, fb->fb_id, 0, 0,
&drm.connector_id, 1, drm.mode);
if (ret) {
printf("failed to set mode: %s\n", strerror(errno));
return ret;
}
while (1) {
struct gbm_bo *next_bo;
int waiting_for_flip = 1;
// TODO: wait for present?
eglSwapBuffers(egl->display, egl->surface);
next_bo = gbm_surface_lock_front_buffer(gbm->surface);
fb = drm_fb_get_from_bo(next_bo);
if (!fb) {
fprintf(stderr, "Failed to get a new framebuffer BO\n");
return -1;
}
/*
* Here you could also update drm plane layers if you want
* hw composition
*/
ret = drmModePageFlip(drm.fd, drm.crtc_id, fb->fb_id,
DRM_MODE_PAGE_FLIP_EVENT, &waiting_for_flip);
if (ret) {
printf("failed to queue page flip: %s\n", strerror(errno));
return -1;
}
while (waiting_for_flip) {
ret = select(drm.fd + 1, &fds, NULL, NULL, NULL);
if (ret < 0) {
printf("select err: %s\n", strerror(errno));
return ret;
} else if (ret == 0) {
printf("select timeout!\n");
return -1;
} else if (FD_ISSET(0, &fds)) {
printf("user interrupted!\n");
break;
}
drmHandleEvent(drm.fd, &evctx);
}
/* release last buffer to render on again: */
gbm_surface_release_buffer(gbm->surface, bo);
bo = next_bo;
}
return 0;
}
const struct drm * init_drm_legacy(const char *device)
{
int ret;
ret = init_drm(&drm, device);
if (ret)
return NULL;
drm.run = legacy_run;
return &drm;
}