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