egl: Add EGL_WL_bind_wayland_display
[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 <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <xf86drm.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #include "egl_dri2.h"
41
42 static void
43 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
44 struct dri2_egl_surface * dri2_surf,
45 int depth)
46 {
47 uint32_t mask;
48 const uint32_t function = GXcopy;
49 uint32_t valgc[2];
50
51 /* create GC's */
52 dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
53 mask = XCB_GC_FUNCTION;
54 xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask, &function);
55
56 dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
57 mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
58 valgc[0] = function;
59 valgc[1] = False;
60 xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask, valgc);
61 dri2_surf->depth = depth;
62 switch (depth) {
63 case 32:
64 case 24:
65 dri2_surf->bytes_per_pixel = 4;
66 break;
67 case 16:
68 dri2_surf->bytes_per_pixel = 2;
69 break;
70 case 8:
71 dri2_surf->bytes_per_pixel = 1;
72 break;
73 case 0:
74 dri2_surf->bytes_per_pixel = 0;
75 break;
76 default:
77 _eglLog(_EGL_WARNING, "unsupported depth %d", depth);
78 }
79 }
80
81 static void
82 swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
83 struct dri2_egl_surface * dri2_surf)
84 {
85 xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
86 xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
87 }
88
89 static void
90 swrastGetDrawableInfo(__DRIdrawable * draw,
91 int *x, int *y, int *w, int *h,
92 void *loaderPrivate)
93 {
94 struct dri2_egl_surface *dri2_surf = loaderPrivate;
95 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
96
97 xcb_get_geometry_cookie_t cookie;
98 xcb_get_geometry_reply_t *reply;
99 xcb_generic_error_t *error;
100
101 *w = *h = 0;
102 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
103 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
104 if (reply == NULL)
105 return;
106
107 if (error != NULL) {
108 _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
109 free(error);
110 } else {
111 *w = reply->width;
112 *h = reply->height;
113 }
114 free(reply);
115 }
116
117 static void
118 swrastPutImage(__DRIdrawable * draw, int op,
119 int x, int y, int w, int h,
120 char *data, void *loaderPrivate)
121 {
122 struct dri2_egl_surface *dri2_surf = loaderPrivate;
123 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
124
125 xcb_gcontext_t gc;
126
127 switch (op) {
128 case __DRI_SWRAST_IMAGE_OP_DRAW:
129 gc = dri2_surf->gc;
130 break;
131 case __DRI_SWRAST_IMAGE_OP_SWAP:
132 gc = dri2_surf->swapgc;
133 break;
134 default:
135 return;
136 }
137
138 xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable,
139 gc, w, h, x, y, 0, dri2_surf->depth,
140 w*h*dri2_surf->bytes_per_pixel, (const uint8_t *)data);
141 }
142
143 static void
144 swrastGetImage(__DRIdrawable * read,
145 int x, int y, int w, int h,
146 char *data, void *loaderPrivate)
147 {
148 struct dri2_egl_surface *dri2_surf = loaderPrivate;
149 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
150
151 xcb_get_image_cookie_t cookie;
152 xcb_get_image_reply_t *reply;
153 xcb_generic_error_t *error;
154
155 cookie = xcb_get_image (dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
156 dri2_surf->drawable, x, y, w, h, ~0);
157 reply = xcb_get_image_reply (dri2_dpy->conn, cookie, &error);
158 if (reply == NULL)
159 return;
160
161 if (error != NULL) {
162 _eglLog(_EGL_WARNING, "error in xcb_get_image");
163 free(error);
164 } else {
165 uint32_t bytes = xcb_get_image_data_length(reply);
166 uint8_t *idata = xcb_get_image_data(reply);
167 memcpy(data, idata, bytes);
168 }
169 free(reply);
170 }
171
172
173 /**
174 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
175 */
176 static _EGLSurface *
177 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
178 _EGLConfig *conf, EGLNativeWindowType window,
179 const EGLint *attrib_list)
180 {
181 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
182 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
183 struct dri2_egl_surface *dri2_surf;
184 xcb_get_geometry_cookie_t cookie;
185 xcb_get_geometry_reply_t *reply;
186 xcb_screen_iterator_t s;
187 xcb_generic_error_t *error;
188
189 (void) drv;
190
191 dri2_surf = malloc(sizeof *dri2_surf);
192 if (!dri2_surf) {
193 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
194 return NULL;
195 }
196
197 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
198 goto cleanup_surf;
199
200 dri2_surf->region = XCB_NONE;
201 if (type == EGL_PBUFFER_BIT) {
202 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
203 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
204 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
205 dri2_surf->drawable, s.data->root,
206 dri2_surf->base.Width, dri2_surf->base.Height);
207 } else {
208 dri2_surf->drawable = window;
209 }
210
211 if (dri2_dpy->dri2) {
212 dri2_surf->dri_drawable =
213 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
214 type == EGL_WINDOW_BIT ?
215 dri2_conf->dri_double_config :
216 dri2_conf->dri_single_config,
217 dri2_surf);
218 } else {
219 assert(dri2_dpy->swrast);
220 dri2_surf->dri_drawable =
221 (*dri2_dpy->swrast->createNewDrawable) (dri2_dpy->dri_screen,
222 dri2_conf->dri_double_config,
223 dri2_surf);
224 }
225
226 if (dri2_surf->dri_drawable == NULL) {
227 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
228 goto cleanup_pixmap;
229 }
230
231 if (dri2_dpy->dri2) {
232 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
233 } else {
234 swrastCreateDrawable(dri2_dpy, dri2_surf, _eglGetConfigKey(conf, EGL_DEPTH_SIZE));
235 }
236
237 if (type != EGL_PBUFFER_BIT) {
238 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
239 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
240 if (reply == NULL || error != NULL) {
241 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
242 free(error);
243 goto cleanup_dri_drawable;
244 }
245
246 dri2_surf->base.Width = reply->width;
247 dri2_surf->base.Height = reply->height;
248 free(reply);
249 }
250
251 return &dri2_surf->base;
252
253 cleanup_dri_drawable:
254 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
255 cleanup_pixmap:
256 if (type == EGL_PBUFFER_BIT)
257 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
258 cleanup_surf:
259 free(dri2_surf);
260
261 return NULL;
262 }
263
264 /**
265 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
266 */
267 static _EGLSurface *
268 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
269 _EGLConfig *conf, EGLNativeWindowType window,
270 const EGLint *attrib_list)
271 {
272 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
273 window, attrib_list);
274 }
275
276 static _EGLSurface *
277 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
278 _EGLConfig *conf, EGLNativePixmapType pixmap,
279 const EGLint *attrib_list)
280 {
281 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
282 pixmap, attrib_list);
283 }
284
285 static _EGLSurface *
286 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
287 _EGLConfig *conf, const EGLint *attrib_list)
288 {
289 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
290 XCB_WINDOW_NONE, attrib_list);
291 }
292
293 static EGLBoolean
294 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
295 {
296 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
297 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
298
299 (void) drv;
300
301 if (!_eglPutSurface(surf))
302 return EGL_TRUE;
303
304 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
305
306 if (dri2_dpy->dri2) {
307 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
308 } else {
309 assert(dri2_dpy->swrast);
310 swrastDestroyDrawable(dri2_dpy, dri2_surf);
311 }
312
313 if (surf->Type == EGL_PBUFFER_BIT)
314 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
315
316 free(surf);
317
318 return EGL_TRUE;
319 }
320
321 /**
322 * Process list of buffer received from the server
323 *
324 * Processes the list of buffers received in a reply from the server to either
325 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
326 */
327 static void
328 dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
329 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
330 {
331 struct dri2_egl_display *dri2_dpy =
332 dri2_egl_display(dri2_surf->base.Resource.Display);
333 xcb_rectangle_t rectangle;
334 unsigned i;
335
336 dri2_surf->buffer_count = count;
337 dri2_surf->have_fake_front = 0;
338
339 /* This assumes the DRI2 buffer attachment tokens matches the
340 * __DRIbuffer tokens. */
341 for (i = 0; i < count; i++) {
342 dri2_surf->buffers[i].attachment = buffers[i].attachment;
343 dri2_surf->buffers[i].name = buffers[i].name;
344 dri2_surf->buffers[i].pitch = buffers[i].pitch;
345 dri2_surf->buffers[i].cpp = buffers[i].cpp;
346 dri2_surf->buffers[i].flags = buffers[i].flags;
347
348 /* We only use the DRI drivers single buffer configs. This
349 * means that if we try to render to a window, DRI2 will give us
350 * the fake front buffer, which we'll use as a back buffer.
351 * Note that EGL doesn't require that several clients rendering
352 * to the same window must see the same aux buffers. */
353 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
354 dri2_surf->have_fake_front = 1;
355 }
356
357 if (dri2_surf->region != XCB_NONE)
358 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
359
360 rectangle.x = 0;
361 rectangle.y = 0;
362 rectangle.width = dri2_surf->base.Width;
363 rectangle.height = dri2_surf->base.Height;
364 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
365 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
366 }
367
368 static __DRIbuffer *
369 dri2_get_buffers(__DRIdrawable * driDrawable,
370 int *width, int *height,
371 unsigned int *attachments, int count,
372 int *out_count, void *loaderPrivate)
373 {
374 struct dri2_egl_surface *dri2_surf = loaderPrivate;
375 struct dri2_egl_display *dri2_dpy =
376 dri2_egl_display(dri2_surf->base.Resource.Display);
377 xcb_dri2_dri2_buffer_t *buffers;
378 xcb_dri2_get_buffers_reply_t *reply;
379 xcb_dri2_get_buffers_cookie_t cookie;
380
381 (void) driDrawable;
382
383 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
384 dri2_surf->drawable,
385 count, count, attachments);
386 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
387 buffers = xcb_dri2_get_buffers_buffers (reply);
388 if (buffers == NULL)
389 return NULL;
390
391 *out_count = reply->count;
392 dri2_surf->base.Width = *width = reply->width;
393 dri2_surf->base.Height = *height = reply->height;
394 dri2_process_buffers(dri2_surf, buffers, *out_count);
395
396 free(reply);
397
398 return dri2_surf->buffers;
399 }
400
401 static __DRIbuffer *
402 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
403 int *width, int *height,
404 unsigned int *attachments, int count,
405 int *out_count, void *loaderPrivate)
406 {
407 struct dri2_egl_surface *dri2_surf = loaderPrivate;
408 struct dri2_egl_display *dri2_dpy =
409 dri2_egl_display(dri2_surf->base.Resource.Display);
410 xcb_dri2_dri2_buffer_t *buffers;
411 xcb_dri2_get_buffers_with_format_reply_t *reply;
412 xcb_dri2_get_buffers_with_format_cookie_t cookie;
413 xcb_dri2_attach_format_t *format_attachments;
414
415 (void) driDrawable;
416
417 format_attachments = (xcb_dri2_attach_format_t *) attachments;
418 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
419 dri2_surf->drawable,
420 count, count,
421 format_attachments);
422
423 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
424 cookie, NULL);
425 if (reply == NULL)
426 return NULL;
427
428 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
429 dri2_surf->base.Width = *width = reply->width;
430 dri2_surf->base.Height = *height = reply->height;
431 *out_count = reply->count;
432 dri2_process_buffers(dri2_surf, buffers, *out_count);
433
434 free(reply);
435
436 return dri2_surf->buffers;
437 }
438
439 static void
440 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
441 {
442 (void) driDrawable;
443
444 /* FIXME: Does EGL support front buffer rendering at all? */
445
446 #if 0
447 struct dri2_egl_surface *dri2_surf = loaderPrivate;
448
449 dri2WaitGL(dri2_surf);
450 #else
451 (void) loaderPrivate;
452 #endif
453 }
454
455 static char *
456 dri2_strndup(const char *s, int length)
457 {
458 char *d;
459
460 d = malloc(length + 1);
461 if (d == NULL)
462 return NULL;
463
464 memcpy(d, s, length);
465 d[length] = '\0';
466
467 return d;
468 }
469
470 static EGLBoolean
471 dri2_connect(struct dri2_egl_display *dri2_dpy)
472 {
473 xcb_xfixes_query_version_reply_t *xfixes_query;
474 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
475 xcb_dri2_query_version_reply_t *dri2_query;
476 xcb_dri2_query_version_cookie_t dri2_query_cookie;
477 xcb_dri2_connect_reply_t *connect;
478 xcb_dri2_connect_cookie_t connect_cookie;
479 xcb_generic_error_t *error;
480 xcb_screen_iterator_t s;
481
482 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
483 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
484
485 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
486 XCB_XFIXES_MAJOR_VERSION,
487 XCB_XFIXES_MINOR_VERSION);
488
489 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
490 XCB_DRI2_MAJOR_VERSION,
491 XCB_DRI2_MINOR_VERSION);
492
493 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
494 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
495 s.data->root,
496 XCB_DRI2_DRIVER_TYPE_DRI);
497
498 xfixes_query =
499 xcb_xfixes_query_version_reply (dri2_dpy->conn,
500 xfixes_query_cookie, &error);
501 if (xfixes_query == NULL ||
502 error != NULL || xfixes_query->major_version < 2) {
503 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
504 free(error);
505 return EGL_FALSE;
506 }
507 free(xfixes_query);
508
509 dri2_query =
510 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
511 if (dri2_query == NULL || error != NULL) {
512 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
513 free(error);
514 return EGL_FALSE;
515 }
516 dri2_dpy->dri2_major = dri2_query->major_version;
517 dri2_dpy->dri2_minor = dri2_query->minor_version;
518 free(dri2_query);
519
520 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
521 if (connect == NULL ||
522 connect->driver_name_length + connect->device_name_length == 0) {
523 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
524 return EGL_FALSE;
525 }
526
527 dri2_dpy->device_name =
528 dri2_strndup(xcb_dri2_connect_device_name (connect),
529 xcb_dri2_connect_device_name_length (connect));
530
531 dri2_dpy->driver_name =
532 dri2_strndup(xcb_dri2_connect_driver_name (connect),
533 xcb_dri2_connect_driver_name_length (connect));
534
535 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
536 free(dri2_dpy->device_name);
537 free(dri2_dpy->driver_name);
538 free(connect);
539 return EGL_FALSE;
540 }
541 free(connect);
542
543 return EGL_TRUE;
544 }
545
546 static int
547 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
548 {
549 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
550 xcb_dri2_authenticate_reply_t *authenticate;
551 xcb_dri2_authenticate_cookie_t authenticate_cookie;
552 xcb_screen_iterator_t s;
553 int ret = 0;
554
555 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
556 authenticate_cookie =
557 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, id);
558 authenticate =
559 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
560
561 if (authenticate == NULL || !authenticate->authenticated)
562 ret = -1;
563
564 if (authenticate)
565 free(authenticate);
566
567 return ret;
568 }
569
570 static EGLBoolean
571 dri2_authenticate(_EGLDisplay *disp)
572 {
573 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
574 drm_magic_t magic;
575
576 if (drmGetMagic(dri2_dpy->fd, &magic)) {
577 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
578 return EGL_FALSE;
579 }
580
581 if (dri2_x11_authenticate(disp, magic) < 0) {
582 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
583 return EGL_FALSE;
584 }
585
586 return EGL_TRUE;
587 }
588
589 static EGLBoolean
590 dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
591 _EGLDisplay *disp)
592 {
593 xcb_screen_iterator_t s;
594 xcb_depth_iterator_t d;
595 xcb_visualtype_t *visuals;
596 int i, j, id;
597 EGLint surface_type;
598 EGLint config_attrs[] = {
599 EGL_NATIVE_VISUAL_ID, 0,
600 EGL_NATIVE_VISUAL_TYPE, 0,
601 EGL_NONE
602 };
603
604 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
605 d = xcb_screen_allowed_depths_iterator(s.data);
606 id = 1;
607
608 surface_type =
609 EGL_WINDOW_BIT |
610 EGL_PIXMAP_BIT |
611 EGL_PBUFFER_BIT |
612 EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
613
614 while (d.rem > 0) {
615 EGLBoolean class_added[6] = { 0, };
616
617 visuals = xcb_depth_visuals(d.data);
618 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
619 if (class_added[visuals[i]._class])
620 continue;
621
622 class_added[visuals[i]._class] = EGL_TRUE;
623 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
624 config_attrs[1] = visuals[i].visual_id;
625 config_attrs[3] = visuals[i]._class;
626
627 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
628 d.data->depth, surface_type, config_attrs);
629 }
630 }
631
632 xcb_depth_next(&d);
633 }
634
635 if (!_eglGetArraySize(disp->Configs)) {
636 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
637 return EGL_FALSE;
638 }
639
640 return EGL_TRUE;
641 }
642
643 static EGLBoolean
644 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
645 _EGLSurface *draw, xcb_xfixes_region_t region)
646 {
647 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
648 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
649 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
650 _EGLContext *ctx;
651 enum xcb_dri2_attachment_t render_attachment;
652 xcb_dri2_copy_region_cookie_t cookie;
653
654 if (dri2_drv->glFlush) {
655 ctx = _eglGetCurrentContext();
656 if (ctx && ctx->DrawSurface == &dri2_surf->base)
657 dri2_drv->glFlush();
658 }
659
660 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
661
662 #if 0
663 /* FIXME: Add support for dri swapbuffers, that'll give us swap
664 * interval and page flipping (at least for fullscreen windows) as
665 * well as the page flip event. Unless surface->SwapBehavior is
666 * EGL_BUFFER_PRESERVED. */
667 #if __DRI2_FLUSH_VERSION >= 2
668 if (pdraw->psc->f)
669 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
670 #endif
671 #endif
672
673 if (dri2_surf->have_fake_front)
674 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
675 else
676 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
677
678 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
679 dri2_surf->drawable,
680 region,
681 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
682 render_attachment);
683 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
684
685 return EGL_TRUE;
686 }
687
688 static EGLBoolean
689 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
690 {
691 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
692 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
693
694 if (dri2_dpy->dri2) {
695 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
696 } else {
697 assert(dri2_dpy->swrast);
698
699 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
700 return EGL_TRUE;
701 }
702 }
703
704 static EGLBoolean
705 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
706 EGLint numRects, const EGLint *rects)
707 {
708 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
709 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
710 EGLBoolean ret;
711 xcb_xfixes_region_t region;
712 xcb_rectangle_t rectangles[16];
713 int i;
714
715 if (numRects > (int)ARRAY_SIZE(rectangles))
716 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
717
718 /* FIXME: Invert y here? */
719 for (i = 0; i < numRects; i++) {
720 rectangles[i].x = rects[i * 4];
721 rectangles[i].y = rects[i * 4 + 1];
722 rectangles[i].width = rects[i * 4 + 2];
723 rectangles[i].height = rects[i * 4 + 3];
724 }
725
726 region = xcb_generate_id(dri2_dpy->conn);
727 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
728 ret = dri2_copy_region(drv, disp, draw, region);
729 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
730
731 return ret;
732 }
733
734 static EGLBoolean
735 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
736 EGLNativePixmapType target)
737 {
738 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
739 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
740 xcb_gcontext_t gc;
741
742 (void) drv;
743
744 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
745
746 gc = xcb_generate_id(dri2_dpy->conn);
747 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
748 xcb_copy_area(dri2_dpy->conn,
749 dri2_surf->drawable,
750 target,
751 gc,
752 0, 0,
753 0, 0,
754 dri2_surf->base.Width,
755 dri2_surf->base.Height);
756 xcb_free_gc(dri2_dpy->conn, gc);
757
758 return EGL_TRUE;
759 }
760
761 static _EGLImage *
762 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
763 EGLClientBuffer buffer, const EGLint *attr_list)
764 {
765 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
766 struct dri2_egl_image *dri2_img;
767 unsigned int attachments[1];
768 xcb_drawable_t drawable;
769 xcb_dri2_get_buffers_cookie_t buffers_cookie;
770 xcb_dri2_get_buffers_reply_t *buffers_reply;
771 xcb_dri2_dri2_buffer_t *buffers;
772 xcb_get_geometry_cookie_t geometry_cookie;
773 xcb_get_geometry_reply_t *geometry_reply;
774 xcb_generic_error_t *error;
775 int stride, format;
776
777 (void) ctx;
778
779 drawable = (xcb_drawable_t) buffer;
780 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
781 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
782 buffers_cookie =
783 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
784 drawable, 1, 1, attachments);
785 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
786 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
787 buffers_cookie, NULL);
788 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
789 if (buffers == NULL) {
790 return NULL;
791 }
792
793 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
794 geometry_cookie, &error);
795 if (geometry_reply == NULL || error != NULL) {
796 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
797 free(error);
798 free(buffers_reply);
799 return NULL;
800 }
801
802 switch (geometry_reply->depth) {
803 case 16:
804 format = __DRI_IMAGE_FORMAT_RGB565;
805 break;
806 case 24:
807 format = __DRI_IMAGE_FORMAT_XRGB8888;
808 break;
809 case 32:
810 format = __DRI_IMAGE_FORMAT_ARGB8888;
811 break;
812 default:
813 _eglError(EGL_BAD_PARAMETER,
814 "dri2_create_image_khr: unsupported pixmap depth");
815 free(buffers_reply);
816 free(geometry_reply);
817 return NULL;
818 }
819
820 dri2_img = malloc(sizeof *dri2_img);
821 if (!dri2_img) {
822 free(buffers_reply);
823 free(geometry_reply);
824 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
825 return EGL_NO_IMAGE_KHR;
826 }
827
828 if (!_eglInitImage(&dri2_img->base, disp)) {
829 free(buffers_reply);
830 free(geometry_reply);
831 return EGL_NO_IMAGE_KHR;
832 }
833
834 stride = buffers[0].pitch / buffers[0].cpp;
835 dri2_img->dri_image =
836 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
837 buffers_reply->width,
838 buffers_reply->height,
839 format,
840 buffers[0].name,
841 stride,
842 dri2_img);
843
844 free(buffers_reply);
845 free(geometry_reply);
846
847 return &dri2_img->base;
848 }
849
850 static _EGLImage *
851 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
852 _EGLContext *ctx, EGLenum target,
853 EGLClientBuffer buffer, const EGLint *attr_list)
854 {
855 (void) drv;
856
857 switch (target) {
858 case EGL_NATIVE_PIXMAP_KHR:
859 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
860 default:
861 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
862 }
863 }
864
865 static EGLBoolean
866 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
867 {
868 struct dri2_egl_display *dri2_dpy;
869
870 drv->API.CreateWindowSurface = dri2_create_window_surface;
871 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
872 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
873 drv->API.DestroySurface = dri2_destroy_surface;
874 drv->API.SwapBuffers = dri2_swap_buffers;
875 drv->API.CopyBuffers = dri2_copy_buffers;
876
877 drv->API.SwapBuffersRegionNOK = NULL;
878 drv->API.CreateImageKHR = NULL;
879 drv->API.DestroyImageKHR = NULL;
880 drv->API.CreateDRMImageMESA = NULL;
881 drv->API.ExportDRMImageMESA = NULL;
882
883 dri2_dpy = malloc(sizeof *dri2_dpy);
884 if (!dri2_dpy)
885 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
886
887 memset(dri2_dpy, 0, sizeof *dri2_dpy);
888
889 disp->DriverData = (void *) dri2_dpy;
890 if (disp->PlatformDisplay == NULL) {
891 dri2_dpy->conn = xcb_connect(0, 0);
892 } else {
893 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
894 }
895
896 if (xcb_connection_has_error(dri2_dpy->conn)) {
897 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
898 goto cleanup_dpy;
899 }
900
901 dri2_dpy->driver_name = dri2_strndup("swrast", strlen("swrast"));
902
903 if (!dri2_load_driver(disp))
904 goto cleanup_conn;
905
906 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
907 dri2_dpy->swrast_loader_extension.base.version = __DRI_SWRAST_LOADER_VERSION;
908 dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
909 dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
910 dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
911
912 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
913 dri2_dpy->extensions[1] = NULL;
914 dri2_dpy->extensions[2] = NULL;
915
916 if (!dri2_create_screen(disp))
917 goto cleanup_driver;
918
919 if (dri2_dpy->conn) {
920 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
921 goto cleanup_configs;
922 }
923
924 /* we're supporting EGL 1.4 */
925 disp->VersionMajor = 1;
926 disp->VersionMinor = 4;
927
928 return EGL_TRUE;
929
930 cleanup_configs:
931 _eglCleanupDisplay(disp);
932 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
933 cleanup_driver:
934 dlclose(dri2_dpy->driver);
935 cleanup_conn:
936 if (disp->PlatformDisplay == NULL)
937 xcb_disconnect(dri2_dpy->conn);
938 cleanup_dpy:
939 free(dri2_dpy);
940
941 return EGL_FALSE;
942 }
943
944
945 static EGLBoolean
946 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
947 {
948 struct dri2_egl_display *dri2_dpy;
949
950 drv->API.CreateWindowSurface = dri2_create_window_surface;
951 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
952 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
953 drv->API.DestroySurface = dri2_destroy_surface;
954 drv->API.SwapBuffers = dri2_swap_buffers;
955 drv->API.CopyBuffers = dri2_copy_buffers;
956 drv->API.CreateImageKHR = dri2_x11_create_image_khr;
957 drv->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
958
959 dri2_dpy = malloc(sizeof *dri2_dpy);
960 if (!dri2_dpy)
961 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
962
963 memset(dri2_dpy, 0, sizeof *dri2_dpy);
964
965 disp->DriverData = (void *) dri2_dpy;
966 if (disp->PlatformDisplay == NULL) {
967 dri2_dpy->conn = xcb_connect(0, 0);
968 } else {
969 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
970 }
971
972 if (xcb_connection_has_error(dri2_dpy->conn)) {
973 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
974 goto cleanup_dpy;
975 }
976
977 if (dri2_dpy->conn) {
978 if (!dri2_connect(dri2_dpy))
979 goto cleanup_conn;
980 }
981
982 if (!dri2_load_driver(disp))
983 goto cleanup_conn;
984
985 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
986 if (dri2_dpy->fd == -1) {
987 _eglLog(_EGL_WARNING,
988 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
989 strerror(errno));
990 goto cleanup_driver;
991 }
992
993 if (dri2_dpy->conn) {
994 if (!dri2_authenticate(disp))
995 goto cleanup_fd;
996 }
997
998 if (dri2_dpy->dri2_minor >= 1) {
999 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1000 dri2_dpy->dri2_loader_extension.base.version = 3;
1001 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
1002 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
1003 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1004 dri2_get_buffers_with_format;
1005 } else {
1006 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1007 dri2_dpy->dri2_loader_extension.base.version = 2;
1008 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
1009 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
1010 dri2_dpy->dri2_loader_extension.getBuffersWithFormat = NULL;
1011 }
1012
1013 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1014 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1015 dri2_dpy->extensions[2] = NULL;
1016
1017 if (!dri2_create_screen(disp))
1018 goto cleanup_fd;
1019
1020 if (dri2_dpy->conn) {
1021 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
1022 goto cleanup_configs;
1023 }
1024
1025 disp->Extensions.MESA_drm_image = EGL_TRUE;
1026 disp->Extensions.KHR_image_base = EGL_TRUE;
1027 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1028 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
1029 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
1030 disp->Extensions.NOK_swap_region = EGL_TRUE;
1031 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1032
1033 #ifdef HAVE_WAYLAND_PLATFORM
1034 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1035 #endif
1036 dri2_dpy->authenticate = dri2_x11_authenticate;
1037
1038 /* we're supporting EGL 1.4 */
1039 disp->VersionMajor = 1;
1040 disp->VersionMinor = 4;
1041
1042 return EGL_TRUE;
1043
1044 cleanup_configs:
1045 _eglCleanupDisplay(disp);
1046 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1047 cleanup_fd:
1048 close(dri2_dpy->fd);
1049 cleanup_driver:
1050 dlclose(dri2_dpy->driver);
1051 cleanup_conn:
1052 if (disp->PlatformDisplay == NULL)
1053 xcb_disconnect(dri2_dpy->conn);
1054 cleanup_dpy:
1055 free(dri2_dpy);
1056
1057 return EGL_FALSE;
1058 }
1059
1060 EGLBoolean
1061 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1062 {
1063 EGLBoolean initialized = EGL_TRUE;
1064
1065 int x11_dri2_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1066
1067 if (x11_dri2_accel) {
1068 if (!dri2_initialize_x11_dri2(drv, disp)) {
1069 initialized = dri2_initialize_x11_swrast(drv, disp);
1070 }
1071 } else {
1072 initialized = dri2_initialize_x11_swrast(drv, disp);
1073 }
1074
1075 return initialized;
1076 }
1077