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