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