egl/x11: Fix initialisation of swap_interval
[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 <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <xf86drm.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #include "egl_dri2.h"
41
42 /* From xmlpool/options.h, user exposed so should be stable */
43 #define DRI_CONF_VBLANK_NEVER 0
44 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
45 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
46 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
47
48 static void
49 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
50 struct dri2_egl_surface * dri2_surf,
51 int depth)
52 {
53 uint32_t mask;
54 const uint32_t function = GXcopy;
55 uint32_t valgc[2];
56
57 /* create GC's */
58 dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
59 mask = XCB_GC_FUNCTION;
60 xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask, &function);
61
62 dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
63 mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
64 valgc[0] = function;
65 valgc[1] = False;
66 xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask, valgc);
67 dri2_surf->depth = depth;
68 switch (depth) {
69 case 32:
70 case 24:
71 dri2_surf->bytes_per_pixel = 4;
72 break;
73 case 16:
74 dri2_surf->bytes_per_pixel = 2;
75 break;
76 case 8:
77 dri2_surf->bytes_per_pixel = 1;
78 break;
79 case 0:
80 dri2_surf->bytes_per_pixel = 0;
81 break;
82 default:
83 _eglLog(_EGL_WARNING, "unsupported depth %d", depth);
84 }
85 }
86
87 static void
88 swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
89 struct dri2_egl_surface * dri2_surf)
90 {
91 xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
92 xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
93 }
94
95 static void
96 swrastGetDrawableInfo(__DRIdrawable * draw,
97 int *x, int *y, int *w, int *h,
98 void *loaderPrivate)
99 {
100 struct dri2_egl_surface *dri2_surf = loaderPrivate;
101 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
102
103 xcb_get_geometry_cookie_t cookie;
104 xcb_get_geometry_reply_t *reply;
105 xcb_generic_error_t *error;
106
107 *w = *h = 0;
108 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
109 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
110 if (reply == NULL)
111 return;
112
113 if (error != NULL) {
114 _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
115 free(error);
116 } else {
117 *w = reply->width;
118 *h = reply->height;
119 }
120 free(reply);
121 }
122
123 static void
124 swrastPutImage(__DRIdrawable * draw, int op,
125 int x, int y, int w, int h,
126 char *data, void *loaderPrivate)
127 {
128 struct dri2_egl_surface *dri2_surf = loaderPrivate;
129 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
130
131 xcb_gcontext_t gc;
132
133 switch (op) {
134 case __DRI_SWRAST_IMAGE_OP_DRAW:
135 gc = dri2_surf->gc;
136 break;
137 case __DRI_SWRAST_IMAGE_OP_SWAP:
138 gc = dri2_surf->swapgc;
139 break;
140 default:
141 return;
142 }
143
144 xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable,
145 gc, w, h, x, y, 0, dri2_surf->depth,
146 w*h*dri2_surf->bytes_per_pixel, (const uint8_t *)data);
147 }
148
149 static void
150 swrastGetImage(__DRIdrawable * read,
151 int x, int y, int w, int h,
152 char *data, void *loaderPrivate)
153 {
154 struct dri2_egl_surface *dri2_surf = loaderPrivate;
155 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
156
157 xcb_get_image_cookie_t cookie;
158 xcb_get_image_reply_t *reply;
159 xcb_generic_error_t *error;
160
161 cookie = xcb_get_image (dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
162 dri2_surf->drawable, x, y, w, h, ~0);
163 reply = xcb_get_image_reply (dri2_dpy->conn, cookie, &error);
164 if (reply == NULL)
165 return;
166
167 if (error != NULL) {
168 _eglLog(_EGL_WARNING, "error in xcb_get_image");
169 free(error);
170 } else {
171 uint32_t bytes = xcb_get_image_data_length(reply);
172 uint8_t *idata = xcb_get_image_data(reply);
173 memcpy(data, idata, bytes);
174 }
175 free(reply);
176 }
177
178
179 /**
180 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
181 */
182 static _EGLSurface *
183 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
184 _EGLConfig *conf, EGLNativeWindowType native_window,
185 const EGLint *attrib_list)
186 {
187 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
188 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
189 struct dri2_egl_surface *dri2_surf;
190 xcb_get_geometry_cookie_t cookie;
191 xcb_get_geometry_reply_t *reply;
192 xcb_screen_iterator_t s;
193 xcb_generic_error_t *error;
194 xcb_drawable_t window = (uintptr_t )native_window;
195
196 (void) drv;
197
198 dri2_surf = malloc(sizeof *dri2_surf);
199 if (!dri2_surf) {
200 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
201 return NULL;
202 }
203
204 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
205 goto cleanup_surf;
206
207 dri2_surf->region = XCB_NONE;
208 if (type == EGL_PBUFFER_BIT) {
209 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
210 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
211 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
212 dri2_surf->drawable, s.data->root,
213 dri2_surf->base.Width, dri2_surf->base.Height);
214 } else {
215 dri2_surf->drawable = window;
216 }
217
218 if (dri2_dpy->dri2) {
219 dri2_surf->dri_drawable =
220 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
221 type == EGL_WINDOW_BIT ?
222 dri2_conf->dri_double_config :
223 dri2_conf->dri_single_config,
224 dri2_surf);
225 } else {
226 assert(dri2_dpy->swrast);
227 dri2_surf->dri_drawable =
228 (*dri2_dpy->swrast->createNewDrawable) (dri2_dpy->dri_screen,
229 dri2_conf->dri_double_config,
230 dri2_surf);
231 }
232
233 if (dri2_surf->dri_drawable == NULL) {
234 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
235 goto cleanup_pixmap;
236 }
237
238 if (dri2_dpy->dri2) {
239 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
240 } else {
241 swrastCreateDrawable(dri2_dpy, dri2_surf, _eglGetConfigKey(conf, EGL_BUFFER_SIZE));
242 }
243
244 if (type != EGL_PBUFFER_BIT) {
245 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
246 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
247 if (reply == NULL || error != NULL) {
248 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
249 free(error);
250 goto cleanup_dri_drawable;
251 }
252
253 dri2_surf->base.Width = reply->width;
254 dri2_surf->base.Height = reply->height;
255 free(reply);
256 }
257
258 /* we always copy the back buffer to front */
259 dri2_surf->base.PostSubBufferSupportedNV = EGL_TRUE;
260
261 return &dri2_surf->base;
262
263 cleanup_dri_drawable:
264 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
265 cleanup_pixmap:
266 if (type == EGL_PBUFFER_BIT)
267 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
268 cleanup_surf:
269 free(dri2_surf);
270
271 return NULL;
272 }
273
274 /**
275 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
276 */
277 static _EGLSurface *
278 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
279 _EGLConfig *conf, EGLNativeWindowType window,
280 const EGLint *attrib_list)
281 {
282 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
283 _EGLSurface *surf;
284
285 surf = dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
286 window, attrib_list);
287
288 /* When we first create the DRI2 drawable, its swap interval on the server
289 * side is 1.
290 */
291 surf->SwapInterval = 1;
292
293 /* Override that with a driconf-set value. */
294 drv->API.SwapInterval(drv, disp, surf, dri2_dpy->default_swap_interval);
295
296 return surf;
297 }
298
299 static _EGLSurface *
300 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
301 _EGLConfig *conf, EGLNativePixmapType pixmap,
302 const EGLint *attrib_list)
303 {
304 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
305 pixmap, attrib_list);
306 }
307
308 static _EGLSurface *
309 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
310 _EGLConfig *conf, const EGLint *attrib_list)
311 {
312 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
313 XCB_WINDOW_NONE, attrib_list);
314 }
315
316 static EGLBoolean
317 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
318 {
319 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
320 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
321
322 (void) drv;
323
324 if (!_eglPutSurface(surf))
325 return EGL_TRUE;
326
327 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
328
329 if (dri2_dpy->dri2) {
330 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
331 } else {
332 assert(dri2_dpy->swrast);
333 swrastDestroyDrawable(dri2_dpy, dri2_surf);
334 }
335
336 if (surf->Type == EGL_PBUFFER_BIT)
337 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
338
339 free(surf);
340
341 return EGL_TRUE;
342 }
343
344 /**
345 * Process list of buffer received from the server
346 *
347 * Processes the list of buffers received in a reply from the server to either
348 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
349 */
350 static void
351 dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
352 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
353 {
354 struct dri2_egl_display *dri2_dpy =
355 dri2_egl_display(dri2_surf->base.Resource.Display);
356 xcb_rectangle_t rectangle;
357 unsigned i;
358
359 dri2_surf->buffer_count = count;
360 dri2_surf->have_fake_front = 0;
361
362 /* This assumes the DRI2 buffer attachment tokens matches the
363 * __DRIbuffer tokens. */
364 for (i = 0; i < count; i++) {
365 dri2_surf->buffers[i].attachment = buffers[i].attachment;
366 dri2_surf->buffers[i].name = buffers[i].name;
367 dri2_surf->buffers[i].pitch = buffers[i].pitch;
368 dri2_surf->buffers[i].cpp = buffers[i].cpp;
369 dri2_surf->buffers[i].flags = buffers[i].flags;
370
371 /* We only use the DRI drivers single buffer configs. This
372 * means that if we try to render to a window, DRI2 will give us
373 * the fake front buffer, which we'll use as a back buffer.
374 * Note that EGL doesn't require that several clients rendering
375 * to the same window must see the same aux buffers. */
376 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
377 dri2_surf->have_fake_front = 1;
378 }
379
380 if (dri2_surf->region != XCB_NONE)
381 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
382
383 rectangle.x = 0;
384 rectangle.y = 0;
385 rectangle.width = dri2_surf->base.Width;
386 rectangle.height = dri2_surf->base.Height;
387 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
388 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
389 }
390
391 static __DRIbuffer *
392 dri2_get_buffers(__DRIdrawable * driDrawable,
393 int *width, int *height,
394 unsigned int *attachments, int count,
395 int *out_count, void *loaderPrivate)
396 {
397 struct dri2_egl_surface *dri2_surf = loaderPrivate;
398 struct dri2_egl_display *dri2_dpy =
399 dri2_egl_display(dri2_surf->base.Resource.Display);
400 xcb_dri2_dri2_buffer_t *buffers;
401 xcb_dri2_get_buffers_reply_t *reply;
402 xcb_dri2_get_buffers_cookie_t cookie;
403
404 (void) driDrawable;
405
406 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
407 dri2_surf->drawable,
408 count, count, attachments);
409 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
410 buffers = xcb_dri2_get_buffers_buffers (reply);
411 if (buffers == NULL)
412 return NULL;
413
414 *out_count = reply->count;
415 dri2_surf->base.Width = *width = reply->width;
416 dri2_surf->base.Height = *height = reply->height;
417 dri2_process_buffers(dri2_surf, buffers, *out_count);
418
419 free(reply);
420
421 return dri2_surf->buffers;
422 }
423
424 static __DRIbuffer *
425 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
426 int *width, int *height,
427 unsigned int *attachments, int count,
428 int *out_count, void *loaderPrivate)
429 {
430 struct dri2_egl_surface *dri2_surf = loaderPrivate;
431 struct dri2_egl_display *dri2_dpy =
432 dri2_egl_display(dri2_surf->base.Resource.Display);
433 xcb_dri2_dri2_buffer_t *buffers;
434 xcb_dri2_get_buffers_with_format_reply_t *reply;
435 xcb_dri2_get_buffers_with_format_cookie_t cookie;
436 xcb_dri2_attach_format_t *format_attachments;
437
438 (void) driDrawable;
439
440 format_attachments = (xcb_dri2_attach_format_t *) attachments;
441 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
442 dri2_surf->drawable,
443 count, count,
444 format_attachments);
445
446 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
447 cookie, NULL);
448 if (reply == NULL)
449 return NULL;
450
451 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
452 dri2_surf->base.Width = *width = reply->width;
453 dri2_surf->base.Height = *height = reply->height;
454 *out_count = reply->count;
455 dri2_process_buffers(dri2_surf, buffers, *out_count);
456
457 free(reply);
458
459 return dri2_surf->buffers;
460 }
461
462 static void
463 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
464 {
465 (void) driDrawable;
466
467 /* FIXME: Does EGL support front buffer rendering at all? */
468
469 #if 0
470 struct dri2_egl_surface *dri2_surf = loaderPrivate;
471
472 dri2WaitGL(dri2_surf);
473 #else
474 (void) loaderPrivate;
475 #endif
476 }
477
478 static char *
479 dri2_strndup(const char *s, int length)
480 {
481 char *d;
482
483 d = malloc(length + 1);
484 if (d == NULL)
485 return NULL;
486
487 memcpy(d, s, length);
488 d[length] = '\0';
489
490 return d;
491 }
492
493 static EGLBoolean
494 dri2_connect(struct dri2_egl_display *dri2_dpy)
495 {
496 xcb_xfixes_query_version_reply_t *xfixes_query;
497 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
498 xcb_dri2_query_version_reply_t *dri2_query;
499 xcb_dri2_query_version_cookie_t dri2_query_cookie;
500 xcb_dri2_connect_reply_t *connect;
501 xcb_dri2_connect_cookie_t connect_cookie;
502 xcb_generic_error_t *error;
503 xcb_screen_iterator_t s;
504 char *driver_name, *device_name;
505 const xcb_query_extension_reply_t *extension;
506
507 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
508 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
509
510 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
511 if (!(extension && extension->present))
512 return EGL_FALSE;
513
514 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
515 if (!(extension && extension->present))
516 return EGL_FALSE;
517
518 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
519 XCB_XFIXES_MAJOR_VERSION,
520 XCB_XFIXES_MINOR_VERSION);
521
522 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
523 XCB_DRI2_MAJOR_VERSION,
524 XCB_DRI2_MINOR_VERSION);
525
526 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
527 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
528 s.data->root,
529 XCB_DRI2_DRIVER_TYPE_DRI);
530
531 xfixes_query =
532 xcb_xfixes_query_version_reply (dri2_dpy->conn,
533 xfixes_query_cookie, &error);
534 if (xfixes_query == NULL ||
535 error != NULL || xfixes_query->major_version < 2) {
536 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
537 free(error);
538 return EGL_FALSE;
539 }
540 free(xfixes_query);
541
542 dri2_query =
543 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
544 if (dri2_query == NULL || error != NULL) {
545 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
546 free(error);
547 return EGL_FALSE;
548 }
549 dri2_dpy->dri2_major = dri2_query->major_version;
550 dri2_dpy->dri2_minor = dri2_query->minor_version;
551 free(dri2_query);
552
553 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
554 if (connect == NULL ||
555 connect->driver_name_length + connect->device_name_length == 0) {
556 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
557 return EGL_FALSE;
558 }
559
560 driver_name = xcb_dri2_connect_driver_name (connect);
561 dri2_dpy->driver_name =
562 dri2_strndup(driver_name,
563 xcb_dri2_connect_driver_name_length (connect));
564
565 device_name = xcb_dri2_connect_device_name (connect);
566
567 dri2_dpy->device_name =
568 dri2_strndup(device_name,
569 xcb_dri2_connect_device_name_length (connect));
570
571 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
572 free(dri2_dpy->device_name);
573 free(dri2_dpy->driver_name);
574 free(connect);
575 return EGL_FALSE;
576 }
577 free(connect);
578
579 return EGL_TRUE;
580 }
581
582 static int
583 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
584 {
585 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
586 xcb_dri2_authenticate_reply_t *authenticate;
587 xcb_dri2_authenticate_cookie_t authenticate_cookie;
588 xcb_screen_iterator_t s;
589 int ret = 0;
590
591 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
592 authenticate_cookie =
593 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, id);
594 authenticate =
595 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
596
597 if (authenticate == NULL || !authenticate->authenticated)
598 ret = -1;
599
600 free(authenticate);
601
602 return ret;
603 }
604
605 static EGLBoolean
606 dri2_authenticate(_EGLDisplay *disp)
607 {
608 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
609 drm_magic_t magic;
610
611 if (drmGetMagic(dri2_dpy->fd, &magic)) {
612 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
613 return EGL_FALSE;
614 }
615
616 if (dri2_x11_authenticate(disp, magic) < 0) {
617 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
618 return EGL_FALSE;
619 }
620
621 return EGL_TRUE;
622 }
623
624 static EGLBoolean
625 dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
626 _EGLDisplay *disp)
627 {
628 xcb_screen_iterator_t s;
629 xcb_depth_iterator_t d;
630 xcb_visualtype_t *visuals;
631 int i, j, id;
632 EGLint surface_type;
633 EGLint config_attrs[] = {
634 EGL_NATIVE_VISUAL_ID, 0,
635 EGL_NATIVE_VISUAL_TYPE, 0,
636 EGL_NONE
637 };
638
639 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
640 d = xcb_screen_allowed_depths_iterator(s.data);
641 id = 1;
642
643 surface_type =
644 EGL_WINDOW_BIT |
645 EGL_PIXMAP_BIT |
646 EGL_PBUFFER_BIT |
647 EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
648
649 while (d.rem > 0) {
650 EGLBoolean class_added[6] = { 0, };
651
652 visuals = xcb_depth_visuals(d.data);
653 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
654 if (class_added[visuals[i]._class])
655 continue;
656
657 class_added[visuals[i]._class] = EGL_TRUE;
658 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
659 config_attrs[1] = visuals[i].visual_id;
660 config_attrs[3] = visuals[i]._class;
661
662 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
663 d.data->depth, surface_type, config_attrs, NULL);
664 }
665 }
666
667 xcb_depth_next(&d);
668 }
669
670 if (!_eglGetArraySize(disp->Configs)) {
671 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
672 return EGL_FALSE;
673 }
674
675 return EGL_TRUE;
676 }
677
678 static EGLBoolean
679 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
680 _EGLSurface *draw, xcb_xfixes_region_t region)
681 {
682 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
683 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
684 enum xcb_dri2_attachment_t render_attachment;
685 xcb_dri2_copy_region_cookie_t cookie;
686
687 /* No-op for a pixmap or pbuffer surface */
688 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
689 return EGL_TRUE;
690
691 if (dri2_dpy->flush)
692 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
693
694 if (dri2_surf->have_fake_front)
695 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
696 else
697 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
698
699 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
700 dri2_surf->drawable,
701 region,
702 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
703 render_attachment);
704 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
705
706 return EGL_TRUE;
707 }
708
709 static int64_t
710 dri2_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
711 int64_t msc, int64_t divisor, int64_t remainder)
712 {
713 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
714 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
715 uint32_t msc_hi = msc >> 32;
716 uint32_t msc_lo = msc & 0xffffffff;
717 uint32_t divisor_hi = divisor >> 32;
718 uint32_t divisor_lo = divisor & 0xffffffff;
719 uint32_t remainder_hi = remainder >> 32;
720 uint32_t remainder_lo = remainder & 0xffffffff;
721 xcb_dri2_swap_buffers_cookie_t cookie;
722 xcb_dri2_swap_buffers_reply_t *reply;
723 int64_t swap_count = -1;
724
725 /* No-op for a pixmap or pbuffer surface */
726 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
727 return 0;
728
729 if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available)
730 return dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
731
732 if (dri2_dpy->flush)
733 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
734
735 cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn, dri2_surf->drawable,
736 msc_hi, msc_lo, divisor_hi, divisor_lo, remainder_hi, remainder_lo);
737
738 reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
739
740 if (reply) {
741 swap_count = (((int64_t)reply->swap_hi) << 32) | reply->swap_lo;
742 free(reply);
743 }
744
745 return swap_count;
746 }
747
748 static EGLBoolean
749 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
750 {
751 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
752 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
753
754 if (dri2_dpy->dri2) {
755 return dri2_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1;
756 } else {
757 assert(dri2_dpy->swrast);
758
759 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
760 return EGL_TRUE;
761 }
762 }
763
764 static EGLBoolean
765 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
766 EGLint numRects, const EGLint *rects)
767 {
768 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
769 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
770 EGLBoolean ret;
771 xcb_xfixes_region_t region;
772 xcb_rectangle_t rectangles[16];
773 int i;
774
775 if (numRects > (int)ARRAY_SIZE(rectangles))
776 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
777
778 for (i = 0; i < numRects; i++) {
779 rectangles[i].x = rects[i * 4];
780 rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
781 rectangles[i].width = rects[i * 4 + 2];
782 rectangles[i].height = rects[i * 4 + 3];
783 }
784
785 region = xcb_generate_id(dri2_dpy->conn);
786 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
787 ret = dri2_copy_region(drv, disp, draw, region);
788 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
789
790 return ret;
791 }
792
793 static EGLBoolean
794 dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
795 EGLint x, EGLint y, EGLint width, EGLint height)
796 {
797 const EGLint rect[4] = { x, y, width, height };
798
799 if (x < 0 || y < 0 || width < 0 || height < 0)
800 _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
801
802 return dri2_swap_buffers_region(drv, disp, draw, 1, rect);
803 }
804
805 static EGLBoolean
806 dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
807 {
808 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
809 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
810
811 if (interval > surf->Config->MaxSwapInterval)
812 interval = surf->Config->MaxSwapInterval;
813 else if (interval < surf->Config->MinSwapInterval)
814 interval = surf->Config->MinSwapInterval;
815
816 if (interval != surf->SwapInterval && dri2_dpy->swap_available)
817 xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
818
819 surf->SwapInterval = interval;
820
821 return EGL_TRUE;
822 }
823
824 static EGLBoolean
825 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
826 EGLNativePixmapType native_target)
827 {
828 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
829 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
830 xcb_gcontext_t gc;
831 xcb_pixmap_t target = (uintptr_t )native_target;
832
833 (void) drv;
834
835 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
836
837 gc = xcb_generate_id(dri2_dpy->conn);
838 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
839 xcb_copy_area(dri2_dpy->conn,
840 dri2_surf->drawable,
841 target,
842 gc,
843 0, 0,
844 0, 0,
845 dri2_surf->base.Width,
846 dri2_surf->base.Height);
847 xcb_free_gc(dri2_dpy->conn, gc);
848
849 return EGL_TRUE;
850 }
851
852 static _EGLImage *
853 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
854 EGLClientBuffer buffer, const EGLint *attr_list)
855 {
856 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
857 struct dri2_egl_image *dri2_img;
858 unsigned int attachments[1];
859 xcb_drawable_t drawable;
860 xcb_dri2_get_buffers_cookie_t buffers_cookie;
861 xcb_dri2_get_buffers_reply_t *buffers_reply;
862 xcb_dri2_dri2_buffer_t *buffers;
863 xcb_get_geometry_cookie_t geometry_cookie;
864 xcb_get_geometry_reply_t *geometry_reply;
865 xcb_generic_error_t *error;
866 int stride, format;
867
868 (void) ctx;
869
870 drawable = (xcb_drawable_t) (uintptr_t) buffer;
871 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
872 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
873 buffers_cookie =
874 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
875 drawable, 1, 1, attachments);
876 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
877 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
878 buffers_cookie, NULL);
879 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
880 if (buffers == NULL) {
881 return NULL;
882 }
883
884 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
885 geometry_cookie, &error);
886 if (geometry_reply == NULL || error != NULL) {
887 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
888 free(error);
889 free(buffers_reply);
890 return NULL;
891 }
892
893 switch (geometry_reply->depth) {
894 case 16:
895 format = __DRI_IMAGE_FORMAT_RGB565;
896 break;
897 case 24:
898 format = __DRI_IMAGE_FORMAT_XRGB8888;
899 break;
900 case 32:
901 format = __DRI_IMAGE_FORMAT_ARGB8888;
902 break;
903 default:
904 _eglError(EGL_BAD_PARAMETER,
905 "dri2_create_image_khr: unsupported pixmap depth");
906 free(buffers_reply);
907 free(geometry_reply);
908 return NULL;
909 }
910
911 dri2_img = malloc(sizeof *dri2_img);
912 if (!dri2_img) {
913 free(buffers_reply);
914 free(geometry_reply);
915 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
916 return EGL_NO_IMAGE_KHR;
917 }
918
919 if (!_eglInitImage(&dri2_img->base, disp)) {
920 free(buffers_reply);
921 free(geometry_reply);
922 free(dri2_img);
923 return EGL_NO_IMAGE_KHR;
924 }
925
926 stride = buffers[0].pitch / buffers[0].cpp;
927 dri2_img->dri_image =
928 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
929 buffers_reply->width,
930 buffers_reply->height,
931 format,
932 buffers[0].name,
933 stride,
934 dri2_img);
935
936 free(buffers_reply);
937 free(geometry_reply);
938
939 return &dri2_img->base;
940 }
941
942 static _EGLImage *
943 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
944 _EGLContext *ctx, EGLenum target,
945 EGLClientBuffer buffer, const EGLint *attr_list)
946 {
947 (void) drv;
948
949 switch (target) {
950 case EGL_NATIVE_PIXMAP_KHR:
951 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
952 default:
953 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
954 }
955 }
956
957 static EGLBoolean
958 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
959 {
960 struct dri2_egl_display *dri2_dpy;
961
962 drv->API.CreateWindowSurface = dri2_create_window_surface;
963 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
964 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
965 drv->API.DestroySurface = dri2_destroy_surface;
966 drv->API.SwapBuffers = dri2_swap_buffers;
967 drv->API.CopyBuffers = dri2_copy_buffers;
968
969 drv->API.SwapBuffersRegionNOK = NULL;
970 drv->API.CreateImageKHR = NULL;
971 drv->API.DestroyImageKHR = NULL;
972 drv->API.CreateDRMImageMESA = NULL;
973 drv->API.ExportDRMImageMESA = NULL;
974
975 dri2_dpy = calloc(1, sizeof *dri2_dpy);
976 if (!dri2_dpy)
977 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
978
979 disp->DriverData = (void *) dri2_dpy;
980 if (disp->PlatformDisplay == NULL) {
981 dri2_dpy->conn = xcb_connect(0, 0);
982 } else {
983 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
984 }
985
986 if (xcb_connection_has_error(dri2_dpy->conn)) {
987 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
988 goto cleanup_dpy;
989 }
990
991 if (!dri2_load_driver_swrast(disp))
992 goto cleanup_conn;
993
994 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
995 dri2_dpy->swrast_loader_extension.base.version = __DRI_SWRAST_LOADER_VERSION;
996 dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
997 dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
998 dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
999
1000 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
1001 dri2_dpy->extensions[1] = NULL;
1002 dri2_dpy->extensions[2] = NULL;
1003
1004 if (!dri2_create_screen(disp))
1005 goto cleanup_driver;
1006
1007 if (dri2_dpy->conn) {
1008 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
1009 goto cleanup_configs;
1010 }
1011
1012 /* we're supporting EGL 1.4 */
1013 disp->VersionMajor = 1;
1014 disp->VersionMinor = 4;
1015
1016 return EGL_TRUE;
1017
1018 cleanup_configs:
1019 _eglCleanupDisplay(disp);
1020 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1021 cleanup_driver:
1022 dlclose(dri2_dpy->driver);
1023 cleanup_conn:
1024 if (disp->PlatformDisplay == NULL)
1025 xcb_disconnect(dri2_dpy->conn);
1026 cleanup_dpy:
1027 free(dri2_dpy);
1028
1029 return EGL_FALSE;
1030 }
1031
1032 static void
1033 dri2_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1034 {
1035 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1036 int arbitrary_max_interval = 1000;
1037
1038 /* default behavior for no SwapBuffers support: no vblank syncing
1039 * either.
1040 */
1041 dri2_dpy->min_swap_interval = 0;
1042 dri2_dpy->max_swap_interval = 0;
1043
1044 if (!dri2_dpy->swap_available)
1045 return;
1046
1047 /* If we do have swapbuffers, then we can support pretty much any swap
1048 * interval, but we allow driconf to override applications.
1049 */
1050 if (dri2_dpy->config)
1051 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1052 "vblank_mode", &vblank_mode);
1053 switch (vblank_mode) {
1054 case DRI_CONF_VBLANK_NEVER:
1055 dri2_dpy->min_swap_interval = 0;
1056 dri2_dpy->max_swap_interval = 0;
1057 dri2_dpy->default_swap_interval = 0;
1058 break;
1059 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1060 dri2_dpy->min_swap_interval = 1;
1061 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1062 dri2_dpy->default_swap_interval = 1;
1063 break;
1064 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1065 dri2_dpy->min_swap_interval = 0;
1066 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1067 dri2_dpy->default_swap_interval = 0;
1068 break;
1069 default:
1070 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1071 dri2_dpy->min_swap_interval = 0;
1072 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1073 dri2_dpy->default_swap_interval = 1;
1074 break;
1075 }
1076 }
1077
1078 static EGLBoolean
1079 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
1080 {
1081 struct dri2_egl_display *dri2_dpy;
1082
1083 drv->API.CreateWindowSurface = dri2_create_window_surface;
1084 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
1085 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
1086 drv->API.DestroySurface = dri2_destroy_surface;
1087 drv->API.SwapBuffers = dri2_swap_buffers;
1088 drv->API.CopyBuffers = dri2_copy_buffers;
1089 drv->API.CreateImageKHR = dri2_x11_create_image_khr;
1090 drv->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
1091 drv->API.PostSubBufferNV = dri2_post_sub_buffer;
1092 drv->API.SwapInterval = dri2_swap_interval;
1093
1094 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1095 if (!dri2_dpy)
1096 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1097
1098 disp->DriverData = (void *) dri2_dpy;
1099 if (disp->PlatformDisplay == NULL) {
1100 dri2_dpy->conn = xcb_connect(0, 0);
1101 } else {
1102 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
1103 }
1104
1105 if (xcb_connection_has_error(dri2_dpy->conn)) {
1106 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1107 goto cleanup_dpy;
1108 }
1109
1110 if (dri2_dpy->conn) {
1111 if (!dri2_connect(dri2_dpy))
1112 goto cleanup_conn;
1113 }
1114
1115 if (!dri2_load_driver(disp))
1116 goto cleanup_conn;
1117
1118 #ifdef O_CLOEXEC
1119 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
1120 if (dri2_dpy->fd == -1 && errno == EINVAL)
1121 #endif
1122 {
1123 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
1124 if (dri2_dpy->fd != -1)
1125 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
1126 FD_CLOEXEC);
1127 }
1128 if (dri2_dpy->fd == -1) {
1129 _eglLog(_EGL_WARNING,
1130 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
1131 strerror(errno));
1132 goto cleanup_driver;
1133 }
1134
1135 if (dri2_dpy->conn) {
1136 if (!dri2_authenticate(disp))
1137 goto cleanup_fd;
1138 }
1139
1140 if (dri2_dpy->dri2_minor >= 1) {
1141 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1142 dri2_dpy->dri2_loader_extension.base.version = 3;
1143 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
1144 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
1145 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1146 dri2_get_buffers_with_format;
1147 } else {
1148 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1149 dri2_dpy->dri2_loader_extension.base.version = 2;
1150 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
1151 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
1152 dri2_dpy->dri2_loader_extension.getBuffersWithFormat = NULL;
1153 }
1154
1155 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1156 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1157 dri2_dpy->extensions[2] = NULL;
1158
1159 dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1160 dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1161
1162 if (!dri2_create_screen(disp))
1163 goto cleanup_fd;
1164
1165 dri2_setup_swap_interval(dri2_dpy);
1166
1167 if (dri2_dpy->conn) {
1168 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
1169 goto cleanup_configs;
1170 }
1171
1172 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1173 disp->Extensions.NOK_swap_region = EGL_TRUE;
1174 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1175 disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1176
1177 #ifdef HAVE_WAYLAND_PLATFORM
1178 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1179 #endif
1180 dri2_dpy->authenticate = dri2_x11_authenticate;
1181
1182 /* we're supporting EGL 1.4 */
1183 disp->VersionMajor = 1;
1184 disp->VersionMinor = 4;
1185
1186 return EGL_TRUE;
1187
1188 cleanup_configs:
1189 _eglCleanupDisplay(disp);
1190 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1191 cleanup_fd:
1192 close(dri2_dpy->fd);
1193 cleanup_driver:
1194 dlclose(dri2_dpy->driver);
1195 cleanup_conn:
1196 if (disp->PlatformDisplay == NULL)
1197 xcb_disconnect(dri2_dpy->conn);
1198 cleanup_dpy:
1199 free(dri2_dpy);
1200
1201 return EGL_FALSE;
1202 }
1203
1204 EGLBoolean
1205 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1206 {
1207 EGLBoolean initialized = EGL_TRUE;
1208
1209 int x11_dri2_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1210
1211 if (x11_dri2_accel) {
1212 if (!dri2_initialize_x11_dri2(drv, disp)) {
1213 initialized = dri2_initialize_x11_swrast(drv, disp);
1214 }
1215 } else {
1216 initialized = dri2_initialize_x11_swrast(drv, disp);
1217 }
1218
1219 return initialized;
1220 }
1221