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