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