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