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