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