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