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