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