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