egl/dri2: drop NULL checks prior to dri2_destroy_surface
[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 #define WL_HIDE_DEPRECATED
29
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <limits.h>
37 #include <dlfcn.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <c11/threads.h>
42 #include <time.h>
43 #ifdef HAVE_LIBDRM
44 #include <xf86drm.h>
45 #include <drm_fourcc.h>
46 #endif
47 #include <GL/gl.h>
48 #include <GL/internal/dri_interface.h>
49 #include "GL/mesa_glinterop.h"
50 #include <sys/types.h>
51 #include <sys/stat.h>
52
53 #ifdef HAVE_WAYLAND_PLATFORM
54 #include "wayland-drm.h"
55 #include "wayland-drm-client-protocol.h"
56 #endif
57
58 #include "egl_dri2.h"
59 #include "util/u_atomic.h"
60
61 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
62 * some of the definitions here so that building Mesa won't bleeding-edge
63 * kernel headers.
64 */
65 #ifndef DRM_FORMAT_R8
66 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
67 #endif
68
69 #ifndef DRM_FORMAT_RG88
70 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
71 #endif
72
73 #ifndef DRM_FORMAT_GR88
74 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
75 #endif
76
77 const __DRIuseInvalidateExtension use_invalidate = {
78 .base = { __DRI_USE_INVALIDATE, 1 }
79 };
80
81 EGLint dri2_to_egl_attribute_map[] = {
82 0,
83 EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */
84 EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */
85 EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */
86 EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */
87 EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */
88 EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */
89 EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */
90 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
91 EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */
92 EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */
93 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
94 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
95 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
96 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
97 EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
98 EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */
99 0, /* __DRI_ATTRIB_RENDER_TYPE, */
100 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
101 0, /* __DRI_ATTRIB_CONFORMANT */
102 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
103 0, /* __DRI_ATTRIB_STEREO */
104 0, /* __DRI_ATTRIB_AUX_BUFFERS */
105 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
106 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
107 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
108 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
109 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
110 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
111 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
112 0, /* __DRI_ATTRIB_RED_MASK */
113 0, /* __DRI_ATTRIB_GREEN_MASK */
114 0, /* __DRI_ATTRIB_BLUE_MASK */
115 0, /* __DRI_ATTRIB_ALPHA_MASK */
116 EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
117 EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
118 EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
119 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
120 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
121 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
122 0, /* __DRI_ATTRIB_SWAP_METHOD */
123 EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
124 EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
125 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
126 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
127 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
128 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
129 EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */
130 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
131 };
132
133 const __DRIconfig *
134 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
135 EGLenum colorspace)
136 {
137 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
138
139 return surface_type == EGL_WINDOW_BIT ? conf->dri_double_config[srgb] :
140 conf->dri_single_config[srgb];
141 }
142
143 static EGLBoolean
144 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
145 {
146 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
147 return EGL_FALSE;
148
149 if (!_eglMatchConfig(conf, criteria))
150 return EGL_FALSE;
151
152 return EGL_TRUE;
153 }
154
155 struct dri2_egl_config *
156 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
157 EGLint surface_type, const EGLint *attr_list,
158 const unsigned int *rgba_masks)
159 {
160 struct dri2_egl_config *conf;
161 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
162 _EGLConfig base;
163 unsigned int attrib, value, double_buffer;
164 bool srgb = false;
165 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
166 unsigned int dri_masks[4] = { 0, 0, 0, 0 };
167 _EGLConfig *matching_config;
168 EGLint num_configs = 0;
169 EGLint config_id;
170 int i;
171
172 _eglInitConfig(&base, disp, id);
173
174 i = 0;
175 double_buffer = 0;
176 bind_to_texture_rgb = 0;
177 bind_to_texture_rgba = 0;
178
179 while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) {
180 switch (attrib) {
181 case __DRI_ATTRIB_RENDER_TYPE:
182 if (value & __DRI_ATTRIB_RGBA_BIT)
183 value = EGL_RGB_BUFFER;
184 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
185 value = EGL_LUMINANCE_BUFFER;
186 else
187 return NULL;
188 _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value);
189 break;
190
191 case __DRI_ATTRIB_CONFIG_CAVEAT:
192 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
193 value = EGL_NON_CONFORMANT_CONFIG;
194 else if (value & __DRI_ATTRIB_SLOW_BIT)
195 value = EGL_SLOW_CONFIG;
196 else
197 value = EGL_NONE;
198 _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value);
199 break;
200
201 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
202 bind_to_texture_rgb = value;
203 break;
204
205 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
206 bind_to_texture_rgba = value;
207 break;
208
209 case __DRI_ATTRIB_DOUBLE_BUFFER:
210 double_buffer = value;
211 break;
212
213 case __DRI_ATTRIB_RED_MASK:
214 dri_masks[0] = value;
215 break;
216
217 case __DRI_ATTRIB_GREEN_MASK:
218 dri_masks[1] = value;
219 break;
220
221 case __DRI_ATTRIB_BLUE_MASK:
222 dri_masks[2] = value;
223 break;
224
225 case __DRI_ATTRIB_ALPHA_MASK:
226 dri_masks[3] = value;
227 break;
228
229 case __DRI_ATTRIB_ACCUM_RED_SIZE:
230 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
231 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
232 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
233 /* Don't expose visuals with the accumulation buffer. */
234 if (value > 0)
235 return NULL;
236 break;
237
238 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
239 srgb = value != 0;
240 if (!disp->Extensions.KHR_gl_colorspace && srgb)
241 return NULL;
242 break;
243
244 default:
245 key = dri2_to_egl_attribute_map[attrib];
246 if (key != 0)
247 _eglSetConfigKey(&base, key, value);
248 break;
249 }
250 }
251
252 if (attr_list)
253 for (i = 0; attr_list[i] != EGL_NONE; i += 2)
254 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
255
256 if (rgba_masks && memcmp(rgba_masks, dri_masks, sizeof(dri_masks)))
257 return NULL;
258
259 base.NativeRenderable = EGL_TRUE;
260
261 base.SurfaceType = surface_type;
262 if (surface_type & (EGL_PBUFFER_BIT |
263 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
264 base.BindToTextureRGB = bind_to_texture_rgb;
265 if (base.AlphaSize > 0)
266 base.BindToTextureRGBA = bind_to_texture_rgba;
267 }
268
269 base.RenderableType = disp->ClientAPIs;
270 base.Conformant = disp->ClientAPIs;
271
272 base.MinSwapInterval = dri2_dpy->min_swap_interval;
273 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
274
275 if (!_eglValidateConfig(&base, EGL_FALSE)) {
276 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
277 return NULL;
278 }
279
280 config_id = base.ConfigID;
281 base.ConfigID = EGL_DONT_CARE;
282 base.SurfaceType = EGL_DONT_CARE;
283 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
284 (_EGLArrayForEach) dri2_match_config, &base);
285
286 if (num_configs == 1) {
287 conf = (struct dri2_egl_config *) matching_config;
288
289 if (double_buffer && !conf->dri_double_config[srgb])
290 conf->dri_double_config[srgb] = dri_config;
291 else if (!double_buffer && !conf->dri_single_config[srgb])
292 conf->dri_single_config[srgb] = dri_config;
293 else
294 /* a similar config type is already added (unlikely) => discard */
295 return NULL;
296 }
297 else if (num_configs == 0) {
298 conf = calloc(1, sizeof *conf);
299 if (conf == NULL)
300 return NULL;
301
302 if (double_buffer)
303 conf->dri_double_config[srgb] = dri_config;
304 else
305 conf->dri_single_config[srgb] = dri_config;
306
307 memcpy(&conf->base, &base, sizeof base);
308 conf->base.SurfaceType = 0;
309 conf->base.ConfigID = config_id;
310
311 _eglLinkConfig(&conf->base);
312 }
313 else {
314 assert(0);
315 return NULL;
316 }
317
318 if (double_buffer) {
319 surface_type &= ~EGL_PIXMAP_BIT;
320 }
321
322 /* No support for pbuffer + MSAA for now.
323 *
324 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
325 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
326 */
327 if (base.Samples) {
328 surface_type &= ~EGL_PBUFFER_BIT;
329 }
330
331 conf->base.SurfaceType |= surface_type;
332
333 return conf;
334 }
335
336 __DRIimage *
337 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
338 {
339 _EGLDisplay *disp = data;
340 struct dri2_egl_image *dri2_img;
341 _EGLImage *img;
342
343 (void) screen;
344
345 img = _eglLookupImage(image, disp);
346 if (img == NULL) {
347 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
348 return NULL;
349 }
350
351 dri2_img = dri2_egl_image(image);
352
353 return dri2_img->dri_image;
354 }
355
356 const __DRIimageLookupExtension image_lookup_extension = {
357 .base = { __DRI_IMAGE_LOOKUP, 1 },
358
359 .lookupEGLImage = dri2_lookup_egl_image
360 };
361
362 struct dri2_extension_match {
363 const char *name;
364 int version;
365 int offset;
366 };
367
368 static struct dri2_extension_match dri3_driver_extensions[] = {
369 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
370 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
371 { NULL, 0, 0 }
372 };
373
374 static struct dri2_extension_match dri2_driver_extensions[] = {
375 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
376 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
377 { NULL, 0, 0 }
378 };
379
380 static struct dri2_extension_match dri2_core_extensions[] = {
381 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
382 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
383 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
384 { NULL, 0, 0 }
385 };
386
387 static struct dri2_extension_match swrast_driver_extensions[] = {
388 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
389 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
390 { NULL, 0, 0 }
391 };
392
393 static struct dri2_extension_match swrast_core_extensions[] = {
394 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
395 { NULL, 0, 0 }
396 };
397
398 static EGLBoolean
399 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
400 struct dri2_extension_match *matches,
401 const __DRIextension **extensions)
402 {
403 int i, j, ret = EGL_TRUE;
404 void *field;
405
406 for (i = 0; extensions[i]; i++) {
407 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
408 for (j = 0; matches[j].name; j++) {
409 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
410 extensions[i]->version >= matches[j].version) {
411 field = ((char *) dri2_dpy + matches[j].offset);
412 *(const __DRIextension **) field = extensions[i];
413 _eglLog(_EGL_INFO, "found extension %s version %d",
414 extensions[i]->name, extensions[i]->version);
415 }
416 }
417 }
418
419 for (j = 0; matches[j].name; j++) {
420 field = ((char *) dri2_dpy + matches[j].offset);
421 if (*(const __DRIextension **) field == NULL) {
422 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
423 matches[j].name, matches[j].version);
424 ret = EGL_FALSE;
425 }
426 }
427
428 return ret;
429 }
430
431 static const __DRIextension **
432 dri2_open_driver(_EGLDisplay *disp)
433 {
434 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
435 const __DRIextension **extensions = NULL;
436 char path[PATH_MAX], *search_paths, *p, *next, *end;
437 char *get_extensions_name;
438 const __DRIextension **(*get_extensions)(void);
439
440 search_paths = NULL;
441 if (geteuid() == getuid()) {
442 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
443 search_paths = getenv("LIBGL_DRIVERS_PATH");
444 }
445 if (search_paths == NULL)
446 search_paths = DEFAULT_DRIVER_DIR;
447
448 dri2_dpy->driver = NULL;
449 end = search_paths + strlen(search_paths);
450 for (p = search_paths; p < end; p = next + 1) {
451 int len;
452 next = strchr(p, ':');
453 if (next == NULL)
454 next = end;
455
456 len = next - p;
457 #if GLX_USE_TLS
458 snprintf(path, sizeof path,
459 "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name);
460 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
461 #endif
462 if (dri2_dpy->driver == NULL) {
463 snprintf(path, sizeof path,
464 "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name);
465 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
466 if (dri2_dpy->driver == NULL)
467 _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror());
468 }
469 /* not need continue to loop all paths once the driver is found */
470 if (dri2_dpy->driver != NULL)
471 break;
472
473 #ifdef ANDROID
474 snprintf(path, sizeof path, "%.*s/gallium_dri.so", len, p);
475 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
476 if (dri2_dpy->driver == NULL)
477 _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror());
478 else
479 break;
480 #endif
481 }
482
483 if (dri2_dpy->driver == NULL) {
484 _eglLog(_EGL_WARNING,
485 "DRI2: failed to open %s (search paths %s)",
486 dri2_dpy->driver_name, search_paths);
487 return NULL;
488 }
489
490 _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path);
491
492 if (asprintf(&get_extensions_name, "%s_%s",
493 __DRI_DRIVER_GET_EXTENSIONS, dri2_dpy->driver_name) != -1) {
494 get_extensions = dlsym(dri2_dpy->driver, get_extensions_name);
495 if (get_extensions) {
496 extensions = get_extensions();
497 } else {
498 _eglLog(_EGL_DEBUG, "driver does not expose %s(): %s\n",
499 get_extensions_name, dlerror());
500 }
501 free(get_extensions_name);
502 }
503
504 if (!extensions)
505 extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS);
506 if (extensions == NULL) {
507 _eglLog(_EGL_WARNING,
508 "DRI2: driver exports no extensions (%s)", dlerror());
509 dlclose(dri2_dpy->driver);
510 }
511
512 return extensions;
513 }
514
515 EGLBoolean
516 dri2_load_driver_dri3(_EGLDisplay *disp)
517 {
518 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
519 const __DRIextension **extensions;
520
521 extensions = dri2_open_driver(disp);
522 if (!extensions)
523 return EGL_FALSE;
524
525 if (!dri2_bind_extensions(dri2_dpy, dri3_driver_extensions, extensions)) {
526 dlclose(dri2_dpy->driver);
527 return EGL_FALSE;
528 }
529 dri2_dpy->driver_extensions = extensions;
530
531 return EGL_TRUE;
532 }
533
534 EGLBoolean
535 dri2_load_driver(_EGLDisplay *disp)
536 {
537 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
538 const __DRIextension **extensions;
539
540 extensions = dri2_open_driver(disp);
541 if (!extensions)
542 return EGL_FALSE;
543
544 if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) {
545 dlclose(dri2_dpy->driver);
546 return EGL_FALSE;
547 }
548 dri2_dpy->driver_extensions = extensions;
549
550 return EGL_TRUE;
551 }
552
553 EGLBoolean
554 dri2_load_driver_swrast(_EGLDisplay *disp)
555 {
556 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
557 const __DRIextension **extensions;
558
559 extensions = dri2_open_driver(disp);
560 if (!extensions)
561 return EGL_FALSE;
562
563 if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) {
564 dlclose(dri2_dpy->driver);
565 return EGL_FALSE;
566 }
567 dri2_dpy->driver_extensions = extensions;
568
569 return EGL_TRUE;
570 }
571
572 static unsigned
573 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
574 {
575 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
576 unsigned int value = 0;
577
578 if (!rendererQuery ||
579 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
580 return 0;
581
582 return value;
583 }
584
585 void
586 dri2_setup_screen(_EGLDisplay *disp)
587 {
588 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
589 unsigned int api_mask;
590
591 if (dri2_dpy->image_driver) {
592 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
593 } else if (dri2_dpy->dri2) {
594 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
595 } else {
596 assert(dri2_dpy->swrast);
597 api_mask = 1 << __DRI_API_OPENGL |
598 1 << __DRI_API_GLES |
599 1 << __DRI_API_GLES2 |
600 1 << __DRI_API_GLES3;
601 }
602
603 disp->ClientAPIs = 0;
604 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
605 disp->ClientAPIs |= EGL_OPENGL_BIT;
606 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
607 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
608 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
609 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
610 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
611 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
612
613 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
614 disp->Extensions.KHR_no_config_context = EGL_TRUE;
615 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
616
617 if (dri2_renderer_query_integer(dri2_dpy,
618 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
619 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
620
621 if (dri2_dpy->image_driver ||
622 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
623 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
624 disp->Extensions.KHR_create_context = EGL_TRUE;
625
626 if (dri2_dpy->robustness)
627 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
628 }
629
630 if (dri2_dpy->fence) {
631 disp->Extensions.KHR_fence_sync = EGL_TRUE;
632 disp->Extensions.KHR_wait_sync = EGL_TRUE;
633 if (dri2_dpy->fence->get_fence_from_cl_event)
634 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
635 }
636
637 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
638
639 if (dri2_dpy->image) {
640 if (dri2_dpy->image->base.version >= 10 &&
641 dri2_dpy->image->getCapabilities != NULL) {
642 int capabilities;
643
644 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
645 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
646
647 if (dri2_dpy->image->base.version >= 11)
648 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
649 } else {
650 disp->Extensions.MESA_drm_image = EGL_TRUE;
651 if (dri2_dpy->image->base.version >= 11)
652 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
653 }
654
655 disp->Extensions.KHR_image_base = EGL_TRUE;
656 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
657 if (dri2_dpy->image->base.version >= 5 &&
658 dri2_dpy->image->createImageFromTexture) {
659 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
660 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
661 }
662 if (dri2_renderer_query_integer(dri2_dpy,
663 __DRI2_RENDERER_HAS_TEXTURE_3D))
664 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
665 #ifdef HAVE_LIBDRM
666 if (dri2_dpy->image->base.version >= 8 &&
667 dri2_dpy->image->createImageFromDmaBufs) {
668 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
669 }
670 #endif
671 }
672 }
673
674 /* All platforms but DRM call this function to create the screen, query the
675 * dri extensions, setup the vtables and populate the driver_configs.
676 * DRM inherits all that information from its display - GBM.
677 */
678 EGLBoolean
679 dri2_create_screen(_EGLDisplay *disp)
680 {
681 const __DRIextension **extensions;
682 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
683 unsigned i;
684
685 if (dri2_dpy->image_driver) {
686 dri2_dpy->dri_screen =
687 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
688 dri2_dpy->extensions,
689 dri2_dpy->driver_extensions,
690 &dri2_dpy->driver_configs,
691 disp);
692 } else if (dri2_dpy->dri2) {
693 if (dri2_dpy->dri2->base.version >= 4) {
694 dri2_dpy->dri_screen =
695 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
696 dri2_dpy->extensions,
697 dri2_dpy->driver_extensions,
698 &dri2_dpy->driver_configs, disp);
699 } else {
700 dri2_dpy->dri_screen =
701 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
702 dri2_dpy->extensions,
703 &dri2_dpy->driver_configs, disp);
704 }
705 } else {
706 assert(dri2_dpy->swrast);
707 if (dri2_dpy->swrast->base.version >= 4) {
708 dri2_dpy->dri_screen =
709 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->extensions,
710 dri2_dpy->driver_extensions,
711 &dri2_dpy->driver_configs, disp);
712 } else {
713 dri2_dpy->dri_screen =
714 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions,
715 &dri2_dpy->driver_configs, disp);
716 }
717 }
718
719 if (dri2_dpy->dri_screen == NULL) {
720 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
721 return EGL_FALSE;
722 }
723
724 dri2_dpy->own_dri_screen = 1;
725
726 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
727
728 if (dri2_dpy->image_driver || dri2_dpy->dri2) {
729 if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
730 goto cleanup_dri_screen;
731 } else {
732 assert(dri2_dpy->swrast);
733 if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions))
734 goto cleanup_dri_screen;
735 }
736
737 for (i = 0; extensions[i]; i++) {
738 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) {
739 dri2_dpy->robustness = (__DRIrobustnessExtension *) extensions[i];
740 }
741 if (strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0) {
742 dri2_dpy->config = (__DRI2configQueryExtension *) extensions[i];
743 }
744 if (strcmp(extensions[i]->name, __DRI2_FENCE) == 0) {
745 dri2_dpy->fence = (__DRI2fenceExtension *) extensions[i];
746 }
747 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
748 dri2_dpy->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
749 }
750 if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
751 dri2_dpy->interop = (__DRI2interopExtension *) extensions[i];
752 }
753
754 dri2_setup_screen(disp);
755
756 return EGL_TRUE;
757
758 cleanup_dri_screen:
759 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
760
761 return EGL_FALSE;
762 }
763
764 /**
765 * Called via eglInitialize(), GLX_drv->API.Initialize().
766 *
767 * This must be guaranteed to be called exactly once, even if eglInitialize is
768 * called many times (without a eglTerminate in between).
769 */
770 static EGLBoolean
771 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp)
772 {
773 EGLBoolean ret = EGL_FALSE;
774 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
775
776 /* In the case where the application calls eglMakeCurrent(context1),
777 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
778 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
779 * initialized, as we need it to be able to free context1 correctly.
780 *
781 * It would probably be safest to forcibly release the display with
782 * dri2_display_release, to make sure the display is reinitialized correctly.
783 * However, the EGL spec states that we need to keep a reference to the
784 * current context (so we cannot call dri2_make_current(NULL)), and therefore
785 * we would leak context1 as we would be missing the old display connection
786 * to free it up correctly.
787 */
788 if (dri2_dpy) {
789 dri2_dpy->ref_count++;
790 return EGL_TRUE;
791 }
792
793 /* not until swrast_dri is supported */
794 if (disp->Options.UseFallback)
795 return EGL_FALSE;
796
797 /* Nothing to initialize for a test only display */
798 if (disp->Options.TestOnly)
799 return EGL_TRUE;
800
801 switch (disp->Platform) {
802 #ifdef HAVE_SURFACELESS_PLATFORM
803 case _EGL_PLATFORM_SURFACELESS:
804 ret = dri2_initialize_surfaceless(drv, disp);
805 break;
806 #endif
807 #ifdef HAVE_X11_PLATFORM
808 case _EGL_PLATFORM_X11:
809 ret = dri2_initialize_x11(drv, disp);
810 break;
811 #endif
812 #ifdef HAVE_DRM_PLATFORM
813 case _EGL_PLATFORM_DRM:
814 ret = dri2_initialize_drm(drv, disp);
815 break;
816 #endif
817 #ifdef HAVE_WAYLAND_PLATFORM
818 case _EGL_PLATFORM_WAYLAND:
819 ret = dri2_initialize_wayland(drv, disp);
820 break;
821 #endif
822 #ifdef HAVE_ANDROID_PLATFORM
823 case _EGL_PLATFORM_ANDROID:
824 ret = dri2_initialize_android(drv, disp);
825 break;
826 #endif
827 default:
828 _eglLog(_EGL_WARNING, "No EGL platform enabled.");
829 return EGL_FALSE;
830 }
831
832 if (ret) {
833 dri2_dpy = dri2_egl_display(disp);
834
835 if (!dri2_dpy) {
836 return EGL_FALSE;
837 }
838
839 dri2_dpy->ref_count++;
840 }
841
842 return ret;
843 }
844
845 /**
846 * Decrement display reference count, and free up display if necessary.
847 */
848 static void
849 dri2_display_release(_EGLDisplay *disp)
850 {
851 struct dri2_egl_display *dri2_dpy;
852 unsigned i;
853
854 if (!disp)
855 return;
856
857 dri2_dpy = dri2_egl_display(disp);
858
859 assert(dri2_dpy->ref_count > 0);
860 dri2_dpy->ref_count--;
861
862 if (dri2_dpy->ref_count > 0)
863 return;
864
865 _eglCleanupDisplay(disp);
866
867 if (dri2_dpy->own_dri_screen)
868 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
869 if (dri2_dpy->fd >= 0)
870 close(dri2_dpy->fd);
871 if (dri2_dpy->driver)
872 dlclose(dri2_dpy->driver);
873 free(dri2_dpy->driver_name);
874
875 #ifdef HAVE_WAYLAND_PLATFORM
876 free(dri2_dpy->device_name);
877 #endif
878
879 switch (disp->Platform) {
880 #ifdef HAVE_X11_PLATFORM
881 case _EGL_PLATFORM_X11:
882 if (dri2_dpy->own_device) {
883 xcb_disconnect(dri2_dpy->conn);
884 }
885 break;
886 #endif
887 #ifdef HAVE_DRM_PLATFORM
888 case _EGL_PLATFORM_DRM:
889 if (dri2_dpy->own_device) {
890 gbm_device_destroy(&dri2_dpy->gbm_dri->base.base);
891 }
892 break;
893 #endif
894 #ifdef HAVE_WAYLAND_PLATFORM
895 case _EGL_PLATFORM_WAYLAND:
896 if (dri2_dpy->wl_drm)
897 wl_drm_destroy(dri2_dpy->wl_drm);
898 if (dri2_dpy->wl_shm)
899 wl_shm_destroy(dri2_dpy->wl_shm);
900 wl_registry_destroy(dri2_dpy->wl_registry);
901 wl_event_queue_destroy(dri2_dpy->wl_queue);
902 if (dri2_dpy->own_device) {
903 wl_display_disconnect(dri2_dpy->wl_dpy);
904 }
905 break;
906 #endif
907 default:
908 break;
909 }
910
911 /* The drm platform does not create the screen/driver_configs but reuses
912 * the ones from the gbm device. As such the gbm itself is responsible
913 * for the cleanup.
914 */
915 if (disp->Platform != _EGL_PLATFORM_DRM) {
916 for (i = 0; dri2_dpy->driver_configs[i]; i++)
917 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
918 free(dri2_dpy->driver_configs);
919 }
920 free(dri2_dpy);
921 disp->DriverData = NULL;
922 }
923
924 /**
925 * Called via eglTerminate(), drv->API.Terminate().
926 *
927 * This must be guaranteed to be called exactly once, even if eglTerminate is
928 * called many times (without a eglInitialize in between).
929 */
930 static EGLBoolean
931 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
932 {
933 /* Release all non-current Context/Surfaces. */
934 _eglReleaseDisplayResources(drv, disp);
935
936 dri2_display_release(disp);
937
938 return EGL_TRUE;
939 }
940
941 /**
942 * Set the error code after a call to
943 * dri2_egl_display::dri2::createContextAttribs.
944 */
945 static void
946 dri2_create_context_attribs_error(int dri_error)
947 {
948 EGLint egl_error;
949
950 switch (dri_error) {
951 case __DRI_CTX_ERROR_SUCCESS:
952 return;
953
954 case __DRI_CTX_ERROR_NO_MEMORY:
955 egl_error = EGL_BAD_ALLOC;
956 break;
957
958 /* From the EGL_KHR_create_context spec, section "Errors":
959 *
960 * * If <config> does not support a client API context compatible
961 * with the requested API major and minor version, [...] context flags,
962 * and context reset notification behavior (for client API types where
963 * these attributes are supported), then an EGL_BAD_MATCH error is
964 * generated.
965 *
966 * * If an OpenGL ES context is requested and the values for
967 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
968 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
969 * is not defined, than an EGL_BAD_MATCH error is generated.
970 *
971 * * If an OpenGL context is requested, the requested version is
972 * greater than 3.2, and the value for attribute
973 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
974 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
975 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
976 * one of these bits set; or if the implementation does not support
977 * the requested profile, then an EGL_BAD_MATCH error is generated.
978 */
979 case __DRI_CTX_ERROR_BAD_API:
980 case __DRI_CTX_ERROR_BAD_VERSION:
981 case __DRI_CTX_ERROR_BAD_FLAG:
982 egl_error = EGL_BAD_MATCH;
983 break;
984
985 /* From the EGL_KHR_create_context spec, section "Errors":
986 *
987 * * If an attribute name or attribute value in <attrib_list> is not
988 * recognized (including unrecognized bits in bitmask attributes),
989 * then an EGL_BAD_ATTRIBUTE error is generated."
990 */
991 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
992 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
993 egl_error = EGL_BAD_ATTRIBUTE;
994 break;
995
996 default:
997 assert(0);
998 egl_error = EGL_BAD_MATCH;
999 break;
1000 }
1001
1002 _eglError(egl_error, "dri2_create_context");
1003 }
1004
1005 static bool
1006 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1007 struct dri2_egl_display *dri2_dpy,
1008 uint32_t *ctx_attribs,
1009 unsigned *num_attribs)
1010 {
1011 int pos = 0;
1012
1013 assert(*num_attribs >= 8);
1014
1015 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1016 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1017 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1018 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1019
1020 if (dri2_ctx->base.Flags != 0) {
1021 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1022 * extension, don't even try to send it the robust-access flag.
1023 * It may explode. Instead, generate the required EGL error here.
1024 */
1025 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1026 && !dri2_dpy->robustness) {
1027 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1028 return false;
1029 }
1030
1031 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1032 ctx_attribs[pos++] = dri2_ctx->base.Flags;
1033 }
1034
1035 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1036 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1037 * extension, don't even try to send it a reset strategy. It may
1038 * explode. Instead, generate the required EGL error here.
1039 */
1040 if (!dri2_dpy->robustness) {
1041 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1042 return false;
1043 }
1044
1045 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1046 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1047 }
1048
1049 *num_attribs = pos;
1050
1051 return true;
1052 }
1053
1054 /**
1055 * Called via eglCreateContext(), drv->API.CreateContext().
1056 */
1057 static _EGLContext *
1058 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
1059 _EGLContext *share_list, const EGLint *attrib_list)
1060 {
1061 struct dri2_egl_context *dri2_ctx;
1062 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1063 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1064 __DRIcontext *shared =
1065 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1066 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1067 const __DRIconfig *dri_config;
1068 int api;
1069
1070 (void) drv;
1071
1072 dri2_ctx = malloc(sizeof *dri2_ctx);
1073 if (!dri2_ctx) {
1074 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1075 return NULL;
1076 }
1077
1078 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1079 goto cleanup;
1080
1081 switch (dri2_ctx->base.ClientAPI) {
1082 case EGL_OPENGL_ES_API:
1083 switch (dri2_ctx->base.ClientMajorVersion) {
1084 case 1:
1085 api = __DRI_API_GLES;
1086 break;
1087 case 2:
1088 api = __DRI_API_GLES2;
1089 break;
1090 case 3:
1091 api = __DRI_API_GLES3;
1092 break;
1093 default:
1094 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1095 free(dri2_ctx);
1096 return NULL;
1097 }
1098 break;
1099 case EGL_OPENGL_API:
1100 if ((dri2_ctx->base.ClientMajorVersion >= 4
1101 || (dri2_ctx->base.ClientMajorVersion == 3
1102 && dri2_ctx->base.ClientMinorVersion >= 2))
1103 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1104 api = __DRI_API_OPENGL_CORE;
1105 else
1106 api = __DRI_API_OPENGL;
1107 break;
1108 default:
1109 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1110 free(dri2_ctx);
1111 return NULL;
1112 }
1113
1114 if (conf != NULL) {
1115 /* The config chosen here isn't necessarily
1116 * used for surfaces later.
1117 * A pixmap surface will use the single config.
1118 * This opportunity depends on disabling the
1119 * doubleBufferMode check in
1120 * src/mesa/main/context.c:check_compatible()
1121 */
1122 if (dri2_config->dri_double_config[0])
1123 dri_config = dri2_config->dri_double_config[0];
1124 else
1125 dri_config = dri2_config->dri_single_config[0];
1126
1127 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1128 * makes sure the back buffer will always be used.
1129 */
1130 if (conf->SurfaceType & EGL_WINDOW_BIT)
1131 dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER;
1132 }
1133 else
1134 dri_config = NULL;
1135
1136 if (dri2_dpy->image_driver) {
1137 unsigned error;
1138 unsigned num_attribs = 8;
1139 uint32_t ctx_attribs[8];
1140
1141 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1142 &num_attribs))
1143 goto cleanup;
1144
1145 dri2_ctx->dri_context =
1146 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1147 api,
1148 dri_config,
1149 shared,
1150 num_attribs / 2,
1151 ctx_attribs,
1152 & error,
1153 dri2_ctx);
1154 dri2_create_context_attribs_error(error);
1155 } else if (dri2_dpy->dri2) {
1156 if (dri2_dpy->dri2->base.version >= 3) {
1157 unsigned error;
1158 unsigned num_attribs = 8;
1159 uint32_t ctx_attribs[8];
1160
1161 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1162 &num_attribs))
1163 goto cleanup;
1164
1165 dri2_ctx->dri_context =
1166 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1167 api,
1168 dri_config,
1169 shared,
1170 num_attribs / 2,
1171 ctx_attribs,
1172 & error,
1173 dri2_ctx);
1174 dri2_create_context_attribs_error(error);
1175 } else {
1176 dri2_ctx->dri_context =
1177 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1178 api,
1179 dri_config,
1180 shared,
1181 dri2_ctx);
1182 }
1183 } else {
1184 assert(dri2_dpy->swrast);
1185 if (dri2_dpy->swrast->base.version >= 3) {
1186 unsigned error;
1187 unsigned num_attribs = 8;
1188 uint32_t ctx_attribs[8];
1189
1190 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1191 &num_attribs))
1192 goto cleanup;
1193
1194 dri2_ctx->dri_context =
1195 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1196 api,
1197 dri_config,
1198 shared,
1199 num_attribs / 2,
1200 ctx_attribs,
1201 & error,
1202 dri2_ctx);
1203 dri2_create_context_attribs_error(error);
1204 } else {
1205 dri2_ctx->dri_context =
1206 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1207 api,
1208 dri_config,
1209 shared,
1210 dri2_ctx);
1211 }
1212 }
1213
1214 if (!dri2_ctx->dri_context)
1215 goto cleanup;
1216
1217 return &dri2_ctx->base;
1218
1219 cleanup:
1220 free(dri2_ctx);
1221 return NULL;
1222 }
1223
1224 /**
1225 * Called via eglDestroyContext(), drv->API.DestroyContext().
1226 */
1227 static EGLBoolean
1228 dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1229 {
1230 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1231 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1232
1233 if (_eglPutContext(ctx)) {
1234 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1235 free(dri2_ctx);
1236 }
1237
1238 return EGL_TRUE;
1239 }
1240
1241 static EGLBoolean
1242 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
1243 {
1244 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1245
1246 if (!_eglPutSurface(surf))
1247 return EGL_TRUE;
1248
1249 return dri2_dpy->vtbl->destroy_surface(drv, dpy, surf);
1250 }
1251
1252 /**
1253 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1254 */
1255 static EGLBoolean
1256 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
1257 _EGLSurface *rsurf, _EGLContext *ctx)
1258 {
1259 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1260 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1261 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1262 _EGLContext *old_ctx;
1263 _EGLSurface *old_dsurf, *old_rsurf;
1264 _EGLSurface *tmp_dsurf, *tmp_rsurf;
1265 __DRIdrawable *ddraw, *rdraw;
1266 __DRIcontext *cctx;
1267 EGLBoolean unbind;
1268
1269 if (!dri2_dpy)
1270 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1271
1272 /* make new bindings */
1273 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) {
1274 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1275 return EGL_FALSE;
1276 }
1277
1278 /* flush before context switch */
1279 if (old_ctx)
1280 dri2_drv->glFlush();
1281
1282 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1283 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1284 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1285
1286 if (old_ctx) {
1287 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1288 dri2_dpy->core->unbindContext(old_cctx);
1289 }
1290
1291 unbind = (cctx == NULL && ddraw == NULL && rdraw == NULL);
1292
1293 if (unbind || dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1294 dri2_destroy_surface(drv, disp, old_dsurf);
1295 dri2_destroy_surface(drv, disp, old_rsurf);
1296
1297 if (!unbind)
1298 dri2_dpy->ref_count++;
1299 if (old_ctx) {
1300 EGLDisplay old_disp = _eglGetDisplayHandle(old_ctx->Resource.Display);
1301 dri2_destroy_context(drv, disp, old_ctx);
1302 dri2_display_release(old_disp);
1303 }
1304
1305 return EGL_TRUE;
1306 } else {
1307 /* undo the previous _eglBindContext */
1308 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1309 assert(&dri2_ctx->base == ctx &&
1310 tmp_dsurf == dsurf &&
1311 tmp_rsurf == rsurf);
1312
1313 _eglPutSurface(dsurf);
1314 _eglPutSurface(rsurf);
1315 _eglPutContext(ctx);
1316
1317 _eglPutSurface(old_dsurf);
1318 _eglPutSurface(old_rsurf);
1319 _eglPutContext(old_ctx);
1320
1321 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1322 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1323 * as EGL_SUCCESS.
1324 */
1325 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
1326 }
1327 }
1328
1329 __DRIdrawable *
1330 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1331 {
1332 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1333
1334 return dri2_surf->dri_drawable;
1335 }
1336
1337 /*
1338 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1339 */
1340 static _EGLProc
1341 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
1342 {
1343 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1344
1345 return dri2_drv->get_proc_address(procname);
1346 }
1347
1348 static _EGLSurface*
1349 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1350 _EGLConfig *conf, void *native_window,
1351 const EGLint *attrib_list)
1352 {
1353 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1354 return dri2_dpy->vtbl->create_window_surface(drv, dpy, conf, native_window,
1355 attrib_list);
1356 }
1357
1358 static _EGLSurface*
1359 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1360 _EGLConfig *conf, void *native_pixmap,
1361 const EGLint *attrib_list)
1362 {
1363 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1364 return dri2_dpy->vtbl->create_pixmap_surface(drv, dpy, conf, native_pixmap,
1365 attrib_list);
1366 }
1367
1368 static _EGLSurface*
1369 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1370 _EGLConfig *conf, const EGLint *attrib_list)
1371 {
1372 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1373 return dri2_dpy->vtbl->create_pbuffer_surface(drv, dpy, conf, attrib_list);
1374 }
1375
1376 static EGLBoolean
1377 dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1378 EGLint interval)
1379 {
1380 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1381 return dri2_dpy->vtbl->swap_interval(drv, dpy, surf, interval);
1382 }
1383
1384 /**
1385 * Asks the client API to flush any rendering to the drawable so that we can
1386 * do our swapbuffers.
1387 */
1388 void
1389 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1390 {
1391 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1392 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1393
1394 if (dri2_dpy->flush) {
1395 if (dri2_dpy->flush->base.version >= 4) {
1396 /* We know there's a current context because:
1397 *
1398 * "If surface is not bound to the calling thread’s current
1399 * context, an EGL_BAD_SURFACE error is generated."
1400 */
1401 _EGLContext *ctx = _eglGetCurrentContext();
1402 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1403
1404 /* From the EGL 1.4 spec (page 52):
1405 *
1406 * "The contents of ancillary buffers are always undefined
1407 * after calling eglSwapBuffers."
1408 */
1409 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1410 dri_drawable,
1411 __DRI2_FLUSH_DRAWABLE |
1412 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1413 __DRI2_THROTTLE_SWAPBUFFER);
1414 } else {
1415 dri2_dpy->flush->flush(dri_drawable);
1416 }
1417 }
1418 }
1419
1420 static EGLBoolean
1421 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
1422 {
1423 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1424 return dri2_dpy->vtbl->swap_buffers(drv, dpy, surf);
1425 }
1426
1427 static EGLBoolean
1428 dri2_swap_buffers_with_damage(_EGLDriver *drv, _EGLDisplay *dpy,
1429 _EGLSurface *surf,
1430 const EGLint *rects, EGLint n_rects)
1431 {
1432 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1433 return dri2_dpy->vtbl->swap_buffers_with_damage(drv, dpy, surf,
1434 rects, n_rects);
1435 }
1436
1437 static EGLBoolean
1438 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1439 EGLint numRects, const EGLint *rects)
1440 {
1441 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1442 return dri2_dpy->vtbl->swap_buffers_region(drv, dpy, surf, numRects, rects);
1443 }
1444
1445 static EGLBoolean
1446 dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1447 EGLint x, EGLint y, EGLint width, EGLint height)
1448 {
1449 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1450 return dri2_dpy->vtbl->post_sub_buffer(drv, dpy, surf, x, y, width, height);
1451 }
1452
1453 static EGLBoolean
1454 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1455 void *native_pixmap_target)
1456 {
1457 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1458 return dri2_dpy->vtbl->copy_buffers(drv, dpy, surf, native_pixmap_target);
1459 }
1460
1461 static EGLint
1462 dri2_query_buffer_age(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
1463 {
1464 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1465 return dri2_dpy->vtbl->query_buffer_age(drv, dpy, surf);
1466 }
1467
1468 static EGLBoolean
1469 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1470 {
1471 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1472 _EGLSurface *surf = ctx->DrawSurface;
1473 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1474
1475 (void) drv;
1476
1477 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1478 * we need to copy fake to real here.*/
1479
1480 if (dri2_dpy->flush != NULL)
1481 dri2_dpy->flush->flush(dri_drawable);
1482
1483 return EGL_TRUE;
1484 }
1485
1486 static EGLBoolean
1487 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
1488 {
1489 (void) drv;
1490 (void) disp;
1491
1492 if (engine != EGL_CORE_NATIVE_ENGINE)
1493 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1494 /* glXWaitX(); */
1495
1496 return EGL_TRUE;
1497 }
1498
1499 static EGLBoolean
1500 dri2_bind_tex_image(_EGLDriver *drv,
1501 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1502 {
1503 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1504 struct dri2_egl_context *dri2_ctx;
1505 _EGLContext *ctx;
1506 GLint format, target;
1507 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1508
1509 ctx = _eglGetCurrentContext();
1510 dri2_ctx = dri2_egl_context(ctx);
1511
1512 if (!_eglBindTexImage(drv, disp, surf, buffer))
1513 return EGL_FALSE;
1514
1515 switch (surf->TextureFormat) {
1516 case EGL_TEXTURE_RGB:
1517 format = __DRI_TEXTURE_FORMAT_RGB;
1518 break;
1519 case EGL_TEXTURE_RGBA:
1520 format = __DRI_TEXTURE_FORMAT_RGBA;
1521 break;
1522 default:
1523 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1524 format = __DRI_TEXTURE_FORMAT_RGBA;
1525 }
1526
1527 switch (surf->TextureTarget) {
1528 case EGL_TEXTURE_2D:
1529 target = GL_TEXTURE_2D;
1530 break;
1531 default:
1532 target = GL_TEXTURE_2D;
1533 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1534 }
1535
1536 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1537 target, format,
1538 dri_drawable);
1539
1540 return EGL_TRUE;
1541 }
1542
1543 static EGLBoolean
1544 dri2_release_tex_image(_EGLDriver *drv,
1545 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1546 {
1547 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1548 struct dri2_egl_context *dri2_ctx;
1549 _EGLContext *ctx;
1550 GLint target;
1551 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1552
1553 ctx = _eglGetCurrentContext();
1554 dri2_ctx = dri2_egl_context(ctx);
1555
1556 if (!_eglReleaseTexImage(drv, disp, surf, buffer))
1557 return EGL_FALSE;
1558
1559 switch (surf->TextureTarget) {
1560 case EGL_TEXTURE_2D:
1561 target = GL_TEXTURE_2D;
1562 break;
1563 default:
1564 assert(0);
1565 }
1566
1567 if (dri2_dpy->tex_buffer->base.version >= 3 &&
1568 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
1569 (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context,
1570 target,
1571 dri_drawable);
1572 }
1573
1574 return EGL_TRUE;
1575 }
1576
1577 static _EGLImage*
1578 dri2_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
1579 EGLenum target, EGLClientBuffer buffer,
1580 const EGLint *attr_list)
1581 {
1582 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1583 return dri2_dpy->vtbl->create_image(drv, dpy, ctx, target, buffer,
1584 attr_list);
1585 }
1586
1587 static _EGLImage *
1588 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
1589 {
1590 struct dri2_egl_image *dri2_img;
1591
1592 if (dri_image == NULL) {
1593 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1594 return NULL;
1595 }
1596
1597 dri2_img = malloc(sizeof *dri2_img);
1598 if (!dri2_img) {
1599 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1600 return NULL;
1601 }
1602
1603 if (!_eglInitImage(&dri2_img->base, disp)) {
1604 free(dri2_img);
1605 return NULL;
1606 }
1607
1608 dri2_img->dri_image = dri_image;
1609
1610 return &dri2_img->base;
1611 }
1612
1613 static _EGLImage *
1614 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1615 EGLClientBuffer buffer,
1616 const EGLint *attr_list)
1617 {
1618 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1619 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1620 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
1621 __DRIimage *dri_image;
1622
1623 if (renderbuffer == 0) {
1624 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1625 return EGL_NO_IMAGE_KHR;
1626 }
1627
1628 dri_image =
1629 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1630 renderbuffer, NULL);
1631
1632 return dri2_create_image_from_dri(disp, dri_image);
1633 }
1634
1635 #ifdef HAVE_WAYLAND_PLATFORM
1636
1637 /* This structure describes how a wl_buffer maps to one or more
1638 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1639 * offsets and strides of the planes in the buffer. This table maps a
1640 * wl_drm format code to a description of the planes in the buffer
1641 * that lets us create a __DRIimage for each of the planes. */
1642
1643 static const struct wl_drm_components_descriptor {
1644 uint32_t dri_components;
1645 EGLint components;
1646 int nplanes;
1647 } wl_drm_components[] = {
1648 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
1649 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
1650 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
1651 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
1652 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
1653 };
1654
1655 static _EGLImage *
1656 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1657 EGLClientBuffer _buffer,
1658 const EGLint *attr_list)
1659 {
1660 struct wl_drm_buffer *buffer;
1661 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1662 const struct wl_drm_components_descriptor *f;
1663 __DRIimage *dri_image;
1664 _EGLImageAttribs attrs;
1665 EGLint err;
1666 int32_t plane;
1667
1668 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
1669 (struct wl_resource *) _buffer);
1670 if (!buffer)
1671 return NULL;
1672
1673 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1674 plane = attrs.PlaneWL;
1675 if (err != EGL_SUCCESS) {
1676 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1677 return NULL;
1678 }
1679
1680 f = buffer->driver_format;
1681 if (plane < 0 || plane >= f->nplanes) {
1682 _eglError(EGL_BAD_PARAMETER,
1683 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1684 return NULL;
1685 }
1686
1687 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
1688
1689 if (dri_image == NULL) {
1690 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1691 return NULL;
1692 }
1693
1694 return dri2_create_image_from_dri(disp, dri_image);
1695 }
1696 #endif
1697
1698 static EGLBoolean
1699 dri2_get_sync_values_chromium(_EGLDisplay *dpy, _EGLSurface *surf,
1700 EGLuint64KHR *ust, EGLuint64KHR *msc,
1701 EGLuint64KHR *sbc)
1702 {
1703 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1704 return dri2_dpy->vtbl->get_sync_values(dpy, surf, ust, msc, sbc);
1705 }
1706
1707 /**
1708 * Set the error code after a call to
1709 * dri2_egl_image::dri_image::createImageFromTexture.
1710 */
1711 static void
1712 dri2_create_image_khr_texture_error(int dri_error)
1713 {
1714 EGLint egl_error;
1715
1716 switch (dri_error) {
1717 case __DRI_IMAGE_ERROR_SUCCESS:
1718 return;
1719
1720 case __DRI_IMAGE_ERROR_BAD_ALLOC:
1721 egl_error = EGL_BAD_ALLOC;
1722 break;
1723
1724 case __DRI_IMAGE_ERROR_BAD_MATCH:
1725 egl_error = EGL_BAD_MATCH;
1726 break;
1727
1728 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
1729 egl_error = EGL_BAD_PARAMETER;
1730 break;
1731
1732 case __DRI_IMAGE_ERROR_BAD_ACCESS:
1733 egl_error = EGL_BAD_ACCESS;
1734 break;
1735
1736 default:
1737 assert(0);
1738 egl_error = EGL_BAD_MATCH;
1739 break;
1740 }
1741
1742 _eglError(egl_error, "dri2_create_image_khr_texture");
1743 }
1744
1745 static _EGLImage *
1746 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
1747 EGLenum target,
1748 EGLClientBuffer buffer,
1749 const EGLint *attr_list)
1750 {
1751 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1752 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1753 struct dri2_egl_image *dri2_img;
1754 GLuint texture = (GLuint) (uintptr_t) buffer;
1755 _EGLImageAttribs attrs;
1756 GLuint depth;
1757 GLenum gl_target;
1758 unsigned error;
1759
1760 if (texture == 0) {
1761 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1762 return EGL_NO_IMAGE_KHR;
1763 }
1764
1765 if (_eglParseImageAttribList(&attrs, disp, attr_list) != EGL_SUCCESS)
1766 return EGL_NO_IMAGE_KHR;
1767
1768 switch (target) {
1769 case EGL_GL_TEXTURE_2D_KHR:
1770 depth = 0;
1771 gl_target = GL_TEXTURE_2D;
1772 break;
1773 case EGL_GL_TEXTURE_3D_KHR:
1774 if (disp->Extensions.KHR_gl_texture_3D_image) {
1775 depth = attrs.GLTextureZOffset;
1776 gl_target = GL_TEXTURE_3D;
1777 break;
1778 }
1779 else {
1780 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1781 return EGL_NO_IMAGE_KHR;
1782 }
1783 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
1784 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
1785 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
1786 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
1787 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
1788 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
1789 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
1790 gl_target = GL_TEXTURE_CUBE_MAP;
1791 break;
1792 default:
1793 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1794 return EGL_NO_IMAGE_KHR;
1795 }
1796
1797 dri2_img = malloc(sizeof *dri2_img);
1798 if (!dri2_img) {
1799 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1800 return EGL_NO_IMAGE_KHR;
1801 }
1802
1803 if (!_eglInitImage(&dri2_img->base, disp)) {
1804 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1805 free(dri2_img);
1806 return EGL_NO_IMAGE_KHR;
1807 }
1808
1809 dri2_img->dri_image =
1810 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
1811 gl_target,
1812 texture,
1813 depth,
1814 attrs.GLTextureLevel,
1815 &error,
1816 dri2_img);
1817 dri2_create_image_khr_texture_error(error);
1818
1819 if (!dri2_img->dri_image) {
1820 free(dri2_img);
1821 return EGL_NO_IMAGE_KHR;
1822 }
1823 return &dri2_img->base;
1824 }
1825
1826 static EGLBoolean
1827 dri2_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
1828 EGLint attribute, EGLint *value)
1829 {
1830 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1831 if (!dri2_dpy->vtbl->query_surface)
1832 return _eglQuerySurface(drv, dpy, surf, attribute, value);
1833 return dri2_dpy->vtbl->query_surface(drv, dpy, surf, attribute, value);
1834 }
1835
1836 static struct wl_buffer*
1837 dri2_create_wayland_buffer_from_image(_EGLDriver *drv, _EGLDisplay *dpy,
1838 _EGLImage *img)
1839 {
1840 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1841 return dri2_dpy->vtbl->create_wayland_buffer_from_image(drv, dpy, img);
1842 }
1843
1844 #ifdef HAVE_LIBDRM
1845 static _EGLImage *
1846 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1847 EGLClientBuffer buffer, const EGLint *attr_list)
1848 {
1849 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1850 EGLint format, name, pitch, err;
1851 _EGLImageAttribs attrs;
1852 __DRIimage *dri_image;
1853
1854 name = (EGLint) (uintptr_t) buffer;
1855
1856 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1857 if (err != EGL_SUCCESS)
1858 return NULL;
1859
1860 if (attrs.Width <= 0 || attrs.Height <= 0 ||
1861 attrs.DRMBufferStrideMESA <= 0) {
1862 _eglError(EGL_BAD_PARAMETER,
1863 "bad width, height or stride");
1864 return NULL;
1865 }
1866
1867 switch (attrs.DRMBufferFormatMESA) {
1868 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1869 format = __DRI_IMAGE_FORMAT_ARGB8888;
1870 pitch = attrs.DRMBufferStrideMESA;
1871 break;
1872 default:
1873 _eglError(EGL_BAD_PARAMETER,
1874 "dri2_create_image_khr: unsupported pixmap depth");
1875 return NULL;
1876 }
1877
1878 dri_image =
1879 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1880 attrs.Width,
1881 attrs.Height,
1882 format,
1883 name,
1884 pitch,
1885 NULL);
1886
1887 return dri2_create_image_from_dri(disp, dri_image);
1888 }
1889
1890 static EGLBoolean
1891 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
1892 {
1893 unsigned i;
1894
1895 /**
1896 * The spec says:
1897 *
1898 * "Required attributes and their values are as follows:
1899 *
1900 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1901 *
1902 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1903 * by drm_fourcc.h and used as the pixel_format parameter of the
1904 * drm_mode_fb_cmd2 ioctl."
1905 *
1906 * and
1907 *
1908 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1909 * incomplete, EGL_BAD_PARAMETER is generated."
1910 */
1911 if (attrs->Width <= 0 || attrs->Height <= 0 ||
1912 !attrs->DMABufFourCC.IsPresent) {
1913 _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
1914 return EGL_FALSE;
1915 }
1916
1917 /**
1918 * Also:
1919 *
1920 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1921 * specified for a plane's pitch or offset isn't supported by EGL,
1922 * EGL_BAD_ACCESS is generated."
1923 */
1924 for (i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
1925 if (attrs->DMABufPlanePitches[i].IsPresent &&
1926 attrs->DMABufPlanePitches[i].Value <= 0) {
1927 _eglError(EGL_BAD_ACCESS, "invalid pitch");
1928 return EGL_FALSE;
1929 }
1930 }
1931
1932 return EGL_TRUE;
1933 }
1934
1935 /* Returns the total number of file descriptors. Zero indicates an error. */
1936 static unsigned
1937 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
1938 {
1939 unsigned i, plane_n;
1940
1941 switch (attrs->DMABufFourCC.Value) {
1942 case DRM_FORMAT_R8:
1943 case DRM_FORMAT_RG88:
1944 case DRM_FORMAT_GR88:
1945 case DRM_FORMAT_RGB332:
1946 case DRM_FORMAT_BGR233:
1947 case DRM_FORMAT_XRGB4444:
1948 case DRM_FORMAT_XBGR4444:
1949 case DRM_FORMAT_RGBX4444:
1950 case DRM_FORMAT_BGRX4444:
1951 case DRM_FORMAT_ARGB4444:
1952 case DRM_FORMAT_ABGR4444:
1953 case DRM_FORMAT_RGBA4444:
1954 case DRM_FORMAT_BGRA4444:
1955 case DRM_FORMAT_XRGB1555:
1956 case DRM_FORMAT_XBGR1555:
1957 case DRM_FORMAT_RGBX5551:
1958 case DRM_FORMAT_BGRX5551:
1959 case DRM_FORMAT_ARGB1555:
1960 case DRM_FORMAT_ABGR1555:
1961 case DRM_FORMAT_RGBA5551:
1962 case DRM_FORMAT_BGRA5551:
1963 case DRM_FORMAT_RGB565:
1964 case DRM_FORMAT_BGR565:
1965 case DRM_FORMAT_RGB888:
1966 case DRM_FORMAT_BGR888:
1967 case DRM_FORMAT_XRGB8888:
1968 case DRM_FORMAT_XBGR8888:
1969 case DRM_FORMAT_RGBX8888:
1970 case DRM_FORMAT_BGRX8888:
1971 case DRM_FORMAT_ARGB8888:
1972 case DRM_FORMAT_ABGR8888:
1973 case DRM_FORMAT_RGBA8888:
1974 case DRM_FORMAT_BGRA8888:
1975 case DRM_FORMAT_XRGB2101010:
1976 case DRM_FORMAT_XBGR2101010:
1977 case DRM_FORMAT_RGBX1010102:
1978 case DRM_FORMAT_BGRX1010102:
1979 case DRM_FORMAT_ARGB2101010:
1980 case DRM_FORMAT_ABGR2101010:
1981 case DRM_FORMAT_RGBA1010102:
1982 case DRM_FORMAT_BGRA1010102:
1983 case DRM_FORMAT_YUYV:
1984 case DRM_FORMAT_YVYU:
1985 case DRM_FORMAT_UYVY:
1986 case DRM_FORMAT_VYUY:
1987 plane_n = 1;
1988 break;
1989 case DRM_FORMAT_NV12:
1990 case DRM_FORMAT_NV21:
1991 case DRM_FORMAT_NV16:
1992 case DRM_FORMAT_NV61:
1993 plane_n = 2;
1994 break;
1995 case DRM_FORMAT_YUV410:
1996 case DRM_FORMAT_YVU410:
1997 case DRM_FORMAT_YUV411:
1998 case DRM_FORMAT_YVU411:
1999 case DRM_FORMAT_YUV420:
2000 case DRM_FORMAT_YVU420:
2001 case DRM_FORMAT_YUV422:
2002 case DRM_FORMAT_YVU422:
2003 case DRM_FORMAT_YUV444:
2004 case DRM_FORMAT_YVU444:
2005 plane_n = 3;
2006 break;
2007 default:
2008 _eglError(EGL_BAD_ATTRIBUTE, "invalid format");
2009 return 0;
2010 }
2011
2012 /**
2013 * The spec says:
2014 *
2015 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2016 * incomplete, EGL_BAD_PARAMETER is generated."
2017 */
2018 for (i = 0; i < plane_n; ++i) {
2019 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2020 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2021 !attrs->DMABufPlanePitches[i].IsPresent) {
2022 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2023 return 0;
2024 }
2025 }
2026
2027 /**
2028 * The spec also says:
2029 *
2030 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2031 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2032 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2033 * attributes are specified."
2034 */
2035 for (i = plane_n; i < 3; ++i) {
2036 if (attrs->DMABufPlaneFds[i].IsPresent ||
2037 attrs->DMABufPlaneOffsets[i].IsPresent ||
2038 attrs->DMABufPlanePitches[i].IsPresent) {
2039 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2040 return 0;
2041 }
2042 }
2043
2044 return plane_n;
2045 }
2046
2047 /**
2048 * The spec says:
2049 *
2050 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2051 * EGL will take a reference to the dma_buf(s) which it will release at any
2052 * time while the EGLDisplay is initialized. It is the responsibility of the
2053 * application to close the dma_buf file descriptors."
2054 *
2055 * Therefore we must never close or otherwise modify the file descriptors.
2056 */
2057 _EGLImage *
2058 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2059 EGLClientBuffer buffer, const EGLint *attr_list)
2060 {
2061 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2062 _EGLImage *res;
2063 EGLint err;
2064 _EGLImageAttribs attrs;
2065 __DRIimage *dri_image;
2066 unsigned num_fds;
2067 unsigned i;
2068 int fds[3];
2069 int pitches[3];
2070 int offsets[3];
2071 unsigned error;
2072
2073 /**
2074 * The spec says:
2075 *
2076 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2077 * error EGL_BAD_PARAMETER is generated."
2078 */
2079 if (buffer != NULL) {
2080 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2081 return NULL;
2082 }
2083
2084 err = _eglParseImageAttribList(&attrs, disp, attr_list);
2085 if (err != EGL_SUCCESS) {
2086 _eglError(err, "bad attribute");
2087 return NULL;
2088 }
2089
2090 if (!dri2_check_dma_buf_attribs(&attrs))
2091 return NULL;
2092
2093 num_fds = dri2_check_dma_buf_format(&attrs);
2094 if (!num_fds)
2095 return NULL;
2096
2097 for (i = 0; i < num_fds; ++i) {
2098 fds[i] = attrs.DMABufPlaneFds[i].Value;
2099 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2100 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2101 }
2102
2103 dri_image =
2104 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2105 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2106 fds, num_fds, pitches, offsets,
2107 attrs.DMABufYuvColorSpaceHint.Value,
2108 attrs.DMABufSampleRangeHint.Value,
2109 attrs.DMABufChromaHorizontalSiting.Value,
2110 attrs.DMABufChromaVerticalSiting.Value,
2111 &error,
2112 NULL);
2113 dri2_create_image_khr_texture_error(error);
2114
2115 if (!dri_image)
2116 return EGL_NO_IMAGE_KHR;
2117
2118 res = dri2_create_image_from_dri(disp, dri_image);
2119
2120 return res;
2121 }
2122 static _EGLImage *
2123 dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
2124 const EGLint *attr_list)
2125 {
2126 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2127 struct dri2_egl_image *dri2_img;
2128 _EGLImageAttribs attrs;
2129 unsigned int dri_use, valid_mask;
2130 int format;
2131 EGLint err = EGL_SUCCESS;
2132
2133 (void) drv;
2134
2135 dri2_img = malloc(sizeof *dri2_img);
2136 if (!dri2_img) {
2137 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2138 return EGL_NO_IMAGE_KHR;
2139 }
2140
2141 if (!attr_list) {
2142 err = EGL_BAD_PARAMETER;
2143 goto cleanup_img;
2144 }
2145
2146 if (!_eglInitImage(&dri2_img->base, disp)) {
2147 err = EGL_BAD_PARAMETER;
2148 goto cleanup_img;
2149 }
2150
2151 err = _eglParseImageAttribList(&attrs, disp, attr_list);
2152 if (err != EGL_SUCCESS)
2153 goto cleanup_img;
2154
2155 if (attrs.Width <= 0 || attrs.Height <= 0) {
2156 _eglLog(_EGL_WARNING, "bad width or height (%dx%d)",
2157 attrs.Width, attrs.Height);
2158 goto cleanup_img;
2159 }
2160
2161 switch (attrs.DRMBufferFormatMESA) {
2162 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2163 format = __DRI_IMAGE_FORMAT_ARGB8888;
2164 break;
2165 default:
2166 _eglLog(_EGL_WARNING, "bad image format value 0x%04x",
2167 attrs.DRMBufferFormatMESA);
2168 goto cleanup_img;
2169 }
2170
2171 valid_mask =
2172 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2173 EGL_DRM_BUFFER_USE_SHARE_MESA |
2174 EGL_DRM_BUFFER_USE_CURSOR_MESA;
2175 if (attrs.DRMBufferUseMESA & ~valid_mask) {
2176 _eglLog(_EGL_WARNING, "bad image use bit 0x%04x",
2177 attrs.DRMBufferUseMESA & ~valid_mask);
2178 goto cleanup_img;
2179 }
2180
2181 dri_use = 0;
2182 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2183 dri_use |= __DRI_IMAGE_USE_SHARE;
2184 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2185 dri_use |= __DRI_IMAGE_USE_SCANOUT;
2186 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2187 dri_use |= __DRI_IMAGE_USE_CURSOR;
2188
2189 dri2_img->dri_image =
2190 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
2191 attrs.Width, attrs.Height,
2192 format, dri_use, dri2_img);
2193 if (dri2_img->dri_image == NULL) {
2194 err = EGL_BAD_ALLOC;
2195 goto cleanup_img;
2196 }
2197
2198 return &dri2_img->base;
2199
2200 cleanup_img:
2201 free(dri2_img);
2202 _eglError(err, "dri2_create_drm_image_mesa");
2203
2204 return EGL_NO_IMAGE_KHR;
2205 }
2206
2207 static EGLBoolean
2208 dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
2209 EGLint *name, EGLint *handle, EGLint *stride)
2210 {
2211 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2212 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2213
2214 (void) drv;
2215
2216 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
2217 __DRI_IMAGE_ATTRIB_NAME, name)) {
2218 _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
2219 return EGL_FALSE;
2220 }
2221
2222 if (handle)
2223 dri2_dpy->image->queryImage(dri2_img->dri_image,
2224 __DRI_IMAGE_ATTRIB_HANDLE, handle);
2225
2226 if (stride)
2227 dri2_dpy->image->queryImage(dri2_img->dri_image,
2228 __DRI_IMAGE_ATTRIB_STRIDE, stride);
2229
2230 return EGL_TRUE;
2231 }
2232
2233 static EGLBoolean
2234 dri2_export_dma_buf_image_query_mesa(_EGLDriver *drv, _EGLDisplay *disp,
2235 _EGLImage *img,
2236 EGLint *fourcc, EGLint *nplanes,
2237 EGLuint64KHR *modifiers)
2238 {
2239 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2240 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2241
2242 (void) drv;
2243
2244
2245 if (nplanes)
2246 dri2_dpy->image->queryImage(dri2_img->dri_image,
2247 __DRI_IMAGE_ATTRIB_NUM_PLANES, nplanes);
2248 if (fourcc)
2249 dri2_dpy->image->queryImage(dri2_img->dri_image,
2250 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
2251
2252 if (modifiers)
2253 *modifiers = 0;
2254
2255 return EGL_TRUE;
2256 }
2257
2258 static EGLBoolean
2259 dri2_export_dma_buf_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
2260 int *fds, EGLint *strides, EGLint *offsets)
2261 {
2262 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2263 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2264
2265 (void) drv;
2266
2267 /* rework later to provide multiple fds/strides/offsets */
2268 if (fds)
2269 dri2_dpy->image->queryImage(dri2_img->dri_image,
2270 __DRI_IMAGE_ATTRIB_FD, fds);
2271
2272 if (strides)
2273 dri2_dpy->image->queryImage(dri2_img->dri_image,
2274 __DRI_IMAGE_ATTRIB_STRIDE, strides);
2275
2276 if (offsets) {
2277 int img_offset;
2278 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
2279 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
2280 if (ret)
2281 offsets[0] = img_offset;
2282 else
2283 offsets[0] = 0;
2284 }
2285
2286 return EGL_TRUE;
2287 }
2288
2289 #endif
2290
2291 _EGLImage *
2292 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
2293 _EGLContext *ctx, EGLenum target,
2294 EGLClientBuffer buffer, const EGLint *attr_list)
2295 {
2296 (void) drv;
2297
2298 switch (target) {
2299 case EGL_GL_TEXTURE_2D_KHR:
2300 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2301 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2302 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2303 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2304 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2305 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2306 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
2307 case EGL_GL_TEXTURE_3D_KHR:
2308 if (disp->Extensions.KHR_gl_texture_3D_image) {
2309 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
2310 }
2311 else {
2312 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2313 return EGL_NO_IMAGE_KHR;
2314 }
2315 case EGL_GL_RENDERBUFFER_KHR:
2316 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
2317 #ifdef HAVE_LIBDRM
2318 case EGL_DRM_BUFFER_MESA:
2319 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
2320 case EGL_LINUX_DMA_BUF_EXT:
2321 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
2322 #endif
2323 #ifdef HAVE_WAYLAND_PLATFORM
2324 case EGL_WAYLAND_BUFFER_WL:
2325 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
2326 #endif
2327 default:
2328 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2329 return EGL_NO_IMAGE_KHR;
2330 }
2331 }
2332
2333 static EGLBoolean
2334 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
2335 {
2336 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2337 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
2338
2339 (void) drv;
2340
2341 dri2_dpy->image->destroyImage(dri2_img->dri_image);
2342 free(dri2_img);
2343
2344 return EGL_TRUE;
2345 }
2346
2347 #ifdef HAVE_WAYLAND_PLATFORM
2348
2349 static void
2350 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
2351 struct wl_drm_buffer *buffer)
2352 {
2353 _EGLDisplay *disp = user_data;
2354 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2355 __DRIimage *img;
2356 int i, dri_components = 0;
2357
2358 if (fd == -1)
2359 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
2360 buffer->width,
2361 buffer->height,
2362 buffer->format,
2363 (int*)&name, 1,
2364 buffer->stride,
2365 buffer->offset,
2366 NULL);
2367 else
2368 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
2369 buffer->width,
2370 buffer->height,
2371 buffer->format,
2372 &fd, 1,
2373 buffer->stride,
2374 buffer->offset,
2375 NULL);
2376
2377 if (img == NULL)
2378 return;
2379
2380 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
2381
2382 buffer->driver_format = NULL;
2383 for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
2384 if (wl_drm_components[i].dri_components == dri_components)
2385 buffer->driver_format = &wl_drm_components[i];
2386
2387 if (buffer->driver_format == NULL)
2388 dri2_dpy->image->destroyImage(img);
2389 else
2390 buffer->driver_buffer = img;
2391 }
2392
2393 static void
2394 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
2395 {
2396 _EGLDisplay *disp = user_data;
2397 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2398
2399 dri2_dpy->image->destroyImage(buffer->driver_buffer);
2400 }
2401
2402 static struct wayland_drm_callbacks wl_drm_callbacks = {
2403 .authenticate = NULL,
2404 .reference_buffer = dri2_wl_reference_buffer,
2405 .release_buffer = dri2_wl_release_buffer
2406 };
2407
2408 static EGLBoolean
2409 dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
2410 struct wl_display *wl_dpy)
2411 {
2412 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2413 int flags = 0;
2414 uint64_t cap;
2415
2416 (void) drv;
2417
2418 if (dri2_dpy->wl_server_drm)
2419 return EGL_FALSE;
2420
2421 wl_drm_callbacks.authenticate =
2422 (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate;
2423
2424 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
2425 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
2426 dri2_dpy->image->base.version >= 7 &&
2427 dri2_dpy->image->createImageFromFds != NULL)
2428 flags |= WAYLAND_DRM_PRIME;
2429
2430 dri2_dpy->wl_server_drm =
2431 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
2432 &wl_drm_callbacks, disp, flags);
2433
2434 if (!dri2_dpy->wl_server_drm)
2435 return EGL_FALSE;
2436
2437 #ifdef HAVE_DRM_PLATFORM
2438 /* We have to share the wl_drm instance with gbm, so gbm can convert
2439 * wl_buffers to gbm bos. */
2440 if (dri2_dpy->gbm_dri)
2441 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
2442 #endif
2443
2444 return EGL_TRUE;
2445 }
2446
2447 static EGLBoolean
2448 dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
2449 struct wl_display *wl_dpy)
2450 {
2451 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2452
2453 (void) drv;
2454
2455 if (!dri2_dpy->wl_server_drm)
2456 return EGL_FALSE;
2457
2458 wayland_drm_uninit(dri2_dpy->wl_server_drm);
2459 dri2_dpy->wl_server_drm = NULL;
2460
2461 return EGL_TRUE;
2462 }
2463
2464 static EGLBoolean
2465 dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
2466 struct wl_resource *buffer_resource,
2467 EGLint attribute, EGLint *value)
2468 {
2469 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2470 struct wl_drm_buffer *buffer;
2471 const struct wl_drm_components_descriptor *format;
2472
2473 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
2474 if (!buffer)
2475 return EGL_FALSE;
2476
2477 format = buffer->driver_format;
2478 switch (attribute) {
2479 case EGL_TEXTURE_FORMAT:
2480 *value = format->components;
2481 return EGL_TRUE;
2482 case EGL_WIDTH:
2483 *value = buffer->width;
2484 return EGL_TRUE;
2485 case EGL_HEIGHT:
2486 *value = buffer->height;
2487 return EGL_TRUE;
2488 }
2489
2490 return EGL_FALSE;
2491 }
2492 #endif
2493
2494 static void
2495 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
2496 {
2497 p_atomic_inc(&sync->refcount);
2498 }
2499
2500 static void
2501 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
2502 struct dri2_egl_sync *dri2_sync)
2503 {
2504 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
2505 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR)
2506 cnd_destroy(&dri2_sync->cond);
2507
2508 if (dri2_sync->fence)
2509 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
2510
2511 free(dri2_sync);
2512 }
2513 }
2514
2515 static _EGLSync *
2516 dri2_create_sync(_EGLDriver *drv, _EGLDisplay *dpy,
2517 EGLenum type, const EGLAttrib *attrib_list)
2518 {
2519 _EGLContext *ctx = _eglGetCurrentContext();
2520 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2521 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2522 struct dri2_egl_sync *dri2_sync;
2523 EGLint ret;
2524 pthread_condattr_t attr;
2525
2526 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
2527 if (!dri2_sync) {
2528 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
2529 return NULL;
2530 }
2531
2532 if (!_eglInitSync(&dri2_sync->base, dpy, type, attrib_list)) {
2533 free(dri2_sync);
2534 return NULL;
2535 }
2536
2537 switch (type) {
2538 case EGL_SYNC_FENCE_KHR:
2539 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
2540 if (!dri2_sync->fence) {
2541 /* Why did it fail? DRI doesn't return an error code, so we emit
2542 * a generic EGL error that doesn't communicate user error.
2543 */
2544 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
2545 free(dri2_sync);
2546 return NULL;
2547 }
2548 break;
2549
2550 case EGL_SYNC_CL_EVENT_KHR:
2551 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
2552 dri2_dpy->dri_screen,
2553 dri2_sync->base.CLEvent);
2554 /* this can only happen if the cl_event passed in is invalid. */
2555 if (!dri2_sync->fence) {
2556 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
2557 free(dri2_sync);
2558 return NULL;
2559 }
2560
2561 /* the initial status must be "signaled" if the cl_event is signaled */
2562 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
2563 dri2_sync->fence, 0, 0))
2564 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
2565 break;
2566
2567 case EGL_SYNC_REUSABLE_KHR:
2568 /* intialize attr */
2569 ret = pthread_condattr_init(&attr);
2570
2571 if (ret) {
2572 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
2573 free(dri2_sync);
2574 return NULL;
2575 }
2576
2577 /* change clock attribute to CLOCK_MONOTONIC */
2578 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
2579
2580 if (ret) {
2581 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
2582 free(dri2_sync);
2583 return NULL;
2584 }
2585
2586 ret = pthread_cond_init(&dri2_sync->cond, &attr);
2587
2588 if (ret) {
2589 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
2590 free(dri2_sync);
2591 return NULL;
2592 }
2593
2594 /* initial status of reusable sync must be "unsignaled" */
2595 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
2596 break;
2597 }
2598
2599 p_atomic_set(&dri2_sync->refcount, 1);
2600 return &dri2_sync->base;
2601 }
2602
2603 static EGLBoolean
2604 dri2_destroy_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync)
2605 {
2606 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2607 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2608 EGLint ret = EGL_TRUE;
2609 EGLint err;
2610
2611 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2612 * then unlock all threads possibly blocked by the reusable sync before
2613 * destroying it.
2614 */
2615 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
2616 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
2617 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
2618 /* unblock all threads currently blocked by sync */
2619 err = cnd_broadcast(&dri2_sync->cond);
2620
2621 if (err) {
2622 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
2623 ret = EGL_FALSE;
2624 }
2625 }
2626 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
2627
2628 return ret;
2629 }
2630
2631 static EGLint
2632 dri2_client_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
2633 EGLint flags, EGLTime timeout)
2634 {
2635 _EGLContext *ctx = _eglGetCurrentContext();
2636 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
2637 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2638 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2639 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2640 unsigned wait_flags = 0;
2641
2642 /* timespecs for cnd_timedwait */
2643 struct timespec current;
2644 xtime expire;
2645
2646 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
2647
2648 /* The EGL_KHR_fence_sync spec states:
2649 *
2650 * "If no context is current for the bound API,
2651 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2652 */
2653 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
2654 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
2655
2656 /* the sync object should take a reference while waiting */
2657 dri2_egl_ref_sync(dri2_sync);
2658
2659 switch (sync->Type) {
2660 case EGL_SYNC_FENCE_KHR:
2661 case EGL_SYNC_CL_EVENT_KHR:
2662 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
2663 dri2_sync->fence, wait_flags,
2664 timeout))
2665 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
2666 else
2667 ret = EGL_TIMEOUT_EXPIRED_KHR;
2668 break;
2669
2670 case EGL_SYNC_REUSABLE_KHR:
2671 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
2672 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
2673 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2674 dri2_drv->glFlush();
2675 }
2676
2677 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2678 if (timeout == EGL_FOREVER_KHR) {
2679 mtx_lock(&dri2_sync->mutex);
2680 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
2681 mtx_unlock(&dri2_sync->mutex);
2682 } else {
2683 /* if reusable sync has not been yet signaled */
2684 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
2685 clock_gettime(CLOCK_MONOTONIC, &current);
2686
2687 /* calculating when to expire */
2688 expire.nsec = timeout % 1000000000L;
2689 expire.sec = timeout / 1000000000L;
2690
2691 expire.nsec += current.tv_nsec;
2692 expire.sec += current.tv_sec;
2693
2694 /* expire.nsec now is a number between 0 and 1999999998 */
2695 if (expire.nsec > 999999999L) {
2696 expire.sec++;
2697 expire.nsec -= 1000000000L;
2698 }
2699
2700 mtx_lock(&dri2_sync->mutex);
2701 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
2702 mtx_unlock(&dri2_sync->mutex);
2703
2704 if (ret == thrd_busy) {
2705 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
2706 ret = EGL_TIMEOUT_EXPIRED_KHR;
2707 } else {
2708 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
2709 ret = EGL_FALSE;
2710 }
2711 }
2712 }
2713 }
2714 break;
2715 }
2716 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
2717
2718 return ret;
2719 }
2720
2721 static EGLBoolean
2722 dri2_signal_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
2723 EGLenum mode)
2724 {
2725 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2726 EGLint ret;
2727
2728 if (sync->Type != EGL_SYNC_REUSABLE_KHR) {
2729 _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
2730 return EGL_FALSE;
2731 }
2732
2733 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR) {
2734 _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
2735 return EGL_FALSE;
2736 }
2737
2738 dri2_sync->base.SyncStatus = mode;
2739
2740 if (mode == EGL_SIGNALED_KHR) {
2741 ret = cnd_broadcast(&dri2_sync->cond);
2742
2743 /* fail to broadcast */
2744 if (ret) {
2745 _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
2746 return EGL_FALSE;
2747 }
2748 }
2749
2750 return EGL_TRUE;
2751 }
2752
2753 static EGLint
2754 dri2_server_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync)
2755 {
2756 _EGLContext *ctx = _eglGetCurrentContext();
2757 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2758 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2759 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
2760
2761 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
2762 dri2_sync->fence, 0);
2763 return EGL_TRUE;
2764 }
2765
2766 static int
2767 dri2_interop_query_device_info(_EGLDisplay *dpy, _EGLContext *ctx,
2768 struct mesa_glinterop_device_info *out)
2769 {
2770 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2771 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2772
2773 if (!dri2_dpy->interop)
2774 return MESA_GLINTEROP_UNSUPPORTED;
2775
2776 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
2777 }
2778
2779 static int
2780 dri2_interop_export_object(_EGLDisplay *dpy, _EGLContext *ctx,
2781 struct mesa_glinterop_export_in *in,
2782 struct mesa_glinterop_export_out *out)
2783 {
2784 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
2785 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2786
2787 if (!dri2_dpy->interop)
2788 return MESA_GLINTEROP_UNSUPPORTED;
2789
2790 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
2791 }
2792
2793 static void
2794 dri2_unload(_EGLDriver *drv)
2795 {
2796 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
2797
2798 dlclose(dri2_drv->handle);
2799 free(dri2_drv);
2800 }
2801
2802 static EGLBoolean
2803 dri2_load(_EGLDriver *drv)
2804 {
2805 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
2806 #ifdef HAVE_ANDROID_PLATFORM
2807 const char *libname = "libglapi.so";
2808 #elif defined(__APPLE__)
2809 const char *libname = "libglapi.0.dylib";
2810 #elif defined(__CYGWIN__)
2811 const char *libname = "cygglapi-0.dll";
2812 #elif defined(__linux__)
2813 const char *libname = "libglapi.so.0";
2814 #else
2815 #error Unknown glapi provider for this platform
2816 #endif
2817 void *handle;
2818
2819 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2820 handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
2821 if (!handle) {
2822 _eglLog(_EGL_WARNING, "DRI2: failed to open glapi provider");
2823 goto no_handle;
2824 }
2825
2826 dri2_drv->get_proc_address = (_EGLProc (*)(const char *))
2827 dlsym(handle, "_glapi_get_proc_address");
2828
2829 /* if glapi is not available, loading DRI drivers will fail */
2830 if (!dri2_drv->get_proc_address) {
2831 _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address");
2832 goto no_symbol;
2833 }
2834
2835 dri2_drv->glFlush = (void (*)(void))
2836 dri2_drv->get_proc_address("glFlush");
2837
2838 /* if glFlush is not available things are horribly broken */
2839 if (!dri2_drv->glFlush) {
2840 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
2841 goto no_symbol;
2842 }
2843
2844 dri2_drv->handle = handle;
2845 return EGL_TRUE;
2846
2847 no_symbol:
2848 dlclose(handle);
2849 no_handle:
2850 return EGL_FALSE;
2851 }
2852
2853 /**
2854 * This is the main entrypoint into the driver, called by libEGL.
2855 * Create a new _EGLDriver object and init its dispatch table.
2856 */
2857 _EGLDriver *
2858 _eglBuiltInDriverDRI2(const char *args)
2859 {
2860 struct dri2_egl_driver *dri2_drv;
2861
2862 (void) args;
2863
2864 dri2_drv = calloc(1, sizeof *dri2_drv);
2865 if (!dri2_drv)
2866 return NULL;
2867
2868 if (!dri2_load(&dri2_drv->base)) {
2869 free(dri2_drv);
2870 return NULL;
2871 }
2872
2873 _eglInitDriverFallbacks(&dri2_drv->base);
2874 dri2_drv->base.API.Initialize = dri2_initialize;
2875 dri2_drv->base.API.Terminate = dri2_terminate;
2876 dri2_drv->base.API.CreateContext = dri2_create_context;
2877 dri2_drv->base.API.DestroyContext = dri2_destroy_context;
2878 dri2_drv->base.API.MakeCurrent = dri2_make_current;
2879 dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
2880 dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
2881 dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
2882 dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
2883 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
2884 dri2_drv->base.API.WaitClient = dri2_wait_client;
2885 dri2_drv->base.API.WaitNative = dri2_wait_native;
2886 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
2887 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
2888 dri2_drv->base.API.SwapInterval = dri2_swap_interval;
2889 dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
2890 dri2_drv->base.API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
2891 dri2_drv->base.API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
2892 dri2_drv->base.API.PostSubBufferNV = dri2_post_sub_buffer;
2893 dri2_drv->base.API.CopyBuffers = dri2_copy_buffers,
2894 dri2_drv->base.API.QueryBufferAge = dri2_query_buffer_age;
2895 dri2_drv->base.API.CreateImageKHR = dri2_create_image;
2896 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
2897 dri2_drv->base.API.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image;
2898 dri2_drv->base.API.QuerySurface = dri2_query_surface;
2899 #ifdef HAVE_LIBDRM
2900 dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa;
2901 dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa;
2902 dri2_drv->base.API.ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa;
2903 dri2_drv->base.API.ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa;
2904 #endif
2905 #ifdef HAVE_WAYLAND_PLATFORM
2906 dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl;
2907 dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl;
2908 dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl;
2909 #endif
2910 dri2_drv->base.API.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium;
2911 dri2_drv->base.API.CreateSyncKHR = dri2_create_sync;
2912 dri2_drv->base.API.ClientWaitSyncKHR = dri2_client_wait_sync;
2913 dri2_drv->base.API.SignalSyncKHR = dri2_signal_sync;
2914 dri2_drv->base.API.WaitSyncKHR = dri2_server_wait_sync;
2915 dri2_drv->base.API.DestroySyncKHR = dri2_destroy_sync;
2916 dri2_drv->base.API.GLInteropQueryDeviceInfo = dri2_interop_query_device_info;
2917 dri2_drv->base.API.GLInteropExportObject = dri2_interop_export_object;
2918
2919 dri2_drv->base.Name = "DRI2";
2920 dri2_drv->base.Unload = dri2_unload;
2921
2922 return &dri2_drv->base;
2923 }