blob: 8c6fd19ab2ecc0f4580cf8f005ce2b21759fb0da [file] [log] [blame]
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001/*
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
Boyan Dingb9ea6082015-07-21 23:43:56 +080028#include <stdbool.h>
Emil Velikovbf0e4d22015-02-28 17:12:40 +000029#include <stdint.h>
Marek Olšákc2c2e9a2015-06-10 02:49:29 +020030#include <stdbool.h>
Kristian Høgsberg42fa0092010-02-03 10:18:28 -050031#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>
Dongwon Kim70299472016-04-04 17:14:10 -070039#include <c11/threads.h>
40#include <time.h>
Emil Velikov6638c552014-05-28 14:36:46 +010041#ifdef HAVE_LIBDRM
Kristian Høgsberg42fa0092010-02-03 10:18:28 -050042#include <xf86drm.h>
Eric Engestromf1374802019-02-12 18:18:03 +000043#include "drm-uapi/drm_fourcc.h"
Samuel Thibaulta594cec2013-11-10 19:32:01 +010044#endif
Kristian Høgsberg42fa0092010-02-03 10:18:28 -050045#include <GL/gl.h>
46#include <GL/internal/dri_interface.h>
Kristian Høgsberg2168b872010-06-02 22:48:06 -040047#include <sys/types.h>
48#include <sys/stat.h>
49
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +030050#ifdef HAVE_WAYLAND_PLATFORM
Emil Velikov9fa1d822018-03-14 17:31:27 +000051#include <wayland-client.h>
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +030052#include "wayland-drm.h"
Chad Versacef506ef62014-01-28 18:53:56 -080053#include "wayland-drm-client-protocol.h"
Daniel Stone02cc3592017-06-16 18:01:23 +010054#include "linux-dmabuf-unstable-v1-client-protocol.h"
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +030055#endif
56
Gregory Hainaut3fde8db2017-05-29 13:18:27 +020057#ifdef HAVE_X11_PLATFORM
58#include "X11/Xlibint.h"
59#endif
60
Eric Engestromd75fbff2018-05-16 14:17:30 +010061#include "egldefines.h"
Chad Versacef506ef62014-01-28 18:53:56 -080062#include "egl_dri2.h"
Matt Turner4e970842017-07-06 18:40:53 -070063#include "GL/mesa_glinterop.h"
Christian Gmeiner0c73a3b2016-11-09 15:50:06 +010064#include "loader/loader.h"
Eric Engestrom419b4462020-06-05 10:22:22 +020065#include "util/os_file.h"
Alexander von Gluck IVba651962015-08-07 12:55:40 -050066#include "util/u_atomic.h"
Daniel Stone02cc3592017-06-16 18:01:23 +010067#include "util/u_vector.h"
Emil Velikovd884d8d2017-09-27 17:36:29 +010068#include "mapi/glapi/glapi.h"
Kevin Strasser641320c2019-09-12 09:38:24 -070069#include "util/bitscan.h"
Mathias Fröhlichc7617d82019-12-13 17:09:56 +010070#include "util/u_math.h"
Chad Versacef506ef62014-01-28 18:53:56 -080071
Tapani Pälli722f96b2019-02-11 10:01:35 +020072/* Additional definitions not yet in the drm_fourcc.h.
73 */
74#ifndef DRM_FORMAT_P010
75#define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
76#endif
77
78#ifndef DRM_FORMAT_P012
79#define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
80#endif
81
82#ifndef DRM_FORMAT_P016
83#define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
84#endif
85
Adam Jacksonc0be3aa2016-09-22 03:47:55 -040086#define NUM_ATTRIBS 12
Chris Wilson5c561832016-10-27 19:54:49 +010087
Mathias Fröhlichd32c4582020-02-09 19:01:53 +010088static const struct dri2_pbuffer_visual {
Mathias Fröhlichf280c002020-02-16 11:13:43 +010089 const char *format_name;
Mathias Fröhlichd32c4582020-02-09 19:01:53 +010090 unsigned int dri_image_format;
91 int rgba_shifts[4];
92 unsigned int rgba_sizes[4];
93} dri2_pbuffer_visuals[] = {
94 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +010095 "ABGR16F",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +010096 __DRI_IMAGE_FORMAT_ABGR16161616F,
97 { 0, 16, 32, 48 },
98 { 16, 16, 16, 16 }
99 },
100 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100101 "XBGR16F",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100102 __DRI_IMAGE_FORMAT_XBGR16161616F,
103 { 0, 16, 32, -1 },
104 { 16, 16, 16, 0 }
105 },
106 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100107 "A2RGB10",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100108 __DRI_IMAGE_FORMAT_ARGB2101010,
109 { 20, 10, 0, 30 },
110 { 10, 10, 10, 2 }
111 },
112 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100113 "X2RGB10",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100114 __DRI_IMAGE_FORMAT_XRGB2101010,
115 { 20, 10, 0, -1 },
116 { 10, 10, 10, 0 }
117 },
118 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100119 "ARGB8888",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100120 __DRI_IMAGE_FORMAT_ARGB8888,
121 { 16, 8, 0, 24 },
122 { 8, 8, 8, 8 }
123 },
124 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100125 "RGB888",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100126 __DRI_IMAGE_FORMAT_XRGB8888,
127 { 16, 8, 0, -1 },
128 { 8, 8, 8, 0 }
129 },
130 {
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100131 "RGB565",
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100132 __DRI_IMAGE_FORMAT_RGB565,
133 { 11, 5, 0, -1 },
134 { 5, 6, 5, 0 }
135 },
136};
137
Eric Anholt3b7b6ad2012-12-27 17:39:37 -0800138static void
139dri_set_background_context(void *loaderPrivate)
140{
141 _EGLContext *ctx = _eglGetCurrentContext();
142 _EGLThreadInfo *t = _eglGetCurrentThread();
143
144 _eglBindContextToThread(ctx, t);
145}
146
Eric Engestrom7823cfe2017-09-26 12:16:33 +0100147static void
148dri2_gl_flush()
149{
150 static void (*glFlush)(void);
151 static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
152
153 mtx_lock(&glFlushMutex);
154 if (!glFlush)
155 glFlush = _glapi_get_proc_address("glFlush");
156 mtx_unlock(&glFlushMutex);
157
158 /* if glFlush is not available things are horribly broken */
159 if (!glFlush) {
160 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
161 return;
162 }
163
164 glFlush();
165}
166
Gregory Hainaut3fde8db2017-05-29 13:18:27 +0200167static GLboolean
168dri_is_thread_safe(void *loaderPrivate)
169{
170 struct dri2_egl_surface *dri2_surf = loaderPrivate;
Eric Engestrom14be04f2019-06-22 14:27:29 +0100171 UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;
Gregory Hainaut3fde8db2017-05-29 13:18:27 +0200172
173#ifdef HAVE_X11_PLATFORM
174 Display *xdpy = (Display*)display->PlatformDisplay;
175
176 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
177 * platform
178 *
179 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
180 * It wll be NULL if XInitThreads wasn't called.
181 */
182 if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
183 return false;
184#endif
185
186#ifdef HAVE_WAYLAND_PLATFORM
187 if (display->Platform == _EGL_PLATFORM_WAYLAND)
188 return true;
189#endif
190
191 return true;
192}
193
Eric Anholt3b7b6ad2012-12-27 17:39:37 -0800194const __DRIbackgroundCallableExtension background_callable_extension = {
Gregory Hainaut3fde8db2017-05-29 13:18:27 +0200195 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
Eric Anholt3b7b6ad2012-12-27 17:39:37 -0800196
197 .setBackgroundContext = dri_set_background_context,
Gregory Hainaut3fde8db2017-05-29 13:18:27 +0200198 .isThreadSafe = dri_is_thread_safe,
Eric Anholt3b7b6ad2012-12-27 17:39:37 -0800199};
200
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500201const __DRIuseInvalidateExtension use_invalidate = {
Emil Velikov5f280d02014-02-12 20:13:15 +0000202 .base = { __DRI_USE_INVALIDATE, 1 }
Kristian Høgsberg2168b872010-06-02 22:48:06 -0400203};
204
Eric Anholt6a8d39dc2019-07-23 13:18:21 -0700205static void
206dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
207 int *x, int *y, int *w, int *h,
208 void *loaderPrivate)
209{
210 struct dri2_egl_surface *dri2_surf = loaderPrivate;
211
212 *x = *y = 0;
213 *w = dri2_surf->base.Width;
214 *h = dri2_surf->base.Height;
215}
216
Mathias Fröhlichc7617d82019-12-13 17:09:56 +0100217static int
218dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
219{
220 const int depth = dri2_surf->base.Config->BufferSize;
221 return depth ? util_next_power_of_two(depth / 8) : 0;
222}
223
224static void
225dri2_put_image(__DRIdrawable * draw, int op,
226 int x, int y, int w, int h,
227 char *data, void *loaderPrivate)
228{
229 struct dri2_egl_surface *dri2_surf = loaderPrivate;
230 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
231 const int width = dri2_surf->base.Width;
232 const int height = dri2_surf->base.Height;
233 const int dst_stride = width*bpp;
234 const int src_stride = w*bpp;
235 const int x_offset = x*bpp;
236 int copy_width = src_stride;
237
238 if (!dri2_surf->swrast_device_buffer)
239 dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
240
241 if (dri2_surf->swrast_device_buffer) {
242 const char *src = data;
243 char *dst = dri2_surf->swrast_device_buffer;
244
245 dst += x_offset;
246 dst += y*dst_stride;
247
248 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
249 if (copy_width > dst_stride - x_offset)
250 copy_width = dst_stride - x_offset;
251 if (h > height - y)
252 h = height - y;
253
254 for (; 0 < h; --h) {
255 memcpy(dst, src, copy_width);
256 dst += dst_stride;
257 src += src_stride;
258 }
259 }
260}
261
262static void
263dri2_get_image(__DRIdrawable * read,
264 int x, int y, int w, int h,
265 char *data, void *loaderPrivate)
266{
267 struct dri2_egl_surface *dri2_surf = loaderPrivate;
268 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
269 const int width = dri2_surf->base.Width;
270 const int height = dri2_surf->base.Height;
271 const int src_stride = width*bpp;
272 const int dst_stride = w*bpp;
273 const int x_offset = x*bpp;
274 int copy_width = dst_stride;
275 const char *src = dri2_surf->swrast_device_buffer;
276 char *dst = data;
277
278 if (!src) {
279 memset(data, 0, copy_width * h);
280 return;
281 }
282
283 src += x_offset;
284 src += y*src_stride;
285
286 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
287 if (copy_width > src_stride - x_offset)
288 copy_width = src_stride - x_offset;
289 if (h > height - y)
290 h = height - y;
291
292 for (; 0 < h; --h) {
293 memcpy(dst, src, copy_width);
294 src += src_stride;
295 dst += dst_stride;
296 }
297
298}
299
300/* HACK: technically we should have swrast_null, instead of these.
Eric Anholt6a8d39dc2019-07-23 13:18:21 -0700301 */
302const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
303 .base = { __DRI_SWRAST_LOADER, 1 },
304 .getDrawableInfo = dri2_get_pbuffer_drawable_info,
Mathias Fröhlichc7617d82019-12-13 17:09:56 +0100305 .putImage = dri2_put_image,
306 .getImage = dri2_get_image,
Eric Anholt6a8d39dc2019-07-23 13:18:21 -0700307};
308
Emil Velikovb6643092017-05-01 19:40:31 +0100309static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
Emil Velikove5efaeb2017-04-28 17:01:03 +0100310 [__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
311 [__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
Emil Velikove5efaeb2017-04-28 17:01:03 +0100312 [__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
Emil Velikove5efaeb2017-04-28 17:01:03 +0100313 [__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
314 [__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
315 [__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
316 [__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
317 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
318 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
319 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
320 [__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
321 [__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
322 [__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500323};
324
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200325const __DRIconfig *
326dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
327 EGLenum colorspace)
328{
Eric Engestromb81cfc72017-06-21 10:40:31 +0100329 const bool double_buffer = surface_type == EGL_WINDOW_BIT;
Emil Velikov858f2f22015-09-13 12:25:27 +0100330 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
331
Eric Engestromb81cfc72017-06-21 10:40:31 +0100332 return conf->dri_config[double_buffer][srgb];
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200333}
334
Benjamin Franzke16e30272011-06-11 23:00:40 +0200335static EGLBoolean
336dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
337{
338 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
339 return EGL_FALSE;
340
341 if (!_eglMatchConfig(conf, criteria))
342 return EGL_FALSE;
343
344 return EGL_TRUE;
345}
346
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800347void
348dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
349 const __DRIconfig *config, int *shifts,
350 unsigned int *sizes)
351{
352 unsigned int mask;
353
354 if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
355 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
356 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
357 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
358 } else {
359 /* Driver isn't exposing shifts, so convert masks to shifts */
360 core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
361 shifts[0] = ffs(mask) - 1;
362 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
363 shifts[1] = ffs(mask) - 1;
364 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
365 shifts[2] = ffs(mask) - 1;
366 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
367 shifts[3] = ffs(mask) - 1;
368 }
369
370 core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
371 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
372 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
373 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
374}
375
Kevin Strasser482ed432019-01-24 16:55:33 -0800376void
377dri2_get_render_type_float(const __DRIcoreExtension *core,
378 const __DRIconfig *config,
379 bool *is_float)
380{
381 unsigned int render_type;
382
383 core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
384 *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
385}
386
Mathias Fröhlichd32c4582020-02-09 19:01:53 +0100387unsigned int
388dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
389 const __DRIconfig *config)
390{
391 int shifts[4];
392 unsigned int sizes[4];
393
394 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
395
396 for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
397 const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
398
399 if (shifts[0] == visual->rgba_shifts[0] &&
400 shifts[1] == visual->rgba_shifts[1] &&
401 shifts[2] == visual->rgba_shifts[2] &&
402 shifts[3] == visual->rgba_shifts[3] &&
403 sizes[0] == visual->rgba_sizes[0] &&
404 sizes[1] == visual->rgba_sizes[1] &&
405 sizes[2] == visual->rgba_sizes[2] &&
406 sizes[3] == visual->rgba_sizes[3]) {
407 return visual->dri_image_format;
408 }
409 }
410
411 return __DRI_IMAGE_FORMAT_NONE;
412}
413
Kristian Høgsberg2889d962011-02-02 22:10:40 -0500414struct dri2_egl_config *
Kristian Høgsbergc3c1a7e2010-02-09 20:49:40 -0500415dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
Emil Velikovebc68e32016-08-25 00:12:13 +0100416 EGLint surface_type, const EGLint *attr_list,
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800417 const int *rgba_shifts, const unsigned int *rgba_sizes)
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500418{
419 struct dri2_egl_config *conf;
Emil Velikov532ec2e2016-07-15 12:47:37 +0100420 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Kristian Høgsbergc19dab72010-02-09 21:01:04 -0500421 _EGLConfig base;
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500422 unsigned int attrib, value, double_buffer;
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200423 bool srgb = false;
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500424 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800425 int dri_shifts[4] = { -1, -1, -1, -1 };
426 unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100427 _EGLConfig *matching_config;
428 EGLint num_configs = 0;
Emil Velikovaeaf21a2016-11-09 21:45:07 +0000429 EGLint config_id;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500430
Kristian Høgsbergc19dab72010-02-09 21:01:04 -0500431 _eglInitConfig(&base, disp, id);
Boyan Ding052b3d42015-06-13 15:36:27 +0800432
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400433 double_buffer = 0;
434 bind_to_texture_rgb = 0;
435 bind_to_texture_rgba = 0;
436
Kevin Strasser70b36c02019-01-28 10:42:44 -0800437 for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
438 if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
439 break;
440
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500441 switch (attrib) {
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500442 case __DRI_ATTRIB_RENDER_TYPE:
Kevin Strasser482ed432019-01-24 16:55:33 -0800443 if (value & __DRI_ATTRIB_FLOAT_BIT)
Eric Engestrom7bee3882020-02-13 15:53:03 +0000444 base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
Emil Velikovebc68e32016-08-25 00:12:13 +0100445 if (value & __DRI_ATTRIB_RGBA_BIT)
446 value = EGL_RGB_BUFFER;
447 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
448 value = EGL_LUMINANCE_BUFFER;
449 else
450 return NULL;
Eric Engestrom7bee3882020-02-13 15:53:03 +0000451 base.ColorBufferType = value;
Emil Velikovebc68e32016-08-25 00:12:13 +0100452 break;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500453
454 case __DRI_ATTRIB_CONFIG_CAVEAT:
455 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
456 value = EGL_NON_CONFORMANT_CONFIG;
457 else if (value & __DRI_ATTRIB_SLOW_BIT)
458 value = EGL_SLOW_CONFIG;
Emil Velikovebc68e32016-08-25 00:12:13 +0100459 else
460 value = EGL_NONE;
Eric Engestrom7bee3882020-02-13 15:53:03 +0000461 base.ConfigCaveat = value;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500462 break;
463
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500464 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
Emil Velikovebc68e32016-08-25 00:12:13 +0100465 bind_to_texture_rgb = value;
466 break;
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500467
468 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
Emil Velikovebc68e32016-08-25 00:12:13 +0100469 bind_to_texture_rgba = value;
470 break;
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500471
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500472 case __DRI_ATTRIB_DOUBLE_BUFFER:
Emil Velikovebc68e32016-08-25 00:12:13 +0100473 double_buffer = value;
474 break;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500475
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800476 case __DRI_ATTRIB_RED_SIZE:
477 dri_sizes[0] = value;
Eric Engestrom7bee3882020-02-13 15:53:03 +0000478 base.RedSize = value;
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800479 break;
480
Chia-I Wu58911b82011-08-05 14:36:14 +0900481 case __DRI_ATTRIB_RED_MASK:
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800482 dri_shifts[0] = ffs(value) - 1;
483 break;
484
485 case __DRI_ATTRIB_RED_SHIFT:
486 dri_shifts[0] = value;
487 break;
488
489 case __DRI_ATTRIB_GREEN_SIZE:
490 dri_sizes[1] = value;
Eric Engestrom7bee3882020-02-13 15:53:03 +0000491 base.GreenSize = value;
Chia-I Wu58911b82011-08-05 14:36:14 +0900492 break;
493
494 case __DRI_ATTRIB_GREEN_MASK:
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800495 dri_shifts[1] = ffs(value) - 1;
496 break;
497
498 case __DRI_ATTRIB_GREEN_SHIFT:
499 dri_shifts[1] = value;
500 break;
501
502 case __DRI_ATTRIB_BLUE_SIZE:
503 dri_sizes[2] = value;
Eric Engestrom7bee3882020-02-13 15:53:03 +0000504 base.BlueSize = value;
Chia-I Wu58911b82011-08-05 14:36:14 +0900505 break;
506
507 case __DRI_ATTRIB_BLUE_MASK:
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800508 dri_shifts[2] = ffs(value) - 1;
509 break;
510
511 case __DRI_ATTRIB_BLUE_SHIFT:
512 dri_shifts[2] = value;
513 break;
514
515 case __DRI_ATTRIB_ALPHA_SIZE:
516 dri_sizes[3] = value;
Eric Engestrom7bee3882020-02-13 15:53:03 +0000517 base.AlphaSize = value;
Chia-I Wu58911b82011-08-05 14:36:14 +0900518 break;
519
520 case __DRI_ATTRIB_ALPHA_MASK:
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800521 dri_shifts[3] = ffs(value) - 1;
522 break;
523
524 case __DRI_ATTRIB_ALPHA_SHIFT:
525 dri_shifts[3] = value;
Chia-I Wu58911b82011-08-05 14:36:14 +0900526 break;
527
Marek Olšák396cbab2015-04-09 23:25:07 +0200528 case __DRI_ATTRIB_ACCUM_RED_SIZE:
529 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
530 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
531 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
532 /* Don't expose visuals with the accumulation buffer. */
533 if (value > 0)
534 return NULL;
535 break;
536
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200537 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
538 srgb = value != 0;
Emil Velikov54702c22015-11-29 16:48:51 +0000539 if (!disp->Extensions.KHR_gl_colorspace && srgb)
540 return NULL;
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200541 break;
542
Tapani Pällib91e1e32016-10-20 10:33:33 +0300543 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
Eric Engestrom7bee3882020-02-13 15:53:03 +0000544 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
Tapani Pällib91e1e32016-10-20 10:33:33 +0300545 break;
546 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
Eric Engestrom7bee3882020-02-13 15:53:03 +0000547 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
Tapani Pällib91e1e32016-10-20 10:33:33 +0300548 break;
Chad Versaceed7c6942018-04-30 22:35:17 -0700549 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
550 if (disp->Extensions.KHR_mutable_render_buffer)
551 surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
552 break;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500553 default:
Emil Velikovebc68e32016-08-25 00:12:13 +0100554 key = dri2_to_egl_attribute_map[attrib];
555 if (key != 0)
556 _eglSetConfigKey(&base, key, value);
557 break;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500558 }
559 }
560
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100561 if (attr_list)
Chad Versacecd717cb2017-06-22 11:00:40 -0700562 for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100563 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500564
Kevin Strasser7b4ed2b2019-01-24 16:32:48 -0800565 if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
566 return NULL;
567
568 if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
Chia-I Wu58911b82011-08-05 14:36:14 +0900569 return NULL;
570
Chia-I Wub67f7292010-10-22 15:03:11 +0800571 base.NativeRenderable = EGL_TRUE;
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400572
Chia-I Wub67f7292010-10-22 15:03:11 +0800573 base.SurfaceType = surface_type;
Benjamin Franzkea8128d72011-02-04 12:32:30 +0100574 if (surface_type & (EGL_PBUFFER_BIT |
575 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
Chia-I Wub67f7292010-10-22 15:03:11 +0800576 base.BindToTextureRGB = bind_to_texture_rgb;
577 if (base.AlphaSize > 0)
578 base.BindToTextureRGBA = bind_to_texture_rgba;
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400579 }
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500580
Hal Gentz4ef2c532019-10-18 01:03:30 -0600581 if (double_buffer) {
582 surface_type &= ~EGL_PIXMAP_BIT;
583 }
584
Adam Jackson7e0e53a2019-09-10 12:07:07 -0400585 /* No support for pbuffer + MSAA for now.
586 *
587 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
588 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
589 */
590 if (base.Samples) {
591 surface_type &= ~EGL_PBUFFER_BIT;
592 }
593
Hal Gentz4ef2c532019-10-18 01:03:30 -0600594 if (!surface_type)
595 return NULL;
596
Chia-I Wua4a38dc2011-01-13 16:53:13 +0800597 base.RenderableType = disp->ClientAPIs;
598 base.Conformant = disp->ClientAPIs;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500599
Chad Versaced3dfce32013-04-23 04:17:48 +0200600 base.MinSwapInterval = dri2_dpy->min_swap_interval;
601 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
602
Kristian Høgsbergc19dab72010-02-09 21:01:04 -0500603 if (!_eglValidateConfig(&base, EGL_FALSE)) {
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -0500604 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400605 return NULL;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500606 }
607
Emil Velikovaeaf21a2016-11-09 21:45:07 +0000608 config_id = base.ConfigID;
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100609 base.ConfigID = EGL_DONT_CARE;
610 base.SurfaceType = EGL_DONT_CARE;
611 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
Benjamin Franzke16e30272011-06-11 23:00:40 +0200612 (_EGLArrayForEach) dri2_match_config, &base);
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100613
614 if (num_configs == 1) {
615 conf = (struct dri2_egl_config *) matching_config;
616
Eric Engestromb81cfc72017-06-21 10:40:31 +0100617 if (!conf->dri_config[double_buffer][srgb])
618 conf->dri_config[double_buffer][srgb] = dri_config;
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100619 else
Benjamin Franzke83c68752011-05-12 19:30:05 +0200620 /* a similar config type is already added (unlikely) => discard */
621 return NULL;
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100622 }
José Fonseca4a47d6d2011-05-12 15:23:48 +0100623 else if (num_configs == 0) {
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200624 conf = calloc(1, sizeof *conf);
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100625 if (conf == NULL)
626 return NULL;
627
Eric Engestromb81cfc72017-06-21 10:40:31 +0100628 conf->dri_config[double_buffer][srgb] = dri_config;
Varad Gautamf988eff2015-10-07 09:48:14 +0530629
Varad Gautamdeb17652015-10-07 09:48:15 +0530630 memcpy(&conf->base, &base, sizeof base);
Varad Gautamf988eff2015-10-07 09:48:14 +0530631 conf->base.SurfaceType = 0;
Emil Velikovaeaf21a2016-11-09 21:45:07 +0000632 conf->base.ConfigID = config_id;
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100633
Chia-I Wu8a6bdf32010-10-23 00:47:22 +0800634 _eglLinkConfig(&conf->base);
Kristian Høgsbergc19dab72010-02-09 21:01:04 -0500635 }
José Fonseca4a47d6d2011-05-12 15:23:48 +0100636 else {
Eric Engestromea738a92018-10-28 18:20:20 +0000637 unreachable("duplicates should not be possible");
José Fonseca4a47d6d2011-05-12 15:23:48 +0100638 return NULL;
639 }
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400640
Kristian Høgsberg447bb452011-08-31 18:20:06 -0400641 conf->base.SurfaceType |= surface_type;
Benjamin Franzke87dde5b2011-02-09 15:30:20 +0100642
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400643 return conf;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500644}
645
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100646EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200647dri2_add_pbuffer_configs_for_visuals(const _EGLDriver *drv, _EGLDisplay *disp)
Mathias Fröhlichf280c002020-02-16 11:13:43 +0100648{
649 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
650 unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
651 unsigned int config_count = 0;
652
653 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
654 for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
655 struct dri2_egl_config *dri2_conf;
656
657 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
658 config_count + 1, EGL_PBUFFER_BIT, NULL,
659 dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
660
661 if (dri2_conf) {
662 if (dri2_conf->base.ConfigID == config_count + 1)
663 config_count++;
664 format_count[j]++;
665 }
666 }
667 }
668
669 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
670 if (!format_count[i]) {
671 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
672 dri2_pbuffer_visuals[i].format_name);
673 }
674 }
675
676 return (config_count != 0);
677}
678
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200679__DRIimage *
Kristian Høgsberg17eace52010-09-22 22:01:17 -0400680dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
Kristian Høgsbergf4e7d1b2010-02-11 19:28:26 -0500681{
Kristian Høgsberg17eace52010-09-22 22:01:17 -0400682 _EGLDisplay *disp = data;
Kristian Høgsbergf4e7d1b2010-02-11 19:28:26 -0500683 struct dri2_egl_image *dri2_img;
684 _EGLImage *img;
685
Ian Romanickeade9462010-09-23 09:39:42 -0700686 (void) screen;
687
Kristian Høgsbergf4e7d1b2010-02-11 19:28:26 -0500688 img = _eglLookupImage(image, disp);
689 if (img == NULL) {
690 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
691 return NULL;
692 }
693
694 dri2_img = dri2_egl_image(image);
695
696 return dri2_img->dri_image;
697}
698
Kristian Høgsberg2889d962011-02-02 22:10:40 -0500699const __DRIimageLookupExtension image_lookup_extension = {
Emil Velikov5f280d02014-02-12 20:13:15 +0000700 .base = { __DRI_IMAGE_LOOKUP, 1 },
701
702 .lookupEGLImage = dri2_lookup_egl_image
Kristian Høgsberg2168b872010-06-02 22:48:06 -0400703};
704
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500705struct dri2_extension_match {
706 const char *name;
707 int version;
708 int offset;
709};
710
Emil Velikov665cad12016-08-25 00:52:36 +0100711static const struct dri2_extension_match dri3_driver_extensions[] = {
Boyan Dingf35198b2015-07-21 23:44:00 +0800712 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
713 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
714 { NULL, 0, 0 }
715};
716
Emil Velikov665cad12016-08-25 00:52:36 +0100717static const struct dri2_extension_match dri2_driver_extensions[] = {
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500718 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
Ian Romanick38f91f22012-07-18 13:17:50 -0700719 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
Ian Romanick137fce22010-09-23 09:40:58 -0700720 { NULL, 0, 0 }
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500721};
722
Emil Velikov665cad12016-08-25 00:52:36 +0100723static const struct dri2_extension_match dri2_core_extensions[] = {
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500724 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
Kristian Høgsbergbba5a982010-02-09 15:49:28 -0500725 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
Kristian Høgsberg16707372013-03-19 13:20:36 -0400726 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
Ian Romanick137fce22010-09-23 09:40:58 -0700727 { NULL, 0, 0 }
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500728};
729
Emil Velikov665cad12016-08-25 00:52:36 +0100730static const struct dri2_extension_match swrast_driver_extensions[] = {
Haitao Fengf55d0272011-02-16 23:05:15 -0500731 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
732 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
Ian Romanick9d76ad22012-07-19 11:08:02 -0700733 { NULL, 0, 0 }
Haitao Fengf55d0272011-02-16 23:05:15 -0500734};
735
Emil Velikov665cad12016-08-25 00:52:36 +0100736static const struct dri2_extension_match swrast_core_extensions[] = {
Haitao Fengf55d0272011-02-16 23:05:15 -0500737 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
Ian Romanick9d76ad22012-07-19 11:08:02 -0700738 { NULL, 0, 0 }
Haitao Fengf55d0272011-02-16 23:05:15 -0500739};
740
Veluri Mithun85edfc02019-01-23 22:45:42 +0530741static const struct dri2_extension_match optional_driver_extensions[] = {
742 { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
743 { NULL, 0, 0 }
744};
745
Emil Velikov665cad12016-08-25 00:52:36 +0100746static const struct dri2_extension_match optional_core_extensions[] = {
Emil Velikov3948ad82016-08-25 00:51:05 +0100747 { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
Grigori Goronzy49095192017-06-29 02:44:03 +0200748 { __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },
Emil Velikov3948ad82016-08-25 00:51:05 +0100749 { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
750 { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
Harish Krupoa4a8ebe2019-08-12 12:07:06 +0200751 { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
Emil Velikov3948ad82016-08-25 00:51:05 +0100752 { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
753 { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
Gurchetan Singh12181b52017-08-01 14:51:40 -0700754 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
Adam Jacksonc0be3aa2016-09-22 03:47:55 -0400755 { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
Tapani Pälli6f5b5702017-12-28 10:51:11 +0200756 { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
Chad Versaceed7c6942018-04-30 22:35:17 -0700757 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
Emil Velikov3948ad82016-08-25 00:51:05 +0100758 { NULL, 0, 0 }
759};
760
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500761static EGLBoolean
762dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
Emil Velikov665cad12016-08-25 00:52:36 +0100763 const struct dri2_extension_match *matches,
Emil Velikov38526bd2016-08-25 00:16:01 +0100764 const __DRIextension **extensions,
765 bool optional)
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500766{
Chad Versacecd717cb2017-06-22 11:00:40 -0700767 int ret = EGL_TRUE;
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500768 void *field;
769
Chad Versacecd717cb2017-06-22 11:00:40 -0700770 for (int i = 0; extensions[i]; i++) {
Martin Peres4518eea2015-10-30 17:16:35 +0200771 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
Chad Versacecd717cb2017-06-22 11:00:40 -0700772 for (int j = 0; matches[j].name; j++) {
Emil Velikovebc68e32016-08-25 00:12:13 +0100773 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
774 extensions[i]->version >= matches[j].version) {
775 field = ((char *) dri2_dpy + matches[j].offset);
776 *(const __DRIextension **) field = extensions[i];
777 _eglLog(_EGL_INFO, "found extension %s version %d",
778 extensions[i]->name, extensions[i]->version);
Emil Velikovfdd373a2016-08-25 09:51:54 +0100779 break;
Emil Velikovebc68e32016-08-25 00:12:13 +0100780 }
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500781 }
782 }
Boyan Ding052b3d42015-06-13 15:36:27 +0800783
Chad Versacecd717cb2017-06-22 11:00:40 -0700784 for (int j = 0; matches[j].name; j++) {
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500785 field = ((char *) dri2_dpy + matches[j].offset);
786 if (*(const __DRIextension **) field == NULL) {
Emil Velikov38526bd2016-08-25 00:16:01 +0100787 if (optional) {
788 _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
789 matches[j].name, matches[j].version);
790 } else {
791 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
792 matches[j].name, matches[j].version);
793 ret = EGL_FALSE;
794 }
Kristian Høgsberg8c593d32010-02-09 10:57:43 -0500795 }
796 }
797
798 return ret;
799}
800
Chia-I Wucf69eea2011-06-08 16:33:55 +0800801static const __DRIextension **
802dri2_open_driver(_EGLDisplay *disp)
Kristian Høgsberg40073422010-03-02 15:34:18 -0500803{
Emil Velikov532ec2e2016-07-15 12:47:37 +0100804 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Anholtd971a422018-11-15 13:54:49 -0800805 static const char *search_path_vars[] = {
806 "LIBGL_DRIVERS_PATH",
807 NULL,
808 };
Kristian Høgsberg42fa0092010-02-03 10:18:28 -0500809
Eric Anholtd971a422018-11-15 13:54:49 -0800810 return loader_open_driver(dri2_dpy->driver_name,
811 &dri2_dpy->driver,
812 search_path_vars);
Chia-I Wucf69eea2011-06-08 16:33:55 +0800813}
814
Emil Velikov9d84a922019-02-05 15:19:46 +0000815static EGLBoolean
816dri2_load_driver_common(_EGLDisplay *disp,
817 const struct dri2_extension_match *driver_extensions)
Boyan Dingf35198b2015-07-21 23:44:00 +0800818{
Emil Velikov532ec2e2016-07-15 12:47:37 +0100819 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Boyan Dingf35198b2015-07-21 23:44:00 +0800820 const __DRIextension **extensions;
821
822 extensions = dri2_open_driver(disp);
823 if (!extensions)
824 return EGL_FALSE;
825
Emil Velikovd3c91432020-03-05 13:05:36 +0000826 if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
827 dlclose(dri2_dpy->driver);
828 dri2_dpy->driver = NULL;
Boyan Dingf35198b2015-07-21 23:44:00 +0800829 return EGL_FALSE;
Emil Velikovd3c91432020-03-05 13:05:36 +0000830 }
Boyan Dingf35198b2015-07-21 23:44:00 +0800831 dri2_dpy->driver_extensions = extensions;
832
Veluri Mithun85edfc02019-01-23 22:45:42 +0530833 dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
834
Boyan Dingf35198b2015-07-21 23:44:00 +0800835 return EGL_TRUE;
836}
837
838EGLBoolean
Chia-I Wucf69eea2011-06-08 16:33:55 +0800839dri2_load_driver(_EGLDisplay *disp)
840{
Emil Velikov9d84a922019-02-05 15:19:46 +0000841 return dri2_load_driver_common(disp, dri2_driver_extensions);
842}
Chia-I Wucf69eea2011-06-08 16:33:55 +0800843
Emil Velikov9d84a922019-02-05 15:19:46 +0000844EGLBoolean
845dri2_load_driver_dri3(_EGLDisplay *disp)
846{
847 return dri2_load_driver_common(disp, dri3_driver_extensions);
Chia-I Wucf69eea2011-06-08 16:33:55 +0800848}
849
850EGLBoolean
851dri2_load_driver_swrast(_EGLDisplay *disp)
852{
Emil Velikov9d84a922019-02-05 15:19:46 +0000853 return dri2_load_driver_common(disp, swrast_driver_extensions);
Kristian Høgsberg0defc252010-05-28 16:16:12 -0400854}
855
Marek Olšák4f57ccd2015-06-10 14:45:58 +0200856static unsigned
857dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
858{
859 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
860 unsigned int value = 0;
861
862 if (!rendererQuery ||
863 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
864 return 0;
865
866 return value;
867}
868
Veluri Mithun85edfc02019-01-23 22:45:42 +0530869static const char *
870dri2_query_driver_name(_EGLDisplay *disp)
871{
872 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
873 return dri2_dpy->driver_name;
874}
875
876static char *
877dri2_query_driver_config(_EGLDisplay *disp)
878{
879 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
880 const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
881
882 if (ext->base.version >= 2)
883 return ext->getXml(dri2_dpy->driver_name);
884
885 return strdup(ext->xml);
886}
887
888
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200889void
890dri2_setup_screen(_EGLDisplay *disp)
Kristian Høgsberg0defc252010-05-28 16:16:12 -0400891{
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200892 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Kristian Høgsberg0defc252010-05-28 16:16:12 -0400893 unsigned int api_mask;
894
Tomasz Figa23a09b42017-08-10 22:59:45 +0900895 /*
896 * EGL 1.5 specification defines the default value to 1. Moreover,
897 * eglSwapInterval() is required to clamp requested value to the supported
898 * range. Since the default value is implicitly assumed to be supported,
899 * use it as both minimum and maximum for the platforms that do not allow
900 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
901 * override these values already.
902 */
903 dri2_dpy->min_swap_interval = 1;
904 dri2_dpy->max_swap_interval = 1;
905 dri2_dpy->default_swap_interval = 1;
906
Boyan Dingf35198b2015-07-21 23:44:00 +0800907 if (dri2_dpy->image_driver) {
908 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
909 } else if (dri2_dpy->dri2) {
Ian Romanick38f91f22012-07-18 13:17:50 -0700910 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
Haitao Fengf55d0272011-02-16 23:05:15 -0500911 } else {
912 assert(dri2_dpy->swrast);
Chad Versaceeb099402012-11-20 13:43:11 -0800913 api_mask = 1 << __DRI_API_OPENGL |
914 1 << __DRI_API_GLES |
915 1 << __DRI_API_GLES2 |
916 1 << __DRI_API_GLES3;
Haitao Fengf55d0272011-02-16 23:05:15 -0500917 }
Kristian Høgsberg2168b872010-06-02 22:48:06 -0400918
Chia-I Wua4a38dc2011-01-13 16:53:13 +0800919 disp->ClientAPIs = 0;
Emil Velikov03eaa6c2016-08-25 12:15:13 +0100920 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
Chia-I Wua4a38dc2011-01-13 16:53:13 +0800921 disp->ClientAPIs |= EGL_OPENGL_BIT;
Emil Velikov03eaa6c2016-08-25 12:15:13 +0100922 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
Chia-I Wua4a38dc2011-01-13 16:53:13 +0800923 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
Emil Velikov03eaa6c2016-08-25 12:15:13 +0100924 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
Chia-I Wua4a38dc2011-01-13 16:53:13 +0800925 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
Emil Velikov03eaa6c2016-08-25 12:15:13 +0100926 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
Chad Versaceeb099402012-11-20 13:43:11 -0800927 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
Kristian Høgsberg2168b872010-06-02 22:48:06 -0400928
Boyan Dingf35198b2015-07-21 23:44:00 +0800929 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
Adam Jacksond9f5b192016-09-09 12:25:34 -0400930 disp->Extensions.KHR_no_config_context = EGL_TRUE;
Ian Romanick38f91f22012-07-18 13:17:50 -0700931 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -0400932
Veluri Mithun85edfc02019-01-23 22:45:42 +0530933 if (dri2_dpy->configOptions) {
934 disp->Extensions.MESA_query_driver = EGL_TRUE;
935 }
936
Chris Wilson95ecf3d2016-10-27 19:34:46 +0100937 /* Report back to EGL the bitmask of priorities supported */
938 disp->Extensions.IMG_context_priority =
939 dri2_renderer_query_integer(dri2_dpy,
940 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
941
Tapani Pälli41f7de42017-10-31 10:57:42 +0200942 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
943
Marek Olšák4f57ccd2015-06-10 14:45:58 +0200944 if (dri2_renderer_query_integer(dri2_dpy,
945 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
946 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
947
Boyan Dingf35198b2015-07-21 23:44:00 +0800948 if (dri2_dpy->image_driver ||
949 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
Boyan Ding63c4b7e2015-07-21 23:43:58 +0800950 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
Ian Romanick7b4b4f82012-07-19 16:04:01 -0700951 disp->Extensions.KHR_create_context = EGL_TRUE;
952
953 if (dri2_dpy->robustness)
954 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
955 }
956
Grigori Goronzy49095192017-06-29 02:44:03 +0200957 if (dri2_dpy->no_error)
958 disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
959
Marek Olšák9a0bda22015-04-10 10:56:02 +0200960 if (dri2_dpy->fence) {
961 disp->Extensions.KHR_fence_sync = EGL_TRUE;
Marek Olšáka8617cc2015-04-10 12:04:18 +0200962 disp->Extensions.KHR_wait_sync = EGL_TRUE;
Marek Olšák290a3eb2015-04-10 13:16:30 +0200963 if (dri2_dpy->fence->get_fence_from_cl_event)
964 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
Emil Velikovf0d053c2017-08-07 17:23:19 +0100965 if (dri2_dpy->fence->base.version >= 2 &&
966 dri2_dpy->fence->get_capabilities) {
Rob Clark0201f012016-11-18 08:39:33 -0500967 unsigned capabilities =
968 dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
969 disp->Extensions.ANDROID_native_fence_sync =
970 (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
971 }
Marek Olšák9a0bda22015-04-10 10:56:02 +0200972 }
973
Tapani Pälli6f5b5702017-12-28 10:51:11 +0200974 if (dri2_dpy->blob)
975 disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
976
Dongwon Kim70299472016-04-04 17:14:10 -0700977 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
978
Kristian Høgsberg981e3382011-03-07 20:18:29 -0500979 if (dri2_dpy->image) {
Giovanni Campagnae57ad3d2014-07-23 19:28:52 +0100980 if (dri2_dpy->image->base.version >= 10 &&
981 dri2_dpy->image->getCapabilities != NULL) {
982 int capabilities;
983
984 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
985 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
Dave Airlie8f7338f2014-03-03 13:57:16 +1000986
987 if (dri2_dpy->image->base.version >= 11)
988 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
989 } else {
Giovanni Campagnae57ad3d2014-07-23 19:28:52 +0100990 disp->Extensions.MESA_drm_image = EGL_TRUE;
Dave Airlie8f7338f2014-03-03 13:57:16 +1000991 if (dri2_dpy->image->base.version >= 11)
992 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
993 }
Giovanni Campagnae57ad3d2014-07-23 19:28:52 +0100994
Kristian Høgsberg981e3382011-03-07 20:18:29 -0500995 disp->Extensions.KHR_image_base = EGL_TRUE;
996 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
Abdiel Janulgue7b7af482012-11-27 15:19:54 +0200997 if (dri2_dpy->image->base.version >= 5 &&
998 dri2_dpy->image->createImageFromTexture) {
999 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
1000 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
Emil Velikov4506a742017-06-28 16:41:30 +01001001
1002 if (dri2_renderer_query_integer(dri2_dpy,
1003 __DRI2_RENDERER_HAS_TEXTURE_3D))
1004 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02001005 }
Axel Davy63986f92015-01-14 20:36:04 +01001006#ifdef HAVE_LIBDRM
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02001007 if (dri2_dpy->image->base.version >= 8 &&
1008 dri2_dpy->image->createImageFromDmaBufs) {
1009 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
Varad Gautam4c412292017-05-30 17:23:40 +05301010 disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
1011 }
Samuel Thibaulta594cec2013-11-10 19:32:01 +01001012#endif
Kristian Høgsberg981e3382011-03-07 20:18:29 -05001013 }
Adam Jacksonc0be3aa2016-09-22 03:47:55 -04001014
1015 if (dri2_dpy->flush_control)
1016 disp->Extensions.KHR_context_flush_control = EGL_TRUE;
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02001017
1018 if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1019 disp->Extensions.KHR_partial_update = EGL_TRUE;
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001020}
1021
Emil Velikov47b06f52017-08-05 00:25:46 +01001022void
1023dri2_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
Emil Velikov2c341f22017-05-11 16:20:04 +01001057/* 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.
Emil Velikov8ed08e62014-11-07 03:42:15 +00001059 */
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001060EGLBoolean
1061dri2_create_screen(_EGLDisplay *disp)
1062{
Emil Velikov532ec2e2016-07-15 12:47:37 +01001063 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001064
Boyan Dingf35198b2015-07-21 23:44:00 +08001065 if (dri2_dpy->image_driver) {
1066 dri2_dpy->dri_screen =
1067 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
Emil Velikovf8719462016-08-24 23:32:27 +01001068 dri2_dpy->loader_extensions,
Boyan Dingf35198b2015-07-21 23:44:00 +08001069 dri2_dpy->driver_extensions,
1070 &dri2_dpy->driver_configs,
1071 disp);
1072 } else if (dri2_dpy->dri2) {
Eric Anholtf93533d2013-09-27 11:39:25 -07001073 if (dri2_dpy->dri2->base.version >= 4) {
1074 dri2_dpy->dri_screen =
1075 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
Emil Velikovf8719462016-08-24 23:32:27 +01001076 dri2_dpy->loader_extensions,
Eric Anholtf93533d2013-09-27 11:39:25 -07001077 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,
Emil Velikovf8719462016-08-24 23:32:27 +01001082 dri2_dpy->loader_extensions,
Eric Anholtf93533d2013-09-27 11:39:25 -07001083 &dri2_dpy->driver_configs, disp);
1084 }
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001085 } else {
1086 assert(dri2_dpy->swrast);
Eric Anholtf93533d2013-09-27 11:39:25 -07001087 if (dri2_dpy->swrast->base.version >= 4) {
1088 dri2_dpy->dri_screen =
Emil Velikovf8719462016-08-24 23:32:27 +01001089 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
Eric Anholtf93533d2013-09-27 11:39:25 -07001090 dri2_dpy->driver_extensions,
1091 &dri2_dpy->driver_configs, disp);
1092 } else {
1093 dri2_dpy->dri_screen =
Emil Velikovf8719462016-08-24 23:32:27 +01001094 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
Eric Anholtf93533d2013-09-27 11:39:25 -07001095 &dri2_dpy->driver_configs, disp);
1096 }
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001097 }
1098
1099 if (dri2_dpy->dri_screen == NULL) {
1100 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1101 return EGL_FALSE;
1102 }
1103
Eric Engestrom60f98422017-06-19 00:16:21 +01001104 dri2_dpy->own_dri_screen = true;
Emil Velikov2c341f22017-05-11 16:20:04 +01001105 return EGL_TRUE;
1106}
1107
1108EGLBoolean
1109dri2_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;
Benjamin Franzke992680c2011-06-27 10:23:34 +02001114
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001115 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
Boyan Ding052b3d42015-06-13 15:36:27 +08001116
Emil Velikov2c341f22017-05-11 16:20:04 +01001117 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;
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +02001124
Daniel Stone478fc2d2018-03-19 15:03:22 +00001125#ifdef HAVE_DRI3_MODIFIERS
Louis-Francis Ratté-Boulianne069fdd52017-07-07 02:54:26 -04001126 dri2_dpy->multibuffers_available =
1127 (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1128 dri2_dpy->dri3_minor_version >= 2)) &&
Louis-Francis Ratté-Boulianne3160cb82017-10-06 01:26:51 -04001129 (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1130 dri2_dpy->present_minor_version >= 2)) &&
Louis-Francis Ratté-Boulianne069fdd52017-07-07 02:54:26 -04001131 (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1132#endif
1133
Emil Velikov3948ad82016-08-25 00:51:05 +01001134 dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001135 return EGL_TRUE;
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001136}
1137
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001138/**
Eric Engestromcc034482020-07-20 13:38:24 +02001139 * Called via eglInitialize(), drv->Initialize().
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001140 *
1141 * This must be guaranteed to be called exactly once, even if eglInitialize is
1142 * called many times (without a eglTerminate in between).
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001143 */
1144static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001145dri2_initialize(const _EGLDriver *drv, _EGLDisplay *disp)
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001146{
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001147 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
Eric Anholt2bc1f5c2018-11-15 14:47:30 -08001167 loader_set_logger(_eglLog);
1168
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001169 switch (disp->Platform) {
Haixia Shi6b8accb2015-06-12 10:10:58 -07001170 case _EGL_PLATFORM_SURFACELESS:
Nicolas Boichat4f3f8bb2016-08-03 21:54:22 +08001171 ret = dri2_initialize_surfaceless(drv, disp);
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001172 break;
Emil Velikovd6edcce2019-05-16 18:01:40 +01001173 case _EGL_PLATFORM_DEVICE:
1174 ret = dri2_initialize_device(drv, disp);
1175 break;
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001176 case _EGL_PLATFORM_X11:
Nicolas Boichat4f3f8bb2016-08-03 21:54:22 +08001177 ret = dri2_initialize_x11(drv, disp);
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001178 break;
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001179 case _EGL_PLATFORM_DRM:
Nicolas Boichat4f3f8bb2016-08-03 21:54:22 +08001180 ret = dri2_initialize_drm(drv, disp);
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001181 break;
Benjamin Franzke93aea842011-02-04 12:39:40 +01001182 case _EGL_PLATFORM_WAYLAND:
Nicolas Boichat4f3f8bb2016-08-03 21:54:22 +08001183 ret = dri2_initialize_wayland(drv, disp);
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001184 break;
Chia-I Wu9779f6f2011-08-05 14:39:18 +09001185 case _EGL_PLATFORM_ANDROID:
Nicolas Boichat4f3f8bb2016-08-03 21:54:22 +08001186 ret = dri2_initialize_android(drv, disp);
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001187 break;
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001188 default:
Emil Velikov55245fe2017-11-09 17:55:19 +00001189 unreachable("Callers ensure we cannot get here.");
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001190 return EGL_FALSE;
1191 }
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001192
Emil Velikov2f76dff2017-08-27 11:20:26 +01001193 if (!ret)
1194 return EGL_FALSE;
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001195
Emil Velikov2f76dff2017-08-27 11:20:26 +01001196 dri2_dpy = dri2_egl_display(disp);
1197 dri2_dpy->ref_count++;
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001198
Emil Velikov2f76dff2017-08-27 11:20:26 +01001199 return EGL_TRUE;
Kristian Høgsberg2168b872010-06-02 22:48:06 -04001200}
1201
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001202/**
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001203 * Decrement display reference count, and free up display if necessary.
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001204 */
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001205static void
Emil Velikov02f11582016-08-16 11:03:33 +01001206dri2_display_release(_EGLDisplay *disp)
1207{
Emil Velikovd6dcf3b2016-08-18 16:43:36 +01001208 struct dri2_egl_display *dri2_dpy;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001209
Emil Velikovd6dcf3b2016-08-18 16:43:36 +01001210 if (!disp)
1211 return;
1212
1213 dri2_dpy = dri2_egl_display(disp);
1214
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001215 assert(dri2_dpy->ref_count > 0);
1216 dri2_dpy->ref_count--;
1217
1218 if (dri2_dpy->ref_count > 0)
1219 return;
1220
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001221 _eglCleanupDisplay(disp);
Emil Velikov3e73c022017-05-11 17:13:33 +01001222 dri2_display_destroy(disp);
1223}
1224
1225void
1226dri2_display_destroy(_EGLDisplay *disp)
1227{
1228 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001229
Thomas Hellstrom2db95482017-08-10 15:35:39 +02001230 if (dri2_dpy->own_dri_screen) {
Frank Binns6160bf92018-02-22 13:37:54 +00001231 if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
Thomas Hellstrom2db95482017-08-10 15:35:39 +02001232 dri2_dpy->vtbl->close_screen_notify(disp);
Benjamin Franzke992680c2011-06-27 10:23:34 +02001233 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
Thomas Hellstrom2db95482017-08-10 15:35:39 +02001234 }
Emil Velikova1ac7422015-09-10 14:41:38 +01001235 if (dri2_dpy->fd >= 0)
Haitao Fengf55d0272011-02-16 23:05:15 -05001236 close(dri2_dpy->fd);
Benjamin Franzke992680c2011-06-27 10:23:34 +02001237 if (dri2_dpy->driver)
1238 dlclose(dri2_dpy->driver);
Emil Velikov5cb1cad2014-06-02 12:26:17 +01001239 free(dri2_dpy->driver_name);
Benjamin Franzkeb8325fd2011-06-11 22:07:02 +02001240
Frank Binnsd6f669b2016-06-17 18:41:22 +01001241#ifdef HAVE_WAYLAND_PLATFORM
1242 free(dri2_dpy->device_name);
1243#endif
1244
Chad Versacef506ef62014-01-28 18:53:56 -08001245 switch (disp->Platform) {
Chad Versacef506ef62014-01-28 18:53:56 -08001246 case _EGL_PLATFORM_X11:
Emil Velikov938fcab2017-11-09 18:58:52 +00001247 dri2_teardown_x11(dri2_dpy);
Chad Versacef506ef62014-01-28 18:53:56 -08001248 break;
Chad Versacef506ef62014-01-28 18:53:56 -08001249 case _EGL_PLATFORM_DRM:
Emil Velikov9b022302017-11-16 18:33:22 +00001250 dri2_teardown_drm(dri2_dpy);
Chad Versacef506ef62014-01-28 18:53:56 -08001251 break;
Chad Versacef506ef62014-01-28 18:53:56 -08001252 case _EGL_PLATFORM_WAYLAND:
Emil Velikov9b022302017-11-16 18:33:22 +00001253 dri2_teardown_wayland(dri2_dpy);
Chad Versacef506ef62014-01-28 18:53:56 -08001254 break;
Chad Versacef506ef62014-01-28 18:53:56 -08001255 default:
Emil Velikov66148042017-11-13 14:02:56 +00001256 /* TODO: add teardown for other platforms */
Chad Versacef506ef62014-01-28 18:53:56 -08001257 break;
Benjamin Franzkeb8325fd2011-06-11 22:07:02 +02001258 }
1259
Emil Velikov38cec032014-11-07 03:33:56 +00001260 /* The drm platform does not create the screen/driver_configs but reuses
1261 * the ones from the gbm device. As such the gbm itself is responsible
1262 * for the cleanup.
1263 */
Tapani Pälli12196d12017-05-12 12:18:32 +03001264 if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
Chad Versacecd717cb2017-06-22 11:00:40 -07001265 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
Emil Velikov38cec032014-11-07 03:33:56 +00001266 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1267 free(dri2_dpy->driver_configs);
1268 }
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001269 free(dri2_dpy);
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001270 disp->DriverData = NULL;
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001271}
1272
Gwan-gyeong Mun640b6e62017-08-05 00:16:11 +09001273__DRIbuffer *
1274dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1275 unsigned int att, unsigned int format)
1276{
1277 struct dri2_egl_display *dri2_dpy =
1278 dri2_egl_display(dri2_surf->base.Resource.Display);
1279
1280 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1281 return NULL;
1282
1283 if (!dri2_surf->local_buffers[att]) {
1284 dri2_surf->local_buffers[att] =
1285 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1286 dri2_surf->base.Width, dri2_surf->base.Height);
1287 }
1288
1289 return dri2_surf->local_buffers[att];
1290}
1291
1292void
1293dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1294{
1295 struct dri2_egl_display *dri2_dpy =
1296 dri2_egl_display(dri2_surf->base.Resource.Display);
1297
1298 for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1299 if (dri2_surf->local_buffers[i]) {
1300 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1301 dri2_surf->local_buffers[i]);
1302 dri2_surf->local_buffers[i] = NULL;
1303 }
1304 }
1305}
1306
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001307/**
Eric Engestromcc034482020-07-20 13:38:24 +02001308 * Called via eglTerminate(), drv->Terminate().
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001309 *
1310 * This must be guaranteed to be called exactly once, even if eglTerminate is
1311 * called many times (without a eglInitialize in between).
1312 */
1313static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001314dri2_terminate(const _EGLDriver *drv, _EGLDisplay *disp)
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001315{
1316 /* Release all non-current Context/Surfaces. */
1317 _eglReleaseDisplayResources(drv, disp);
1318
1319 dri2_display_release(disp);
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001320
1321 return EGL_TRUE;
1322}
1323
Chad Versace5cf85362012-11-09 14:06:41 -08001324/**
1325 * Set the error code after a call to
1326 * dri2_egl_display::dri2::createContextAttribs.
1327 */
1328static void
1329dri2_create_context_attribs_error(int dri_error)
1330{
1331 EGLint egl_error;
1332
1333 switch (dri_error) {
1334 case __DRI_CTX_ERROR_SUCCESS:
1335 return;
1336
1337 case __DRI_CTX_ERROR_NO_MEMORY:
1338 egl_error = EGL_BAD_ALLOC;
1339 break;
1340
1341 /* From the EGL_KHR_create_context spec, section "Errors":
1342 *
1343 * * If <config> does not support a client API context compatible
1344 * with the requested API major and minor version, [...] context flags,
1345 * and context reset notification behavior (for client API types where
1346 * these attributes are supported), then an EGL_BAD_MATCH error is
1347 * generated.
1348 *
1349 * * If an OpenGL ES context is requested and the values for
1350 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1351 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1352 * is not defined, than an EGL_BAD_MATCH error is generated.
1353 *
1354 * * If an OpenGL context is requested, the requested version is
1355 * greater than 3.2, and the value for attribute
1356 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1357 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1358 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1359 * one of these bits set; or if the implementation does not support
1360 * the requested profile, then an EGL_BAD_MATCH error is generated.
1361 */
1362 case __DRI_CTX_ERROR_BAD_API:
1363 case __DRI_CTX_ERROR_BAD_VERSION:
1364 case __DRI_CTX_ERROR_BAD_FLAG:
1365 egl_error = EGL_BAD_MATCH;
1366 break;
1367
1368 /* From the EGL_KHR_create_context spec, section "Errors":
1369 *
1370 * * If an attribute name or attribute value in <attrib_list> is not
1371 * recognized (including unrecognized bits in bitmask attributes),
1372 * then an EGL_BAD_ATTRIBUTE error is generated."
1373 */
1374 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1375 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1376 egl_error = EGL_BAD_ATTRIBUTE;
1377 break;
1378
1379 default:
Eric Engestromea738a92018-10-28 18:20:20 +00001380 assert(!"unknown dri_error code");
Chad Versace5cf85362012-11-09 14:06:41 -08001381 egl_error = EGL_BAD_MATCH;
1382 break;
1383 }
1384
1385 _eglError(egl_error, "dri2_create_context");
1386}
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001387
Boyan Dingb9ea6082015-07-21 23:43:56 +08001388static bool
1389dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1390 struct dri2_egl_display *dri2_dpy,
1391 uint32_t *ctx_attribs,
1392 unsigned *num_attribs)
1393{
1394 int pos = 0;
1395
Chris Wilson5c561832016-10-27 19:54:49 +01001396 assert(*num_attribs >= NUM_ATTRIBS);
Boyan Dingb9ea6082015-07-21 23:43:56 +08001397
1398 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1399 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1400 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1401 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1402
Grigori Goronzy49095192017-06-29 02:44:03 +02001403 if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {
Boyan Dingb9ea6082015-07-21 23:43:56 +08001404 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1405 * extension, don't even try to send it the robust-access flag.
1406 * It may explode. Instead, generate the required EGL error here.
1407 */
1408 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1409 && !dri2_dpy->robustness) {
1410 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1411 return false;
1412 }
1413
1414 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
Grigori Goronzy49095192017-06-29 02:44:03 +02001415 ctx_attribs[pos++] = dri2_ctx->base.Flags |
Kenneth Graunkec7af6d22017-07-14 14:26:42 -07001416 (dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);
Boyan Dingb9ea6082015-07-21 23:43:56 +08001417 }
1418
1419 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1420 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1421 * extension, don't even try to send it a reset strategy. It may
1422 * explode. Instead, generate the required EGL error here.
1423 */
1424 if (!dri2_dpy->robustness) {
1425 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1426 return false;
1427 }
1428
1429 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1430 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1431 }
1432
Chris Wilson5c561832016-10-27 19:54:49 +01001433 if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1434 unsigned val;
1435
1436 switch (dri2_ctx->base.ContextPriority) {
1437 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1438 val = __DRI_CTX_PRIORITY_HIGH;
1439 break;
1440 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1441 val = __DRI_CTX_PRIORITY_MEDIUM;
1442 break;
1443 case EGL_CONTEXT_PRIORITY_LOW_IMG:
1444 val = __DRI_CTX_PRIORITY_LOW;
1445 break;
1446 default:
1447 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1448 return false;
1449 }
1450
1451 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1452 ctx_attribs[pos++] = val;
1453 }
1454
Adam Jacksonc0be3aa2016-09-22 03:47:55 -04001455 if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1456 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1457 ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1458 }
1459
Boyan Dingb9ea6082015-07-21 23:43:56 +08001460 *num_attribs = pos;
1461
1462 return true;
1463}
1464
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001465/**
Eric Engestromcc034482020-07-20 13:38:24 +02001466 * Called via eglCreateContext(), drv->CreateContext().
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001467 */
1468static _EGLContext *
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001469dri2_create_context(const _EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
Emil Velikovebc68e32016-08-25 00:12:13 +01001470 _EGLContext *share_list, const EGLint *attrib_list)
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001471{
1472 struct dri2_egl_context *dri2_ctx;
1473 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1474 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
Ian Romanickcb77f5d2012-07-18 14:37:28 -07001475 __DRIcontext *shared =
1476 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001477 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -04001478 const __DRIconfig *dri_config;
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001479 int api;
Adam Jackson9ef71582016-09-22 03:38:01 -04001480 unsigned error;
1481 unsigned num_attribs = NUM_ATTRIBS;
1482 uint32_t ctx_attribs[NUM_ATTRIBS];
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001483
Ian Romanickeade9462010-09-23 09:39:42 -07001484 (void) drv;
1485
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001486 dri2_ctx = malloc(sizeof *dri2_ctx);
1487 if (!dri2_ctx) {
1488 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1489 return NULL;
1490 }
1491
Kristian Høgsberg9c94fae2010-02-05 10:49:33 -05001492 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1493 goto cleanup;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001494
Kenneth Graunke4061bbc2017-02-28 17:20:52 -08001495 /* The EGL_EXT_create_context_robustness spec says:
1496 *
1497 * "Add to the eglCreateContext context creation errors: [...]
1498 *
1499 * * If the reset notification behavior of <share_context> and the
1500 * newly created context are different then an EGL_BAD_MATCH error is
1501 * generated."
1502 */
1503 if (share_list && share_list->ResetNotificationStrategy !=
1504 dri2_ctx->base.ResetNotificationStrategy) {
1505 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1506 goto cleanup;
1507 }
1508
Grigori Goronzy49095192017-06-29 02:44:03 +02001509 /* The EGL_KHR_create_context_no_error spec says:
1510 *
1511 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1512 * used to create <share_context> does not match the value of
1513 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1514 */
1515 if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1516 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1517 goto cleanup;
1518 }
1519
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001520 switch (dri2_ctx->base.ClientAPI) {
1521 case EGL_OPENGL_ES_API:
Ian Romanick3fd79dd2012-07-18 15:59:15 -07001522 switch (dri2_ctx->base.ClientMajorVersion) {
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001523 case 1:
1524 api = __DRI_API_GLES;
1525 break;
1526 case 2:
Ian Romanickd786bf22013-01-15 14:17:46 -08001527 api = __DRI_API_GLES2;
Chad Versaceeb099402012-11-20 13:43:11 -08001528 break;
Ian Romanickdbecb412012-07-19 16:12:13 -07001529 case 3:
Chad Versaceeb099402012-11-20 13:43:11 -08001530 api = __DRI_API_GLES3;
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001531 break;
1532 default:
Emil Velikova3e78ba2014-05-08 16:49:45 +01001533 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1534 free(dri2_ctx);
1535 return NULL;
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001536 }
1537 break;
1538 case EGL_OPENGL_API:
Ian Romanick7b4b4f82012-07-19 16:04:01 -07001539 if ((dri2_ctx->base.ClientMajorVersion >= 4
1540 || (dri2_ctx->base.ClientMajorVersion == 3
1541 && dri2_ctx->base.ClientMinorVersion >= 2))
1542 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1543 api = __DRI_API_OPENGL_CORE;
Timothy Arceri74f96b02019-08-02 11:38:45 +10001544 else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1545 dri2_ctx->base.ClientMinorVersion == 1)
1546 api = __DRI_API_OPENGL_CORE;
Ian Romanick7b4b4f82012-07-19 16:04:01 -07001547 else
1548 api = __DRI_API_OPENGL;
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001549 break;
1550 default:
1551 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
Vinson Lee080e91a2013-01-31 23:41:14 -08001552 free(dri2_ctx);
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001553 return NULL;
1554 }
1555
Benjamin Franzke87dde5b2011-02-09 15:30:20 +01001556 if (conf != NULL) {
1557 /* The config chosen here isn't necessarily
1558 * used for surfaces later.
1559 * A pixmap surface will use the single config.
1560 * This opportunity depends on disabling the
1561 * doubleBufferMode check in
1562 * src/mesa/main/context.c:check_compatible()
1563 */
Eric Engestromb81cfc72017-06-21 10:40:31 +01001564 if (dri2_config->dri_config[1][0])
1565 dri_config = dri2_config->dri_config[1][0];
Benjamin Franzke87dde5b2011-02-09 15:30:20 +01001566 else
Eric Engestromb81cfc72017-06-21 10:40:31 +01001567 dri_config = dri2_config->dri_config[0][0];
Benjamin Franzke87dde5b2011-02-09 15:30:20 +01001568 }
Kristian Høgsbergefd4a8c2010-07-27 18:25:54 -04001569 else
1570 dri_config = NULL;
1571
Adam Jackson9ef71582016-09-22 03:38:01 -04001572 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1573 &num_attribs))
1574 goto cleanup;
1575
Boyan Dingf35198b2015-07-21 23:44:00 +08001576 if (dri2_dpy->image_driver) {
Boyan Dingf35198b2015-07-21 23:44:00 +08001577 dri2_ctx->dri_context =
1578 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1579 api,
1580 dri_config,
1581 shared,
1582 num_attribs / 2,
1583 ctx_attribs,
1584 & error,
1585 dri2_ctx);
1586 dri2_create_context_attribs_error(error);
1587 } else if (dri2_dpy->dri2) {
Ian Romanickce557412012-07-18 14:41:28 -07001588 if (dri2_dpy->dri2->base.version >= 3) {
Emil Velikovebc68e32016-08-25 00:12:13 +01001589 dri2_ctx->dri_context =
1590 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
Ian Romanickce557412012-07-18 14:41:28 -07001591 api,
1592 dri_config,
1593 shared,
Ian Romanick7b4b4f82012-07-19 16:04:01 -07001594 num_attribs / 2,
Ian Romanickce557412012-07-18 14:41:28 -07001595 ctx_attribs,
1596 & error,
1597 dri2_ctx);
Emil Velikovebc68e32016-08-25 00:12:13 +01001598 dri2_create_context_attribs_error(error);
Ian Romanickce557412012-07-18 14:41:28 -07001599 } else {
Emil Velikovebc68e32016-08-25 00:12:13 +01001600 dri2_ctx->dri_context =
1601 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1602 api,
1603 dri_config,
Ian Romanickcb77f5d2012-07-18 14:37:28 -07001604 shared,
Emil Velikovebc68e32016-08-25 00:12:13 +01001605 dri2_ctx);
Haitao Fengf55d0272011-02-16 23:05:15 -05001606 }
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001607 } else {
Haitao Fengf55d0272011-02-16 23:05:15 -05001608 assert(dri2_dpy->swrast);
Boyan Ding6345d2d2015-07-21 23:43:57 +08001609 if (dri2_dpy->swrast->base.version >= 3) {
Boyan Ding6345d2d2015-07-21 23:43:57 +08001610 dri2_ctx->dri_context =
1611 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1612 api,
1613 dri_config,
1614 shared,
1615 num_attribs / 2,
1616 ctx_attribs,
1617 & error,
1618 dri2_ctx);
1619 dri2_create_context_attribs_error(error);
1620 } else {
1621 dri2_ctx->dri_context =
1622 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1623 api,
1624 dri_config,
1625 shared,
1626 dri2_ctx);
1627 }
Kristian Høgsberge2fd98d2010-04-27 11:38:46 -04001628 }
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001629
Kristian Høgsberg9c94fae2010-02-05 10:49:33 -05001630 if (!dri2_ctx->dri_context)
1631 goto cleanup;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001632
1633 return &dri2_ctx->base;
Kristian Høgsberg9c94fae2010-02-05 10:49:33 -05001634
1635 cleanup:
1636 free(dri2_ctx);
1637 return NULL;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001638}
1639
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001640/**
Eric Engestromcc034482020-07-20 13:38:24 +02001641 * Called via eglDestroyContext(), drv->DestroyContext().
Cooper Yuanf5e757e2011-08-14 15:14:17 +08001642 */
1643static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001644dri2_destroy_context(const _EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
Cooper Yuanf5e757e2011-08-14 15:14:17 +08001645{
1646 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1647 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1648
1649 if (_eglPutContext(ctx)) {
1650 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1651 free(dri2_ctx);
1652 }
1653
1654 return EGL_TRUE;
1655}
1656
Zhongmin Wue013ce82017-09-15 18:32:42 +01001657EGLBoolean
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001658dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
Paulo Zanoni04ecda32019-05-01 15:42:26 -07001659 _EGLConfig *conf, const EGLint *attrib_list,
1660 EGLBoolean enable_out_fence, void *native_surface)
Zhongmin Wue013ce82017-09-15 18:32:42 +01001661{
1662 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001663 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Zhongmin Wue013ce82017-09-15 18:32:42 +01001664
1665 dri2_surf->out_fence_fd = -1;
1666 dri2_surf->enable_out_fence = false;
1667 if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1668 dri2_dpy->fence->get_capabilities &&
1669 (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1670 __DRI_FENCE_CAP_NATIVE_FD)) {
1671 dri2_surf->enable_out_fence = enable_out_fence;
1672 }
1673
Paulo Zanoni04ecda32019-05-01 15:42:26 -07001674 return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
Zhongmin Wue013ce82017-09-15 18:32:42 +01001675}
1676
1677static void
1678dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1679{
1680 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1681
1682 if (dri2_surf->out_fence_fd >= 0)
1683 close(dri2_surf->out_fence_fd);
1684
1685 dri2_surf->out_fence_fd = fence_fd;
1686}
1687
1688void
1689dri2_fini_surface(_EGLSurface *surf)
1690{
1691 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1692
1693 dri2_surface_set_out_fence_fd(surf, -1);
1694 dri2_surf->enable_out_fence = false;
1695}
1696
Emil Velikov8cf83f92016-08-18 16:33:39 +01001697static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001698dri2_destroy_surface(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Emil Velikov8cf83f92016-08-18 16:33:39 +01001699{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001700 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Emil Velikov8cf83f92016-08-18 16:33:39 +01001701
1702 if (!_eglPutSurface(surf))
1703 return EGL_TRUE;
1704
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001705 return dri2_dpy->vtbl->destroy_surface(drv, disp, surf);
Emil Velikov8cf83f92016-08-18 16:33:39 +01001706}
1707
Zhongmin Wue013ce82017-09-15 18:32:42 +01001708static void
1709dri2_surf_update_fence_fd(_EGLContext *ctx,
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001710 _EGLDisplay *disp, _EGLSurface *surf)
Zhongmin Wue013ce82017-09-15 18:32:42 +01001711{
1712 __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001713 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Zhongmin Wue013ce82017-09-15 18:32:42 +01001714 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1715 int fence_fd = -1;
1716 void *fence;
1717
1718 if (!dri2_surf->enable_out_fence)
1719 return;
1720
1721 fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1722 if (fence) {
1723 fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1724 fence);
1725 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1726 }
1727 dri2_surface_set_out_fence_fd(surf, fence_fd);
1728}
1729
Emil Velikov2282ec02019-05-16 18:01:38 +01001730EGLBoolean
1731dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1732 const __DRIconfig *config,
Mathias Fröhlicha7ecf782019-06-07 07:12:42 +02001733 struct dri2_egl_surface *dri2_surf,
1734 void *loaderPrivate)
Emil Velikov2282ec02019-05-16 18:01:38 +01001735{
1736 __DRIcreateNewDrawableFunc createNewDrawable;
Emil Velikov2282ec02019-05-16 18:01:38 +01001737
1738 if (dri2_dpy->image_driver)
1739 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1740 else if (dri2_dpy->dri2)
1741 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1742 else if (dri2_dpy->swrast)
1743 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1744 else
1745 return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1746
Eric Engestrom1361ab32018-11-27 12:48:42 +00001747 dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1748 config, loaderPrivate);
Emil Velikov2282ec02019-05-16 18:01:38 +01001749 if (dri2_surf->dri_drawable == NULL)
1750 return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1751
1752 return EGL_TRUE;
1753}
1754
Cooper Yuanf5e757e2011-08-14 15:14:17 +08001755/**
Eric Engestromcc034482020-07-20 13:38:24 +02001756 * Called via eglMakeCurrent(), drv->MakeCurrent().
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001757 */
1758static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001759dri2_make_current(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
Emil Velikovebc68e32016-08-25 00:12:13 +01001760 _EGLSurface *rsurf, _EGLContext *ctx)
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001761{
1762 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001763 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
Chad Versaceed7c6942018-04-30 22:35:17 -07001764 _EGLDisplay *old_disp = NULL;
1765 struct dri2_egl_display *old_dri2_dpy = NULL;
Chia-I Wud19afc52010-10-23 12:52:26 +08001766 _EGLContext *old_ctx;
1767 _EGLSurface *old_dsurf, *old_rsurf;
Boyan Dinga25df542015-07-21 23:43:59 +08001768 _EGLSurface *tmp_dsurf, *tmp_rsurf;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001769 __DRIdrawable *ddraw, *rdraw;
1770 __DRIcontext *cctx;
Luigi Santivetti2907fae2020-06-30 14:59:45 +01001771 EGLint egl_error = EGL_SUCCESS;
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001772
1773 if (!dri2_dpy)
1774 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001775
Luigi Santivetti6b129992020-06-30 10:14:08 +01001776 /* make new bindings, set the EGL error otherwise */
1777 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001778 return EGL_FALSE;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001779
Benjamin Franzke4e8f95f2011-02-09 21:19:45 +01001780 if (old_ctx) {
1781 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
Luigi Santivetti6b129992020-06-30 10:14:08 +01001782 old_disp = old_ctx->Resource.Display;
1783 old_dri2_dpy = dri2_egl_display(old_disp);
1784
1785 /* flush before context switch */
1786 dri2_gl_flush();
Zhongmin Wue013ce82017-09-15 18:32:42 +01001787
1788 if (old_dsurf)
1789 dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
Chad Versaceed7c6942018-04-30 22:35:17 -07001790
1791 /* Disable shared buffer mode */
1792 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1793 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1794 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1795 }
1796
Benjamin Franzke4e8f95f2011-02-09 21:19:45 +01001797 dri2_dpy->core->unbindContext(old_cctx);
1798 }
1799
Luigi Santivetti6b129992020-06-30 10:14:08 +01001800 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1801 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1802 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
Nicolas Boichat9ee683f2016-07-22 11:27:41 +08001803
Luigi Santivetti8b0b6f92020-06-30 11:32:49 +01001804 if (cctx || ddraw || rdraw) {
1805 if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
Luigi Santivetti2907fae2020-06-30 14:59:45 +01001806 _EGLContext *tmp_ctx;
1807
1808 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1809 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1810 * as EGL_SUCCESS.
1811 */
1812 egl_error = EGL_BAD_MATCH;
1813
Luigi Santivetti8b0b6f92020-06-30 11:32:49 +01001814 /* undo the previous _eglBindContext */
1815 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1816 assert(&dri2_ctx->base == ctx &&
1817 tmp_dsurf == dsurf &&
1818 tmp_rsurf == rsurf);
Emil Velikovebffa7b2016-10-14 16:07:33 +01001819
Luigi Santivetti8b0b6f92020-06-30 11:32:49 +01001820 _eglPutSurface(dsurf);
1821 _eglPutSurface(rsurf);
1822 _eglPutContext(ctx);
1823
1824 _eglPutSurface(old_dsurf);
1825 _eglPutSurface(old_rsurf);
1826 _eglPutContext(old_ctx);
1827
Luigi Santivetti2907fae2020-06-30 14:59:45 +01001828 ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1829 rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1830 cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
Chad Versaceed7c6942018-04-30 22:35:17 -07001831
Luigi Santivetti2907fae2020-06-30 14:59:45 +01001832 /* undo the previous dri2_dpy->core->unbindContext */
1833 if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1834 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1835 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1836 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1837 }
1838
1839 return _eglError(egl_error, "eglMakeCurrent");
1840 }
1841
1842 /* We cannot restore the same state as it was before calling
1843 * eglMakeCurrent() and the spec isn't clear about what to do. We
1844 * can prevent EGL from calling into the DRI driver with no DRI
1845 * context bound.
1846 */
1847 dsurf = rsurf = NULL;
1848 ctx = NULL;
1849
1850 _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1851 assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1852 tmp_rsurf == old_rsurf);
1853
1854 _eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
Frank Binnsd0e32e52020-07-28 15:50:30 +01001855 } else {
1856 /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1857 * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1858 * EGLDisplay is terminated and then initialized again while a
1859 * context is still bound. See dri2_intitialize() for a more in depth
1860 * explanation. */
1861 dri2_dpy->ref_count++;
Luigi Santivetti2907fae2020-06-30 14:59:45 +01001862 }
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001863 }
Chad Versaceeabf5972018-04-07 13:01:15 -07001864
1865 dri2_destroy_surface(drv, disp, old_dsurf);
1866 dri2_destroy_surface(drv, disp, old_rsurf);
1867
Chad Versaceeabf5972018-04-07 13:01:15 -07001868 if (old_ctx) {
Chad Versaceeabf5972018-04-07 13:01:15 -07001869 dri2_destroy_context(drv, disp, old_ctx);
1870 dri2_display_release(old_disp);
1871 }
1872
Luigi Santivetti2907fae2020-06-30 14:59:45 +01001873 if (egl_error != EGL_SUCCESS)
1874 return _eglError(egl_error, "eglMakeCurrent");
1875
Chad Versaceed7c6942018-04-30 22:35:17 -07001876 if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1877 dri2_dpy->vtbl->set_shared_buffer_mode) {
1878 /* Always update the shared buffer mode. This is obviously needed when
1879 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1880 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1881 * case where external non-EGL API may have changed window's shared
1882 * buffer mode since we last saw it.
1883 */
1884 bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1885 dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1886 }
1887
Chad Versaceeabf5972018-04-07 13:01:15 -07001888 return EGL_TRUE;
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001889}
1890
Boyan Dinga25df542015-07-21 23:43:59 +08001891__DRIdrawable *
1892dri2_surface_get_dri_drawable(_EGLSurface *surf)
1893{
1894 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1895
1896 return dri2_surf->dri_drawable;
1897}
1898
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001899/*
Eric Engestromcc034482020-07-20 13:38:24 +02001900 * Called from eglGetProcAddress() via drv->GetProcAddress().
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001901 */
1902static _EGLProc
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001903dri2_get_proc_address(const _EGLDriver *drv, const char *procname)
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001904{
Emil Velikovd884d8d2017-09-27 17:36:29 +01001905 return _glapi_get_proc_address(procname);
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001906}
1907
Chad Versace0a0c8812014-01-28 16:39:09 -08001908static _EGLSurface*
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001909dri2_create_window_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -08001910 _EGLConfig *conf, void *native_window,
Chad Versace0a0c8812014-01-28 16:39:09 -08001911 const EGLint *attrib_list)
1912{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001913 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1914 return dri2_dpy->vtbl->create_window_surface(drv, disp, conf, native_window,
Chad Versace0a0c8812014-01-28 16:39:09 -08001915 attrib_list);
1916}
1917
Chad Versacebc8b07a2014-01-28 16:45:07 -08001918static _EGLSurface*
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001919dri2_create_pixmap_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -08001920 _EGLConfig *conf, void *native_pixmap,
Chad Versacebc8b07a2014-01-28 16:45:07 -08001921 const EGLint *attrib_list)
1922{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001923 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestrom179e4422020-07-11 00:29:17 +02001924 if (!dri2_dpy->vtbl->create_pixmap_surface)
1925 return NULL;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001926 return dri2_dpy->vtbl->create_pixmap_surface(drv, disp, conf, native_pixmap,
Chad Versacebc8b07a2014-01-28 16:45:07 -08001927 attrib_list);
1928}
1929
Chad Versacebf200762014-01-28 17:03:03 -08001930static _EGLSurface*
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001931dri2_create_pbuffer_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Chad Versacebf200762014-01-28 17:03:03 -08001932 _EGLConfig *conf, const EGLint *attrib_list)
1933{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001934 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestrom43e2d802020-07-11 00:31:32 +02001935 if (!dri2_dpy->vtbl->create_pbuffer_surface)
1936 return NULL;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001937 return dri2_dpy->vtbl->create_pbuffer_surface(drv, disp, conf, attrib_list);
Chad Versacebf200762014-01-28 17:03:03 -08001938}
1939
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05001940static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001941dri2_swap_interval(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
Chad Versace8b9298a2014-01-28 12:34:19 -08001942 EGLint interval)
1943{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001944 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestromdd9eb8d2017-08-02 17:25:44 +01001945 if (!dri2_dpy->vtbl->swap_interval)
1946 return EGL_TRUE;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001947 return dri2_dpy->vtbl->swap_interval(drv, disp, surf, interval);
Chad Versace8b9298a2014-01-28 12:34:19 -08001948}
1949
Eric Anholt70e8ccc2014-12-21 11:51:33 -08001950/**
1951 * Asks the client API to flush any rendering to the drawable so that we can
1952 * do our swapbuffers.
1953 */
1954void
1955dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1956{
1957 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Boyan Dinga25df542015-07-21 23:43:59 +08001958 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
Eric Anholt70e8ccc2014-12-21 11:51:33 -08001959
1960 if (dri2_dpy->flush) {
1961 if (dri2_dpy->flush->base.version >= 4) {
1962 /* We know there's a current context because:
1963 *
1964 * "If surface is not bound to the calling thread’s current
1965 * context, an EGL_BAD_SURFACE error is generated."
1966 */
1967 _EGLContext *ctx = _eglGetCurrentContext();
1968 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1969
1970 /* From the EGL 1.4 spec (page 52):
1971 *
1972 * "The contents of ancillary buffers are always undefined
1973 * after calling eglSwapBuffers."
1974 */
1975 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
Boyan Dinga25df542015-07-21 23:43:59 +08001976 dri_drawable,
Eric Anholt70e8ccc2014-12-21 11:51:33 -08001977 __DRI2_FLUSH_DRAWABLE |
1978 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1979 __DRI2_THROTTLE_SWAPBUFFER);
1980 } else {
Boyan Dinga25df542015-07-21 23:43:59 +08001981 dri2_dpy->flush->flush(dri_drawable);
Eric Anholt70e8ccc2014-12-21 11:51:33 -08001982 }
1983 }
1984}
1985
Chad Versace8b9298a2014-01-28 12:34:19 -08001986static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02001987dri2_swap_buffers(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Chad Versacead173bc2014-01-28 16:21:21 -08001988{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001989 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02001990 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
Zhongmin Wue013ce82017-09-15 18:32:42 +01001991 _EGLContext *ctx = _eglGetCurrentContext();
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02001992 EGLBoolean ret;
Zhongmin Wue013ce82017-09-15 18:32:42 +01001993
1994 if (ctx && surf)
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00001995 dri2_surf_update_fence_fd(ctx, disp, surf);
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02001996 ret = dri2_dpy->vtbl->swap_buffers(drv, disp, surf);
1997
1998 /* SwapBuffers marks the end of the frame; reset the damage region for
1999 * use again next time.
2000 */
2001 if (ret && dri2_dpy->buffer_damage &&
2002 dri2_dpy->buffer_damage->set_damage_region)
2003 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2004
2005 return ret;
Chad Versacead173bc2014-01-28 16:21:21 -08002006}
2007
2008static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002009dri2_swap_buffers_with_damage(const _EGLDriver *drv, _EGLDisplay *disp,
Chad Versaced03948a2014-01-28 16:26:44 -08002010 _EGLSurface *surf,
2011 const EGLint *rects, EGLint n_rects)
2012{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002013 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02002014 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
Zhongmin Wue013ce82017-09-15 18:32:42 +01002015 _EGLContext *ctx = _eglGetCurrentContext();
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02002016 EGLBoolean ret;
Zhongmin Wue013ce82017-09-15 18:32:42 +01002017
2018 if (ctx && surf)
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002019 dri2_surf_update_fence_fd(ctx, disp, surf);
Eric Engestrom7d5a13e2020-07-11 00:36:08 +02002020 if (dri2_dpy->vtbl->swap_buffers_with_damage)
2021 ret = dri2_dpy->vtbl->swap_buffers_with_damage(drv, disp, surf,
2022 rects, n_rects);
2023 else
2024 ret = dri2_dpy->vtbl->swap_buffers(drv, disp, surf);
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02002025
2026 /* SwapBuffers marks the end of the frame; reset the damage region for
2027 * use again next time.
2028 */
2029 if (ret && dri2_dpy->buffer_damage &&
2030 dri2_dpy->buffer_damage->set_damage_region)
2031 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2032
2033 return ret;
Chad Versaced03948a2014-01-28 16:26:44 -08002034}
2035
Chad Versacebc2cbc02014-01-28 17:03:03 -08002036static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002037dri2_swap_buffers_region(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
Chad Versace75d398e2014-01-28 17:03:03 -08002038 EGLint numRects, const EGLint *rects)
2039{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002040 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02002041 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2042 EGLBoolean ret;
2043
Eric Engestrom1ba5075a2020-07-11 00:38:38 +02002044 if (!dri2_dpy->vtbl->swap_buffers_region)
2045 return EGL_FALSE;
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02002046 ret = dri2_dpy->vtbl->swap_buffers_region(drv, disp, surf, numRects, rects);
2047
2048 /* SwapBuffers marks the end of the frame; reset the damage region for
2049 * use again next time.
2050 */
2051 if (ret && dri2_dpy->buffer_damage &&
2052 dri2_dpy->buffer_damage->set_damage_region)
2053 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2054
2055 return ret;
Chad Versace75d398e2014-01-28 17:03:03 -08002056}
2057
2058static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002059dri2_set_damage_region(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
Harish Krupo98275472017-06-09 20:13:34 +05302060 EGLint *rects, EGLint n_rects)
2061{
Harish Krupoa4a8ebe2019-08-12 12:07:06 +02002062 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2063 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2064
2065 if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
2066 return EGL_FALSE;
2067
2068 dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2069 return EGL_TRUE;
Harish Krupo98275472017-06-09 20:13:34 +05302070}
2071
2072static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002073dri2_post_sub_buffer(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
Chad Versace688a0e82014-01-28 17:03:03 -08002074 EGLint x, EGLint y, EGLint width, EGLint height)
2075{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002076 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestrom2d5f12ae32020-07-11 00:39:41 +02002077 if (!dri2_dpy->vtbl->post_sub_buffer)
2078 return EGL_FALSE;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002079 return dri2_dpy->vtbl->post_sub_buffer(drv, disp, surf, x, y, width, height);
Chad Versace688a0e82014-01-28 17:03:03 -08002080}
2081
2082static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002083dri2_copy_buffers(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
Chad Versace6d1f83e2014-01-07 14:54:51 -08002084 void *native_pixmap_target)
Chad Versacebc2cbc02014-01-28 17:03:03 -08002085{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002086 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestrom90000b02020-07-11 00:40:20 +02002087 if (!dri2_dpy->vtbl->copy_buffers)
2088 return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002089 return dri2_dpy->vtbl->copy_buffers(drv, disp, surf, native_pixmap_target);
Chad Versacebc2cbc02014-01-28 17:03:03 -08002090}
2091
Chad Versace3fdfbd22014-01-28 17:03:03 -08002092static EGLint
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002093dri2_query_buffer_age(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Chad Versace3fdfbd22014-01-28 17:03:03 -08002094{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002095 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestromc5fb1fb2020-07-11 00:40:57 +02002096 if (!dri2_dpy->vtbl->query_buffer_age)
2097 return 0;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002098 return dri2_dpy->vtbl->query_buffer_age(drv, disp, surf);
Chad Versace3fdfbd22014-01-28 17:03:03 -08002099}
2100
Chad Versaced03948a2014-01-28 16:26:44 -08002101static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002102dri2_wait_client(const _EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05002103{
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -05002104 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Boyan Dinga25df542015-07-21 23:43:59 +08002105 _EGLSurface *surf = ctx->DrawSurface;
2106 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -05002107
Ian Romanickeade9462010-09-23 09:39:42 -07002108 (void) drv;
2109
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -05002110 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2111 * we need to copy fake to real here.*/
2112
José Fonseca5048e692013-02-05 13:43:01 +00002113 if (dri2_dpy->flush != NULL)
Boyan Dinga25df542015-07-21 23:43:59 +08002114 dri2_dpy->flush->flush(dri_drawable);
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05002115
2116 return EGL_TRUE;
2117}
2118
2119static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002120dri2_wait_native(const _EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05002121{
Ian Romanickeade9462010-09-23 09:39:42 -07002122 (void) drv;
2123 (void) disp;
2124
Kristian Høgsberg42fa0092010-02-03 10:18:28 -05002125 if (engine != EGL_CORE_NATIVE_ENGINE)
2126 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2127 /* glXWaitX(); */
2128
2129 return EGL_TRUE;
2130}
2131
Kristian Høgsbergc6e830c2010-02-04 21:49:44 -05002132static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002133dri2_bind_tex_image(const _EGLDriver *drv,
Emil Velikovebc68e32016-08-25 00:12:13 +01002134 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002135{
2136 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002137 struct dri2_egl_context *dri2_ctx;
2138 _EGLContext *ctx;
2139 GLint format, target;
Boyan Dinga25df542015-07-21 23:43:59 +08002140 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002141
2142 ctx = _eglGetCurrentContext();
2143 dri2_ctx = dri2_egl_context(ctx);
2144
Kristian Høgsberg5dc0f492010-05-06 22:40:25 -04002145 if (!_eglBindTexImage(drv, disp, surf, buffer))
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002146 return EGL_FALSE;
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002147
Boyan Dinga25df542015-07-21 23:43:59 +08002148 switch (surf->TextureFormat) {
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002149 case EGL_TEXTURE_RGB:
2150 format = __DRI_TEXTURE_FORMAT_RGB;
2151 break;
2152 case EGL_TEXTURE_RGBA:
2153 format = __DRI_TEXTURE_FORMAT_RGBA;
2154 break;
2155 default:
Brian Paul54070a92015-05-29 16:01:37 -06002156 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2157 format = __DRI_TEXTURE_FORMAT_RGBA;
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002158 }
2159
Boyan Dinga25df542015-07-21 23:43:59 +08002160 switch (surf->TextureTarget) {
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002161 case EGL_TEXTURE_2D:
2162 target = GL_TEXTURE_2D;
2163 break;
2164 default:
Brian Paul54070a92015-05-29 16:01:37 -06002165 target = GL_TEXTURE_2D;
2166 assert(!"Unexpected texture target in dri2_bind_tex_image()");
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002167 }
2168
Boyan Ding868ae3e2015-11-25 13:27:02 +08002169 dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2170 target, format,
2171 dri_drawable);
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002172
Kristian Høgsberg5dc0f492010-05-06 22:40:25 -04002173 return EGL_TRUE;
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002174}
2175
2176static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002177dri2_release_tex_image(const _EGLDriver *drv,
Emil Velikovebc68e32016-08-25 00:12:13 +01002178 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002179{
Juan Zhaoe59fa4c2011-01-09 12:03:02 -05002180 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Juan Zhaoe59fa4c2011-01-09 12:03:02 -05002181 struct dri2_egl_context *dri2_ctx;
2182 _EGLContext *ctx;
2183 GLint target;
Boyan Dinga25df542015-07-21 23:43:59 +08002184 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
Juan Zhaoe59fa4c2011-01-09 12:03:02 -05002185
2186 ctx = _eglGetCurrentContext();
2187 dri2_ctx = dri2_egl_context(ctx);
2188
2189 if (!_eglReleaseTexImage(drv, disp, surf, buffer))
2190 return EGL_FALSE;
2191
Boyan Dinga25df542015-07-21 23:43:59 +08002192 switch (surf->TextureTarget) {
Juan Zhaoe59fa4c2011-01-09 12:03:02 -05002193 case EGL_TEXTURE_2D:
2194 target = GL_TEXTURE_2D;
2195 break;
2196 default:
Eric Engestromea738a92018-10-28 18:20:20 +00002197 assert(!"missing texture target");
Juan Zhaoe59fa4c2011-01-09 12:03:02 -05002198 }
Emil Velikov9b42fd12014-02-18 03:04:03 +00002199
2200 if (dri2_dpy->tex_buffer->base.version >= 3 &&
2201 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
Boyan Ding868ae3e2015-11-25 13:27:02 +08002202 dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2203 target, dri_drawable);
Emil Velikov9b42fd12014-02-18 03:04:03 +00002204 }
Ian Romanickeade9462010-09-23 09:39:42 -07002205
Kristian Høgsbergbba5a982010-02-09 15:49:28 -05002206 return EGL_TRUE;
2207}
2208
Chad Versaceeef68a92014-01-28 17:03:03 -08002209static _EGLImage*
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002210dri2_create_image(const _EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx,
Chad Versaceeef68a92014-01-28 17:03:03 -08002211 EGLenum target, EGLClientBuffer buffer,
2212 const EGLint *attr_list)
2213{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002214 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2215 return dri2_dpy->vtbl->create_image(drv, disp, ctx, target, buffer,
Chad Versaceeef68a92014-01-28 17:03:03 -08002216 attr_list);
2217}
2218
Kristian Høgsbergf4e7d1b2010-02-11 19:28:26 -05002219static _EGLImage *
Chad Versaceeef68a92014-01-28 17:03:03 -08002220dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
Kristian Høgsberg5f5746a2012-07-05 00:39:25 -04002221{
2222 struct dri2_egl_image *dri2_img;
2223
2224 if (dri_image == NULL) {
2225 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2226 return NULL;
2227 }
2228
2229 dri2_img = malloc(sizeof *dri2_img);
2230 if (!dri2_img) {
2231 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2232 return NULL;
2233 }
2234
Emil Velikovd42b0952017-06-20 15:22:39 +01002235 _eglInitImage(&dri2_img->base, disp);
Kristian Høgsberg5f5746a2012-07-05 00:39:25 -04002236
2237 dri2_img->dri_image = dri_image;
2238
2239 return &dri2_img->base;
2240}
2241
Nicolai Hähnled0d6efc2017-10-10 13:58:47 +02002242/**
2243 * Translate a DRI Image extension error code into an EGL error code.
2244 */
2245static EGLint
2246egl_error_from_dri_image_error(int dri_error)
2247{
2248 switch (dri_error) {
2249 case __DRI_IMAGE_ERROR_SUCCESS:
2250 return EGL_SUCCESS;
2251 case __DRI_IMAGE_ERROR_BAD_ALLOC:
2252 return EGL_BAD_ALLOC;
2253 case __DRI_IMAGE_ERROR_BAD_MATCH:
2254 return EGL_BAD_MATCH;
2255 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2256 return EGL_BAD_PARAMETER;
2257 case __DRI_IMAGE_ERROR_BAD_ACCESS:
2258 return EGL_BAD_ACCESS;
2259 default:
Eric Engestromea738a92018-10-28 18:20:20 +00002260 assert(!"unknown dri_error code");
Nicolai Hähnled0d6efc2017-10-10 13:58:47 +02002261 return EGL_BAD_ALLOC;
2262 }
2263}
2264
Kristian Høgsberg5f5746a2012-07-05 00:39:25 -04002265static _EGLImage *
Kristian Høgsberg557c20b2010-02-26 14:51:11 -05002266dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikovebc68e32016-08-25 00:12:13 +01002267 EGLClientBuffer buffer,
2268 const EGLint *attr_list)
Kristian Høgsberg557c20b2010-02-26 14:51:11 -05002269{
2270 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2271 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
José Fonseca6a661892011-05-12 15:12:16 +01002272 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
Kristian Høgsberg5f5746a2012-07-05 00:39:25 -04002273 __DRIimage *dri_image;
Kristian Høgsberg557c20b2010-02-26 14:51:11 -05002274
2275 if (renderbuffer == 0) {
2276 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2277 return EGL_NO_IMAGE_KHR;
2278 }
Kristian Høgsbergf4e7d1b2010-02-11 19:28:26 -05002279
Emil Velikov962110f2017-06-29 00:35:56 +01002280 if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2281 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2282 return EGL_NO_IMAGE_KHR;
2283 }
2284
Emil Velikov872a3732017-10-10 18:50:13 +01002285 if (dri2_dpy->image->base.version >= 17 &&
2286 dri2_dpy->image->createImageFromRenderbuffer2) {
Nicolai Hähnlebad24392017-10-10 13:58:47 +02002287 unsigned error = ~0;
2288
2289 dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2290 dri2_ctx->dri_context, renderbuffer, NULL, &error);
2291
2292 assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2293
2294 if (!dri_image) {
2295 _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2296 return EGL_NO_IMAGE_KHR;
2297 }
2298 } else {
2299 dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2300 dri2_ctx->dri_context, renderbuffer, NULL);
2301 if (!dri_image) {
2302 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2303 return EGL_NO_IMAGE_KHR;
2304 }
2305 }
Kristian Høgsbergf4e7d1b2010-02-11 19:28:26 -05002306
Chad Versaceeef68a92014-01-28 17:03:03 -08002307 return dri2_create_image_from_dri(disp, dri_image);
Robert Bragg670f1822012-01-10 22:16:26 +00002308}
2309
José Fonseca5de2c672011-05-12 15:24:26 +01002310#ifdef HAVE_WAYLAND_PLATFORM
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002311
2312/* This structure describes how a wl_buffer maps to one or more
2313 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2314 * offsets and strides of the planes in the buffer. This table maps a
2315 * wl_drm format code to a description of the planes in the buffer
2316 * that lets us create a __DRIimage for each of the planes. */
2317
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02002318static const struct wl_drm_components_descriptor {
2319 uint32_t dri_components;
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002320 EGLint components;
2321 int nplanes;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02002322} wl_drm_components[] = {
2323 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2324 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2325 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2326 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2327 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002328};
2329
Benjamin Franzke6b369c42011-02-21 16:22:34 +01002330static _EGLImage *
Benjamin Franzke6b369c42011-02-21 16:22:34 +01002331dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikovebc68e32016-08-25 00:12:13 +01002332 EGLClientBuffer _buffer,
2333 const EGLint *attr_list)
Benjamin Franzke6b369c42011-02-21 16:22:34 +01002334{
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03002335 struct wl_drm_buffer *buffer;
Robert Bragg670f1822012-01-10 22:16:26 +00002336 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02002337 const struct wl_drm_components_descriptor *f;
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002338 __DRIimage *dri_image;
2339 _EGLImageAttribs attrs;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02002340 int32_t plane;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01002341
Kristian Høgsberg360a1412013-09-26 12:25:11 -07002342 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2343 (struct wl_resource *) _buffer);
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03002344 if (!buffer)
Benjamin Franzke834b8412011-04-30 11:17:01 +02002345 return NULL;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01002346
Emil Velikov81e95922017-06-28 20:31:18 +01002347 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002348 return NULL;
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002349
Emil Velikov81e95922017-06-28 20:31:18 +01002350 plane = attrs.PlaneWL;
Kristian Høgsberge6a33572012-07-05 16:43:04 -04002351 f = buffer->driver_format;
2352 if (plane < 0 || plane >= f->nplanes) {
2353 _eglError(EGL_BAD_PARAMETER,
2354 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2355 return NULL;
2356 }
2357
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02002358 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
Louis-Francis Ratté-Bouliannea34715a2017-09-28 03:18:33 -04002359 if (dri_image == NULL && plane == 0)
2360 dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02002361 if (dri_image == NULL) {
2362 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2363 return NULL;
2364 }
Robert Bragg670f1822012-01-10 22:16:26 +00002365
Chad Versaceeef68a92014-01-28 17:03:03 -08002366 return dri2_create_image_from_dri(disp, dri_image);
Benjamin Franzke6b369c42011-02-21 16:22:34 +01002367}
2368#endif
2369
Sarah Sharpc524f3e2014-05-06 12:10:57 -07002370static EGLBoolean
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002371dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
Sarah Sharpc524f3e2014-05-06 12:10:57 -07002372 EGLuint64KHR *ust, EGLuint64KHR *msc,
2373 EGLuint64KHR *sbc)
2374{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002375 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestrombc38fe82020-07-11 00:42:08 +02002376 if (!dri2_dpy->vtbl->get_sync_values)
2377 return EGL_FALSE;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002378 return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
Sarah Sharpc524f3e2014-05-06 12:10:57 -07002379}
2380
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002381/**
2382 * Set the error code after a call to
2383 * dri2_egl_image::dri_image::createImageFromTexture.
2384 */
2385static void
2386dri2_create_image_khr_texture_error(int dri_error)
2387{
Nicolai Hähnled0d6efc2017-10-10 13:58:47 +02002388 EGLint egl_error = egl_error_from_dri_image_error(dri_error);
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002389
Nicolai Hähnled0d6efc2017-10-10 13:58:47 +02002390 if (egl_error != EGL_SUCCESS)
2391 _eglError(egl_error, "dri2_create_image_khr_texture");
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002392}
2393
2394static _EGLImage *
2395dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikovebc68e32016-08-25 00:12:13 +01002396 EGLenum target,
2397 EGLClientBuffer buffer,
2398 const EGLint *attr_list)
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002399{
2400 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2401 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2402 struct dri2_egl_image *dri2_img;
2403 GLuint texture = (GLuint) (uintptr_t) buffer;
2404 _EGLImageAttribs attrs;
2405 GLuint depth;
2406 GLenum gl_target;
2407 unsigned error;
2408
2409 if (texture == 0) {
2410 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2411 return EGL_NO_IMAGE_KHR;
2412 }
2413
Emil Velikov81e95922017-06-28 20:31:18 +01002414 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002415 return EGL_NO_IMAGE_KHR;
2416
2417 switch (target) {
2418 case EGL_GL_TEXTURE_2D_KHR:
Emil Velikov962110f2017-06-29 00:35:56 +01002419 if (!disp->Extensions.KHR_gl_texture_2D_image) {
2420 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2421 return EGL_NO_IMAGE_KHR;
2422 }
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002423 depth = 0;
2424 gl_target = GL_TEXTURE_2D;
2425 break;
2426 case EGL_GL_TEXTURE_3D_KHR:
Emil Velikov962110f2017-06-29 00:35:56 +01002427 if (!disp->Extensions.KHR_gl_texture_3D_image) {
Anatoli Antonovitchb193f2b2015-06-10 14:42:31 +02002428 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2429 return EGL_NO_IMAGE_KHR;
2430 }
Emil Velikov962110f2017-06-29 00:35:56 +01002431
2432 depth = attrs.GLTextureZOffset;
2433 gl_target = GL_TEXTURE_3D;
2434 break;
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002435 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2436 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2437 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2438 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2439 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2440 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
Emil Velikov962110f2017-06-29 00:35:56 +01002441 if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2442 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2443 return EGL_NO_IMAGE_KHR;
2444 }
2445
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002446 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2447 gl_target = GL_TEXTURE_CUBE_MAP;
2448 break;
2449 default:
Emil Velikov962110f2017-06-29 00:35:56 +01002450 unreachable("Unexpected target in dri2_create_image_khr_texture()");
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002451 return EGL_NO_IMAGE_KHR;
2452 }
2453
2454 dri2_img = malloc(sizeof *dri2_img);
2455 if (!dri2_img) {
2456 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2457 return EGL_NO_IMAGE_KHR;
2458 }
2459
Emil Velikovd42b0952017-06-20 15:22:39 +01002460 _eglInitImage(&dri2_img->base, disp);
Abdiel Janulgue7b7af482012-11-27 15:19:54 +02002461
2462 dri2_img->dri_image =
2463 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2464 gl_target,
2465 texture,
2466 depth,
2467 attrs.GLTextureLevel,
2468 &error,
2469 dri2_img);
2470 dri2_create_image_khr_texture_error(error);
2471
2472 if (!dri2_img->dri_image) {
2473 free(dri2_img);
2474 return EGL_NO_IMAGE_KHR;
2475 }
2476 return &dri2_img->base;
2477}
2478
Haixia Shia7c69932016-07-28 10:51:12 -07002479static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002480dri2_query_surface(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
Haixia Shia7c69932016-07-28 10:51:12 -07002481 EGLint attribute, EGLint *value)
2482{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002483 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Haixia Shia7c69932016-07-28 10:51:12 -07002484 if (!dri2_dpy->vtbl->query_surface)
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002485 return _eglQuerySurface(drv, disp, surf, attribute, value);
2486 return dri2_dpy->vtbl->query_surface(drv, disp, surf, attribute, value);
Haixia Shia7c69932016-07-28 10:51:12 -07002487}
2488
Chad Versaceeadd5e02014-01-28 17:03:03 -08002489static struct wl_buffer*
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002490dri2_create_wayland_buffer_from_image(const _EGLDriver *drv, _EGLDisplay *disp,
Chad Versaceeadd5e02014-01-28 17:03:03 -08002491 _EGLImage *img)
2492{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002493 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Eric Engestrom2e3eb0c2020-07-11 00:41:35 +02002494 if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)
2495 return NULL;
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00002496 return dri2_dpy->vtbl->create_wayland_buffer_from_image(drv, disp, img);
Chad Versaceeadd5e02014-01-28 17:03:03 -08002497}
2498
Axel Davy63986f92015-01-14 20:36:04 +01002499#ifdef HAVE_LIBDRM
Emil Velikov57c670a2015-07-10 11:01:35 +01002500static _EGLImage *
2501dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikovebc68e32016-08-25 00:12:13 +01002502 EGLClientBuffer buffer, const EGLint *attr_list)
Emil Velikov57c670a2015-07-10 11:01:35 +01002503{
2504 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Alejandro Piñeiro57671022017-07-13 11:38:48 +02002505 EGLint format, name, pitch;
Emil Velikov57c670a2015-07-10 11:01:35 +01002506 _EGLImageAttribs attrs;
2507 __DRIimage *dri_image;
2508
2509 name = (EGLint) (uintptr_t) buffer;
2510
Emil Velikov81e95922017-06-28 20:31:18 +01002511 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
Emil Velikov57c670a2015-07-10 11:01:35 +01002512 return NULL;
2513
2514 if (attrs.Width <= 0 || attrs.Height <= 0 ||
2515 attrs.DRMBufferStrideMESA <= 0) {
2516 _eglError(EGL_BAD_PARAMETER,
Emil Velikovebc68e32016-08-25 00:12:13 +01002517 "bad width, height or stride");
Emil Velikov57c670a2015-07-10 11:01:35 +01002518 return NULL;
2519 }
2520
2521 switch (attrs.DRMBufferFormatMESA) {
2522 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2523 format = __DRI_IMAGE_FORMAT_ARGB8888;
2524 pitch = attrs.DRMBufferStrideMESA;
2525 break;
2526 default:
2527 _eglError(EGL_BAD_PARAMETER,
Emil Velikovebc68e32016-08-25 00:12:13 +01002528 "dri2_create_image_khr: unsupported pixmap depth");
Emil Velikov57c670a2015-07-10 11:01:35 +01002529 return NULL;
2530 }
2531
Tapani Pälliff05f162019-11-12 17:32:41 +02002532 dri_image =
2533 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2534 attrs.Width,
2535 attrs.Height,
2536 format,
2537 name,
2538 pitch,
2539 NULL);
Emil Velikov57c670a2015-07-10 11:01:35 +01002540
2541 return dri2_create_image_from_dri(disp, dri_image);
2542}
2543
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002544static EGLBoolean
2545dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2546{
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002547 /**
2548 * The spec says:
2549 *
2550 * "Required attributes and their values are as follows:
2551 *
2552 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2553 *
2554 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2555 * by drm_fourcc.h and used as the pixel_format parameter of the
2556 * drm_mode_fb_cmd2 ioctl."
2557 *
2558 * and
2559 *
2560 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2561 * incomplete, EGL_BAD_PARAMETER is generated."
2562 */
2563 if (attrs->Width <= 0 || attrs->Height <= 0 ||
Emil Velikovc58af5c2017-06-20 15:40:28 +01002564 !attrs->DMABufFourCC.IsPresent)
2565 return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002566
2567 /**
2568 * Also:
2569 *
2570 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2571 * specified for a plane's pitch or offset isn't supported by EGL,
2572 * EGL_BAD_ACCESS is generated."
2573 */
Chad Versacecd717cb2017-06-22 11:00:40 -07002574 for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002575 if (attrs->DMABufPlanePitches[i].IsPresent &&
Emil Velikovc58af5c2017-06-20 15:40:28 +01002576 attrs->DMABufPlanePitches[i].Value <= 0)
2577 return _eglError(EGL_BAD_ACCESS, "invalid pitch");
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002578 }
2579
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302580 /**
2581 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2582 * attribute values may be given.
2583 *
2584 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2585 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2586 */
Chad Versacecd717cb2017-06-22 11:00:40 -07002587 for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302588 if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
Emil Velikovc58af5c2017-06-20 15:40:28 +01002589 attrs->DMABufPlaneModifiersHi[i].IsPresent)
2590 return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302591 }
2592
2593 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2594 * mandate it, we only accept the same modifier across all planes. */
Chad Versacecd717cb2017-06-22 11:00:40 -07002595 for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302596 if (attrs->DMABufPlaneFds[i].IsPresent) {
2597 if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2598 attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2599 (attrs->DMABufPlaneModifiersLo[0].Value !=
2600 attrs->DMABufPlaneModifiersLo[i].Value) ||
2601 (attrs->DMABufPlaneModifiersHi[0].Value !=
Emil Velikovc58af5c2017-06-20 15:40:28 +01002602 attrs->DMABufPlaneModifiersHi[i].Value))
2603 return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302604 }
2605 }
2606
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002607 return EGL_TRUE;
2608}
2609
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002610/* Returns the total number of planes for the format or zero if it isn't a
2611 * valid fourcc format.
2612 */
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002613static unsigned
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002614dri2_num_fourcc_format_planes(EGLint format)
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002615{
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002616 switch (format) {
Chad Versacefd865d52015-06-23 15:48:17 -07002617 case DRM_FORMAT_R8:
2618 case DRM_FORMAT_RG88:
2619 case DRM_FORMAT_GR88:
Rainer Hochecker09b140a2017-01-05 16:58:56 +01002620 case DRM_FORMAT_R16:
2621 case DRM_FORMAT_GR1616:
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002622 case DRM_FORMAT_RGB332:
2623 case DRM_FORMAT_BGR233:
2624 case DRM_FORMAT_XRGB4444:
2625 case DRM_FORMAT_XBGR4444:
2626 case DRM_FORMAT_RGBX4444:
2627 case DRM_FORMAT_BGRX4444:
2628 case DRM_FORMAT_ARGB4444:
2629 case DRM_FORMAT_ABGR4444:
2630 case DRM_FORMAT_RGBA4444:
2631 case DRM_FORMAT_BGRA4444:
2632 case DRM_FORMAT_XRGB1555:
2633 case DRM_FORMAT_XBGR1555:
2634 case DRM_FORMAT_RGBX5551:
2635 case DRM_FORMAT_BGRX5551:
2636 case DRM_FORMAT_ARGB1555:
2637 case DRM_FORMAT_ABGR1555:
2638 case DRM_FORMAT_RGBA5551:
2639 case DRM_FORMAT_BGRA5551:
2640 case DRM_FORMAT_RGB565:
2641 case DRM_FORMAT_BGR565:
2642 case DRM_FORMAT_RGB888:
2643 case DRM_FORMAT_BGR888:
2644 case DRM_FORMAT_XRGB8888:
2645 case DRM_FORMAT_XBGR8888:
2646 case DRM_FORMAT_RGBX8888:
2647 case DRM_FORMAT_BGRX8888:
2648 case DRM_FORMAT_ARGB8888:
2649 case DRM_FORMAT_ABGR8888:
2650 case DRM_FORMAT_RGBA8888:
2651 case DRM_FORMAT_BGRA8888:
2652 case DRM_FORMAT_XRGB2101010:
2653 case DRM_FORMAT_XBGR2101010:
2654 case DRM_FORMAT_RGBX1010102:
2655 case DRM_FORMAT_BGRX1010102:
2656 case DRM_FORMAT_ARGB2101010:
2657 case DRM_FORMAT_ABGR2101010:
2658 case DRM_FORMAT_RGBA1010102:
2659 case DRM_FORMAT_BGRA1010102:
Kevin Strasser4861d2a2019-01-24 17:32:36 -08002660 case DRM_FORMAT_XBGR16161616F:
2661 case DRM_FORMAT_ABGR16161616F:
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002662 case DRM_FORMAT_YUYV:
2663 case DRM_FORMAT_YVYU:
2664 case DRM_FORMAT_UYVY:
2665 case DRM_FORMAT_VYUY:
Lionel Landwerlin252ca7b2018-11-08 17:28:20 +00002666 case DRM_FORMAT_AYUV:
Kasireddy, Vivek65600d02019-02-12 16:44:04 -08002667 case DRM_FORMAT_XYUV8888:
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002668 return 1;
2669
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002670 case DRM_FORMAT_NV12:
2671 case DRM_FORMAT_NV21:
2672 case DRM_FORMAT_NV16:
2673 case DRM_FORMAT_NV61:
Tapani Pälli722f96b2019-02-11 10:01:35 +02002674 case DRM_FORMAT_P010:
2675 case DRM_FORMAT_P012:
2676 case DRM_FORMAT_P016:
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002677 return 2;
2678
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002679 case DRM_FORMAT_YUV410:
2680 case DRM_FORMAT_YVU410:
2681 case DRM_FORMAT_YUV411:
2682 case DRM_FORMAT_YVU411:
2683 case DRM_FORMAT_YUV420:
2684 case DRM_FORMAT_YVU420:
2685 case DRM_FORMAT_YUV422:
2686 case DRM_FORMAT_YVU422:
2687 case DRM_FORMAT_YUV444:
2688 case DRM_FORMAT_YVU444:
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002689 return 3;
2690
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002691 default:
Jason Ekstrandb95896f2018-08-28 16:31:22 -05002692 return 0;
2693 }
2694}
2695
2696/* Returns the total number of file descriptors. Zero indicates an error. */
2697static unsigned
2698dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2699{
2700 unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2701 if (plane_n == 0) {
Lionel Landwerlin1c56d212018-11-13 14:10:45 +00002702 _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002703 return 0;
2704 }
2705
Daniel Stone7d26a522017-07-31 18:34:57 +01002706 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2707 /**
2708 * The modifiers extension spec says:
2709 *
2710 * "Modifiers may modify any attribute of a buffer import, including
2711 * but not limited to adding extra planes to a format which
2712 * otherwise does not have those planes. As an example, a modifier
2713 * may add a plane for an external compression buffer to a
2714 * single-plane format. The exact meaning and effect of any
2715 * modifier is canonically defined by drm_fourcc.h, not as part of
2716 * this extension."
2717 */
2718 if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2719 attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2720 plane_n = i + 1;
2721 }
2722 }
2723
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002724 /**
2725 * The spec says:
2726 *
2727 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2728 * incomplete, EGL_BAD_PARAMETER is generated."
2729 */
Chad Versacecd717cb2017-06-22 11:00:40 -07002730 for (unsigned i = 0; i < plane_n; ++i) {
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002731 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2732 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2733 !attrs->DMABufPlanePitches[i].IsPresent) {
2734 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2735 return 0;
2736 }
2737 }
2738
2739 /**
2740 * The spec also says:
2741 *
2742 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2743 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2744 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
Pekka Paalanenfb2a1c22017-05-30 17:23:35 +05302745 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002746 */
Chad Versacecd717cb2017-06-22 11:00:40 -07002747 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002748 if (attrs->DMABufPlaneFds[i].IsPresent ||
2749 attrs->DMABufPlaneOffsets[i].IsPresent ||
Daniel Stone7d26a522017-07-31 18:34:57 +01002750 attrs->DMABufPlanePitches[i].IsPresent) {
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002751 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2752 return 0;
2753 }
2754 }
2755
2756 return plane_n;
2757}
2758
Varad Gautam6719e052017-05-30 17:23:38 +05302759static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002760dri2_query_dma_buf_formats(const _EGLDriver *drv, _EGLDisplay *disp,
Varad Gautam6719e052017-05-30 17:23:38 +05302761 EGLint max, EGLint *formats, EGLint *count)
2762{
2763 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Emil Velikovc58af5c2017-06-20 15:40:28 +01002764 if (max < 0 || (max > 0 && formats == NULL))
2765 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
Varad Gautam6719e052017-05-30 17:23:38 +05302766
2767 if (dri2_dpy->image->base.version < 15 ||
2768 dri2_dpy->image->queryDmaBufFormats == NULL)
2769 return EGL_FALSE;
2770
2771 if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2772 formats, count))
2773 return EGL_FALSE;
2774
Jason Ekstrand8c1b9882018-08-28 16:43:57 -05002775 if (max > 0) {
2776 /* Assert that all of the formats returned are actually fourcc formats.
2777 * Some day, if we want the internal interface function to be able to
2778 * return the fake fourcc formats defined in dri_interface.h, we'll have
2779 * to do something more clever here to pair the list down to just real
2780 * fourcc formats so that we don't leak the fake internal ones.
2781 */
2782 for (int i = 0; i < *count; i++) {
2783 assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2784 }
2785 }
2786
Varad Gautam6719e052017-05-30 17:23:38 +05302787 return EGL_TRUE;
2788}
2789
Varad Gautamde3c4592017-05-30 17:23:39 +05302790static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002791dri2_query_dma_buf_modifiers(const _EGLDriver *drv, _EGLDisplay *disp, EGLint format,
Varad Gautamde3c4592017-05-30 17:23:39 +05302792 EGLint max, EGLuint64KHR *modifiers,
2793 EGLBoolean *external_only, EGLint *count)
2794{
2795 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2796
Jason Ekstrand8c1b9882018-08-28 16:43:57 -05002797 if (dri2_num_fourcc_format_planes(format) == 0)
2798 return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2799
Emil Velikovc58af5c2017-06-20 15:40:28 +01002800 if (max < 0)
2801 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
Varad Gautamde3c4592017-05-30 17:23:39 +05302802
Emil Velikovc58af5c2017-06-20 15:40:28 +01002803 if (max > 0 && modifiers == NULL)
2804 return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
Varad Gautamde3c4592017-05-30 17:23:39 +05302805
2806 if (dri2_dpy->image->base.version < 15 ||
2807 dri2_dpy->image->queryDmaBufModifiers == NULL)
2808 return EGL_FALSE;
2809
2810 if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2811 max, modifiers,
2812 (unsigned int *) external_only,
Emil Velikovc58af5c2017-06-20 15:40:28 +01002813 count) == false)
2814 return _eglError(EGL_BAD_PARAMETER, "invalid format");
Varad Gautamde3c4592017-05-30 17:23:39 +05302815
2816 return EGL_TRUE;
2817}
2818
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002819/**
2820 * The spec says:
2821 *
Pekka Paalanen08264e52014-08-08 16:26:28 +03002822 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2823 * EGL will take a reference to the dma_buf(s) which it will release at any
2824 * time while the EGLDisplay is initialized. It is the responsibility of the
2825 * application to close the dma_buf file descriptors."
2826 *
2827 * Therefore we must never close or otherwise modify the file descriptors.
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002828 */
Emil Velikov38ef6f52016-05-01 12:42:53 +01002829_EGLImage *
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002830dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikovebc68e32016-08-25 00:12:13 +01002831 EGLClientBuffer buffer, const EGLint *attr_list)
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002832{
2833 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2834 _EGLImage *res;
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002835 _EGLImageAttribs attrs;
2836 __DRIimage *dri_image;
2837 unsigned num_fds;
Pekka Paalanen9434f052017-05-30 17:23:34 +05302838 int fds[DMA_BUF_MAX_PLANES];
2839 int pitches[DMA_BUF_MAX_PLANES];
2840 int offsets[DMA_BUF_MAX_PLANES];
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302841 uint64_t modifier;
2842 bool has_modifier = false;
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002843 unsigned error;
2844
2845 /**
2846 * The spec says:
2847 *
2848 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2849 * error EGL_BAD_PARAMETER is generated."
2850 */
2851 if (buffer != NULL) {
2852 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2853 return NULL;
2854 }
2855
Emil Velikov81e95922017-06-28 20:31:18 +01002856 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002857 return NULL;
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002858
2859 if (!dri2_check_dma_buf_attribs(&attrs))
2860 return NULL;
2861
2862 num_fds = dri2_check_dma_buf_format(&attrs);
2863 if (!num_fds)
2864 return NULL;
2865
Chad Versacecd717cb2017-06-22 11:00:40 -07002866 for (unsigned i = 0; i < num_fds; ++i) {
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002867 fds[i] = attrs.DMABufPlaneFds[i].Value;
2868 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2869 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2870 }
2871
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302872 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2873 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2874 * attrs.DMABufPlaneModifiersHi[0] */
2875 if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
Eric Engestrom2de9e842018-08-16 15:22:46 +01002876 modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2877 attrs.DMABufPlaneModifiersLo[0].Value);
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302878 has_modifier = true;
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302879 }
2880
Tapani Pälliff05f162019-11-12 17:32:41 +02002881 if (has_modifier) {
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302882 if (dri2_dpy->image->base.version < 15 ||
2883 dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2884 _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2885 return EGL_NO_IMAGE_KHR;
2886 }
2887 dri_image =
2888 dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2889 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2890 modifier, fds, num_fds, pitches, offsets,
2891 attrs.DMABufYuvColorSpaceHint.Value,
2892 attrs.DMABufSampleRangeHint.Value,
2893 attrs.DMABufChromaHorizontalSiting.Value,
2894 attrs.DMABufChromaVerticalSiting.Value,
2895 &error,
2896 NULL);
Tapani Pälliff05f162019-11-12 17:32:41 +02002897 }
2898 else {
Varad Gautam6f10e7c2017-05-30 17:23:37 +05302899 dri_image =
2900 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2901 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2902 fds, num_fds, pitches, offsets,
2903 attrs.DMABufYuvColorSpaceHint.Value,
2904 attrs.DMABufSampleRangeHint.Value,
2905 attrs.DMABufChromaHorizontalSiting.Value,
2906 attrs.DMABufChromaVerticalSiting.Value,
2907 &error,
2908 NULL);
2909 }
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002910 dri2_create_image_khr_texture_error(error);
2911
2912 if (!dri_image)
2913 return EGL_NO_IMAGE_KHR;
2914
Chad Versaceeef68a92014-01-28 17:03:03 -08002915 res = dri2_create_image_from_dri(disp, dri_image);
Topi Pohjolainen20de7f92013-03-22 15:58:05 +02002916
2917 return res;
2918}
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002919static _EGLImage *
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002920dri2_create_drm_image_mesa(const _EGLDriver *drv, _EGLDisplay *disp,
Emil Velikovebc68e32016-08-25 00:12:13 +01002921 const EGLint *attr_list)
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002922{
2923 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2924 struct dri2_egl_image *dri2_img;
Chia-I Wu5664a9832010-10-22 17:09:40 +08002925 _EGLImageAttribs attrs;
2926 unsigned int dri_use, valid_mask;
2927 int format;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002928
Ian Romanickeade9462010-09-23 09:39:42 -07002929 (void) drv;
2930
Emil Velikov9365ff42017-06-28 21:23:00 +01002931 if (!attr_list) {
2932 _eglError(EGL_BAD_PARAMETER, __func__);
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002933 return EGL_NO_IMAGE_KHR;
2934 }
2935
Emil Velikov81e95922017-06-28 20:31:18 +01002936 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
Emil Velikov9365ff42017-06-28 21:23:00 +01002937 return EGL_NO_IMAGE_KHR;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002938
Chia-I Wu5664a9832010-10-22 17:09:40 +08002939 if (attrs.Width <= 0 || attrs.Height <= 0) {
Emil Velikov9365ff42017-06-28 21:23:00 +01002940 _eglError(EGL_BAD_PARAMETER, __func__);
2941 return EGL_NO_IMAGE_KHR;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002942 }
2943
Chia-I Wu5664a9832010-10-22 17:09:40 +08002944 switch (attrs.DRMBufferFormatMESA) {
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002945 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2946 format = __DRI_IMAGE_FORMAT_ARGB8888;
2947 break;
2948 default:
Emil Velikov9365ff42017-06-28 21:23:00 +01002949 _eglError(EGL_BAD_PARAMETER, __func__);
2950 return EGL_NO_IMAGE_KHR;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002951 }
2952
2953 valid_mask =
2954 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
Kristian Høgsberge5169e92011-05-06 10:31:18 -04002955 EGL_DRM_BUFFER_USE_SHARE_MESA |
2956 EGL_DRM_BUFFER_USE_CURSOR_MESA;
Chia-I Wu5664a9832010-10-22 17:09:40 +08002957 if (attrs.DRMBufferUseMESA & ~valid_mask) {
Emil Velikov9365ff42017-06-28 21:23:00 +01002958 _eglError(EGL_BAD_PARAMETER, __func__);
2959 return EGL_NO_IMAGE_KHR;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002960 }
2961
2962 dri_use = 0;
Chia-I Wu5664a9832010-10-22 17:09:40 +08002963 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002964 dri_use |= __DRI_IMAGE_USE_SHARE;
Chia-I Wu5664a9832010-10-22 17:09:40 +08002965 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002966 dri_use |= __DRI_IMAGE_USE_SCANOUT;
Kristian Høgsberge5169e92011-05-06 10:31:18 -04002967 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2968 dri_use |= __DRI_IMAGE_USE_CURSOR;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002969
Emil Velikov9365ff42017-06-28 21:23:00 +01002970 dri2_img = malloc(sizeof *dri2_img);
2971 if (!dri2_img) {
2972 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2973 return EGL_NO_IMAGE_KHR;
2974 }
2975
2976 _eglInitImage(&dri2_img->base, disp);
2977
Boyan Ding052b3d42015-06-13 15:36:27 +08002978 dri2_img->dri_image =
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002979 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
Emil Velikovebc68e32016-08-25 00:12:13 +01002980 attrs.Width, attrs.Height,
Chia-I Wu5664a9832010-10-22 17:09:40 +08002981 format, dri_use, dri2_img);
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002982 if (dri2_img->dri_image == NULL) {
Emil Velikov9365ff42017-06-28 21:23:00 +01002983 free(dri2_img);
2984 _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
2985 return EGL_NO_IMAGE_KHR;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002986 }
2987
2988 return &dri2_img->base;
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002989}
2990
2991static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02002992dri2_export_drm_image_mesa(const _EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
Emil Velikovebc68e32016-08-25 00:12:13 +01002993 EGLint *name, EGLint *handle, EGLint *stride)
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04002994{
2995 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2996 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2997
Ian Romanickeade9462010-09-23 09:39:42 -07002998 (void) drv;
2999
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04003000 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
Emil Velikovc58af5c2017-06-20 15:40:28 +01003001 __DRI_IMAGE_ATTRIB_NAME, name))
3002 return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04003003
3004 if (handle)
3005 dri2_dpy->image->queryImage(dri2_img->dri_image,
Emil Velikovebc68e32016-08-25 00:12:13 +01003006 __DRI_IMAGE_ATTRIB_HANDLE, handle);
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04003007
3008 if (stride)
3009 dri2_dpy->image->queryImage(dri2_img->dri_image,
Emil Velikovebc68e32016-08-25 00:12:13 +01003010 __DRI_IMAGE_ATTRIB_STRIDE, stride);
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04003011
3012 return EGL_TRUE;
3013}
Dave Airlie8f7338f2014-03-03 13:57:16 +10003014
Eric Anholtf6750452018-07-16 16:18:03 -07003015/**
3016 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3017
3018 * The spec provides a boolean return for the driver to reject exporting for
3019 * basically any reason, but doesn't specify any particular error cases. For
3020 * now, we just fail if we don't have a DRM fourcc for the format.
3021 */
3022static bool
3023dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3024{
3025 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3026 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3027 EGLint fourcc;
3028
3029 if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3030 __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3031 return false;
3032 }
3033
3034 return true;
3035}
3036
Dave Airlie8f7338f2014-03-03 13:57:16 +10003037static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003038dri2_export_dma_buf_image_query_mesa(const _EGLDriver *drv, _EGLDisplay *disp,
Dave Airlie8f7338f2014-03-03 13:57:16 +10003039 _EGLImage *img,
3040 EGLint *fourcc, EGLint *nplanes,
Dave Airlieb5045e22015-05-05 09:10:34 +10003041 EGLuint64KHR *modifiers)
Dave Airlie8f7338f2014-03-03 13:57:16 +10003042{
3043 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3044 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
Nicolas Dufresne08f1cef2019-07-14 14:48:11 -04003045 int num_planes;
Dave Airlie8f7338f2014-03-03 13:57:16 +10003046
3047 (void) drv;
3048
Eric Anholtf6750452018-07-16 16:18:03 -07003049 if (!dri2_can_export_dma_buf_image(disp, img))
3050 return EGL_FALSE;
Dave Airlie8f7338f2014-03-03 13:57:16 +10003051
Nicolas Dufresne08f1cef2019-07-14 14:48:11 -04003052 dri2_dpy->image->queryImage(dri2_img->dri_image,
3053 __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
Dave Airlie8f7338f2014-03-03 13:57:16 +10003054 if (nplanes)
Nicolas Dufresne08f1cef2019-07-14 14:48:11 -04003055 *nplanes = num_planes;
3056
Dave Airlie8f7338f2014-03-03 13:57:16 +10003057 if (fourcc)
3058 dri2_dpy->image->queryImage(dri2_img->dri_image,
Emil Velikovebc68e32016-08-25 00:12:13 +01003059 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
Dave Airlie8f7338f2014-03-03 13:57:16 +10003060
Nicolas Dufresne08f1cef2019-07-14 14:48:11 -04003061 if (modifiers) {
3062 int mod_hi, mod_lo;
3063 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3064 bool query;
3065
3066 query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3067 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3068 &mod_hi);
3069 query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3070 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3071 &mod_lo);
3072 if (query)
3073 modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3074
3075 for (int i = 0; i < num_planes; i++)
3076 modifiers[i] = modifier;
3077 }
Dave Airlie8f7338f2014-03-03 13:57:16 +10003078
3079 return EGL_TRUE;
3080}
3081
3082static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003083dri2_export_dma_buf_image_mesa(const _EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
Dave Airlie8f7338f2014-03-03 13:57:16 +10003084 int *fds, EGLint *strides, EGLint *offsets)
3085{
3086 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3087 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
Tapani Pällief923082019-04-09 10:43:59 +03003088 EGLint nplanes;
Dave Airlie8f7338f2014-03-03 13:57:16 +10003089
3090 (void) drv;
3091
Eric Anholtf6750452018-07-16 16:18:03 -07003092 if (!dri2_can_export_dma_buf_image(disp, img))
3093 return EGL_FALSE;
3094
Tapani Pällief923082019-04-09 10:43:59 +03003095 /* EGL_MESA_image_dma_buf_export spec says:
3096 * "If the number of fds is less than the number of planes, then
3097 * subsequent fd slots should contain -1."
3098 */
3099 if (fds) {
3100 /* Query nplanes so that we know how big the given array is. */
3101 dri2_dpy->image->queryImage(dri2_img->dri_image,
3102 __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3103 memset(fds, -1, nplanes * sizeof(int));
3104 }
3105
Dave Airlie8f7338f2014-03-03 13:57:16 +10003106 /* rework later to provide multiple fds/strides/offsets */
3107 if (fds)
3108 dri2_dpy->image->queryImage(dri2_img->dri_image,
Emil Velikovebc68e32016-08-25 00:12:13 +01003109 __DRI_IMAGE_ATTRIB_FD, fds);
Dave Airlie8f7338f2014-03-03 13:57:16 +10003110
3111 if (strides)
3112 dri2_dpy->image->queryImage(dri2_img->dri_image,
Emil Velikovebc68e32016-08-25 00:12:13 +01003113 __DRI_IMAGE_ATTRIB_STRIDE, strides);
Dave Airlie8f7338f2014-03-03 13:57:16 +10003114
Chuanbo Weng9e8de862016-09-14 01:07:10 +08003115 if (offsets) {
3116 int img_offset;
3117 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3118 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3119 if (ret)
3120 offsets[0] = img_offset;
3121 else
3122 offsets[0] = 0;
3123 }
Dave Airlie8f7338f2014-03-03 13:57:16 +10003124
3125 return EGL_TRUE;
3126}
Emil Velikov57c670a2015-07-10 11:01:35 +01003127
Samuel Thibaulta594cec2013-11-10 19:32:01 +01003128#endif
Kristian Høgsberg5aaa53e2010-06-03 21:36:40 -04003129
Emil Velikov57c670a2015-07-10 11:01:35 +01003130_EGLImage *
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003131dri2_create_image_khr(const _EGLDriver *drv, _EGLDisplay *disp,
Emil Velikovebc68e32016-08-25 00:12:13 +01003132 _EGLContext *ctx, EGLenum target,
3133 EGLClientBuffer buffer, const EGLint *attr_list)
Emil Velikov57c670a2015-07-10 11:01:35 +01003134{
3135 (void) drv;
3136
3137 switch (target) {
3138 case EGL_GL_TEXTURE_2D_KHR:
3139 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3140 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3141 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3142 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3143 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3144 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
Emil Velikov57c670a2015-07-10 11:01:35 +01003145 case EGL_GL_TEXTURE_3D_KHR:
Emil Velikov962110f2017-06-29 00:35:56 +01003146 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
Emil Velikov57c670a2015-07-10 11:01:35 +01003147 case EGL_GL_RENDERBUFFER_KHR:
3148 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3149#ifdef HAVE_LIBDRM
3150 case EGL_DRM_BUFFER_MESA:
3151 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3152 case EGL_LINUX_DMA_BUF_EXT:
3153 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3154#endif
3155#ifdef HAVE_WAYLAND_PLATFORM
3156 case EGL_WAYLAND_BUFFER_WL:
3157 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3158#endif
3159 default:
3160 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3161 return EGL_NO_IMAGE_KHR;
3162 }
3163}
3164
3165static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003166dri2_destroy_image_khr(const _EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
Emil Velikov57c670a2015-07-10 11:01:35 +01003167{
3168 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3169 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3170
3171 (void) drv;
3172
3173 dri2_dpy->image->destroyImage(dri2_img->dri_image);
3174 free(dri2_img);
3175
3176 return EGL_TRUE;
3177}
3178
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003179#ifdef HAVE_WAYLAND_PLATFORM
Benjamin Franzke834b8412011-04-30 11:17:01 +02003180
Kristian Høgsberg379eb472012-07-05 14:19:48 -04003181static void
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003182dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
Kristian Høgsberg379eb472012-07-05 14:19:48 -04003183 struct wl_drm_buffer *buffer)
Benjamin Franzke834b8412011-04-30 11:17:01 +02003184{
3185 _EGLDisplay *disp = user_data;
3186 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02003187 __DRIimage *img;
Chad Versacecd717cb2017-06-22 11:00:40 -07003188 int dri_components = 0;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -04003189
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003190 if (fd == -1)
3191 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003192 buffer->width,
3193 buffer->height,
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003194 buffer->format,
3195 (int*)&name, 1,
3196 buffer->stride,
3197 buffer->offset,
3198 NULL);
3199 else
3200 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003201 buffer->width,
3202 buffer->height,
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003203 buffer->format,
3204 &fd, 1,
3205 buffer->stride,
3206 buffer->offset,
3207 NULL);
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003208
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02003209 if (img == NULL)
Kristian Høgsberg379eb472012-07-05 14:19:48 -04003210 return;
Benjamin Franzke834b8412011-04-30 11:17:01 +02003211
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02003212 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3213
3214 buffer->driver_format = NULL;
Chad Versacecd717cb2017-06-22 11:00:40 -07003215 for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02003216 if (wl_drm_components[i].dri_components == dri_components)
3217 buffer->driver_format = &wl_drm_components[i];
3218
3219 if (buffer->driver_format == NULL)
3220 dri2_dpy->image->destroyImage(img);
3221 else
3222 buffer->driver_buffer = img;
Benjamin Franzke834b8412011-04-30 11:17:01 +02003223}
3224
3225static void
Kristian Høgsberg379eb472012-07-05 14:19:48 -04003226dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
Benjamin Franzke834b8412011-04-30 11:17:01 +02003227{
3228 _EGLDisplay *disp = user_data;
Benjamin Franzke834b8412011-04-30 11:17:01 +02003229 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3230
Kristian Høgsberg379eb472012-07-05 14:19:48 -04003231 dri2_dpy->image->destroyImage(buffer->driver_buffer);
Benjamin Franzke834b8412011-04-30 11:17:01 +02003232}
3233
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003234static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003235dri2_bind_wayland_display_wl(const _EGLDriver *drv, _EGLDisplay *disp,
Emil Velikovebc68e32016-08-25 00:12:13 +01003236 struct wl_display *wl_dpy)
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003237{
3238 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Emil Velikovacb84ff2017-09-27 19:49:12 +01003239 const struct wayland_drm_callbacks wl_drm_callbacks = {
3240 .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3241 .reference_buffer = dri2_wl_reference_buffer,
Mario Kleiner820dfce2018-06-13 06:04:15 +02003242 .release_buffer = dri2_wl_release_buffer,
3243 .is_format_supported = dri2_wl_is_format_supported
Emil Velikovacb84ff2017-09-27 19:49:12 +01003244 };
Emil Velikov6638c552014-05-28 14:36:46 +01003245 int flags = 0;
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003246 uint64_t cap;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003247
3248 (void) drv;
3249
3250 if (dri2_dpy->wl_server_drm)
Emil Velikovebc68e32016-08-25 00:12:13 +01003251 return EGL_FALSE;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003252
Emil Velikov6638c552014-05-28 14:36:46 +01003253 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3254 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
Kristian Høgsberg16707372013-03-19 13:20:36 -04003255 dri2_dpy->image->base.version >= 7 &&
3256 dri2_dpy->image->createImageFromFds != NULL)
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003257 flags |= WAYLAND_DRM_PRIME;
3258
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003259 dri2_dpy->wl_server_drm =
Emil Velikovebc68e32016-08-25 00:12:13 +01003260 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
Kristian Høgsbergde315f72013-02-02 12:26:12 -05003261 &wl_drm_callbacks, disp, flags);
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003262
3263 if (!dri2_dpy->wl_server_drm)
Emil Velikovebc68e32016-08-25 00:12:13 +01003264 return EGL_FALSE;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003265
Ander Conselvan de Oliveira17860302014-04-08 23:28:42 +03003266#ifdef HAVE_DRM_PLATFORM
Kristian Høgsberg360a1412013-09-26 12:25:11 -07003267 /* We have to share the wl_drm instance with gbm, so gbm can convert
3268 * wl_buffers to gbm bos. */
Kristian Høgsberg1d349272013-10-11 15:30:09 -07003269 if (dri2_dpy->gbm_dri)
3270 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
Ander Conselvan de Oliveira17860302014-04-08 23:28:42 +03003271#endif
Kristian Høgsberg360a1412013-09-26 12:25:11 -07003272
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003273 return EGL_TRUE;
3274}
3275
3276static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003277dri2_unbind_wayland_display_wl(const _EGLDriver *drv, _EGLDisplay *disp,
Emil Velikovebc68e32016-08-25 00:12:13 +01003278 struct wl_display *wl_dpy)
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003279{
3280 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3281
3282 (void) drv;
3283
3284 if (!dri2_dpy->wl_server_drm)
Emil Velikovebc68e32016-08-25 00:12:13 +01003285 return EGL_FALSE;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003286
Benjamin Franzke834b8412011-04-30 11:17:01 +02003287 wayland_drm_uninit(dri2_dpy->wl_server_drm);
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003288 dri2_dpy->wl_server_drm = NULL;
3289
3290 return EGL_TRUE;
3291}
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003292
3293static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003294dri2_query_wayland_buffer_wl(const _EGLDriver *drv, _EGLDisplay *disp,
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003295 struct wl_resource *buffer_resource,
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003296 EGLint attribute, EGLint *value)
3297{
Kristian Høgsberg360a1412013-09-26 12:25:11 -07003298 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003299 struct wl_drm_buffer *buffer;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02003300 const struct wl_drm_components_descriptor *format;
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003301
Kristian Høgsberg360a1412013-09-26 12:25:11 -07003302 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003303 if (!buffer)
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003304 return EGL_FALSE;
3305
3306 format = buffer->driver_format;
Kristian Høgsbergd7522ed2012-07-19 09:02:25 -04003307 switch (attribute) {
3308 case EGL_TEXTURE_FORMAT:
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003309 *value = format->components;
3310 return EGL_TRUE;
Kristian Høgsbergd7522ed2012-07-19 09:02:25 -04003311 case EGL_WIDTH:
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003312 *value = buffer->width;
Rob Bradford15e64de2013-04-10 11:36:24 +01003313 return EGL_TRUE;
Kristian Høgsbergd7522ed2012-07-19 09:02:25 -04003314 case EGL_HEIGHT:
Ander Conselvan de Oliveira8d29b522013-07-18 15:11:25 +03003315 *value = buffer->height;
Rob Bradford15e64de2013-04-10 11:36:24 +01003316 return EGL_TRUE;
Kristian Høgsberge6a33572012-07-05 16:43:04 -04003317 }
3318
3319 return EGL_FALSE;
3320}
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003321#endif
3322
Chia-I Wufef5d142011-01-07 15:02:41 +08003323static void
Marek Olšák9a0bda22015-04-10 10:56:02 +02003324dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3325{
3326 p_atomic_inc(&sync->refcount);
3327}
3328
3329static void
3330dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3331 struct dri2_egl_sync *dri2_sync)
3332{
3333 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
Rob Clark0201f012016-11-18 08:39:33 -05003334 switch (dri2_sync->base.Type) {
3335 case EGL_SYNC_REUSABLE_KHR:
Dongwon Kim70299472016-04-04 17:14:10 -07003336 cnd_destroy(&dri2_sync->cond);
Rob Clark0201f012016-11-18 08:39:33 -05003337 break;
3338 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3339 if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3340 close(dri2_sync->base.SyncFd);
3341 break;
3342 default:
3343 break;
3344 }
Dongwon Kim70299472016-04-04 17:14:10 -07003345
3346 if (dri2_sync->fence)
3347 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3348
Marek Olšák9a0bda22015-04-10 10:56:02 +02003349 free(dri2_sync);
3350 }
3351}
3352
3353static _EGLSync *
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003354dri2_create_sync(const _EGLDriver *drv, _EGLDisplay *disp,
Chad Versace80448852016-09-27 13:27:21 -07003355 EGLenum type, const EGLAttrib *attrib_list)
Marek Olšák9a0bda22015-04-10 10:56:02 +02003356{
3357 _EGLContext *ctx = _eglGetCurrentContext();
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003358 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Marek Olšák9a0bda22015-04-10 10:56:02 +02003359 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3360 struct dri2_egl_sync *dri2_sync;
Dongwon Kim70299472016-04-04 17:14:10 -07003361 EGLint ret;
3362 pthread_condattr_t attr;
Marek Olšák9a0bda22015-04-10 10:56:02 +02003363
3364 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3365 if (!dri2_sync) {
3366 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3367 return NULL;
3368 }
3369
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003370 if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
Marek Olšák9a0bda22015-04-10 10:56:02 +02003371 free(dri2_sync);
3372 return NULL;
3373 }
3374
3375 switch (type) {
3376 case EGL_SYNC_FENCE_KHR:
3377 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
Chad Versaceb0f410a2015-05-05 19:05:20 -07003378 if (!dri2_sync->fence) {
3379 /* Why did it fail? DRI doesn't return an error code, so we emit
Chad Versacea6bfdd72015-05-07 08:09:07 -07003380 * a generic EGL error that doesn't communicate user error.
3381 */
Chad Versaceb0f410a2015-05-05 19:05:20 -07003382 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3383 free(dri2_sync);
3384 return NULL;
3385 }
Marek Olšák9a0bda22015-04-10 10:56:02 +02003386 break;
Marek Olšák290a3eb2015-04-10 13:16:30 +02003387
3388 case EGL_SYNC_CL_EVENT_KHR:
3389 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3390 dri2_dpy->dri_screen,
3391 dri2_sync->base.CLEvent);
3392 /* this can only happen if the cl_event passed in is invalid. */
3393 if (!dri2_sync->fence) {
3394 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3395 free(dri2_sync);
3396 return NULL;
3397 }
3398
3399 /* the initial status must be "signaled" if the cl_event is signaled */
3400 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3401 dri2_sync->fence, 0, 0))
3402 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3403 break;
Dongwon Kim70299472016-04-04 17:14:10 -07003404
3405 case EGL_SYNC_REUSABLE_KHR:
3406 /* intialize attr */
3407 ret = pthread_condattr_init(&attr);
3408
3409 if (ret) {
3410 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3411 free(dri2_sync);
3412 return NULL;
3413 }
3414
3415 /* change clock attribute to CLOCK_MONOTONIC */
3416 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3417
3418 if (ret) {
3419 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3420 free(dri2_sync);
3421 return NULL;
3422 }
3423
3424 ret = pthread_cond_init(&dri2_sync->cond, &attr);
3425
3426 if (ret) {
3427 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3428 free(dri2_sync);
3429 return NULL;
3430 }
3431
3432 /* initial status of reusable sync must be "unsignaled" */
3433 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3434 break;
Rob Clark0201f012016-11-18 08:39:33 -05003435
3436 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3437 if (dri2_dpy->fence->create_fence_fd) {
3438 dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3439 dri2_ctx->dri_context,
3440 dri2_sync->base.SyncFd);
3441 }
3442 if (!dri2_sync->fence) {
3443 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3444 free(dri2_sync);
3445 return NULL;
3446 }
3447 break;
Marek Olšák9a0bda22015-04-10 10:56:02 +02003448 }
3449
3450 p_atomic_set(&dri2_sync->refcount, 1);
3451 return &dri2_sync->base;
3452}
3453
3454static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003455dri2_destroy_sync(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync)
Marek Olšák9a0bda22015-04-10 10:56:02 +02003456{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003457 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Marek Olšák9a0bda22015-04-10 10:56:02 +02003458 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
Dongwon Kim70299472016-04-04 17:14:10 -07003459 EGLint ret = EGL_TRUE;
3460 EGLint err;
Marek Olšák9a0bda22015-04-10 10:56:02 +02003461
Dongwon Kim70299472016-04-04 17:14:10 -07003462 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3463 * then unlock all threads possibly blocked by the reusable sync before
3464 * destroying it.
3465 */
3466 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3467 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3468 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3469 /* unblock all threads currently blocked by sync */
3470 err = cnd_broadcast(&dri2_sync->cond);
3471
3472 if (err) {
3473 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3474 ret = EGL_FALSE;
3475 }
3476 }
Rob Clark0201f012016-11-18 08:39:33 -05003477
Marek Olšák9a0bda22015-04-10 10:56:02 +02003478 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
Dongwon Kim70299472016-04-04 17:14:10 -07003479
3480 return ret;
Marek Olšák9a0bda22015-04-10 10:56:02 +02003481}
3482
3483static EGLint
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003484dri2_dup_native_fence_fd(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync)
Rob Clark0201f012016-11-18 08:39:33 -05003485{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003486 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Rob Clark0201f012016-11-18 08:39:33 -05003487 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3488
3489 assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3490
3491 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3492 /* try to retrieve the actual native fence fd.. if rendering is
3493 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3494 */
3495 sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3496 dri2_sync->fence);
3497 }
3498
3499 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3500 /* if native fence fd still not created, return an error: */
3501 _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3502 return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3503 }
3504
Eric Engestrom419b4462020-06-05 10:22:22 +02003505 return os_dupfd_cloexec(sync->SyncFd);
Rob Clark0201f012016-11-18 08:39:33 -05003506}
3507
Tapani Pälli6f5b5702017-12-28 10:51:11 +02003508static void
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003509dri2_set_blob_cache_funcs(const _EGLDriver *drv, _EGLDisplay *disp,
Tapani Pälli6f5b5702017-12-28 10:51:11 +02003510 EGLSetBlobFuncANDROID set,
3511 EGLGetBlobFuncANDROID get)
3512{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003513 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Tapani Pälli6f5b5702017-12-28 10:51:11 +02003514 dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003515 disp->BlobCacheSet,
3516 disp->BlobCacheGet);
Tapani Pälli6f5b5702017-12-28 10:51:11 +02003517}
3518
Rob Clark0201f012016-11-18 08:39:33 -05003519static EGLint
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003520dri2_client_wait_sync(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync,
Marek Olšákd333d302015-05-12 17:34:57 +02003521 EGLint flags, EGLTime timeout)
Marek Olšák9a0bda22015-04-10 10:56:02 +02003522{
3523 _EGLContext *ctx = _eglGetCurrentContext();
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003524 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Marek Olšák9a0bda22015-04-10 10:56:02 +02003525 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3526 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3527 unsigned wait_flags = 0;
Dongwon Kim70299472016-04-04 17:14:10 -07003528
Marek Olšák9a0bda22015-04-10 10:56:02 +02003529 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3530
Marek Olšák18123a72015-09-25 22:48:00 +02003531 /* The EGL_KHR_fence_sync spec states:
3532 *
3533 * "If no context is current for the bound API,
3534 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3535 */
3536 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
Marek Olšák9a0bda22015-04-10 10:56:02 +02003537 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3538
3539 /* the sync object should take a reference while waiting */
3540 dri2_egl_ref_sync(dri2_sync);
3541
Dongwon Kim70299472016-04-04 17:14:10 -07003542 switch (sync->Type) {
3543 case EGL_SYNC_FENCE_KHR:
Rob Clark0201f012016-11-18 08:39:33 -05003544 case EGL_SYNC_NATIVE_FENCE_ANDROID:
Dongwon Kim70299472016-04-04 17:14:10 -07003545 case EGL_SYNC_CL_EVENT_KHR:
3546 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
Marek Olšák9a0bda22015-04-10 10:56:02 +02003547 dri2_sync->fence, wait_flags,
3548 timeout))
Dongwon Kim70299472016-04-04 17:14:10 -07003549 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3550 else
3551 ret = EGL_TIMEOUT_EXPIRED_KHR;
3552 break;
Marek Olšák9a0bda22015-04-10 10:56:02 +02003553
Dongwon Kim70299472016-04-04 17:14:10 -07003554 case EGL_SYNC_REUSABLE_KHR:
3555 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3556 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3557 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
Eric Engestrom7823cfe2017-09-26 12:16:33 +01003558 dri2_gl_flush();
Dongwon Kim70299472016-04-04 17:14:10 -07003559 }
3560
3561 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3562 if (timeout == EGL_FOREVER_KHR) {
Dongwon Kimc6e97aa2016-08-15 15:12:03 -07003563 mtx_lock(&dri2_sync->mutex);
3564 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
Matt Turner53f64a82016-05-16 14:43:26 -07003565 mtx_unlock(&dri2_sync->mutex);
Dongwon Kim70299472016-04-04 17:14:10 -07003566 } else {
3567 /* if reusable sync has not been yet signaled */
3568 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
Nicolai Hähnlef1a36482017-10-22 17:38:45 +02003569 /* timespecs for cnd_timedwait */
3570 struct timespec current;
3571 struct timespec expire;
3572
3573 /* We override the clock to monotonic when creating the condition
3574 * variable. */
Dongwon Kim70299472016-04-04 17:14:10 -07003575 clock_gettime(CLOCK_MONOTONIC, &current);
3576
3577 /* calculating when to expire */
Nicolai Hähnlef1a36482017-10-22 17:38:45 +02003578 expire.tv_nsec = timeout % 1000000000L;
3579 expire.tv_sec = timeout / 1000000000L;
Dongwon Kim70299472016-04-04 17:14:10 -07003580
Nicolai Hähnlef1a36482017-10-22 17:38:45 +02003581 expire.tv_nsec += current.tv_nsec;
3582 expire.tv_sec += current.tv_sec;
Dongwon Kim70299472016-04-04 17:14:10 -07003583
3584 /* expire.nsec now is a number between 0 and 1999999998 */
Nicolai Hähnlef1a36482017-10-22 17:38:45 +02003585 if (expire.tv_nsec > 999999999L) {
3586 expire.tv_sec++;
3587 expire.tv_nsec -= 1000000000L;
Dongwon Kim70299472016-04-04 17:14:10 -07003588 }
3589
Dongwon Kimc6e97aa2016-08-15 15:12:03 -07003590 mtx_lock(&dri2_sync->mutex);
Dongwon Kim70299472016-04-04 17:14:10 -07003591 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
Matt Turner53f64a82016-05-16 14:43:26 -07003592 mtx_unlock(&dri2_sync->mutex);
Dongwon Kim70299472016-04-04 17:14:10 -07003593
Dongwon Kimc6e97aa2016-08-15 15:12:03 -07003594 if (ret == thrd_busy) {
3595 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3596 ret = EGL_TIMEOUT_EXPIRED_KHR;
3597 } else {
3598 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3599 ret = EGL_FALSE;
Dongwon Kim70299472016-04-04 17:14:10 -07003600 }
Dongwon Kimc6e97aa2016-08-15 15:12:03 -07003601 }
Dongwon Kim70299472016-04-04 17:14:10 -07003602 }
3603 }
3604 break;
3605 }
Dongwon Kimc6e97aa2016-08-15 15:12:03 -07003606 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
Dongwon Kim70299472016-04-04 17:14:10 -07003607
Dongwon Kimc6e97aa2016-08-15 15:12:03 -07003608 return ret;
Marek Olšák9a0bda22015-04-10 10:56:02 +02003609}
3610
Dongwon Kim70299472016-04-04 17:14:10 -07003611static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003612dri2_signal_sync(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync,
Dongwon Kim70299472016-04-04 17:14:10 -07003613 EGLenum mode)
3614{
3615 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3616 EGLint ret;
3617
Emil Velikovc58af5c2017-06-20 15:40:28 +01003618 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3619 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
Dongwon Kim70299472016-04-04 17:14:10 -07003620
Emil Velikovc58af5c2017-06-20 15:40:28 +01003621 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3622 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
Dongwon Kim70299472016-04-04 17:14:10 -07003623
3624 dri2_sync->base.SyncStatus = mode;
3625
3626 if (mode == EGL_SIGNALED_KHR) {
3627 ret = cnd_broadcast(&dri2_sync->cond);
3628
3629 /* fail to broadcast */
Emil Velikovc58af5c2017-06-20 15:40:28 +01003630 if (ret)
3631 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
Dongwon Kim70299472016-04-04 17:14:10 -07003632 }
3633
3634 return EGL_TRUE;
3635}
3636
Marek Olšáka8617cc2015-04-10 12:04:18 +02003637static EGLint
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003638dri2_server_wait_sync(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync)
Marek Olšáka8617cc2015-04-10 12:04:18 +02003639{
3640 _EGLContext *ctx = _eglGetCurrentContext();
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003641 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Marek Olšáka8617cc2015-04-10 12:04:18 +02003642 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3643 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3644
3645 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3646 dri2_sync->fence, 0);
3647 return EGL_TRUE;
3648}
3649
Marek Olšákb6eda702016-03-03 15:59:48 +01003650static int
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003651dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikov13faddb2016-05-30 10:56:33 +01003652 struct mesa_glinterop_device_info *out)
Marek Olšákb6eda702016-03-03 15:59:48 +01003653{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003654 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Marek Olšákb6eda702016-03-03 15:59:48 +01003655 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3656
3657 if (!dri2_dpy->interop)
3658 return MESA_GLINTEROP_UNSUPPORTED;
3659
3660 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3661}
3662
3663static int
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003664dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
Emil Velikov13faddb2016-05-30 10:56:33 +01003665 struct mesa_glinterop_export_in *in,
3666 struct mesa_glinterop_export_out *out)
Marek Olšákb6eda702016-03-03 15:59:48 +01003667{
Eric Engestrom54fa5ec2019-02-02 11:38:45 +00003668 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
Marek Olšákb6eda702016-03-03 15:59:48 +01003669 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3670
3671 if (!dri2_dpy->interop)
3672 return MESA_GLINTEROP_UNSUPPORTED;
3673
3674 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3675}
3676
Eric Engestrom9c6fa942020-07-31 01:38:41 +02003677const _EGLDriver _eglDriver = {
Eric Engestromd24e3ea2020-07-22 01:06:05 +02003678 .Initialize = dri2_initialize,
3679 .Terminate = dri2_terminate,
3680 .CreateContext = dri2_create_context,
3681 .DestroyContext = dri2_destroy_context,
3682 .MakeCurrent = dri2_make_current,
3683 .CreateWindowSurface = dri2_create_window_surface,
3684 .CreatePixmapSurface = dri2_create_pixmap_surface,
3685 .CreatePbufferSurface = dri2_create_pbuffer_surface,
3686 .DestroySurface = dri2_destroy_surface,
3687 .GetProcAddress = dri2_get_proc_address,
3688 .WaitClient = dri2_wait_client,
3689 .WaitNative = dri2_wait_native,
3690 .BindTexImage = dri2_bind_tex_image,
3691 .ReleaseTexImage = dri2_release_tex_image,
3692 .SwapInterval = dri2_swap_interval,
3693 .SwapBuffers = dri2_swap_buffers,
3694 .SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3695 .SwapBuffersRegionNOK = dri2_swap_buffers_region,
3696 .SetDamageRegion = dri2_set_damage_region,
3697 .PostSubBufferNV = dri2_post_sub_buffer,
3698 .CopyBuffers = dri2_copy_buffers,
3699 .QueryBufferAge = dri2_query_buffer_age,
3700 .CreateImageKHR = dri2_create_image,
3701 .DestroyImageKHR = dri2_destroy_image_khr,
3702 .CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3703 .QuerySurface = dri2_query_surface,
3704 .QueryDriverName = dri2_query_driver_name,
3705 .QueryDriverConfig = dri2_query_driver_config,
Axel Davy63986f92015-01-14 20:36:04 +01003706#ifdef HAVE_LIBDRM
Eric Engestromd24e3ea2020-07-22 01:06:05 +02003707 .CreateDRMImageMESA = dri2_create_drm_image_mesa,
3708 .ExportDRMImageMESA = dri2_export_drm_image_mesa,
3709 .ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3710 .ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3711 .QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3712 .QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
Samuel Thibaulta594cec2013-11-10 19:32:01 +01003713#endif
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003714#ifdef HAVE_WAYLAND_PLATFORM
Eric Engestromd24e3ea2020-07-22 01:06:05 +02003715 .BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3716 .UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3717 .QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
Benjamin Franzke6b369c42011-02-21 16:22:34 +01003718#endif
Eric Engestromd24e3ea2020-07-22 01:06:05 +02003719 .GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3720 .CreateSyncKHR = dri2_create_sync,
3721 .ClientWaitSyncKHR = dri2_client_wait_sync,
3722 .SignalSyncKHR = dri2_signal_sync,
3723 .WaitSyncKHR = dri2_server_wait_sync,
3724 .DestroySyncKHR = dri2_destroy_sync,
3725 .GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3726 .GLInteropExportObject = dri2_interop_export_object,
3727 .DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3728 .SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,
3729};