Debian Codegraph @

src/egl/drivers/dri2/platform_x11_dri3.c

raw
1 /*
2 * Copyright © 2015 Boyan Ding
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #include <fcntl.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <xcb/dri3.h>
30 #include <xcb/present.h>
31 #include <xcb/xcb.h>
32
33 #if defined(HAVE_WAYLAND_PLATFORM)
34 #include "util/libdrm.h"
35 #endif
36 #include "drm-uapi/drm_fourcc.h"
37 #include "util/macros.h"
38
39 #include "egl_dri2.h"
40 #include "platform_x11_dri3.h"
41
42 #include "loader.h"
43 #include "x11_dri3.h"
44 #include "loader_dri3_helper.h"
45
46 static struct dri3_egl_surface *
47 loader_drawable_to_egl_surface(struct loader_dri3_drawable *draw)
48 {
49 size_t offset = offsetof(struct dri3_egl_surface, loader_drawable);
50 return (struct dri3_egl_surface *)(((void *)draw) - offset);
51 }
52
53 static void
54 egl_dri3_set_drawable_size(struct loader_dri3_drawable *draw, int width,
55 int height)
56 {
57 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
58
59 dri3_surf->surf.base.Width = width;
60 dri3_surf->surf.base.Height = height;
61 }
62
63 static bool
64 egl_dri3_in_current_context(struct loader_dri3_drawable *draw)
65 {
66 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
67 _EGLContext *ctx = _eglGetCurrentContext();
68
69 return ctx->Resource.Display == dri3_surf->surf.base.Resource.Display;
70 }
71
72 static struct dri_context *
73 egl_dri3_get_dri_context(struct loader_dri3_drawable *draw)
74 {
75 _EGLContext *ctx = _eglGetCurrentContext();
76 struct dri2_egl_context *dri2_ctx;
77 if (!ctx)
78 return NULL;
79 dri2_ctx = dri2_egl_context(ctx);
80 return dri2_ctx->dri_context;
81 }
82
83 static struct dri_screen *
84 egl_dri3_get_dri_screen(void)
85 {
86 _EGLContext *ctx = _eglGetCurrentContext();
87 struct dri2_egl_context *dri2_ctx;
88 if (!ctx)
89 return NULL;
90 dri2_ctx = dri2_egl_context(ctx);
91 return dri2_egl_display(dri2_ctx->base.Resource.Display)
92 ->dri_screen_render_gpu;
93 }
94
95 static void
96 egl_dri3_flush_drawable(struct loader_dri3_drawable *draw, unsigned flags)
97 {
98 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
99 _EGLDisplay *disp = dri3_surf->surf.base.Resource.Display;
100
101 dri2_flush_drawable_for_swapbuffers(disp, &dri3_surf->surf.base);
102 }
103
104 static const struct loader_dri3_vtable egl_dri3_vtable = {
105 .set_drawable_size = egl_dri3_set_drawable_size,
106 .in_current_context = egl_dri3_in_current_context,
107 .get_dri_context = egl_dri3_get_dri_context,
108 .get_dri_screen = egl_dri3_get_dri_screen,
109 .flush_drawable = egl_dri3_flush_drawable,
110 };
111
112 static EGLBoolean
113 dri3_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
114 {
115 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
116 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
117 xcb_drawable_t drawable = dri3_surf->loader_drawable.drawable;
118
119 loader_dri3_drawable_fini(&dri3_surf->loader_drawable);
120
121 if (surf->Type == EGL_PBUFFER_BIT)
122 xcb_free_pixmap(dri2_dpy->conn, drawable);
123
124 dri2_fini_surface(surf);
125 free(surf);
126
127 return EGL_TRUE;
128 }
129
130 static EGLBoolean
131 dri3_set_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
132 {
133 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
134
135 dri3_surf->surf.base.SwapInterval = interval;
136 loader_dri3_set_swap_interval(&dri3_surf->loader_drawable, interval);
137
138 return EGL_TRUE;
139 }
140
141 static enum loader_dri3_drawable_type
142 egl_to_loader_dri3_drawable_type(EGLint type)
143 {
144 switch (type) {
145 case EGL_WINDOW_BIT:
146 return LOADER_DRI3_DRAWABLE_WINDOW;
147 case EGL_PIXMAP_BIT:
148 return LOADER_DRI3_DRAWABLE_PIXMAP;
149 case EGL_PBUFFER_BIT:
150 return LOADER_DRI3_DRAWABLE_PBUFFER;
151 default:
152 return LOADER_DRI3_DRAWABLE_UNKNOWN;
153 }
154 }
155
156 static _EGLSurface *
157 dri3_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
158 void *native_surface, const EGLint *attrib_list)
159 {
160 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
161 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
162 struct dri3_egl_surface *dri3_surf;
163 const struct dri_config *dri_config;
164 xcb_drawable_t drawable;
165
166 dri3_surf = calloc(1, sizeof *dri3_surf);
167 if (!dri3_surf) {
168 _eglError(EGL_BAD_ALLOC, "dri3_create_surface");
169 return NULL;
170 }
171
172 if (!dri2_init_surface(&dri3_surf->surf.base, disp, type, conf, attrib_list,
173 false, native_surface))
174 goto cleanup_surf;
175
176 if (type == EGL_PBUFFER_BIT) {
177 drawable = xcb_generate_id(dri2_dpy->conn);
178 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize, drawable,
179 dri2_dpy->screen->root, dri3_surf->surf.base.Width,
180 dri3_surf->surf.base.Height);
181 } else {
182 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
183 drawable = (uintptr_t)native_surface;
184 }
185
186 dri_config =
187 dri2_get_dri_config(dri2_conf, type, dri3_surf->surf.base.GLColorspace);
188
189 if (!dri_config) {
190 _eglError(EGL_BAD_MATCH,
191 "Unsupported surfacetype/colorspace configuration");
192 goto cleanup_pixmap;
193 }
194
195 if (loader_dri3_drawable_init(
196 dri2_dpy->conn, drawable, egl_to_loader_dri3_drawable_type(type),
197 dri2_dpy->dri_screen_render_gpu, dri2_dpy->dri_screen_display_gpu,
198 dri2_dpy->multibuffers_available, true, dri_config,
199 &egl_dri3_vtable,
200 &dri3_surf->loader_drawable)) {
201 _eglError(EGL_BAD_ALLOC, "dri3_surface_create");
202 goto cleanup_pixmap;
203 }
204
205 if (dri3_surf->surf.base.ProtectedContent &&
206 dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu) {
207 _eglError(EGL_BAD_ALLOC, "dri3_surface_create");
208 goto cleanup_pixmap;
209 }
210
211 dri3_surf->loader_drawable.is_protected_content =
212 dri3_surf->surf.base.ProtectedContent;
213
214 return &dri3_surf->surf.base;
215
216 cleanup_pixmap:
217 if (type == EGL_PBUFFER_BIT)
218 xcb_free_pixmap(dri2_dpy->conn, drawable);
219 cleanup_surf:
220 free(dri3_surf);
221
222 return NULL;
223 }
224
225 static int
226 dri3_authenticate(_EGLDisplay *disp, uint32_t id)
227 {
228 #ifdef HAVE_WAYLAND_PLATFORM
229 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
230
231 if (!dri2_dpy->swrast) {
232 _eglLog(_EGL_WARNING,
233 "Wayland client render node authentication is unnecessary");
234 return 0;
235 }
236
237 _eglLog(_EGL_WARNING,
238 "Wayland client primary node authentication isn't supported");
239 #endif
240
241 return -1;
242 }
243
244 /**
245 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
246 */
247 static _EGLSurface *
248 dri3_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
249 void *native_window, const EGLint *attrib_list)
250 {
251 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
252 _EGLSurface *surf;
253
254 surf = dri3_create_surface(disp, EGL_WINDOW_BIT, conf, native_window,
255 attrib_list);
256 if (surf != NULL)
257 dri3_set_swap_interval(disp, surf, dri2_dpy->default_swap_interval);
258
259 return surf;
260 }
261
262 static _EGLSurface *
263 dri3_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
264 void *native_pixmap, const EGLint *attrib_list)
265 {
266 return dri3_create_surface(disp, EGL_PIXMAP_BIT, conf, native_pixmap,
267 attrib_list);
268 }
269
270 static _EGLSurface *
271 dri3_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
272 const EGLint *attrib_list)
273 {
274 return dri3_create_surface(disp, EGL_PBUFFER_BIT, conf, NULL, attrib_list);
275 }
276
277 static EGLBoolean
278 dri3_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
279 EGLuint64KHR *ust, EGLuint64KHR *msc, EGLuint64KHR *sbc)
280 {
281 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surface);
282
283 return loader_dri3_wait_for_msc(&dri3_surf->loader_drawable, 0, 0, 0,
284 (int64_t *)ust, (int64_t *)msc,
285 (int64_t *)sbc)
286 ? EGL_TRUE
287 : EGL_FALSE;
288 }
289
290 static _EGLImage *
291 dri3_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
292 EGLClientBuffer buffer, const EGLint *attr_list)
293 {
294 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
295 struct dri2_egl_image *dri2_img;
296 xcb_drawable_t drawable;
297 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
298 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
299 unsigned int fourcc;
300
301 drawable = (xcb_drawable_t)(uintptr_t)buffer;
302 bp_cookie = xcb_dri3_buffer_from_pixmap(dri2_dpy->conn, drawable);
303 bp_reply =
304 xcb_dri3_buffer_from_pixmap_reply(dri2_dpy->conn, bp_cookie, NULL);
305 if (!bp_reply) {
306 _eglError(EGL_BAD_ALLOC, "xcb_dri3_buffer_from_pixmap");
307 return NULL;
308 }
309
310 fourcc = dri2_fourcc_for_depth(dri2_dpy, bp_reply->depth);
311 if (fourcc == DRM_FORMAT_INVALID) {
312 _eglError(EGL_BAD_PARAMETER,
313 "dri3_create_image_khr: unsupported pixmap depth");
314 free(bp_reply);
315 return EGL_NO_IMAGE_KHR;
316 }
317
318 dri2_img = malloc(sizeof *dri2_img);
319 if (!dri2_img) {
320 _eglError(EGL_BAD_ALLOC, "dri3_create_image_khr");
321 free(bp_reply);
322 return EGL_NO_IMAGE_KHR;
323 }
324
325 _eglInitImage(&dri2_img->base, disp);
326
327 dri2_img->dri_image = loader_dri3_create_image(
328 dri2_dpy->conn, bp_reply, fourcc, dri2_dpy->dri_screen_render_gpu,
329 dri2_img);
330
331 free(bp_reply);
332
333 return &dri2_img->base;
334 }
335
336 static _EGLImage *
337 dri3_create_image_khr_pixmap_from_buffers(_EGLDisplay *disp, _EGLContext *ctx,
338 EGLClientBuffer buffer,
339 const EGLint *attr_list)
340 {
341 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
342 struct dri2_egl_image *dri2_img;
343 xcb_dri3_buffers_from_pixmap_cookie_t bp_cookie;
344 xcb_dri3_buffers_from_pixmap_reply_t *bp_reply;
345 xcb_drawable_t drawable;
346 unsigned int fourcc;
347
348 drawable = (xcb_drawable_t)(uintptr_t)buffer;
349 bp_cookie = xcb_dri3_buffers_from_pixmap(dri2_dpy->conn, drawable);
350 bp_reply =
351 xcb_dri3_buffers_from_pixmap_reply(dri2_dpy->conn, bp_cookie, NULL);
352
353 if (!bp_reply) {
354 _eglError(EGL_BAD_ATTRIBUTE, "dri3_create_image_khr");
355 return EGL_NO_IMAGE_KHR;
356 }
357
358 fourcc = dri2_fourcc_for_depth(dri2_dpy, bp_reply->depth);
359 if (fourcc == DRM_FORMAT_INVALID) {
360 _eglError(EGL_BAD_PARAMETER,
361 "dri3_create_image_khr: unsupported pixmap depth");
362 free(bp_reply);
363 return EGL_NO_IMAGE_KHR;
364 }
365
366 dri2_img = malloc(sizeof *dri2_img);
367 if (!dri2_img) {
368 _eglError(EGL_BAD_ALLOC, "dri3_create_image_khr");
369 free(bp_reply);
370 return EGL_NO_IMAGE_KHR;
371 }
372
373 _eglInitImage(&dri2_img->base, disp);
374
375 dri2_img->dri_image = loader_dri3_create_image_from_buffers(
376 dri2_dpy->conn, bp_reply, fourcc, dri2_dpy->dri_screen_render_gpu,
377 dri2_img);
378 free(bp_reply);
379
380 if (!dri2_img->dri_image) {
381 _eglError(EGL_BAD_ATTRIBUTE, "dri3_create_image_khr");
382 free(dri2_img);
383 return EGL_NO_IMAGE_KHR;
384 }
385
386 return &dri2_img->base;
387 }
388
389 static _EGLImage *
390 dri3_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
391 EGLClientBuffer buffer, const EGLint *attr_list)
392 {
393 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
394
395 switch (target) {
396 case EGL_NATIVE_PIXMAP_KHR:
397 if (dri2_dpy->multibuffers_available)
398 return dri3_create_image_khr_pixmap_from_buffers(disp, ctx, buffer,
399 attr_list);
400 return dri3_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
401 default:
402 return dri2_create_image_khr(disp, ctx, target, buffer, attr_list);
403 }
404 }
405
406 /**
407 * Called by the driver when it needs to update the real front buffer with the
408 * contents of its fake front buffer.
409 */
410 static void
411 dri3_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate)
412 {
413 struct loader_dri3_drawable *draw = loaderPrivate;
414 (void)driDrawable;
415
416 /* There does not seem to be any kind of consensus on whether we should
417 * support front-buffer rendering or not:
418 * http://lists.freedesktop.org/archives/mesa-dev/2013-June/040129.html
419 */
420 if (draw->type == LOADER_DRI3_DRAWABLE_WINDOW)
421 _eglLog(_EGL_WARNING,
422 "FIXME: egl/x11 doesn't support front buffer rendering.");
423 }
424
425 const __DRIimageLoaderExtension dri3_image_loader_extension = {
426 .base = {__DRI_IMAGE_LOADER, 1},
427
428 .getBuffers = loader_dri3_get_buffers,
429 .flushFrontBuffer = dri3_flush_front_buffer,
430 };
431
432 static EGLBoolean
433 dri3_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
434 const EGLint *rects, EGLint n_rects)
435 {
436 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(draw);
437
438 return loader_dri3_swap_buffers_msc(
439 &dri3_surf->loader_drawable, 0, 0, 0, 0, rects, n_rects,
440 draw->SwapBehavior == EGL_BUFFER_PRESERVED) != -1;
441 }
442
443 static EGLBoolean
444 dri3_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
445 {
446 return dri3_swap_buffers_with_damage(disp, draw, NULL, 0);
447 }
448
449 static EGLBoolean
450 dri3_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf,
451 void *native_pixmap_target)
452 {
453 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
454 xcb_pixmap_t target;
455
456 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
457 target = (uintptr_t)native_pixmap_target;
458
459 loader_dri3_copy_drawable(&dri3_surf->loader_drawable, target,
460 dri3_surf->loader_drawable.drawable);
461
462 return EGL_TRUE;
463 }
464
465 static int
466 dri3_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
467 {
468 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
469
470 return loader_dri3_query_buffer_age(&dri3_surf->loader_drawable);
471 }
472
473 static EGLBoolean
474 dri3_query_surface(_EGLDisplay *disp, _EGLSurface *surf, EGLint attribute,
475 EGLint *value)
476 {
477 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
478
479 switch (attribute) {
480 case EGL_WIDTH:
481 case EGL_HEIGHT:
482 loader_dri3_update_drawable_geometry(&dri3_surf->loader_drawable);
483 break;
484 default:
485 break;
486 }
487
488 return _eglQuerySurface(disp, surf, attribute, value);
489 }
490
491 static struct dri_drawable *
492 dri3_get_dri_drawable(_EGLSurface *surf)
493 {
494 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
495
496 return dri3_surf->loader_drawable.dri_drawable;
497 }
498
499 static void
500 dri3_close_screen_notify(_EGLDisplay *disp)
501 {
502 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
503
504 loader_dri3_close_screen(dri2_dpy->dri_screen_render_gpu);
505 }
506
507 struct dri2_egl_display_vtbl dri3_x11_display_vtbl = {
508 .authenticate = dri3_authenticate,
509 .create_window_surface = dri3_create_window_surface,
510 .create_pixmap_surface = dri3_create_pixmap_surface,
511 .create_pbuffer_surface = dri3_create_pbuffer_surface,
512 .destroy_surface = dri3_destroy_surface,
513 .create_image = dri3_create_image_khr,
514 .swap_interval = dri3_set_swap_interval,
515 .swap_buffers = dri3_swap_buffers,
516 .swap_buffers_with_damage = dri3_swap_buffers_with_damage,
517 .copy_buffers = dri3_copy_buffers,
518 .query_buffer_age = dri3_query_buffer_age,
519 .query_surface = dri3_query_surface,
520 .get_sync_values = dri3_get_sync_values,
521 .get_msc_rate = dri2_x11_get_msc_rate,
522 .get_dri_drawable = dri3_get_dri_drawable,
523 .close_screen_notify = dri3_close_screen_notify,
524 };
525
526 bool
527 dri3_x11_connect(struct dri2_egl_display *dri2_dpy, bool swrast)
528 {
529 dri2_dpy->fd_render_gpu =
530 x11_dri3_open(dri2_dpy->conn, dri2_dpy->screen->root, 0);
531 if (dri2_dpy->fd_render_gpu < 0) {
532 int conn_error = xcb_connection_has_error(dri2_dpy->conn);
533 if (!swrast) {
534 _eglLog(_EGL_WARNING, "DRI3 error: Could not get DRI3 device");
535 _eglLog(_EGL_WARNING, "Ensure your X server supports DRI3 to get accelerated rendering");
536 if (conn_error)
537 _eglLog(_EGL_WARNING, "DRI3: Failed to initialize");
538 }
539
540 return false;
541 }
542
543 loader_get_user_preferred_fd(&dri2_dpy->fd_render_gpu,
544 &dri2_dpy->fd_display_gpu);
545
546 if (!dri2_dpy->driver_name)
547 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
548
549 if (!dri2_dpy->driver_name) {
550 _eglLog(_EGL_WARNING, "DRI3: No driver found");
551 close(dri2_dpy->fd_render_gpu);
552 dri2_dpy->fd_render_gpu = -1;
553 return false;
554 }
555
556 #ifdef HAVE_WAYLAND_PLATFORM
557 /* Only try to get a render device name since dri3 doesn't provide a
558 * mechanism for authenticating client opened device node fds. If this
559 * fails then don't advertise the extension. */
560 dri2_dpy->device_name =
561 drmGetRenderDeviceNameFromFd(dri2_dpy->fd_render_gpu);
562 #endif
563
564 return true;
565 }