Debian Codegraph @

src/backend/gl/egl.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 /*
3 * Copyright (c) 2022 Yuxuan Shui <[email protected]>
4 */
5
6 #include <X11/Xlib-xcb.h>
7 #include <stdbool.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <xcb/xcb.h>
11
12 #include "backend/backend.h"
13 #include "backend/backend_common.h"
14 #include "common.h"
15 #include "compiler.h"
16 #include "config.h"
17 #include "log.h"
18 #include "picom.h"
19 #include "utils/misc.h"
20 #include "x.h"
21
22 #include "egl.h"
23 #include "gl_common.h"
24
25 struct egl_data {
26 struct gl_data gl;
27 EGLDisplay display;
28 EGLSurface target_win;
29 EGLContext ctx;
30 };
31
32 const char *eglGetErrorString(EGLint error) {
33 #define CASE_STR(value) \
34 case value: return #value;
35 switch (error) {
36 CASE_STR(EGL_SUCCESS)
37 CASE_STR(EGL_NOT_INITIALIZED)
38 CASE_STR(EGL_BAD_ACCESS)
39 CASE_STR(EGL_BAD_ALLOC)
40 CASE_STR(EGL_BAD_ATTRIBUTE)
41 CASE_STR(EGL_BAD_CONTEXT)
42 CASE_STR(EGL_BAD_CONFIG)
43 CASE_STR(EGL_BAD_CURRENT_SURFACE)
44 CASE_STR(EGL_BAD_DISPLAY)
45 CASE_STR(EGL_BAD_SURFACE)
46 CASE_STR(EGL_BAD_MATCH)
47 CASE_STR(EGL_BAD_PARAMETER)
48 CASE_STR(EGL_BAD_NATIVE_PIXMAP)
49 CASE_STR(EGL_BAD_NATIVE_WINDOW)
50 CASE_STR(EGL_CONTEXT_LOST)
51 default: return "Unknown";
52 }
53 #undef CASE_STR
54 }
55
56 /**
57 * Free a gl_texture_t.
58 */
59 static void egl_release_image(backend_t *base, struct gl_texture *tex) {
60 struct egl_data *gd = (void *)base;
61 EGLImage *p = tex->user_data;
62 // Release binding
63 if (p && *p != EGL_NO_IMAGE) {
64 eglDestroyImage(gd->display, *p);
65 *p = EGL_NO_IMAGE;
66 }
67
68 free(p);
69 tex->user_data = NULL;
70 }
71
72 /**
73 * Destroy EGL related resources.
74 */
75 void egl_deinit(backend_t *base) {
76 struct egl_data *gd = (void *)base;
77
78 // Destroy EGL context
79 if (gd->ctx != EGL_NO_CONTEXT) {
80 gl_deinit(&gd->gl);
81 eglMakeCurrent(gd->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
82 eglDestroyContext(gd->display, gd->ctx);
83 gd->ctx = EGL_NO_CONTEXT;
84 }
85
86 if (gd->target_win != EGL_NO_SURFACE) {
87 eglDestroySurface(gd->display, gd->target_win);
88 gd->target_win = EGL_NO_SURFACE;
89 }
90
91 if (gd->display != EGL_NO_DISPLAY) {
92 eglTerminate(gd->display);
93 gd->display = EGL_NO_DISPLAY;
94 }
95
96 free(gd);
97 }
98
99 static bool egl_set_swap_interval(int interval, EGLDisplay dpy) {
100 return eglSwapInterval(dpy, interval);
101 }
102
103 const struct backend_operations egl_ops;
104 /**
105 * Initialize OpenGL.
106 */
107 static backend_t *egl_init(session_t *ps, xcb_window_t target) {
108 bool success = false;
109 struct egl_data *gd = NULL;
110
111 // Check if we have the X11 platform
112 const char *exts = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
113 if (strstr(exts, "EGL_EXT_platform_x11") == NULL) {
114 log_error("X11 platform not available.");
115 return NULL;
116 }
117
118 log_warn("The egl backend is still experimental, use with care.");
119
120 gd = ccalloc(1, struct egl_data);
121 gd->display = eglGetPlatformDisplayEXT(EGL_PLATFORM_X11_EXT, ps->c.dpy,
122 (EGLint[]){
123 EGL_PLATFORM_X11_SCREEN_EXT,
124 ps->c.screen,
125 EGL_NONE,
126 });
127 if (gd->display == EGL_NO_DISPLAY) {
128 log_error("Failed to get EGL display.");
129 goto end;
130 }
131
132 EGLint major, minor;
133 if (!eglInitialize(gd->display, &major, &minor)) {
134 log_error("Failed to initialize EGL.");
135 goto end;
136 }
137
138 if (major < 1 || (major == 1 && minor < 5)) {
139 log_error("EGL version too old, need at least 1.5.");
140 goto end;
141 }
142
143 // Check if EGL supports OpenGL
144 const char *apis = eglQueryString(gd->display, EGL_CLIENT_APIS);
145 if (strstr(apis, "OpenGL") == NULL) {
146 log_error("EGL does not support OpenGL.");
147 goto end;
148 }
149
150 eglext_init(gd->display);
151 init_backend_base(&gd->gl.base, ps);
152 gd->gl.base.ops = egl_ops;
153 if (!eglext.has_KHR_image_pixmap) {
154 log_error("EGL_KHR_image_pixmap not available.");
155 goto end;
156 }
157
158 auto visual_info = x_get_visual_info(&ps->c, ps->c.screen_info->root_visual);
159 EGLConfig config = NULL;
160 int nconfigs = 1;
161 // clang-format off
162 if (eglChooseConfig(gd->display,
163 (EGLint[]){
164 EGL_RED_SIZE, visual_info.red_size,
165 EGL_GREEN_SIZE, visual_info.green_size,
166 EGL_BLUE_SIZE, visual_info.blue_size,
167 EGL_ALPHA_SIZE, visual_info.alpha_size,
168 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
169 EGL_CONFIG_CAVEAT, EGL_NONE,
170 EGL_NONE,
171 }, &config, nconfigs, &nconfigs) != EGL_TRUE) {
172 log_error("Failed to choose EGL config for the root window.");
173 goto end;
174 }
175 // clang-format on
176
177 gd->target_win =
178 eglCreatePlatformWindowSurfaceEXT(gd->display, config, &target, NULL);
179 if (gd->target_win == EGL_NO_SURFACE) {
180 log_error("Failed to create EGL surface.");
181 goto end;
182 }
183
184 if (eglBindAPI(EGL_OPENGL_API) != EGL_TRUE) {
185 log_error("Failed to bind OpenGL API.");
186 goto end;
187 }
188
189 gd->ctx = eglCreateContext(
190 gd->display, config, NULL,
191 (EGLint[]){EGL_CONTEXT_MAJOR_VERSION, 3, EGL_CONTEXT_MINOR_VERSION, 3,
192 EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
193 EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY,
194 EGL_LOSE_CONTEXT_ON_RESET, EGL_NONE});
195 if (gd->ctx == EGL_NO_CONTEXT) {
196 log_error("Failed to get EGL context: %#x", eglGetError());
197 goto end;
198 }
199
200 if (!eglMakeCurrent(gd->display, gd->target_win, gd->target_win, gd->ctx)) {
201 log_error("Failed to attach EGL context.");
202 goto end;
203 }
204
205 if (!gl_init(&gd->gl, ps)) {
206 log_error("Failed to setup OpenGL");
207 goto end;
208 }
209 if (!gd->gl.has_egl_image_storage) {
210 log_error("GL_EXT_EGL_image_storage extension not available.");
211 goto end;
212 }
213
214 gd->gl.release_user_data = egl_release_image;
215
216 if (ps->o.vsync) {
217 if (!egl_set_swap_interval(1, gd->display)) {
218 log_error("Failed to enable vsync. %#x", eglGetError());
219 }
220 } else {
221 egl_set_swap_interval(0, gd->display);
222 }
223
224 success = true;
225
226 end:
227 if (!success) {
228 if (gd != NULL) {
229 egl_deinit(&gd->gl.base);
230 }
231 return NULL;
232 }
233
234 return &gd->gl.base;
235 }
236
237 static image_handle
238 egl_bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt) {
239 struct egl_data *gd = (void *)base;
240 EGLImage *eglpixmap = NULL;
241
242 auto r =
243 xcb_get_geometry_reply(base->c->c, xcb_get_geometry(base->c->c, pixmap), NULL);
244 if (!r) {
245 log_error("Invalid pixmap %#010x", pixmap);
246 return NULL;
247 }
248
249 log_trace("Binding pixmap %#010x", pixmap);
250 auto inner = ccalloc(1, struct gl_texture);
251 inner->format = BACKEND_IMAGE_FORMAT_PIXMAP;
252 inner->width = r->width;
253 inner->height = r->height;
254 free(r);
255
256 log_debug("depth %d", fmt.visual_depth);
257
258 inner->y_inverted = true;
259 inner->pixmap = pixmap;
260
261 eglpixmap = cmalloc(EGLImage);
262 *eglpixmap = eglCreateImage(gd->display, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR,
263 (EGLClientBuffer)(uintptr_t)pixmap, NULL);
264
265 if (*eglpixmap == EGL_NO_IMAGE) {
266 log_error("Failed to create eglpixmap for pixmap %#010x: %s", pixmap,
267 eglGetErrorString(eglGetError()));
268 goto err;
269 }
270
271 log_trace("EGLImage %p", *eglpixmap);
272
273 // Create texture
274 inner->user_data = eglpixmap;
275 inner->texture = gl_new_texture();
276 glBindTexture(GL_TEXTURE_2D, inner->texture);
277 glEGLImageTargetTexStorageEXT(GL_TEXTURE_2D, *eglpixmap, NULL);
278 glBindTexture(GL_TEXTURE_2D, 0);
279
280 gl_check_err();
281 return (image_handle)inner;
282 err:
283 free(eglpixmap);
284 free(inner);
285 return NULL;
286 }
287
288 static bool egl_present(backend_t *base) {
289 struct egl_data *gd = (void *)base;
290 gl_finish_render(&gd->gl);
291 eglSwapBuffers(gd->display, gd->target_win);
292 return true;
293 }
294
295 static int egl_buffer_age(backend_t *base) {
296 if (!eglext.has_EXT_buffer_age) {
297 return -1;
298 }
299
300 struct egl_data *gd = (void *)base;
301 EGLint val;
302 eglQuerySurface(gd->display, (EGLSurface)gd->target_win, EGL_BUFFER_AGE_EXT, &val);
303 return (int)val ?: -1;
304 }
305
306 static void egl_diagnostics(backend_t *base) {
307 struct egl_data *gd = (void *)base;
308 bool warn_software_rendering = false;
309 const char *software_renderer_names[] = {"llvmpipe", "SWR", "softpipe"};
310 auto egl_vendor = eglQueryString(gd->display, EGL_VENDOR);
311 printf("* Driver vendors:\n");
312 printf(" * EGL: %s\n", egl_vendor);
313 #ifdef EGL_MESA_query_driver
314 if (eglext.has_MESA_query_driver) {
315 printf(" * EGL driver: %s\n", eglGetDisplayDriverName(gd->display));
316 }
317 #endif
318 printf(" * GL: %s\n", glGetString(GL_VENDOR));
319
320 auto gl_renderer = (const char *)glGetString(GL_RENDERER);
321 printf("* GL renderer: %s\n", gl_renderer);
322 if (strstr(egl_vendor, "Mesa")) {
323 for (size_t i = 0; i < ARR_SIZE(software_renderer_names); i++) {
324 if (strstr(gl_renderer, software_renderer_names[i]) != NULL) {
325 warn_software_rendering = true;
326 break;
327 }
328 }
329 }
330
331 if (warn_software_rendering) {
332 printf("\n(You are using a software renderer. Unless you are doing this\n"
333 "intentionally, this means you don't have a graphics driver\n"
334 "properly installed. Performance will suffer. Please fix this\n"
335 "before reporting your issue.)\n");
336 }
337 }
338
339 static int egl_max_buffer_age(backend_t *base attr_unused) {
340 if (!eglext.has_EXT_buffer_age) {
341 return 0;
342 }
343
344 return 5; // Why?
345 }
346
347 #define PICOM_BACKEND_EGL_MAJOR (0UL)
348 #define PICOM_BACKEND_EGL_MINOR (1UL)
349
350 static void egl_version(struct backend_base * /*base*/, uint64_t *major, uint64_t *minor) {
351 *major = PICOM_BACKEND_EGL_MAJOR;
352 *minor = PICOM_BACKEND_EGL_MINOR;
353 }
354
355 const struct backend_operations egl_ops = {
356 .apply_alpha = gl_apply_alpha,
357 .back_buffer = gl_back_buffer,
358 .blit = gl_blit,
359 .blur = gl_blur,
360 .bind_pixmap = egl_bind_pixmap,
361 .clear = gl_clear,
362 .copy_area = gl_copy_area,
363 .copy_area_quantize = gl_copy_area_quantize,
364 .is_format_supported = gl_is_format_supported,
365 .image_capabilities = gl_image_capabilities,
366 .new_image = gl_new_image,
367 .present = egl_present,
368 .quirks = backend_no_quirks,
369 .version = egl_version,
370 .release_image = gl_release_image,
371
372 .init = egl_init,
373 .deinit = egl_deinit,
374 .root_change = gl_root_change,
375 .prepare = gl_prepare,
376 .buffer_age = egl_buffer_age,
377 .last_render_time = gl_last_render_time,
378 .create_blur_context = gl_create_blur_context,
379 .destroy_blur_context = gl_destroy_blur_context,
380 .get_blur_size = gl_get_blur_size,
381 .diagnostics = egl_diagnostics,
382 .device_status = gl_device_status,
383 .create_shader = gl_create_window_shader,
384 .destroy_shader = gl_destroy_window_shader,
385 .get_shader_attributes = gl_get_shader_attributes,
386 .max_buffer_age = egl_max_buffer_age,
387 };
388
389 struct eglext_info eglext = {0};
390
391 void eglext_init(EGLDisplay dpy) {
392 if (eglext.initialized) {
393 return;
394 }
395 eglext.initialized = true;
396 #define X(name) \
397 eglext.has_##name = epoxy_has_egl_extension(dpy, "EGL_" #name); \
398 log_info("EGL extension " #name " - %s", eglext.has_##name ? "present" : "absent");
399
400 EGL_EXTS;
401 #ifdef EGL_MESA_query_driver
402 X(MESA_query_driver);
403 #endif
404 #undef X
405 }
406
407 BACKEND_ENTRYPOINT(egl_register) {
408 if (!backend_register(PICOM_BACKEND_MAJOR, PICOM_BACKEND_MINOR, "egl",
409 egl_ops.init, true)) {
410 log_error("Failed to register egl backend");
411 }
412 }