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