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