3dc9b98a90982ca7e2e65354181e6bfcf39710cc
[mesa.git] / src / egl / drivers / dri2 / platform_x11_dri3.c
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 <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <xcb/xcb.h>
29 #include <xcb/dri3.h>
30 #include <xcb/present.h>
31
32 #include <xf86drm.h>
33
34 #include "egl_dri2.h"
35 #include "egl_dri2_fallbacks.h"
36 #include "platform_x11_dri3.h"
37
38 #include "loader.h"
39 #include "loader_dri3_helper.h"
40
41 static struct dri3_egl_surface *
42 loader_drawable_to_egl_surface(struct loader_dri3_drawable *draw) {
43 size_t offset = offsetof(struct dri3_egl_surface, loader_drawable);
44 return (struct dri3_egl_surface *)(((void*) draw) - offset);
45 }
46
47 static int
48 egl_dri3_get_swap_interval(struct loader_dri3_drawable *draw)
49 {
50 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
51
52 return dri3_surf->base.SwapInterval;
53 }
54
55 static int
56 egl_dri3_clamp_swap_interval(struct loader_dri3_drawable *draw, int interval)
57 {
58 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
59
60 if (interval > dri3_surf->base.Config->MaxSwapInterval)
61 interval = dri3_surf->base.Config->MaxSwapInterval;
62 else if (interval < dri3_surf->base.Config->MinSwapInterval)
63 interval = dri3_surf->base.Config->MinSwapInterval;
64
65 return interval;
66 }
67
68 static void
69 egl_dri3_set_swap_interval(struct loader_dri3_drawable *draw, int interval)
70 {
71 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
72
73 dri3_surf->base.SwapInterval = interval;
74 }
75
76 static void
77 egl_dri3_set_drawable_size(struct loader_dri3_drawable *draw,
78 int width, int height)
79 {
80 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
81
82 dri3_surf->base.Width = width;
83 dri3_surf->base.Height = height;
84 }
85
86 static bool
87 egl_dri3_in_current_context(struct loader_dri3_drawable *draw)
88 {
89 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
90 _EGLContext *ctx = _eglGetCurrentContext();
91
92 return ctx->Resource.Display == dri3_surf->base.Resource.Display;
93 }
94
95 static __DRIcontext *
96 egl_dri3_get_dri_context(struct loader_dri3_drawable *draw)
97 {
98 _EGLContext *ctx = _eglGetCurrentContext();
99 struct dri2_egl_context *dri2_ctx;
100 if (!ctx)
101 return NULL;
102 dri2_ctx = dri2_egl_context(ctx);
103 return dri2_ctx->dri_context;
104 }
105
106 static __DRIscreen *
107 egl_dri3_get_dri_screen(struct loader_dri3_drawable *draw)
108 {
109 _EGLContext *ctx = _eglGetCurrentContext();
110 struct dri2_egl_context *dri2_ctx;
111 if (!ctx)
112 return NULL;
113 dri2_ctx = dri2_egl_context(ctx);
114 return dri2_egl_display(dri2_ctx->base.Resource.Display)->dri_screen;
115 }
116
117 static void
118 egl_dri3_flush_drawable(struct loader_dri3_drawable *draw, unsigned flags)
119 {
120 struct dri3_egl_surface *dri3_surf = loader_drawable_to_egl_surface(draw);
121 _EGLDisplay *disp = dri3_surf->base.Resource.Display;
122
123 dri2_flush_drawable_for_swapbuffers(disp, &dri3_surf->base);
124 }
125
126 static const struct loader_dri3_vtable egl_dri3_vtable = {
127 .get_swap_interval = egl_dri3_get_swap_interval,
128 .clamp_swap_interval = egl_dri3_clamp_swap_interval,
129 .set_swap_interval = egl_dri3_set_swap_interval,
130 .set_drawable_size = egl_dri3_set_drawable_size,
131 .in_current_context = egl_dri3_in_current_context,
132 .get_dri_context = egl_dri3_get_dri_context,
133 .get_dri_screen = egl_dri3_get_dri_screen,
134 .flush_drawable = egl_dri3_flush_drawable,
135 .show_fps = NULL,
136 };
137
138 static EGLBoolean
139 dri3_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
140 {
141 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
142
143 (void) drv;
144
145 loader_dri3_drawable_fini(&dri3_surf->loader_drawable);
146
147 free(surf);
148
149 return EGL_TRUE;
150 }
151
152 static EGLBoolean
153 dri3_set_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
154 EGLint interval)
155 {
156 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
157
158 loader_dri3_set_swap_interval(&dri3_surf->loader_drawable, interval);
159
160 return EGL_TRUE;
161 }
162
163 static xcb_screen_t *
164 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
165 {
166 for (; iter.rem; --screen, xcb_screen_next(&iter))
167 if (screen == 0)
168 return iter.data;
169
170 return NULL;
171 }
172
173 static _EGLSurface *
174 dri3_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
175 _EGLConfig *conf, void *native_surface,
176 const EGLint *attrib_list)
177 {
178 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
179 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
180 struct dri3_egl_surface *dri3_surf;
181 const __DRIconfig *dri_config;
182 xcb_drawable_t drawable;
183 xcb_screen_iterator_t s;
184 xcb_screen_t *screen;
185
186 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
187 drawable = (uintptr_t) native_surface;
188
189 (void) drv;
190
191 dri3_surf = calloc(1, sizeof *dri3_surf);
192 if (!dri3_surf) {
193 _eglError(EGL_BAD_ALLOC, "dri3_create_surface");
194 return NULL;
195 }
196
197 if (!_eglInitSurface(&dri3_surf->base, disp, type, conf, attrib_list))
198 goto cleanup_surf;
199
200 if (type == EGL_PBUFFER_BIT) {
201 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
202 screen = get_xcb_screen(s, dri2_dpy->screen);
203 if (!screen) {
204 _eglError(EGL_BAD_NATIVE_WINDOW, "dri3_create_surface");
205 goto cleanup_surf;
206 }
207
208 drawable = xcb_generate_id(dri2_dpy->conn);
209 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
210 drawable, screen->root,
211 dri3_surf->base.Width, dri3_surf->base.Height);
212 }
213
214 dri_config = dri2_get_dri_config(dri2_conf, type,
215 dri3_surf->base.GLColorspace);
216
217 if (loader_dri3_drawable_init(dri2_dpy->conn, drawable,
218 dri2_dpy->dri_screen,
219 dri2_dpy->is_different_gpu, dri_config,
220 &dri2_dpy->loader_dri3_ext,
221 &egl_dri3_vtable,
222 &dri3_surf->loader_drawable)) {
223 _eglError(EGL_BAD_ALLOC, "dri3_surface_create");
224 goto cleanup_pixmap;
225 }
226
227 return &dri3_surf->base;
228
229 cleanup_pixmap:
230 if (type == EGL_PBUFFER_BIT)
231 xcb_free_pixmap(dri2_dpy->conn, drawable);
232 cleanup_surf:
233 free(dri3_surf);
234
235 return NULL;
236 }
237
238 static int
239 dri3_authenticate(_EGLDisplay *disp, uint32_t id)
240 {
241 #ifdef HAVE_WAYLAND_PLATFORM
242 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
243
244 if (dri2_dpy->device_name) {
245 _eglLog(_EGL_WARNING,
246 "Wayland client render node authentication is unnecessary");
247 return 0;
248 }
249
250 _eglLog(_EGL_WARNING,
251 "Wayland client primary node authentication isn't supported");
252 #endif
253
254 return -1;
255 }
256
257 /**
258 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
259 */
260 static _EGLSurface *
261 dri3_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
262 _EGLConfig *conf, void *native_window,
263 const EGLint *attrib_list)
264 {
265 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
266 _EGLSurface *surf;
267
268 surf = dri3_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
269 native_window, attrib_list);
270 if (surf != NULL)
271 dri3_set_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval);
272
273 return surf;
274 }
275
276 static _EGLSurface *
277 dri3_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
278 _EGLConfig *conf, void *native_pixmap,
279 const EGLint *attrib_list)
280 {
281 return dri3_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
282 native_pixmap, attrib_list);
283 }
284
285 static _EGLSurface *
286 dri3_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
287 _EGLConfig *conf, const EGLint *attrib_list)
288 {
289 return dri3_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
290 XCB_WINDOW_NONE, attrib_list);
291 }
292
293 static EGLBoolean
294 dri3_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
295 EGLuint64KHR *ust, EGLuint64KHR *msc,
296 EGLuint64KHR *sbc)
297 {
298 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surface);
299
300 return loader_dri3_wait_for_msc(&dri3_surf->loader_drawable, 0, 0, 0,
301 (int64_t *) ust, (int64_t *) msc,
302 (int64_t *) sbc) ? EGL_TRUE : EGL_FALSE;
303 }
304
305 static _EGLImage *
306 dri3_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
307 EGLClientBuffer buffer, const EGLint *attr_list)
308 {
309 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
310 struct dri2_egl_image *dri2_img;
311 xcb_drawable_t drawable;
312 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
313 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
314 unsigned int format;
315
316 drawable = (xcb_drawable_t) (uintptr_t) buffer;
317 bp_cookie = xcb_dri3_buffer_from_pixmap(dri2_dpy->conn, drawable);
318 bp_reply = xcb_dri3_buffer_from_pixmap_reply(dri2_dpy->conn,
319 bp_cookie, NULL);
320 if (!bp_reply) {
321 _eglError(EGL_BAD_ALLOC, "xcb_dri3_buffer_from_pixmap");
322 return NULL;
323 }
324
325 switch (bp_reply->depth) {
326 case 16:
327 format = __DRI_IMAGE_FORMAT_RGB565;
328 break;
329 case 24:
330 format = __DRI_IMAGE_FORMAT_XRGB8888;
331 break;
332 case 32:
333 format = __DRI_IMAGE_FORMAT_ARGB8888;
334 break;
335 default:
336 _eglError(EGL_BAD_PARAMETER,
337 "dri3_create_image_khr: unsupported pixmap depth");
338 free(bp_reply);
339 return EGL_NO_IMAGE_KHR;
340 }
341
342 dri2_img = malloc(sizeof *dri2_img);
343 if (!dri2_img) {
344 _eglError(EGL_BAD_ALLOC, "dri3_create_image_khr");
345 return EGL_NO_IMAGE_KHR;
346 }
347
348 if (!_eglInitImage(&dri2_img->base, disp)) {
349 free(dri2_img);
350 return EGL_NO_IMAGE_KHR;
351 }
352
353 dri2_img->dri_image = loader_dri3_create_image(dri2_dpy->conn,
354 bp_reply,
355 format,
356 dri2_dpy->dri_screen,
357 dri2_dpy->image,
358 dri2_img);
359
360 free(bp_reply);
361
362 return &dri2_img->base;
363 }
364
365 static _EGLImage *
366 dri3_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
367 _EGLContext *ctx, EGLenum target,
368 EGLClientBuffer buffer, const EGLint *attr_list)
369 {
370 (void) drv;
371
372 switch (target) {
373 case EGL_NATIVE_PIXMAP_KHR:
374 return dri3_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
375 default:
376 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
377 }
378 }
379
380 /**
381 * Called by the driver when it needs to update the real front buffer with the
382 * contents of its fake front buffer.
383 */
384 static void
385 dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
386 {
387 /* There does not seem to be any kind of consensus on whether we should
388 * support front-buffer rendering or not:
389 * http://lists.freedesktop.org/archives/mesa-dev/2013-June/040129.html
390 */
391 _eglLog(_EGL_WARNING, "FIXME: egl/x11 doesn't support front buffer rendering.");
392 (void) driDrawable;
393 (void) loaderPrivate;
394 }
395
396 const __DRIimageLoaderExtension dri3_image_loader_extension = {
397 .base = { __DRI_IMAGE_LOADER, 1 },
398
399 .getBuffers = loader_dri3_get_buffers,
400 .flushFrontBuffer = dri3_flush_front_buffer,
401 };
402
403 static EGLBoolean
404 dri3_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
405 {
406 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(draw);
407
408 /* No-op for a pixmap or pbuffer surface */
409 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
410 return 0;
411
412 return loader_dri3_swap_buffers_msc(&dri3_surf->loader_drawable,
413 0, 0, 0, 0,
414 draw->SwapBehavior == EGL_BUFFER_PRESERVED) != -1;
415 }
416
417 static EGLBoolean
418 dri3_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
419 void *native_pixmap_target)
420 {
421 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
422 xcb_pixmap_t target;
423
424 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
425 target = (uintptr_t) native_pixmap_target;
426
427 loader_dri3_copy_drawable(&dri3_surf->loader_drawable, target,
428 dri3_surf->loader_drawable.drawable);
429
430 return EGL_TRUE;
431 }
432
433 static int
434 dri3_query_buffer_age(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
435 {
436 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
437
438 return loader_dri3_query_buffer_age(&dri3_surf->loader_drawable);
439 }
440
441 static __DRIdrawable *
442 dri3_get_dri_drawable(_EGLSurface *surf)
443 {
444 struct dri3_egl_surface *dri3_surf = dri3_egl_surface(surf);
445
446 return dri3_surf->loader_drawable.dri_drawable;
447 }
448
449 struct dri2_egl_display_vtbl dri3_x11_display_vtbl = {
450 .authenticate = dri3_authenticate,
451 .create_window_surface = dri3_create_window_surface,
452 .create_pixmap_surface = dri3_create_pixmap_surface,
453 .create_pbuffer_surface = dri3_create_pbuffer_surface,
454 .destroy_surface = dri3_destroy_surface,
455 .create_image = dri3_create_image_khr,
456 .swap_interval = dri3_set_swap_interval,
457 .swap_buffers = dri3_swap_buffers,
458 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
459 .swap_buffers_region = dri2_fallback_swap_buffers_region,
460 .post_sub_buffer = dri2_fallback_post_sub_buffer,
461 .copy_buffers = dri3_copy_buffers,
462 .query_buffer_age = dri3_query_buffer_age,
463 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
464 .get_sync_values = dri3_get_sync_values,
465 .get_dri_drawable = dri3_get_dri_drawable,
466 };
467
468 EGLBoolean
469 dri3_x11_connect(struct dri2_egl_display *dri2_dpy)
470 {
471 xcb_dri3_query_version_reply_t *dri3_query;
472 xcb_dri3_query_version_cookie_t dri3_query_cookie;
473 xcb_present_query_version_reply_t *present_query;
474 xcb_present_query_version_cookie_t present_query_cookie;
475 xcb_generic_error_t *error;
476 xcb_screen_iterator_t s;
477 xcb_screen_t *screen;
478 const xcb_query_extension_reply_t *extension;
479
480 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri3_id);
481 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_present_id);
482
483 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri3_id);
484 if (!(extension && extension->present))
485 return EGL_FALSE;
486
487 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_present_id);
488 if (!(extension && extension->present))
489 return EGL_FALSE;
490
491 dri3_query_cookie = xcb_dri3_query_version(dri2_dpy->conn,
492 XCB_DRI3_MAJOR_VERSION,
493 XCB_DRI3_MINOR_VERSION);
494
495 present_query_cookie = xcb_present_query_version(dri2_dpy->conn,
496 XCB_PRESENT_MAJOR_VERSION,
497 XCB_PRESENT_MINOR_VERSION);
498
499 dri3_query =
500 xcb_dri3_query_version_reply(dri2_dpy->conn, dri3_query_cookie, &error);
501 if (dri3_query == NULL || error != NULL) {
502 _eglLog(_EGL_WARNING, "DRI3: failed to query the version");
503 free(dri3_query);
504 free(error);
505 return EGL_FALSE;
506 }
507 free(dri3_query);
508
509 present_query =
510 xcb_present_query_version_reply(dri2_dpy->conn,
511 present_query_cookie, &error);
512 if (present_query == NULL || error != NULL) {
513 _eglLog(_EGL_WARNING, "DRI3: failed to query Present version");
514 free(present_query);
515 free(error);
516 return EGL_FALSE;
517 }
518 free(present_query);
519
520 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
521 screen = get_xcb_screen(s, dri2_dpy->screen);
522 if (!screen) {
523 _eglError(EGL_BAD_NATIVE_WINDOW, "dri3_x11_connect");
524 return EGL_FALSE;
525 }
526
527 dri2_dpy->fd = loader_dri3_open(dri2_dpy->conn, screen->root, 0);
528 if (dri2_dpy->fd < 0) {
529 int conn_error = xcb_connection_has_error(dri2_dpy->conn);
530 _eglLog(_EGL_WARNING, "DRI3: Screen seems not DRI3 capable");
531
532 if (conn_error)
533 _eglLog(_EGL_WARNING, "DRI3: Failed to initialize");
534
535 return EGL_FALSE;
536 }
537
538 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd, &dri2_dpy->is_different_gpu);
539
540 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
541 if (!dri2_dpy->driver_name) {
542 _eglLog(_EGL_WARNING, "DRI3: No driver found");
543 close(dri2_dpy->fd);
544 return EGL_FALSE;
545 }
546
547 #ifdef HAVE_WAYLAND_PLATFORM
548 /* Only try to get a render device name since dri3 doesn't provide a
549 * mechanism for authenticating client opened device node fds. If this
550 * fails then don't advertise the extension. */
551 dri2_dpy->device_name = drmGetRenderDeviceNameFromFd(dri2_dpy->fd);
552 #endif
553
554 return EGL_TRUE;
555 }