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