egl/dri2: rework dri2_egl_display::extensions storage
[mesa.git] / src / egl / drivers / dri2 / platform_x11.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <limits.h>
34 #include <dlfcn.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #ifdef HAVE_LIBDRM
39 #include <xf86drm.h>
40 #endif
41 #include <sys/types.h>
42 #include <sys/stat.h>
43
44 #include "egl_dri2.h"
45 #include "egl_dri2_fallbacks.h"
46 #include "loader.h"
47
48 #ifdef HAVE_DRI3
49 #include "platform_x11_dri3.h"
50 #endif
51
52 static EGLBoolean
53 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
54 EGLint interval);
55
56 static void
57 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
58 struct dri2_egl_surface * dri2_surf)
59 {
60 uint32_t mask;
61 const uint32_t function = GXcopy;
62 uint32_t valgc[2];
63
64 /* create GC's */
65 dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
66 mask = XCB_GC_FUNCTION;
67 xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask, &function);
68
69 dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
70 mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
71 valgc[0] = function;
72 valgc[1] = False;
73 xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask, valgc);
74 switch (dri2_surf->depth) {
75 case 32:
76 case 24:
77 dri2_surf->bytes_per_pixel = 4;
78 break;
79 case 16:
80 dri2_surf->bytes_per_pixel = 2;
81 break;
82 case 8:
83 dri2_surf->bytes_per_pixel = 1;
84 break;
85 case 0:
86 dri2_surf->bytes_per_pixel = 0;
87 break;
88 default:
89 _eglLog(_EGL_WARNING, "unsupported depth %d", dri2_surf->depth);
90 }
91 }
92
93 static void
94 swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
95 struct dri2_egl_surface * dri2_surf)
96 {
97 xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
98 xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
99 }
100
101 static void
102 swrastGetDrawableInfo(__DRIdrawable * draw,
103 int *x, int *y, int *w, int *h,
104 void *loaderPrivate)
105 {
106 struct dri2_egl_surface *dri2_surf = loaderPrivate;
107 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
108
109 xcb_get_geometry_cookie_t cookie;
110 xcb_get_geometry_reply_t *reply;
111 xcb_generic_error_t *error;
112
113 *w = *h = 0;
114 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
115 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
116 if (reply == NULL)
117 return;
118
119 if (error != NULL) {
120 _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
121 free(error);
122 } else {
123 *w = reply->width;
124 *h = reply->height;
125 }
126 free(reply);
127 }
128
129 static void
130 swrastPutImage(__DRIdrawable * draw, int op,
131 int x, int y, int w, int h,
132 char *data, void *loaderPrivate)
133 {
134 struct dri2_egl_surface *dri2_surf = loaderPrivate;
135 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
136
137 xcb_gcontext_t gc;
138
139 switch (op) {
140 case __DRI_SWRAST_IMAGE_OP_DRAW:
141 gc = dri2_surf->gc;
142 break;
143 case __DRI_SWRAST_IMAGE_OP_SWAP:
144 gc = dri2_surf->swapgc;
145 break;
146 default:
147 return;
148 }
149
150 xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable,
151 gc, w, h, x, y, 0, dri2_surf->depth,
152 w*h*dri2_surf->bytes_per_pixel, (const uint8_t *)data);
153 }
154
155 static void
156 swrastGetImage(__DRIdrawable * read,
157 int x, int y, int w, int h,
158 char *data, void *loaderPrivate)
159 {
160 struct dri2_egl_surface *dri2_surf = loaderPrivate;
161 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
162
163 xcb_get_image_cookie_t cookie;
164 xcb_get_image_reply_t *reply;
165 xcb_generic_error_t *error;
166
167 cookie = xcb_get_image (dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
168 dri2_surf->drawable, x, y, w, h, ~0);
169 reply = xcb_get_image_reply (dri2_dpy->conn, cookie, &error);
170 if (reply == NULL)
171 return;
172
173 if (error != NULL) {
174 _eglLog(_EGL_WARNING, "error in xcb_get_image");
175 free(error);
176 } else {
177 uint32_t bytes = xcb_get_image_data_length(reply);
178 uint8_t *idata = xcb_get_image_data(reply);
179 memcpy(data, idata, bytes);
180 }
181 free(reply);
182 }
183
184
185 static xcb_screen_t *
186 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
187 {
188 for (; iter.rem; --screen, xcb_screen_next(&iter))
189 if (screen == 0)
190 return iter.data;
191
192 return NULL;
193 }
194
195
196 /**
197 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
198 */
199 static _EGLSurface *
200 dri2_x11_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
201 _EGLConfig *conf, void *native_surface,
202 const EGLint *attrib_list)
203 {
204 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
205 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
206 struct dri2_egl_surface *dri2_surf;
207 xcb_get_geometry_cookie_t cookie;
208 xcb_get_geometry_reply_t *reply;
209 xcb_screen_iterator_t s;
210 xcb_generic_error_t *error;
211 xcb_drawable_t drawable;
212 xcb_screen_t *screen;
213 const __DRIconfig *config;
214
215 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
216 drawable = (uintptr_t) native_surface;
217
218 (void) drv;
219
220 dri2_surf = malloc(sizeof *dri2_surf);
221 if (!dri2_surf) {
222 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
223 return NULL;
224 }
225
226 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
227 goto cleanup_surf;
228
229 dri2_surf->region = XCB_NONE;
230 if (type == EGL_PBUFFER_BIT) {
231 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
232 screen = get_xcb_screen(s, dri2_dpy->screen);
233 if (!screen) {
234 _eglError(EGL_BAD_ALLOC, "failed to get xcb screen");
235 goto cleanup_surf;
236 }
237
238 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
239 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
240 dri2_surf->drawable, screen->root,
241 dri2_surf->base.Width, dri2_surf->base.Height);
242 } else {
243 if (!drawable) {
244 if (type == EGL_WINDOW_BIT)
245 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
246 else
247 _eglError(EGL_BAD_NATIVE_PIXMAP, "dri2_create_surface");
248 goto cleanup_surf;
249 }
250 dri2_surf->drawable = drawable;
251 }
252
253 config = dri2_get_dri_config(dri2_conf, type,
254 dri2_surf->base.GLColorspace);
255
256 if (dri2_dpy->dri2) {
257 dri2_surf->dri_drawable =
258 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
259 dri2_surf);
260 } else {
261 assert(dri2_dpy->swrast);
262 dri2_surf->dri_drawable =
263 (*dri2_dpy->swrast->createNewDrawable)(dri2_dpy->dri_screen, config,
264 dri2_surf);
265 }
266
267 if (dri2_surf->dri_drawable == NULL) {
268 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
269 goto cleanup_pixmap;
270 }
271
272 if (type != EGL_PBUFFER_BIT) {
273 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
274 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
275 if (error != NULL) {
276 if (error->error_code == BadAlloc)
277 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
278 else if (type == EGL_WINDOW_BIT)
279 _eglError(EGL_BAD_NATIVE_WINDOW, "xcb_get_geometry");
280 else
281 _eglError(EGL_BAD_NATIVE_PIXMAP, "xcb_get_geometry");
282 free(error);
283 goto cleanup_dri_drawable;
284 } else if (reply == NULL) {
285 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
286 goto cleanup_dri_drawable;
287 }
288
289 dri2_surf->base.Width = reply->width;
290 dri2_surf->base.Height = reply->height;
291 dri2_surf->depth = reply->depth;
292 free(reply);
293 }
294
295 if (dri2_dpy->dri2) {
296 xcb_void_cookie_t cookie;
297 int conn_error;
298
299 cookie = xcb_dri2_create_drawable_checked(dri2_dpy->conn,
300 dri2_surf->drawable);
301 error = xcb_request_check(dri2_dpy->conn, cookie);
302 conn_error = xcb_connection_has_error(dri2_dpy->conn);
303 if (conn_error || error != NULL) {
304 if (type == EGL_PBUFFER_BIT || conn_error || error->error_code == BadAlloc)
305 _eglError(EGL_BAD_ALLOC, "xcb_dri2_create_drawable_checked");
306 else if (type == EGL_WINDOW_BIT)
307 _eglError(EGL_BAD_NATIVE_WINDOW,
308 "xcb_dri2_create_drawable_checked");
309 else
310 _eglError(EGL_BAD_NATIVE_PIXMAP,
311 "xcb_dri2_create_drawable_checked");
312 free(error);
313 goto cleanup_dri_drawable;
314 }
315 } else {
316 if (type == EGL_PBUFFER_BIT) {
317 dri2_surf->depth = _eglGetConfigKey(conf, EGL_BUFFER_SIZE);
318 }
319 swrastCreateDrawable(dri2_dpy, dri2_surf);
320 }
321
322 /* we always copy the back buffer to front */
323 dri2_surf->base.PostSubBufferSupportedNV = EGL_TRUE;
324
325 return &dri2_surf->base;
326
327 cleanup_dri_drawable:
328 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
329 cleanup_pixmap:
330 if (type == EGL_PBUFFER_BIT)
331 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
332 cleanup_surf:
333 free(dri2_surf);
334
335 return NULL;
336 }
337
338 /**
339 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
340 */
341 static _EGLSurface *
342 dri2_x11_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
343 _EGLConfig *conf, void *native_window,
344 const EGLint *attrib_list)
345 {
346 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
347 _EGLSurface *surf;
348
349 surf = dri2_x11_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
350 native_window, attrib_list);
351 if (surf != NULL) {
352 /* When we first create the DRI2 drawable, its swap interval on the
353 * server side is 1.
354 */
355 surf->SwapInterval = 1;
356
357 /* Override that with a driconf-set value. */
358 dri2_x11_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval);
359 }
360
361 return surf;
362 }
363
364 static _EGLSurface *
365 dri2_x11_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
366 _EGLConfig *conf, void *native_pixmap,
367 const EGLint *attrib_list)
368 {
369 return dri2_x11_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
370 native_pixmap, attrib_list);
371 }
372
373 static _EGLSurface *
374 dri2_x11_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
375 _EGLConfig *conf, const EGLint *attrib_list)
376 {
377 return dri2_x11_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
378 XCB_WINDOW_NONE, attrib_list);
379 }
380
381 static EGLBoolean
382 dri2_x11_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
383 {
384 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
385 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
386
387 (void) drv;
388
389 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
390
391 if (dri2_dpy->dri2) {
392 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
393 } else {
394 assert(dri2_dpy->swrast);
395 swrastDestroyDrawable(dri2_dpy, dri2_surf);
396 }
397
398 if (surf->Type == EGL_PBUFFER_BIT)
399 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
400
401 free(surf);
402
403 return EGL_TRUE;
404 }
405
406 /**
407 * Process list of buffer received from the server
408 *
409 * Processes the list of buffers received in a reply from the server to either
410 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
411 */
412 static void
413 dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
414 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
415 {
416 struct dri2_egl_display *dri2_dpy =
417 dri2_egl_display(dri2_surf->base.Resource.Display);
418 xcb_rectangle_t rectangle;
419 unsigned i;
420
421 dri2_surf->buffer_count = count;
422 dri2_surf->have_fake_front = 0;
423
424 /* This assumes the DRI2 buffer attachment tokens matches the
425 * __DRIbuffer tokens. */
426 for (i = 0; i < count; i++) {
427 dri2_surf->buffers[i].attachment = buffers[i].attachment;
428 dri2_surf->buffers[i].name = buffers[i].name;
429 dri2_surf->buffers[i].pitch = buffers[i].pitch;
430 dri2_surf->buffers[i].cpp = buffers[i].cpp;
431 dri2_surf->buffers[i].flags = buffers[i].flags;
432
433 /* We only use the DRI drivers single buffer configs. This
434 * means that if we try to render to a window, DRI2 will give us
435 * the fake front buffer, which we'll use as a back buffer.
436 * Note that EGL doesn't require that several clients rendering
437 * to the same window must see the same aux buffers. */
438 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
439 dri2_surf->have_fake_front = 1;
440 }
441
442 if (dri2_surf->region != XCB_NONE)
443 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
444
445 rectangle.x = 0;
446 rectangle.y = 0;
447 rectangle.width = dri2_surf->base.Width;
448 rectangle.height = dri2_surf->base.Height;
449 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
450 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
451 }
452
453 static __DRIbuffer *
454 dri2_x11_get_buffers(__DRIdrawable * driDrawable,
455 int *width, int *height,
456 unsigned int *attachments, int count,
457 int *out_count, void *loaderPrivate)
458 {
459 struct dri2_egl_surface *dri2_surf = loaderPrivate;
460 struct dri2_egl_display *dri2_dpy =
461 dri2_egl_display(dri2_surf->base.Resource.Display);
462 xcb_dri2_dri2_buffer_t *buffers;
463 xcb_dri2_get_buffers_reply_t *reply;
464 xcb_dri2_get_buffers_cookie_t cookie;
465
466 (void) driDrawable;
467
468 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
469 dri2_surf->drawable,
470 count, count, attachments);
471 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
472 if (reply == NULL)
473 return NULL;
474 buffers = xcb_dri2_get_buffers_buffers (reply);
475 if (buffers == NULL)
476 return NULL;
477
478 *out_count = reply->count;
479 dri2_surf->base.Width = *width = reply->width;
480 dri2_surf->base.Height = *height = reply->height;
481 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
482
483 free(reply);
484
485 return dri2_surf->buffers;
486 }
487
488 static __DRIbuffer *
489 dri2_x11_get_buffers_with_format(__DRIdrawable * driDrawable,
490 int *width, int *height,
491 unsigned int *attachments, int count,
492 int *out_count, void *loaderPrivate)
493 {
494 struct dri2_egl_surface *dri2_surf = loaderPrivate;
495 struct dri2_egl_display *dri2_dpy =
496 dri2_egl_display(dri2_surf->base.Resource.Display);
497 xcb_dri2_dri2_buffer_t *buffers;
498 xcb_dri2_get_buffers_with_format_reply_t *reply;
499 xcb_dri2_get_buffers_with_format_cookie_t cookie;
500 xcb_dri2_attach_format_t *format_attachments;
501
502 (void) driDrawable;
503
504 format_attachments = (xcb_dri2_attach_format_t *) attachments;
505 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
506 dri2_surf->drawable,
507 count, count,
508 format_attachments);
509
510 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
511 cookie, NULL);
512 if (reply == NULL)
513 return NULL;
514
515 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
516 dri2_surf->base.Width = *width = reply->width;
517 dri2_surf->base.Height = *height = reply->height;
518 *out_count = reply->count;
519 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
520
521 free(reply);
522
523 return dri2_surf->buffers;
524 }
525
526 static void
527 dri2_x11_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
528 {
529 (void) driDrawable;
530
531 /* FIXME: Does EGL support front buffer rendering at all? */
532
533 #if 0
534 struct dri2_egl_surface *dri2_surf = loaderPrivate;
535
536 dri2WaitGL(dri2_surf);
537 #else
538 (void) loaderPrivate;
539 #endif
540 }
541
542 static int
543 dri2_x11_do_authenticate(struct dri2_egl_display *dri2_dpy, uint32_t id)
544 {
545 xcb_dri2_authenticate_reply_t *authenticate;
546 xcb_dri2_authenticate_cookie_t authenticate_cookie;
547 xcb_screen_iterator_t s;
548 xcb_screen_t *screen;
549 int ret = 0;
550
551 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
552
553 screen = get_xcb_screen(s, dri2_dpy->screen);
554 if (!screen) {
555 _eglLog(_EGL_WARNING, "DRI2: failed to get xcb screen");
556 return -1;
557 }
558
559 authenticate_cookie =
560 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, screen->root, id);
561 authenticate =
562 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
563
564 if (authenticate == NULL || !authenticate->authenticated)
565 ret = -1;
566
567 free(authenticate);
568
569 return ret;
570 }
571
572 static EGLBoolean
573 dri2_x11_local_authenticate(struct dri2_egl_display *dri2_dpy)
574 {
575 #ifdef HAVE_LIBDRM
576 drm_magic_t magic;
577
578 if (drmGetMagic(dri2_dpy->fd, &magic)) {
579 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
580 return EGL_FALSE;
581 }
582
583 if (dri2_x11_do_authenticate(dri2_dpy, magic) < 0) {
584 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
585 return EGL_FALSE;
586 }
587 #endif
588 return EGL_TRUE;
589 }
590
591 static EGLBoolean
592 dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
593 {
594 xcb_xfixes_query_version_reply_t *xfixes_query;
595 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
596 xcb_dri2_query_version_reply_t *dri2_query;
597 xcb_dri2_query_version_cookie_t dri2_query_cookie;
598 xcb_dri2_connect_reply_t *connect;
599 xcb_dri2_connect_cookie_t connect_cookie;
600 xcb_generic_error_t *error;
601 xcb_screen_iterator_t s;
602 xcb_screen_t *screen;
603 char *driver_name, *loader_driver_name, *device_name;
604 const xcb_query_extension_reply_t *extension;
605
606 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
607 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
608
609 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
610 if (!(extension && extension->present))
611 return EGL_FALSE;
612
613 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
614 if (!(extension && extension->present))
615 return EGL_FALSE;
616
617 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
618 XCB_XFIXES_MAJOR_VERSION,
619 XCB_XFIXES_MINOR_VERSION);
620
621 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
622 XCB_DRI2_MAJOR_VERSION,
623 XCB_DRI2_MINOR_VERSION);
624
625 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
626 screen = get_xcb_screen(s, dri2_dpy->screen);
627 if (!screen) {
628 _eglLog(_EGL_WARNING, "DRI2: failed to get xcb screen");
629 return EGL_FALSE;
630 }
631 connect_cookie = xcb_dri2_connect_unchecked(dri2_dpy->conn, screen->root,
632 XCB_DRI2_DRIVER_TYPE_DRI);
633
634 xfixes_query =
635 xcb_xfixes_query_version_reply (dri2_dpy->conn,
636 xfixes_query_cookie, &error);
637 if (xfixes_query == NULL ||
638 error != NULL || xfixes_query->major_version < 2) {
639 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
640 free(error);
641 return EGL_FALSE;
642 }
643 free(xfixes_query);
644
645 dri2_query =
646 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
647 if (dri2_query == NULL || error != NULL) {
648 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
649 free(error);
650 return EGL_FALSE;
651 }
652 dri2_dpy->dri2_major = dri2_query->major_version;
653 dri2_dpy->dri2_minor = dri2_query->minor_version;
654 free(dri2_query);
655
656 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
657 if (connect == NULL ||
658 connect->driver_name_length + connect->device_name_length == 0) {
659 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
660 return EGL_FALSE;
661 }
662
663 device_name = xcb_dri2_connect_device_name (connect);
664
665 dri2_dpy->fd = loader_open_device(device_name);
666 if (dri2_dpy->fd == -1) {
667 _eglLog(_EGL_WARNING,
668 "DRI2: could not open %s (%s)", device_name, strerror(errno));
669 free(connect);
670 return EGL_FALSE;
671 }
672
673 if (!dri2_x11_local_authenticate(dri2_dpy)) {
674 close(dri2_dpy->fd);
675 free(connect);
676 return EGL_FALSE;
677 }
678
679 driver_name = xcb_dri2_connect_driver_name (connect);
680
681 /* If Mesa knows about the appropriate driver for this fd, then trust it.
682 * Otherwise, default to the server's value.
683 */
684 loader_driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
685 if (loader_driver_name) {
686 dri2_dpy->driver_name = loader_driver_name;
687 } else {
688 dri2_dpy->driver_name =
689 strndup(driver_name,
690 xcb_dri2_connect_driver_name_length(connect));
691 }
692
693 if (dri2_dpy->driver_name == NULL) {
694 close(dri2_dpy->fd);
695 free(dri2_dpy->driver_name);
696 free(connect);
697 return EGL_FALSE;
698 }
699
700 #ifdef HAVE_WAYLAND_PLATFORM
701 dri2_dpy->device_name =
702 strndup(device_name,
703 xcb_dri2_connect_device_name_length(connect));
704 #endif
705
706 free(connect);
707
708 return EGL_TRUE;
709 }
710
711 static int
712 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
713 {
714 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
715
716 return dri2_x11_do_authenticate(dri2_dpy, id);
717 }
718
719 static EGLBoolean
720 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
721 _EGLDisplay *disp, bool supports_preserved)
722 {
723 xcb_screen_iterator_t s;
724 xcb_depth_iterator_t d;
725 xcb_visualtype_t *visuals;
726 int i, j, id;
727 unsigned int rgba_masks[4];
728 EGLint surface_type;
729 EGLint config_attrs[] = {
730 EGL_NATIVE_VISUAL_ID, 0,
731 EGL_NATIVE_VISUAL_TYPE, 0,
732 EGL_NONE
733 };
734
735 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
736 d = xcb_screen_allowed_depths_iterator(get_xcb_screen(s, dri2_dpy->screen));
737 id = 1;
738
739 surface_type =
740 EGL_WINDOW_BIT |
741 EGL_PIXMAP_BIT |
742 EGL_PBUFFER_BIT;
743
744 if (supports_preserved)
745 surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
746
747 while (d.rem > 0) {
748 EGLBoolean class_added[6] = { 0, };
749
750 visuals = xcb_depth_visuals(d.data);
751 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
752 if (class_added[visuals[i]._class])
753 continue;
754
755 class_added[visuals[i]._class] = EGL_TRUE;
756 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
757 config_attrs[1] = visuals[i].visual_id;
758 config_attrs[3] = visuals[i]._class;
759
760 rgba_masks[0] = visuals[i].red_mask;
761 rgba_masks[1] = visuals[i].green_mask;
762 rgba_masks[2] = visuals[i].blue_mask;
763 rgba_masks[3] = 0;
764 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
765 surface_type, config_attrs, rgba_masks);
766
767 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
768 * Otherwise it will only match a 32-bit RGBA visual. On a
769 * composited window manager on X11, this will make all of the
770 * EGLConfigs with destination alpha get blended by the
771 * compositor. This is probably not what the application
772 * wants... especially on drivers that only have 32-bit RGBA
773 * EGLConfigs! */
774 if (d.data->depth == 24) {
775 rgba_masks[3] =
776 ~(rgba_masks[0] | rgba_masks[1] | rgba_masks[2]);
777 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
778 surface_type, config_attrs, rgba_masks);
779 }
780 }
781 }
782
783 xcb_depth_next(&d);
784 }
785
786 if (!_eglGetArraySize(disp->Configs)) {
787 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
788 return EGL_FALSE;
789 }
790
791 return EGL_TRUE;
792 }
793
794 static EGLBoolean
795 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
796 _EGLSurface *draw, xcb_xfixes_region_t region)
797 {
798 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
799 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
800 enum xcb_dri2_attachment_t render_attachment;
801 xcb_dri2_copy_region_cookie_t cookie;
802
803 /* No-op for a pixmap or pbuffer surface */
804 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
805 return EGL_TRUE;
806
807 if (dri2_dpy->flush)
808 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
809
810 if (dri2_surf->have_fake_front)
811 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
812 else
813 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
814
815 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
816 dri2_surf->drawable,
817 region,
818 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
819 render_attachment);
820 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
821
822 return EGL_TRUE;
823 }
824
825 static int64_t
826 dri2_x11_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
827 int64_t msc, int64_t divisor, int64_t remainder)
828 {
829 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
830 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
831 uint32_t msc_hi = msc >> 32;
832 uint32_t msc_lo = msc & 0xffffffff;
833 uint32_t divisor_hi = divisor >> 32;
834 uint32_t divisor_lo = divisor & 0xffffffff;
835 uint32_t remainder_hi = remainder >> 32;
836 uint32_t remainder_lo = remainder & 0xffffffff;
837 xcb_dri2_swap_buffers_cookie_t cookie;
838 xcb_dri2_swap_buffers_reply_t *reply;
839 int64_t swap_count = -1;
840
841 /* No-op for a pixmap or pbuffer surface */
842 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
843 return 0;
844
845 if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available)
846 return dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
847
848 dri2_flush_drawable_for_swapbuffers(disp, draw);
849
850 cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn, dri2_surf->drawable,
851 msc_hi, msc_lo, divisor_hi, divisor_lo, remainder_hi, remainder_lo);
852
853 reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
854
855 if (reply) {
856 swap_count = (((int64_t)reply->swap_hi) << 32) | reply->swap_lo;
857 free(reply);
858 }
859
860 /* Since we aren't watching for the server's invalidate events like we're
861 * supposed to (due to XCB providing no mechanism for filtering the events
862 * the way xlib does), and SwapBuffers is a common cause of invalidate
863 * events, just shove one down to the driver, even though we haven't told
864 * the driver that we're the kind of loader that provides reliable
865 * invalidate events. This causes the driver to request buffers again at
866 * its next draw, so that we get the correct buffers if a pageflip
867 * happened. The driver should still be using the viewport hack to catch
868 * window resizes.
869 */
870 if (dri2_dpy->flush &&
871 dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
872 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
873
874 return swap_count;
875 }
876
877 static EGLBoolean
878 dri2_x11_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
879 {
880 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
881 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
882
883 if (dri2_dpy->dri2) {
884 if (dri2_x11_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1) {
885 return EGL_TRUE;
886 }
887 /* Swap failed with a window drawable. */
888 _eglError(EGL_BAD_NATIVE_WINDOW, __FUNCTION__);
889 return EGL_FALSE;
890 } else {
891 assert(dri2_dpy->swrast);
892
893 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
894 return EGL_TRUE;
895 }
896 }
897
898 static EGLBoolean
899 dri2_x11_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp,
900 _EGLSurface *draw,
901 EGLint numRects, const EGLint *rects)
902 {
903 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
904 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
905 EGLBoolean ret;
906 xcb_xfixes_region_t region;
907 xcb_rectangle_t rectangles[16];
908 int i;
909
910 if (numRects > (int)ARRAY_SIZE(rectangles))
911 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
912
913 for (i = 0; i < numRects; i++) {
914 rectangles[i].x = rects[i * 4];
915 rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
916 rectangles[i].width = rects[i * 4 + 2];
917 rectangles[i].height = rects[i * 4 + 3];
918 }
919
920 region = xcb_generate_id(dri2_dpy->conn);
921 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
922 ret = dri2_copy_region(drv, disp, draw, region);
923 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
924
925 return ret;
926 }
927
928 static EGLBoolean
929 dri2_x11_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
930 EGLint x, EGLint y, EGLint width, EGLint height)
931 {
932 const EGLint rect[4] = { x, y, width, height };
933
934 if (x < 0 || y < 0 || width < 0 || height < 0)
935 _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
936
937 return dri2_x11_swap_buffers_region(drv, disp, draw, 1, rect);
938 }
939
940 static EGLBoolean
941 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
942 EGLint interval)
943 {
944 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
945 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
946
947 if (interval > surf->Config->MaxSwapInterval)
948 interval = surf->Config->MaxSwapInterval;
949 else if (interval < surf->Config->MinSwapInterval)
950 interval = surf->Config->MinSwapInterval;
951
952 if (interval != surf->SwapInterval && dri2_dpy->swap_available)
953 xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
954
955 surf->SwapInterval = interval;
956
957 return EGL_TRUE;
958 }
959
960 static EGLBoolean
961 dri2_x11_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
962 void *native_pixmap_target)
963 {
964 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
965 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
966 xcb_gcontext_t gc;
967 xcb_pixmap_t target;
968
969 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
970 target = (uintptr_t) native_pixmap_target;
971
972 (void) drv;
973
974 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
975
976 gc = xcb_generate_id(dri2_dpy->conn);
977 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
978 xcb_copy_area(dri2_dpy->conn,
979 dri2_surf->drawable,
980 target,
981 gc,
982 0, 0,
983 0, 0,
984 dri2_surf->base.Width,
985 dri2_surf->base.Height);
986 xcb_free_gc(dri2_dpy->conn, gc);
987
988 return EGL_TRUE;
989 }
990
991 static _EGLImage *
992 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
993 EGLClientBuffer buffer, const EGLint *attr_list)
994 {
995 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
996 struct dri2_egl_image *dri2_img;
997 unsigned int attachments[1];
998 xcb_drawable_t drawable;
999 xcb_dri2_get_buffers_cookie_t buffers_cookie;
1000 xcb_dri2_get_buffers_reply_t *buffers_reply;
1001 xcb_dri2_dri2_buffer_t *buffers;
1002 xcb_get_geometry_cookie_t geometry_cookie;
1003 xcb_get_geometry_reply_t *geometry_reply;
1004 xcb_generic_error_t *error;
1005 int stride, format;
1006
1007 (void) ctx;
1008
1009 drawable = (xcb_drawable_t) (uintptr_t) buffer;
1010 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1011 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1012 buffers_cookie =
1013 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1014 drawable, 1, 1, attachments);
1015 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1016 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1017 buffers_cookie, NULL);
1018 if (buffers_reply == NULL)
1019 return NULL;
1020
1021 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1022 if (buffers == NULL) {
1023 return NULL;
1024 }
1025
1026 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1027 geometry_cookie, &error);
1028 if (geometry_reply == NULL || error != NULL) {
1029 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1030 free(error);
1031 free(buffers_reply);
1032 return NULL;
1033 }
1034
1035 switch (geometry_reply->depth) {
1036 case 16:
1037 format = __DRI_IMAGE_FORMAT_RGB565;
1038 break;
1039 case 24:
1040 format = __DRI_IMAGE_FORMAT_XRGB8888;
1041 break;
1042 case 32:
1043 format = __DRI_IMAGE_FORMAT_ARGB8888;
1044 break;
1045 default:
1046 _eglError(EGL_BAD_PARAMETER,
1047 "dri2_create_image_khr: unsupported pixmap depth");
1048 free(buffers_reply);
1049 free(geometry_reply);
1050 return NULL;
1051 }
1052
1053 dri2_img = malloc(sizeof *dri2_img);
1054 if (!dri2_img) {
1055 free(buffers_reply);
1056 free(geometry_reply);
1057 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1058 return EGL_NO_IMAGE_KHR;
1059 }
1060
1061 if (!_eglInitImage(&dri2_img->base, disp)) {
1062 free(buffers_reply);
1063 free(geometry_reply);
1064 free(dri2_img);
1065 return EGL_NO_IMAGE_KHR;
1066 }
1067
1068 stride = buffers[0].pitch / buffers[0].cpp;
1069 dri2_img->dri_image =
1070 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1071 buffers_reply->width,
1072 buffers_reply->height,
1073 format,
1074 buffers[0].name,
1075 stride,
1076 dri2_img);
1077
1078 free(buffers_reply);
1079 free(geometry_reply);
1080
1081 return &dri2_img->base;
1082 }
1083
1084 static _EGLImage *
1085 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1086 _EGLContext *ctx, EGLenum target,
1087 EGLClientBuffer buffer, const EGLint *attr_list)
1088 {
1089 (void) drv;
1090
1091 switch (target) {
1092 case EGL_NATIVE_PIXMAP_KHR:
1093 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1094 default:
1095 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
1096 }
1097 }
1098
1099 static EGLBoolean
1100 dri2_x11_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
1101 EGLuint64KHR *ust, EGLuint64KHR *msc,
1102 EGLuint64KHR *sbc)
1103 {
1104 struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
1105 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1106 xcb_dri2_get_msc_cookie_t cookie;
1107 xcb_dri2_get_msc_reply_t *reply;
1108
1109 cookie = xcb_dri2_get_msc(dri2_dpy->conn, dri2_surf->drawable);
1110 reply = xcb_dri2_get_msc_reply(dri2_dpy->conn, cookie, NULL);
1111
1112 if (!reply) {
1113 _eglError(EGL_BAD_ACCESS, __func__);
1114 return EGL_FALSE;
1115 }
1116
1117 *ust = ((EGLuint64KHR) reply->ust_hi << 32) | reply->ust_lo;
1118 *msc = ((EGLuint64KHR) reply->msc_hi << 32) | reply->msc_lo;
1119 *sbc = ((EGLuint64KHR) reply->sbc_hi << 32) | reply->sbc_lo;
1120 free(reply);
1121
1122 return EGL_TRUE;
1123 }
1124
1125 static struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
1126 .authenticate = NULL,
1127 .create_window_surface = dri2_x11_create_window_surface,
1128 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1129 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1130 .destroy_surface = dri2_x11_destroy_surface,
1131 .create_image = dri2_fallback_create_image_khr,
1132 .swap_interval = dri2_fallback_swap_interval,
1133 .swap_buffers = dri2_x11_swap_buffers,
1134 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1135 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1136 .copy_buffers = dri2_x11_copy_buffers,
1137 .query_buffer_age = dri2_fallback_query_buffer_age,
1138 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1139 .get_sync_values = dri2_fallback_get_sync_values,
1140 .get_dri_drawable = dri2_surface_get_dri_drawable,
1141 };
1142
1143 static struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
1144 .authenticate = dri2_x11_authenticate,
1145 .create_window_surface = dri2_x11_create_window_surface,
1146 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1147 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1148 .destroy_surface = dri2_x11_destroy_surface,
1149 .create_image = dri2_x11_create_image_khr,
1150 .swap_interval = dri2_x11_swap_interval,
1151 .swap_buffers = dri2_x11_swap_buffers,
1152 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1153 .swap_buffers_region = dri2_x11_swap_buffers_region,
1154 .post_sub_buffer = dri2_x11_post_sub_buffer,
1155 .copy_buffers = dri2_x11_copy_buffers,
1156 .query_buffer_age = dri2_fallback_query_buffer_age,
1157 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1158 .get_sync_values = dri2_x11_get_sync_values,
1159 .get_dri_drawable = dri2_surface_get_dri_drawable,
1160 };
1161
1162 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1163 .base = { __DRI_SWRAST_LOADER, 1 },
1164
1165 .getDrawableInfo = swrastGetDrawableInfo,
1166 .putImage = swrastPutImage,
1167 .getImage = swrastGetImage,
1168 };
1169
1170 static const __DRIextension *swrast_loader_extensions[] = {
1171 &swrast_loader_extension.base,
1172 NULL,
1173 };
1174
1175 static EGLBoolean
1176 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1177 {
1178 struct dri2_egl_display *dri2_dpy;
1179
1180 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1181 if (!dri2_dpy)
1182 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1183
1184 disp->DriverData = (void *) dri2_dpy;
1185 if (disp->PlatformDisplay == NULL) {
1186 dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
1187 dri2_dpy->own_device = true;
1188 } else {
1189 Display *dpy = disp->PlatformDisplay;
1190
1191 dri2_dpy->conn = XGetXCBConnection(dpy);
1192 dri2_dpy->screen = DefaultScreen(dpy);
1193 }
1194
1195 if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1196 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1197 goto cleanup_dpy;
1198 }
1199
1200 /*
1201 * Every hardware driver_name is set using strdup. Doing the same in
1202 * here will allow is to simply free the memory at dri2_terminate().
1203 */
1204 dri2_dpy->fd = -1;
1205 dri2_dpy->driver_name = strdup("swrast");
1206 if (!dri2_load_driver_swrast(disp))
1207 goto cleanup_conn;
1208
1209 dri2_dpy->loader_extensions = swrast_loader_extensions;
1210
1211 if (!dri2_create_screen(disp))
1212 goto cleanup_driver;
1213
1214 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true))
1215 goto cleanup_configs;
1216
1217 /* Fill vtbl last to prevent accidentally calling virtual function during
1218 * initialization.
1219 */
1220 dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1221
1222 return EGL_TRUE;
1223
1224 cleanup_configs:
1225 _eglCleanupDisplay(disp);
1226 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1227 cleanup_driver:
1228 dlclose(dri2_dpy->driver);
1229 cleanup_conn:
1230 free(dri2_dpy->driver_name);
1231 if (disp->PlatformDisplay == NULL)
1232 xcb_disconnect(dri2_dpy->conn);
1233 cleanup_dpy:
1234 free(dri2_dpy);
1235 disp->DriverData = NULL;
1236
1237 return EGL_FALSE;
1238 }
1239
1240 static void
1241 dri2_x11_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1242 {
1243 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1244 int arbitrary_max_interval = 1000;
1245
1246 /* default behavior for no SwapBuffers support: no vblank syncing
1247 * either.
1248 */
1249 dri2_dpy->min_swap_interval = 0;
1250 dri2_dpy->max_swap_interval = 0;
1251
1252 if (!dri2_dpy->swap_available)
1253 return;
1254
1255 /* If we do have swapbuffers, then we can support pretty much any swap
1256 * interval, but we allow driconf to override applications.
1257 */
1258 if (dri2_dpy->config)
1259 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1260 "vblank_mode", &vblank_mode);
1261 switch (vblank_mode) {
1262 case DRI_CONF_VBLANK_NEVER:
1263 dri2_dpy->min_swap_interval = 0;
1264 dri2_dpy->max_swap_interval = 0;
1265 dri2_dpy->default_swap_interval = 0;
1266 break;
1267 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1268 dri2_dpy->min_swap_interval = 1;
1269 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1270 dri2_dpy->default_swap_interval = 1;
1271 break;
1272 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1273 dri2_dpy->min_swap_interval = 0;
1274 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1275 dri2_dpy->default_swap_interval = 0;
1276 break;
1277 default:
1278 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1279 dri2_dpy->min_swap_interval = 0;
1280 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1281 dri2_dpy->default_swap_interval = 1;
1282 break;
1283 }
1284 }
1285
1286 #ifdef HAVE_DRI3
1287
1288 static const __DRIextension *dri3_image_loader_extensions[] = {
1289 &dri3_image_loader_extension.base,
1290 &image_lookup_extension.base,
1291 &use_invalidate.base,
1292 NULL,
1293 };
1294
1295 static EGLBoolean
1296 dri2_initialize_x11_dri3(_EGLDriver *drv, _EGLDisplay *disp)
1297 {
1298 struct dri2_egl_display *dri2_dpy;
1299
1300 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1301 if (!dri2_dpy)
1302 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1303
1304 disp->DriverData = (void *) dri2_dpy;
1305 if (disp->PlatformDisplay == NULL) {
1306 dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
1307 dri2_dpy->own_device = true;
1308 } else {
1309 Display *dpy = disp->PlatformDisplay;
1310
1311 dri2_dpy->conn = XGetXCBConnection(dpy);
1312 dri2_dpy->screen = DefaultScreen(dpy);
1313 }
1314
1315 if (xcb_connection_has_error(dri2_dpy->conn)) {
1316 _eglLog(_EGL_WARNING, "DRI3: xcb_connect failed");
1317 goto cleanup_dpy;
1318 }
1319
1320 if (dri2_dpy->conn) {
1321 if (!dri3_x11_connect(dri2_dpy))
1322 goto cleanup_conn;
1323 }
1324
1325 if (!dri2_load_driver_dri3(disp))
1326 goto cleanup_conn;
1327
1328 dri2_dpy->loader_extensions = dri3_image_loader_extensions;
1329
1330 dri2_dpy->swap_available = true;
1331 dri2_dpy->invalidate_available = true;
1332
1333 if (!dri2_create_screen(disp))
1334 goto cleanup_fd;
1335
1336 dri2_x11_setup_swap_interval(dri2_dpy);
1337
1338 if (!dri2_dpy->is_different_gpu)
1339 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1340 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1341 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1342 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1343
1344 #ifdef HAVE_WAYLAND_PLATFORM
1345 if (dri2_dpy->device_name)
1346 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1347 #endif
1348
1349 if (dri2_dpy->conn) {
1350 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, false))
1351 goto cleanup_configs;
1352 }
1353
1354 dri2_dpy->loader_dri3_ext.core = dri2_dpy->core;
1355 dri2_dpy->loader_dri3_ext.image_driver = dri2_dpy->image_driver;
1356 dri2_dpy->loader_dri3_ext.flush = dri2_dpy->flush;
1357 dri2_dpy->loader_dri3_ext.tex_buffer = dri2_dpy->tex_buffer;
1358 dri2_dpy->loader_dri3_ext.image = dri2_dpy->image;
1359 dri2_dpy->loader_dri3_ext.config = dri2_dpy->config;
1360
1361 /* Fill vtbl last to prevent accidentally calling virtual function during
1362 * initialization.
1363 */
1364 dri2_dpy->vtbl = &dri3_x11_display_vtbl;
1365
1366 _eglLog(_EGL_INFO, "Using DRI3");
1367
1368 return EGL_TRUE;
1369
1370 cleanup_configs:
1371 _eglCleanupDisplay(disp);
1372 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1373 dlclose(dri2_dpy->driver);
1374 cleanup_fd:
1375 close(dri2_dpy->fd);
1376 cleanup_conn:
1377 if (disp->PlatformDisplay == NULL)
1378 xcb_disconnect(dri2_dpy->conn);
1379 cleanup_dpy:
1380 free(dri2_dpy);
1381 disp->DriverData = NULL;
1382
1383 return EGL_FALSE;
1384 }
1385 #endif
1386
1387 static const __DRIdri2LoaderExtension dri2_loader_extension_old = {
1388 .base = { __DRI_DRI2_LOADER, 2 },
1389
1390 .getBuffers = dri2_x11_get_buffers,
1391 .flushFrontBuffer = dri2_x11_flush_front_buffer,
1392 .getBuffersWithFormat = NULL,
1393 };
1394
1395 static const __DRIdri2LoaderExtension dri2_loader_extension = {
1396 .base = { __DRI_DRI2_LOADER, 3 },
1397
1398 .getBuffers = dri2_x11_get_buffers,
1399 .flushFrontBuffer = dri2_x11_flush_front_buffer,
1400 .getBuffersWithFormat = dri2_x11_get_buffers_with_format,
1401 };
1402
1403 static const __DRIextension *dri2_loader_extensions_old[] = {
1404 &dri2_loader_extension_old.base,
1405 &image_lookup_extension.base,
1406 NULL,
1407 };
1408
1409 static const __DRIextension *dri2_loader_extensions[] = {
1410 &dri2_loader_extension.base,
1411 &image_lookup_extension.base,
1412 NULL,
1413 };
1414
1415 static EGLBoolean
1416 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
1417 {
1418 struct dri2_egl_display *dri2_dpy;
1419
1420 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1421 if (!dri2_dpy)
1422 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1423
1424 disp->DriverData = (void *) dri2_dpy;
1425 if (disp->PlatformDisplay == NULL) {
1426 dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
1427 dri2_dpy->own_device = true;
1428 } else {
1429 Display *dpy = disp->PlatformDisplay;
1430
1431 dri2_dpy->conn = XGetXCBConnection(dpy);
1432 dri2_dpy->screen = DefaultScreen(dpy);
1433 }
1434
1435 if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1436 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1437 goto cleanup_dpy;
1438 }
1439
1440 if (!dri2_x11_connect(dri2_dpy))
1441 goto cleanup_conn;
1442
1443 if (!dri2_load_driver(disp))
1444 goto cleanup_fd;
1445
1446 if (dri2_dpy->dri2_minor >= 1)
1447 dri2_dpy->loader_extensions = dri2_loader_extensions;
1448 else
1449 dri2_dpy->loader_extensions = dri2_loader_extensions_old;
1450
1451 dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1452 dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1453
1454 if (!dri2_create_screen(disp))
1455 goto cleanup_driver;
1456
1457 dri2_x11_setup_swap_interval(dri2_dpy);
1458
1459 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1460 disp->Extensions.NOK_swap_region = EGL_TRUE;
1461 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1462 disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1463 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1464
1465 #ifdef HAVE_WAYLAND_PLATFORM
1466 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1467 #endif
1468
1469 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true))
1470 goto cleanup_configs;
1471
1472 /* Fill vtbl last to prevent accidentally calling virtual function during
1473 * initialization.
1474 */
1475 dri2_dpy->vtbl = &dri2_x11_display_vtbl;
1476
1477 _eglLog(_EGL_INFO, "Using DRI2");
1478
1479 return EGL_TRUE;
1480
1481 cleanup_configs:
1482 _eglCleanupDisplay(disp);
1483 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1484 cleanup_driver:
1485 dlclose(dri2_dpy->driver);
1486 cleanup_fd:
1487 close(dri2_dpy->fd);
1488 cleanup_conn:
1489 if (disp->PlatformDisplay == NULL)
1490 xcb_disconnect(dri2_dpy->conn);
1491 cleanup_dpy:
1492 free(dri2_dpy);
1493 disp->DriverData = NULL;
1494
1495 return EGL_FALSE;
1496 }
1497
1498 EGLBoolean
1499 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1500 {
1501 EGLBoolean initialized = EGL_TRUE;
1502
1503 int x11_dri2_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1504
1505 if (x11_dri2_accel) {
1506 #ifdef HAVE_DRI3
1507 if (getenv("LIBGL_DRI3_DISABLE") != NULL ||
1508 !dri2_initialize_x11_dri3(drv, disp)) {
1509 #endif
1510 if (!dri2_initialize_x11_dri2(drv, disp)) {
1511 initialized = dri2_initialize_x11_swrast(drv, disp);
1512 }
1513 #ifdef HAVE_DRI3
1514 }
1515 #endif
1516 } else {
1517 initialized = dri2_initialize_x11_swrast(drv, disp);
1518 }
1519
1520 return initialized;
1521 }
1522