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