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