7c46f8e320c3992a51a0eb4265101b2dd5d4ecce
[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.MESA_drm_image = EGL_TRUE;
840 disp->Extensions.KHR_image_base = EGL_TRUE;
841 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
842 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
843 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
844 disp->Extensions.NOK_swap_region = EGL_TRUE;
845 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
846
847 /* we're supporting EGL 1.4 */
848 *major = 1;
849 *minor = 4;
850
851 return EGL_TRUE;
852
853 cleanup_configs:
854 _eglCleanupDisplay(disp);
855 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
856 cleanup_fd:
857 close(dri2_dpy->fd);
858 cleanup_driver:
859 dlclose(dri2_dpy->driver);
860 cleanup_conn:
861 if (disp->PlatformDisplay == NULL)
862 xcb_disconnect(dri2_dpy->conn);
863 cleanup_dpy:
864 free(dri2_dpy);
865
866 return EGL_FALSE;
867 }
868
869 #ifdef HAVE_LIBUDEV
870
871 struct dri2_driver_map {
872 int vendor_id;
873 const char *driver;
874 const int *chip_ids;
875 int num_chips_ids;
876 };
877
878 const int i915_chip_ids[] = {
879 0x3577, /* PCI_CHIP_I830_M */
880 0x2562, /* PCI_CHIP_845_G */
881 0x3582, /* PCI_CHIP_I855_GM */
882 0x2572, /* PCI_CHIP_I865_G */
883 0x2582, /* PCI_CHIP_I915_G */
884 0x258a, /* PCI_CHIP_E7221_G */
885 0x2592, /* PCI_CHIP_I915_GM */
886 0x2772, /* PCI_CHIP_I945_G */
887 0x27a2, /* PCI_CHIP_I945_GM */
888 0x27ae, /* PCI_CHIP_I945_GME */
889 0x29b2, /* PCI_CHIP_Q35_G */
890 0x29c2, /* PCI_CHIP_G33_G */
891 0x29d2, /* PCI_CHIP_Q33_G */
892 };
893
894 const int i965_chip_ids[] = {
895 0x29a2, /* PCI_CHIP_I965_G */
896 0x2992, /* PCI_CHIP_I965_Q */
897 0x2982, /* PCI_CHIP_I965_G_1 */
898 0x2972, /* PCI_CHIP_I946_GZ */
899 0x2a02, /* PCI_CHIP_I965_GM */
900 0x2a12, /* PCI_CHIP_I965_GME */
901 0x2a42, /* PCI_CHIP_GM45_GM */
902 0x2e02, /* PCI_CHIP_IGD_E_G */
903 0x2e12, /* PCI_CHIP_Q45_G */
904 0x2e22, /* PCI_CHIP_G45_G */
905 0x2e32, /* PCI_CHIP_G41_G */
906 0xa011, /* Ironlake */
907 };
908
909 const struct dri2_driver_map driver_map[] = {
910 { 0x8086, "i915", i915_chip_ids, ARRAY_SIZE(i915_chip_ids) },
911 { 0x8086, "i965", i965_chip_ids, ARRAY_SIZE(i965_chip_ids) },
912 };
913
914 static char *
915 dri2_get_driver_for_fd(int fd)
916 {
917 struct udev *udev;
918 struct udev_device *device, *parent;
919 struct stat buf;
920 const char *pci_id;
921 char *driver = NULL;
922 int vendor_id, chip_id, i, j;
923
924 udev = udev_new();
925 if (fstat(fd, &buf) < 0) {
926 _eglLog(_EGL_WARNING, "EGL-DRI2: failed to stat fd %d", fd);
927 goto out;
928 }
929
930 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
931 if (device == NULL) {
932 _eglLog(_EGL_WARNING,
933 "EGL-DRI2: could not create udev device for fd %d", fd);
934 goto out;
935 }
936
937 parent = udev_device_get_parent(device);
938 if (parent == NULL) {
939 _eglLog(_EGL_WARNING, "DRI2: could not get parent device");
940 goto out;
941 }
942
943 pci_id = udev_device_get_property_value(parent, "PCI_ID");
944 if (pci_id == NULL || sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
945 _eglLog(_EGL_WARNING, "EGL-DRI2: malformed or no PCI ID");
946 goto out;
947 }
948
949 for (i = 0; i < ARRAY_SIZE(driver_map); i++) {
950 if (vendor_id != driver_map[i].vendor_id)
951 continue;
952 for (j = 0; j < driver_map[i].num_chips_ids; j++)
953 if (driver_map[i].chip_ids[j] == chip_id) {
954 driver = strdup(driver_map[i].driver);
955 _eglLog(_EGL_DEBUG, "pci id for %d: %04x:%04x, driver %s",
956 fd, vendor_id, chip_id, driver);
957 goto out;
958 }
959 }
960
961 out:
962 udev_device_unref(device);
963 udev_unref(udev);
964
965 return driver;
966 }
967
968 static EGLBoolean
969 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp,
970 EGLint *major, EGLint *minor)
971 {
972 struct dri2_egl_display *dri2_dpy;
973 int i;
974
975 dri2_dpy = malloc(sizeof *dri2_dpy);
976 if (!dri2_dpy)
977 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
978
979 disp->DriverData = (void *) dri2_dpy;
980 dri2_dpy->fd = (int) disp->PlatformDisplay;
981
982 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
983 if (dri2_dpy->driver_name == NULL)
984 return _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
985
986 if (!dri2_load_driver(disp))
987 goto cleanup_driver_name;
988
989 dri2_dpy->extensions[0] = &image_lookup_extension.base;
990 dri2_dpy->extensions[1] = &use_invalidate.base;
991 dri2_dpy->extensions[2] = NULL;
992
993 if (!dri2_create_screen(disp))
994 goto cleanup_driver;
995
996 for (i = 0; dri2_dpy->driver_configs[i]; i++)
997 dri2_add_config(disp, dri2_dpy->driver_configs[i], i + 1, 0, 0);
998
999 disp->Extensions.MESA_drm_image = EGL_TRUE;
1000 disp->Extensions.KHR_image_base = EGL_TRUE;
1001 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
1002 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
1003
1004 /* we're supporting EGL 1.4 */
1005 *major = 1;
1006 *minor = 4;
1007
1008 return EGL_TRUE;
1009
1010 cleanup_driver:
1011 dlclose(dri2_dpy->driver);
1012 cleanup_driver_name:
1013 free(dri2_dpy->driver_name);
1014
1015 return EGL_FALSE;
1016 }
1017
1018 #endif
1019
1020 /**
1021 * Called via eglInitialize(), GLX_drv->API.Initialize().
1022 */
1023 static EGLBoolean
1024 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp,
1025 EGLint *major, EGLint *minor)
1026 {
1027 switch (disp->Platform) {
1028 case _EGL_PLATFORM_X11:
1029 return dri2_initialize_x11(drv, disp, major, minor);
1030
1031 #ifdef HAVE_LIBUDEV
1032 case _EGL_PLATFORM_DRM:
1033 return dri2_initialize_drm(drv, disp, major, minor);
1034 #endif
1035
1036 default:
1037 return EGL_FALSE;
1038 }
1039 }
1040
1041 /**
1042 * Called via eglTerminate(), drv->API.Terminate().
1043 */
1044 static EGLBoolean
1045 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
1046 {
1047 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1048
1049 _eglReleaseDisplayResources(drv, disp);
1050 _eglCleanupDisplay(disp);
1051
1052 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1053 close(dri2_dpy->fd);
1054 dlclose(dri2_dpy->driver);
1055 if (disp->PlatformDisplay == NULL)
1056 xcb_disconnect(dri2_dpy->conn);
1057 free(dri2_dpy);
1058 disp->DriverData = NULL;
1059
1060 return EGL_TRUE;
1061 }
1062
1063
1064 /**
1065 * Called via eglCreateContext(), drv->API.CreateContext().
1066 */
1067 static _EGLContext *
1068 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
1069 _EGLContext *share_list, const EGLint *attrib_list)
1070 {
1071 struct dri2_egl_context *dri2_ctx;
1072 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1073 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1074 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1075 const __DRIconfig *dri_config;
1076 int api;
1077
1078 dri2_ctx = malloc(sizeof *dri2_ctx);
1079 if (!dri2_ctx) {
1080 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1081 return NULL;
1082 }
1083
1084 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1085 goto cleanup;
1086
1087 switch (dri2_ctx->base.ClientAPI) {
1088 case EGL_OPENGL_ES_API:
1089 switch (dri2_ctx->base.ClientVersion) {
1090 case 1:
1091 api = __DRI_API_GLES;
1092 break;
1093 case 2:
1094 api = __DRI_API_GLES2;
1095 break;
1096 default:
1097 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1098 return NULL;
1099 }
1100 break;
1101 case EGL_OPENGL_API:
1102 api = __DRI_API_OPENGL;
1103 break;
1104 default:
1105 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1106 return NULL;
1107 }
1108
1109 if (conf != NULL)
1110 dri_config = dri2_config->dri_config;
1111 else
1112 dri_config = NULL;
1113
1114 if (dri2_dpy->dri2->base.version >= 2) {
1115 dri2_ctx->dri_context =
1116 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1117 api,
1118 dri_config,
1119 dri2_ctx_shared ?
1120 dri2_ctx_shared->dri_context : NULL,
1121 dri2_ctx);
1122 } else if (api == __DRI_API_OPENGL) {
1123 dri2_ctx->dri_context =
1124 dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen,
1125 dri2_config->dri_config,
1126 dri2_ctx_shared ?
1127 dri2_ctx_shared->dri_context : NULL,
1128 dri2_ctx);
1129 } else {
1130 /* fail */
1131 }
1132
1133 if (!dri2_ctx->dri_context)
1134 goto cleanup;
1135
1136 return &dri2_ctx->base;
1137
1138 cleanup:
1139 free(dri2_ctx);
1140 return NULL;
1141 }
1142
1143 static EGLBoolean
1144 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
1145 {
1146 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1147 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1148
1149 if (_eglIsSurfaceBound(surf))
1150 return EGL_TRUE;
1151
1152 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
1153
1154 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
1155
1156 if (surf->Type == EGL_PBUFFER_BIT)
1157 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
1158
1159 free(surf);
1160
1161 return EGL_TRUE;
1162 }
1163
1164 /**
1165 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1166 */
1167 static EGLBoolean
1168 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
1169 _EGLSurface *rsurf, _EGLContext *ctx)
1170 {
1171 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1172 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1173 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
1174 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
1175 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1176 __DRIdrawable *ddraw, *rdraw;
1177 __DRIcontext *cctx;
1178
1179 /* bind the new context and return the "orphaned" one */
1180 if (!_eglBindContext(&ctx, &dsurf, &rsurf))
1181 return EGL_FALSE;
1182
1183 /* flush before context switch */
1184 if (ctx && dri2_drv->glFlush)
1185 dri2_drv->glFlush();
1186
1187 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
1188 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
1189 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1190
1191 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
1192 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1193 if (dsurf && !_eglIsSurfaceLinked(dsurf))
1194 dri2_destroy_surface(drv, disp, dsurf);
1195 if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf))
1196 dri2_destroy_surface(drv, disp, rsurf);
1197 if (ctx != NULL && !_eglIsContextLinked(ctx))
1198 dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context);
1199
1200 return EGL_TRUE;
1201 } else {
1202 _eglBindContext(&ctx, &dsurf, &rsurf);
1203
1204 return EGL_FALSE;
1205 }
1206 }
1207
1208 /**
1209 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1210 */
1211 static _EGLSurface *
1212 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
1213 _EGLConfig *conf, EGLNativeWindowType window,
1214 const EGLint *attrib_list)
1215 {
1216 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1217 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
1218 struct dri2_egl_surface *dri2_surf;
1219 xcb_get_geometry_cookie_t cookie;
1220 xcb_get_geometry_reply_t *reply;
1221 xcb_screen_iterator_t s;
1222 xcb_generic_error_t *error;
1223
1224 dri2_surf = malloc(sizeof *dri2_surf);
1225 if (!dri2_surf) {
1226 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
1227 return NULL;
1228 }
1229
1230 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
1231 goto cleanup_surf;
1232
1233 dri2_surf->region = XCB_NONE;
1234 if (type == EGL_PBUFFER_BIT) {
1235 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
1236 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
1237 xcb_create_pixmap(dri2_dpy->conn,
1238 _eglGetConfigKey(conf, EGL_BUFFER_SIZE),
1239 dri2_surf->drawable, s.data->root,
1240 dri2_surf->base.Width, dri2_surf->base.Height);
1241 } else {
1242 dri2_surf->drawable = window;
1243 }
1244
1245 dri2_surf->dri_drawable =
1246 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
1247 dri2_conf->dri_config, dri2_surf);
1248 if (dri2_surf->dri_drawable == NULL) {
1249 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
1250 goto cleanup_pixmap;
1251 }
1252
1253 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
1254
1255 if (type != EGL_PBUFFER_BIT) {
1256 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
1257 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
1258 if (reply == NULL || error != NULL) {
1259 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1260 free(error);
1261 goto cleanup_dri_drawable;
1262 }
1263
1264 dri2_surf->base.Width = reply->width;
1265 dri2_surf->base.Height = reply->height;
1266 free(reply);
1267 }
1268
1269 return &dri2_surf->base;
1270
1271 cleanup_dri_drawable:
1272 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
1273 cleanup_pixmap:
1274 if (type == EGL_PBUFFER_BIT)
1275 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
1276 cleanup_surf:
1277 free(dri2_surf);
1278
1279 return NULL;
1280 }
1281
1282 /**
1283 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1284 */
1285 static _EGLSurface *
1286 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
1287 _EGLConfig *conf, EGLNativeWindowType window,
1288 const EGLint *attrib_list)
1289 {
1290 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
1291 window, attrib_list);
1292 }
1293
1294 static _EGLSurface *
1295 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
1296 _EGLConfig *conf, EGLNativePixmapType pixmap,
1297 const EGLint *attrib_list)
1298 {
1299 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
1300 pixmap, attrib_list);
1301 }
1302
1303 static _EGLSurface *
1304 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
1305 _EGLConfig *conf, const EGLint *attrib_list)
1306 {
1307 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
1308 XCB_WINDOW_NONE, attrib_list);
1309 }
1310
1311 static EGLBoolean
1312 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
1313 _EGLSurface *draw, xcb_xfixes_region_t region)
1314 {
1315 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1316 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1317 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1318 _EGLContext *ctx;
1319 xcb_dri2_copy_region_cookie_t cookie;
1320
1321 if (dri2_drv->glFlush) {
1322 ctx = _eglGetCurrentContext();
1323 if (ctx && ctx->DrawSurface == &dri2_surf->base)
1324 dri2_drv->glFlush();
1325 }
1326
1327 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1328
1329 #if 0
1330 /* FIXME: Add support for dri swapbuffers, that'll give us swap
1331 * interval and page flipping (at least for fullscreen windows) as
1332 * well as the page flip event. Unless surface->SwapBehavior is
1333 * EGL_BUFFER_PRESERVED. */
1334 #if __DRI2_FLUSH_VERSION >= 2
1335 if (pdraw->psc->f)
1336 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
1337 #endif
1338 #endif
1339
1340 if (!dri2_surf->have_fake_front)
1341 return EGL_TRUE;
1342
1343 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
1344 dri2_surf->drawable,
1345 region,
1346 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
1347 XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT);
1348 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
1349
1350 return EGL_TRUE;
1351 }
1352
1353 static EGLBoolean
1354 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1355 {
1356 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1357
1358 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
1359 }
1360
1361 static EGLBoolean
1362 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
1363 EGLint numRects, const EGLint *rects)
1364 {
1365 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1366 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1367 EGLBoolean ret;
1368 xcb_xfixes_region_t region;
1369 xcb_rectangle_t rectangles[16];
1370 int i;
1371
1372 if (numRects > ARRAY_SIZE(rectangles))
1373 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
1374
1375 /* FIXME: Invert y here? */
1376 for (i = 0; i < numRects; i++) {
1377 rectangles[i].x = rects[i * 4];
1378 rectangles[i].y = rects[i * 4 + 1];
1379 rectangles[i].width = rects[i * 4 + 2];
1380 rectangles[i].height = rects[i * 4 + 3];
1381 }
1382
1383 region = xcb_generate_id(dri2_dpy->conn);
1384 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
1385 ret = dri2_copy_region(drv, disp, draw, region);
1386 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
1387
1388 return ret;
1389 }
1390
1391 /*
1392 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1393 */
1394 static _EGLProc
1395 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
1396 {
1397 /* FIXME: Do we need to support lookup of EGL symbols too? */
1398
1399 return (_EGLProc) _glapi_get_proc_address(procname);
1400 }
1401
1402 static EGLBoolean
1403 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1404 {
1405 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1406 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
1407
1408 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1409 * we need to copy fake to real here.*/
1410
1411 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1412
1413 return EGL_TRUE;
1414 }
1415
1416 static EGLBoolean
1417 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
1418 {
1419 if (engine != EGL_CORE_NATIVE_ENGINE)
1420 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1421 /* glXWaitX(); */
1422
1423 return EGL_TRUE;
1424 }
1425
1426 static void
1427 dri2_unload(_EGLDriver *drv)
1428 {
1429 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1430 free(dri2_drv);
1431 }
1432
1433 static EGLBoolean
1434 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1435 EGLNativePixmapType target)
1436 {
1437 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1438 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1439 xcb_gcontext_t gc;
1440
1441 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
1442
1443 gc = xcb_generate_id(dri2_dpy->conn);
1444 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
1445 xcb_copy_area(dri2_dpy->conn,
1446 dri2_surf->drawable,
1447 target,
1448 gc,
1449 0, 0,
1450 0, 0,
1451 dri2_surf->base.Width,
1452 dri2_surf->base.Height);
1453 xcb_free_gc(dri2_dpy->conn, gc);
1454
1455 return EGL_TRUE;
1456 }
1457
1458 static EGLBoolean
1459 dri2_bind_tex_image(_EGLDriver *drv,
1460 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1461 {
1462 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1463 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1464 struct dri2_egl_context *dri2_ctx;
1465 _EGLContext *ctx;
1466 GLint format, target;
1467
1468 ctx = _eglGetCurrentContext();
1469 dri2_ctx = dri2_egl_context(ctx);
1470
1471 if (!_eglBindTexImage(drv, disp, surf, buffer))
1472 return EGL_FALSE;
1473
1474 switch (dri2_surf->base.TextureFormat) {
1475 case EGL_TEXTURE_RGB:
1476 format = __DRI_TEXTURE_FORMAT_RGB;
1477 break;
1478 case EGL_TEXTURE_RGBA:
1479 format = __DRI_TEXTURE_FORMAT_RGBA;
1480 break;
1481 default:
1482 assert(0);
1483 }
1484
1485 switch (dri2_surf->base.TextureTarget) {
1486 case EGL_TEXTURE_2D:
1487 target = GL_TEXTURE_2D;
1488 break;
1489 default:
1490 assert(0);
1491 }
1492
1493 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1494 target, format,
1495 dri2_surf->dri_drawable);
1496
1497 return EGL_TRUE;
1498 }
1499
1500 static EGLBoolean
1501 dri2_release_tex_image(_EGLDriver *drv,
1502 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1503 {
1504 return EGL_TRUE;
1505 }
1506
1507 static _EGLImage *
1508 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
1509 EGLClientBuffer buffer, const EGLint *attr_list)
1510 {
1511 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1512 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1513 struct dri2_egl_image *dri2_img;
1514 unsigned int attachments[1];
1515 xcb_drawable_t drawable;
1516 xcb_dri2_get_buffers_cookie_t buffers_cookie;
1517 xcb_dri2_get_buffers_reply_t *buffers_reply;
1518 xcb_dri2_dri2_buffer_t *buffers;
1519 xcb_get_geometry_cookie_t geometry_cookie;
1520 xcb_get_geometry_reply_t *geometry_reply;
1521 xcb_generic_error_t *error;
1522 int stride, format;
1523
1524 drawable = (xcb_drawable_t) buffer;
1525 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1526 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1527 buffers_cookie =
1528 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1529 drawable, 1, 1, attachments);
1530 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1531 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1532 buffers_cookie, NULL);
1533 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1534 if (buffers == NULL) {
1535 return NULL;
1536 }
1537
1538 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1539 geometry_cookie, &error);
1540 if (geometry_reply == NULL || error != NULL) {
1541 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1542 free(error);
1543 free(buffers_reply);
1544 }
1545
1546 switch (geometry_reply->depth) {
1547 case 16:
1548 format = __DRI_IMAGE_FORMAT_RGB565;
1549 break;
1550 case 24:
1551 format = __DRI_IMAGE_FORMAT_XRGB8888;
1552 break;
1553 case 32:
1554 format = __DRI_IMAGE_FORMAT_ARGB8888;
1555 break;
1556 default:
1557 _eglError(EGL_BAD_PARAMETER,
1558 "dri2_create_image_khr: unsupported pixmap depth");
1559 free(buffers_reply);
1560 free(geometry_reply);
1561 return NULL;
1562 }
1563
1564 dri2_img = malloc(sizeof *dri2_img);
1565 if (!dri2_img) {
1566 free(buffers_reply);
1567 free(geometry_reply);
1568 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1569 return EGL_NO_IMAGE_KHR;
1570 }
1571
1572 if (!_eglInitImage(&dri2_img->base, disp, attr_list)) {
1573 free(buffers_reply);
1574 free(geometry_reply);
1575 return EGL_NO_IMAGE_KHR;
1576 }
1577
1578 stride = buffers[0].pitch / buffers[0].cpp;
1579 dri2_img->dri_image =
1580 dri2_dpy->image->createImageFromName(dri2_ctx->dri_context,
1581 buffers_reply->width,
1582 buffers_reply->height,
1583 format,
1584 buffers[0].name,
1585 stride,
1586 dri2_img);
1587
1588 free(buffers_reply);
1589 free(geometry_reply);
1590
1591 return &dri2_img->base;
1592 }
1593
1594 static _EGLImage *
1595 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1596 EGLClientBuffer buffer,
1597 const EGLint *attr_list)
1598 {
1599 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1600 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1601 struct dri2_egl_image *dri2_img;
1602 GLuint renderbuffer = (GLuint) buffer;
1603
1604 if (renderbuffer == 0) {
1605 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1606 return EGL_NO_IMAGE_KHR;
1607 }
1608
1609 dri2_img = malloc(sizeof *dri2_img);
1610 if (!dri2_img) {
1611 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1612 return EGL_NO_IMAGE_KHR;
1613 }
1614
1615 if (!_eglInitImage(&dri2_img->base, disp, attr_list))
1616 return EGL_NO_IMAGE_KHR;
1617
1618 dri2_img->dri_image =
1619 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1620 renderbuffer,
1621 dri2_img);
1622
1623 return &dri2_img->base;
1624 }
1625
1626 static _EGLImage *
1627 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1628 EGLClientBuffer buffer, const EGLint *attr_list)
1629 {
1630 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1631 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1632 struct dri2_egl_image *dri2_img;
1633 EGLint width, height, format, name, stride, pitch, i, err;
1634
1635 name = (EGLint) buffer;
1636
1637 err = EGL_SUCCESS;
1638 width = 0;
1639 height = 0;
1640 format = 0;
1641 stride = 0;
1642
1643 for (i = 0; attr_list[i] != EGL_NONE; i++) {
1644 EGLint attr = attr_list[i++];
1645 EGLint val = attr_list[i];
1646
1647 switch (attr) {
1648 case EGL_WIDTH:
1649 width = val;
1650 break;
1651 case EGL_HEIGHT:
1652 height = val;
1653 break;
1654 case EGL_DRM_BUFFER_FORMAT_MESA:
1655 format = val;
1656 break;
1657 case EGL_DRM_BUFFER_STRIDE_MESA:
1658 stride = val;
1659 break;
1660 default:
1661 err = EGL_BAD_ATTRIBUTE;
1662 break;
1663 }
1664
1665 if (err != EGL_SUCCESS) {
1666 _eglLog(_EGL_WARNING, "bad image attribute 0x%04x", attr);
1667 return NULL;
1668 }
1669 }
1670
1671 if (width <= 0 || height <= 0 || stride <= 0) {
1672 _eglError(EGL_BAD_PARAMETER,
1673 "bad width, height or stride");
1674 return NULL;
1675 }
1676
1677 switch (format) {
1678 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1679 format = __DRI_IMAGE_FORMAT_ARGB8888;
1680 pitch = stride;
1681 break;
1682 default:
1683 _eglError(EGL_BAD_PARAMETER,
1684 "dri2_create_image_khr: unsupported pixmap depth");
1685 return NULL;
1686 }
1687
1688 dri2_img = malloc(sizeof *dri2_img);
1689 if (!dri2_img) {
1690 _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm");
1691 return NULL;
1692 }
1693
1694 if (!_eglInitImage(&dri2_img->base, disp, attr_list)) {
1695 free(dri2_img);
1696 return NULL;
1697 }
1698
1699 dri2_img->dri_image =
1700 dri2_dpy->image->createImageFromName(dri2_ctx->dri_context,
1701 width,
1702 height,
1703 format,
1704 name,
1705 pitch,
1706 dri2_img);
1707 if (dri2_img->dri_image == NULL) {
1708 free(dri2_img);
1709 _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm");
1710 return NULL;
1711 }
1712
1713 return &dri2_img->base;
1714 }
1715
1716 static _EGLImage *
1717 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1718 _EGLContext *ctx, EGLenum target,
1719 EGLClientBuffer buffer, const EGLint *attr_list)
1720 {
1721 switch (target) {
1722 case EGL_NATIVE_PIXMAP_KHR:
1723 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1724 case EGL_GL_RENDERBUFFER_KHR:
1725 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
1726 case EGL_DRM_BUFFER_MESA:
1727 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
1728 default:
1729 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1730 return EGL_NO_IMAGE_KHR;
1731 }
1732 }
1733
1734 static EGLBoolean
1735 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
1736 {
1737 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1738 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
1739
1740 dri2_dpy->image->destroyImage(dri2_img->dri_image);
1741 free(dri2_img);
1742
1743 return EGL_TRUE;
1744 }
1745
1746 static _EGLImage *
1747 dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
1748 const EGLint *attr_list)
1749 {
1750 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1751 struct dri2_egl_image *dri2_img;
1752 int width, height, format, i;
1753 unsigned int use, dri_use, valid_mask;
1754 EGLint err = EGL_SUCCESS;
1755
1756 dri2_img = malloc(sizeof *dri2_img);
1757 if (!dri2_img) {
1758 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1759 return EGL_NO_IMAGE_KHR;
1760 }
1761
1762 if (!attr_list) {
1763 err = EGL_BAD_PARAMETER;
1764 goto cleanup_img;
1765 }
1766
1767 if (!_eglInitImage(&dri2_img->base, disp, attr_list)) {
1768 err = EGL_BAD_PARAMETER;
1769 goto cleanup_img;
1770 }
1771
1772 width = 0;
1773 height = 0;
1774 format = 0;
1775 use = 0;
1776 for (i = 0; attr_list[i] != EGL_NONE; i++) {
1777 EGLint attr = attr_list[i++];
1778 EGLint val = attr_list[i];
1779
1780 switch (attr) {
1781 case EGL_WIDTH:
1782 width = val;
1783 break;
1784 case EGL_HEIGHT:
1785 height = val;
1786 break;
1787 case EGL_DRM_BUFFER_FORMAT_MESA:
1788 format = val;
1789 break;
1790 case EGL_DRM_BUFFER_USE_MESA:
1791 use = val;
1792 break;
1793 default:
1794 err = EGL_BAD_ATTRIBUTE;
1795 break;
1796 }
1797
1798 if (err != EGL_SUCCESS) {
1799 _eglLog(_EGL_WARNING, "bad image attribute 0x%04x", attr);
1800 goto cleanup_img;
1801 }
1802 }
1803
1804 if (width <= 0 || height <= 0) {
1805 _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", width, height);
1806 goto cleanup_img;
1807 }
1808
1809 switch (format) {
1810 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1811 format = __DRI_IMAGE_FORMAT_ARGB8888;
1812 break;
1813 default:
1814 _eglLog(_EGL_WARNING, "bad image format value 0x%04x", format);
1815 goto cleanup_img;
1816 }
1817
1818 valid_mask =
1819 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
1820 EGL_DRM_BUFFER_USE_SHARE_MESA;
1821 if (use & ~valid_mask) {
1822 _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", use & ~valid_mask);
1823 goto cleanup_img;
1824 }
1825
1826 dri_use = 0;
1827 if (use & EGL_DRM_BUFFER_USE_SHARE_MESA)
1828 dri_use |= __DRI_IMAGE_USE_SHARE;
1829 if (use & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
1830 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1831
1832 dri2_img->dri_image =
1833 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
1834 width, height, format, dri_use, dri2_img);
1835 if (dri2_img->dri_image == NULL) {
1836 err = EGL_BAD_ALLOC;
1837 goto cleanup_img;
1838 }
1839
1840 return &dri2_img->base;
1841
1842 cleanup_img:
1843 free(dri2_img);
1844 _eglError(err, "dri2_create_drm_image_mesa");
1845
1846 return EGL_NO_IMAGE_KHR;
1847 }
1848
1849 static EGLBoolean
1850 dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
1851 EGLint *name, EGLint *handle, EGLint *stride)
1852 {
1853 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1854 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1855
1856 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
1857 __DRI_IMAGE_ATTRIB_NAME, name)) {
1858 _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
1859 return EGL_FALSE;
1860 }
1861
1862 if (handle)
1863 dri2_dpy->image->queryImage(dri2_img->dri_image,
1864 __DRI_IMAGE_ATTRIB_HANDLE, handle);
1865
1866 if (stride)
1867 dri2_dpy->image->queryImage(dri2_img->dri_image,
1868 __DRI_IMAGE_ATTRIB_STRIDE, stride);
1869
1870 return EGL_TRUE;
1871 }
1872
1873 /**
1874 * This is the main entrypoint into the driver, called by libEGL.
1875 * Create a new _EGLDriver object and init its dispatch table.
1876 */
1877 _EGLDriver *
1878 _eglMain(const char *args)
1879 {
1880 struct dri2_egl_driver *dri2_drv;
1881
1882 dri2_drv = malloc(sizeof *dri2_drv);
1883 if (!dri2_drv)
1884 return NULL;
1885
1886 memset(dri2_drv, 0, sizeof *dri2_drv);
1887 _eglInitDriverFallbacks(&dri2_drv->base);
1888 dri2_drv->base.API.Initialize = dri2_initialize;
1889 dri2_drv->base.API.Terminate = dri2_terminate;
1890 dri2_drv->base.API.CreateContext = dri2_create_context;
1891 dri2_drv->base.API.MakeCurrent = dri2_make_current;
1892 dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
1893 dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
1894 dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
1895 dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
1896 dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
1897 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
1898 dri2_drv->base.API.WaitClient = dri2_wait_client;
1899 dri2_drv->base.API.WaitNative = dri2_wait_native;
1900 dri2_drv->base.API.CopyBuffers = dri2_copy_buffers;
1901 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
1902 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
1903 dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr;
1904 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
1905 dri2_drv->base.API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
1906 dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa;
1907 dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa;
1908
1909 dri2_drv->base.Name = "DRI2";
1910 dri2_drv->base.Unload = dri2_unload;
1911
1912 dri2_drv->glFlush =
1913 (void (*)(void)) dri2_get_proc_address(&dri2_drv->base, "glFlush");
1914
1915 return &dri2_drv->base;
1916 }