-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathds.cpp
More file actions
188 lines (136 loc) · 5.17 KB
/
Copy pathds.cpp
File metadata and controls
188 lines (136 loc) · 5.17 KB
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
// Copyright 2019 David Butler <croepha@gmail.com>
extern EGLDisplay egl_display;
extern EGLSurface egl_surface;
extern bool _debug_should_quit;
extern bool _debug_ignoring_events;
SDL_Window* sdl_window;
// TODO get rid of libDRM.... but, if we do that, then MESA will still depend on it?
int drm_fd = -1;
void ds_do_egl_display() {
// TODO Enumerate???
drm_fd = open("/dev/dri/card0", O_RDWR);
assert(drm_fd != -1);
auto resources = drmModeGetResources(drm.fd);
assert(resources);
drmModeConnector* connector = 0;
for (int i = 0; i < resources->count_connectors; i++) {
connector = drmModeGetConnector(drm.fd, resources->connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED) break;
drmModeFreeConnector(connector);
connector = NULL;
}
assert(connector);
// TODO: Optionally use a configured resolution...
drmModeModeInfo *mode = 0;
/* find prefered mode or the highest resolution mode: */
for (int i = 0, int area = 0; i < connector->count_modes; i++) {
drmModeModeInfo *current_mode = &connector->modes[i];
if (current_mode->type & DRM_MODE_TYPE_PREFERRED) {
mode = current_mode;
}
int current_area = current_mode->hdisplay * current_mode->vdisplay;
if (current_area > area) {
mode = current_mode;
area = current_area;
}
}
assert(mode);
drmModeEncoder* encoder = 0;
for (i = 0; i < resources->count_encoders; i++) {
encoder = drmModeGetEncoder(drm.fd, resources->encoders[i]);
if (encoder->encoder_id == connector->encoder_id)
break;
drmModeFreeEncoder(encoder);
encoder = NULL;
}
u32 crtc_id = 0;
if (encoder) {
crtc_id = encoder->crtc_id;
} else {
crtc_id = find_crtc_for_connector(resources, connector);
assert(crtc_id != 0);
}
u32 connector_id = connector->connector_id;
auto gbm_device = gbm_create_device(drm_fd);
auto gbm_surface = gbm_surface_create(
gbm_device,
drm_mode->hdisplay, drm_mode->vdisplay,
GBM_FORMAT_XRGB8888,
// TODO look into what these options do???
GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
assert(gbm_surface);
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
auto eglGetPlatformDisplayEXT =
(PFNEGLGETPLATFORMDISPLAYEXTPROC)
eglGetProcAddress("eglGetPlatformDisplayEXT");
assert(eglGetPlatformDisplayEXT);
egl_display = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR, gbm.dev, NULL);
}
struct gbm_bo *drm_bo;
void ds_do_specific_setup() {
EGLConfig config;
EGLint num_config;
static EGLint const attribute_list[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 0,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};
eglChooseConfig(egl_display, attribute_list, &config, 1, &num_config);
EGLint context_attr_list[] = {
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE,
};
auto egl_context = eglCreateContext(egl_display, config,
EGL_NO_CONTEXT, context_attr_list);
auto r5 = eglGetError();
assert(r5 == EGL_SUCCESS);
assert(eglGetError() == EGL_SUCCESS);
assert(egl_context != EGL_NO_CONTEXT);
egl_surface = eglCreateWindowSurface(egl_display, config,
(EGLNativeWindowType)
gbm_surface, NULL);
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
bo = gbm_surface_lock_front_buffer(gbm.surface);
auto fb = drm_fb_get_from_bo(bo);
auto r2 = drmModeSetCrtc(drm_fd, drm_crtc_id, fb->fb_id, 0, 0,
&drm_connector_id, 1, drm_mode);
assert(!r2);
}
void ds_platform_frame() {
int waiting_for_flip = 1;
auto next_bo = gbm_surface_lock_front_buffer(gbm_surface);
auto fb = drm_fb_get_from_bo(next_bo);
auto r1 = drmModePageFlip(drm_fd, drm_crtc_id, fb->fb_id,
DRM_MODE_PAGE_FLIP_EVENT,
&waiting_for_flip);
assert(!r1);
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
FD_SET(drm.fd, &fds);
drmEventContext evctx = {
.version = DRM_EVENT_CONTEXT_VERSION,
.page_flip_handler = page_flip_handler,
};
while (waiting_for_flip) {
auto 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);
}
gbm_surface_release_buffer(gbm_surface, drm_bo);
drm_bo = next_bo;
}