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