egl: dri2 driver error output
[mesa.git] / src / egl / drivers / dri2 / egl_dri2.c
1 /*
2 * Copyright © 2010 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 <GL/gl.h>
38 #include <GL/internal/dri_interface.h>
39 #include <xcb/xcb.h>
40 #include <xcb/dri2.h>
41 #include <xcb/xfixes.h>
42 #include <X11/Xlib-xcb.h>
43
44 #include <glapi/glapi.h>
45 #include "eglconfig.h"
46 #include "eglcontext.h"
47 #include "egldisplay.h"
48 #include "egldriver.h"
49 #include "eglcurrent.h"
50 #include "egllog.h"
51 #include "eglsurface.h"
52 #include "eglimage.h"
53
54 struct dri2_egl_driver
55 {
56 _EGLDriver base;
57 };
58
59 struct dri2_egl_display
60 {
61 xcb_connection_t *conn;
62 int dri2_major;
63 int dri2_minor;
64 __DRIscreen *dri_screen;
65 const __DRIconfig **driver_configs;
66 void *driver;
67 __DRIcoreExtension *core;
68 __DRIdri2Extension *dri2;
69 __DRI2flushExtension *flush;
70 __DRItexBufferExtension *tex_buffer;
71 __DRIimageExtension *image;
72 int fd;
73
74 char *device_name;
75 char *driver_name;
76
77 __DRIdri2LoaderExtension loader_extension;
78 __DRIimageLookupExtension image_lookup_extension;
79 const __DRIextension *extensions[3];
80 };
81
82 struct dri2_egl_context
83 {
84 _EGLContext base;
85 __DRIcontext *dri_context;
86 };
87
88 struct dri2_egl_surface
89 {
90 _EGLSurface base;
91 __DRIdrawable *dri_drawable;
92 xcb_drawable_t drawable;
93 __DRIbuffer buffers[5];
94 int buffer_count;
95 xcb_xfixes_region_t region;
96 int have_fake_front;
97 int swap_interval;
98 };
99
100 struct dri2_egl_config
101 {
102 _EGLConfig base;
103 const __DRIconfig *dri_config;
104 };
105
106 struct dri2_egl_image
107 {
108 _EGLImage base;
109 __DRIimage *dri_image;
110 };
111
112 /* standard typecasts */
113 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)
114 _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)
115
116 EGLint dri2_to_egl_attribute_map[] = {
117 0,
118 EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */
119 EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */
120 EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */
121 EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */
122 EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */
123 EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */
124 EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */
125 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
126 EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */
127 EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */
128 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
129 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
130 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
131 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
132 EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
133 EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */
134 0, /* __DRI_ATTRIB_RENDER_TYPE, */
135 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
136 0, /* __DRI_ATTRIB_CONFORMANT */
137 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
138 0, /* __DRI_ATTRIB_STEREO */
139 0, /* __DRI_ATTRIB_AUX_BUFFERS */
140 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
141 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
142 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
143 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
144 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
145 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
146 0, /* __DRI_ATTRIB_FLOAT_MODE */
147 0, /* __DRI_ATTRIB_RED_MASK */
148 0, /* __DRI_ATTRIB_GREEN_MASK */
149 0, /* __DRI_ATTRIB_BLUE_MASK */
150 0, /* __DRI_ATTRIB_ALPHA_MASK */
151 EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
152 EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
153 EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
154 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
155 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
156 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
157 0, /* __DRI_ATTRIB_SWAP_METHOD */
158 EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
159 EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
160 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
161 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
162 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
163 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
164 0, /* __DRI_ATTRIB_YINVERTED */
165 };
166
167 static void
168 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
169 int depth, xcb_visualtype_t *visual)
170 {
171 struct dri2_egl_config *conf;
172 struct dri2_egl_display *dri2_dpy;
173 _EGLConfig base;
174 unsigned int attrib, value, double_buffer;
175 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
176 int i;
177
178 dri2_dpy = disp->DriverData;
179 _eglInitConfig(&base, disp, id);
180
181 i = 0;
182 while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) {
183 switch (attrib) {
184 case __DRI_ATTRIB_RENDER_TYPE:
185 if (value & __DRI_ATTRIB_RGBA_BIT)
186 value = EGL_RGB_BUFFER;
187 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
188 value = EGL_LUMINANCE_BUFFER;
189 else
190 /* not valid */;
191 _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value);
192 break;
193
194 case __DRI_ATTRIB_CONFIG_CAVEAT:
195 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
196 value = EGL_NON_CONFORMANT_CONFIG;
197 else if (value & __DRI_ATTRIB_SLOW_BIT)
198 value = EGL_SLOW_CONFIG;
199 else
200 value = EGL_NONE;
201 _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value);
202 break;
203
204 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
205 bind_to_texture_rgb = value;
206 break;
207
208 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
209 bind_to_texture_rgba = value;
210 break;
211
212 case __DRI_ATTRIB_DOUBLE_BUFFER:
213 double_buffer = value;
214 break;
215
216 default:
217 key = dri2_to_egl_attribute_map[attrib];
218 if (key != 0)
219 _eglSetConfigKey(&base, key, value);
220 break;
221 }
222 }
223
224 /* In EGL, double buffer or not isn't a config attribute. Pixmaps
225 * surfaces are always single buffered, pbuffer surfaces are always
226 * back buffers and windows can be either, selected by passing an
227 * attribute at window surface construction time. To support this
228 * we ignore all double buffer configs and manipulate the buffer we
229 * return in the getBuffer callback to get the behaviour we want. */
230
231 if (double_buffer)
232 return;
233
234 if (visual != NULL) {
235 if (depth != _eglGetConfigKey(&base, EGL_BUFFER_SIZE))
236 return;
237
238 _eglSetConfigKey(&base, EGL_SURFACE_TYPE,
239 EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT |
240 EGL_SWAP_BEHAVIOR_PRESERVED_BIT);
241
242 _eglSetConfigKey(&base, EGL_NATIVE_VISUAL_ID, visual->visual_id);
243 _eglSetConfigKey(&base, EGL_NATIVE_VISUAL_TYPE, visual->_class);
244 } else {
245 _eglSetConfigKey(&base, EGL_SURFACE_TYPE,
246 EGL_PIXMAP_BIT | EGL_PBUFFER_BIT);
247 }
248
249 _eglSetConfigKey(&base, EGL_NATIVE_RENDERABLE, EGL_TRUE);
250 _eglSetConfigKey(&base, EGL_BIND_TO_TEXTURE_RGB, bind_to_texture_rgb);
251 if (_eglGetConfigKey(&base, EGL_ALPHA_SIZE) > 0)
252 _eglSetConfigKey(&base,
253 EGL_BIND_TO_TEXTURE_RGBA, bind_to_texture_rgba);
254
255 /* EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT, EGL_OPENGL_ES2_BIT */
256 _eglSetConfigKey(&base, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT);
257 _eglSetConfigKey(&base, EGL_CONFORMANT, EGL_OPENGL_BIT);
258
259 if (!_eglValidateConfig(&base, EGL_FALSE)) {
260 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
261 return;
262 }
263
264 conf = malloc(sizeof *conf);
265 if (conf != NULL) {
266 memcpy(&conf->base, &base, sizeof base);
267 conf->dri_config = dri_config;
268 _eglAddConfig(disp, &conf->base);
269 }
270 }
271
272 /**
273 * Process list of buffer received from the server
274 *
275 * Processes the list of buffers received in a reply from the server to either
276 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
277 */
278 static void
279 dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
280 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
281 {
282 struct dri2_egl_display *dri2_dpy =
283 dri2_egl_display(dri2_surf->base.Resource.Display);
284 xcb_rectangle_t rectangle;
285 int i;
286
287 dri2_surf->buffer_count = count;
288 dri2_surf->have_fake_front = 0;
289
290 /* This assumes the DRI2 buffer attachment tokens matches the
291 * __DRIbuffer tokens. */
292 for (i = 0; i < count; i++) {
293 dri2_surf->buffers[i].attachment = buffers[i].attachment;
294 dri2_surf->buffers[i].name = buffers[i].name;
295 dri2_surf->buffers[i].pitch = buffers[i].pitch;
296 dri2_surf->buffers[i].cpp = buffers[i].cpp;
297 dri2_surf->buffers[i].flags = buffers[i].flags;
298
299 /* We only use the DRI drivers single buffer configs. This
300 * means that if we try to render to a window, DRI2 will give us
301 * the fake front buffer, which we'll use as a back buffer.
302 * Note that EGL doesn't require that several clients rendering
303 * to the same window must see the same aux buffers. */
304 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
305 dri2_surf->have_fake_front = 1;
306 }
307
308 if (dri2_surf->region != XCB_NONE)
309 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
310
311 rectangle.x = 0;
312 rectangle.y = 0;
313 rectangle.width = dri2_surf->base.Width;
314 rectangle.height = dri2_surf->base.Height;
315 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
316 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
317 }
318
319 static __DRIbuffer *
320 dri2_get_buffers(__DRIdrawable * driDrawable,
321 int *width, int *height,
322 unsigned int *attachments, int count,
323 int *out_count, void *loaderPrivate)
324 {
325 struct dri2_egl_surface *dri2_surf = loaderPrivate;
326 struct dri2_egl_display *dri2_dpy =
327 dri2_egl_display(dri2_surf->base.Resource.Display);
328 xcb_dri2_dri2_buffer_t *buffers;
329 xcb_dri2_get_buffers_reply_t *reply;
330 xcb_dri2_get_buffers_cookie_t cookie;
331
332 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
333 dri2_surf->drawable,
334 count, count, attachments);
335 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
336 buffers = xcb_dri2_get_buffers_buffers (reply);
337 if (buffers == NULL)
338 return NULL;
339
340 *out_count = reply->count;
341 dri2_surf->base.Width = *width = reply->width;
342 dri2_surf->base.Height = *height = reply->height;
343 dri2_process_buffers(dri2_surf, buffers, *out_count);
344
345 free(reply);
346
347 return dri2_surf->buffers;
348 }
349
350 static void
351 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
352 {
353 /* FIXME: Does EGL support front buffer rendering at all? */
354
355 #if 0
356 struct dri2_egl_surface *dri2_surf = loaderPrivate;
357
358 dri2WaitGL(dri2_surf);
359 #endif
360 }
361
362 static __DRIimage *
363 dri2_lookup_egl_image(__DRIcontext *context, void *image, void *data)
364 {
365 struct dri2_egl_context *dri2_ctx = data;
366 _EGLDisplay *disp = dri2_ctx->base.Resource.Display;
367 struct dri2_egl_image *dri2_img;
368 _EGLImage *img;
369
370 img = _eglLookupImage(image, disp);
371 if (img == NULL) {
372 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
373 return NULL;
374 }
375
376 dri2_img = dri2_egl_image(image);
377
378 return dri2_img->dri_image;
379 }
380
381 static __DRIbuffer *
382 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
383 int *width, int *height,
384 unsigned int *attachments, int count,
385 int *out_count, void *loaderPrivate)
386 {
387 struct dri2_egl_surface *dri2_surf = loaderPrivate;
388 struct dri2_egl_display *dri2_dpy =
389 dri2_egl_display(dri2_surf->base.Resource.Display);
390 xcb_dri2_dri2_buffer_t *buffers;
391 xcb_dri2_get_buffers_with_format_reply_t *reply;
392 xcb_dri2_get_buffers_with_format_cookie_t cookie;
393 xcb_dri2_attach_format_t *format_attachments;
394
395 format_attachments = (xcb_dri2_attach_format_t *) attachments;
396 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
397 dri2_surf->drawable,
398 count, count,
399 format_attachments);
400
401 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
402 cookie, NULL);
403 if (reply == NULL)
404 return NULL;
405
406 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
407 dri2_surf->base.Width = *width = reply->width;
408 dri2_surf->base.Height = *height = reply->height;
409 *out_count = reply->count;
410 dri2_process_buffers(dri2_surf, buffers, *out_count);
411
412 free(reply);
413
414 return dri2_surf->buffers;
415 }
416
417 #ifdef GLX_USE_TLS
418 static const char dri_driver_format[] = "%.*s/tls/%s_dri.so";
419 #else
420 static const char dri_driver_format[] = "%.*s/%s_dri.so";
421 #endif
422
423 static const char dri_driver_path[] = DEFAULT_DRIVER_DIR;
424
425 struct dri2_extension_match {
426 const char *name;
427 int version;
428 int offset;
429 };
430
431 static struct dri2_extension_match dri2_driver_extensions[] = {
432 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
433 { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) },
434 { NULL }
435 };
436
437 static struct dri2_extension_match dri2_core_extensions[] = {
438 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
439 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
440 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
441 { NULL }
442 };
443
444 static EGLBoolean
445 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
446 struct dri2_extension_match *matches,
447 const __DRIextension **extensions)
448 {
449 int i, j, ret = EGL_TRUE;
450 void *field;
451
452 for (i = 0; extensions[i]; i++) {
453 _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name);
454 for (j = 0; matches[j].name; j++) {
455 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
456 extensions[i]->version >= matches[j].version) {
457 field = ((char *) dri2_dpy + matches[j].offset);
458 *(const __DRIextension **) field = extensions[i];
459 _eglLog(_EGL_INFO, "DRI2: found extension %s version %d",
460 extensions[i]->name, extensions[i]->version);
461 }
462 }
463 }
464
465 for (j = 0; matches[j].name; j++) {
466 field = ((char *) dri2_dpy + matches[j].offset);
467 if (*(const __DRIextension **) field == NULL) {
468 _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d",
469 matches[j].name, matches[j].version);
470 ret = EGL_FALSE;
471 }
472 }
473
474 return ret;
475 }
476
477 static char *
478 dri2_strndup(const char *s, int length)
479 {
480 char *d;
481
482 d = malloc(length + 1);
483 if (d == NULL)
484 return NULL;
485
486 memcpy(d, s, length);
487 d[length] = '\0';
488
489 return d;
490 }
491
492 static EGLBoolean
493 dri2_connect(struct dri2_egl_display *dri2_dpy)
494 {
495 xcb_xfixes_query_version_reply_t *xfixes_query;
496 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
497 xcb_dri2_query_version_reply_t *dri2_query;
498 xcb_dri2_query_version_cookie_t dri2_query_cookie;
499 xcb_dri2_connect_reply_t *connect;
500 xcb_dri2_connect_cookie_t connect_cookie;
501 xcb_generic_error_t *error;
502 xcb_screen_iterator_t s;
503
504 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
505 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
506
507 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
508 XCB_XFIXES_MAJOR_VERSION,
509 XCB_XFIXES_MINOR_VERSION);
510
511 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
512 XCB_DRI2_MAJOR_VERSION,
513 XCB_DRI2_MINOR_VERSION);
514
515 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
516 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
517 s.data->root,
518 XCB_DRI2_DRIVER_TYPE_DRI);
519
520 xfixes_query =
521 xcb_xfixes_query_version_reply (dri2_dpy->conn,
522 xfixes_query_cookie, &error);
523 if (xfixes_query == NULL ||
524 error != NULL || xfixes_query->major_version < 2) {
525 _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version");
526 free(error);
527 return EGL_FALSE;
528 }
529 free(xfixes_query);
530
531 dri2_query =
532 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
533 if (dri2_query == NULL || error != NULL) {
534 _eglLog(_EGL_FATAL, "DRI2: failed to query version");
535 free(error);
536 return EGL_FALSE;
537 }
538 dri2_dpy->dri2_major = dri2_query->major_version;
539 dri2_dpy->dri2_minor = dri2_query->minor_version;
540 free(dri2_query);
541
542 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
543 if (connect == NULL ||
544 connect->driver_name_length + connect->device_name_length == 0) {
545 _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
546 return EGL_FALSE;
547 }
548
549 dri2_dpy->device_name =
550 dri2_strndup(xcb_dri2_connect_device_name (connect),
551 xcb_dri2_connect_device_name_length (connect));
552
553 dri2_dpy->driver_name =
554 dri2_strndup(xcb_dri2_connect_driver_name (connect),
555 xcb_dri2_connect_driver_name_length (connect));
556
557 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
558 free(dri2_dpy->device_name);
559 free(dri2_dpy->driver_name);
560 free(connect);
561 return EGL_FALSE;
562 }
563 free(connect);
564
565 return EGL_TRUE;
566 }
567
568 static EGLBoolean
569 dri2_authenticate(struct dri2_egl_display *dri2_dpy)
570 {
571 xcb_dri2_authenticate_reply_t *authenticate;
572 xcb_dri2_authenticate_cookie_t authenticate_cookie;
573 xcb_screen_iterator_t s;
574 drm_magic_t magic;
575
576 if (drmGetMagic(dri2_dpy->fd, &magic)) {
577 _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic");
578 return EGL_FALSE;
579 }
580
581 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
582 authenticate_cookie =
583 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, magic);
584 authenticate =
585 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
586 if (authenticate == NULL || !authenticate->authenticated) {
587 _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
588 free(authenticate);
589 return EGL_FALSE;
590 }
591
592 free(authenticate);
593
594 return EGL_TRUE;
595 }
596
597 static EGLBoolean
598 dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
599 _EGLDisplay *disp)
600 {
601 xcb_screen_iterator_t s;
602 xcb_depth_iterator_t d;
603 xcb_visualtype_t *visuals;
604 int i, j, id;
605
606 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
607 d = xcb_screen_allowed_depths_iterator(s.data);
608 id = 1;
609 while (d.rem > 0) {
610 EGLBoolean class_added[6] = { 0, };
611
612 visuals = xcb_depth_visuals(d.data);
613 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
614 if (class_added[visuals[i]._class])
615 continue;
616
617 class_added[visuals[i]._class] = EGL_TRUE;
618 for (j = 0; dri2_dpy->driver_configs[j]; j++)
619 dri2_add_config(disp, dri2_dpy->driver_configs[j],
620 id++, d.data->depth, &visuals[i]);
621 }
622
623 xcb_depth_next(&d);
624 }
625
626 if (!disp->NumConfigs) {
627 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
628 return EGL_FALSE;
629 }
630
631 return EGL_TRUE;
632 }
633
634 /**
635 * Called via eglInitialize(), GLX_drv->API.Initialize().
636 */
637 static EGLBoolean
638 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp,
639 EGLint *major, EGLint *minor)
640 {
641 const __DRIextension **extensions;
642 struct dri2_egl_display *dri2_dpy;
643 char path[PATH_MAX], *search_paths, *p, *next, *end;
644
645 dri2_dpy = malloc(sizeof *dri2_dpy);
646 if (!dri2_dpy)
647 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
648
649 disp->DriverData = (void *) dri2_dpy;
650 if (disp->NativeDisplay == NULL) {
651 dri2_dpy->conn = xcb_connect(0, 0);
652 if (!dri2_dpy->conn) {
653 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
654 goto cleanup_dpy;
655 }
656 } else {
657 dri2_dpy->conn = XGetXCBConnection(disp->NativeDisplay);
658 }
659
660 if (dri2_dpy->conn == NULL)
661 goto cleanup_conn;
662
663 if (dri2_dpy->conn) {
664 if (!dri2_connect(dri2_dpy))
665 goto cleanup_conn;
666 }
667
668 search_paths = NULL;
669 if (geteuid() == getuid()) {
670 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
671 search_paths = getenv("LIBGL_DRIVERS_PATH");
672 }
673 if (search_paths == NULL)
674 search_paths = DEFAULT_DRIVER_DIR;
675
676 dri2_dpy->driver = NULL;
677 end = search_paths + strlen(search_paths);
678 for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) {
679 next = strchr(p, ':');
680 if (next == NULL)
681 next = end;
682
683 snprintf(path, sizeof path,
684 dri_driver_format, (int) (next - p), p, dri2_dpy->driver_name);
685 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
686 }
687
688 if (dri2_dpy->driver == NULL) {
689 _eglLog(_EGL_FATAL,
690 "DRI2: failed to open any driver (search paths %s)",
691 search_paths);
692 goto cleanup_conn;
693 }
694
695 _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path);
696 extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS);
697 if (extensions == NULL) {
698 _eglLog(_EGL_FATAL,
699 "DRI2: driver exports no extensions (%s)", dlerror());
700 goto cleanup_driver;
701 }
702
703 if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions))
704 goto cleanup_driver;
705
706 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
707 if (dri2_dpy->fd == -1) {
708 _eglLog(_EGL_FATAL,
709 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
710 strerror(errno));
711 goto cleanup_driver;
712 }
713
714 if (dri2_dpy->conn) {
715 if (!dri2_authenticate(dri2_dpy))
716 goto cleanup_fd;
717 }
718
719 if (dri2_dpy->dri2_minor >= 1) {
720 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
721 dri2_dpy->loader_extension.base.version = 3;
722 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
723 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
724 dri2_dpy->loader_extension.getBuffersWithFormat =
725 dri2_get_buffers_with_format;
726 } else {
727 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
728 dri2_dpy->loader_extension.base.version = 2;
729 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
730 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
731 dri2_dpy->loader_extension.getBuffersWithFormat = NULL;
732 }
733
734 dri2_dpy->image_lookup_extension.base.name = __DRI_IMAGE_LOOKUP;
735 dri2_dpy->image_lookup_extension.base.version = 1;
736 dri2_dpy->image_lookup_extension.lookupEGLImage = dri2_lookup_egl_image;
737
738 dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base;
739 dri2_dpy->extensions[1] = &dri2_dpy->image_lookup_extension.base;
740 dri2_dpy->extensions[2] = NULL;
741
742 dri2_dpy->dri_screen =
743 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions,
744 &dri2_dpy->driver_configs, dri2_dpy);
745
746 if (dri2_dpy->dri_screen == NULL) {
747 _eglLog(_EGL_FATAL, "DRI2: failed to create dri screen");
748 goto cleanup_fd;
749 }
750
751 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
752 if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
753 goto cleanup_dri_screen;
754
755 if (dri2_dpy->conn) {
756 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
757 goto cleanup_configs;
758 }
759
760 disp->ClientAPIsMask = EGL_OPENGL_BIT;
761 disp->Extensions.KHR_image_base = EGL_TRUE;
762 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
763 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
764
765 /* we're supporting EGL 1.4 */
766 *major = 1;
767 *minor = 4;
768
769 return EGL_TRUE;
770
771 cleanup_configs:
772 _eglCleanupDisplay(disp);
773 cleanup_dri_screen:
774 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
775 cleanup_fd:
776 close(dri2_dpy->fd);
777 cleanup_driver:
778 dlclose(dri2_dpy->driver);
779 cleanup_conn:
780 if (disp->NativeDisplay == NULL)
781 xcb_disconnect(dri2_dpy->conn);
782 cleanup_dpy:
783 free(dri2_dpy);
784
785 return EGL_FALSE;
786 }
787
788 /**
789 * Called via eglTerminate(), drv->API.Terminate().
790 */
791 static EGLBoolean
792 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
793 {
794 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
795
796 _eglReleaseDisplayResources(drv, disp);
797 _eglCleanupDisplay(disp);
798
799 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
800 close(dri2_dpy->fd);
801 dlclose(dri2_dpy->driver);
802 if (disp->NativeDisplay == NULL)
803 xcb_disconnect(dri2_dpy->conn);
804 free(dri2_dpy);
805 disp->DriverData = NULL;
806
807 return EGL_TRUE;
808 }
809
810
811 /**
812 * Called via eglCreateContext(), drv->API.CreateContext().
813 */
814 static _EGLContext *
815 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
816 _EGLContext *share_list, const EGLint *attrib_list)
817 {
818 struct dri2_egl_context *dri2_ctx;
819 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
820 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
821 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
822
823 dri2_ctx = malloc(sizeof *dri2_ctx);
824 if (!dri2_ctx) {
825 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
826 return NULL;
827 }
828
829 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
830 goto cleanup;
831
832 dri2_ctx->dri_context =
833 dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen,
834 dri2_config->dri_config,
835 dri2_ctx_shared ?
836 dri2_ctx_shared->dri_context : NULL,
837 dri2_ctx);
838
839 if (!dri2_ctx->dri_context)
840 goto cleanup;
841
842 return &dri2_ctx->base;
843
844 cleanup:
845 free(dri2_ctx);
846 return NULL;
847 }
848
849 static EGLBoolean
850 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
851 {
852 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
853 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
854
855 if (_eglIsSurfaceBound(surf))
856 return EGL_TRUE;
857
858 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
859
860 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
861
862 if (surf->Type == EGL_PBUFFER_BIT)
863 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
864
865 free(surf);
866
867 return EGL_TRUE;
868 }
869
870 /**
871 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
872 */
873 static EGLBoolean
874 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
875 _EGLSurface *rsurf, _EGLContext *ctx)
876 {
877 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
878 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
879 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
880 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
881 __DRIdrawable *ddraw, *rdraw;
882 __DRIcontext *cctx;
883
884 /* bind the new context and return the "orphaned" one */
885 if (!_eglBindContext(&ctx, &dsurf, &rsurf))
886 return EGL_FALSE;
887
888 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
889 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
890 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
891
892 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
893 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
894 if (dsurf && !_eglIsSurfaceLinked(dsurf))
895 dri2_destroy_surface(drv, disp, dsurf);
896 if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf))
897 dri2_destroy_surface(drv, disp, rsurf);
898 if (ctx != NULL && !_eglIsContextLinked(ctx))
899 dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context);
900
901 return EGL_TRUE;
902 } else {
903 _eglBindContext(&ctx, &dsurf, &rsurf);
904
905 return EGL_FALSE;
906 }
907 }
908
909 /**
910 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
911 */
912 static _EGLSurface *
913 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
914 _EGLConfig *conf, EGLNativeWindowType window,
915 const EGLint *attrib_list)
916 {
917 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
918 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
919 struct dri2_egl_surface *dri2_surf;
920 xcb_get_geometry_cookie_t cookie;
921 xcb_get_geometry_reply_t *reply;
922 xcb_screen_iterator_t s;
923 xcb_generic_error_t *error;
924
925 dri2_surf = malloc(sizeof *dri2_surf);
926 if (!dri2_surf) {
927 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
928 return NULL;
929 }
930
931 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
932 goto cleanup_surf;
933
934 dri2_surf->region = XCB_NONE;
935 if (type == EGL_PBUFFER_BIT) {
936 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
937 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
938 xcb_create_pixmap(dri2_dpy->conn,
939 _eglGetConfigKey(conf, EGL_BUFFER_SIZE),
940 dri2_surf->drawable, s.data->root,
941 dri2_surf->base.Width, dri2_surf->base.Height);
942 } else {
943 dri2_surf->drawable = window;
944 }
945
946 dri2_surf->dri_drawable =
947 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
948 dri2_conf->dri_config, dri2_surf);
949 if (dri2_surf->dri_drawable == NULL) {
950 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
951 goto cleanup_pixmap;
952 }
953
954 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
955
956 if (type != EGL_PBUFFER_BIT) {
957 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
958 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
959 if (reply == NULL || error != NULL) {
960 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
961 free(error);
962 goto cleanup_dri_drawable;
963 }
964
965 dri2_surf->base.Width = reply->width;
966 dri2_surf->base.Height = reply->height;
967 free(reply);
968 }
969
970 return &dri2_surf->base;
971
972 cleanup_dri_drawable:
973 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
974 cleanup_pixmap:
975 if (type == EGL_PBUFFER_BIT)
976 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
977 cleanup_surf:
978 free(dri2_surf);
979
980 return NULL;
981 }
982
983 /**
984 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
985 */
986 static _EGLSurface *
987 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
988 _EGLConfig *conf, EGLNativeWindowType window,
989 const EGLint *attrib_list)
990 {
991 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
992 window, attrib_list);
993 }
994
995 static _EGLSurface *
996 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
997 _EGLConfig *conf, EGLNativePixmapType pixmap,
998 const EGLint *attrib_list)
999 {
1000 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
1001 pixmap, attrib_list);
1002 }
1003
1004 static _EGLSurface *
1005 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
1006 _EGLConfig *conf, const EGLint *attrib_list)
1007 {
1008 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
1009 XCB_WINDOW_NONE, attrib_list);
1010 }
1011
1012 static EGLBoolean
1013 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1014 {
1015 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1016 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1017 xcb_dri2_copy_region_cookie_t cookie;
1018
1019 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1020
1021 #if 0
1022 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1023 * interval and page flipping (at least for fullscreen windows) as
1024 * well as the page flip event. Unless surface->SwapBehavior is
1025 * EGL_BUFFER_PRESERVED. */
1026 #if __DRI2_FLUSH_VERSION >= 2
1027 if (pdraw->psc->f)
1028 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
1029 #endif
1030 #endif
1031
1032 if (!dri2_surf->have_fake_front)
1033 return EGL_TRUE;
1034
1035 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
1036 dri2_surf->drawable,
1037 dri2_surf->region,
1038 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
1039 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT);
1040 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
1041
1042 return EGL_TRUE;
1043 }
1044
1045 /*
1046 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1047 */
1048 static _EGLProc
1049 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
1050 {
1051 /* FIXME: Do we need to support lookup of EGL symbols too? */
1052
1053 return (_EGLProc) _glapi_get_proc_address(procname);
1054 }
1055
1056 static EGLBoolean
1057 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1058 {
1059 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1060 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
1061
1062 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1063 * we need to copy fake to real here.*/
1064
1065 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1066
1067 return EGL_TRUE;
1068 }
1069
1070 static EGLBoolean
1071 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
1072 {
1073 if (engine != EGL_CORE_NATIVE_ENGINE)
1074 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1075 /* glXWaitX(); */
1076
1077 return EGL_TRUE;
1078 }
1079
1080 static void
1081 dri2_unload(_EGLDriver *drv)
1082 {
1083 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1084 free(dri2_drv);
1085 }
1086
1087 static EGLBoolean
1088 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1089 EGLNativePixmapType target)
1090 {
1091 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1092 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1093 xcb_gcontext_t gc;
1094
1095 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1096
1097 gc = xcb_generate_id(dri2_dpy->conn);
1098 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
1099 xcb_copy_area(dri2_dpy->conn,
1100 dri2_surf->drawable,
1101 target,
1102 gc,
1103 0, 0,
1104 0, 0,
1105 dri2_surf->base.Width,
1106 dri2_surf->base.Height);
1107 xcb_free_gc(dri2_dpy->conn, gc);
1108
1109 return EGL_TRUE;
1110 }
1111
1112 static EGLBoolean
1113 dri2_bind_tex_image(_EGLDriver *drv,
1114 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1115 {
1116 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1117 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1118 struct dri2_egl_context *dri2_ctx;
1119 _EGLContext *ctx;
1120 GLint format, target;
1121
1122 ctx = _eglGetCurrentContext();
1123 dri2_ctx = dri2_egl_context(ctx);
1124
1125 if (buffer != EGL_BACK_BUFFER) {
1126 _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
1127 return EGL_FALSE;
1128 }
1129
1130 /* We allow binding pixmaps too... Not conformat, but we can do it
1131 * for free and it's useful for X compositors. Supposedly there's
1132 * a EGL_NOKIA_texture_from_pixmap extension that allows that, but
1133 * I couldn't find it at this time. */
1134 if ((dri2_surf->base.Type & (EGL_PBUFFER_BIT | EGL_PIXMAP_BIT)) == 0) {
1135 _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
1136 return EGL_FALSE;
1137 }
1138
1139 switch (dri2_surf->base.TextureFormat) {
1140 case EGL_TEXTURE_RGB:
1141 format = __DRI_TEXTURE_FORMAT_RGB;
1142 break;
1143 case EGL_TEXTURE_RGBA:
1144 format = __DRI_TEXTURE_FORMAT_RGBA;
1145 break;
1146 default:
1147 _eglError(EGL_BAD_MATCH, "eglBindTexImage");
1148 return EGL_FALSE;
1149 }
1150
1151 switch (dri2_surf->base.TextureTarget) {
1152 case EGL_TEXTURE_2D:
1153 target = GL_TEXTURE_2D;
1154 break;
1155 default:
1156 _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
1157 return EGL_FALSE;
1158 }
1159
1160 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1161 target, format,
1162 dri2_surf->dri_drawable);
1163
1164 return dri2_surf->base.BoundToTexture = EGL_TRUE;
1165 }
1166
1167 static EGLBoolean
1168 dri2_release_tex_image(_EGLDriver *drv,
1169 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1170 {
1171 return EGL_TRUE;
1172 }
1173
1174 static _EGLImage *
1175 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
1176 EGLClientBuffer buffer, const EGLint *attr_list)
1177 {
1178 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1179 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1180 struct dri2_egl_image *dri2_img;
1181 unsigned int attachments[1];
1182 xcb_drawable_t drawable;
1183 xcb_dri2_get_buffers_cookie_t buffers_cookie;
1184 xcb_dri2_get_buffers_reply_t *buffers_reply;
1185 xcb_dri2_dri2_buffer_t *buffers;
1186 xcb_get_geometry_cookie_t geometry_cookie;
1187 xcb_get_geometry_reply_t *geometry_reply;
1188 xcb_generic_error_t *error;
1189 int stride, format;
1190
1191 drawable = (xcb_drawable_t) buffer;
1192 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1193 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1194 buffers_cookie =
1195 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1196 drawable, 1, 1, attachments);
1197 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1198 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1199 buffers_cookie, NULL);
1200 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1201 if (buffers == NULL) {
1202 return NULL;
1203 }
1204
1205 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1206 geometry_cookie, &error);
1207 if (geometry_reply == NULL || error != NULL) {
1208 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1209 free(error);
1210 free(buffers_reply);
1211 }
1212
1213 switch (geometry_reply->depth) {
1214 case 16:
1215 format = __DRI_IMAGE_FORMAT_RGB565;
1216 break;
1217 case 24:
1218 format = __DRI_IMAGE_FORMAT_XRGB8888;
1219 break;
1220 case 32:
1221 format = __DRI_IMAGE_FORMAT_ARGB8888;
1222 break;
1223 default:
1224 _eglError(EGL_BAD_PARAMETER,
1225 "dri2_create_image_khr: unsupported pixmap depth");
1226 free(buffers_reply);
1227 free(geometry_reply);
1228 return NULL;
1229 }
1230
1231 dri2_img = malloc(sizeof *dri2_img);
1232 if (!dri2_img) {
1233 free(buffers_reply);
1234 free(geometry_reply);
1235 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1236 return EGL_NO_IMAGE_KHR;
1237 }
1238
1239 if (!_eglInitImage(&dri2_img->base, disp, attr_list)) {
1240 free(buffers_reply);
1241 free(geometry_reply);
1242 return EGL_NO_IMAGE_KHR;
1243 }
1244
1245 stride = buffers[0].pitch / buffers[0].cpp;
1246 dri2_img->dri_image =
1247 dri2_dpy->image->createImageFromName(dri2_ctx->dri_context,
1248 buffers_reply->width,
1249 buffers_reply->height,
1250 format,
1251 buffers[0].name,
1252 stride,
1253 dri2_img);
1254
1255 free(buffers_reply);
1256 free(geometry_reply);
1257
1258 return &dri2_img->base;
1259 }
1260
1261 static _EGLImage *
1262 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1263 EGLClientBuffer buffer,
1264 const EGLint *attr_list)
1265 {
1266 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1267 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1268 struct dri2_egl_image *dri2_img;
1269 GLuint renderbuffer = (GLuint) buffer;
1270
1271 if (renderbuffer == 0) {
1272 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1273 return EGL_NO_IMAGE_KHR;
1274 }
1275
1276 dri2_img = malloc(sizeof *dri2_img);
1277 if (!dri2_img) {
1278 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1279 return EGL_NO_IMAGE_KHR;
1280 }
1281
1282 if (!_eglInitImage(&dri2_img->base, disp, attr_list))
1283 return EGL_NO_IMAGE_KHR;
1284
1285 dri2_img->dri_image =
1286 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1287 renderbuffer,
1288 dri2_img);
1289
1290 return &dri2_img->base;
1291 }
1292
1293 static _EGLImage *
1294 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1295 _EGLContext *ctx, EGLenum target,
1296 EGLClientBuffer buffer, const EGLint *attr_list)
1297 {
1298 switch (target) {
1299 case EGL_NATIVE_PIXMAP_KHR:
1300 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1301 case EGL_GL_RENDERBUFFER_KHR:
1302 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
1303 default:
1304 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1305 return EGL_NO_IMAGE_KHR;
1306 }
1307 }
1308
1309 static EGLBoolean
1310 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
1311 {
1312 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1313 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
1314
1315 dri2_dpy->image->destroyImage(dri2_img->dri_image);
1316 free(dri2_img);
1317
1318 return EGL_TRUE;
1319 }
1320
1321 /**
1322 * This is the main entrypoint into the driver, called by libEGL.
1323 * Create a new _EGLDriver object and init its dispatch table.
1324 */
1325 _EGLDriver *
1326 _eglMain(const char *args)
1327 {
1328 struct dri2_egl_driver *dri2_drv;
1329
1330 dri2_drv = malloc(sizeof *dri2_drv);
1331 if (!dri2_drv)
1332 return NULL;
1333
1334 _eglInitDriverFallbacks(&dri2_drv->base);
1335 dri2_drv->base.API.Initialize = dri2_initialize;
1336 dri2_drv->base.API.Terminate = dri2_terminate;
1337 dri2_drv->base.API.CreateContext = dri2_create_context;
1338 dri2_drv->base.API.MakeCurrent = dri2_make_current;
1339 dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
1340 dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
1341 dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
1342 dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
1343 dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
1344 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
1345 dri2_drv->base.API.WaitClient = dri2_wait_client;
1346 dri2_drv->base.API.WaitNative = dri2_wait_native;
1347 dri2_drv->base.API.CopyBuffers = dri2_copy_buffers;
1348 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
1349 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
1350 dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr;
1351 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
1352
1353 dri2_drv->base.Name = "DRI2";
1354 dri2_drv->base.Unload = dri2_unload;
1355
1356 return &dri2_drv->base;
1357 }