egl_dri2: Use double buffering for window surfaces
[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 /**
43 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
44 */
45 static _EGLSurface *
46 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
47 _EGLConfig *conf, EGLNativeWindowType window,
48 const EGLint *attrib_list)
49 {
50 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
51 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
52 struct dri2_egl_surface *dri2_surf;
53 xcb_get_geometry_cookie_t cookie;
54 xcb_get_geometry_reply_t *reply;
55 xcb_screen_iterator_t s;
56 xcb_generic_error_t *error;
57
58 (void) drv;
59
60 dri2_surf = malloc(sizeof *dri2_surf);
61 if (!dri2_surf) {
62 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
63 return NULL;
64 }
65
66 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
67 goto cleanup_surf;
68
69 dri2_surf->region = XCB_NONE;
70 if (type == EGL_PBUFFER_BIT) {
71 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
72 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
73 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
74 dri2_surf->drawable, s.data->root,
75 dri2_surf->base.Width, dri2_surf->base.Height);
76 } else {
77 dri2_surf->drawable = window;
78 }
79
80 dri2_surf->dri_drawable =
81 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
82 type == EGL_WINDOW_BIT ?
83 dri2_conf->dri_double_config :
84 dri2_conf->dri_single_config,
85 dri2_surf);
86 if (dri2_surf->dri_drawable == NULL) {
87 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
88 goto cleanup_pixmap;
89 }
90
91 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
92
93 if (type != EGL_PBUFFER_BIT) {
94 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
95 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
96 if (reply == NULL || error != NULL) {
97 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
98 free(error);
99 goto cleanup_dri_drawable;
100 }
101
102 dri2_surf->base.Width = reply->width;
103 dri2_surf->base.Height = reply->height;
104 free(reply);
105 }
106
107 return &dri2_surf->base;
108
109 cleanup_dri_drawable:
110 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
111 cleanup_pixmap:
112 if (type == EGL_PBUFFER_BIT)
113 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
114 cleanup_surf:
115 free(dri2_surf);
116
117 return NULL;
118 }
119
120 /**
121 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
122 */
123 static _EGLSurface *
124 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
125 _EGLConfig *conf, EGLNativeWindowType window,
126 const EGLint *attrib_list)
127 {
128 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
129 window, attrib_list);
130 }
131
132 static _EGLSurface *
133 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
134 _EGLConfig *conf, EGLNativePixmapType pixmap,
135 const EGLint *attrib_list)
136 {
137 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
138 pixmap, attrib_list);
139 }
140
141 static _EGLSurface *
142 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
143 _EGLConfig *conf, const EGLint *attrib_list)
144 {
145 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
146 XCB_WINDOW_NONE, attrib_list);
147 }
148
149 static EGLBoolean
150 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
151 {
152 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
153 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
154
155 (void) drv;
156
157 if (!_eglPutSurface(surf))
158 return EGL_TRUE;
159
160 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
161
162 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
163
164 if (surf->Type == EGL_PBUFFER_BIT)
165 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
166
167 free(surf);
168
169 return EGL_TRUE;
170 }
171
172 /**
173 * Process list of buffer received from the server
174 *
175 * Processes the list of buffers received in a reply from the server to either
176 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
177 */
178 static void
179 dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
180 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
181 {
182 struct dri2_egl_display *dri2_dpy =
183 dri2_egl_display(dri2_surf->base.Resource.Display);
184 xcb_rectangle_t rectangle;
185 unsigned i;
186
187 dri2_surf->buffer_count = count;
188 dri2_surf->have_fake_front = 0;
189
190 /* This assumes the DRI2 buffer attachment tokens matches the
191 * __DRIbuffer tokens. */
192 for (i = 0; i < count; i++) {
193 dri2_surf->buffers[i].attachment = buffers[i].attachment;
194 dri2_surf->buffers[i].name = buffers[i].name;
195 dri2_surf->buffers[i].pitch = buffers[i].pitch;
196 dri2_surf->buffers[i].cpp = buffers[i].cpp;
197 dri2_surf->buffers[i].flags = buffers[i].flags;
198
199 /* We only use the DRI drivers single buffer configs. This
200 * means that if we try to render to a window, DRI2 will give us
201 * the fake front buffer, which we'll use as a back buffer.
202 * Note that EGL doesn't require that several clients rendering
203 * to the same window must see the same aux buffers. */
204 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
205 dri2_surf->have_fake_front = 1;
206 }
207
208 if (dri2_surf->region != XCB_NONE)
209 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
210
211 rectangle.x = 0;
212 rectangle.y = 0;
213 rectangle.width = dri2_surf->base.Width;
214 rectangle.height = dri2_surf->base.Height;
215 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
216 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
217 }
218
219 static __DRIbuffer *
220 dri2_get_buffers(__DRIdrawable * driDrawable,
221 int *width, int *height,
222 unsigned int *attachments, int count,
223 int *out_count, void *loaderPrivate)
224 {
225 struct dri2_egl_surface *dri2_surf = loaderPrivate;
226 struct dri2_egl_display *dri2_dpy =
227 dri2_egl_display(dri2_surf->base.Resource.Display);
228 xcb_dri2_dri2_buffer_t *buffers;
229 xcb_dri2_get_buffers_reply_t *reply;
230 xcb_dri2_get_buffers_cookie_t cookie;
231
232 (void) driDrawable;
233
234 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
235 dri2_surf->drawable,
236 count, count, attachments);
237 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
238 buffers = xcb_dri2_get_buffers_buffers (reply);
239 if (buffers == NULL)
240 return NULL;
241
242 *out_count = reply->count;
243 dri2_surf->base.Width = *width = reply->width;
244 dri2_surf->base.Height = *height = reply->height;
245 dri2_process_buffers(dri2_surf, buffers, *out_count);
246
247 free(reply);
248
249 return dri2_surf->buffers;
250 }
251
252 static __DRIbuffer *
253 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
254 int *width, int *height,
255 unsigned int *attachments, int count,
256 int *out_count, void *loaderPrivate)
257 {
258 struct dri2_egl_surface *dri2_surf = loaderPrivate;
259 struct dri2_egl_display *dri2_dpy =
260 dri2_egl_display(dri2_surf->base.Resource.Display);
261 xcb_dri2_dri2_buffer_t *buffers;
262 xcb_dri2_get_buffers_with_format_reply_t *reply;
263 xcb_dri2_get_buffers_with_format_cookie_t cookie;
264 xcb_dri2_attach_format_t *format_attachments;
265
266 (void) driDrawable;
267
268 format_attachments = (xcb_dri2_attach_format_t *) attachments;
269 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
270 dri2_surf->drawable,
271 count, count,
272 format_attachments);
273
274 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
275 cookie, NULL);
276 if (reply == NULL)
277 return NULL;
278
279 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
280 dri2_surf->base.Width = *width = reply->width;
281 dri2_surf->base.Height = *height = reply->height;
282 *out_count = reply->count;
283 dri2_process_buffers(dri2_surf, buffers, *out_count);
284
285 free(reply);
286
287 return dri2_surf->buffers;
288 }
289
290 static void
291 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
292 {
293 (void) driDrawable;
294
295 /* FIXME: Does EGL support front buffer rendering at all? */
296
297 #if 0
298 struct dri2_egl_surface *dri2_surf = loaderPrivate;
299
300 dri2WaitGL(dri2_surf);
301 #else
302 (void) loaderPrivate;
303 #endif
304 }
305
306 static char *
307 dri2_strndup(const char *s, int length)
308 {
309 char *d;
310
311 d = malloc(length + 1);
312 if (d == NULL)
313 return NULL;
314
315 memcpy(d, s, length);
316 d[length] = '\0';
317
318 return d;
319 }
320
321 static EGLBoolean
322 dri2_connect(struct dri2_egl_display *dri2_dpy)
323 {
324 xcb_xfixes_query_version_reply_t *xfixes_query;
325 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
326 xcb_dri2_query_version_reply_t *dri2_query;
327 xcb_dri2_query_version_cookie_t dri2_query_cookie;
328 xcb_dri2_connect_reply_t *connect;
329 xcb_dri2_connect_cookie_t connect_cookie;
330 xcb_generic_error_t *error;
331 xcb_screen_iterator_t s;
332
333 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
334 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
335
336 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
337 XCB_XFIXES_MAJOR_VERSION,
338 XCB_XFIXES_MINOR_VERSION);
339
340 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
341 XCB_DRI2_MAJOR_VERSION,
342 XCB_DRI2_MINOR_VERSION);
343
344 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
345 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
346 s.data->root,
347 XCB_DRI2_DRIVER_TYPE_DRI);
348
349 xfixes_query =
350 xcb_xfixes_query_version_reply (dri2_dpy->conn,
351 xfixes_query_cookie, &error);
352 if (xfixes_query == NULL ||
353 error != NULL || xfixes_query->major_version < 2) {
354 _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version");
355 free(error);
356 return EGL_FALSE;
357 }
358 free(xfixes_query);
359
360 dri2_query =
361 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
362 if (dri2_query == NULL || error != NULL) {
363 _eglLog(_EGL_FATAL, "DRI2: failed to query version");
364 free(error);
365 return EGL_FALSE;
366 }
367 dri2_dpy->dri2_major = dri2_query->major_version;
368 dri2_dpy->dri2_minor = dri2_query->minor_version;
369 free(dri2_query);
370
371 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
372 if (connect == NULL ||
373 connect->driver_name_length + connect->device_name_length == 0) {
374 _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
375 return EGL_FALSE;
376 }
377
378 dri2_dpy->device_name =
379 dri2_strndup(xcb_dri2_connect_device_name (connect),
380 xcb_dri2_connect_device_name_length (connect));
381
382 dri2_dpy->driver_name =
383 dri2_strndup(xcb_dri2_connect_driver_name (connect),
384 xcb_dri2_connect_driver_name_length (connect));
385
386 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
387 free(dri2_dpy->device_name);
388 free(dri2_dpy->driver_name);
389 free(connect);
390 return EGL_FALSE;
391 }
392 free(connect);
393
394 return EGL_TRUE;
395 }
396
397 static EGLBoolean
398 dri2_authenticate(struct dri2_egl_display *dri2_dpy)
399 {
400 xcb_dri2_authenticate_reply_t *authenticate;
401 xcb_dri2_authenticate_cookie_t authenticate_cookie;
402 xcb_screen_iterator_t s;
403 drm_magic_t magic;
404
405 if (drmGetMagic(dri2_dpy->fd, &magic)) {
406 _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic");
407 return EGL_FALSE;
408 }
409
410 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
411 authenticate_cookie =
412 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, magic);
413 authenticate =
414 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
415 if (authenticate == NULL || !authenticate->authenticated) {
416 _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
417 free(authenticate);
418 return EGL_FALSE;
419 }
420
421 free(authenticate);
422
423 return EGL_TRUE;
424 }
425
426 static EGLBoolean
427 dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
428 _EGLDisplay *disp)
429 {
430 xcb_screen_iterator_t s;
431 xcb_depth_iterator_t d;
432 xcb_visualtype_t *visuals;
433 int i, j, id;
434 EGLint surface_type;
435 EGLint config_attrs[] = {
436 EGL_NATIVE_VISUAL_ID, 0,
437 EGL_NATIVE_VISUAL_TYPE, 0,
438 EGL_NONE
439 };
440
441 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
442 d = xcb_screen_allowed_depths_iterator(s.data);
443 id = 1;
444
445 surface_type =
446 EGL_WINDOW_BIT |
447 EGL_PIXMAP_BIT |
448 EGL_PBUFFER_BIT |
449 EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
450
451 while (d.rem > 0) {
452 EGLBoolean class_added[6] = { 0, };
453
454 visuals = xcb_depth_visuals(d.data);
455 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
456 if (class_added[visuals[i]._class])
457 continue;
458
459 class_added[visuals[i]._class] = EGL_TRUE;
460 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
461 config_attrs[1] = visuals[i].visual_id;
462 config_attrs[3] = visuals[i]._class;
463
464 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
465 d.data->depth, surface_type, config_attrs);
466 }
467 }
468
469 xcb_depth_next(&d);
470 }
471
472 if (!_eglGetArraySize(disp->Configs)) {
473 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
474 return EGL_FALSE;
475 }
476
477 return EGL_TRUE;
478 }
479
480 static EGLBoolean
481 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
482 _EGLSurface *draw, xcb_xfixes_region_t region)
483 {
484 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
485 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
486 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
487 _EGLContext *ctx;
488 enum xcb_dri2_attachment_t render_attachment;
489 xcb_dri2_copy_region_cookie_t cookie;
490
491 if (dri2_drv->glFlush) {
492 ctx = _eglGetCurrentContext();
493 if (ctx && ctx->DrawSurface == &dri2_surf->base)
494 dri2_drv->glFlush();
495 }
496
497 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
498
499 #if 0
500 /* FIXME: Add support for dri swapbuffers, that'll give us swap
501 * interval and page flipping (at least for fullscreen windows) as
502 * well as the page flip event. Unless surface->SwapBehavior is
503 * EGL_BUFFER_PRESERVED. */
504 #if __DRI2_FLUSH_VERSION >= 2
505 if (pdraw->psc->f)
506 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
507 #endif
508 #endif
509
510 if (dri2_surf->have_fake_front)
511 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
512 else
513 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
514
515 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
516 dri2_surf->drawable,
517 region,
518 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
519 render_attachment);
520 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
521
522 return EGL_TRUE;
523 }
524
525 static EGLBoolean
526 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
527 {
528 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
529
530 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
531 }
532
533 static EGLBoolean
534 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
535 EGLint numRects, const EGLint *rects)
536 {
537 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
538 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
539 EGLBoolean ret;
540 xcb_xfixes_region_t region;
541 xcb_rectangle_t rectangles[16];
542 int i;
543
544 if (numRects > (int)ARRAY_SIZE(rectangles))
545 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
546
547 /* FIXME: Invert y here? */
548 for (i = 0; i < numRects; i++) {
549 rectangles[i].x = rects[i * 4];
550 rectangles[i].y = rects[i * 4 + 1];
551 rectangles[i].width = rects[i * 4 + 2];
552 rectangles[i].height = rects[i * 4 + 3];
553 }
554
555 region = xcb_generate_id(dri2_dpy->conn);
556 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
557 ret = dri2_copy_region(drv, disp, draw, region);
558 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
559
560 return ret;
561 }
562
563 static EGLBoolean
564 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
565 EGLNativePixmapType target)
566 {
567 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
568 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
569 xcb_gcontext_t gc;
570
571 (void) drv;
572
573 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
574
575 gc = xcb_generate_id(dri2_dpy->conn);
576 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
577 xcb_copy_area(dri2_dpy->conn,
578 dri2_surf->drawable,
579 target,
580 gc,
581 0, 0,
582 0, 0,
583 dri2_surf->base.Width,
584 dri2_surf->base.Height);
585 xcb_free_gc(dri2_dpy->conn, gc);
586
587 return EGL_TRUE;
588 }
589
590 static _EGLImage *
591 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
592 EGLClientBuffer buffer, const EGLint *attr_list)
593 {
594 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
595 struct dri2_egl_image *dri2_img;
596 unsigned int attachments[1];
597 xcb_drawable_t drawable;
598 xcb_dri2_get_buffers_cookie_t buffers_cookie;
599 xcb_dri2_get_buffers_reply_t *buffers_reply;
600 xcb_dri2_dri2_buffer_t *buffers;
601 xcb_get_geometry_cookie_t geometry_cookie;
602 xcb_get_geometry_reply_t *geometry_reply;
603 xcb_generic_error_t *error;
604 int stride, format;
605
606 (void) ctx;
607
608 drawable = (xcb_drawable_t) buffer;
609 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
610 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
611 buffers_cookie =
612 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
613 drawable, 1, 1, attachments);
614 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
615 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
616 buffers_cookie, NULL);
617 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
618 if (buffers == NULL) {
619 return NULL;
620 }
621
622 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
623 geometry_cookie, &error);
624 if (geometry_reply == NULL || error != NULL) {
625 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
626 free(error);
627 free(buffers_reply);
628 }
629
630 switch (geometry_reply->depth) {
631 case 16:
632 format = __DRI_IMAGE_FORMAT_RGB565;
633 break;
634 case 24:
635 format = __DRI_IMAGE_FORMAT_XRGB8888;
636 break;
637 case 32:
638 format = __DRI_IMAGE_FORMAT_ARGB8888;
639 break;
640 default:
641 _eglError(EGL_BAD_PARAMETER,
642 "dri2_create_image_khr: unsupported pixmap depth");
643 free(buffers_reply);
644 free(geometry_reply);
645 return NULL;
646 }
647
648 dri2_img = malloc(sizeof *dri2_img);
649 if (!dri2_img) {
650 free(buffers_reply);
651 free(geometry_reply);
652 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
653 return EGL_NO_IMAGE_KHR;
654 }
655
656 if (!_eglInitImage(&dri2_img->base, disp)) {
657 free(buffers_reply);
658 free(geometry_reply);
659 return EGL_NO_IMAGE_KHR;
660 }
661
662 stride = buffers[0].pitch / buffers[0].cpp;
663 dri2_img->dri_image =
664 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
665 buffers_reply->width,
666 buffers_reply->height,
667 format,
668 buffers[0].name,
669 stride,
670 dri2_img);
671
672 free(buffers_reply);
673 free(geometry_reply);
674
675 return &dri2_img->base;
676 }
677
678 static _EGLImage *
679 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
680 _EGLContext *ctx, EGLenum target,
681 EGLClientBuffer buffer, const EGLint *attr_list)
682 {
683 (void) drv;
684
685 switch (target) {
686 case EGL_NATIVE_PIXMAP_KHR:
687 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
688 default:
689 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
690 }
691 }
692
693 EGLBoolean
694 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
695 {
696 struct dri2_egl_display *dri2_dpy;
697
698 drv->API.CreateWindowSurface = dri2_create_window_surface;
699 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
700 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
701 drv->API.DestroySurface = dri2_destroy_surface;
702 drv->API.SwapBuffers = dri2_swap_buffers;
703 drv->API.CopyBuffers = dri2_copy_buffers;
704 drv->API.CreateImageKHR = dri2_x11_create_image_khr;
705 drv->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
706
707 dri2_dpy = malloc(sizeof *dri2_dpy);
708 if (!dri2_dpy)
709 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
710
711 disp->DriverData = (void *) dri2_dpy;
712 if (disp->PlatformDisplay == NULL) {
713 dri2_dpy->conn = xcb_connect(0, 0);
714 } else {
715 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
716 }
717
718 if (xcb_connection_has_error(dri2_dpy->conn)) {
719 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
720 goto cleanup_dpy;
721 }
722
723 if (dri2_dpy->conn) {
724 if (!dri2_connect(dri2_dpy))
725 goto cleanup_conn;
726 }
727
728 if (!dri2_load_driver(disp))
729 goto cleanup_conn;
730
731 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
732 if (dri2_dpy->fd == -1) {
733 _eglLog(_EGL_WARNING,
734 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
735 strerror(errno));
736 goto cleanup_driver;
737 }
738
739 if (dri2_dpy->conn) {
740 if (!dri2_authenticate(dri2_dpy))
741 goto cleanup_fd;
742 }
743
744 if (dri2_dpy->dri2_minor >= 1) {
745 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
746 dri2_dpy->loader_extension.base.version = 3;
747 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
748 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
749 dri2_dpy->loader_extension.getBuffersWithFormat =
750 dri2_get_buffers_with_format;
751 } else {
752 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
753 dri2_dpy->loader_extension.base.version = 2;
754 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
755 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
756 dri2_dpy->loader_extension.getBuffersWithFormat = NULL;
757 }
758
759 dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base;
760 dri2_dpy->extensions[1] = &image_lookup_extension.base;
761 dri2_dpy->extensions[2] = NULL;
762
763 if (!dri2_create_screen(disp))
764 goto cleanup_fd;
765
766 if (dri2_dpy->conn) {
767 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
768 goto cleanup_configs;
769 }
770
771 disp->Extensions.MESA_drm_image = EGL_TRUE;
772 disp->Extensions.KHR_image_base = EGL_TRUE;
773 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
774 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
775 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
776 disp->Extensions.NOK_swap_region = EGL_TRUE;
777 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
778
779 /* we're supporting EGL 1.4 */
780 disp->VersionMajor = 1;
781 disp->VersionMinor = 4;
782
783 return EGL_TRUE;
784
785 cleanup_configs:
786 _eglCleanupDisplay(disp);
787 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
788 cleanup_fd:
789 close(dri2_dpy->fd);
790 cleanup_driver:
791 dlclose(dri2_dpy->driver);
792 cleanup_conn:
793 if (disp->PlatformDisplay == NULL)
794 xcb_disconnect(dri2_dpy->conn);
795 cleanup_dpy:
796 free(dri2_dpy);
797
798 return EGL_FALSE;
799 }