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