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