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