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> |
Emil Velikov | 6638c55 | 2014-05-28 14:36:46 +0100 | [diff] [blame] | 41 | #ifdef HAVE_LIBDRM |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 42 | #include <xf86drm.h> |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 43 | #include <drm_fourcc.h> |
Samuel Thibault | a594cec | 2013-11-10 19:32:01 +0100 | [diff] [blame] | 44 | #endif |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 45 | #include <GL/gl.h> |
| 46 | #include <GL/internal/dri_interface.h> |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 50 | #ifdef HAVE_WAYLAND_PLATFORM |
| 51 | #include "wayland-drm.h" |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 52 | #include "wayland-drm-client-protocol.h" |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 53 | #endif |
| 54 | |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 55 | #include "egl_dri2.h" |
Alexander von Gluck IV | ba65196 | 2015-08-07 12:55:40 -0500 | [diff] [blame] | 56 | #include "util/u_atomic.h" |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 57 | |
Chad Versace | fd865d5 | 2015-06-23 15:48:17 -0700 | [diff] [blame] | 58 | /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate |
| 59 | * some of the definitions here so that building Mesa won't bleeding-edge |
| 60 | * kernel headers. |
| 61 | */ |
| 62 | #ifndef DRM_FORMAT_R8 |
| 63 | #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */ |
| 64 | #endif |
| 65 | |
| 66 | #ifndef DRM_FORMAT_RG88 |
| 67 | #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */ |
| 68 | #endif |
| 69 | |
| 70 | #ifndef DRM_FORMAT_GR88 |
| 71 | #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */ |
| 72 | #endif |
| 73 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 74 | const __DRIuseInvalidateExtension use_invalidate = { |
Emil Velikov | 5f280d0 | 2014-02-12 20:13:15 +0000 | [diff] [blame] | 75 | .base = { __DRI_USE_INVALIDATE, 1 } |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 78 | EGLint dri2_to_egl_attribute_map[] = { |
| 79 | 0, |
| 80 | EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */ |
| 81 | EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */ |
| 82 | EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */ |
| 83 | EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */ |
| 84 | EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 85 | EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 86 | EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */ |
| 87 | 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */ |
| 88 | EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */ |
| 89 | EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */ |
| 90 | 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */ |
| 91 | 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */ |
| 92 | 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */ |
| 93 | 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */ |
| 94 | EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */ |
| 95 | EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */ |
| 96 | 0, /* __DRI_ATTRIB_RENDER_TYPE, */ |
| 97 | 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */ |
| 98 | 0, /* __DRI_ATTRIB_CONFORMANT */ |
| 99 | 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */ |
| 100 | 0, /* __DRI_ATTRIB_STEREO */ |
| 101 | 0, /* __DRI_ATTRIB_AUX_BUFFERS */ |
| 102 | 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */ |
| 103 | 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */ |
| 104 | 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */ |
| 105 | 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */ |
| 106 | 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */ |
| 107 | 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */ |
Tomasz Lis | c37c367 | 2013-07-17 13:49:21 +0200 | [diff] [blame] | 108 | 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 109 | 0, /* __DRI_ATTRIB_RED_MASK */ |
| 110 | 0, /* __DRI_ATTRIB_GREEN_MASK */ |
| 111 | 0, /* __DRI_ATTRIB_BLUE_MASK */ |
| 112 | 0, /* __DRI_ATTRIB_ALPHA_MASK */ |
| 113 | EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */ |
| 114 | EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */ |
| 115 | EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */ |
| 116 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */ |
| 117 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */ |
| 118 | 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */ |
| 119 | 0, /* __DRI_ATTRIB_SWAP_METHOD */ |
| 120 | EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */ |
| 121 | EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 122 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */ |
| 123 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 124 | 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */ |
| 125 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */ |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 126 | EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */ |
Benjamin Franzke | 4f6fbfa | 2011-03-07 00:29:13 +0100 | [diff] [blame] | 127 | 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 128 | }; |
| 129 | |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 130 | const __DRIconfig * |
| 131 | dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type, |
| 132 | EGLenum colorspace) |
| 133 | { |
Emil Velikov | 858f2f2 | 2015-09-13 12:25:27 +0100 | [diff] [blame] | 134 | const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR; |
| 135 | |
| 136 | return surface_type == EGL_WINDOW_BIT ? conf->dri_double_config[srgb] : |
| 137 | conf->dri_single_config[srgb]; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Benjamin Franzke | 16e3027 | 2011-06-11 23:00:40 +0200 | [diff] [blame] | 140 | static EGLBoolean |
| 141 | dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria) |
| 142 | { |
| 143 | if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0) |
| 144 | return EGL_FALSE; |
| 145 | |
| 146 | if (!_eglMatchConfig(conf, criteria)) |
| 147 | return EGL_FALSE; |
| 148 | |
| 149 | return EGL_TRUE; |
| 150 | } |
| 151 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 152 | struct dri2_egl_config * |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 153 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id, |
Kristian Høgsberg | 44e584a | 2013-09-14 23:13:22 -0700 | [diff] [blame] | 154 | EGLint surface_type, const EGLint *attr_list, |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 155 | const unsigned int *rgba_masks) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 156 | { |
| 157 | struct dri2_egl_config *conf; |
| 158 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 159 | _EGLConfig base; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 160 | unsigned int attrib, value, double_buffer; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 161 | bool srgb = false; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 162 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 163 | unsigned int dri_masks[4] = { 0, 0, 0, 0 }; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 164 | _EGLConfig *matching_config; |
| 165 | EGLint num_configs = 0; |
| 166 | EGLint config_id; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 167 | int i; |
| 168 | |
| 169 | dri2_dpy = disp->DriverData; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 170 | _eglInitConfig(&base, disp, id); |
Boyan Ding | 052b3d4 | 2015-06-13 15:36:27 +0800 | [diff] [blame] | 171 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 172 | i = 0; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 173 | double_buffer = 0; |
| 174 | bind_to_texture_rgb = 0; |
| 175 | bind_to_texture_rgba = 0; |
| 176 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 177 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 178 | switch (attrib) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 179 | case __DRI_ATTRIB_RENDER_TYPE: |
| 180 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 181 | value = EGL_RGB_BUFFER; |
| 182 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 183 | value = EGL_LUMINANCE_BUFFER; |
| 184 | else |
Ian Romanick | 74cbe6e | 2013-07-18 15:13:45 -0700 | [diff] [blame] | 185 | return NULL; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 186 | _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value); |
Boyan Ding | 052b3d4 | 2015-06-13 15:36:27 +0800 | [diff] [blame] | 187 | break; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 188 | |
| 189 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 190 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 191 | value = EGL_NON_CONFORMANT_CONFIG; |
| 192 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 193 | value = EGL_SLOW_CONFIG; |
| 194 | else |
| 195 | value = EGL_NONE; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 196 | _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 197 | break; |
| 198 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 199 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 200 | bind_to_texture_rgb = value; |
| 201 | break; |
| 202 | |
| 203 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 204 | bind_to_texture_rgba = value; |
| 205 | break; |
| 206 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 207 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 208 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 209 | break; |
| 210 | |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 211 | case __DRI_ATTRIB_RED_MASK: |
| 212 | dri_masks[0] = value; |
| 213 | break; |
| 214 | |
| 215 | case __DRI_ATTRIB_GREEN_MASK: |
| 216 | dri_masks[1] = value; |
| 217 | break; |
| 218 | |
| 219 | case __DRI_ATTRIB_BLUE_MASK: |
| 220 | dri_masks[2] = value; |
| 221 | break; |
| 222 | |
| 223 | case __DRI_ATTRIB_ALPHA_MASK: |
| 224 | dri_masks[3] = value; |
| 225 | break; |
| 226 | |
Marek Olšák | 396cbab | 2015-04-09 23:25:07 +0200 | [diff] [blame] | 227 | case __DRI_ATTRIB_ACCUM_RED_SIZE: |
| 228 | case __DRI_ATTRIB_ACCUM_GREEN_SIZE: |
| 229 | case __DRI_ATTRIB_ACCUM_BLUE_SIZE: |
| 230 | case __DRI_ATTRIB_ACCUM_ALPHA_SIZE: |
| 231 | /* Don't expose visuals with the accumulation buffer. */ |
| 232 | if (value > 0) |
| 233 | return NULL; |
| 234 | break; |
| 235 | |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 236 | case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE: |
| 237 | srgb = value != 0; |
Emil Velikov | 54702c2 | 2015-11-29 16:48:51 +0000 | [diff] [blame^] | 238 | if (!disp->Extensions.KHR_gl_colorspace && srgb) |
| 239 | return NULL; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 240 | break; |
| 241 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 242 | default: |
| 243 | key = dri2_to_egl_attribute_map[attrib]; |
| 244 | if (key != 0) |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 245 | _eglSetConfigKey(&base, key, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 250 | if (attr_list) |
| 251 | for (i = 0; attr_list[i] != EGL_NONE; i += 2) |
| 252 | _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 253 | |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 254 | if (rgba_masks && memcmp(rgba_masks, dri_masks, sizeof(dri_masks))) |
| 255 | return NULL; |
| 256 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 257 | base.NativeRenderable = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 258 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 259 | base.SurfaceType = surface_type; |
Benjamin Franzke | a8128d7 | 2011-02-04 12:32:30 +0100 | [diff] [blame] | 260 | if (surface_type & (EGL_PBUFFER_BIT | |
| 261 | (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) { |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 262 | base.BindToTextureRGB = bind_to_texture_rgb; |
| 263 | if (base.AlphaSize > 0) |
| 264 | base.BindToTextureRGBA = bind_to_texture_rgba; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 265 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 266 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 267 | base.RenderableType = disp->ClientAPIs; |
| 268 | base.Conformant = disp->ClientAPIs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 269 | |
Chad Versace | d3dfce3 | 2013-04-23 04:17:48 +0200 | [diff] [blame] | 270 | base.MinSwapInterval = dri2_dpy->min_swap_interval; |
| 271 | base.MaxSwapInterval = dri2_dpy->max_swap_interval; |
| 272 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 273 | if (!_eglValidateConfig(&base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 274 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 275 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 278 | config_id = base.ConfigID; |
| 279 | base.ConfigID = EGL_DONT_CARE; |
| 280 | base.SurfaceType = EGL_DONT_CARE; |
| 281 | num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1, |
Benjamin Franzke | 16e3027 | 2011-06-11 23:00:40 +0200 | [diff] [blame] | 282 | (_EGLArrayForEach) dri2_match_config, &base); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 283 | |
| 284 | if (num_configs == 1) { |
| 285 | conf = (struct dri2_egl_config *) matching_config; |
| 286 | |
Emil Velikov | 858f2f2 | 2015-09-13 12:25:27 +0100 | [diff] [blame] | 287 | if (double_buffer && !conf->dri_double_config[srgb]) |
| 288 | conf->dri_double_config[srgb] = dri_config; |
| 289 | else if (!double_buffer && !conf->dri_single_config[srgb]) |
| 290 | conf->dri_single_config[srgb] = dri_config; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 291 | else |
Benjamin Franzke | 83c6875 | 2011-05-12 19:30:05 +0200 | [diff] [blame] | 292 | /* a similar config type is already added (unlikely) => discard */ |
| 293 | return NULL; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 294 | } |
José Fonseca | 4a47d6d | 2011-05-12 15:23:48 +0100 | [diff] [blame] | 295 | else if (num_configs == 0) { |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 296 | conf = calloc(1, sizeof *conf); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 297 | if (conf == NULL) |
| 298 | return NULL; |
| 299 | |
Emil Velikov | 858f2f2 | 2015-09-13 12:25:27 +0100 | [diff] [blame] | 300 | if (double_buffer) |
| 301 | conf->dri_double_config[srgb] = dri_config; |
| 302 | else |
| 303 | conf->dri_single_config[srgb] = dri_config; |
Varad Gautam | f988eff | 2015-10-07 09:48:14 +0530 | [diff] [blame] | 304 | |
Varad Gautam | deb1765 | 2015-10-07 09:48:15 +0530 | [diff] [blame] | 305 | memcpy(&conf->base, &base, sizeof base); |
Varad Gautam | f988eff | 2015-10-07 09:48:14 +0530 | [diff] [blame] | 306 | conf->base.SurfaceType = 0; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 307 | conf->base.ConfigID = config_id; |
| 308 | |
Chia-I Wu | 8a6bdf3 | 2010-10-23 00:47:22 +0800 | [diff] [blame] | 309 | _eglLinkConfig(&conf->base); |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 310 | } |
José Fonseca | 4a47d6d | 2011-05-12 15:23:48 +0100 | [diff] [blame] | 311 | else { |
| 312 | assert(0); |
| 313 | return NULL; |
| 314 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 315 | |
Fredrik Höglund | 655f2c1 | 2011-12-20 17:17:02 +0100 | [diff] [blame] | 316 | if (double_buffer) { |
Kristian Høgsberg | 447bb45 | 2011-08-31 18:20:06 -0400 | [diff] [blame] | 317 | surface_type &= ~EGL_PIXMAP_BIT; |
Fredrik Höglund | 655f2c1 | 2011-12-20 17:17:02 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Kristian Høgsberg | 447bb45 | 2011-08-31 18:20:06 -0400 | [diff] [blame] | 320 | conf->base.SurfaceType |= surface_type; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 321 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 322 | return conf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 325 | __DRIimage * |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 326 | dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 327 | { |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 328 | _EGLDisplay *disp = data; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 329 | struct dri2_egl_image *dri2_img; |
| 330 | _EGLImage *img; |
| 331 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 332 | (void) screen; |
| 333 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 334 | img = _eglLookupImage(image, disp); |
| 335 | if (img == NULL) { |
| 336 | _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image"); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | dri2_img = dri2_egl_image(image); |
| 341 | |
| 342 | return dri2_img->dri_image; |
| 343 | } |
| 344 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 345 | const __DRIimageLookupExtension image_lookup_extension = { |
Emil Velikov | 5f280d0 | 2014-02-12 20:13:15 +0000 | [diff] [blame] | 346 | .base = { __DRI_IMAGE_LOOKUP, 1 }, |
| 347 | |
| 348 | .lookupEGLImage = dri2_lookup_egl_image |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 349 | }; |
| 350 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 351 | struct dri2_extension_match { |
| 352 | const char *name; |
| 353 | int version; |
| 354 | int offset; |
| 355 | }; |
| 356 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 357 | static struct dri2_extension_match dri3_driver_extensions[] = { |
| 358 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 359 | { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) }, |
| 360 | { NULL, 0, 0 } |
| 361 | }; |
| 362 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 363 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 364 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
Ian Romanick | 38f91f2 | 2012-07-18 13:17:50 -0700 | [diff] [blame] | 365 | { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 366 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 370 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 371 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Kristian Høgsberg | 1670737 | 2013-03-19 13:20:36 -0400 | [diff] [blame] | 372 | { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 373 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 374 | }; |
| 375 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 376 | static struct dri2_extension_match swrast_driver_extensions[] = { |
| 377 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 378 | { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) }, |
Ian Romanick | 9d76ad2 | 2012-07-19 11:08:02 -0700 | [diff] [blame] | 379 | { NULL, 0, 0 } |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | static struct dri2_extension_match swrast_core_extensions[] = { |
| 383 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Ian Romanick | 9d76ad2 | 2012-07-19 11:08:02 -0700 | [diff] [blame] | 384 | { NULL, 0, 0 } |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 385 | }; |
| 386 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 387 | static EGLBoolean |
| 388 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 389 | struct dri2_extension_match *matches, |
| 390 | const __DRIextension **extensions) |
| 391 | { |
| 392 | int i, j, ret = EGL_TRUE; |
| 393 | void *field; |
| 394 | |
| 395 | for (i = 0; extensions[i]; i++) { |
Martin Peres | 4518eea | 2015-10-30 17:16:35 +0200 | [diff] [blame] | 396 | _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name); |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 397 | for (j = 0; matches[j].name; j++) { |
| 398 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 399 | extensions[i]->version >= matches[j].version) { |
| 400 | field = ((char *) dri2_dpy + matches[j].offset); |
| 401 | *(const __DRIextension **) field = extensions[i]; |
Martin Peres | 4518eea | 2015-10-30 17:16:35 +0200 | [diff] [blame] | 402 | _eglLog(_EGL_INFO, "found extension %s version %d", |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 403 | extensions[i]->name, extensions[i]->version); |
| 404 | } |
| 405 | } |
| 406 | } |
Boyan Ding | 052b3d4 | 2015-06-13 15:36:27 +0800 | [diff] [blame] | 407 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 408 | for (j = 0; matches[j].name; j++) { |
| 409 | field = ((char *) dri2_dpy + matches[j].offset); |
| 410 | if (*(const __DRIextension **) field == NULL) { |
Martin Peres | 4518eea | 2015-10-30 17:16:35 +0200 | [diff] [blame] | 411 | _eglLog(_EGL_WARNING, "did not find extension %s version %d", |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 412 | matches[j].name, matches[j].version); |
| 413 | ret = EGL_FALSE; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 420 | static const __DRIextension ** |
| 421 | dri2_open_driver(_EGLDisplay *disp) |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 422 | { |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 423 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
Eric Anholt | a64bb75 | 2013-09-24 11:05:22 -0700 | [diff] [blame] | 424 | const __DRIextension **extensions = NULL; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 425 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
Eric Anholt | a64bb75 | 2013-09-24 11:05:22 -0700 | [diff] [blame] | 426 | char *get_extensions_name; |
| 427 | const __DRIextension **(*get_extensions)(void); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 428 | |
| 429 | search_paths = NULL; |
| 430 | if (geteuid() == getuid()) { |
| 431 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 432 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 433 | } |
| 434 | if (search_paths == NULL) |
| 435 | search_paths = DEFAULT_DRIVER_DIR; |
| 436 | |
| 437 | dri2_dpy->driver = NULL; |
| 438 | end = search_paths + strlen(search_paths); |
Eric Anholt | 932d161 | 2015-06-09 12:16:19 -0700 | [diff] [blame] | 439 | for (p = search_paths; p < end; p = next + 1) { |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 440 | int len; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 441 | next = strchr(p, ':'); |
| 442 | if (next == NULL) |
| 443 | next = end; |
| 444 | |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 445 | len = next - p; |
| 446 | #if GLX_USE_TLS |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 447 | snprintf(path, sizeof path, |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 448 | "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 449 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 450 | #endif |
| 451 | if (dri2_dpy->driver == NULL) { |
| 452 | snprintf(path, sizeof path, |
| 453 | "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name); |
| 454 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 455 | if (dri2_dpy->driver == NULL) |
| 456 | _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror()); |
| 457 | } |
Quanxian Wang | 1413a09 | 2013-12-18 22:11:00 -0700 | [diff] [blame] | 458 | /* not need continue to loop all paths once the driver is found */ |
| 459 | if (dri2_dpy->driver != NULL) |
| 460 | break; |
Chih-Wei Huang | c3b5afb | 2015-05-20 11:25:30 +0800 | [diff] [blame] | 461 | |
| 462 | #ifdef ANDROID |
| 463 | snprintf(path, sizeof path, "%.*s/gallium_dri.so", len, p); |
| 464 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 465 | if (dri2_dpy->driver == NULL) |
| 466 | _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror()); |
| 467 | else |
| 468 | break; |
| 469 | #endif |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (dri2_dpy->driver == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 473 | _eglLog(_EGL_WARNING, |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 474 | "DRI2: failed to open %s (search paths %s)", |
| 475 | dri2_dpy->driver_name, search_paths); |
| 476 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
Eric Anholt | a64bb75 | 2013-09-24 11:05:22 -0700 | [diff] [blame] | 480 | |
| 481 | if (asprintf(&get_extensions_name, "%s_%s", |
| 482 | __DRI_DRIVER_GET_EXTENSIONS, dri2_dpy->driver_name) != -1) { |
| 483 | get_extensions = dlsym(dri2_dpy->driver, get_extensions_name); |
| 484 | if (get_extensions) { |
| 485 | extensions = get_extensions(); |
| 486 | } else { |
| 487 | _eglLog(_EGL_DEBUG, "driver does not expose %s(): %s\n", |
| 488 | get_extensions_name, dlerror()); |
| 489 | } |
| 490 | free(get_extensions_name); |
| 491 | } |
| 492 | |
| 493 | if (!extensions) |
| 494 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 495 | if (extensions == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 496 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 497 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 498 | dlclose(dri2_dpy->driver); |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | return extensions; |
| 502 | } |
| 503 | |
| 504 | EGLBoolean |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 505 | dri2_load_driver_dri3(_EGLDisplay *disp) |
| 506 | { |
| 507 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 508 | const __DRIextension **extensions; |
| 509 | |
| 510 | extensions = dri2_open_driver(disp); |
| 511 | if (!extensions) |
| 512 | return EGL_FALSE; |
| 513 | |
| 514 | if (!dri2_bind_extensions(dri2_dpy, dri3_driver_extensions, extensions)) { |
| 515 | dlclose(dri2_dpy->driver); |
| 516 | return EGL_FALSE; |
| 517 | } |
| 518 | dri2_dpy->driver_extensions = extensions; |
| 519 | |
| 520 | return EGL_TRUE; |
| 521 | } |
| 522 | |
| 523 | EGLBoolean |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 524 | dri2_load_driver(_EGLDisplay *disp) |
| 525 | { |
| 526 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 527 | const __DRIextension **extensions; |
| 528 | |
| 529 | extensions = dri2_open_driver(disp); |
| 530 | if (!extensions) |
| 531 | return EGL_FALSE; |
| 532 | |
| 533 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) { |
| 534 | dlclose(dri2_dpy->driver); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 535 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 536 | } |
Eric Anholt | f93533d | 2013-09-27 11:39:25 -0700 | [diff] [blame] | 537 | dri2_dpy->driver_extensions = extensions; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 538 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 539 | return EGL_TRUE; |
| 540 | } |
| 541 | |
| 542 | EGLBoolean |
| 543 | dri2_load_driver_swrast(_EGLDisplay *disp) |
| 544 | { |
| 545 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 546 | const __DRIextension **extensions; |
| 547 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 548 | extensions = dri2_open_driver(disp); |
| 549 | if (!extensions) |
| 550 | return EGL_FALSE; |
| 551 | |
| 552 | if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) { |
| 553 | dlclose(dri2_dpy->driver); |
| 554 | return EGL_FALSE; |
| 555 | } |
Eric Anholt | f93533d | 2013-09-27 11:39:25 -0700 | [diff] [blame] | 556 | dri2_dpy->driver_extensions = extensions; |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 557 | |
| 558 | return EGL_TRUE; |
| 559 | } |
| 560 | |
Marek Olšák | 4f57ccd | 2015-06-10 14:45:58 +0200 | [diff] [blame] | 561 | static unsigned |
| 562 | dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param) |
| 563 | { |
| 564 | const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery; |
| 565 | unsigned int value = 0; |
| 566 | |
| 567 | if (!rendererQuery || |
| 568 | rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1) |
| 569 | return 0; |
| 570 | |
| 571 | return value; |
| 572 | } |
| 573 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 574 | void |
| 575 | dri2_setup_screen(_EGLDisplay *disp) |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 576 | { |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 577 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 578 | unsigned int api_mask; |
| 579 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 580 | if (dri2_dpy->image_driver) { |
| 581 | api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen); |
| 582 | } else if (dri2_dpy->dri2) { |
Ian Romanick | 38f91f2 | 2012-07-18 13:17:50 -0700 | [diff] [blame] | 583 | api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 584 | } else { |
| 585 | assert(dri2_dpy->swrast); |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 586 | api_mask = 1 << __DRI_API_OPENGL | |
| 587 | 1 << __DRI_API_GLES | |
| 588 | 1 << __DRI_API_GLES2 | |
| 589 | 1 << __DRI_API_GLES3; |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 590 | } |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 591 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 592 | disp->ClientAPIs = 0; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 593 | if (api_mask & (1 <<__DRI_API_OPENGL)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 594 | disp->ClientAPIs |= EGL_OPENGL_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 595 | if (api_mask & (1 <<__DRI_API_GLES)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 596 | disp->ClientAPIs |= EGL_OPENGL_ES_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 597 | if (api_mask & (1 << __DRI_API_GLES2)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 598 | disp->ClientAPIs |= EGL_OPENGL_ES2_BIT; |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 599 | if (api_mask & (1 << __DRI_API_GLES3)) |
| 600 | disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 601 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 602 | assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast); |
Ian Romanick | 38f91f2 | 2012-07-18 13:17:50 -0700 | [diff] [blame] | 603 | disp->Extensions.KHR_surfaceless_context = EGL_TRUE; |
Neil Roberts | 551d459 | 2014-03-07 18:05:47 +0000 | [diff] [blame] | 604 | disp->Extensions.MESA_configless_context = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 605 | |
Marek Olšák | 4f57ccd | 2015-06-10 14:45:58 +0200 | [diff] [blame] | 606 | if (dri2_renderer_query_integer(dri2_dpy, |
| 607 | __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB)) |
| 608 | disp->Extensions.KHR_gl_colorspace = EGL_TRUE; |
| 609 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 610 | if (dri2_dpy->image_driver || |
| 611 | (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) || |
Boyan Ding | 63c4b7e | 2015-07-21 23:43:58 +0800 | [diff] [blame] | 612 | (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) { |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 613 | disp->Extensions.KHR_create_context = EGL_TRUE; |
| 614 | |
| 615 | if (dri2_dpy->robustness) |
| 616 | disp->Extensions.EXT_create_context_robustness = EGL_TRUE; |
| 617 | } |
| 618 | |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 619 | if (dri2_dpy->fence) { |
| 620 | disp->Extensions.KHR_fence_sync = EGL_TRUE; |
Marek Olšák | a8617cc | 2015-04-10 12:04:18 +0200 | [diff] [blame] | 621 | disp->Extensions.KHR_wait_sync = EGL_TRUE; |
Marek Olšák | 290a3eb | 2015-04-10 13:16:30 +0200 | [diff] [blame] | 622 | if (dri2_dpy->fence->get_fence_from_cl_event) |
| 623 | disp->Extensions.KHR_cl_event2 = EGL_TRUE; |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 626 | if (dri2_dpy->image) { |
Giovanni Campagna | e57ad3d | 2014-07-23 19:28:52 +0100 | [diff] [blame] | 627 | if (dri2_dpy->image->base.version >= 10 && |
| 628 | dri2_dpy->image->getCapabilities != NULL) { |
| 629 | int capabilities; |
| 630 | |
| 631 | capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen); |
| 632 | disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0; |
Dave Airlie | 8f7338f | 2014-03-03 13:57:16 +1000 | [diff] [blame] | 633 | |
| 634 | if (dri2_dpy->image->base.version >= 11) |
| 635 | disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE; |
| 636 | } else { |
Giovanni Campagna | e57ad3d | 2014-07-23 19:28:52 +0100 | [diff] [blame] | 637 | disp->Extensions.MESA_drm_image = EGL_TRUE; |
Dave Airlie | 8f7338f | 2014-03-03 13:57:16 +1000 | [diff] [blame] | 638 | if (dri2_dpy->image->base.version >= 11) |
| 639 | disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE; |
| 640 | } |
Giovanni Campagna | e57ad3d | 2014-07-23 19:28:52 +0100 | [diff] [blame] | 641 | |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 642 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 643 | disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE; |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 644 | if (dri2_dpy->image->base.version >= 5 && |
| 645 | dri2_dpy->image->createImageFromTexture) { |
| 646 | disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE; |
| 647 | disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE; |
| 648 | } |
Marek Olšák | 4f57ccd | 2015-06-10 14:45:58 +0200 | [diff] [blame] | 649 | if (dri2_renderer_query_integer(dri2_dpy, |
| 650 | __DRI2_RENDERER_HAS_TEXTURE_3D)) |
| 651 | disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE; |
Axel Davy | 63986f9 | 2015-01-14 20:36:04 +0100 | [diff] [blame] | 652 | #ifdef HAVE_LIBDRM |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 653 | if (dri2_dpy->image->base.version >= 8 && |
| 654 | dri2_dpy->image->createImageFromDmaBufs) { |
| 655 | disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE; |
| 656 | } |
Samuel Thibault | a594cec | 2013-11-10 19:32:01 +0100 | [diff] [blame] | 657 | #endif |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 658 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Emil Velikov | 8ed08e6 | 2014-11-07 03:42:15 +0000 | [diff] [blame] | 661 | /* All platforms but DRM call this function to create the screen, query the |
| 662 | * dri extensions, setup the vtables and populate the driver_configs. |
| 663 | * DRM inherits all that information from its display - GBM. |
| 664 | */ |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 665 | EGLBoolean |
| 666 | dri2_create_screen(_EGLDisplay *disp) |
| 667 | { |
| 668 | const __DRIextension **extensions; |
| 669 | struct dri2_egl_display *dri2_dpy; |
Axel Davy | 13fa84e | 2015-05-01 19:08:37 +0200 | [diff] [blame] | 670 | unsigned i; |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 671 | |
| 672 | dri2_dpy = disp->DriverData; |
| 673 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 674 | if (dri2_dpy->image_driver) { |
| 675 | dri2_dpy->dri_screen = |
| 676 | dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd, |
| 677 | dri2_dpy->extensions, |
| 678 | dri2_dpy->driver_extensions, |
| 679 | &dri2_dpy->driver_configs, |
| 680 | disp); |
| 681 | } else if (dri2_dpy->dri2) { |
Eric Anholt | f93533d | 2013-09-27 11:39:25 -0700 | [diff] [blame] | 682 | if (dri2_dpy->dri2->base.version >= 4) { |
| 683 | dri2_dpy->dri_screen = |
| 684 | dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd, |
| 685 | dri2_dpy->extensions, |
| 686 | dri2_dpy->driver_extensions, |
| 687 | &dri2_dpy->driver_configs, disp); |
| 688 | } else { |
| 689 | dri2_dpy->dri_screen = |
| 690 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, |
| 691 | dri2_dpy->extensions, |
| 692 | &dri2_dpy->driver_configs, disp); |
| 693 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 694 | } else { |
| 695 | assert(dri2_dpy->swrast); |
Eric Anholt | f93533d | 2013-09-27 11:39:25 -0700 | [diff] [blame] | 696 | if (dri2_dpy->swrast->base.version >= 4) { |
| 697 | dri2_dpy->dri_screen = |
| 698 | dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->extensions, |
| 699 | dri2_dpy->driver_extensions, |
| 700 | &dri2_dpy->driver_configs, disp); |
| 701 | } else { |
| 702 | dri2_dpy->dri_screen = |
| 703 | dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions, |
| 704 | &dri2_dpy->driver_configs, disp); |
| 705 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if (dri2_dpy->dri_screen == NULL) { |
| 709 | _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen"); |
| 710 | return EGL_FALSE; |
| 711 | } |
| 712 | |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 713 | dri2_dpy->own_dri_screen = 1; |
| 714 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 715 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
Boyan Ding | 052b3d4 | 2015-06-13 15:36:27 +0800 | [diff] [blame] | 716 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 717 | if (dri2_dpy->image_driver || dri2_dpy->dri2) { |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 718 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
| 719 | goto cleanup_dri_screen; |
| 720 | } else { |
| 721 | assert(dri2_dpy->swrast); |
| 722 | if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions)) |
| 723 | goto cleanup_dri_screen; |
| 724 | } |
| 725 | |
Axel Davy | 13fa84e | 2015-05-01 19:08:37 +0200 | [diff] [blame] | 726 | for (i = 0; extensions[i]; i++) { |
| 727 | if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) { |
| 728 | dri2_dpy->robustness = (__DRIrobustnessExtension *) extensions[i]; |
| 729 | } |
| 730 | if (strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0) { |
| 731 | dri2_dpy->config = (__DRI2configQueryExtension *) extensions[i]; |
| 732 | } |
| 733 | if (strcmp(extensions[i]->name, __DRI2_FENCE) == 0) { |
| 734 | dri2_dpy->fence = (__DRI2fenceExtension *) extensions[i]; |
| 735 | } |
Marek Olšák | 4f57ccd | 2015-06-10 14:45:58 +0200 | [diff] [blame] | 736 | if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) { |
| 737 | dri2_dpy->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i]; |
| 738 | } |
Axel Davy | 13fa84e | 2015-05-01 19:08:37 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 741 | dri2_setup_screen(disp); |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 742 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 743 | return EGL_TRUE; |
| 744 | |
| 745 | cleanup_dri_screen: |
| 746 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 747 | |
| 748 | return EGL_FALSE; |
| 749 | } |
| 750 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 751 | /** |
| 752 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 753 | */ |
| 754 | static EGLBoolean |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 755 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp) |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 756 | { |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 757 | /* not until swrast_dri is supported */ |
| 758 | if (disp->Options.UseFallback) |
| 759 | return EGL_FALSE; |
| 760 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 761 | switch (disp->Platform) { |
Haixia Shi | 6b8accb | 2015-06-12 10:10:58 -0700 | [diff] [blame] | 762 | #ifdef HAVE_SURFACELESS_PLATFORM |
| 763 | case _EGL_PLATFORM_SURFACELESS: |
| 764 | if (disp->Options.TestOnly) |
| 765 | return EGL_TRUE; |
| 766 | return dri2_initialize_surfaceless(drv, disp); |
| 767 | #endif |
| 768 | |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 769 | #ifdef HAVE_X11_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 770 | case _EGL_PLATFORM_X11: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 771 | if (disp->Options.TestOnly) |
| 772 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 773 | return dri2_initialize_x11(drv, disp); |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 774 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 775 | |
Benjamin Franzke | 629c15a | 2011-06-24 21:45:05 +0200 | [diff] [blame] | 776 | #ifdef HAVE_DRM_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 777 | case _EGL_PLATFORM_DRM: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 778 | if (disp->Options.TestOnly) |
| 779 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 780 | return dri2_initialize_drm(drv, disp); |
Benjamin Franzke | 629c15a | 2011-06-24 21:45:05 +0200 | [diff] [blame] | 781 | #endif |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 782 | #ifdef HAVE_WAYLAND_PLATFORM |
| 783 | case _EGL_PLATFORM_WAYLAND: |
| 784 | if (disp->Options.TestOnly) |
| 785 | return EGL_TRUE; |
| 786 | return dri2_initialize_wayland(drv, disp); |
| 787 | #endif |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 788 | #ifdef HAVE_ANDROID_PLATFORM |
| 789 | case _EGL_PLATFORM_ANDROID: |
| 790 | if (disp->Options.TestOnly) |
| 791 | return EGL_TRUE; |
| 792 | return dri2_initialize_android(drv, disp); |
| 793 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 794 | |
| 795 | default: |
Matt Turner | 0b4a688 | 2014-12-03 16:32:39 -0800 | [diff] [blame] | 796 | _eglLog(_EGL_WARNING, "No EGL platform enabled."); |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 797 | return EGL_FALSE; |
| 798 | } |
| 799 | } |
| 800 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 801 | /** |
| 802 | * Called via eglTerminate(), drv->API.Terminate(). |
| 803 | */ |
| 804 | static EGLBoolean |
| 805 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 806 | { |
| 807 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Emil Velikov | ba0bb42 | 2014-10-23 17:27:01 +0100 | [diff] [blame] | 808 | unsigned i; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 809 | |
| 810 | _eglReleaseDisplayResources(drv, disp); |
| 811 | _eglCleanupDisplay(disp); |
| 812 | |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 813 | if (dri2_dpy->own_dri_screen) |
| 814 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Emil Velikov | a1ac742 | 2015-09-10 14:41:38 +0100 | [diff] [blame] | 815 | if (dri2_dpy->fd >= 0) |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 816 | close(dri2_dpy->fd); |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 817 | if (dri2_dpy->driver) |
| 818 | dlclose(dri2_dpy->driver); |
Matt Turner | 5067506 | 2012-09-03 20:24:35 -0700 | [diff] [blame] | 819 | free(dri2_dpy->device_name); |
Emil Velikov | 5cb1cad | 2014-06-02 12:26:17 +0100 | [diff] [blame] | 820 | free(dri2_dpy->driver_name); |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 821 | |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 822 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 823 | #ifdef HAVE_X11_PLATFORM |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 824 | case _EGL_PLATFORM_X11: |
| 825 | if (dri2_dpy->own_device) { |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 826 | xcb_disconnect(dri2_dpy->conn); |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 827 | } |
| 828 | break; |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 829 | #endif |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 830 | #ifdef HAVE_DRM_PLATFORM |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 831 | case _EGL_PLATFORM_DRM: |
| 832 | if (dri2_dpy->own_device) { |
| 833 | gbm_device_destroy(&dri2_dpy->gbm_dri->base.base); |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 834 | } |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 835 | break; |
| 836 | #endif |
| 837 | #ifdef HAVE_WAYLAND_PLATFORM |
| 838 | case _EGL_PLATFORM_WAYLAND: |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 839 | if (dri2_dpy->wl_drm) |
| 840 | wl_drm_destroy(dri2_dpy->wl_drm); |
| 841 | if (dri2_dpy->wl_shm) |
| 842 | wl_shm_destroy(dri2_dpy->wl_shm); |
Axel Davy | 6aaf09b | 2015-05-01 00:03:32 +0200 | [diff] [blame] | 843 | wl_registry_destroy(dri2_dpy->wl_registry); |
| 844 | wl_event_queue_destroy(dri2_dpy->wl_queue); |
Chad Versace | f506ef6 | 2014-01-28 18:53:56 -0800 | [diff] [blame] | 845 | if (dri2_dpy->own_device) { |
| 846 | wl_display_disconnect(dri2_dpy->wl_dpy); |
| 847 | } |
| 848 | break; |
| 849 | #endif |
| 850 | default: |
| 851 | break; |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Emil Velikov | 38cec03 | 2014-11-07 03:33:56 +0000 | [diff] [blame] | 854 | /* The drm platform does not create the screen/driver_configs but reuses |
| 855 | * the ones from the gbm device. As such the gbm itself is responsible |
| 856 | * for the cleanup. |
| 857 | */ |
| 858 | if (disp->Platform != _EGL_PLATFORM_DRM) { |
| 859 | for (i = 0; dri2_dpy->driver_configs[i]; i++) |
| 860 | free((__DRIconfig *) dri2_dpy->driver_configs[i]); |
| 861 | free(dri2_dpy->driver_configs); |
| 862 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 863 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 864 | disp->DriverData = NULL; |
| 865 | |
| 866 | return EGL_TRUE; |
| 867 | } |
| 868 | |
Chad Versace | 5cf8536 | 2012-11-09 14:06:41 -0800 | [diff] [blame] | 869 | /** |
| 870 | * Set the error code after a call to |
| 871 | * dri2_egl_display::dri2::createContextAttribs. |
| 872 | */ |
| 873 | static void |
| 874 | dri2_create_context_attribs_error(int dri_error) |
| 875 | { |
| 876 | EGLint egl_error; |
| 877 | |
| 878 | switch (dri_error) { |
| 879 | case __DRI_CTX_ERROR_SUCCESS: |
| 880 | return; |
| 881 | |
| 882 | case __DRI_CTX_ERROR_NO_MEMORY: |
| 883 | egl_error = EGL_BAD_ALLOC; |
| 884 | break; |
| 885 | |
| 886 | /* From the EGL_KHR_create_context spec, section "Errors": |
| 887 | * |
| 888 | * * If <config> does not support a client API context compatible |
| 889 | * with the requested API major and minor version, [...] context flags, |
| 890 | * and context reset notification behavior (for client API types where |
| 891 | * these attributes are supported), then an EGL_BAD_MATCH error is |
| 892 | * generated. |
| 893 | * |
| 894 | * * If an OpenGL ES context is requested and the values for |
| 895 | * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and |
| 896 | * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that |
| 897 | * is not defined, than an EGL_BAD_MATCH error is generated. |
| 898 | * |
| 899 | * * If an OpenGL context is requested, the requested version is |
| 900 | * greater than 3.2, and the value for attribute |
| 901 | * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any |
| 902 | * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and |
| 903 | * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than |
| 904 | * one of these bits set; or if the implementation does not support |
| 905 | * the requested profile, then an EGL_BAD_MATCH error is generated. |
| 906 | */ |
| 907 | case __DRI_CTX_ERROR_BAD_API: |
| 908 | case __DRI_CTX_ERROR_BAD_VERSION: |
| 909 | case __DRI_CTX_ERROR_BAD_FLAG: |
| 910 | egl_error = EGL_BAD_MATCH; |
| 911 | break; |
| 912 | |
| 913 | /* From the EGL_KHR_create_context spec, section "Errors": |
| 914 | * |
| 915 | * * If an attribute name or attribute value in <attrib_list> is not |
| 916 | * recognized (including unrecognized bits in bitmask attributes), |
| 917 | * then an EGL_BAD_ATTRIBUTE error is generated." |
| 918 | */ |
| 919 | case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE: |
| 920 | case __DRI_CTX_ERROR_UNKNOWN_FLAG: |
| 921 | egl_error = EGL_BAD_ATTRIBUTE; |
| 922 | break; |
| 923 | |
| 924 | default: |
| 925 | assert(0); |
| 926 | egl_error = EGL_BAD_MATCH; |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | _eglError(egl_error, "dri2_create_context"); |
| 931 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 932 | |
Boyan Ding | b9ea608 | 2015-07-21 23:43:56 +0800 | [diff] [blame] | 933 | static bool |
| 934 | dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx, |
| 935 | struct dri2_egl_display *dri2_dpy, |
| 936 | uint32_t *ctx_attribs, |
| 937 | unsigned *num_attribs) |
| 938 | { |
| 939 | int pos = 0; |
| 940 | |
| 941 | assert(*num_attribs >= 8); |
| 942 | |
| 943 | ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION; |
| 944 | ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion; |
| 945 | ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION; |
| 946 | ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion; |
| 947 | |
| 948 | if (dri2_ctx->base.Flags != 0) { |
| 949 | /* If the implementation doesn't support the __DRI2_ROBUSTNESS |
| 950 | * extension, don't even try to send it the robust-access flag. |
| 951 | * It may explode. Instead, generate the required EGL error here. |
| 952 | */ |
| 953 | if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0 |
| 954 | && !dri2_dpy->robustness) { |
| 955 | _eglError(EGL_BAD_MATCH, "eglCreateContext"); |
| 956 | return false; |
| 957 | } |
| 958 | |
| 959 | ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS; |
| 960 | ctx_attribs[pos++] = dri2_ctx->base.Flags; |
| 961 | } |
| 962 | |
| 963 | if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) { |
| 964 | /* If the implementation doesn't support the __DRI2_ROBUSTNESS |
| 965 | * extension, don't even try to send it a reset strategy. It may |
| 966 | * explode. Instead, generate the required EGL error here. |
| 967 | */ |
| 968 | if (!dri2_dpy->robustness) { |
| 969 | _eglError(EGL_BAD_CONFIG, "eglCreateContext"); |
| 970 | return false; |
| 971 | } |
| 972 | |
| 973 | ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY; |
| 974 | ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT; |
| 975 | } |
| 976 | |
| 977 | *num_attribs = pos; |
| 978 | |
| 979 | return true; |
| 980 | } |
| 981 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 982 | /** |
| 983 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 984 | */ |
| 985 | static _EGLContext * |
| 986 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 987 | _EGLContext *share_list, const EGLint *attrib_list) |
| 988 | { |
| 989 | struct dri2_egl_context *dri2_ctx; |
| 990 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 991 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
Ian Romanick | cb77f5d | 2012-07-18 14:37:28 -0700 | [diff] [blame] | 992 | __DRIcontext *shared = |
| 993 | dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 994 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 995 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 996 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 997 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 998 | (void) drv; |
| 999 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1000 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 1001 | if (!dri2_ctx) { |
| 1002 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1006 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 1007 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1008 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1009 | switch (dri2_ctx->base.ClientAPI) { |
| 1010 | case EGL_OPENGL_ES_API: |
Ian Romanick | 3fd79dd | 2012-07-18 15:59:15 -0700 | [diff] [blame] | 1011 | switch (dri2_ctx->base.ClientMajorVersion) { |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1012 | case 1: |
| 1013 | api = __DRI_API_GLES; |
| 1014 | break; |
| 1015 | case 2: |
Ian Romanick | d786bf2 | 2013-01-15 14:17:46 -0800 | [diff] [blame] | 1016 | api = __DRI_API_GLES2; |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 1017 | break; |
Ian Romanick | dbecb41 | 2012-07-19 16:12:13 -0700 | [diff] [blame] | 1018 | case 3: |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 1019 | api = __DRI_API_GLES3; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1020 | break; |
| 1021 | default: |
Emil Velikov | a3e78ba | 2014-05-08 16:49:45 +0100 | [diff] [blame] | 1022 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 1023 | free(dri2_ctx); |
| 1024 | return NULL; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1025 | } |
| 1026 | break; |
| 1027 | case EGL_OPENGL_API: |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 1028 | if ((dri2_ctx->base.ClientMajorVersion >= 4 |
| 1029 | || (dri2_ctx->base.ClientMajorVersion == 3 |
| 1030 | && dri2_ctx->base.ClientMinorVersion >= 2)) |
| 1031 | && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR) |
| 1032 | api = __DRI_API_OPENGL_CORE; |
| 1033 | else |
| 1034 | api = __DRI_API_OPENGL; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1035 | break; |
| 1036 | default: |
| 1037 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
Vinson Lee | 080e91a | 2013-01-31 23:41:14 -0800 | [diff] [blame] | 1038 | free(dri2_ctx); |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1039 | return NULL; |
| 1040 | } |
| 1041 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 1042 | if (conf != NULL) { |
| 1043 | /* The config chosen here isn't necessarily |
| 1044 | * used for surfaces later. |
| 1045 | * A pixmap surface will use the single config. |
| 1046 | * This opportunity depends on disabling the |
| 1047 | * doubleBufferMode check in |
| 1048 | * src/mesa/main/context.c:check_compatible() |
| 1049 | */ |
Matt Turner | 164c827 | 2015-10-07 11:43:58 -0700 | [diff] [blame] | 1050 | if (dri2_config->dri_double_config[0]) |
| 1051 | dri_config = dri2_config->dri_double_config[0]; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 1052 | else |
Matt Turner | 164c827 | 2015-10-07 11:43:58 -0700 | [diff] [blame] | 1053 | dri_config = dri2_config->dri_single_config[0]; |
Chia-I Wu | 357d3f3 | 2011-08-27 01:42:16 +0800 | [diff] [blame] | 1054 | |
| 1055 | /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This |
| 1056 | * makes sure the back buffer will always be used. |
| 1057 | */ |
| 1058 | if (conf->SurfaceType & EGL_WINDOW_BIT) |
| 1059 | dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 1060 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 1061 | else |
| 1062 | dri_config = NULL; |
| 1063 | |
Boyan Ding | f35198b | 2015-07-21 23:44:00 +0800 | [diff] [blame] | 1064 | if (dri2_dpy->image_driver) { |
| 1065 | unsigned error; |
| 1066 | unsigned num_attribs = 8; |
| 1067 | uint32_t ctx_attribs[8]; |
| 1068 | |
| 1069 | if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs, |
| 1070 | &num_attribs)) |
| 1071 | goto cleanup; |
| 1072 | |
| 1073 | dri2_ctx->dri_context = |
| 1074 | dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen, |
| 1075 | api, |
| 1076 | dri_config, |
| 1077 | shared, |
| 1078 | num_attribs / 2, |
| 1079 | ctx_attribs, |
| 1080 | & error, |
| 1081 | dri2_ctx); |
| 1082 | dri2_create_context_attribs_error(error); |
| 1083 | } else if (dri2_dpy->dri2) { |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 1084 | if (dri2_dpy->dri2->base.version >= 3) { |
| 1085 | unsigned error; |
Boyan Ding | b9ea608 | 2015-07-21 23:43:56 +0800 | [diff] [blame] | 1086 | unsigned num_attribs = 8; |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 1087 | uint32_t ctx_attribs[8]; |
| 1088 | |
Boyan Ding | b9ea608 | 2015-07-21 23:43:56 +0800 | [diff] [blame] | 1089 | if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs, |
| 1090 | &num_attribs)) |
| 1091 | goto cleanup; |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 1092 | |
| 1093 | dri2_ctx->dri_context = |
| 1094 | dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen, |
| 1095 | api, |
| 1096 | dri_config, |
| 1097 | shared, |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 1098 | num_attribs / 2, |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 1099 | ctx_attribs, |
| 1100 | & error, |
| 1101 | dri2_ctx); |
Chad Versace | 5cf8536 | 2012-11-09 14:06:41 -0800 | [diff] [blame] | 1102 | dri2_create_context_attribs_error(error); |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 1103 | } else { |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 1104 | dri2_ctx->dri_context = |
| 1105 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 1106 | api, |
| 1107 | dri_config, |
Ian Romanick | cb77f5d | 2012-07-18 14:37:28 -0700 | [diff] [blame] | 1108 | shared, |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 1109 | dri2_ctx); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 1110 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1111 | } else { |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 1112 | assert(dri2_dpy->swrast); |
Boyan Ding | 6345d2d | 2015-07-21 23:43:57 +0800 | [diff] [blame] | 1113 | if (dri2_dpy->swrast->base.version >= 3) { |
| 1114 | unsigned error; |
| 1115 | unsigned num_attribs = 8; |
| 1116 | uint32_t ctx_attribs[8]; |
| 1117 | |
| 1118 | if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs, |
| 1119 | &num_attribs)) |
| 1120 | goto cleanup; |
| 1121 | |
| 1122 | dri2_ctx->dri_context = |
| 1123 | dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen, |
| 1124 | api, |
| 1125 | dri_config, |
| 1126 | shared, |
| 1127 | num_attribs / 2, |
| 1128 | ctx_attribs, |
| 1129 | & error, |
| 1130 | dri2_ctx); |
| 1131 | dri2_create_context_attribs_error(error); |
| 1132 | } else { |
| 1133 | dri2_ctx->dri_context = |
| 1134 | dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen, |
| 1135 | api, |
| 1136 | dri_config, |
| 1137 | shared, |
| 1138 | dri2_ctx); |
| 1139 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1140 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1141 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1142 | if (!dri2_ctx->dri_context) |
| 1143 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1144 | |
| 1145 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1146 | |
| 1147 | cleanup: |
| 1148 | free(dri2_ctx); |
| 1149 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1152 | /** |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame] | 1153 | * Called via eglDestroyContext(), drv->API.DestroyContext(). |
| 1154 | */ |
| 1155 | static EGLBoolean |
| 1156 | dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
| 1157 | { |
| 1158 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1159 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1160 | |
| 1161 | if (_eglPutContext(ctx)) { |
| 1162 | dri2_dpy->core->destroyContext(dri2_ctx->dri_context); |
| 1163 | free(dri2_ctx); |
| 1164 | } |
| 1165 | |
| 1166 | return EGL_TRUE; |
| 1167 | } |
| 1168 | |
| 1169 | /** |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1170 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 1171 | */ |
| 1172 | static EGLBoolean |
| 1173 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 1174 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 1175 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1176 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1177 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1178 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 1179 | _EGLContext *old_ctx; |
| 1180 | _EGLSurface *old_dsurf, *old_rsurf; |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1181 | _EGLSurface *tmp_dsurf, *tmp_rsurf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1182 | __DRIdrawable *ddraw, *rdraw; |
| 1183 | __DRIcontext *cctx; |
| 1184 | |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 1185 | /* make new bindings */ |
| 1186 | if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1187 | return EGL_FALSE; |
| 1188 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1189 | /* flush before context switch */ |
Chia-I Wu | 547e761 | 2010-10-26 15:00:56 +0800 | [diff] [blame] | 1190 | if (old_ctx && dri2_drv->glFlush) |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1191 | dri2_drv->glFlush(); |
| 1192 | |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1193 | ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL; |
| 1194 | rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1195 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 1196 | |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 1197 | if (old_ctx) { |
| 1198 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 1199 | dri2_dpy->core->unbindContext(old_cctx); |
| 1200 | } |
| 1201 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1202 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 1203 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 1204 | if (old_dsurf) |
| 1205 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 1206 | if (old_rsurf) |
| 1207 | drv->API.DestroySurface(drv, disp, old_rsurf); |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 1208 | if (old_ctx) |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame] | 1209 | drv->API.DestroyContext(drv, disp, old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1210 | |
| 1211 | return EGL_TRUE; |
| 1212 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 1213 | /* undo the previous _eglBindContext */ |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1214 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 1215 | assert(&dri2_ctx->base == ctx && |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1216 | tmp_dsurf == dsurf && |
| 1217 | tmp_rsurf == rsurf); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 1218 | |
| 1219 | _eglPutSurface(dsurf); |
| 1220 | _eglPutSurface(rsurf); |
| 1221 | _eglPutContext(ctx); |
| 1222 | |
| 1223 | _eglPutSurface(old_dsurf); |
| 1224 | _eglPutSurface(old_rsurf); |
| 1225 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1226 | |
| 1227 | return EGL_FALSE; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1231 | __DRIdrawable * |
| 1232 | dri2_surface_get_dri_drawable(_EGLSurface *surf) |
| 1233 | { |
| 1234 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1235 | |
| 1236 | return dri2_surf->dri_drawable; |
| 1237 | } |
| 1238 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1239 | /* |
| 1240 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 1241 | */ |
| 1242 | static _EGLProc |
| 1243 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 1244 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1245 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1246 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1247 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1248 | } |
| 1249 | |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 1250 | static _EGLSurface* |
| 1251 | dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 1252 | _EGLConfig *conf, void *native_window, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 1253 | const EGLint *attrib_list) |
| 1254 | { |
| 1255 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 1256 | return dri2_dpy->vtbl->create_window_surface(drv, dpy, conf, native_window, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 1257 | attrib_list); |
| 1258 | } |
| 1259 | |
Chad Versace | bc8b07a | 2014-01-28 16:45:07 -0800 | [diff] [blame] | 1260 | static _EGLSurface* |
| 1261 | dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 1262 | _EGLConfig *conf, void *native_pixmap, |
Chad Versace | bc8b07a | 2014-01-28 16:45:07 -0800 | [diff] [blame] | 1263 | const EGLint *attrib_list) |
| 1264 | { |
| 1265 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 1266 | return dri2_dpy->vtbl->create_pixmap_surface(drv, dpy, conf, native_pixmap, |
Chad Versace | bc8b07a | 2014-01-28 16:45:07 -0800 | [diff] [blame] | 1267 | attrib_list); |
| 1268 | } |
| 1269 | |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1270 | static _EGLSurface* |
| 1271 | dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy, |
| 1272 | _EGLConfig *conf, const EGLint *attrib_list) |
| 1273 | { |
| 1274 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1275 | return dri2_dpy->vtbl->create_pbuffer_surface(drv, dpy, conf, attrib_list); |
| 1276 | } |
| 1277 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1278 | static EGLBoolean |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1279 | dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) |
| 1280 | { |
| 1281 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1282 | return dri2_dpy->vtbl->destroy_surface(drv, dpy, surf); |
| 1283 | } |
| 1284 | |
| 1285 | static EGLBoolean |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 1286 | dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, |
| 1287 | EGLint interval) |
| 1288 | { |
| 1289 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1290 | return dri2_dpy->vtbl->swap_interval(drv, dpy, surf, interval); |
| 1291 | } |
| 1292 | |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 1293 | /** |
| 1294 | * Asks the client API to flush any rendering to the drawable so that we can |
| 1295 | * do our swapbuffers. |
| 1296 | */ |
| 1297 | void |
| 1298 | dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw) |
| 1299 | { |
| 1300 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1301 | __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw); |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 1302 | |
| 1303 | if (dri2_dpy->flush) { |
| 1304 | if (dri2_dpy->flush->base.version >= 4) { |
| 1305 | /* We know there's a current context because: |
| 1306 | * |
| 1307 | * "If surface is not bound to the calling thread’s current |
| 1308 | * context, an EGL_BAD_SURFACE error is generated." |
| 1309 | */ |
| 1310 | _EGLContext *ctx = _eglGetCurrentContext(); |
| 1311 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1312 | |
| 1313 | /* From the EGL 1.4 spec (page 52): |
| 1314 | * |
| 1315 | * "The contents of ancillary buffers are always undefined |
| 1316 | * after calling eglSwapBuffers." |
| 1317 | */ |
| 1318 | dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1319 | dri_drawable, |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 1320 | __DRI2_FLUSH_DRAWABLE | |
| 1321 | __DRI2_FLUSH_INVALIDATE_ANCILLARY, |
| 1322 | __DRI2_THROTTLE_SWAPBUFFER); |
| 1323 | } else { |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1324 | dri2_dpy->flush->flush(dri_drawable); |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 1329 | static EGLBoolean |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 1330 | dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) |
| 1331 | { |
| 1332 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1333 | return dri2_dpy->vtbl->swap_buffers(drv, dpy, surf); |
| 1334 | } |
| 1335 | |
| 1336 | static EGLBoolean |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 1337 | dri2_swap_buffers_with_damage(_EGLDriver *drv, _EGLDisplay *dpy, |
| 1338 | _EGLSurface *surf, |
| 1339 | const EGLint *rects, EGLint n_rects) |
| 1340 | { |
| 1341 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1342 | return dri2_dpy->vtbl->swap_buffers_with_damage(drv, dpy, surf, |
| 1343 | rects, n_rects); |
| 1344 | } |
| 1345 | |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1346 | static EGLBoolean |
Chad Versace | 75d398e | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1347 | dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, |
| 1348 | EGLint numRects, const EGLint *rects) |
| 1349 | { |
| 1350 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1351 | return dri2_dpy->vtbl->swap_buffers_region(drv, dpy, surf, numRects, rects); |
| 1352 | } |
| 1353 | |
| 1354 | static EGLBoolean |
Chad Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1355 | dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, |
| 1356 | EGLint x, EGLint y, EGLint width, EGLint height) |
| 1357 | { |
| 1358 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1359 | return dri2_dpy->vtbl->post_sub_buffer(drv, dpy, surf, x, y, width, height); |
| 1360 | } |
| 1361 | |
| 1362 | static EGLBoolean |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1363 | dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 1364 | void *native_pixmap_target) |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1365 | { |
| 1366 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 1367 | return dri2_dpy->vtbl->copy_buffers(drv, dpy, surf, native_pixmap_target); |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
Chad Versace | 3fdfbd2 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1370 | static EGLint |
| 1371 | dri2_query_buffer_age(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) |
| 1372 | { |
| 1373 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1374 | return dri2_dpy->vtbl->query_buffer_age(drv, dpy, surf); |
| 1375 | } |
| 1376 | |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 1377 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1378 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1379 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1380 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1381 | _EGLSurface *surf = ctx->DrawSurface; |
| 1382 | __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1383 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1384 | (void) drv; |
| 1385 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1386 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 1387 | * we need to copy fake to real here.*/ |
| 1388 | |
José Fonseca | 5048e69 | 2013-02-05 13:43:01 +0000 | [diff] [blame] | 1389 | if (dri2_dpy->flush != NULL) |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1390 | dri2_dpy->flush->flush(dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1391 | |
| 1392 | return EGL_TRUE; |
| 1393 | } |
| 1394 | |
| 1395 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1396 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1397 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1398 | (void) drv; |
| 1399 | (void) disp; |
| 1400 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1401 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 1402 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 1403 | /* glXWaitX(); */ |
| 1404 | |
| 1405 | return EGL_TRUE; |
| 1406 | } |
| 1407 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1408 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1409 | dri2_bind_tex_image(_EGLDriver *drv, |
| 1410 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1411 | { |
| 1412 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1413 | struct dri2_egl_context *dri2_ctx; |
| 1414 | _EGLContext *ctx; |
| 1415 | GLint format, target; |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1416 | __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1417 | |
| 1418 | ctx = _eglGetCurrentContext(); |
| 1419 | dri2_ctx = dri2_egl_context(ctx); |
| 1420 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1421 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1422 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1423 | |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1424 | switch (surf->TextureFormat) { |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1425 | case EGL_TEXTURE_RGB: |
| 1426 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 1427 | break; |
| 1428 | case EGL_TEXTURE_RGBA: |
| 1429 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 1430 | break; |
| 1431 | default: |
Brian Paul | 54070a9 | 2015-05-29 16:01:37 -0600 | [diff] [blame] | 1432 | assert(!"Unexpected texture format in dri2_bind_tex_image()"); |
| 1433 | format = __DRI_TEXTURE_FORMAT_RGBA; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1434 | } |
| 1435 | |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1436 | switch (surf->TextureTarget) { |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1437 | case EGL_TEXTURE_2D: |
| 1438 | target = GL_TEXTURE_2D; |
| 1439 | break; |
| 1440 | default: |
Brian Paul | 54070a9 | 2015-05-29 16:01:37 -0600 | [diff] [blame] | 1441 | target = GL_TEXTURE_2D; |
| 1442 | assert(!"Unexpected texture target in dri2_bind_tex_image()"); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 1446 | target, format, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1447 | dri_drawable); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1448 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1449 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | static EGLBoolean |
| 1453 | dri2_release_tex_image(_EGLDriver *drv, |
| 1454 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1455 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 1456 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 1457 | struct dri2_egl_context *dri2_ctx; |
| 1458 | _EGLContext *ctx; |
| 1459 | GLint target; |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1460 | __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf); |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 1461 | |
| 1462 | ctx = _eglGetCurrentContext(); |
| 1463 | dri2_ctx = dri2_egl_context(ctx); |
| 1464 | |
| 1465 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 1466 | return EGL_FALSE; |
| 1467 | |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1468 | switch (surf->TextureTarget) { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 1469 | case EGL_TEXTURE_2D: |
| 1470 | target = GL_TEXTURE_2D; |
| 1471 | break; |
| 1472 | default: |
| 1473 | assert(0); |
| 1474 | } |
Emil Velikov | 9b42fd1 | 2014-02-18 03:04:03 +0000 | [diff] [blame] | 1475 | |
| 1476 | if (dri2_dpy->tex_buffer->base.version >= 3 && |
| 1477 | dri2_dpy->tex_buffer->releaseTexBuffer != NULL) { |
| 1478 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 1479 | target, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1480 | dri_drawable); |
Emil Velikov | 9b42fd1 | 2014-02-18 03:04:03 +0000 | [diff] [blame] | 1481 | } |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1482 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1483 | return EGL_TRUE; |
| 1484 | } |
| 1485 | |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1486 | static _EGLImage* |
| 1487 | dri2_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx, |
| 1488 | EGLenum target, EGLClientBuffer buffer, |
| 1489 | const EGLint *attr_list) |
| 1490 | { |
| 1491 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1492 | return dri2_dpy->vtbl->create_image(drv, dpy, ctx, target, buffer, |
| 1493 | attr_list); |
| 1494 | } |
| 1495 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1496 | static _EGLImage * |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1497 | dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image) |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1498 | { |
| 1499 | struct dri2_egl_image *dri2_img; |
| 1500 | |
| 1501 | if (dri_image == NULL) { |
| 1502 | _eglError(EGL_BAD_ALLOC, "dri2_create_image"); |
| 1503 | return NULL; |
| 1504 | } |
| 1505 | |
| 1506 | dri2_img = malloc(sizeof *dri2_img); |
| 1507 | if (!dri2_img) { |
| 1508 | _eglError(EGL_BAD_ALLOC, "dri2_create_image"); |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | |
| 1512 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 1513 | free(dri2_img); |
| 1514 | return NULL; |
| 1515 | } |
| 1516 | |
| 1517 | dri2_img->dri_image = dri_image; |
| 1518 | |
| 1519 | return &dri2_img->base; |
| 1520 | } |
| 1521 | |
| 1522 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1523 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 1524 | EGLClientBuffer buffer, |
| 1525 | const EGLint *attr_list) |
| 1526 | { |
| 1527 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1528 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 1529 | GLuint renderbuffer = (GLuint) (uintptr_t) buffer; |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1530 | __DRIimage *dri_image; |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1531 | |
| 1532 | if (renderbuffer == 0) { |
| 1533 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1534 | return EGL_NO_IMAGE_KHR; |
| 1535 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1536 | |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1537 | dri_image = |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1538 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1539 | renderbuffer, NULL); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1540 | |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1541 | return dri2_create_image_from_dri(disp, dri_image); |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
José Fonseca | 5de2c67 | 2011-05-12 15:24:26 +0100 | [diff] [blame] | 1544 | #ifdef HAVE_WAYLAND_PLATFORM |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1545 | |
| 1546 | /* This structure describes how a wl_buffer maps to one or more |
| 1547 | * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the |
| 1548 | * offsets and strides of the planes in the buffer. This table maps a |
| 1549 | * wl_drm format code to a description of the planes in the buffer |
| 1550 | * that lets us create a __DRIimage for each of the planes. */ |
| 1551 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1552 | static const struct wl_drm_components_descriptor { |
| 1553 | uint32_t dri_components; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1554 | EGLint components; |
| 1555 | int nplanes; |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1556 | } wl_drm_components[] = { |
| 1557 | { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 }, |
| 1558 | { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 }, |
| 1559 | { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 }, |
| 1560 | { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 }, |
| 1561 | { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 }, |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1562 | }; |
| 1563 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1564 | static _EGLImage * |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1565 | dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1566 | EGLClientBuffer _buffer, |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1567 | const EGLint *attr_list) |
| 1568 | { |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 1569 | struct wl_drm_buffer *buffer; |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1570 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1571 | const struct wl_drm_components_descriptor *f; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1572 | __DRIimage *dri_image; |
| 1573 | _EGLImageAttribs attrs; |
| 1574 | EGLint err; |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1575 | int32_t plane; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1576 | |
Kristian Høgsberg | 360a141 | 2013-09-26 12:25:11 -0700 | [diff] [blame] | 1577 | buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, |
| 1578 | (struct wl_resource *) _buffer); |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 1579 | if (!buffer) |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1580 | return NULL; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1581 | |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1582 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1583 | plane = attrs.PlaneWL; |
| 1584 | if (err != EGL_SUCCESS) { |
| 1585 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer"); |
| 1586 | return NULL; |
| 1587 | } |
| 1588 | |
| 1589 | f = buffer->driver_format; |
| 1590 | if (plane < 0 || plane >= f->nplanes) { |
| 1591 | _eglError(EGL_BAD_PARAMETER, |
| 1592 | "dri2_create_image_wayland_wl_buffer (plane out of bounds)"); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1596 | dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL); |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1597 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1598 | if (dri_image == NULL) { |
| 1599 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer"); |
| 1600 | return NULL; |
| 1601 | } |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1602 | |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1603 | return dri2_create_image_from_dri(disp, dri_image); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1604 | } |
| 1605 | #endif |
| 1606 | |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame] | 1607 | static EGLBoolean |
| 1608 | dri2_get_sync_values_chromium(_EGLDisplay *dpy, _EGLSurface *surf, |
| 1609 | EGLuint64KHR *ust, EGLuint64KHR *msc, |
| 1610 | EGLuint64KHR *sbc) |
| 1611 | { |
| 1612 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1613 | return dri2_dpy->vtbl->get_sync_values(dpy, surf, ust, msc, sbc); |
| 1614 | } |
| 1615 | |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 1616 | /** |
| 1617 | * Set the error code after a call to |
| 1618 | * dri2_egl_image::dri_image::createImageFromTexture. |
| 1619 | */ |
| 1620 | static void |
| 1621 | dri2_create_image_khr_texture_error(int dri_error) |
| 1622 | { |
| 1623 | EGLint egl_error; |
| 1624 | |
| 1625 | switch (dri_error) { |
| 1626 | case __DRI_IMAGE_ERROR_SUCCESS: |
| 1627 | return; |
| 1628 | |
| 1629 | case __DRI_IMAGE_ERROR_BAD_ALLOC: |
| 1630 | egl_error = EGL_BAD_ALLOC; |
| 1631 | break; |
| 1632 | |
| 1633 | case __DRI_IMAGE_ERROR_BAD_MATCH: |
| 1634 | egl_error = EGL_BAD_MATCH; |
| 1635 | break; |
| 1636 | |
| 1637 | case __DRI_IMAGE_ERROR_BAD_PARAMETER: |
| 1638 | egl_error = EGL_BAD_PARAMETER; |
| 1639 | break; |
| 1640 | |
Frank Binns | cfc3200 | 2015-07-31 09:11:46 +0100 | [diff] [blame] | 1641 | case __DRI_IMAGE_ERROR_BAD_ACCESS: |
| 1642 | egl_error = EGL_BAD_ACCESS; |
| 1643 | break; |
| 1644 | |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 1645 | default: |
| 1646 | assert(0); |
| 1647 | egl_error = EGL_BAD_MATCH; |
| 1648 | break; |
| 1649 | } |
| 1650 | |
| 1651 | _eglError(egl_error, "dri2_create_image_khr_texture"); |
| 1652 | } |
| 1653 | |
| 1654 | static _EGLImage * |
| 1655 | dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx, |
| 1656 | EGLenum target, |
| 1657 | EGLClientBuffer buffer, |
| 1658 | const EGLint *attr_list) |
| 1659 | { |
| 1660 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1661 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1662 | struct dri2_egl_image *dri2_img; |
| 1663 | GLuint texture = (GLuint) (uintptr_t) buffer; |
| 1664 | _EGLImageAttribs attrs; |
| 1665 | GLuint depth; |
| 1666 | GLenum gl_target; |
| 1667 | unsigned error; |
| 1668 | |
| 1669 | if (texture == 0) { |
| 1670 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1671 | return EGL_NO_IMAGE_KHR; |
| 1672 | } |
| 1673 | |
| 1674 | if (_eglParseImageAttribList(&attrs, disp, attr_list) != EGL_SUCCESS) |
| 1675 | return EGL_NO_IMAGE_KHR; |
| 1676 | |
| 1677 | switch (target) { |
| 1678 | case EGL_GL_TEXTURE_2D_KHR: |
| 1679 | depth = 0; |
| 1680 | gl_target = GL_TEXTURE_2D; |
| 1681 | break; |
| 1682 | case EGL_GL_TEXTURE_3D_KHR: |
Anatoli Antonovitch | b193f2b | 2015-06-10 14:42:31 +0200 | [diff] [blame] | 1683 | if (disp->Extensions.KHR_gl_texture_3D_image) { |
| 1684 | depth = attrs.GLTextureZOffset; |
| 1685 | gl_target = GL_TEXTURE_3D; |
| 1686 | break; |
| 1687 | } |
| 1688 | else { |
| 1689 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1690 | return EGL_NO_IMAGE_KHR; |
| 1691 | } |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 1692 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR: |
| 1693 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR: |
| 1694 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR: |
| 1695 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR: |
| 1696 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR: |
| 1697 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR: |
| 1698 | depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR; |
| 1699 | gl_target = GL_TEXTURE_CUBE_MAP; |
| 1700 | break; |
| 1701 | default: |
| 1702 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1703 | return EGL_NO_IMAGE_KHR; |
| 1704 | } |
| 1705 | |
| 1706 | dri2_img = malloc(sizeof *dri2_img); |
| 1707 | if (!dri2_img) { |
| 1708 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1709 | return EGL_NO_IMAGE_KHR; |
| 1710 | } |
| 1711 | |
| 1712 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 1713 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1714 | free(dri2_img); |
| 1715 | return EGL_NO_IMAGE_KHR; |
| 1716 | } |
| 1717 | |
| 1718 | dri2_img->dri_image = |
| 1719 | dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context, |
| 1720 | gl_target, |
| 1721 | texture, |
| 1722 | depth, |
| 1723 | attrs.GLTextureLevel, |
| 1724 | &error, |
| 1725 | dri2_img); |
| 1726 | dri2_create_image_khr_texture_error(error); |
| 1727 | |
| 1728 | if (!dri2_img->dri_image) { |
| 1729 | free(dri2_img); |
| 1730 | return EGL_NO_IMAGE_KHR; |
| 1731 | } |
| 1732 | return &dri2_img->base; |
| 1733 | } |
| 1734 | |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1735 | static struct wl_buffer* |
| 1736 | dri2_create_wayland_buffer_from_image(_EGLDriver *drv, _EGLDisplay *dpy, |
| 1737 | _EGLImage *img) |
| 1738 | { |
| 1739 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 1740 | return dri2_dpy->vtbl->create_wayland_buffer_from_image(drv, dpy, img); |
| 1741 | } |
| 1742 | |
Axel Davy | 63986f9 | 2015-01-14 20:36:04 +0100 | [diff] [blame] | 1743 | #ifdef HAVE_LIBDRM |
Emil Velikov | 57c670a | 2015-07-10 11:01:35 +0100 | [diff] [blame] | 1744 | static _EGLImage * |
| 1745 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 1746 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1747 | { |
| 1748 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1749 | EGLint format, name, pitch, err; |
| 1750 | _EGLImageAttribs attrs; |
| 1751 | __DRIimage *dri_image; |
| 1752 | |
| 1753 | name = (EGLint) (uintptr_t) buffer; |
| 1754 | |
| 1755 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1756 | if (err != EGL_SUCCESS) |
| 1757 | return NULL; |
| 1758 | |
| 1759 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 1760 | attrs.DRMBufferStrideMESA <= 0) { |
| 1761 | _eglError(EGL_BAD_PARAMETER, |
| 1762 | "bad width, height or stride"); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | |
| 1766 | switch (attrs.DRMBufferFormatMESA) { |
| 1767 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 1768 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 1769 | pitch = attrs.DRMBufferStrideMESA; |
| 1770 | break; |
| 1771 | default: |
| 1772 | _eglError(EGL_BAD_PARAMETER, |
| 1773 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 1774 | return NULL; |
| 1775 | } |
| 1776 | |
| 1777 | dri_image = |
| 1778 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 1779 | attrs.Width, |
| 1780 | attrs.Height, |
| 1781 | format, |
| 1782 | name, |
| 1783 | pitch, |
| 1784 | NULL); |
| 1785 | |
| 1786 | return dri2_create_image_from_dri(disp, dri_image); |
| 1787 | } |
| 1788 | |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 1789 | static EGLBoolean |
| 1790 | dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs) |
| 1791 | { |
| 1792 | unsigned i; |
| 1793 | |
| 1794 | /** |
| 1795 | * The spec says: |
| 1796 | * |
| 1797 | * "Required attributes and their values are as follows: |
| 1798 | * |
| 1799 | * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels |
| 1800 | * |
| 1801 | * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified |
| 1802 | * by drm_fourcc.h and used as the pixel_format parameter of the |
| 1803 | * drm_mode_fb_cmd2 ioctl." |
| 1804 | * |
| 1805 | * and |
| 1806 | * |
| 1807 | * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is |
| 1808 | * incomplete, EGL_BAD_PARAMETER is generated." |
| 1809 | */ |
| 1810 | if (attrs->Width <= 0 || attrs->Height <= 0 || |
| 1811 | !attrs->DMABufFourCC.IsPresent) { |
| 1812 | _eglError(EGL_BAD_PARAMETER, "attribute(s) missing"); |
| 1813 | return EGL_FALSE; |
| 1814 | } |
| 1815 | |
| 1816 | /** |
| 1817 | * Also: |
| 1818 | * |
| 1819 | * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values |
| 1820 | * specified for a plane's pitch or offset isn't supported by EGL, |
| 1821 | * EGL_BAD_ACCESS is generated." |
| 1822 | */ |
| 1823 | for (i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) { |
| 1824 | if (attrs->DMABufPlanePitches[i].IsPresent && |
| 1825 | attrs->DMABufPlanePitches[i].Value <= 0) { |
| 1826 | _eglError(EGL_BAD_ACCESS, "invalid pitch"); |
| 1827 | return EGL_FALSE; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | return EGL_TRUE; |
| 1832 | } |
| 1833 | |
| 1834 | /* Returns the total number of file descriptors. Zero indicates an error. */ |
| 1835 | static unsigned |
| 1836 | dri2_check_dma_buf_format(const _EGLImageAttribs *attrs) |
| 1837 | { |
| 1838 | unsigned i, plane_n; |
| 1839 | |
| 1840 | switch (attrs->DMABufFourCC.Value) { |
Chad Versace | fd865d5 | 2015-06-23 15:48:17 -0700 | [diff] [blame] | 1841 | case DRM_FORMAT_R8: |
| 1842 | case DRM_FORMAT_RG88: |
| 1843 | case DRM_FORMAT_GR88: |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 1844 | case DRM_FORMAT_RGB332: |
| 1845 | case DRM_FORMAT_BGR233: |
| 1846 | case DRM_FORMAT_XRGB4444: |
| 1847 | case DRM_FORMAT_XBGR4444: |
| 1848 | case DRM_FORMAT_RGBX4444: |
| 1849 | case DRM_FORMAT_BGRX4444: |
| 1850 | case DRM_FORMAT_ARGB4444: |
| 1851 | case DRM_FORMAT_ABGR4444: |
| 1852 | case DRM_FORMAT_RGBA4444: |
| 1853 | case DRM_FORMAT_BGRA4444: |
| 1854 | case DRM_FORMAT_XRGB1555: |
| 1855 | case DRM_FORMAT_XBGR1555: |
| 1856 | case DRM_FORMAT_RGBX5551: |
| 1857 | case DRM_FORMAT_BGRX5551: |
| 1858 | case DRM_FORMAT_ARGB1555: |
| 1859 | case DRM_FORMAT_ABGR1555: |
| 1860 | case DRM_FORMAT_RGBA5551: |
| 1861 | case DRM_FORMAT_BGRA5551: |
| 1862 | case DRM_FORMAT_RGB565: |
| 1863 | case DRM_FORMAT_BGR565: |
| 1864 | case DRM_FORMAT_RGB888: |
| 1865 | case DRM_FORMAT_BGR888: |
| 1866 | case DRM_FORMAT_XRGB8888: |
| 1867 | case DRM_FORMAT_XBGR8888: |
| 1868 | case DRM_FORMAT_RGBX8888: |
| 1869 | case DRM_FORMAT_BGRX8888: |
| 1870 | case DRM_FORMAT_ARGB8888: |
| 1871 | case DRM_FORMAT_ABGR8888: |
| 1872 | case DRM_FORMAT_RGBA8888: |
| 1873 | case DRM_FORMAT_BGRA8888: |
| 1874 | case DRM_FORMAT_XRGB2101010: |
| 1875 | case DRM_FORMAT_XBGR2101010: |
| 1876 | case DRM_FORMAT_RGBX1010102: |
| 1877 | case DRM_FORMAT_BGRX1010102: |
| 1878 | case DRM_FORMAT_ARGB2101010: |
| 1879 | case DRM_FORMAT_ABGR2101010: |
| 1880 | case DRM_FORMAT_RGBA1010102: |
| 1881 | case DRM_FORMAT_BGRA1010102: |
| 1882 | case DRM_FORMAT_YUYV: |
| 1883 | case DRM_FORMAT_YVYU: |
| 1884 | case DRM_FORMAT_UYVY: |
| 1885 | case DRM_FORMAT_VYUY: |
| 1886 | plane_n = 1; |
| 1887 | break; |
| 1888 | case DRM_FORMAT_NV12: |
| 1889 | case DRM_FORMAT_NV21: |
| 1890 | case DRM_FORMAT_NV16: |
| 1891 | case DRM_FORMAT_NV61: |
| 1892 | plane_n = 2; |
| 1893 | break; |
| 1894 | case DRM_FORMAT_YUV410: |
| 1895 | case DRM_FORMAT_YVU410: |
| 1896 | case DRM_FORMAT_YUV411: |
| 1897 | case DRM_FORMAT_YVU411: |
| 1898 | case DRM_FORMAT_YUV420: |
| 1899 | case DRM_FORMAT_YVU420: |
| 1900 | case DRM_FORMAT_YUV422: |
| 1901 | case DRM_FORMAT_YVU422: |
| 1902 | case DRM_FORMAT_YUV444: |
| 1903 | case DRM_FORMAT_YVU444: |
| 1904 | plane_n = 3; |
| 1905 | break; |
| 1906 | default: |
| 1907 | _eglError(EGL_BAD_ATTRIBUTE, "invalid format"); |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
| 1911 | /** |
| 1912 | * The spec says: |
| 1913 | * |
| 1914 | * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is |
| 1915 | * incomplete, EGL_BAD_PARAMETER is generated." |
| 1916 | */ |
| 1917 | for (i = 0; i < plane_n; ++i) { |
| 1918 | if (!attrs->DMABufPlaneFds[i].IsPresent || |
| 1919 | !attrs->DMABufPlaneOffsets[i].IsPresent || |
| 1920 | !attrs->DMABufPlanePitches[i].IsPresent) { |
| 1921 | _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing"); |
| 1922 | return 0; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | /** |
| 1927 | * The spec also says: |
| 1928 | * |
| 1929 | * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT |
| 1930 | * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is |
| 1931 | * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_* |
| 1932 | * attributes are specified." |
| 1933 | */ |
| 1934 | for (i = plane_n; i < 3; ++i) { |
| 1935 | if (attrs->DMABufPlaneFds[i].IsPresent || |
| 1936 | attrs->DMABufPlaneOffsets[i].IsPresent || |
| 1937 | attrs->DMABufPlanePitches[i].IsPresent) { |
| 1938 | _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes"); |
| 1939 | return 0; |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | return plane_n; |
| 1944 | } |
| 1945 | |
| 1946 | /** |
| 1947 | * The spec says: |
| 1948 | * |
Pekka Paalanen | 08264e5 | 2014-08-08 16:26:28 +0300 | [diff] [blame] | 1949 | * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the |
| 1950 | * EGL will take a reference to the dma_buf(s) which it will release at any |
| 1951 | * time while the EGLDisplay is initialized. It is the responsibility of the |
| 1952 | * application to close the dma_buf file descriptors." |
| 1953 | * |
| 1954 | * Therefore we must never close or otherwise modify the file descriptors. |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 1955 | */ |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 1956 | static _EGLImage * |
| 1957 | dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx, |
| 1958 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1959 | { |
| 1960 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1961 | _EGLImage *res; |
| 1962 | EGLint err; |
| 1963 | _EGLImageAttribs attrs; |
| 1964 | __DRIimage *dri_image; |
| 1965 | unsigned num_fds; |
| 1966 | unsigned i; |
| 1967 | int fds[3]; |
| 1968 | int pitches[3]; |
| 1969 | int offsets[3]; |
| 1970 | unsigned error; |
| 1971 | |
| 1972 | /** |
| 1973 | * The spec says: |
| 1974 | * |
| 1975 | * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the |
| 1976 | * error EGL_BAD_PARAMETER is generated." |
| 1977 | */ |
| 1978 | if (buffer != NULL) { |
| 1979 | _eglError(EGL_BAD_PARAMETER, "buffer not NULL"); |
| 1980 | return NULL; |
| 1981 | } |
| 1982 | |
| 1983 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1984 | if (err != EGL_SUCCESS) { |
| 1985 | _eglError(err, "bad attribute"); |
| 1986 | return NULL; |
| 1987 | } |
| 1988 | |
| 1989 | if (!dri2_check_dma_buf_attribs(&attrs)) |
| 1990 | return NULL; |
| 1991 | |
| 1992 | num_fds = dri2_check_dma_buf_format(&attrs); |
| 1993 | if (!num_fds) |
| 1994 | return NULL; |
| 1995 | |
| 1996 | for (i = 0; i < num_fds; ++i) { |
| 1997 | fds[i] = attrs.DMABufPlaneFds[i].Value; |
| 1998 | pitches[i] = attrs.DMABufPlanePitches[i].Value; |
| 1999 | offsets[i] = attrs.DMABufPlaneOffsets[i].Value; |
| 2000 | } |
| 2001 | |
| 2002 | dri_image = |
| 2003 | dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen, |
| 2004 | attrs.Width, attrs.Height, attrs.DMABufFourCC.Value, |
| 2005 | fds, num_fds, pitches, offsets, |
| 2006 | attrs.DMABufYuvColorSpaceHint.Value, |
| 2007 | attrs.DMABufSampleRangeHint.Value, |
| 2008 | attrs.DMABufChromaHorizontalSiting.Value, |
| 2009 | attrs.DMABufChromaVerticalSiting.Value, |
| 2010 | &error, |
| 2011 | NULL); |
| 2012 | dri2_create_image_khr_texture_error(error); |
| 2013 | |
| 2014 | if (!dri_image) |
| 2015 | return EGL_NO_IMAGE_KHR; |
| 2016 | |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2017 | res = dri2_create_image_from_dri(disp, dri_image); |
Topi Pohjolainen | 20de7f9 | 2013-03-22 15:58:05 +0200 | [diff] [blame] | 2018 | |
| 2019 | return res; |
| 2020 | } |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2021 | static _EGLImage * |
| 2022 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 2023 | const EGLint *attr_list) |
| 2024 | { |
| 2025 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2026 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2027 | _EGLImageAttribs attrs; |
| 2028 | unsigned int dri_use, valid_mask; |
| 2029 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2030 | EGLint err = EGL_SUCCESS; |
| 2031 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 2032 | (void) drv; |
| 2033 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2034 | dri2_img = malloc(sizeof *dri2_img); |
| 2035 | if (!dri2_img) { |
| 2036 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 2037 | return EGL_NO_IMAGE_KHR; |
| 2038 | } |
| 2039 | |
| 2040 | if (!attr_list) { |
| 2041 | err = EGL_BAD_PARAMETER; |
| 2042 | goto cleanup_img; |
| 2043 | } |
| 2044 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 2045 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2046 | err = EGL_BAD_PARAMETER; |
| 2047 | goto cleanup_img; |
| 2048 | } |
| 2049 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2050 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 2051 | if (err != EGL_SUCCESS) |
| 2052 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2053 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2054 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 2055 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 2056 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2057 | goto cleanup_img; |
| 2058 | } |
| 2059 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2060 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2061 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 2062 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 2063 | break; |
| 2064 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2065 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 2066 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2067 | goto cleanup_img; |
| 2068 | } |
| 2069 | |
| 2070 | valid_mask = |
| 2071 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 2072 | EGL_DRM_BUFFER_USE_SHARE_MESA | |
| 2073 | EGL_DRM_BUFFER_USE_CURSOR_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2074 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 2075 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 2076 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2077 | goto cleanup_img; |
| 2078 | } |
| 2079 | |
| 2080 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2081 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2082 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2083 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2084 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 2085 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) |
| 2086 | dri_use |= __DRI_IMAGE_USE_CURSOR; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2087 | |
Boyan Ding | 052b3d4 | 2015-06-13 15:36:27 +0800 | [diff] [blame] | 2088 | dri2_img->dri_image = |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2089 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 2090 | attrs.Width, attrs.Height, |
| 2091 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2092 | if (dri2_img->dri_image == NULL) { |
| 2093 | err = EGL_BAD_ALLOC; |
| 2094 | goto cleanup_img; |
| 2095 | } |
| 2096 | |
| 2097 | return &dri2_img->base; |
| 2098 | |
| 2099 | cleanup_img: |
| 2100 | free(dri2_img); |
| 2101 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 2102 | |
| 2103 | return EGL_NO_IMAGE_KHR; |
| 2104 | } |
| 2105 | |
| 2106 | static EGLBoolean |
| 2107 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 2108 | EGLint *name, EGLint *handle, EGLint *stride) |
| 2109 | { |
| 2110 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2111 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 2112 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 2113 | (void) drv; |
| 2114 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2115 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2116 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 2117 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 2118 | return EGL_FALSE; |
| 2119 | } |
| 2120 | |
| 2121 | if (handle) |
| 2122 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2123 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 2124 | |
| 2125 | if (stride) |
| 2126 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2127 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 2128 | |
| 2129 | return EGL_TRUE; |
| 2130 | } |
Dave Airlie | 8f7338f | 2014-03-03 13:57:16 +1000 | [diff] [blame] | 2131 | |
| 2132 | static EGLBoolean |
| 2133 | dri2_export_dma_buf_image_query_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 2134 | _EGLImage *img, |
| 2135 | EGLint *fourcc, EGLint *nplanes, |
Dave Airlie | b5045e2 | 2015-05-05 09:10:34 +1000 | [diff] [blame] | 2136 | EGLuint64KHR *modifiers) |
Dave Airlie | 8f7338f | 2014-03-03 13:57:16 +1000 | [diff] [blame] | 2137 | { |
| 2138 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2139 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 2140 | |
| 2141 | (void) drv; |
| 2142 | |
| 2143 | |
| 2144 | if (nplanes) |
| 2145 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2146 | __DRI_IMAGE_ATTRIB_NUM_PLANES, nplanes); |
| 2147 | if (fourcc) |
| 2148 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2149 | __DRI_IMAGE_ATTRIB_FOURCC, fourcc); |
| 2150 | |
| 2151 | if (modifiers) |
| 2152 | *modifiers = 0; |
| 2153 | |
| 2154 | return EGL_TRUE; |
| 2155 | } |
| 2156 | |
| 2157 | static EGLBoolean |
| 2158 | dri2_export_dma_buf_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 2159 | int *fds, EGLint *strides, EGLint *offsets) |
| 2160 | { |
| 2161 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2162 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 2163 | |
| 2164 | (void) drv; |
| 2165 | |
| 2166 | /* rework later to provide multiple fds/strides/offsets */ |
| 2167 | if (fds) |
| 2168 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2169 | __DRI_IMAGE_ATTRIB_FD, fds); |
| 2170 | |
| 2171 | if (strides) |
| 2172 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 2173 | __DRI_IMAGE_ATTRIB_STRIDE, strides); |
| 2174 | |
| 2175 | if (offsets) |
| 2176 | offsets[0] = 0; |
| 2177 | |
| 2178 | return EGL_TRUE; |
| 2179 | } |
Emil Velikov | 57c670a | 2015-07-10 11:01:35 +0100 | [diff] [blame] | 2180 | |
Samuel Thibault | a594cec | 2013-11-10 19:32:01 +0100 | [diff] [blame] | 2181 | #endif |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2182 | |
Emil Velikov | 57c670a | 2015-07-10 11:01:35 +0100 | [diff] [blame] | 2183 | _EGLImage * |
| 2184 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 2185 | _EGLContext *ctx, EGLenum target, |
| 2186 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 2187 | { |
| 2188 | (void) drv; |
| 2189 | |
| 2190 | switch (target) { |
| 2191 | case EGL_GL_TEXTURE_2D_KHR: |
| 2192 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR: |
| 2193 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR: |
| 2194 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR: |
| 2195 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR: |
| 2196 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR: |
| 2197 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR: |
| 2198 | return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list); |
| 2199 | case EGL_GL_TEXTURE_3D_KHR: |
| 2200 | if (disp->Extensions.KHR_gl_texture_3D_image) { |
| 2201 | return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list); |
| 2202 | } |
| 2203 | else { |
| 2204 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 2205 | return EGL_NO_IMAGE_KHR; |
| 2206 | } |
| 2207 | case EGL_GL_RENDERBUFFER_KHR: |
| 2208 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
| 2209 | #ifdef HAVE_LIBDRM |
| 2210 | case EGL_DRM_BUFFER_MESA: |
| 2211 | return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list); |
| 2212 | case EGL_LINUX_DMA_BUF_EXT: |
| 2213 | return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list); |
| 2214 | #endif |
| 2215 | #ifdef HAVE_WAYLAND_PLATFORM |
| 2216 | case EGL_WAYLAND_BUFFER_WL: |
| 2217 | return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list); |
| 2218 | #endif |
| 2219 | default: |
| 2220 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 2221 | return EGL_NO_IMAGE_KHR; |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | static EGLBoolean |
| 2226 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 2227 | { |
| 2228 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2229 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 2230 | |
| 2231 | (void) drv; |
| 2232 | |
| 2233 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 2234 | free(dri2_img); |
| 2235 | |
| 2236 | return EGL_TRUE; |
| 2237 | } |
| 2238 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2239 | #ifdef HAVE_WAYLAND_PLATFORM |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2240 | |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 2241 | static void |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2242 | 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] | 2243 | struct wl_drm_buffer *buffer) |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2244 | { |
| 2245 | _EGLDisplay *disp = user_data; |
| 2246 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 2247 | __DRIimage *img; |
| 2248 | int i, dri_components = 0; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 2249 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2250 | if (fd == -1) |
| 2251 | img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen, |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 2252 | buffer->width, |
| 2253 | buffer->height, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2254 | buffer->format, |
| 2255 | (int*)&name, 1, |
| 2256 | buffer->stride, |
| 2257 | buffer->offset, |
| 2258 | NULL); |
| 2259 | else |
| 2260 | img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen, |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 2261 | buffer->width, |
| 2262 | buffer->height, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2263 | buffer->format, |
| 2264 | &fd, 1, |
| 2265 | buffer->stride, |
| 2266 | buffer->offset, |
| 2267 | NULL); |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2268 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 2269 | if (img == NULL) |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 2270 | return; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2271 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 2272 | dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components); |
| 2273 | |
| 2274 | buffer->driver_format = NULL; |
| 2275 | for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++) |
| 2276 | if (wl_drm_components[i].dri_components == dri_components) |
| 2277 | buffer->driver_format = &wl_drm_components[i]; |
| 2278 | |
| 2279 | if (buffer->driver_format == NULL) |
| 2280 | dri2_dpy->image->destroyImage(img); |
| 2281 | else |
| 2282 | buffer->driver_buffer = img; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | static void |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 2286 | dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer) |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2287 | { |
| 2288 | _EGLDisplay *disp = user_data; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2289 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2290 | |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 2291 | dri2_dpy->image->destroyImage(buffer->driver_buffer); |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | static struct wayland_drm_callbacks wl_drm_callbacks = { |
| 2295 | .authenticate = NULL, |
| 2296 | .reference_buffer = dri2_wl_reference_buffer, |
| 2297 | .release_buffer = dri2_wl_release_buffer |
| 2298 | }; |
| 2299 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2300 | static EGLBoolean |
| 2301 | dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 2302 | struct wl_display *wl_dpy) |
| 2303 | { |
| 2304 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Emil Velikov | 6638c55 | 2014-05-28 14:36:46 +0100 | [diff] [blame] | 2305 | int flags = 0; |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2306 | uint64_t cap; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2307 | |
| 2308 | (void) drv; |
| 2309 | |
| 2310 | if (dri2_dpy->wl_server_drm) |
| 2311 | return EGL_FALSE; |
| 2312 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2313 | wl_drm_callbacks.authenticate = |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 2314 | (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2315 | |
Emil Velikov | 6638c55 | 2014-05-28 14:36:46 +0100 | [diff] [blame] | 2316 | if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 && |
| 2317 | cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) && |
Kristian Høgsberg | 1670737 | 2013-03-19 13:20:36 -0400 | [diff] [blame] | 2318 | dri2_dpy->image->base.version >= 7 && |
| 2319 | dri2_dpy->image->createImageFromFds != NULL) |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2320 | flags |= WAYLAND_DRM_PRIME; |
| 2321 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2322 | dri2_dpy->wl_server_drm = |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2323 | wayland_drm_init(wl_dpy, dri2_dpy->device_name, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 2324 | &wl_drm_callbacks, disp, flags); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2325 | |
| 2326 | if (!dri2_dpy->wl_server_drm) |
| 2327 | return EGL_FALSE; |
| 2328 | |
Ander Conselvan de Oliveira | 1786030 | 2014-04-08 23:28:42 +0300 | [diff] [blame] | 2329 | #ifdef HAVE_DRM_PLATFORM |
Kristian Høgsberg | 360a141 | 2013-09-26 12:25:11 -0700 | [diff] [blame] | 2330 | /* We have to share the wl_drm instance with gbm, so gbm can convert |
| 2331 | * wl_buffers to gbm bos. */ |
Kristian Høgsberg | 1d34927 | 2013-10-11 15:30:09 -0700 | [diff] [blame] | 2332 | if (dri2_dpy->gbm_dri) |
| 2333 | 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] | 2334 | #endif |
Kristian Høgsberg | 360a141 | 2013-09-26 12:25:11 -0700 | [diff] [blame] | 2335 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2336 | return EGL_TRUE; |
| 2337 | } |
| 2338 | |
| 2339 | static EGLBoolean |
| 2340 | dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 2341 | struct wl_display *wl_dpy) |
| 2342 | { |
| 2343 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 2344 | |
| 2345 | (void) drv; |
| 2346 | |
| 2347 | if (!dri2_dpy->wl_server_drm) |
| 2348 | return EGL_FALSE; |
| 2349 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 2350 | wayland_drm_uninit(dri2_dpy->wl_server_drm); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2351 | dri2_dpy->wl_server_drm = NULL; |
| 2352 | |
| 2353 | return EGL_TRUE; |
| 2354 | } |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2355 | |
| 2356 | static EGLBoolean |
| 2357 | dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp, |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 2358 | struct wl_resource *buffer_resource, |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2359 | EGLint attribute, EGLint *value) |
| 2360 | { |
Kristian Høgsberg | 360a141 | 2013-09-26 12:25:11 -0700 | [diff] [blame] | 2361 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 2362 | struct wl_drm_buffer *buffer; |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 2363 | const struct wl_drm_components_descriptor *format; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2364 | |
Kristian Høgsberg | 360a141 | 2013-09-26 12:25:11 -0700 | [diff] [blame] | 2365 | 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] | 2366 | if (!buffer) |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2367 | return EGL_FALSE; |
| 2368 | |
| 2369 | format = buffer->driver_format; |
Kristian Høgsberg | d7522ed | 2012-07-19 09:02:25 -0400 | [diff] [blame] | 2370 | switch (attribute) { |
| 2371 | case EGL_TEXTURE_FORMAT: |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2372 | *value = format->components; |
| 2373 | return EGL_TRUE; |
Kristian Høgsberg | d7522ed | 2012-07-19 09:02:25 -0400 | [diff] [blame] | 2374 | case EGL_WIDTH: |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 2375 | *value = buffer->width; |
Rob Bradford | 15e64de | 2013-04-10 11:36:24 +0100 | [diff] [blame] | 2376 | return EGL_TRUE; |
Kristian Høgsberg | d7522ed | 2012-07-19 09:02:25 -0400 | [diff] [blame] | 2377 | case EGL_HEIGHT: |
Ander Conselvan de Oliveira | 8d29b52 | 2013-07-18 15:11:25 +0300 | [diff] [blame] | 2378 | *value = buffer->height; |
Rob Bradford | 15e64de | 2013-04-10 11:36:24 +0100 | [diff] [blame] | 2379 | return EGL_TRUE; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | return EGL_FALSE; |
| 2383 | } |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2384 | #endif |
| 2385 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2386 | static void |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2387 | dri2_egl_ref_sync(struct dri2_egl_sync *sync) |
| 2388 | { |
| 2389 | p_atomic_inc(&sync->refcount); |
| 2390 | } |
| 2391 | |
| 2392 | static void |
| 2393 | dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy, |
| 2394 | struct dri2_egl_sync *dri2_sync) |
| 2395 | { |
| 2396 | if (p_atomic_dec_zero(&dri2_sync->refcount)) { |
| 2397 | dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence); |
| 2398 | free(dri2_sync); |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | static _EGLSync * |
| 2403 | dri2_create_sync(_EGLDriver *drv, _EGLDisplay *dpy, |
Marek Olšák | 290a3eb | 2015-04-10 13:16:30 +0200 | [diff] [blame] | 2404 | EGLenum type, const EGLint *attrib_list, |
Marek Olšák | d333d30 | 2015-05-12 17:34:57 +0200 | [diff] [blame] | 2405 | const EGLAttrib *attrib_list64) |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2406 | { |
| 2407 | _EGLContext *ctx = _eglGetCurrentContext(); |
| 2408 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 2409 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 2410 | struct dri2_egl_sync *dri2_sync; |
| 2411 | |
| 2412 | dri2_sync = calloc(1, sizeof(struct dri2_egl_sync)); |
| 2413 | if (!dri2_sync) { |
| 2414 | _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR"); |
| 2415 | return NULL; |
| 2416 | } |
| 2417 | |
Marek Olšák | 290a3eb | 2015-04-10 13:16:30 +0200 | [diff] [blame] | 2418 | if (!_eglInitSync(&dri2_sync->base, dpy, type, attrib_list, |
| 2419 | attrib_list64)) { |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2420 | free(dri2_sync); |
| 2421 | return NULL; |
| 2422 | } |
| 2423 | |
| 2424 | switch (type) { |
| 2425 | case EGL_SYNC_FENCE_KHR: |
| 2426 | dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context); |
Chad Versace | b0f410a | 2015-05-05 19:05:20 -0700 | [diff] [blame] | 2427 | if (!dri2_sync->fence) { |
| 2428 | /* 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] | 2429 | * a generic EGL error that doesn't communicate user error. |
| 2430 | */ |
Chad Versace | b0f410a | 2015-05-05 19:05:20 -0700 | [diff] [blame] | 2431 | _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR"); |
| 2432 | free(dri2_sync); |
| 2433 | return NULL; |
| 2434 | } |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2435 | break; |
Marek Olšák | 290a3eb | 2015-04-10 13:16:30 +0200 | [diff] [blame] | 2436 | |
| 2437 | case EGL_SYNC_CL_EVENT_KHR: |
| 2438 | dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event( |
| 2439 | dri2_dpy->dri_screen, |
| 2440 | dri2_sync->base.CLEvent); |
| 2441 | /* this can only happen if the cl_event passed in is invalid. */ |
| 2442 | if (!dri2_sync->fence) { |
| 2443 | _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR"); |
| 2444 | free(dri2_sync); |
| 2445 | return NULL; |
| 2446 | } |
| 2447 | |
| 2448 | /* the initial status must be "signaled" if the cl_event is signaled */ |
| 2449 | if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context, |
| 2450 | dri2_sync->fence, 0, 0)) |
| 2451 | dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR; |
| 2452 | break; |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | p_atomic_set(&dri2_sync->refcount, 1); |
| 2456 | return &dri2_sync->base; |
| 2457 | } |
| 2458 | |
| 2459 | static EGLBoolean |
| 2460 | dri2_destroy_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync) |
| 2461 | { |
| 2462 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 2463 | struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync); |
| 2464 | |
| 2465 | dri2_egl_unref_sync(dri2_dpy, dri2_sync); |
| 2466 | return EGL_TRUE; |
| 2467 | } |
| 2468 | |
| 2469 | static EGLint |
| 2470 | dri2_client_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync, |
Marek Olšák | d333d30 | 2015-05-12 17:34:57 +0200 | [diff] [blame] | 2471 | EGLint flags, EGLTime timeout) |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2472 | { |
| 2473 | _EGLContext *ctx = _eglGetCurrentContext(); |
| 2474 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 2475 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 2476 | struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync); |
| 2477 | unsigned wait_flags = 0; |
| 2478 | EGLint ret = EGL_CONDITION_SATISFIED_KHR; |
| 2479 | |
Marek Olšák | 18123a7 | 2015-09-25 22:48:00 +0200 | [diff] [blame] | 2480 | /* The EGL_KHR_fence_sync spec states: |
| 2481 | * |
| 2482 | * "If no context is current for the bound API, |
| 2483 | * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored. |
| 2484 | */ |
| 2485 | if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR) |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2486 | wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS; |
| 2487 | |
| 2488 | /* the sync object should take a reference while waiting */ |
| 2489 | dri2_egl_ref_sync(dri2_sync); |
| 2490 | |
Marek Olšák | 18123a7 | 2015-09-25 22:48:00 +0200 | [diff] [blame] | 2491 | 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] | 2492 | dri2_sync->fence, wait_flags, |
| 2493 | timeout)) |
| 2494 | dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR; |
| 2495 | else |
| 2496 | ret = EGL_TIMEOUT_EXPIRED_KHR; |
| 2497 | |
| 2498 | dri2_egl_unref_sync(dri2_dpy, dri2_sync); |
| 2499 | return ret; |
| 2500 | } |
| 2501 | |
Marek Olšák | a8617cc | 2015-04-10 12:04:18 +0200 | [diff] [blame] | 2502 | static EGLint |
| 2503 | dri2_server_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync) |
| 2504 | { |
| 2505 | _EGLContext *ctx = _eglGetCurrentContext(); |
| 2506 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy); |
| 2507 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 2508 | struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync); |
| 2509 | |
| 2510 | dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context, |
| 2511 | dri2_sync->fence, 0); |
| 2512 | return EGL_TRUE; |
| 2513 | } |
| 2514 | |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2515 | static void |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2516 | dri2_unload(_EGLDriver *drv) |
| 2517 | { |
| 2518 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 2519 | |
| 2520 | if (dri2_drv->handle) |
| 2521 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2522 | free(dri2_drv); |
| 2523 | } |
| 2524 | |
| 2525 | static EGLBoolean |
| 2526 | dri2_load(_EGLDriver *drv) |
| 2527 | { |
| 2528 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 2529 | #ifdef HAVE_ANDROID_PLATFORM |
| 2530 | const char *libname = "libglapi.so"; |
Julien Isorce | e27ea99 | 2015-06-18 06:53:52 +0100 | [diff] [blame] | 2531 | #elif defined(__APPLE__) |
| 2532 | const char *libname = "libglapi.0.dylib"; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 2533 | #else |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 2534 | const char *libname = "libglapi.so.0"; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 2535 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2536 | void *handle; |
| 2537 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 2538 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 2539 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2540 | if (handle) { |
| 2541 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 2542 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 2543 | if (!dri2_drv->get_proc_address || !libname) { |
| 2544 | /* no need to keep a reference */ |
| 2545 | dlclose(handle); |
| 2546 | handle = NULL; |
| 2547 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2548 | } |
| 2549 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 2550 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2551 | if (!dri2_drv->get_proc_address) { |
| 2552 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 2553 | return EGL_FALSE; |
| 2554 | } |
| 2555 | |
| 2556 | dri2_drv->glFlush = (void (*)(void)) |
| 2557 | dri2_drv->get_proc_address("glFlush"); |
| 2558 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 2559 | dri2_drv->handle = handle; |
| 2560 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2561 | return EGL_TRUE; |
| 2562 | } |
| 2563 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2564 | /** |
| 2565 | * This is the main entrypoint into the driver, called by libEGL. |
| 2566 | * Create a new _EGLDriver object and init its dispatch table. |
| 2567 | */ |
| 2568 | _EGLDriver * |
Eric Anholt | 9f0f00c | 2012-02-05 07:01:17 +0100 | [diff] [blame] | 2569 | _eglBuiltInDriverDRI2(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2570 | { |
| 2571 | struct dri2_egl_driver *dri2_drv; |
| 2572 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 2573 | (void) args; |
| 2574 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 2575 | dri2_drv = calloc(1, sizeof *dri2_drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2576 | if (!dri2_drv) |
| 2577 | return NULL; |
| 2578 | |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 2579 | if (!dri2_load(&dri2_drv->base)) { |
| 2580 | free(dri2_drv); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2581 | return NULL; |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 2582 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 2583 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2584 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 2585 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 2586 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 2587 | dri2_drv->base.API.CreateContext = dri2_create_context; |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame] | 2588 | dri2_drv->base.API.DestroyContext = dri2_destroy_context; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2589 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 2590 | dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface; |
Chad Versace | bc8b07a | 2014-01-28 16:45:07 -0800 | [diff] [blame] | 2591 | dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface; |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2592 | dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface; |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2593 | dri2_drv->base.API.DestroySurface = dri2_destroy_surface; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2594 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 2595 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 2596 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 2597 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 2598 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 2599 | dri2_drv->base.API.SwapInterval = dri2_swap_interval; |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 2600 | dri2_drv->base.API.SwapBuffers = dri2_swap_buffers; |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 2601 | dri2_drv->base.API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage; |
Chad Versace | 75d398e | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2602 | dri2_drv->base.API.SwapBuffersRegionNOK = dri2_swap_buffers_region; |
Chad Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2603 | dri2_drv->base.API.PostSubBufferNV = dri2_post_sub_buffer; |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2604 | dri2_drv->base.API.CopyBuffers = dri2_copy_buffers, |
Chad Versace | 3fdfbd2 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2605 | dri2_drv->base.API.QueryBufferAge = dri2_query_buffer_age; |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2606 | dri2_drv->base.API.CreateImageKHR = dri2_create_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 2607 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 2608 | dri2_drv->base.API.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image; |
Axel Davy | 63986f9 | 2015-01-14 20:36:04 +0100 | [diff] [blame] | 2609 | #ifdef HAVE_LIBDRM |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 2610 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 2611 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Dave Airlie | 8f7338f | 2014-03-03 13:57:16 +1000 | [diff] [blame] | 2612 | dri2_drv->base.API.ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa; |
| 2613 | dri2_drv->base.API.ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa; |
Samuel Thibault | a594cec | 2013-11-10 19:32:01 +0100 | [diff] [blame] | 2614 | #endif |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2615 | #ifdef HAVE_WAYLAND_PLATFORM |
| 2616 | dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl; |
| 2617 | dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 2618 | dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 2619 | #endif |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame] | 2620 | dri2_drv->base.API.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium; |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2621 | dri2_drv->base.API.CreateSyncKHR = dri2_create_sync; |
| 2622 | dri2_drv->base.API.ClientWaitSyncKHR = dri2_client_wait_sync; |
Marek Olšák | a8617cc | 2015-04-10 12:04:18 +0200 | [diff] [blame] | 2623 | dri2_drv->base.API.WaitSyncKHR = dri2_server_wait_sync; |
Marek Olšák | 9a0bda2 | 2015-04-10 10:56:02 +0200 | [diff] [blame] | 2624 | dri2_drv->base.API.DestroySyncKHR = dri2_destroy_sync; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2625 | |
| 2626 | dri2_drv->base.Name = "DRI2"; |
| 2627 | dri2_drv->base.Unload = dri2_unload; |
| 2628 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 2629 | return &dri2_drv->base; |
| 2630 | } |