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