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