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 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <limits.h> |
| 32 | #include <dlfcn.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
| 35 | #include <unistd.h> |
| 36 | #include <xf86drm.h> |
| 37 | #include <GL/gl.h> |
| 38 | #include <GL/internal/dri_interface.h> |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 42 | #include "egl_dri2.h" |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 43 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 44 | const __DRIuseInvalidateExtension use_invalidate = { |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 45 | { __DRI_USE_INVALIDATE, 1 } |
| 46 | }; |
| 47 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 48 | EGLint dri2_to_egl_attribute_map[] = { |
| 49 | 0, |
| 50 | EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */ |
| 51 | EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */ |
| 52 | EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */ |
| 53 | EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */ |
| 54 | EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 55 | EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 56 | EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */ |
| 57 | 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */ |
| 58 | EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */ |
| 59 | EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */ |
| 60 | 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */ |
| 61 | 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */ |
| 62 | 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */ |
| 63 | 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */ |
| 64 | EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */ |
| 65 | EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */ |
| 66 | 0, /* __DRI_ATTRIB_RENDER_TYPE, */ |
| 67 | 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */ |
| 68 | 0, /* __DRI_ATTRIB_CONFORMANT */ |
| 69 | 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */ |
| 70 | 0, /* __DRI_ATTRIB_STEREO */ |
| 71 | 0, /* __DRI_ATTRIB_AUX_BUFFERS */ |
| 72 | 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */ |
| 73 | 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */ |
| 74 | 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */ |
| 75 | 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */ |
| 76 | 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */ |
| 77 | 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */ |
| 78 | 0, /* __DRI_ATTRIB_FLOAT_MODE */ |
| 79 | 0, /* __DRI_ATTRIB_RED_MASK */ |
| 80 | 0, /* __DRI_ATTRIB_GREEN_MASK */ |
| 81 | 0, /* __DRI_ATTRIB_BLUE_MASK */ |
| 82 | 0, /* __DRI_ATTRIB_ALPHA_MASK */ |
| 83 | EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */ |
| 84 | EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */ |
| 85 | EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */ |
| 86 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */ |
| 87 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */ |
| 88 | 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */ |
| 89 | 0, /* __DRI_ATTRIB_SWAP_METHOD */ |
| 90 | EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */ |
| 91 | EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 92 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */ |
| 93 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 94 | 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */ |
| 95 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */ |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 96 | EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */ |
Benjamin Franzke | 4f6fbfa | 2011-03-07 00:29:13 +0100 | [diff] [blame] | 97 | 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 98 | }; |
| 99 | |
Benjamin Franzke | 16e3027 | 2011-06-11 23:00:40 +0200 | [diff] [blame] | 100 | static EGLBoolean |
| 101 | dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria) |
| 102 | { |
| 103 | if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0) |
| 104 | return EGL_FALSE; |
| 105 | |
| 106 | if (!_eglMatchConfig(conf, criteria)) |
| 107 | return EGL_FALSE; |
| 108 | |
| 109 | return EGL_TRUE; |
| 110 | } |
| 111 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 112 | struct dri2_egl_config * |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 113 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id, |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 114 | int depth, EGLint surface_type, const EGLint *attr_list, |
| 115 | const unsigned int *rgba_masks) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 116 | { |
| 117 | struct dri2_egl_config *conf; |
| 118 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 119 | _EGLConfig base; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 120 | unsigned int attrib, value, double_buffer; |
| 121 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 122 | unsigned int dri_masks[4] = { 0, 0, 0, 0 }; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 123 | _EGLConfig *matching_config; |
| 124 | EGLint num_configs = 0; |
| 125 | EGLint config_id; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 126 | int i; |
| 127 | |
| 128 | dri2_dpy = disp->DriverData; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 129 | _eglInitConfig(&base, disp, id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 130 | |
| 131 | i = 0; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 132 | double_buffer = 0; |
| 133 | bind_to_texture_rgb = 0; |
| 134 | bind_to_texture_rgba = 0; |
| 135 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 136 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 137 | switch (attrib) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 138 | case __DRI_ATTRIB_RENDER_TYPE: |
| 139 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 140 | value = EGL_RGB_BUFFER; |
| 141 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 142 | value = EGL_LUMINANCE_BUFFER; |
| 143 | else |
| 144 | /* not valid */; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 145 | _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 149 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 150 | value = EGL_NON_CONFORMANT_CONFIG; |
| 151 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 152 | value = EGL_SLOW_CONFIG; |
| 153 | else |
| 154 | value = EGL_NONE; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 155 | _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 156 | break; |
| 157 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 158 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 159 | bind_to_texture_rgb = value; |
| 160 | break; |
| 161 | |
| 162 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 163 | bind_to_texture_rgba = value; |
| 164 | break; |
| 165 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 166 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 167 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 168 | break; |
| 169 | |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 170 | case __DRI_ATTRIB_RED_MASK: |
| 171 | dri_masks[0] = value; |
| 172 | break; |
| 173 | |
| 174 | case __DRI_ATTRIB_GREEN_MASK: |
| 175 | dri_masks[1] = value; |
| 176 | break; |
| 177 | |
| 178 | case __DRI_ATTRIB_BLUE_MASK: |
| 179 | dri_masks[2] = value; |
| 180 | break; |
| 181 | |
| 182 | case __DRI_ATTRIB_ALPHA_MASK: |
| 183 | dri_masks[3] = value; |
| 184 | break; |
| 185 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 186 | default: |
| 187 | key = dri2_to_egl_attribute_map[attrib]; |
| 188 | if (key != 0) |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 189 | _eglSetConfigKey(&base, key, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 194 | if (attr_list) |
| 195 | for (i = 0; attr_list[i] != EGL_NONE; i += 2) |
| 196 | _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 197 | |
Ian Romanick | 68a147e | 2013-02-22 19:01:40 -0800 | [diff] [blame] | 198 | /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig. Otherwise |
| 199 | * it will only match a 32-bit RGBA visual. On a composited window manager |
| 200 | * on X11, this will make all of the EGLConfigs with destination alpha get |
| 201 | * blended by the compositor. This is probably not what the application |
| 202 | * wants... especially on drivers that only have 32-bit RGBA EGLConfigs! |
| 203 | */ |
| 204 | if (depth > 0 && depth != base.BufferSize |
| 205 | && !(depth == 24 && base.BufferSize == 32)) |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 206 | return NULL; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 207 | |
Chia-I Wu | 58911b8 | 2011-08-05 14:36:14 +0900 | [diff] [blame] | 208 | if (rgba_masks && memcmp(rgba_masks, dri_masks, sizeof(dri_masks))) |
| 209 | return NULL; |
| 210 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 211 | base.NativeRenderable = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 212 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 213 | base.SurfaceType = surface_type; |
Benjamin Franzke | a8128d7 | 2011-02-04 12:32:30 +0100 | [diff] [blame] | 214 | if (surface_type & (EGL_PBUFFER_BIT | |
| 215 | (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) { |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 216 | base.BindToTextureRGB = bind_to_texture_rgb; |
| 217 | if (base.AlphaSize > 0) |
| 218 | base.BindToTextureRGBA = bind_to_texture_rgba; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 219 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 220 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 221 | base.RenderableType = disp->ClientAPIs; |
| 222 | base.Conformant = disp->ClientAPIs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 223 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 224 | if (!_eglValidateConfig(&base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 225 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 226 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 229 | config_id = base.ConfigID; |
| 230 | base.ConfigID = EGL_DONT_CARE; |
| 231 | base.SurfaceType = EGL_DONT_CARE; |
| 232 | num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1, |
Benjamin Franzke | 16e3027 | 2011-06-11 23:00:40 +0200 | [diff] [blame] | 233 | (_EGLArrayForEach) dri2_match_config, &base); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 234 | |
| 235 | if (num_configs == 1) { |
| 236 | conf = (struct dri2_egl_config *) matching_config; |
| 237 | |
| 238 | if (double_buffer && !conf->dri_double_config) |
| 239 | conf->dri_double_config = dri_config; |
| 240 | else if (!double_buffer && !conf->dri_single_config) |
| 241 | conf->dri_single_config = dri_config; |
| 242 | else |
Benjamin Franzke | 83c6875 | 2011-05-12 19:30:05 +0200 | [diff] [blame] | 243 | /* a similar config type is already added (unlikely) => discard */ |
| 244 | return NULL; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 245 | } |
José Fonseca | 4a47d6d | 2011-05-12 15:23:48 +0100 | [diff] [blame] | 246 | else if (num_configs == 0) { |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 247 | conf = malloc(sizeof *conf); |
| 248 | if (conf == NULL) |
| 249 | return NULL; |
| 250 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 251 | memcpy(&conf->base, &base, sizeof base); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 252 | if (double_buffer) { |
| 253 | conf->dri_double_config = dri_config; |
| 254 | conf->dri_single_config = NULL; |
| 255 | } else { |
| 256 | conf->dri_single_config = dri_config; |
| 257 | conf->dri_double_config = NULL; |
| 258 | } |
| 259 | conf->base.SurfaceType = 0; |
| 260 | conf->base.ConfigID = config_id; |
| 261 | |
Chia-I Wu | 8a6bdf3 | 2010-10-23 00:47:22 +0800 | [diff] [blame] | 262 | _eglLinkConfig(&conf->base); |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 263 | } |
José Fonseca | 4a47d6d | 2011-05-12 15:23:48 +0100 | [diff] [blame] | 264 | else { |
| 265 | assert(0); |
| 266 | return NULL; |
| 267 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 268 | |
Fredrik Höglund | 655f2c1 | 2011-12-20 17:17:02 +0100 | [diff] [blame] | 269 | if (double_buffer) { |
Kristian Høgsberg | 447bb45 | 2011-08-31 18:20:06 -0400 | [diff] [blame] | 270 | surface_type &= ~EGL_PIXMAP_BIT; |
| 271 | |
Eric Anholt | 7e9bd2b | 2012-09-25 14:05:30 -0700 | [diff] [blame] | 272 | conf->base.MinSwapInterval = dri2_dpy->min_swap_interval; |
| 273 | conf->base.MaxSwapInterval = dri2_dpy->max_swap_interval; |
Fredrik Höglund | 655f2c1 | 2011-12-20 17:17:02 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Kristian Høgsberg | 447bb45 | 2011-08-31 18:20:06 -0400 | [diff] [blame] | 276 | conf->base.SurfaceType |= surface_type; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 277 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 278 | return conf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 281 | __DRIimage * |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 282 | dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 283 | { |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 284 | _EGLDisplay *disp = data; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 285 | struct dri2_egl_image *dri2_img; |
| 286 | _EGLImage *img; |
| 287 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 288 | (void) screen; |
| 289 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 290 | img = _eglLookupImage(image, disp); |
| 291 | if (img == NULL) { |
| 292 | _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image"); |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | dri2_img = dri2_egl_image(image); |
| 297 | |
| 298 | return dri2_img->dri_image; |
| 299 | } |
| 300 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 301 | const __DRIimageLookupExtension image_lookup_extension = { |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 302 | { __DRI_IMAGE_LOOKUP, 1 }, |
| 303 | dri2_lookup_egl_image |
| 304 | }; |
| 305 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 306 | static const char dri_driver_path[] = DEFAULT_DRIVER_DIR; |
| 307 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 308 | struct dri2_extension_match { |
| 309 | const char *name; |
| 310 | int version; |
| 311 | int offset; |
| 312 | }; |
| 313 | |
| 314 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 315 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
Ian Romanick | 38f91f2 | 2012-07-18 13:17:50 -0700 | [diff] [blame] | 316 | { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 317 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 321 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 322 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame^] | 323 | { __DRI_IMAGE, 7, offsetof(struct dri2_egl_display, image) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 324 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 325 | }; |
| 326 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 327 | static struct dri2_extension_match swrast_driver_extensions[] = { |
| 328 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 329 | { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) }, |
Ian Romanick | 9d76ad2 | 2012-07-19 11:08:02 -0700 | [diff] [blame] | 330 | { NULL, 0, 0 } |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | static struct dri2_extension_match swrast_core_extensions[] = { |
| 334 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Ian Romanick | 9d76ad2 | 2012-07-19 11:08:02 -0700 | [diff] [blame] | 335 | { NULL, 0, 0 } |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 336 | }; |
| 337 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 338 | static EGLBoolean |
| 339 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 340 | struct dri2_extension_match *matches, |
| 341 | const __DRIextension **extensions) |
| 342 | { |
| 343 | int i, j, ret = EGL_TRUE; |
| 344 | void *field; |
| 345 | |
| 346 | for (i = 0; extensions[i]; i++) { |
| 347 | _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name); |
| 348 | for (j = 0; matches[j].name; j++) { |
| 349 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 350 | extensions[i]->version >= matches[j].version) { |
| 351 | field = ((char *) dri2_dpy + matches[j].offset); |
| 352 | *(const __DRIextension **) field = extensions[i]; |
| 353 | _eglLog(_EGL_INFO, "DRI2: found extension %s version %d", |
| 354 | extensions[i]->name, extensions[i]->version); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | for (j = 0; matches[j].name; j++) { |
| 360 | field = ((char *) dri2_dpy + matches[j].offset); |
| 361 | if (*(const __DRIextension **) field == NULL) { |
| 362 | _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d", |
| 363 | matches[j].name, matches[j].version); |
| 364 | ret = EGL_FALSE; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 371 | static const __DRIextension ** |
| 372 | dri2_open_driver(_EGLDisplay *disp) |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 373 | { |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 374 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 375 | const __DRIextension **extensions; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 376 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 377 | |
| 378 | search_paths = NULL; |
| 379 | if (geteuid() == getuid()) { |
| 380 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 381 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 382 | } |
| 383 | if (search_paths == NULL) |
| 384 | search_paths = DEFAULT_DRIVER_DIR; |
| 385 | |
| 386 | dri2_dpy->driver = NULL; |
| 387 | end = search_paths + strlen(search_paths); |
| 388 | for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) { |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 389 | int len; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 390 | next = strchr(p, ':'); |
| 391 | if (next == NULL) |
| 392 | next = end; |
| 393 | |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 394 | len = next - p; |
| 395 | #if GLX_USE_TLS |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 396 | snprintf(path, sizeof path, |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 397 | "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 398 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 399 | #endif |
| 400 | if (dri2_dpy->driver == NULL) { |
| 401 | snprintf(path, sizeof path, |
| 402 | "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name); |
| 403 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 404 | if (dri2_dpy->driver == NULL) |
| 405 | _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror()); |
| 406 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (dri2_dpy->driver == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 410 | _eglLog(_EGL_WARNING, |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 411 | "DRI2: failed to open %s (search paths %s)", |
| 412 | dri2_dpy->driver_name, search_paths); |
| 413 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
| 417 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
| 418 | if (extensions == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 419 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 420 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 421 | dlclose(dri2_dpy->driver); |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return extensions; |
| 425 | } |
| 426 | |
| 427 | EGLBoolean |
| 428 | dri2_load_driver(_EGLDisplay *disp) |
| 429 | { |
| 430 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 431 | const __DRIextension **extensions; |
| 432 | |
| 433 | extensions = dri2_open_driver(disp); |
| 434 | if (!extensions) |
| 435 | return EGL_FALSE; |
| 436 | |
| 437 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) { |
| 438 | dlclose(dri2_dpy->driver); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 439 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 440 | } |
| 441 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 442 | return EGL_TRUE; |
| 443 | } |
| 444 | |
| 445 | EGLBoolean |
| 446 | dri2_load_driver_swrast(_EGLDisplay *disp) |
| 447 | { |
| 448 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 449 | const __DRIextension **extensions; |
| 450 | |
| 451 | dri2_dpy->driver_name = "swrast"; |
| 452 | extensions = dri2_open_driver(disp); |
Chia-I Wu | 77e031a | 2011-06-08 16:33:56 +0800 | [diff] [blame] | 453 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 454 | if (!extensions) |
| 455 | return EGL_FALSE; |
| 456 | |
| 457 | if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) { |
| 458 | dlclose(dri2_dpy->driver); |
| 459 | return EGL_FALSE; |
| 460 | } |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 461 | |
| 462 | return EGL_TRUE; |
| 463 | } |
| 464 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 465 | void |
| 466 | dri2_setup_screen(_EGLDisplay *disp) |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 467 | { |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 468 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 469 | unsigned int api_mask; |
| 470 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 471 | if (dri2_dpy->dri2) { |
Ian Romanick | 38f91f2 | 2012-07-18 13:17:50 -0700 | [diff] [blame] | 472 | api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 473 | } else { |
| 474 | assert(dri2_dpy->swrast); |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 475 | api_mask = 1 << __DRI_API_OPENGL | |
| 476 | 1 << __DRI_API_GLES | |
| 477 | 1 << __DRI_API_GLES2 | |
| 478 | 1 << __DRI_API_GLES3; |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 479 | } |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 480 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 481 | disp->ClientAPIs = 0; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 482 | if (api_mask & (1 <<__DRI_API_OPENGL)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 483 | disp->ClientAPIs |= EGL_OPENGL_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 484 | if (api_mask & (1 <<__DRI_API_GLES)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 485 | disp->ClientAPIs |= EGL_OPENGL_ES_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 486 | if (api_mask & (1 << __DRI_API_GLES2)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 487 | disp->ClientAPIs |= EGL_OPENGL_ES2_BIT; |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 488 | if (api_mask & (1 << __DRI_API_GLES3)) |
| 489 | disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 490 | |
Ian Romanick | 38f91f2 | 2012-07-18 13:17:50 -0700 | [diff] [blame] | 491 | assert(dri2_dpy->dri2 || dri2_dpy->swrast); |
| 492 | disp->Extensions.KHR_surfaceless_context = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 493 | |
Paulo Alcantara | b41f36b | 2012-08-17 14:08:10 -0300 | [diff] [blame] | 494 | if (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) { |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 495 | disp->Extensions.KHR_create_context = EGL_TRUE; |
| 496 | |
| 497 | if (dri2_dpy->robustness) |
| 498 | disp->Extensions.EXT_create_context_robustness = EGL_TRUE; |
| 499 | } |
| 500 | |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 501 | if (dri2_dpy->image) { |
| 502 | disp->Extensions.MESA_drm_image = EGL_TRUE; |
| 503 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 504 | disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE; |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 505 | if (dri2_dpy->image->base.version >= 5 && |
| 506 | dri2_dpy->image->createImageFromTexture) { |
| 507 | disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE; |
| 508 | disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE; |
| 509 | } |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 510 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | EGLBoolean |
| 514 | dri2_create_screen(_EGLDisplay *disp) |
| 515 | { |
| 516 | const __DRIextension **extensions; |
| 517 | struct dri2_egl_display *dri2_dpy; |
| 518 | |
| 519 | dri2_dpy = disp->DriverData; |
| 520 | |
| 521 | if (dri2_dpy->dri2) { |
| 522 | dri2_dpy->dri_screen = |
| 523 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
| 524 | &dri2_dpy->driver_configs, disp); |
| 525 | } else { |
| 526 | assert(dri2_dpy->swrast); |
| 527 | dri2_dpy->dri_screen = |
| 528 | dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions, |
| 529 | &dri2_dpy->driver_configs, disp); |
| 530 | } |
| 531 | |
| 532 | if (dri2_dpy->dri_screen == NULL) { |
| 533 | _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen"); |
| 534 | return EGL_FALSE; |
| 535 | } |
| 536 | |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 537 | dri2_dpy->own_dri_screen = 1; |
| 538 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 539 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
| 540 | |
| 541 | if (dri2_dpy->dri2) { |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 542 | unsigned i; |
| 543 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 544 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
| 545 | goto cleanup_dri_screen; |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 546 | |
| 547 | for (i = 0; extensions[i]; i++) { |
| 548 | if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) { |
| 549 | dri2_dpy->robustness = (__DRIrobustnessExtension *) extensions[i]; |
| 550 | } |
Eric Anholt | 7e9bd2b | 2012-09-25 14:05:30 -0700 | [diff] [blame] | 551 | if (strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0) { |
| 552 | dri2_dpy->config = (__DRI2configQueryExtension *) extensions[i]; |
| 553 | } |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 554 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 555 | } else { |
| 556 | assert(dri2_dpy->swrast); |
| 557 | if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions)) |
| 558 | goto cleanup_dri_screen; |
| 559 | } |
| 560 | |
| 561 | dri2_setup_screen(disp); |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 562 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 563 | return EGL_TRUE; |
| 564 | |
| 565 | cleanup_dri_screen: |
| 566 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 567 | |
| 568 | return EGL_FALSE; |
| 569 | } |
| 570 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 571 | /** |
| 572 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 573 | */ |
| 574 | static EGLBoolean |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 575 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp) |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 576 | { |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 577 | /* not until swrast_dri is supported */ |
| 578 | if (disp->Options.UseFallback) |
| 579 | return EGL_FALSE; |
| 580 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 581 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 582 | #ifdef HAVE_X11_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 583 | case _EGL_PLATFORM_X11: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 584 | if (disp->Options.TestOnly) |
| 585 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 586 | return dri2_initialize_x11(drv, disp); |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 587 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 588 | |
| 589 | #ifdef HAVE_LIBUDEV |
Benjamin Franzke | 629c15a | 2011-06-24 21:45:05 +0200 | [diff] [blame] | 590 | #ifdef HAVE_DRM_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 591 | case _EGL_PLATFORM_DRM: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 592 | if (disp->Options.TestOnly) |
| 593 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 594 | return dri2_initialize_drm(drv, disp); |
Benjamin Franzke | 629c15a | 2011-06-24 21:45:05 +0200 | [diff] [blame] | 595 | #endif |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 596 | #ifdef HAVE_WAYLAND_PLATFORM |
| 597 | case _EGL_PLATFORM_WAYLAND: |
| 598 | if (disp->Options.TestOnly) |
| 599 | return EGL_TRUE; |
| 600 | return dri2_initialize_wayland(drv, disp); |
| 601 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 602 | #endif |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 603 | #ifdef HAVE_ANDROID_PLATFORM |
| 604 | case _EGL_PLATFORM_ANDROID: |
| 605 | if (disp->Options.TestOnly) |
| 606 | return EGL_TRUE; |
| 607 | return dri2_initialize_android(drv, disp); |
| 608 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 609 | |
| 610 | default: |
| 611 | return EGL_FALSE; |
| 612 | } |
| 613 | } |
| 614 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 615 | /** |
| 616 | * Called via eglTerminate(), drv->API.Terminate(). |
| 617 | */ |
| 618 | static EGLBoolean |
| 619 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 620 | { |
| 621 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 622 | |
| 623 | _eglReleaseDisplayResources(drv, disp); |
| 624 | _eglCleanupDisplay(disp); |
| 625 | |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 626 | if (dri2_dpy->own_dri_screen) |
| 627 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 628 | if (dri2_dpy->fd) |
| 629 | close(dri2_dpy->fd); |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 630 | if (dri2_dpy->driver) |
| 631 | dlclose(dri2_dpy->driver); |
Matt Turner | 5067506 | 2012-09-03 20:24:35 -0700 | [diff] [blame] | 632 | free(dri2_dpy->device_name); |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 633 | |
| 634 | if (disp->PlatformDisplay == NULL) { |
| 635 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 636 | #ifdef HAVE_X11_PLATFORM |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 637 | case _EGL_PLATFORM_X11: |
| 638 | xcb_disconnect(dri2_dpy->conn); |
| 639 | break; |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 640 | #endif |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 641 | #ifdef HAVE_DRM_PLATFORM |
| 642 | case _EGL_PLATFORM_DRM: |
Benjamin Franzke | 2a58453 | 2011-12-13 14:43:48 +0100 | [diff] [blame] | 643 | if (dri2_dpy->own_device) { |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 644 | gbm_device_destroy(&dri2_dpy->gbm_dri->base.base); |
| 645 | } |
| 646 | break; |
| 647 | #endif |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 648 | default: |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 653 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 654 | disp->DriverData = NULL; |
| 655 | |
| 656 | return EGL_TRUE; |
| 657 | } |
| 658 | |
Chad Versace | 5cf8536 | 2012-11-09 14:06:41 -0800 | [diff] [blame] | 659 | /** |
| 660 | * Set the error code after a call to |
| 661 | * dri2_egl_display::dri2::createContextAttribs. |
| 662 | */ |
| 663 | static void |
| 664 | dri2_create_context_attribs_error(int dri_error) |
| 665 | { |
| 666 | EGLint egl_error; |
| 667 | |
| 668 | switch (dri_error) { |
| 669 | case __DRI_CTX_ERROR_SUCCESS: |
| 670 | return; |
| 671 | |
| 672 | case __DRI_CTX_ERROR_NO_MEMORY: |
| 673 | egl_error = EGL_BAD_ALLOC; |
| 674 | break; |
| 675 | |
| 676 | /* From the EGL_KHR_create_context spec, section "Errors": |
| 677 | * |
| 678 | * * If <config> does not support a client API context compatible |
| 679 | * with the requested API major and minor version, [...] context flags, |
| 680 | * and context reset notification behavior (for client API types where |
| 681 | * these attributes are supported), then an EGL_BAD_MATCH error is |
| 682 | * generated. |
| 683 | * |
| 684 | * * If an OpenGL ES context is requested and the values for |
| 685 | * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and |
| 686 | * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that |
| 687 | * is not defined, than an EGL_BAD_MATCH error is generated. |
| 688 | * |
| 689 | * * If an OpenGL context is requested, the requested version is |
| 690 | * greater than 3.2, and the value for attribute |
| 691 | * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any |
| 692 | * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and |
| 693 | * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than |
| 694 | * one of these bits set; or if the implementation does not support |
| 695 | * the requested profile, then an EGL_BAD_MATCH error is generated. |
| 696 | */ |
| 697 | case __DRI_CTX_ERROR_BAD_API: |
| 698 | case __DRI_CTX_ERROR_BAD_VERSION: |
| 699 | case __DRI_CTX_ERROR_BAD_FLAG: |
| 700 | egl_error = EGL_BAD_MATCH; |
| 701 | break; |
| 702 | |
| 703 | /* From the EGL_KHR_create_context spec, section "Errors": |
| 704 | * |
| 705 | * * If an attribute name or attribute value in <attrib_list> is not |
| 706 | * recognized (including unrecognized bits in bitmask attributes), |
| 707 | * then an EGL_BAD_ATTRIBUTE error is generated." |
| 708 | */ |
| 709 | case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE: |
| 710 | case __DRI_CTX_ERROR_UNKNOWN_FLAG: |
| 711 | egl_error = EGL_BAD_ATTRIBUTE; |
| 712 | break; |
| 713 | |
| 714 | default: |
| 715 | assert(0); |
| 716 | egl_error = EGL_BAD_MATCH; |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | _eglError(egl_error, "dri2_create_context"); |
| 721 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 722 | |
| 723 | /** |
| 724 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 725 | */ |
| 726 | static _EGLContext * |
| 727 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 728 | _EGLContext *share_list, const EGLint *attrib_list) |
| 729 | { |
| 730 | struct dri2_egl_context *dri2_ctx; |
| 731 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 732 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
Ian Romanick | cb77f5d | 2012-07-18 14:37:28 -0700 | [diff] [blame] | 733 | __DRIcontext *shared = |
| 734 | dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 735 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 736 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 737 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 738 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 739 | (void) drv; |
| 740 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 741 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 742 | if (!dri2_ctx) { |
| 743 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 744 | return NULL; |
| 745 | } |
| 746 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 747 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 748 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 749 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 750 | switch (dri2_ctx->base.ClientAPI) { |
| 751 | case EGL_OPENGL_ES_API: |
Ian Romanick | 3fd79dd | 2012-07-18 15:59:15 -0700 | [diff] [blame] | 752 | switch (dri2_ctx->base.ClientMajorVersion) { |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 753 | case 1: |
| 754 | api = __DRI_API_GLES; |
| 755 | break; |
| 756 | case 2: |
Ian Romanick | d786bf2 | 2013-01-15 14:17:46 -0800 | [diff] [blame] | 757 | api = __DRI_API_GLES2; |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 758 | break; |
Ian Romanick | dbecb41 | 2012-07-19 16:12:13 -0700 | [diff] [blame] | 759 | case 3: |
Chad Versace | eb09940 | 2012-11-20 13:43:11 -0800 | [diff] [blame] | 760 | api = __DRI_API_GLES3; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 761 | break; |
| 762 | default: |
| 763 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 764 | return NULL; |
| 765 | } |
| 766 | break; |
| 767 | case EGL_OPENGL_API: |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 768 | if ((dri2_ctx->base.ClientMajorVersion >= 4 |
| 769 | || (dri2_ctx->base.ClientMajorVersion == 3 |
| 770 | && dri2_ctx->base.ClientMinorVersion >= 2)) |
| 771 | && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR) |
| 772 | api = __DRI_API_OPENGL_CORE; |
| 773 | else |
| 774 | api = __DRI_API_OPENGL; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 775 | break; |
| 776 | default: |
| 777 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
Vinson Lee | 080e91a | 2013-01-31 23:41:14 -0800 | [diff] [blame] | 778 | free(dri2_ctx); |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 779 | return NULL; |
| 780 | } |
| 781 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 782 | if (conf != NULL) { |
| 783 | /* The config chosen here isn't necessarily |
| 784 | * used for surfaces later. |
| 785 | * A pixmap surface will use the single config. |
| 786 | * This opportunity depends on disabling the |
| 787 | * doubleBufferMode check in |
| 788 | * src/mesa/main/context.c:check_compatible() |
| 789 | */ |
| 790 | if (dri2_config->dri_double_config) |
| 791 | dri_config = dri2_config->dri_double_config; |
| 792 | else |
| 793 | dri_config = dri2_config->dri_single_config; |
Chia-I Wu | 357d3f3 | 2011-08-27 01:42:16 +0800 | [diff] [blame] | 794 | |
| 795 | /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This |
| 796 | * makes sure the back buffer will always be used. |
| 797 | */ |
| 798 | if (conf->SurfaceType & EGL_WINDOW_BIT) |
| 799 | dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 800 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 801 | else |
| 802 | dri_config = NULL; |
| 803 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 804 | if (dri2_dpy->dri2) { |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 805 | if (dri2_dpy->dri2->base.version >= 3) { |
| 806 | unsigned error; |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 807 | unsigned num_attribs = 0; |
| 808 | uint32_t ctx_attribs[8]; |
| 809 | |
| 810 | ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION; |
| 811 | ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMajorVersion; |
| 812 | ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION; |
| 813 | ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMinorVersion; |
| 814 | |
| 815 | if (dri2_ctx->base.Flags != 0) { |
| 816 | /* If the implementation doesn't support the __DRI2_ROBUSTNESS |
| 817 | * extension, don't even try to send it the robust-access flag. |
| 818 | * It may explode. Instead, generate the required EGL error here. |
| 819 | */ |
| 820 | if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0 |
| 821 | && !dri2_dpy->robustness) { |
| 822 | _eglError(EGL_BAD_MATCH, "eglCreateContext"); |
| 823 | goto cleanup; |
| 824 | } |
| 825 | |
| 826 | ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_FLAGS; |
| 827 | ctx_attribs[num_attribs++] = dri2_ctx->base.Flags; |
| 828 | } |
| 829 | |
| 830 | if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) { |
| 831 | /* If the implementation doesn't support the __DRI2_ROBUSTNESS |
| 832 | * extension, don't even try to send it a reset strategy. It may |
| 833 | * explode. Instead, generate the required EGL error here. |
| 834 | */ |
| 835 | if (!dri2_dpy->robustness) { |
| 836 | _eglError(EGL_BAD_CONFIG, "eglCreateContext"); |
| 837 | goto cleanup; |
| 838 | } |
| 839 | |
| 840 | ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY; |
| 841 | ctx_attribs[num_attribs++] = __DRI_CTX_RESET_LOSE_CONTEXT; |
| 842 | } |
| 843 | |
| 844 | assert(num_attribs <= ARRAY_SIZE(ctx_attribs)); |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 845 | |
| 846 | dri2_ctx->dri_context = |
| 847 | dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen, |
| 848 | api, |
| 849 | dri_config, |
| 850 | shared, |
Ian Romanick | 7b4b4f8 | 2012-07-19 16:04:01 -0700 | [diff] [blame] | 851 | num_attribs / 2, |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 852 | ctx_attribs, |
| 853 | & error, |
| 854 | dri2_ctx); |
Chad Versace | 5cf8536 | 2012-11-09 14:06:41 -0800 | [diff] [blame] | 855 | dri2_create_context_attribs_error(error); |
Ian Romanick | ce55741 | 2012-07-18 14:41:28 -0700 | [diff] [blame] | 856 | } else { |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 857 | dri2_ctx->dri_context = |
| 858 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 859 | api, |
| 860 | dri_config, |
Ian Romanick | cb77f5d | 2012-07-18 14:37:28 -0700 | [diff] [blame] | 861 | shared, |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 862 | dri2_ctx); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 863 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 864 | } else { |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 865 | assert(dri2_dpy->swrast); |
Ian Romanick | 05413dd | 2012-07-18 10:08:49 -0700 | [diff] [blame] | 866 | dri2_ctx->dri_context = |
| 867 | dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen, |
| 868 | api, |
| 869 | dri_config, |
Ian Romanick | cb77f5d | 2012-07-18 14:37:28 -0700 | [diff] [blame] | 870 | shared, |
Ian Romanick | 05413dd | 2012-07-18 10:08:49 -0700 | [diff] [blame] | 871 | dri2_ctx); |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 872 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 873 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 874 | if (!dri2_ctx->dri_context) |
| 875 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 876 | |
| 877 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 878 | |
| 879 | cleanup: |
| 880 | free(dri2_ctx); |
| 881 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 882 | } |
| 883 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 884 | /** |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame] | 885 | * Called via eglDestroyContext(), drv->API.DestroyContext(). |
| 886 | */ |
| 887 | static EGLBoolean |
| 888 | dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
| 889 | { |
| 890 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 891 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 892 | |
| 893 | if (_eglPutContext(ctx)) { |
| 894 | dri2_dpy->core->destroyContext(dri2_ctx->dri_context); |
| 895 | free(dri2_ctx); |
| 896 | } |
| 897 | |
| 898 | return EGL_TRUE; |
| 899 | } |
| 900 | |
| 901 | /** |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 902 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 903 | */ |
| 904 | static EGLBoolean |
| 905 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 906 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 907 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 908 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 909 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 910 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 911 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 912 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 913 | _EGLContext *old_ctx; |
| 914 | _EGLSurface *old_dsurf, *old_rsurf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 915 | __DRIdrawable *ddraw, *rdraw; |
| 916 | __DRIcontext *cctx; |
| 917 | |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 918 | /* make new bindings */ |
| 919 | if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 920 | return EGL_FALSE; |
| 921 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 922 | /* flush before context switch */ |
Chia-I Wu | 547e761 | 2010-10-26 15:00:56 +0800 | [diff] [blame] | 923 | if (old_ctx && dri2_drv->glFlush) |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 924 | dri2_drv->glFlush(); |
| 925 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 926 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 927 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 928 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 929 | |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 930 | if (old_ctx) { |
| 931 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 932 | dri2_dpy->core->unbindContext(old_cctx); |
| 933 | } |
| 934 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 935 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 936 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 937 | if (old_dsurf) |
| 938 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 939 | if (old_rsurf) |
| 940 | drv->API.DestroySurface(drv, disp, old_rsurf); |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 941 | if (old_ctx) |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame] | 942 | drv->API.DestroyContext(drv, disp, old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 943 | |
| 944 | return EGL_TRUE; |
| 945 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 946 | /* undo the previous _eglBindContext */ |
| 947 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf); |
| 948 | assert(&dri2_ctx->base == ctx && |
| 949 | &dri2_dsurf->base == dsurf && |
| 950 | &dri2_rsurf->base == rsurf); |
| 951 | |
| 952 | _eglPutSurface(dsurf); |
| 953 | _eglPutSurface(rsurf); |
| 954 | _eglPutContext(ctx); |
| 955 | |
| 956 | _eglPutSurface(old_dsurf); |
| 957 | _eglPutSurface(old_rsurf); |
| 958 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 959 | |
| 960 | return EGL_FALSE; |
| 961 | } |
| 962 | } |
| 963 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 964 | /* |
| 965 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 966 | */ |
| 967 | static _EGLProc |
| 968 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 969 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 970 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 971 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 972 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 976 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 977 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 978 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 979 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 980 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 981 | (void) drv; |
| 982 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 983 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 984 | * we need to copy fake to real here.*/ |
| 985 | |
José Fonseca | 5048e69 | 2013-02-05 13:43:01 +0000 | [diff] [blame] | 986 | if (dri2_dpy->flush != NULL) |
| 987 | dri2_dpy->flush->flush(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 988 | |
| 989 | return EGL_TRUE; |
| 990 | } |
| 991 | |
| 992 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 993 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 994 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 995 | (void) drv; |
| 996 | (void) disp; |
| 997 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 998 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 999 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 1000 | /* glXWaitX(); */ |
| 1001 | |
| 1002 | return EGL_TRUE; |
| 1003 | } |
| 1004 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1005 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1006 | dri2_bind_tex_image(_EGLDriver *drv, |
| 1007 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1008 | { |
| 1009 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1010 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1011 | struct dri2_egl_context *dri2_ctx; |
| 1012 | _EGLContext *ctx; |
| 1013 | GLint format, target; |
| 1014 | |
| 1015 | ctx = _eglGetCurrentContext(); |
| 1016 | dri2_ctx = dri2_egl_context(ctx); |
| 1017 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1018 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1019 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1020 | |
| 1021 | switch (dri2_surf->base.TextureFormat) { |
| 1022 | case EGL_TEXTURE_RGB: |
| 1023 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 1024 | break; |
| 1025 | case EGL_TEXTURE_RGBA: |
| 1026 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 1027 | break; |
| 1028 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1029 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | switch (dri2_surf->base.TextureTarget) { |
| 1033 | case EGL_TEXTURE_2D: |
| 1034 | target = GL_TEXTURE_2D; |
| 1035 | break; |
| 1036 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1037 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 1041 | target, format, |
| 1042 | dri2_surf->dri_drawable); |
| 1043 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1044 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | static EGLBoolean |
| 1048 | dri2_release_tex_image(_EGLDriver *drv, |
| 1049 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1050 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 1051 | #if __DRI_TEX_BUFFER_VERSION >= 3 |
| 1052 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1053 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1054 | struct dri2_egl_context *dri2_ctx; |
| 1055 | _EGLContext *ctx; |
| 1056 | GLint target; |
| 1057 | |
| 1058 | ctx = _eglGetCurrentContext(); |
| 1059 | dri2_ctx = dri2_egl_context(ctx); |
| 1060 | |
| 1061 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 1062 | return EGL_FALSE; |
| 1063 | |
| 1064 | switch (dri2_surf->base.TextureTarget) { |
| 1065 | case EGL_TEXTURE_2D: |
| 1066 | target = GL_TEXTURE_2D; |
| 1067 | break; |
| 1068 | default: |
| 1069 | assert(0); |
| 1070 | } |
| 1071 | if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL) |
| 1072 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 1073 | target, |
| 1074 | dri2_surf->dri_drawable); |
| 1075 | #endif |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1076 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1077 | return EGL_TRUE; |
| 1078 | } |
| 1079 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1080 | static _EGLImage * |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1081 | dri2_create_image(_EGLDisplay *disp, __DRIimage *dri_image) |
| 1082 | { |
| 1083 | struct dri2_egl_image *dri2_img; |
| 1084 | |
| 1085 | if (dri_image == NULL) { |
| 1086 | _eglError(EGL_BAD_ALLOC, "dri2_create_image"); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | dri2_img = malloc(sizeof *dri2_img); |
| 1091 | if (!dri2_img) { |
| 1092 | _eglError(EGL_BAD_ALLOC, "dri2_create_image"); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
| 1096 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 1097 | free(dri2_img); |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | |
| 1101 | dri2_img->dri_image = dri_image; |
| 1102 | |
| 1103 | return &dri2_img->base; |
| 1104 | } |
| 1105 | |
| 1106 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1107 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 1108 | EGLClientBuffer buffer, |
| 1109 | const EGLint *attr_list) |
| 1110 | { |
| 1111 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1112 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 1113 | GLuint renderbuffer = (GLuint) (uintptr_t) buffer; |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1114 | __DRIimage *dri_image; |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1115 | |
| 1116 | if (renderbuffer == 0) { |
| 1117 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1118 | return EGL_NO_IMAGE_KHR; |
| 1119 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1120 | |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1121 | dri_image = |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1122 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1123 | renderbuffer, NULL); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1124 | |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1125 | return dri2_create_image(disp, dri_image); |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static _EGLImage * |
| 1129 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 1130 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1131 | { |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1132 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1133 | EGLint format, name, pitch, err; |
| 1134 | _EGLImageAttribs attrs; |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1135 | __DRIimage *dri_image; |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1136 | |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 1137 | name = (EGLint) (uintptr_t) buffer; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1138 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1139 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1140 | if (err != EGL_SUCCESS) |
| 1141 | return NULL; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1142 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1143 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 1144 | attrs.DRMBufferStrideMESA <= 0) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1145 | _eglError(EGL_BAD_PARAMETER, |
| 1146 | "bad width, height or stride"); |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1150 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1151 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 1152 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1153 | pitch = attrs.DRMBufferStrideMESA; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1154 | break; |
| 1155 | default: |
| 1156 | _eglError(EGL_BAD_PARAMETER, |
| 1157 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 1158 | return NULL; |
| 1159 | } |
| 1160 | |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1161 | dri_image = |
| 1162 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 1163 | attrs.Width, |
| 1164 | attrs.Height, |
| 1165 | format, |
| 1166 | name, |
| 1167 | pitch, |
| 1168 | NULL); |
| 1169 | |
| 1170 | return dri2_create_image(disp, dri_image); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1171 | } |
| 1172 | |
José Fonseca | 5de2c67 | 2011-05-12 15:24:26 +0100 | [diff] [blame] | 1173 | #ifdef HAVE_WAYLAND_PLATFORM |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1174 | |
| 1175 | /* This structure describes how a wl_buffer maps to one or more |
| 1176 | * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the |
| 1177 | * offsets and strides of the planes in the buffer. This table maps a |
| 1178 | * wl_drm format code to a description of the planes in the buffer |
| 1179 | * that lets us create a __DRIimage for each of the planes. */ |
| 1180 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1181 | static const struct wl_drm_components_descriptor { |
| 1182 | uint32_t dri_components; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1183 | EGLint components; |
| 1184 | int nplanes; |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1185 | } wl_drm_components[] = { |
| 1186 | { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 }, |
| 1187 | { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 }, |
| 1188 | { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 }, |
| 1189 | { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 }, |
| 1190 | { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 }, |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1191 | }; |
| 1192 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1193 | static _EGLImage * |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1194 | dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1195 | EGLClientBuffer _buffer, |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1196 | const EGLint *attr_list) |
| 1197 | { |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1198 | struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer; |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1199 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1200 | const struct wl_drm_components_descriptor *f; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1201 | __DRIimage *dri_image; |
| 1202 | _EGLImageAttribs attrs; |
| 1203 | EGLint err; |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1204 | int32_t plane; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1205 | |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1206 | if (!wayland_buffer_is_drm(&buffer->buffer)) |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1207 | return NULL; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1208 | |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1209 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1210 | plane = attrs.PlaneWL; |
| 1211 | if (err != EGL_SUCCESS) { |
| 1212 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer"); |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | |
| 1216 | f = buffer->driver_format; |
| 1217 | if (plane < 0 || plane >= f->nplanes) { |
| 1218 | _eglError(EGL_BAD_PARAMETER, |
| 1219 | "dri2_create_image_wayland_wl_buffer (plane out of bounds)"); |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1223 | dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL); |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1224 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1225 | if (dri_image == NULL) { |
| 1226 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer"); |
| 1227 | return NULL; |
| 1228 | } |
Robert Bragg | 670f182 | 2012-01-10 22:16:26 +0000 | [diff] [blame] | 1229 | |
Kristian Høgsberg | 5f5746a | 2012-07-05 00:39:25 -0400 | [diff] [blame] | 1230 | return dri2_create_image(disp, dri_image); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1231 | } |
| 1232 | #endif |
| 1233 | |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 1234 | /** |
| 1235 | * Set the error code after a call to |
| 1236 | * dri2_egl_image::dri_image::createImageFromTexture. |
| 1237 | */ |
| 1238 | static void |
| 1239 | dri2_create_image_khr_texture_error(int dri_error) |
| 1240 | { |
| 1241 | EGLint egl_error; |
| 1242 | |
| 1243 | switch (dri_error) { |
| 1244 | case __DRI_IMAGE_ERROR_SUCCESS: |
| 1245 | return; |
| 1246 | |
| 1247 | case __DRI_IMAGE_ERROR_BAD_ALLOC: |
| 1248 | egl_error = EGL_BAD_ALLOC; |
| 1249 | break; |
| 1250 | |
| 1251 | case __DRI_IMAGE_ERROR_BAD_MATCH: |
| 1252 | egl_error = EGL_BAD_MATCH; |
| 1253 | break; |
| 1254 | |
| 1255 | case __DRI_IMAGE_ERROR_BAD_PARAMETER: |
| 1256 | egl_error = EGL_BAD_PARAMETER; |
| 1257 | break; |
| 1258 | |
| 1259 | default: |
| 1260 | assert(0); |
| 1261 | egl_error = EGL_BAD_MATCH; |
| 1262 | break; |
| 1263 | } |
| 1264 | |
| 1265 | _eglError(egl_error, "dri2_create_image_khr_texture"); |
| 1266 | } |
| 1267 | |
| 1268 | static _EGLImage * |
| 1269 | dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx, |
| 1270 | EGLenum target, |
| 1271 | EGLClientBuffer buffer, |
| 1272 | const EGLint *attr_list) |
| 1273 | { |
| 1274 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1275 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1276 | struct dri2_egl_image *dri2_img; |
| 1277 | GLuint texture = (GLuint) (uintptr_t) buffer; |
| 1278 | _EGLImageAttribs attrs; |
| 1279 | GLuint depth; |
| 1280 | GLenum gl_target; |
| 1281 | unsigned error; |
| 1282 | |
| 1283 | if (texture == 0) { |
| 1284 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1285 | return EGL_NO_IMAGE_KHR; |
| 1286 | } |
| 1287 | |
| 1288 | if (_eglParseImageAttribList(&attrs, disp, attr_list) != EGL_SUCCESS) |
| 1289 | return EGL_NO_IMAGE_KHR; |
| 1290 | |
| 1291 | switch (target) { |
| 1292 | case EGL_GL_TEXTURE_2D_KHR: |
| 1293 | depth = 0; |
| 1294 | gl_target = GL_TEXTURE_2D; |
| 1295 | break; |
| 1296 | case EGL_GL_TEXTURE_3D_KHR: |
| 1297 | depth = attrs.GLTextureZOffset; |
| 1298 | gl_target = GL_TEXTURE_3D; |
| 1299 | break; |
| 1300 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR: |
| 1301 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR: |
| 1302 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR: |
| 1303 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR: |
| 1304 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR: |
| 1305 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR: |
| 1306 | depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR; |
| 1307 | gl_target = GL_TEXTURE_CUBE_MAP; |
| 1308 | break; |
| 1309 | default: |
| 1310 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1311 | return EGL_NO_IMAGE_KHR; |
| 1312 | } |
| 1313 | |
| 1314 | dri2_img = malloc(sizeof *dri2_img); |
| 1315 | if (!dri2_img) { |
| 1316 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1317 | return EGL_NO_IMAGE_KHR; |
| 1318 | } |
| 1319 | |
| 1320 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 1321 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1322 | free(dri2_img); |
| 1323 | return EGL_NO_IMAGE_KHR; |
| 1324 | } |
| 1325 | |
| 1326 | dri2_img->dri_image = |
| 1327 | dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context, |
| 1328 | gl_target, |
| 1329 | texture, |
| 1330 | depth, |
| 1331 | attrs.GLTextureLevel, |
| 1332 | &error, |
| 1333 | dri2_img); |
| 1334 | dri2_create_image_khr_texture_error(error); |
| 1335 | |
| 1336 | if (!dri2_img->dri_image) { |
| 1337 | free(dri2_img); |
| 1338 | return EGL_NO_IMAGE_KHR; |
| 1339 | } |
| 1340 | return &dri2_img->base; |
| 1341 | } |
| 1342 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 1343 | _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1344 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 1345 | _EGLContext *ctx, EGLenum target, |
| 1346 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1347 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1348 | (void) drv; |
| 1349 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1350 | switch (target) { |
Abdiel Janulgue | 7b7af48 | 2012-11-27 15:19:54 +0200 | [diff] [blame] | 1351 | case EGL_GL_TEXTURE_2D_KHR: |
| 1352 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR: |
| 1353 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR: |
| 1354 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR: |
| 1355 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR: |
| 1356 | case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR: |
| 1357 | case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR: |
| 1358 | return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list); |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1359 | case EGL_GL_RENDERBUFFER_KHR: |
| 1360 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1361 | case EGL_DRM_BUFFER_MESA: |
| 1362 | return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1363 | #ifdef HAVE_WAYLAND_PLATFORM |
| 1364 | case EGL_WAYLAND_BUFFER_WL: |
| 1365 | return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list); |
| 1366 | #endif |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1367 | default: |
| 1368 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1369 | return EGL_NO_IMAGE_KHR; |
| 1370 | } |
| 1371 | } |
| 1372 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1373 | static EGLBoolean |
| 1374 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 1375 | { |
| 1376 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1377 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 1378 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1379 | (void) drv; |
| 1380 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1381 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 1382 | free(dri2_img); |
| 1383 | |
| 1384 | return EGL_TRUE; |
| 1385 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1386 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1387 | static _EGLImage * |
| 1388 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 1389 | const EGLint *attr_list) |
| 1390 | { |
| 1391 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1392 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1393 | _EGLImageAttribs attrs; |
| 1394 | unsigned int dri_use, valid_mask; |
| 1395 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1396 | EGLint err = EGL_SUCCESS; |
| 1397 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1398 | (void) drv; |
| 1399 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1400 | dri2_img = malloc(sizeof *dri2_img); |
| 1401 | if (!dri2_img) { |
| 1402 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1403 | return EGL_NO_IMAGE_KHR; |
| 1404 | } |
| 1405 | |
| 1406 | if (!attr_list) { |
| 1407 | err = EGL_BAD_PARAMETER; |
| 1408 | goto cleanup_img; |
| 1409 | } |
| 1410 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 1411 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1412 | err = EGL_BAD_PARAMETER; |
| 1413 | goto cleanup_img; |
| 1414 | } |
| 1415 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1416 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1417 | if (err != EGL_SUCCESS) |
| 1418 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1419 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1420 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 1421 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 1422 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1423 | goto cleanup_img; |
| 1424 | } |
| 1425 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1426 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1427 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 1428 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 1429 | break; |
| 1430 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1431 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 1432 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1433 | goto cleanup_img; |
| 1434 | } |
| 1435 | |
| 1436 | valid_mask = |
| 1437 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 1438 | EGL_DRM_BUFFER_USE_SHARE_MESA | |
| 1439 | EGL_DRM_BUFFER_USE_CURSOR_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1440 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 1441 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 1442 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1443 | goto cleanup_img; |
| 1444 | } |
| 1445 | |
| 1446 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1447 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1448 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1449 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1450 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 1451 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) |
| 1452 | dri_use |= __DRI_IMAGE_USE_CURSOR; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1453 | |
| 1454 | dri2_img->dri_image = |
| 1455 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1456 | attrs.Width, attrs.Height, |
| 1457 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1458 | if (dri2_img->dri_image == NULL) { |
| 1459 | err = EGL_BAD_ALLOC; |
| 1460 | goto cleanup_img; |
| 1461 | } |
| 1462 | |
| 1463 | return &dri2_img->base; |
| 1464 | |
| 1465 | cleanup_img: |
| 1466 | free(dri2_img); |
| 1467 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 1468 | |
| 1469 | return EGL_NO_IMAGE_KHR; |
| 1470 | } |
| 1471 | |
| 1472 | static EGLBoolean |
| 1473 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 1474 | EGLint *name, EGLint *handle, EGLint *stride) |
| 1475 | { |
| 1476 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1477 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 1478 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1479 | (void) drv; |
| 1480 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1481 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1482 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 1483 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 1484 | return EGL_FALSE; |
| 1485 | } |
| 1486 | |
| 1487 | if (handle) |
| 1488 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1489 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 1490 | |
| 1491 | if (stride) |
| 1492 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1493 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 1494 | |
| 1495 | return EGL_TRUE; |
| 1496 | } |
| 1497 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1498 | #ifdef HAVE_WAYLAND_PLATFORM |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1499 | |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 1500 | static void |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame^] | 1501 | 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] | 1502 | struct wl_drm_buffer *buffer) |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1503 | { |
| 1504 | _EGLDisplay *disp = user_data; |
| 1505 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1506 | __DRIimage *img; |
| 1507 | int i, dri_components = 0; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1508 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame^] | 1509 | if (fd == -1) |
| 1510 | img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen, |
| 1511 | buffer->buffer.width, |
| 1512 | buffer->buffer.height, |
| 1513 | buffer->format, |
| 1514 | (int*)&name, 1, |
| 1515 | buffer->stride, |
| 1516 | buffer->offset, |
| 1517 | NULL); |
| 1518 | else |
| 1519 | img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen, |
| 1520 | buffer->buffer.width, |
| 1521 | buffer->buffer.height, |
| 1522 | buffer->format, |
| 1523 | &fd, 1, |
| 1524 | buffer->stride, |
| 1525 | buffer->offset, |
| 1526 | NULL); |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1527 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1528 | if (img == NULL) |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 1529 | return; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1530 | |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1531 | dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components); |
| 1532 | |
| 1533 | buffer->driver_format = NULL; |
| 1534 | for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++) |
| 1535 | if (wl_drm_components[i].dri_components == dri_components) |
| 1536 | buffer->driver_format = &wl_drm_components[i]; |
| 1537 | |
| 1538 | if (buffer->driver_format == NULL) |
| 1539 | dri2_dpy->image->destroyImage(img); |
| 1540 | else |
| 1541 | buffer->driver_buffer = img; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static void |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 1545 | dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer) |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1546 | { |
| 1547 | _EGLDisplay *disp = user_data; |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1548 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1549 | |
Kristian Høgsberg | 379eb47 | 2012-07-05 14:19:48 -0400 | [diff] [blame] | 1550 | dri2_dpy->image->destroyImage(buffer->driver_buffer); |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | static struct wayland_drm_callbacks wl_drm_callbacks = { |
| 1554 | .authenticate = NULL, |
| 1555 | .reference_buffer = dri2_wl_reference_buffer, |
| 1556 | .release_buffer = dri2_wl_release_buffer |
| 1557 | }; |
| 1558 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1559 | static EGLBoolean |
| 1560 | dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1561 | struct wl_display *wl_dpy) |
| 1562 | { |
| 1563 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame^] | 1564 | int ret, flags = 0; |
| 1565 | uint64_t cap; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1566 | |
| 1567 | (void) drv; |
| 1568 | |
| 1569 | if (dri2_dpy->wl_server_drm) |
| 1570 | return EGL_FALSE; |
| 1571 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1572 | wl_drm_callbacks.authenticate = |
| 1573 | (int(*)(void *, uint32_t)) dri2_dpy->authenticate; |
| 1574 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame^] | 1575 | ret = drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap); |
| 1576 | if (ret == 0 && cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT)) |
| 1577 | flags |= WAYLAND_DRM_PRIME; |
| 1578 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1579 | dri2_dpy->wl_server_drm = |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1580 | wayland_drm_init(wl_dpy, dri2_dpy->device_name, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame^] | 1581 | &wl_drm_callbacks, disp, flags); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1582 | |
| 1583 | if (!dri2_dpy->wl_server_drm) |
| 1584 | return EGL_FALSE; |
| 1585 | |
| 1586 | return EGL_TRUE; |
| 1587 | } |
| 1588 | |
| 1589 | static EGLBoolean |
| 1590 | dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1591 | struct wl_display *wl_dpy) |
| 1592 | { |
| 1593 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1594 | |
| 1595 | (void) drv; |
| 1596 | |
| 1597 | if (!dri2_dpy->wl_server_drm) |
| 1598 | return EGL_FALSE; |
| 1599 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1600 | wayland_drm_uninit(dri2_dpy->wl_server_drm); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1601 | dri2_dpy->wl_server_drm = NULL; |
| 1602 | |
| 1603 | return EGL_TRUE; |
| 1604 | } |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1605 | |
| 1606 | static EGLBoolean |
| 1607 | dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1608 | struct wl_buffer *_buffer, |
| 1609 | EGLint attribute, EGLint *value) |
| 1610 | { |
| 1611 | struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer; |
Jakob Bornecrantz | 6a7dea9 | 2012-08-31 19:48:26 +0200 | [diff] [blame] | 1612 | const struct wl_drm_components_descriptor *format; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1613 | |
| 1614 | if (!wayland_buffer_is_drm(&buffer->buffer)) |
| 1615 | return EGL_FALSE; |
| 1616 | |
| 1617 | format = buffer->driver_format; |
Kristian Høgsberg | d7522ed | 2012-07-19 09:02:25 -0400 | [diff] [blame] | 1618 | switch (attribute) { |
| 1619 | case EGL_TEXTURE_FORMAT: |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1620 | *value = format->components; |
| 1621 | return EGL_TRUE; |
Kristian Høgsberg | d7522ed | 2012-07-19 09:02:25 -0400 | [diff] [blame] | 1622 | case EGL_WIDTH: |
| 1623 | *value = buffer->buffer.width; |
| 1624 | break; |
| 1625 | case EGL_HEIGHT: |
| 1626 | *value = buffer->buffer.height; |
| 1627 | break; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | return EGL_FALSE; |
| 1631 | } |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1632 | #endif |
| 1633 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1634 | static void |
| 1635 | dri2_unload(_EGLDriver *drv) |
| 1636 | { |
| 1637 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1638 | |
| 1639 | if (dri2_drv->handle) |
| 1640 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1641 | free(dri2_drv); |
| 1642 | } |
| 1643 | |
| 1644 | static EGLBoolean |
| 1645 | dri2_load(_EGLDriver *drv) |
| 1646 | { |
| 1647 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Matt Turner | 027ce0c | 2012-01-24 11:34:42 -0500 | [diff] [blame] | 1648 | #ifdef HAVE_SHARED_GLAPI |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1649 | #ifdef HAVE_ANDROID_PLATFORM |
| 1650 | const char *libname = "libglapi.so"; |
| 1651 | #else |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1652 | const char *libname = "libglapi.so.0"; |
Chia-I Wu | 9779f6f | 2011-08-05 14:39:18 +0900 | [diff] [blame] | 1653 | #endif |
Matt Turner | 027ce0c | 2012-01-24 11:34:42 -0500 | [diff] [blame] | 1654 | #else |
| 1655 | /* |
| 1656 | * Both libGL.so and libglapi.so are glapi providers. There is no way to |
| 1657 | * tell which one to load. |
| 1658 | */ |
| 1659 | const char *libname = NULL; |
| 1660 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1661 | void *handle; |
| 1662 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1663 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 1664 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1665 | if (handle) { |
| 1666 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 1667 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1668 | if (!dri2_drv->get_proc_address || !libname) { |
| 1669 | /* no need to keep a reference */ |
| 1670 | dlclose(handle); |
| 1671 | handle = NULL; |
| 1672 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1673 | } |
| 1674 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1675 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1676 | if (!dri2_drv->get_proc_address) { |
| 1677 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 1678 | return EGL_FALSE; |
| 1679 | } |
| 1680 | |
| 1681 | dri2_drv->glFlush = (void (*)(void)) |
| 1682 | dri2_drv->get_proc_address("glFlush"); |
| 1683 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1684 | dri2_drv->handle = handle; |
| 1685 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1686 | return EGL_TRUE; |
| 1687 | } |
| 1688 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1689 | /** |
| 1690 | * This is the main entrypoint into the driver, called by libEGL. |
| 1691 | * Create a new _EGLDriver object and init its dispatch table. |
| 1692 | */ |
| 1693 | _EGLDriver * |
Eric Anholt | 9f0f00c | 2012-02-05 07:01:17 +0100 | [diff] [blame] | 1694 | _eglBuiltInDriverDRI2(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1695 | { |
| 1696 | struct dri2_egl_driver *dri2_drv; |
| 1697 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1698 | (void) args; |
| 1699 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 1700 | dri2_drv = calloc(1, sizeof *dri2_drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1701 | if (!dri2_drv) |
| 1702 | return NULL; |
| 1703 | |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 1704 | if (!dri2_load(&dri2_drv->base)) { |
| 1705 | free(dri2_drv); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1706 | return NULL; |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 1707 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1708 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1709 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1710 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1711 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1712 | dri2_drv->base.API.CreateContext = dri2_create_context; |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame] | 1713 | dri2_drv->base.API.DestroyContext = dri2_destroy_context; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1714 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1715 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1716 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1717 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1718 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1719 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1720 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1721 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1722 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 1723 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1724 | #ifdef HAVE_WAYLAND_PLATFORM |
| 1725 | dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl; |
| 1726 | dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl; |
Kristian Høgsberg | e6a3357 | 2012-07-05 16:43:04 -0400 | [diff] [blame] | 1727 | dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1728 | #endif |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1729 | |
| 1730 | dri2_drv->base.Name = "DRI2"; |
| 1731 | dri2_drv->base.Unload = dri2_unload; |
| 1732 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1733 | return &dri2_drv->base; |
| 1734 | } |