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