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