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