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