Merge commit 'origin/gallium-winsys-handle-rebased'
[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)", path, strerror(errno));
710 goto cleanup_driver;
711 }
712
713 if (dri2_dpy->conn) {
714 if (!dri2_authenticate(dri2_dpy))
715 goto cleanup_fd;
716 }
717
718 if (dri2_dpy->dri2_minor >= 1) {
719 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
720 dri2_dpy->loader_extension.base.version = 3;
721 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
722 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
723 dri2_dpy->loader_extension.getBuffersWithFormat =
724 dri2_get_buffers_with_format;
725 } else {
726 dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
727 dri2_dpy->loader_extension.base.version = 2;
728 dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
729 dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
730 dri2_dpy->loader_extension.getBuffersWithFormat = NULL;
731 }
732
733 dri2_dpy->image_lookup_extension.base.name = __DRI_IMAGE_LOOKUP;
734 dri2_dpy->image_lookup_extension.base.version = 1;
735 dri2_dpy->image_lookup_extension.lookupEGLImage = dri2_lookup_egl_image;
736
737 dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base;
738 dri2_dpy->extensions[1] = &dri2_dpy->image_lookup_extension.base;
739 dri2_dpy->extensions[2] = NULL;
740
741 dri2_dpy->dri_screen =
742 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions,
743 &dri2_dpy->driver_configs, dri2_dpy);
744
745 if (dri2_dpy->dri_screen == NULL) {
746 _eglLog(_EGL_FATAL, "DRI2: failed to create dri screen");
747 goto cleanup_fd;
748 }
749
750 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
751 if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
752 goto cleanup_dri_screen;
753
754 if (dri2_dpy->conn) {
755 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
756 goto cleanup_configs;
757 }
758
759 disp->ClientAPIsMask = EGL_OPENGL_BIT;
760 disp->Extensions.KHR_image_base = EGL_TRUE;
761 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
762 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
763
764 /* we're supporting EGL 1.4 */
765 *major = 1;
766 *minor = 4;
767
768 return EGL_TRUE;
769
770 cleanup_configs:
771 _eglCleanupDisplay(disp);
772 cleanup_dri_screen:
773 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
774 cleanup_fd:
775 close(dri2_dpy->fd);
776 cleanup_driver:
777 dlclose(dri2_dpy->driver);
778 cleanup_conn:
779 if (disp->NativeDisplay == NULL)
780 xcb_disconnect(dri2_dpy->conn);
781 cleanup_dpy:
782 free(dri2_dpy);
783
784 return EGL_FALSE;
785 }
786
787 /**
788 * Called via eglTerminate(), drv->API.Terminate().
789 */
790 static EGLBoolean
791 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
792 {
793 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
794
795 _eglReleaseDisplayResources(drv, disp);
796 _eglCleanupDisplay(disp);
797
798 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
799 close(dri2_dpy->fd);
800 dlclose(dri2_dpy->driver);
801 if (disp->NativeDisplay == NULL)
802 xcb_disconnect(dri2_dpy->conn);
803 free(dri2_dpy);
804 disp->DriverData = NULL;
805
806 return EGL_TRUE;
807 }
808
809
810 /**
811 * Called via eglCreateContext(), drv->API.CreateContext().
812 */
813 static _EGLContext *
814 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
815 _EGLContext *share_list, const EGLint *attrib_list)
816 {
817 struct dri2_egl_context *dri2_ctx;
818 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
819 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
820 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
821
822 dri2_ctx = malloc(sizeof *dri2_ctx);
823 if (!dri2_ctx) {
824 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
825 return NULL;
826 }
827
828 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
829 goto cleanup;
830
831 dri2_ctx->dri_context =
832 dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen,
833 dri2_config->dri_config,
834 dri2_ctx_shared ?
835 dri2_ctx_shared->dri_context : NULL,
836 dri2_ctx);
837
838 if (!dri2_ctx->dri_context)
839 goto cleanup;
840
841 return &dri2_ctx->base;
842
843 cleanup:
844 free(dri2_ctx);
845 return NULL;
846 }
847
848 static EGLBoolean
849 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
850 {
851 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
852 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
853
854 if (_eglIsSurfaceBound(surf))
855 return EGL_TRUE;
856
857 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
858
859 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
860
861 if (surf->Type == EGL_PBUFFER_BIT)
862 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
863
864 free(surf);
865
866 return EGL_TRUE;
867 }
868
869 /**
870 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
871 */
872 static EGLBoolean
873 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
874 _EGLSurface *rsurf, _EGLContext *ctx)
875 {
876 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
877 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
878 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
879 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
880 __DRIdrawable *ddraw, *rdraw;
881 __DRIcontext *cctx;
882
883 /* bind the new context and return the "orphaned" one */
884 if (!_eglBindContext(&ctx, &dsurf, &rsurf))
885 return EGL_FALSE;
886
887 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
888 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
889 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
890
891 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
892 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
893 if (dsurf && !_eglIsSurfaceLinked(dsurf))
894 dri2_destroy_surface(drv, disp, dsurf);
895 if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf))
896 dri2_destroy_surface(drv, disp, rsurf);
897 if (ctx != NULL && !_eglIsContextLinked(ctx))
898 dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context);
899
900 return EGL_TRUE;
901 } else {
902 _eglBindContext(&ctx, &dsurf, &rsurf);
903
904 return EGL_FALSE;
905 }
906 }
907
908 /**
909 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
910 */
911 static _EGLSurface *
912 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
913 _EGLConfig *conf, EGLNativeWindowType window,
914 const EGLint *attrib_list)
915 {
916 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
917 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
918 struct dri2_egl_surface *dri2_surf;
919 xcb_get_geometry_cookie_t cookie;
920 xcb_get_geometry_reply_t *reply;
921 xcb_screen_iterator_t s;
922 xcb_generic_error_t *error;
923
924 dri2_surf = malloc(sizeof *dri2_surf);
925 if (!dri2_surf) {
926 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
927 return NULL;
928 }
929
930 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
931 goto cleanup_surf;
932
933 dri2_surf->region = XCB_NONE;
934 if (type == EGL_PBUFFER_BIT) {
935 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
936 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
937 xcb_create_pixmap(dri2_dpy->conn,
938 _eglGetConfigKey(conf, EGL_BUFFER_SIZE),
939 dri2_surf->drawable, s.data->root,
940 dri2_surf->base.Width, dri2_surf->base.Height);
941 } else {
942 dri2_surf->drawable = window;
943 }
944
945 dri2_surf->dri_drawable =
946 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
947 dri2_conf->dri_config, dri2_surf);
948 if (dri2_surf->dri_drawable == NULL) {
949 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
950 goto cleanup_pixmap;
951 }
952
953 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
954
955 if (type != EGL_PBUFFER_BIT) {
956 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
957 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
958 if (reply == NULL || error != NULL) {
959 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
960 free(error);
961 goto cleanup_dri_drawable;
962 }
963
964 dri2_surf->base.Width = reply->width;
965 dri2_surf->base.Height = reply->height;
966 free(reply);
967 }
968
969 return &dri2_surf->base;
970
971 cleanup_dri_drawable:
972 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
973 cleanup_pixmap:
974 if (type == EGL_PBUFFER_BIT)
975 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
976 cleanup_surf:
977 free(dri2_surf);
978
979 return NULL;
980 }
981
982 /**
983 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
984 */
985 static _EGLSurface *
986 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
987 _EGLConfig *conf, EGLNativeWindowType window,
988 const EGLint *attrib_list)
989 {
990 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
991 window, attrib_list);
992 }
993
994 static _EGLSurface *
995 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
996 _EGLConfig *conf, EGLNativePixmapType pixmap,
997 const EGLint *attrib_list)
998 {
999 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
1000 pixmap, attrib_list);
1001 }
1002
1003 static _EGLSurface *
1004 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
1005 _EGLConfig *conf, const EGLint *attrib_list)
1006 {
1007 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
1008 XCB_WINDOW_NONE, attrib_list);
1009 }
1010
1011 static EGLBoolean
1012 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1013 {
1014 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1015 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1016 xcb_dri2_copy_region_cookie_t cookie;
1017
1018 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1019
1020 #if 0
1021 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1022 * interval and page flipping (at least for fullscreen windows) as
1023 * well as the page flip event. Unless surface->SwapBehavior is
1024 * EGL_BUFFER_PRESERVED. */
1025 #if __DRI2_FLUSH_VERSION >= 2
1026 if (pdraw->psc->f)
1027 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
1028 #endif
1029 #endif
1030
1031 if (!dri2_surf->have_fake_front)
1032 return EGL_TRUE;
1033
1034 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
1035 dri2_surf->drawable,
1036 dri2_surf->region,
1037 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
1038 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT);
1039 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
1040
1041 return EGL_TRUE;
1042 }
1043
1044 /*
1045 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1046 */
1047 static _EGLProc
1048 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
1049 {
1050 /* FIXME: Do we need to support lookup of EGL symbols too? */
1051
1052 return (_EGLProc) _glapi_get_proc_address(procname);
1053 }
1054
1055 static EGLBoolean
1056 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1057 {
1058 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1059 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
1060
1061 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1062 * we need to copy fake to real here.*/
1063
1064 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1065
1066 return EGL_TRUE;
1067 }
1068
1069 static EGLBoolean
1070 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
1071 {
1072 if (engine != EGL_CORE_NATIVE_ENGINE)
1073 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1074 /* glXWaitX(); */
1075
1076 return EGL_TRUE;
1077 }
1078
1079 static void
1080 dri2_unload(_EGLDriver *drv)
1081 {
1082 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1083 free(dri2_drv);
1084 }
1085
1086 static EGLBoolean
1087 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1088 EGLNativePixmapType target)
1089 {
1090 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1091 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1092 xcb_gcontext_t gc;
1093
1094 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1095
1096 gc = xcb_generate_id(dri2_dpy->conn);
1097 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
1098 xcb_copy_area(dri2_dpy->conn,
1099 dri2_surf->drawable,
1100 target,
1101 gc,
1102 0, 0,
1103 0, 0,
1104 dri2_surf->base.Width,
1105 dri2_surf->base.Height);
1106 xcb_free_gc(dri2_dpy->conn, gc);
1107
1108 return EGL_TRUE;
1109 }
1110
1111 static EGLBoolean
1112 dri2_bind_tex_image(_EGLDriver *drv,
1113 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1114 {
1115 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1116 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1117 struct dri2_egl_context *dri2_ctx;
1118 _EGLContext *ctx;
1119 GLint format, target;
1120
1121 ctx = _eglGetCurrentContext();
1122 dri2_ctx = dri2_egl_context(ctx);
1123
1124 if (buffer != EGL_BACK_BUFFER) {
1125 _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
1126 return EGL_FALSE;
1127 }
1128
1129 /* We allow binding pixmaps too... Not conformat, but we can do it
1130 * for free and it's useful for X compositors. Supposedly there's
1131 * a EGL_NOKIA_texture_from_pixmap extension that allows that, but
1132 * I couldn't find it at this time. */
1133 if ((dri2_surf->base.Type & (EGL_PBUFFER_BIT | EGL_PIXMAP_BIT)) == 0) {
1134 _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
1135 return EGL_FALSE;
1136 }
1137
1138 switch (dri2_surf->base.TextureFormat) {
1139 case EGL_TEXTURE_RGB:
1140 format = __DRI_TEXTURE_FORMAT_RGB;
1141 break;
1142 case EGL_TEXTURE_RGBA:
1143 format = __DRI_TEXTURE_FORMAT_RGBA;
1144 break;
1145 default:
1146 _eglError(EGL_BAD_MATCH, "eglBindTexImage");
1147 return EGL_FALSE;
1148 }
1149
1150 switch (dri2_surf->base.TextureTarget) {
1151 case EGL_TEXTURE_2D:
1152 target = GL_TEXTURE_2D;
1153 break;
1154 default:
1155 _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
1156 return EGL_FALSE;
1157 }
1158
1159 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1160 target, format,
1161 dri2_surf->dri_drawable);
1162
1163 return dri2_surf->base.BoundToTexture = EGL_TRUE;
1164 }
1165
1166 static EGLBoolean
1167 dri2_release_tex_image(_EGLDriver *drv,
1168 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1169 {
1170 return EGL_TRUE;
1171 }
1172
1173 static _EGLImage *
1174 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
1175 EGLClientBuffer buffer, const EGLint *attr_list)
1176 {
1177 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1178 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1179 struct dri2_egl_image *dri2_img;
1180 unsigned int attachments[1];
1181 xcb_drawable_t drawable;
1182 xcb_dri2_get_buffers_cookie_t buffers_cookie;
1183 xcb_dri2_get_buffers_reply_t *buffers_reply;
1184 xcb_dri2_dri2_buffer_t *buffers;
1185 xcb_get_geometry_cookie_t geometry_cookie;
1186 xcb_get_geometry_reply_t *geometry_reply;
1187 xcb_generic_error_t *error;
1188 int stride, format;
1189
1190 drawable = (xcb_drawable_t) buffer;
1191 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1192 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1193 buffers_cookie =
1194 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1195 drawable, 1, 1, attachments);
1196 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1197 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1198 buffers_cookie, NULL);
1199 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1200 if (buffers == NULL) {
1201 return NULL;
1202 }
1203
1204 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1205 geometry_cookie, &error);
1206 if (geometry_reply == NULL || error != NULL) {
1207 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1208 free(error);
1209 free(buffers_reply);
1210 }
1211
1212 switch (geometry_reply->depth) {
1213 case 16:
1214 format = __DRI_IMAGE_FORMAT_RGB565;
1215 break;
1216 case 24:
1217 format = __DRI_IMAGE_FORMAT_XRGB8888;
1218 break;
1219 case 32:
1220 format = __DRI_IMAGE_FORMAT_ARGB8888;
1221 break;
1222 default:
1223 _eglError(EGL_BAD_PARAMETER,
1224 "dri2_create_image_khr: unsupported pixmap depth");
1225 free(buffers_reply);
1226 free(geometry_reply);
1227 return NULL;
1228 }
1229
1230 dri2_img = malloc(sizeof *dri2_img);
1231 if (!dri2_img) {
1232 free(buffers_reply);
1233 free(geometry_reply);
1234 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1235 return EGL_NO_IMAGE_KHR;
1236 }
1237
1238 if (!_eglInitImage(&dri2_img->base, disp, attr_list)) {
1239 free(buffers_reply);
1240 free(geometry_reply);
1241 return EGL_NO_IMAGE_KHR;
1242 }
1243
1244 stride = buffers[0].pitch / buffers[0].cpp;
1245 dri2_img->dri_image =
1246 dri2_dpy->image->createImageFromName(dri2_ctx->dri_context,
1247 buffers_reply->width,
1248 buffers_reply->height,
1249 format,
1250 buffers[0].name,
1251 stride,
1252 dri2_img);
1253
1254 free(buffers_reply);
1255 free(geometry_reply);
1256
1257 return &dri2_img->base;
1258 }
1259
1260 static _EGLImage *
1261 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1262 EGLClientBuffer buffer,
1263 const EGLint *attr_list)
1264 {
1265 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1266 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1267 struct dri2_egl_image *dri2_img;
1268 GLuint renderbuffer = (GLuint) buffer;
1269
1270 if (renderbuffer == 0) {
1271 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1272 return EGL_NO_IMAGE_KHR;
1273 }
1274
1275 dri2_img = malloc(sizeof *dri2_img);
1276 if (!dri2_img) {
1277 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1278 return EGL_NO_IMAGE_KHR;
1279 }
1280
1281 if (!_eglInitImage(&dri2_img->base, disp, attr_list))
1282 return EGL_NO_IMAGE_KHR;
1283
1284 dri2_img->dri_image =
1285 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1286 renderbuffer,
1287 dri2_img);
1288
1289 return &dri2_img->base;
1290 }
1291
1292 static _EGLImage *
1293 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1294 _EGLContext *ctx, EGLenum target,
1295 EGLClientBuffer buffer, const EGLint *attr_list)
1296 {
1297 switch (target) {
1298 case EGL_NATIVE_PIXMAP_KHR:
1299 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1300 case EGL_GL_RENDERBUFFER_KHR:
1301 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
1302 default:
1303 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1304 return EGL_NO_IMAGE_KHR;
1305 }
1306 }
1307
1308 static EGLBoolean
1309 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
1310 {
1311 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1312 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
1313
1314 dri2_dpy->image->destroyImage(dri2_img->dri_image);
1315 free(dri2_img);
1316
1317 return EGL_TRUE;
1318 }
1319
1320 /**
1321 * This is the main entrypoint into the driver, called by libEGL.
1322 * Create a new _EGLDriver object and init its dispatch table.
1323 */
1324 _EGLDriver *
1325 _eglMain(const char *args)
1326 {
1327 struct dri2_egl_driver *dri2_drv;
1328
1329 dri2_drv = malloc(sizeof *dri2_drv);
1330 if (!dri2_drv)
1331 return NULL;
1332
1333 _eglInitDriverFallbacks(&dri2_drv->base);
1334 dri2_drv->base.API.Initialize = dri2_initialize;
1335 dri2_drv->base.API.Terminate = dri2_terminate;
1336 dri2_drv->base.API.CreateContext = dri2_create_context;
1337 dri2_drv->base.API.MakeCurrent = dri2_make_current;
1338 dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
1339 dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
1340 dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
1341 dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
1342 dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
1343 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
1344 dri2_drv->base.API.WaitClient = dri2_wait_client;
1345 dri2_drv->base.API.WaitNative = dri2_wait_native;
1346 dri2_drv->base.API.CopyBuffers = dri2_copy_buffers;
1347 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
1348 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
1349 dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr;
1350 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
1351
1352 dri2_drv->base.Name = "DRI2";
1353 dri2_drv->base.Unload = dri2_unload;
1354
1355 return &dri2_drv->base;
1356 }