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