Debian Codegraph @

src/egl/drivers/dri2/egl_dri2.c

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