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