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