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