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