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