egl/x11: avoid using freed memory if dri2 init fails
[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 if (!_eglPutSurface(surf))
390 return EGL_TRUE;
391
392 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
393
394 if (dri2_dpy->dri2) {
395 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
396 } else {
397 assert(dri2_dpy->swrast);
398 swrastDestroyDrawable(dri2_dpy, dri2_surf);
399 }
400
401 if (surf->Type == EGL_PBUFFER_BIT)
402 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
403
404 free(surf);
405
406 return EGL_TRUE;
407 }
408
409 /**
410 * Process list of buffer received from the server
411 *
412 * Processes the list of buffers received in a reply from the server to either
413 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
414 */
415 static void
416 dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
417 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
418 {
419 struct dri2_egl_display *dri2_dpy =
420 dri2_egl_display(dri2_surf->base.Resource.Display);
421 xcb_rectangle_t rectangle;
422 unsigned i;
423
424 dri2_surf->buffer_count = count;
425 dri2_surf->have_fake_front = 0;
426
427 /* This assumes the DRI2 buffer attachment tokens matches the
428 * __DRIbuffer tokens. */
429 for (i = 0; i < count; i++) {
430 dri2_surf->buffers[i].attachment = buffers[i].attachment;
431 dri2_surf->buffers[i].name = buffers[i].name;
432 dri2_surf->buffers[i].pitch = buffers[i].pitch;
433 dri2_surf->buffers[i].cpp = buffers[i].cpp;
434 dri2_surf->buffers[i].flags = buffers[i].flags;
435
436 /* We only use the DRI drivers single buffer configs. This
437 * means that if we try to render to a window, DRI2 will give us
438 * the fake front buffer, which we'll use as a back buffer.
439 * Note that EGL doesn't require that several clients rendering
440 * to the same window must see the same aux buffers. */
441 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
442 dri2_surf->have_fake_front = 1;
443 }
444
445 if (dri2_surf->region != XCB_NONE)
446 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
447
448 rectangle.x = 0;
449 rectangle.y = 0;
450 rectangle.width = dri2_surf->base.Width;
451 rectangle.height = dri2_surf->base.Height;
452 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
453 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
454 }
455
456 static __DRIbuffer *
457 dri2_x11_get_buffers(__DRIdrawable * driDrawable,
458 int *width, int *height,
459 unsigned int *attachments, int count,
460 int *out_count, void *loaderPrivate)
461 {
462 struct dri2_egl_surface *dri2_surf = loaderPrivate;
463 struct dri2_egl_display *dri2_dpy =
464 dri2_egl_display(dri2_surf->base.Resource.Display);
465 xcb_dri2_dri2_buffer_t *buffers;
466 xcb_dri2_get_buffers_reply_t *reply;
467 xcb_dri2_get_buffers_cookie_t cookie;
468
469 (void) driDrawable;
470
471 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
472 dri2_surf->drawable,
473 count, count, attachments);
474 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
475 if (reply == NULL)
476 return NULL;
477 buffers = xcb_dri2_get_buffers_buffers (reply);
478 if (buffers == NULL)
479 return NULL;
480
481 *out_count = reply->count;
482 dri2_surf->base.Width = *width = reply->width;
483 dri2_surf->base.Height = *height = reply->height;
484 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
485
486 free(reply);
487
488 return dri2_surf->buffers;
489 }
490
491 static __DRIbuffer *
492 dri2_x11_get_buffers_with_format(__DRIdrawable * driDrawable,
493 int *width, int *height,
494 unsigned int *attachments, int count,
495 int *out_count, void *loaderPrivate)
496 {
497 struct dri2_egl_surface *dri2_surf = loaderPrivate;
498 struct dri2_egl_display *dri2_dpy =
499 dri2_egl_display(dri2_surf->base.Resource.Display);
500 xcb_dri2_dri2_buffer_t *buffers;
501 xcb_dri2_get_buffers_with_format_reply_t *reply;
502 xcb_dri2_get_buffers_with_format_cookie_t cookie;
503 xcb_dri2_attach_format_t *format_attachments;
504
505 (void) driDrawable;
506
507 format_attachments = (xcb_dri2_attach_format_t *) attachments;
508 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
509 dri2_surf->drawable,
510 count, count,
511 format_attachments);
512
513 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
514 cookie, NULL);
515 if (reply == NULL)
516 return NULL;
517
518 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
519 dri2_surf->base.Width = *width = reply->width;
520 dri2_surf->base.Height = *height = reply->height;
521 *out_count = reply->count;
522 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
523
524 free(reply);
525
526 return dri2_surf->buffers;
527 }
528
529 static void
530 dri2_x11_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
531 {
532 (void) driDrawable;
533
534 /* FIXME: Does EGL support front buffer rendering at all? */
535
536 #if 0
537 struct dri2_egl_surface *dri2_surf = loaderPrivate;
538
539 dri2WaitGL(dri2_surf);
540 #else
541 (void) loaderPrivate;
542 #endif
543 }
544
545 static int
546 dri2_x11_do_authenticate(struct dri2_egl_display *dri2_dpy, uint32_t id)
547 {
548 xcb_dri2_authenticate_reply_t *authenticate;
549 xcb_dri2_authenticate_cookie_t authenticate_cookie;
550 xcb_screen_iterator_t s;
551 xcb_screen_t *screen;
552 int ret = 0;
553
554 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
555
556 screen = get_xcb_screen(s, dri2_dpy->screen);
557 if (!screen) {
558 _eglLog(_EGL_WARNING, "DRI2: failed to get xcb screen");
559 return -1;
560 }
561
562 authenticate_cookie =
563 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, screen->root, id);
564 authenticate =
565 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
566
567 if (authenticate == NULL || !authenticate->authenticated)
568 ret = -1;
569
570 free(authenticate);
571
572 return ret;
573 }
574
575 static EGLBoolean
576 dri2_x11_local_authenticate(struct dri2_egl_display *dri2_dpy)
577 {
578 #ifdef HAVE_LIBDRM
579 drm_magic_t magic;
580
581 if (drmGetMagic(dri2_dpy->fd, &magic)) {
582 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
583 return EGL_FALSE;
584 }
585
586 if (dri2_x11_do_authenticate(dri2_dpy, magic) < 0) {
587 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
588 return EGL_FALSE;
589 }
590 #endif
591 return EGL_TRUE;
592 }
593
594 static EGLBoolean
595 dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
596 {
597 xcb_xfixes_query_version_reply_t *xfixes_query;
598 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
599 xcb_dri2_query_version_reply_t *dri2_query;
600 xcb_dri2_query_version_cookie_t dri2_query_cookie;
601 xcb_dri2_connect_reply_t *connect;
602 xcb_dri2_connect_cookie_t connect_cookie;
603 xcb_generic_error_t *error;
604 xcb_screen_iterator_t s;
605 xcb_screen_t *screen;
606 char *driver_name, *loader_driver_name, *device_name;
607 const xcb_query_extension_reply_t *extension;
608
609 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
610 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
611
612 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
613 if (!(extension && extension->present))
614 return EGL_FALSE;
615
616 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
617 if (!(extension && extension->present))
618 return EGL_FALSE;
619
620 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
621 XCB_XFIXES_MAJOR_VERSION,
622 XCB_XFIXES_MINOR_VERSION);
623
624 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
625 XCB_DRI2_MAJOR_VERSION,
626 XCB_DRI2_MINOR_VERSION);
627
628 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
629 screen = get_xcb_screen(s, dri2_dpy->screen);
630 if (!screen) {
631 _eglLog(_EGL_WARNING, "DRI2: failed to get xcb screen");
632 return EGL_FALSE;
633 }
634 connect_cookie = xcb_dri2_connect_unchecked(dri2_dpy->conn, screen->root,
635 XCB_DRI2_DRIVER_TYPE_DRI);
636
637 xfixes_query =
638 xcb_xfixes_query_version_reply (dri2_dpy->conn,
639 xfixes_query_cookie, &error);
640 if (xfixes_query == NULL ||
641 error != NULL || xfixes_query->major_version < 2) {
642 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
643 free(error);
644 return EGL_FALSE;
645 }
646 free(xfixes_query);
647
648 dri2_query =
649 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
650 if (dri2_query == NULL || error != NULL) {
651 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
652 free(error);
653 return EGL_FALSE;
654 }
655 dri2_dpy->dri2_major = dri2_query->major_version;
656 dri2_dpy->dri2_minor = dri2_query->minor_version;
657 free(dri2_query);
658
659 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
660 if (connect == NULL ||
661 connect->driver_name_length + connect->device_name_length == 0) {
662 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
663 return EGL_FALSE;
664 }
665
666 device_name = xcb_dri2_connect_device_name (connect);
667
668 dri2_dpy->device_name =
669 strndup(device_name,
670 xcb_dri2_connect_device_name_length(connect));
671
672 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
673 if (dri2_dpy->fd == -1) {
674 _eglLog(_EGL_WARNING,
675 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
676 strerror(errno));
677 free(dri2_dpy->device_name);
678 free(connect);
679 return EGL_FALSE;
680 }
681
682 if (!dri2_x11_local_authenticate(dri2_dpy)) {
683 close(dri2_dpy->fd);
684 free(dri2_dpy->device_name);
685 free(connect);
686 return EGL_FALSE;
687 }
688
689 driver_name = xcb_dri2_connect_driver_name (connect);
690
691 /* If Mesa knows about the appropriate driver for this fd, then trust it.
692 * Otherwise, default to the server's value.
693 */
694 loader_driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
695 if (loader_driver_name) {
696 dri2_dpy->driver_name = loader_driver_name;
697 } else {
698 dri2_dpy->driver_name =
699 strndup(driver_name,
700 xcb_dri2_connect_driver_name_length(connect));
701 }
702
703 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
704 close(dri2_dpy->fd);
705 free(dri2_dpy->device_name);
706 free(dri2_dpy->driver_name);
707 free(connect);
708 return EGL_FALSE;
709 }
710 free(connect);
711
712 return EGL_TRUE;
713 }
714
715 static int
716 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
717 {
718 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
719
720 return dri2_x11_do_authenticate(dri2_dpy, id);
721 }
722
723 static EGLBoolean
724 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
725 _EGLDisplay *disp, bool supports_preserved)
726 {
727 xcb_screen_iterator_t s;
728 xcb_depth_iterator_t d;
729 xcb_visualtype_t *visuals;
730 int i, j, id;
731 unsigned int rgba_masks[4];
732 EGLint surface_type;
733 EGLint config_attrs[] = {
734 EGL_NATIVE_VISUAL_ID, 0,
735 EGL_NATIVE_VISUAL_TYPE, 0,
736 EGL_NONE
737 };
738
739 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
740 d = xcb_screen_allowed_depths_iterator(get_xcb_screen(s, dri2_dpy->screen));
741 id = 1;
742
743 surface_type =
744 EGL_WINDOW_BIT |
745 EGL_PIXMAP_BIT |
746 EGL_PBUFFER_BIT;
747
748 if (supports_preserved)
749 surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
750
751 while (d.rem > 0) {
752 EGLBoolean class_added[6] = { 0, };
753
754 visuals = xcb_depth_visuals(d.data);
755 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
756 if (class_added[visuals[i]._class])
757 continue;
758
759 class_added[visuals[i]._class] = EGL_TRUE;
760 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
761 config_attrs[1] = visuals[i].visual_id;
762 config_attrs[3] = visuals[i]._class;
763
764 rgba_masks[0] = visuals[i].red_mask;
765 rgba_masks[1] = visuals[i].green_mask;
766 rgba_masks[2] = visuals[i].blue_mask;
767 rgba_masks[3] = 0;
768 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
769 surface_type, config_attrs, rgba_masks);
770
771 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
772 * Otherwise it will only match a 32-bit RGBA visual. On a
773 * composited window manager on X11, this will make all of the
774 * EGLConfigs with destination alpha get blended by the
775 * compositor. This is probably not what the application
776 * wants... especially on drivers that only have 32-bit RGBA
777 * EGLConfigs! */
778 if (d.data->depth == 24) {
779 rgba_masks[3] =
780 ~(rgba_masks[0] | rgba_masks[1] | rgba_masks[2]);
781 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
782 surface_type, config_attrs, rgba_masks);
783 }
784 }
785 }
786
787 xcb_depth_next(&d);
788 }
789
790 if (!_eglGetArraySize(disp->Configs)) {
791 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
792 return EGL_FALSE;
793 }
794
795 return EGL_TRUE;
796 }
797
798 static EGLBoolean
799 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
800 _EGLSurface *draw, xcb_xfixes_region_t region)
801 {
802 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
803 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
804 enum xcb_dri2_attachment_t render_attachment;
805 xcb_dri2_copy_region_cookie_t cookie;
806
807 /* No-op for a pixmap or pbuffer surface */
808 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
809 return EGL_TRUE;
810
811 if (dri2_dpy->flush)
812 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
813
814 if (dri2_surf->have_fake_front)
815 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
816 else
817 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
818
819 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
820 dri2_surf->drawable,
821 region,
822 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
823 render_attachment);
824 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
825
826 return EGL_TRUE;
827 }
828
829 static int64_t
830 dri2_x11_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
831 int64_t msc, int64_t divisor, int64_t remainder)
832 {
833 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
834 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
835 uint32_t msc_hi = msc >> 32;
836 uint32_t msc_lo = msc & 0xffffffff;
837 uint32_t divisor_hi = divisor >> 32;
838 uint32_t divisor_lo = divisor & 0xffffffff;
839 uint32_t remainder_hi = remainder >> 32;
840 uint32_t remainder_lo = remainder & 0xffffffff;
841 xcb_dri2_swap_buffers_cookie_t cookie;
842 xcb_dri2_swap_buffers_reply_t *reply;
843 int64_t swap_count = -1;
844
845 /* No-op for a pixmap or pbuffer surface */
846 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
847 return 0;
848
849 if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available)
850 return dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
851
852 dri2_flush_drawable_for_swapbuffers(disp, draw);
853
854 cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn, dri2_surf->drawable,
855 msc_hi, msc_lo, divisor_hi, divisor_lo, remainder_hi, remainder_lo);
856
857 reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
858
859 if (reply) {
860 swap_count = (((int64_t)reply->swap_hi) << 32) | reply->swap_lo;
861 free(reply);
862 }
863
864 /* Since we aren't watching for the server's invalidate events like we're
865 * supposed to (due to XCB providing no mechanism for filtering the events
866 * the way xlib does), and SwapBuffers is a common cause of invalidate
867 * events, just shove one down to the driver, even though we haven't told
868 * the driver that we're the kind of loader that provides reliable
869 * invalidate events. This causes the driver to request buffers again at
870 * its next draw, so that we get the correct buffers if a pageflip
871 * happened. The driver should still be using the viewport hack to catch
872 * window resizes.
873 */
874 if (dri2_dpy->flush &&
875 dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
876 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
877
878 return swap_count;
879 }
880
881 static EGLBoolean
882 dri2_x11_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
883 {
884 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
885 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
886
887 if (dri2_dpy->dri2) {
888 if (dri2_x11_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1) {
889 return EGL_TRUE;
890 }
891 /* Swap failed with a window drawable. */
892 _eglError(EGL_BAD_NATIVE_WINDOW, __FUNCTION__);
893 return EGL_FALSE;
894 } else {
895 assert(dri2_dpy->swrast);
896
897 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
898 return EGL_TRUE;
899 }
900 }
901
902 static EGLBoolean
903 dri2_x11_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp,
904 _EGLSurface *draw,
905 EGLint numRects, const EGLint *rects)
906 {
907 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
908 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
909 EGLBoolean ret;
910 xcb_xfixes_region_t region;
911 xcb_rectangle_t rectangles[16];
912 int i;
913
914 if (numRects > (int)ARRAY_SIZE(rectangles))
915 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
916
917 for (i = 0; i < numRects; i++) {
918 rectangles[i].x = rects[i * 4];
919 rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
920 rectangles[i].width = rects[i * 4 + 2];
921 rectangles[i].height = rects[i * 4 + 3];
922 }
923
924 region = xcb_generate_id(dri2_dpy->conn);
925 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
926 ret = dri2_copy_region(drv, disp, draw, region);
927 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
928
929 return ret;
930 }
931
932 static EGLBoolean
933 dri2_x11_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
934 EGLint x, EGLint y, EGLint width, EGLint height)
935 {
936 const EGLint rect[4] = { x, y, width, height };
937
938 if (x < 0 || y < 0 || width < 0 || height < 0)
939 _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
940
941 return dri2_x11_swap_buffers_region(drv, disp, draw, 1, rect);
942 }
943
944 static EGLBoolean
945 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
946 EGLint interval)
947 {
948 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
949 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
950
951 if (interval > surf->Config->MaxSwapInterval)
952 interval = surf->Config->MaxSwapInterval;
953 else if (interval < surf->Config->MinSwapInterval)
954 interval = surf->Config->MinSwapInterval;
955
956 if (interval != surf->SwapInterval && dri2_dpy->swap_available)
957 xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
958
959 surf->SwapInterval = interval;
960
961 return EGL_TRUE;
962 }
963
964 static EGLBoolean
965 dri2_x11_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
966 void *native_pixmap_target)
967 {
968 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
969 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
970 xcb_gcontext_t gc;
971 xcb_pixmap_t target;
972
973 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
974 target = (uintptr_t) native_pixmap_target;
975
976 (void) drv;
977
978 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
979
980 gc = xcb_generate_id(dri2_dpy->conn);
981 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
982 xcb_copy_area(dri2_dpy->conn,
983 dri2_surf->drawable,
984 target,
985 gc,
986 0, 0,
987 0, 0,
988 dri2_surf->base.Width,
989 dri2_surf->base.Height);
990 xcb_free_gc(dri2_dpy->conn, gc);
991
992 return EGL_TRUE;
993 }
994
995 static _EGLImage *
996 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
997 EGLClientBuffer buffer, const EGLint *attr_list)
998 {
999 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1000 struct dri2_egl_image *dri2_img;
1001 unsigned int attachments[1];
1002 xcb_drawable_t drawable;
1003 xcb_dri2_get_buffers_cookie_t buffers_cookie;
1004 xcb_dri2_get_buffers_reply_t *buffers_reply;
1005 xcb_dri2_dri2_buffer_t *buffers;
1006 xcb_get_geometry_cookie_t geometry_cookie;
1007 xcb_get_geometry_reply_t *geometry_reply;
1008 xcb_generic_error_t *error;
1009 int stride, format;
1010
1011 (void) ctx;
1012
1013 drawable = (xcb_drawable_t) (uintptr_t) buffer;
1014 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1015 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1016 buffers_cookie =
1017 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1018 drawable, 1, 1, attachments);
1019 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1020 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1021 buffers_cookie, NULL);
1022 if (buffers_reply == NULL)
1023 return NULL;
1024
1025 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1026 if (buffers == NULL) {
1027 return NULL;
1028 }
1029
1030 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1031 geometry_cookie, &error);
1032 if (geometry_reply == NULL || error != NULL) {
1033 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1034 free(error);
1035 free(buffers_reply);
1036 return NULL;
1037 }
1038
1039 switch (geometry_reply->depth) {
1040 case 16:
1041 format = __DRI_IMAGE_FORMAT_RGB565;
1042 break;
1043 case 24:
1044 format = __DRI_IMAGE_FORMAT_XRGB8888;
1045 break;
1046 case 32:
1047 format = __DRI_IMAGE_FORMAT_ARGB8888;
1048 break;
1049 default:
1050 _eglError(EGL_BAD_PARAMETER,
1051 "dri2_create_image_khr: unsupported pixmap depth");
1052 free(buffers_reply);
1053 free(geometry_reply);
1054 return NULL;
1055 }
1056
1057 dri2_img = malloc(sizeof *dri2_img);
1058 if (!dri2_img) {
1059 free(buffers_reply);
1060 free(geometry_reply);
1061 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1062 return EGL_NO_IMAGE_KHR;
1063 }
1064
1065 if (!_eglInitImage(&dri2_img->base, disp)) {
1066 free(buffers_reply);
1067 free(geometry_reply);
1068 free(dri2_img);
1069 return EGL_NO_IMAGE_KHR;
1070 }
1071
1072 stride = buffers[0].pitch / buffers[0].cpp;
1073 dri2_img->dri_image =
1074 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1075 buffers_reply->width,
1076 buffers_reply->height,
1077 format,
1078 buffers[0].name,
1079 stride,
1080 dri2_img);
1081
1082 free(buffers_reply);
1083 free(geometry_reply);
1084
1085 return &dri2_img->base;
1086 }
1087
1088 static _EGLImage *
1089 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1090 _EGLContext *ctx, EGLenum target,
1091 EGLClientBuffer buffer, const EGLint *attr_list)
1092 {
1093 (void) drv;
1094
1095 switch (target) {
1096 case EGL_NATIVE_PIXMAP_KHR:
1097 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1098 default:
1099 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
1100 }
1101 }
1102
1103 static EGLBoolean
1104 dri2_x11_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
1105 EGLuint64KHR *ust, EGLuint64KHR *msc,
1106 EGLuint64KHR *sbc)
1107 {
1108 struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
1109 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1110 xcb_dri2_get_msc_cookie_t cookie;
1111 xcb_dri2_get_msc_reply_t *reply;
1112
1113 cookie = xcb_dri2_get_msc(dri2_dpy->conn, dri2_surf->drawable);
1114 reply = xcb_dri2_get_msc_reply(dri2_dpy->conn, cookie, NULL);
1115
1116 if (!reply) {
1117 _eglError(EGL_BAD_ACCESS, __func__);
1118 return EGL_FALSE;
1119 }
1120
1121 *ust = ((EGLuint64KHR) reply->ust_hi << 32) | reply->ust_lo;
1122 *msc = ((EGLuint64KHR) reply->msc_hi << 32) | reply->msc_lo;
1123 *sbc = ((EGLuint64KHR) reply->sbc_hi << 32) | reply->sbc_lo;
1124 free(reply);
1125
1126 return EGL_TRUE;
1127 }
1128
1129 static struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
1130 .authenticate = NULL,
1131 .create_window_surface = dri2_x11_create_window_surface,
1132 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1133 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1134 .destroy_surface = dri2_x11_destroy_surface,
1135 .create_image = dri2_fallback_create_image_khr,
1136 .swap_interval = dri2_fallback_swap_interval,
1137 .swap_buffers = dri2_x11_swap_buffers,
1138 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1139 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1140 .copy_buffers = dri2_x11_copy_buffers,
1141 .query_buffer_age = dri2_fallback_query_buffer_age,
1142 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1143 .get_sync_values = dri2_fallback_get_sync_values,
1144 .get_dri_drawable = dri2_surface_get_dri_drawable,
1145 };
1146
1147 static struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
1148 .authenticate = dri2_x11_authenticate,
1149 .create_window_surface = dri2_x11_create_window_surface,
1150 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1151 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1152 .destroy_surface = dri2_x11_destroy_surface,
1153 .create_image = dri2_x11_create_image_khr,
1154 .swap_interval = dri2_x11_swap_interval,
1155 .swap_buffers = dri2_x11_swap_buffers,
1156 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1157 .swap_buffers_region = dri2_x11_swap_buffers_region,
1158 .post_sub_buffer = dri2_x11_post_sub_buffer,
1159 .copy_buffers = dri2_x11_copy_buffers,
1160 .query_buffer_age = dri2_fallback_query_buffer_age,
1161 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1162 .get_sync_values = dri2_x11_get_sync_values,
1163 .get_dri_drawable = dri2_surface_get_dri_drawable,
1164 };
1165
1166 static EGLBoolean
1167 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1168 {
1169 struct dri2_egl_display *dri2_dpy;
1170
1171 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1172 if (!dri2_dpy)
1173 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1174
1175 disp->DriverData = (void *) dri2_dpy;
1176 if (disp->PlatformDisplay == NULL) {
1177 dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
1178 dri2_dpy->own_device = true;
1179 } else {
1180 Display *dpy = disp->PlatformDisplay;
1181
1182 dri2_dpy->conn = XGetXCBConnection(dpy);
1183 dri2_dpy->screen = DefaultScreen(dpy);
1184 }
1185
1186 if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1187 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1188 goto cleanup_dpy;
1189 }
1190
1191 /*
1192 * Every hardware driver_name is set using strdup. Doing the same in
1193 * here will allow is to simply free the memory at dri2_terminate().
1194 */
1195 dri2_dpy->fd = -1;
1196 dri2_dpy->driver_name = strdup("swrast");
1197 if (!dri2_load_driver_swrast(disp))
1198 goto cleanup_conn;
1199
1200 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
1201 dri2_dpy->swrast_loader_extension.base.version = 2;
1202 dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
1203 dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
1204 dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
1205
1206 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
1207 dri2_dpy->extensions[1] = NULL;
1208 dri2_dpy->extensions[2] = NULL;
1209
1210 if (!dri2_create_screen(disp))
1211 goto cleanup_driver;
1212
1213 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true))
1214 goto cleanup_configs;
1215
1216 /* Fill vtbl last to prevent accidentally calling virtual function during
1217 * initialization.
1218 */
1219 dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1220
1221 return EGL_TRUE;
1222
1223 cleanup_configs:
1224 _eglCleanupDisplay(disp);
1225 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1226 cleanup_driver:
1227 dlclose(dri2_dpy->driver);
1228 cleanup_conn:
1229 free(dri2_dpy->driver_name);
1230 if (disp->PlatformDisplay == NULL)
1231 xcb_disconnect(dri2_dpy->conn);
1232 cleanup_dpy:
1233 free(dri2_dpy);
1234 disp->DriverData = NULL;
1235
1236 return EGL_FALSE;
1237 }
1238
1239 static void
1240 dri2_x11_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1241 {
1242 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1243 int arbitrary_max_interval = 1000;
1244
1245 /* default behavior for no SwapBuffers support: no vblank syncing
1246 * either.
1247 */
1248 dri2_dpy->min_swap_interval = 0;
1249 dri2_dpy->max_swap_interval = 0;
1250
1251 if (!dri2_dpy->swap_available)
1252 return;
1253
1254 /* If we do have swapbuffers, then we can support pretty much any swap
1255 * interval, but we allow driconf to override applications.
1256 */
1257 if (dri2_dpy->config)
1258 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1259 "vblank_mode", &vblank_mode);
1260 switch (vblank_mode) {
1261 case DRI_CONF_VBLANK_NEVER:
1262 dri2_dpy->min_swap_interval = 0;
1263 dri2_dpy->max_swap_interval = 0;
1264 dri2_dpy->default_swap_interval = 0;
1265 break;
1266 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1267 dri2_dpy->min_swap_interval = 1;
1268 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1269 dri2_dpy->default_swap_interval = 1;
1270 break;
1271 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1272 dri2_dpy->min_swap_interval = 0;
1273 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1274 dri2_dpy->default_swap_interval = 0;
1275 break;
1276 default:
1277 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1278 dri2_dpy->min_swap_interval = 0;
1279 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1280 dri2_dpy->default_swap_interval = 1;
1281 break;
1282 }
1283 }
1284
1285 #ifdef HAVE_DRI3
1286 static EGLBoolean
1287 dri2_initialize_x11_dri3(_EGLDriver *drv, _EGLDisplay *disp)
1288 {
1289 struct dri2_egl_display *dri2_dpy;
1290
1291 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1292 if (!dri2_dpy)
1293 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1294
1295 disp->DriverData = (void *) dri2_dpy;
1296 if (disp->PlatformDisplay == NULL) {
1297 dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
1298 dri2_dpy->own_device = true;
1299 } else {
1300 Display *dpy = disp->PlatformDisplay;
1301
1302 dri2_dpy->conn = XGetXCBConnection(dpy);
1303 dri2_dpy->screen = DefaultScreen(dpy);
1304 }
1305
1306 if (xcb_connection_has_error(dri2_dpy->conn)) {
1307 _eglLog(_EGL_WARNING, "DRI3: xcb_connect failed");
1308 goto cleanup_dpy;
1309 }
1310
1311 if (dri2_dpy->conn) {
1312 if (!dri3_x11_connect(dri2_dpy))
1313 goto cleanup_conn;
1314 }
1315
1316 if (!dri2_load_driver_dri3(disp))
1317 goto cleanup_conn;
1318
1319 dri2_dpy->extensions[0] = &dri3_image_loader_extension.base;
1320 dri2_dpy->extensions[1] = &use_invalidate.base;
1321 dri2_dpy->extensions[2] = &image_lookup_extension.base;
1322 dri2_dpy->extensions[3] = NULL;
1323
1324 dri2_dpy->swap_available = true;
1325 dri2_dpy->invalidate_available = true;
1326
1327 if (!dri2_create_screen(disp))
1328 goto cleanup_fd;
1329
1330 dri2_x11_setup_swap_interval(dri2_dpy);
1331
1332 if (!dri2_dpy->is_different_gpu)
1333 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1334 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1335 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1336 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1337
1338 #ifdef HAVE_WAYLAND_PLATFORM
1339 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1340 #endif
1341
1342 if (dri2_dpy->conn) {
1343 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, false))
1344 goto cleanup_configs;
1345 }
1346
1347 dri2_dpy->loader_dri3_ext.core = dri2_dpy->core;
1348 dri2_dpy->loader_dri3_ext.image_driver = dri2_dpy->image_driver;
1349 dri2_dpy->loader_dri3_ext.flush = dri2_dpy->flush;
1350 dri2_dpy->loader_dri3_ext.tex_buffer = dri2_dpy->tex_buffer;
1351 dri2_dpy->loader_dri3_ext.image = dri2_dpy->image;
1352 dri2_dpy->loader_dri3_ext.config = dri2_dpy->config;
1353
1354 /* Fill vtbl last to prevent accidentally calling virtual function during
1355 * initialization.
1356 */
1357 dri2_dpy->vtbl = &dri3_x11_display_vtbl;
1358
1359 _eglLog(_EGL_INFO, "Using DRI3");
1360
1361 return EGL_TRUE;
1362
1363 cleanup_configs:
1364 _eglCleanupDisplay(disp);
1365 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1366 dlclose(dri2_dpy->driver);
1367 cleanup_fd:
1368 close(dri2_dpy->fd);
1369 cleanup_conn:
1370 if (disp->PlatformDisplay == NULL)
1371 xcb_disconnect(dri2_dpy->conn);
1372 cleanup_dpy:
1373 free(dri2_dpy);
1374 disp->DriverData = NULL;
1375
1376 return EGL_FALSE;
1377 }
1378 #endif
1379
1380 static EGLBoolean
1381 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
1382 {
1383 struct dri2_egl_display *dri2_dpy;
1384
1385 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1386 if (!dri2_dpy)
1387 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1388
1389 disp->DriverData = (void *) dri2_dpy;
1390 if (disp->PlatformDisplay == NULL) {
1391 dri2_dpy->conn = xcb_connect(0, &dri2_dpy->screen);
1392 dri2_dpy->own_device = true;
1393 } else {
1394 Display *dpy = disp->PlatformDisplay;
1395
1396 dri2_dpy->conn = XGetXCBConnection(dpy);
1397 dri2_dpy->screen = DefaultScreen(dpy);
1398 }
1399
1400 if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1401 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1402 goto cleanup_dpy;
1403 }
1404
1405 if (!dri2_x11_connect(dri2_dpy))
1406 goto cleanup_conn;
1407
1408 if (!dri2_load_driver(disp))
1409 goto cleanup_fd;
1410
1411 if (dri2_dpy->dri2_minor >= 1) {
1412 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1413 dri2_dpy->dri2_loader_extension.base.version = 3;
1414 dri2_dpy->dri2_loader_extension.getBuffers = dri2_x11_get_buffers;
1415 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_x11_flush_front_buffer;
1416 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1417 dri2_x11_get_buffers_with_format;
1418 } else {
1419 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1420 dri2_dpy->dri2_loader_extension.base.version = 2;
1421 dri2_dpy->dri2_loader_extension.getBuffers = dri2_x11_get_buffers;
1422 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_x11_flush_front_buffer;
1423 dri2_dpy->dri2_loader_extension.getBuffersWithFormat = NULL;
1424 }
1425
1426 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1427 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1428 dri2_dpy->extensions[2] = NULL;
1429
1430 dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1431 dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1432
1433 if (!dri2_create_screen(disp))
1434 goto cleanup_driver;
1435
1436 dri2_x11_setup_swap_interval(dri2_dpy);
1437
1438 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1439 disp->Extensions.NOK_swap_region = EGL_TRUE;
1440 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1441 disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1442 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1443
1444 #ifdef HAVE_WAYLAND_PLATFORM
1445 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1446 #endif
1447
1448 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true))
1449 goto cleanup_configs;
1450
1451 /* Fill vtbl last to prevent accidentally calling virtual function during
1452 * initialization.
1453 */
1454 dri2_dpy->vtbl = &dri2_x11_display_vtbl;
1455
1456 _eglLog(_EGL_INFO, "Using DRI2");
1457
1458 return EGL_TRUE;
1459
1460 cleanup_configs:
1461 _eglCleanupDisplay(disp);
1462 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1463 cleanup_driver:
1464 dlclose(dri2_dpy->driver);
1465 cleanup_fd:
1466 close(dri2_dpy->fd);
1467 cleanup_conn:
1468 if (disp->PlatformDisplay == NULL)
1469 xcb_disconnect(dri2_dpy->conn);
1470 cleanup_dpy:
1471 free(dri2_dpy);
1472 disp->DriverData = NULL;
1473
1474 return EGL_FALSE;
1475 }
1476
1477 EGLBoolean
1478 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1479 {
1480 EGLBoolean initialized = EGL_TRUE;
1481
1482 int x11_dri2_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1483
1484 if (x11_dri2_accel) {
1485 #ifdef HAVE_DRI3
1486 if (getenv("LIBGL_DRI3_DISABLE") != NULL ||
1487 !dri2_initialize_x11_dri3(drv, disp)) {
1488 #endif
1489 if (!dri2_initialize_x11_dri2(drv, disp)) {
1490 initialized = dri2_initialize_x11_swrast(drv, disp);
1491 }
1492 #ifdef HAVE_DRI3
1493 }
1494 #endif
1495 } else {
1496 initialized = dri2_initialize_x11_swrast(drv, disp);
1497 }
1498
1499 return initialized;
1500 }
1501