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