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