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