Debian Codegraph @

src/backend/gl/glx.c

raw symbols SCIP
1 // SPDX-License-Identifier: MIT
2 /*
3 * Compton - a compositor for X11
4 *
5 * Based on `xcompmgr` - Copyright (c) 2003, Keith Packard
6 *
7 * Copyright (c) 2011-2013, Christopher Jeffrey
8 * Copyright (c) 2019 Yuxuan Shui <[email protected]>
9 * See LICENSE-mit for more information.
10 *
11 */
12 #include <limits.h>
13 #include <stdbool.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <X11/Xlib-xcb.h>
18 #include <pixman.h>
19 #include <uthash.h>
20 #include <xcb/composite.h>
21 #include <xcb/xcb.h>
22 #include <xcb/xcb_aux.h>
23
24 #include "backend/backend.h"
25 #include "backend/backend_common.h"
26 #include "common.h"
27 #include "compiler.h"
28 #include "config.h"
29 #include "log.h"
30 #include "picom.h"
31 #include "utils/misc.h"
32 #include "x.h"
33
34 #include "gl_common.h"
35 #include "glx.h"
36
37 struct _glx_data {
38 struct gl_data gl;
39 xcb_window_t target_win;
40 GLXContext ctx;
41 struct glx_fbconfig_cache *cached_fbconfigs;
42 };
43
44 struct glx_fbconfig_cache {
45 UT_hash_handle hh;
46 struct xvisual_info visual_info;
47 struct glx_fbconfig_info info;
48 };
49
50 #define glXGetFBConfigAttribChecked(a, b, attr, c) \
51 do { \
52 if (glXGetFBConfigAttrib(a, b, attr, c)) { \
53 log_info("Cannot get FBConfig attribute " #attr); \
54 break; \
55 } \
56 } while (0)
57
58 bool glx_find_fbconfig(struct x_connection *c, struct xvisual_info m,
59 struct glx_fbconfig_info *info) {
60 log_debug("Looking for FBConfig for RGBA%d%d%d%d, depth: %d, visual id: %#x", m.red_size,
61 m.blue_size, m.green_size, m.alpha_size, m.visual_depth, m.visual);
62
63 info->cfg = NULL;
64
65 int ncfg;
66 // clang-format off
67 GLXFBConfig *cfg =
68 glXChooseFBConfig(c->dpy, c->screen, (int[]){
69 GLX_RED_SIZE, m.red_size,
70 GLX_GREEN_SIZE, m.green_size,
71 GLX_BLUE_SIZE, m.blue_size,
72 GLX_ALPHA_SIZE, m.alpha_size,
73 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
74 GLX_X_RENDERABLE, True,
75 GLX_CONFIG_CAVEAT, GLX_NONE,
76 None,
77 }, &ncfg);
78 // clang-format on
79
80 int texture_tgts, y_inverted, texture_fmt;
81 bool found = false;
82 int min_cost = INT_MAX;
83 GLXFBConfig ret;
84 for (int i = 0; i < ncfg; i++) {
85 int depthbuf, stencil, doublebuf, bufsize;
86 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_BUFFER_SIZE, &bufsize);
87 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_DEPTH_SIZE, &depthbuf);
88 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_STENCIL_SIZE, &stencil);
89 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_DOUBLEBUFFER, &doublebuf);
90 if (depthbuf + stencil + bufsize * (doublebuf + 1) >= min_cost) {
91 continue;
92 }
93 int red, green, blue;
94 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_RED_SIZE, &red);
95 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_BLUE_SIZE, &blue);
96 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_GREEN_SIZE, &green);
97 if (red != m.red_size || green != m.green_size || blue != m.blue_size) {
98 // Color size doesn't match, this cannot work
99 continue;
100 }
101
102 int rgb, rgba;
103 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_BIND_TO_TEXTURE_RGB_EXT, &rgb);
104 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_BIND_TO_TEXTURE_RGBA_EXT,
105 &rgba);
106 if (!rgb && !rgba) {
107 log_info("FBConfig is neither RGBA nor RGB, we cannot "
108 "handle this setup.");
109 continue;
110 }
111
112 int visual;
113 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_VISUAL_ID, &visual);
114 if (m.visual_depth != -1 &&
115 xcb_aux_get_depth_of_visual(c->screen_info, (xcb_visualid_t)visual) !=
116 m.visual_depth) {
117 // FBConfig and the correspondent X Visual might not have the same
118 // depth. (e.g. 32 bit FBConfig with a 24 bit Visual). This is
119 // quite common, seen in both open source and proprietary drivers.
120 //
121 // If the FBConfig has a matching depth but its visual doesn't, we
122 // still cannot use it.
123 continue;
124 }
125
126 // All check passed, we are using this one.
127 found = true;
128 ret = cfg[i];
129 glXGetFBConfigAttribChecked(
130 c->dpy, cfg[i], GLX_BIND_TO_TEXTURE_TARGETS_EXT, &texture_tgts);
131 glXGetFBConfigAttribChecked(c->dpy, cfg[i], GLX_Y_INVERTED_EXT, &y_inverted);
132
133 // Prefer the texture format with matching alpha, with the other one as
134 // fallback
135 if (m.alpha_size) {
136 texture_fmt = rgba ? GLX_TEXTURE_FORMAT_RGBA_EXT
137 : GLX_TEXTURE_FORMAT_RGB_EXT;
138 } else {
139 texture_fmt =
140 rgb ? GLX_TEXTURE_FORMAT_RGB_EXT : GLX_TEXTURE_FORMAT_RGBA_EXT;
141 }
142 min_cost = depthbuf + stencil + bufsize * (doublebuf + 1);
143 }
144 free(cfg);
145 if (found) {
146 info->cfg = ret;
147 info->texture_tgts = texture_tgts;
148 info->texture_fmt = texture_fmt;
149 info->y_inverted = y_inverted;
150 }
151 return found;
152 }
153
154 /**
155 * Free a glx_texture_t.
156 */
157 static void glx_release_image(backend_t *base, struct gl_texture *tex) {
158 GLXPixmap *p = tex->user_data;
159 // Release binding
160 if (p && tex->texture) {
161 glBindTexture(GL_TEXTURE_2D, tex->texture);
162 glXReleaseTexImageEXT(base->c->dpy, *p, GLX_FRONT_LEFT_EXT);
163 glBindTexture(GL_TEXTURE_2D, 0);
164 }
165
166 // Free GLX Pixmap
167 if (p) {
168 glXDestroyPixmap(base->c->dpy, *p);
169 *p = 0;
170 }
171
172 free(p);
173 tex->user_data = NULL;
174 }
175
176 /**
177 * Destroy GLX related resources.
178 */
179 void glx_deinit(backend_t *base) {
180 struct _glx_data *gd = (void *)base;
181
182 // Destroy GLX context
183 if (gd->ctx) {
184 gl_deinit(&gd->gl);
185 glXMakeCurrent(base->c->dpy, None, NULL);
186 glXDestroyContext(base->c->dpy, gd->ctx);
187 gd->ctx = 0;
188 }
189
190 struct glx_fbconfig_cache *cached_fbconfig = NULL, *tmp = NULL;
191 HASH_ITER(hh, gd->cached_fbconfigs, cached_fbconfig, tmp) {
192 HASH_DEL(gd->cached_fbconfigs, cached_fbconfig);
193 free(cached_fbconfig);
194 }
195
196 free(gd);
197 }
198
199 static bool glx_set_swap_interval(int interval, Display *dpy, GLXDrawable drawable) {
200 bool vsync_enabled = false;
201 if (glxext.has_MESA_swap_control) {
202 vsync_enabled = (glXSwapIntervalMESA((uint)interval) == 0);
203 }
204 if (!vsync_enabled && glxext.has_SGI_swap_control) {
205 vsync_enabled = (glXSwapIntervalSGI(interval) == 0);
206 }
207 if (!vsync_enabled && glxext.has_EXT_swap_control) {
208 // glXSwapIntervalEXT doesn't return if it's successful
209 glXSwapIntervalEXT(dpy, drawable, interval);
210 vsync_enabled = true;
211 }
212 return vsync_enabled;
213 }
214
215 const struct backend_operations glx_ops;
216 /**
217 * Initialize OpenGL.
218 */
219 static backend_t *glx_init(session_t *ps, xcb_window_t target) {
220 bool success = false;
221 glxext_init(ps->c.dpy, ps->c.screen);
222 auto gd = ccalloc(1, struct _glx_data);
223 init_backend_base(&gd->gl.base, ps);
224 gd->gl.base.ops = glx_ops;
225
226 gd->target_win = target;
227
228 XVisualInfo *pvis = NULL;
229
230 // Check for GLX extension
231 if (!ps->c.e.has_glx) {
232 log_error("No GLX extension.");
233 goto end;
234 }
235
236 // Get XVisualInfo
237 int nitems = 0;
238 XVisualInfo vreq = {.visualid = ps->c.screen_info->root_visual};
239 pvis = XGetVisualInfo(ps->c.dpy, VisualIDMask, &vreq, &nitems);
240 if (!pvis) {
241 log_error("Failed to acquire XVisualInfo for current visual.");
242 goto end;
243 }
244
245 // Ensure the visual is double-buffered
246 int value = 0;
247 if (glXGetConfig(ps->c.dpy, pvis, GLX_USE_GL, &value) || !value) {
248 log_error("Root visual is not a GL visual.");
249 goto end;
250 }
251
252 if (glXGetConfig(ps->c.dpy, pvis, GLX_DOUBLEBUFFER, &value) || !value) {
253 log_error("Root visual is not a double buffered GL visual.");
254 goto end;
255 }
256
257 if (glXGetConfig(ps->c.dpy, pvis, GLX_RGBA, &value) || !value) {
258 log_error("Root visual is a color index visual, not supported");
259 goto end;
260 }
261
262 if (!glxext.has_EXT_texture_from_pixmap) {
263 log_error("GLX_EXT_texture_from_pixmap is not supported by your driver");
264 goto end;
265 }
266
267 if (!glxext.has_ARB_create_context) {
268 log_error("GLX_ARB_create_context is not supported by your driver");
269 goto end;
270 }
271
272 // Find a fbconfig with visualid matching the one from the target win, so we can
273 // be sure that the fbconfig is compatible with our target window.
274 int ncfgs;
275 GLXFBConfig *cfg = glXGetFBConfigs(ps->c.dpy, ps->c.screen, &ncfgs);
276 bool found = false;
277 for (int i = 0; i < ncfgs; i++) {
278 int visualid;
279 glXGetFBConfigAttribChecked(ps->c.dpy, cfg[i], GLX_VISUAL_ID, &visualid);
280 if ((VisualID)visualid != pvis->visualid) {
281 continue;
282 }
283
284 int *attributes = (int[]){GLX_CONTEXT_MAJOR_VERSION_ARB,
285 3,
286 GLX_CONTEXT_MINOR_VERSION_ARB,
287 3,
288 GLX_CONTEXT_PROFILE_MASK_ARB,
289 GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
290 0,
291 0,
292 0};
293 if (glxext.has_ARB_create_context_robustness) {
294 attributes[6] = GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB;
295 attributes[7] = GLX_LOSE_CONTEXT_ON_RESET_ARB;
296 }
297
298 gd->ctx = glXCreateContextAttribsARB(ps->c.dpy, cfg[i], 0, true, attributes);
299 free((void *)cfg);
300
301 if (!gd->ctx) {
302 log_error("Failed to get GLX context.");
303 goto end;
304 }
305 found = true;
306 break;
307 }
308
309 if (!found) {
310 log_error("Couldn't find a suitable fbconfig for the target window");
311 goto end;
312 }
313
314 // Attach GLX context
315 GLXDrawable tgt = gd->target_win;
316 if (!glXMakeCurrent(ps->c.dpy, tgt, gd->ctx)) {
317 log_error("Failed to attach GLX context.");
318 goto end;
319 }
320
321 if (!gl_init(&gd->gl, ps)) {
322 log_error("Failed to setup OpenGL");
323 goto end;
324 }
325
326 gd->gl.release_user_data = glx_release_image;
327
328 if (ps->o.vsync) {
329 if (!glx_set_swap_interval(1, ps->c.dpy, tgt)) {
330 log_error("Failed to enable vsync.");
331 }
332 } else {
333 glx_set_swap_interval(0, ps->c.dpy, tgt);
334 }
335
336 success = true;
337
338 end:
339 if (pvis) {
340 XFree(pvis);
341 }
342
343 if (!success) {
344 glx_deinit(&gd->gl.base);
345 return NULL;
346 }
347
348 return &gd->gl.base;
349 }
350
351 static image_handle
352 glx_bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt) {
353 GLXPixmap *glxpixmap = NULL;
354 auto gd = (struct _glx_data *)base;
355
356 if (fmt.visual_depth < 0) {
357 log_error("Pixmap %#010x with invalid depth %d", pixmap, fmt.visual_depth);
358 return NULL;
359 }
360
361 auto r =
362 xcb_get_geometry_reply(base->c->c, xcb_get_geometry(base->c->c, pixmap), NULL);
363 if (!r) {
364 log_error("Invalid pixmap %#010x", pixmap);
365 return NULL;
366 }
367
368 log_trace("Binding pixmap %#010x", pixmap);
369 auto inner = ccalloc(1, struct gl_texture);
370 inner->width = r->width;
371 inner->height = r->height;
372 inner->format = BACKEND_IMAGE_FORMAT_PIXMAP;
373 free(r);
374
375 struct glx_fbconfig_cache *cached_fbconfig = NULL;
376 HASH_FIND(hh, gd->cached_fbconfigs, &fmt, sizeof(fmt), cached_fbconfig);
377 if (!cached_fbconfig) {
378 struct glx_fbconfig_info fbconfig;
379 if (!glx_find_fbconfig(base->c, fmt, &fbconfig)) {
380 log_error("Couldn't find FBConfig with requested visual %#x",
381 fmt.visual);
382 goto err;
383 }
384 cached_fbconfig = cmalloc(struct glx_fbconfig_cache);
385 cached_fbconfig->visual_info = fmt;
386 cached_fbconfig->info = fbconfig;
387 HASH_ADD(hh, gd->cached_fbconfigs, visual_info, sizeof(fmt), cached_fbconfig);
388 } else {
389 log_debug("Found cached FBConfig for RGBA%d%d%d%d, depth: %d, visual id: "
390 "%#x",
391 fmt.red_size, fmt.blue_size, fmt.green_size, fmt.alpha_size,
392 fmt.visual_depth, fmt.visual);
393 }
394 struct glx_fbconfig_info *fbconfig = &cached_fbconfig->info;
395
396 // Choose a suitable texture target for our pixmap.
397 // Refer to GLX_EXT_texture_om_pixmap spec to see what are the mean
398 // of the bits in texture_tgts
399 if (!(fbconfig->texture_tgts & GLX_TEXTURE_2D_BIT_EXT)) {
400 log_error("Cannot bind pixmap to GL_TEXTURE_2D, giving up");
401 goto err;
402 }
403
404 log_debug("depth %d, rgba %d", fmt.visual_depth,
405 (fbconfig->texture_fmt == GLX_TEXTURE_FORMAT_RGBA_EXT));
406
407 GLint attrs[] = {
408 GLX_TEXTURE_FORMAT_EXT,
409 fbconfig->texture_fmt,
410 GLX_TEXTURE_TARGET_EXT,
411 GLX_TEXTURE_2D_EXT,
412 0,
413 };
414
415 inner->y_inverted = fbconfig->y_inverted;
416
417 glxpixmap = cmalloc(GLXPixmap);
418 inner->pixmap = pixmap;
419 *glxpixmap = glXCreatePixmap(base->c->dpy, fbconfig->cfg, pixmap, attrs);
420
421 if (!*glxpixmap) {
422 log_error("Failed to create glpixmap for pixmap %#010x", pixmap);
423 goto err;
424 }
425
426 log_trace("GLXPixmap %#010lx", *glxpixmap);
427
428 // Create texture
429 inner->user_data = glxpixmap;
430 inner->texture = gl_new_texture();
431 glBindTexture(GL_TEXTURE_2D, inner->texture);
432 glXBindTexImageEXT(base->c->dpy, *glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
433 glBindTexture(GL_TEXTURE_2D, 0);
434
435 gl_check_err();
436 return (image_handle)inner;
437 err:
438 free(glxpixmap);
439 free(inner);
440 return NULL;
441 }
442
443 static bool glx_present(backend_t *base) {
444 struct _glx_data *gd = (void *)base;
445 gl_finish_render(&gd->gl);
446 glXSwapBuffers(base->c->dpy, gd->target_win);
447 return true;
448 }
449
450 static int glx_buffer_age(backend_t *base) {
451 if (!glxext.has_EXT_buffer_age) {
452 return -1;
453 }
454
455 struct _glx_data *gd = (void *)base;
456 unsigned int val;
457 glXQueryDrawable(base->c->dpy, gd->target_win, GLX_BACK_BUFFER_AGE_EXT, &val);
458 return (int)val ?: -1;
459 }
460
461 static void glx_diagnostics(backend_t *base) {
462 bool warn_software_rendering = false;
463 const char *software_renderer_names[] = {"llvmpipe", "SWR", "softpipe"};
464 auto glx_vendor = glXGetClientString(base->c->dpy, GLX_VENDOR);
465 printf("* Driver vendors:\n");
466 printf(" * GLX: %s\n", glx_vendor);
467 printf(" * GL: %s\n", glGetString(GL_VENDOR));
468
469 auto gl_renderer = (const char *)glGetString(GL_RENDERER);
470 printf("* GL renderer: %s\n", gl_renderer);
471 if (strcmp(glx_vendor, "Mesa Project and SGI") == 0) {
472 for (size_t i = 0; i < ARR_SIZE(software_renderer_names); i++) {
473 if (strstr(gl_renderer, software_renderer_names[i]) != NULL) {
474 warn_software_rendering = true;
475 break;
476 }
477 }
478 }
479
480 #ifdef GLX_MESA_query_renderer
481 if (glxext.has_MESA_query_renderer) {
482 unsigned int accelerated = 0;
483 glXQueryCurrentRendererIntegerMESA(GLX_RENDERER_ACCELERATED_MESA, &accelerated);
484 printf("* Accelerated: %d\n", accelerated);
485
486 // Trust GLX_MESA_query_renderer when it's available
487 warn_software_rendering = (accelerated == 0);
488 }
489 #endif
490
491 if (warn_software_rendering) {
492 printf("\n(You are using a software renderer. Unless you are doing this\n"
493 "intentionally, this means you don't have a graphics driver\n"
494 "properly installed. Performance will suffer. Please fix this\n"
495 "before reporting your issue.)\n");
496 }
497 }
498
499 static int glx_max_buffer_age(struct backend_base *base attr_unused) {
500 return 5; // Why?
501 }
502
503 #define PICOM_BACKEND_GLX_MAJOR (0UL)
504 #define PICOM_BACKEND_GLX_MINOR (1UL)
505
506 static void glx_version(struct backend_base * /*base*/, uint64_t *major, uint64_t *minor) {
507 *major = PICOM_BACKEND_GLX_MAJOR;
508 *minor = PICOM_BACKEND_GLX_MINOR;
509 }
510
511 const struct backend_operations glx_ops = {
512 .apply_alpha = gl_apply_alpha,
513 .back_buffer = gl_back_buffer,
514 .bind_pixmap = glx_bind_pixmap,
515 .blit = gl_blit,
516 .blur = gl_blur,
517 .clear = gl_clear,
518 .copy_area = gl_copy_area,
519 .copy_area_quantize = gl_copy_area_quantize,
520 .image_capabilities = gl_image_capabilities,
521 .is_format_supported = gl_is_format_supported,
522 .new_image = gl_new_image,
523 .present = glx_present,
524 .quirks = backend_no_quirks,
525 .version = glx_version,
526 .release_image = gl_release_image,
527
528 .init = glx_init,
529 .deinit = glx_deinit,
530 .root_change = gl_root_change,
531 .prepare = gl_prepare,
532 .buffer_age = glx_buffer_age,
533 .last_render_time = gl_last_render_time,
534 .create_blur_context = gl_create_blur_context,
535 .destroy_blur_context = gl_destroy_blur_context,
536 .get_blur_size = gl_get_blur_size,
537 .diagnostics = glx_diagnostics,
538 .device_status = gl_device_status,
539 .create_shader = gl_create_window_shader,
540 .destroy_shader = gl_destroy_window_shader,
541 .get_shader_attributes = gl_get_shader_attributes,
542 .max_buffer_age = glx_max_buffer_age,
543 };
544
545 struct glxext_info glxext = {0};
546
547 void glxext_init(Display *dpy, int screen) {
548 if (glxext.initialized) {
549 return;
550 }
551 glxext.initialized = true;
552 #define X(name) \
553 glxext.has_##name = epoxy_has_glx_extension(dpy, screen, "GLX_" #name); \
554 log_info("GLX extension " #name " - %s", glxext.has_##name ? "present" : "absent");
555
556 GLX_EXTS;
557 #ifdef GLX_MESA_query_renderer
558 X(MESA_query_renderer);
559 #endif
560 #undef X
561 }
562
563 BACKEND_ENTRYPOINT(glx_register) {
564 if (!backend_register(PICOM_BACKEND_MAJOR, PICOM_BACKEND_MINOR, "glx",
565 glx_ops.init, true)) {
566 log_error("Failed to register glx backend");
567 }
568 }