egl/wl,x11: Call dri2_swap_interval() statically
[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 #ifdef HAVE_LIBDRM
37 #include <xf86drm.h>
38 #endif
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "egl_dri2.h"
43
44 static EGLBoolean
45 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
46 EGLint interval);
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_x11_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_x11_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_x11_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
286 window, attrib_list);
287 if (surf != NULL) {
288 /* When we first create the DRI2 drawable, its swap interval on the
289 * server side is 1.
290 */
291 surf->SwapInterval = 1;
292
293 /* Override that with a driconf-set value. */
294 dri2_x11_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval);
295 }
296
297 return surf;
298 }
299
300 static _EGLSurface *
301 dri2_x11_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
302 _EGLConfig *conf, EGLNativePixmapType pixmap,
303 const EGLint *attrib_list)
304 {
305 return dri2_x11_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
306 pixmap, attrib_list);
307 }
308
309 static _EGLSurface *
310 dri2_x11_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
311 _EGLConfig *conf, const EGLint *attrib_list)
312 {
313 return dri2_x11_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
314 XCB_WINDOW_NONE, attrib_list);
315 }
316
317 static EGLBoolean
318 dri2_x11_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
319 {
320 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
321 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
322
323 (void) drv;
324
325 if (!_eglPutSurface(surf))
326 return EGL_TRUE;
327
328 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
329
330 if (dri2_dpy->dri2) {
331 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
332 } else {
333 assert(dri2_dpy->swrast);
334 swrastDestroyDrawable(dri2_dpy, dri2_surf);
335 }
336
337 if (surf->Type == EGL_PBUFFER_BIT)
338 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
339
340 free(surf);
341
342 return EGL_TRUE;
343 }
344
345 /**
346 * Process list of buffer received from the server
347 *
348 * Processes the list of buffers received in a reply from the server to either
349 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
350 */
351 static void
352 dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
353 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
354 {
355 struct dri2_egl_display *dri2_dpy =
356 dri2_egl_display(dri2_surf->base.Resource.Display);
357 xcb_rectangle_t rectangle;
358 unsigned i;
359
360 dri2_surf->buffer_count = count;
361 dri2_surf->have_fake_front = 0;
362
363 /* This assumes the DRI2 buffer attachment tokens matches the
364 * __DRIbuffer tokens. */
365 for (i = 0; i < count; i++) {
366 dri2_surf->buffers[i].attachment = buffers[i].attachment;
367 dri2_surf->buffers[i].name = buffers[i].name;
368 dri2_surf->buffers[i].pitch = buffers[i].pitch;
369 dri2_surf->buffers[i].cpp = buffers[i].cpp;
370 dri2_surf->buffers[i].flags = buffers[i].flags;
371
372 /* We only use the DRI drivers single buffer configs. This
373 * means that if we try to render to a window, DRI2 will give us
374 * the fake front buffer, which we'll use as a back buffer.
375 * Note that EGL doesn't require that several clients rendering
376 * to the same window must see the same aux buffers. */
377 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
378 dri2_surf->have_fake_front = 1;
379 }
380
381 if (dri2_surf->region != XCB_NONE)
382 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
383
384 rectangle.x = 0;
385 rectangle.y = 0;
386 rectangle.width = dri2_surf->base.Width;
387 rectangle.height = dri2_surf->base.Height;
388 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
389 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
390 }
391
392 static __DRIbuffer *
393 dri2_x11_get_buffers(__DRIdrawable * driDrawable,
394 int *width, int *height,
395 unsigned int *attachments, int count,
396 int *out_count, void *loaderPrivate)
397 {
398 struct dri2_egl_surface *dri2_surf = loaderPrivate;
399 struct dri2_egl_display *dri2_dpy =
400 dri2_egl_display(dri2_surf->base.Resource.Display);
401 xcb_dri2_dri2_buffer_t *buffers;
402 xcb_dri2_get_buffers_reply_t *reply;
403 xcb_dri2_get_buffers_cookie_t cookie;
404
405 (void) driDrawable;
406
407 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
408 dri2_surf->drawable,
409 count, count, attachments);
410 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
411 buffers = xcb_dri2_get_buffers_buffers (reply);
412 if (buffers == NULL)
413 return NULL;
414
415 *out_count = reply->count;
416 dri2_surf->base.Width = *width = reply->width;
417 dri2_surf->base.Height = *height = reply->height;
418 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
419
420 free(reply);
421
422 return dri2_surf->buffers;
423 }
424
425 static __DRIbuffer *
426 dri2_x11_get_buffers_with_format(__DRIdrawable * driDrawable,
427 int *width, int *height,
428 unsigned int *attachments, int count,
429 int *out_count, void *loaderPrivate)
430 {
431 struct dri2_egl_surface *dri2_surf = loaderPrivate;
432 struct dri2_egl_display *dri2_dpy =
433 dri2_egl_display(dri2_surf->base.Resource.Display);
434 xcb_dri2_dri2_buffer_t *buffers;
435 xcb_dri2_get_buffers_with_format_reply_t *reply;
436 xcb_dri2_get_buffers_with_format_cookie_t cookie;
437 xcb_dri2_attach_format_t *format_attachments;
438
439 (void) driDrawable;
440
441 format_attachments = (xcb_dri2_attach_format_t *) attachments;
442 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
443 dri2_surf->drawable,
444 count, count,
445 format_attachments);
446
447 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
448 cookie, NULL);
449 if (reply == NULL)
450 return NULL;
451
452 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
453 dri2_surf->base.Width = *width = reply->width;
454 dri2_surf->base.Height = *height = reply->height;
455 *out_count = reply->count;
456 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
457
458 free(reply);
459
460 return dri2_surf->buffers;
461 }
462
463 static void
464 dri2_x11_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
465 {
466 (void) driDrawable;
467
468 /* FIXME: Does EGL support front buffer rendering at all? */
469
470 #if 0
471 struct dri2_egl_surface *dri2_surf = loaderPrivate;
472
473 dri2WaitGL(dri2_surf);
474 #else
475 (void) loaderPrivate;
476 #endif
477 }
478
479 static char *
480 dri2_x11_strndup(const char *s, int length)
481 {
482 char *d;
483
484 d = malloc(length + 1);
485 if (d == NULL)
486 return NULL;
487
488 memcpy(d, s, length);
489 d[length] = '\0';
490
491 return d;
492 }
493
494 static EGLBoolean
495 dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
496 {
497 xcb_xfixes_query_version_reply_t *xfixes_query;
498 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
499 xcb_dri2_query_version_reply_t *dri2_query;
500 xcb_dri2_query_version_cookie_t dri2_query_cookie;
501 xcb_dri2_connect_reply_t *connect;
502 xcb_dri2_connect_cookie_t connect_cookie;
503 xcb_generic_error_t *error;
504 xcb_screen_iterator_t s;
505 char *driver_name, *device_name;
506 const xcb_query_extension_reply_t *extension;
507
508 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
509 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
510
511 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
512 if (!(extension && extension->present))
513 return EGL_FALSE;
514
515 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
516 if (!(extension && extension->present))
517 return EGL_FALSE;
518
519 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
520 XCB_XFIXES_MAJOR_VERSION,
521 XCB_XFIXES_MINOR_VERSION);
522
523 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
524 XCB_DRI2_MAJOR_VERSION,
525 XCB_DRI2_MINOR_VERSION);
526
527 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
528 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
529 s.data->root,
530 XCB_DRI2_DRIVER_TYPE_DRI);
531
532 xfixes_query =
533 xcb_xfixes_query_version_reply (dri2_dpy->conn,
534 xfixes_query_cookie, &error);
535 if (xfixes_query == NULL ||
536 error != NULL || xfixes_query->major_version < 2) {
537 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
538 free(error);
539 return EGL_FALSE;
540 }
541 free(xfixes_query);
542
543 dri2_query =
544 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
545 if (dri2_query == NULL || error != NULL) {
546 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
547 free(error);
548 return EGL_FALSE;
549 }
550 dri2_dpy->dri2_major = dri2_query->major_version;
551 dri2_dpy->dri2_minor = dri2_query->minor_version;
552 free(dri2_query);
553
554 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
555 if (connect == NULL ||
556 connect->driver_name_length + connect->device_name_length == 0) {
557 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
558 return EGL_FALSE;
559 }
560
561 driver_name = xcb_dri2_connect_driver_name (connect);
562 dri2_dpy->driver_name =
563 dri2_x11_strndup(driver_name,
564 xcb_dri2_connect_driver_name_length(connect));
565
566 device_name = xcb_dri2_connect_device_name (connect);
567
568 dri2_dpy->device_name =
569 dri2_x11_strndup(device_name,
570 xcb_dri2_connect_device_name_length(connect));
571
572 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
573 free(dri2_dpy->device_name);
574 free(dri2_dpy->driver_name);
575 free(connect);
576 return EGL_FALSE;
577 }
578 free(connect);
579
580 return EGL_TRUE;
581 }
582
583 static int
584 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
585 {
586 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
587 xcb_dri2_authenticate_reply_t *authenticate;
588 xcb_dri2_authenticate_cookie_t authenticate_cookie;
589 xcb_screen_iterator_t s;
590 int ret = 0;
591
592 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
593 authenticate_cookie =
594 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, id);
595 authenticate =
596 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
597
598 if (authenticate == NULL || !authenticate->authenticated)
599 ret = -1;
600
601 free(authenticate);
602
603 return ret;
604 }
605
606 static EGLBoolean
607 dri2_x11_local_authenticate(_EGLDisplay *disp)
608 {
609 #ifdef HAVE_LIBDRM
610 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
611 drm_magic_t magic;
612
613 if (drmGetMagic(dri2_dpy->fd, &magic)) {
614 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
615 return EGL_FALSE;
616 }
617
618 if (dri2_x11_authenticate(disp, magic) < 0) {
619 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
620 return EGL_FALSE;
621 }
622 #endif
623 return EGL_TRUE;
624 }
625
626 static EGLBoolean
627 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
628 _EGLDisplay *disp)
629 {
630 xcb_screen_iterator_t s;
631 xcb_depth_iterator_t d;
632 xcb_visualtype_t *visuals;
633 int i, j, id;
634 unsigned int rgba_masks[4];
635 EGLint surface_type;
636 EGLint config_attrs[] = {
637 EGL_NATIVE_VISUAL_ID, 0,
638 EGL_NATIVE_VISUAL_TYPE, 0,
639 EGL_NONE
640 };
641
642 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
643 d = xcb_screen_allowed_depths_iterator(s.data);
644 id = 1;
645
646 surface_type =
647 EGL_WINDOW_BIT |
648 EGL_PIXMAP_BIT |
649 EGL_PBUFFER_BIT |
650 EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
651
652 while (d.rem > 0) {
653 EGLBoolean class_added[6] = { 0, };
654
655 visuals = xcb_depth_visuals(d.data);
656 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
657 if (class_added[visuals[i]._class])
658 continue;
659
660 class_added[visuals[i]._class] = EGL_TRUE;
661 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
662 config_attrs[1] = visuals[i].visual_id;
663 config_attrs[3] = visuals[i]._class;
664
665 rgba_masks[0] = visuals[i].red_mask;
666 rgba_masks[1] = visuals[i].green_mask;
667 rgba_masks[2] = visuals[i].blue_mask;
668 rgba_masks[3] = 0;
669 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
670 surface_type, config_attrs, rgba_masks);
671
672 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
673 * Otherwise it will only match a 32-bit RGBA visual. On a
674 * composited window manager on X11, this will make all of the
675 * EGLConfigs with destination alpha get blended by the
676 * compositor. This is probably not what the application
677 * wants... especially on drivers that only have 32-bit RGBA
678 * EGLConfigs! */
679 if (d.data->depth == 24) {
680 rgba_masks[3] =
681 ~(rgba_masks[0] | rgba_masks[1] | rgba_masks[2]);
682 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
683 surface_type, config_attrs, rgba_masks);
684 }
685 }
686 }
687
688 xcb_depth_next(&d);
689 }
690
691 if (!_eglGetArraySize(disp->Configs)) {
692 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
693 return EGL_FALSE;
694 }
695
696 return EGL_TRUE;
697 }
698
699 static EGLBoolean
700 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
701 _EGLSurface *draw, xcb_xfixes_region_t region)
702 {
703 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
704 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
705 enum xcb_dri2_attachment_t render_attachment;
706 xcb_dri2_copy_region_cookie_t cookie;
707
708 /* No-op for a pixmap or pbuffer surface */
709 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
710 return EGL_TRUE;
711
712 if (dri2_dpy->flush)
713 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
714
715 if (dri2_surf->have_fake_front)
716 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
717 else
718 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
719
720 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
721 dri2_surf->drawable,
722 region,
723 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
724 render_attachment);
725 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
726
727 return EGL_TRUE;
728 }
729
730 static int64_t
731 dri2_x11_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
732 int64_t msc, int64_t divisor, int64_t remainder)
733 {
734 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
735 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
736 uint32_t msc_hi = msc >> 32;
737 uint32_t msc_lo = msc & 0xffffffff;
738 uint32_t divisor_hi = divisor >> 32;
739 uint32_t divisor_lo = divisor & 0xffffffff;
740 uint32_t remainder_hi = remainder >> 32;
741 uint32_t remainder_lo = remainder & 0xffffffff;
742 xcb_dri2_swap_buffers_cookie_t cookie;
743 xcb_dri2_swap_buffers_reply_t *reply;
744 int64_t swap_count = -1;
745
746 /* No-op for a pixmap or pbuffer surface */
747 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
748 return 0;
749
750 if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available)
751 return dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
752
753 if (dri2_dpy->flush)
754 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
755
756 cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn, dri2_surf->drawable,
757 msc_hi, msc_lo, divisor_hi, divisor_lo, remainder_hi, remainder_lo);
758
759 reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
760
761 if (reply) {
762 swap_count = (((int64_t)reply->swap_hi) << 32) | reply->swap_lo;
763 free(reply);
764 }
765
766 /* Since we aren't watching for the server's invalidate events like we're
767 * supposed to (due to XCB providing no mechanism for filtering the events
768 * the way xlib does), and SwapBuffers is a common cause of invalidate
769 * events, just shove one down to the driver, even though we haven't told
770 * the driver that we're the kind of loader that provides reliable
771 * invalidate events. This causes the driver to request buffers again at
772 * its next draw, so that we get the correct buffers if a pageflip
773 * happened. The driver should still be using the viewport hack to catch
774 * window resizes.
775 */
776 if (dri2_dpy->flush &&
777 dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
778 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
779
780 return swap_count;
781 }
782
783 static EGLBoolean
784 dri2_x11_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
785 {
786 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
787 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
788
789 if (dri2_dpy->dri2) {
790 return dri2_x11_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1;
791 } else {
792 assert(dri2_dpy->swrast);
793
794 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
795 return EGL_TRUE;
796 }
797 }
798
799 static EGLBoolean
800 dri2_x11_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp,
801 _EGLSurface *draw,
802 EGLint numRects, const EGLint *rects)
803 {
804 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
805 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
806 EGLBoolean ret;
807 xcb_xfixes_region_t region;
808 xcb_rectangle_t rectangles[16];
809 int i;
810
811 if (numRects > (int)ARRAY_SIZE(rectangles))
812 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
813
814 for (i = 0; i < numRects; i++) {
815 rectangles[i].x = rects[i * 4];
816 rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
817 rectangles[i].width = rects[i * 4 + 2];
818 rectangles[i].height = rects[i * 4 + 3];
819 }
820
821 region = xcb_generate_id(dri2_dpy->conn);
822 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
823 ret = dri2_copy_region(drv, disp, draw, region);
824 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
825
826 return ret;
827 }
828
829 static EGLBoolean
830 dri2_x11_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
831 EGLint x, EGLint y, EGLint width, EGLint height)
832 {
833 const EGLint rect[4] = { x, y, width, height };
834
835 if (x < 0 || y < 0 || width < 0 || height < 0)
836 _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
837
838 return dri2_x11_swap_buffers_region(drv, disp, draw, 1, rect);
839 }
840
841 static EGLBoolean
842 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
843 EGLint interval)
844 {
845 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
846 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
847
848 if (interval > surf->Config->MaxSwapInterval)
849 interval = surf->Config->MaxSwapInterval;
850 else if (interval < surf->Config->MinSwapInterval)
851 interval = surf->Config->MinSwapInterval;
852
853 if (interval != surf->SwapInterval && dri2_dpy->swap_available)
854 xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
855
856 surf->SwapInterval = interval;
857
858 return EGL_TRUE;
859 }
860
861 static EGLBoolean
862 dri2_x11_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
863 EGLNativePixmapType native_target)
864 {
865 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
866 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
867 xcb_gcontext_t gc;
868 xcb_pixmap_t target = (uintptr_t )native_target;
869
870 (void) drv;
871
872 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
873
874 gc = xcb_generate_id(dri2_dpy->conn);
875 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
876 xcb_copy_area(dri2_dpy->conn,
877 dri2_surf->drawable,
878 target,
879 gc,
880 0, 0,
881 0, 0,
882 dri2_surf->base.Width,
883 dri2_surf->base.Height);
884 xcb_free_gc(dri2_dpy->conn, gc);
885
886 return EGL_TRUE;
887 }
888
889 static _EGLImage *
890 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
891 EGLClientBuffer buffer, const EGLint *attr_list)
892 {
893 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
894 struct dri2_egl_image *dri2_img;
895 unsigned int attachments[1];
896 xcb_drawable_t drawable;
897 xcb_dri2_get_buffers_cookie_t buffers_cookie;
898 xcb_dri2_get_buffers_reply_t *buffers_reply;
899 xcb_dri2_dri2_buffer_t *buffers;
900 xcb_get_geometry_cookie_t geometry_cookie;
901 xcb_get_geometry_reply_t *geometry_reply;
902 xcb_generic_error_t *error;
903 int stride, format;
904
905 (void) ctx;
906
907 drawable = (xcb_drawable_t) (uintptr_t) buffer;
908 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
909 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
910 buffers_cookie =
911 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
912 drawable, 1, 1, attachments);
913 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
914 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
915 buffers_cookie, NULL);
916 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
917 if (buffers == NULL) {
918 return NULL;
919 }
920
921 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
922 geometry_cookie, &error);
923 if (geometry_reply == NULL || error != NULL) {
924 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
925 free(error);
926 free(buffers_reply);
927 return NULL;
928 }
929
930 switch (geometry_reply->depth) {
931 case 16:
932 format = __DRI_IMAGE_FORMAT_RGB565;
933 break;
934 case 24:
935 format = __DRI_IMAGE_FORMAT_XRGB8888;
936 break;
937 case 32:
938 format = __DRI_IMAGE_FORMAT_ARGB8888;
939 break;
940 default:
941 _eglError(EGL_BAD_PARAMETER,
942 "dri2_create_image_khr: unsupported pixmap depth");
943 free(buffers_reply);
944 free(geometry_reply);
945 return NULL;
946 }
947
948 dri2_img = malloc(sizeof *dri2_img);
949 if (!dri2_img) {
950 free(buffers_reply);
951 free(geometry_reply);
952 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
953 return EGL_NO_IMAGE_KHR;
954 }
955
956 if (!_eglInitImage(&dri2_img->base, disp)) {
957 free(buffers_reply);
958 free(geometry_reply);
959 free(dri2_img);
960 return EGL_NO_IMAGE_KHR;
961 }
962
963 stride = buffers[0].pitch / buffers[0].cpp;
964 dri2_img->dri_image =
965 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
966 buffers_reply->width,
967 buffers_reply->height,
968 format,
969 buffers[0].name,
970 stride,
971 dri2_img);
972
973 free(buffers_reply);
974 free(geometry_reply);
975
976 return &dri2_img->base;
977 }
978
979 static _EGLImage *
980 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
981 _EGLContext *ctx, EGLenum target,
982 EGLClientBuffer buffer, const EGLint *attr_list)
983 {
984 (void) drv;
985
986 switch (target) {
987 case EGL_NATIVE_PIXMAP_KHR:
988 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
989 default:
990 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
991 }
992 }
993
994 static struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
995 .authenticate = NULL,
996 };
997
998 static struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
999 .authenticate = dri2_x11_authenticate,
1000 };
1001
1002 static EGLBoolean
1003 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1004 {
1005 struct dri2_egl_display *dri2_dpy;
1006
1007 drv->API.CreateWindowSurface = dri2_x11_create_window_surface;
1008 drv->API.CreatePixmapSurface = dri2_x11_create_pixmap_surface;
1009 drv->API.CreatePbufferSurface = dri2_x11_create_pbuffer_surface;
1010 drv->API.DestroySurface = dri2_x11_destroy_surface;
1011 drv->API.SwapBuffers = dri2_x11_swap_buffers;
1012 drv->API.CopyBuffers = dri2_x11_copy_buffers;
1013
1014 drv->API.SwapBuffersRegionNOK = NULL;
1015 drv->API.CreateImageKHR = NULL;
1016 drv->API.DestroyImageKHR = NULL;
1017 drv->API.CreateDRMImageMESA = NULL;
1018 drv->API.ExportDRMImageMESA = NULL;
1019
1020 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1021 if (!dri2_dpy)
1022 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1023
1024 disp->DriverData = (void *) dri2_dpy;
1025 if (disp->PlatformDisplay == NULL) {
1026 dri2_dpy->conn = xcb_connect(0, 0);
1027 } else {
1028 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
1029 }
1030
1031 if (xcb_connection_has_error(dri2_dpy->conn)) {
1032 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1033 goto cleanup_dpy;
1034 }
1035
1036 if (!dri2_load_driver_swrast(disp))
1037 goto cleanup_conn;
1038
1039 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
1040 dri2_dpy->swrast_loader_extension.base.version = __DRI_SWRAST_LOADER_VERSION;
1041 dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
1042 dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
1043 dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
1044
1045 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
1046 dri2_dpy->extensions[1] = NULL;
1047 dri2_dpy->extensions[2] = NULL;
1048
1049 if (!dri2_create_screen(disp))
1050 goto cleanup_driver;
1051
1052 if (dri2_dpy->conn) {
1053 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp))
1054 goto cleanup_configs;
1055 }
1056
1057 /* we're supporting EGL 1.4 */
1058 disp->VersionMajor = 1;
1059 disp->VersionMinor = 4;
1060
1061 /* Fill vtbl last to prevent accidentally calling virtual function during
1062 * initialization.
1063 */
1064 dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1065
1066 return EGL_TRUE;
1067
1068 cleanup_configs:
1069 _eglCleanupDisplay(disp);
1070 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1071 cleanup_driver:
1072 dlclose(dri2_dpy->driver);
1073 cleanup_conn:
1074 if (disp->PlatformDisplay == NULL)
1075 xcb_disconnect(dri2_dpy->conn);
1076 cleanup_dpy:
1077 free(dri2_dpy);
1078
1079 return EGL_FALSE;
1080 }
1081
1082 static void
1083 dri2_x11_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1084 {
1085 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1086 int arbitrary_max_interval = 1000;
1087
1088 /* default behavior for no SwapBuffers support: no vblank syncing
1089 * either.
1090 */
1091 dri2_dpy->min_swap_interval = 0;
1092 dri2_dpy->max_swap_interval = 0;
1093
1094 if (!dri2_dpy->swap_available)
1095 return;
1096
1097 /* If we do have swapbuffers, then we can support pretty much any swap
1098 * interval, but we allow driconf to override applications.
1099 */
1100 if (dri2_dpy->config)
1101 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1102 "vblank_mode", &vblank_mode);
1103 switch (vblank_mode) {
1104 case DRI_CONF_VBLANK_NEVER:
1105 dri2_dpy->min_swap_interval = 0;
1106 dri2_dpy->max_swap_interval = 0;
1107 dri2_dpy->default_swap_interval = 0;
1108 break;
1109 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1110 dri2_dpy->min_swap_interval = 1;
1111 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1112 dri2_dpy->default_swap_interval = 1;
1113 break;
1114 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1115 dri2_dpy->min_swap_interval = 0;
1116 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1117 dri2_dpy->default_swap_interval = 0;
1118 break;
1119 default:
1120 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1121 dri2_dpy->min_swap_interval = 0;
1122 dri2_dpy->max_swap_interval = arbitrary_max_interval;
1123 dri2_dpy->default_swap_interval = 1;
1124 break;
1125 }
1126 }
1127
1128 static EGLBoolean
1129 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
1130 {
1131 struct dri2_egl_display *dri2_dpy;
1132
1133 drv->API.CreateWindowSurface = dri2_x11_create_window_surface;
1134 drv->API.CreatePixmapSurface = dri2_x11_create_pixmap_surface;
1135 drv->API.CreatePbufferSurface = dri2_x11_create_pbuffer_surface;
1136 drv->API.DestroySurface = dri2_x11_destroy_surface;
1137 drv->API.SwapBuffers = dri2_x11_swap_buffers;
1138 drv->API.CopyBuffers = dri2_x11_copy_buffers;
1139 drv->API.CreateImageKHR = dri2_x11_create_image_khr;
1140 drv->API.SwapBuffersRegionNOK = dri2_x11_swap_buffers_region;
1141 drv->API.PostSubBufferNV = dri2_x11_post_sub_buffer;
1142 drv->API.SwapInterval = dri2_x11_swap_interval;
1143
1144 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1145 if (!dri2_dpy)
1146 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1147
1148 disp->DriverData = (void *) dri2_dpy;
1149 if (disp->PlatformDisplay == NULL) {
1150 dri2_dpy->conn = xcb_connect(0, 0);
1151 } else {
1152 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
1153 }
1154
1155 if (xcb_connection_has_error(dri2_dpy->conn)) {
1156 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1157 goto cleanup_dpy;
1158 }
1159
1160 if (dri2_dpy->conn) {
1161 if (!dri2_x11_connect(dri2_dpy))
1162 goto cleanup_conn;
1163 }
1164
1165 if (!dri2_load_driver(disp))
1166 goto cleanup_conn;
1167
1168 #ifdef O_CLOEXEC
1169 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
1170 if (dri2_dpy->fd == -1 && errno == EINVAL)
1171 #endif
1172 {
1173 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
1174 if (dri2_dpy->fd != -1)
1175 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
1176 FD_CLOEXEC);
1177 }
1178 if (dri2_dpy->fd == -1) {
1179 _eglLog(_EGL_WARNING,
1180 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
1181 strerror(errno));
1182 goto cleanup_driver;
1183 }
1184
1185 if (dri2_dpy->conn) {
1186 if (!dri2_x11_local_authenticate(disp))
1187 goto cleanup_fd;
1188 }
1189
1190 if (dri2_dpy->dri2_minor >= 1) {
1191 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1192 dri2_dpy->dri2_loader_extension.base.version = 3;
1193 dri2_dpy->dri2_loader_extension.getBuffers = dri2_x11_get_buffers;
1194 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_x11_flush_front_buffer;
1195 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1196 dri2_x11_get_buffers_with_format;
1197 } else {
1198 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1199 dri2_dpy->dri2_loader_extension.base.version = 2;
1200 dri2_dpy->dri2_loader_extension.getBuffers = dri2_x11_get_buffers;
1201 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_x11_flush_front_buffer;
1202 dri2_dpy->dri2_loader_extension.getBuffersWithFormat = NULL;
1203 }
1204
1205 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1206 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1207 dri2_dpy->extensions[2] = NULL;
1208
1209 dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1210 dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1211
1212 if (!dri2_create_screen(disp))
1213 goto cleanup_fd;
1214
1215 dri2_x11_setup_swap_interval(dri2_dpy);
1216
1217 if (dri2_dpy->conn) {
1218 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp))
1219 goto cleanup_configs;
1220 }
1221
1222 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1223 disp->Extensions.NOK_swap_region = EGL_TRUE;
1224 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1225 disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1226
1227 #ifdef HAVE_WAYLAND_PLATFORM
1228 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1229 #endif
1230
1231 if (dri2_dpy->conn) {
1232 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp))
1233 goto cleanup_configs;
1234 }
1235
1236 /* we're supporting EGL 1.4 */
1237 disp->VersionMajor = 1;
1238 disp->VersionMinor = 4;
1239
1240 /* Fill vtbl last to prevent accidentally calling virtual function during
1241 * initialization.
1242 */
1243 dri2_dpy->vtbl = &dri2_x11_display_vtbl;
1244
1245 return EGL_TRUE;
1246
1247 cleanup_configs:
1248 _eglCleanupDisplay(disp);
1249 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1250 cleanup_fd:
1251 close(dri2_dpy->fd);
1252 cleanup_driver:
1253 dlclose(dri2_dpy->driver);
1254 cleanup_conn:
1255 if (disp->PlatformDisplay == NULL)
1256 xcb_disconnect(dri2_dpy->conn);
1257 cleanup_dpy:
1258 free(dri2_dpy);
1259
1260 return EGL_FALSE;
1261 }
1262
1263 EGLBoolean
1264 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1265 {
1266 EGLBoolean initialized = EGL_TRUE;
1267
1268 int x11_dri2_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1269
1270 if (x11_dri2_accel) {
1271 if (!dri2_initialize_x11_dri2(drv, disp)) {
1272 initialized = dri2_initialize_x11_swrast(drv, disp);
1273 }
1274 } else {
1275 initialized = dri2_initialize_x11_swrast(drv, disp);
1276 }
1277
1278 return initialized;
1279 }
1280