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