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