Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 19 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Kristian Høgsberg <krh@bitplanet.net> |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <limits.h> |
| 32 | #include <dlfcn.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
| 35 | #include <unistd.h> |
| 36 | #include <xf86drm.h> |
| 37 | #include <GL/gl.h> |
| 38 | #include <GL/internal/dri_interface.h> |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 42 | #include "egl_dri2.h" |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 43 | |
Kristian Høgsberg | 9dc5de5 | 2011-02-02 22:21:13 -0500 | [diff] [blame] | 44 | const __DRIuseInvalidateExtension use_invalidate = { |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 45 | { __DRI_USE_INVALIDATE, 1 } |
| 46 | }; |
| 47 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 48 | EGLint dri2_to_egl_attribute_map[] = { |
| 49 | 0, |
| 50 | EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */ |
| 51 | EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */ |
| 52 | EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */ |
| 53 | EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */ |
| 54 | EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 55 | EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 56 | EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */ |
| 57 | 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */ |
| 58 | EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */ |
| 59 | EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */ |
| 60 | 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */ |
| 61 | 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */ |
| 62 | 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */ |
| 63 | 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */ |
| 64 | EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */ |
| 65 | EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */ |
| 66 | 0, /* __DRI_ATTRIB_RENDER_TYPE, */ |
| 67 | 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */ |
| 68 | 0, /* __DRI_ATTRIB_CONFORMANT */ |
| 69 | 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */ |
| 70 | 0, /* __DRI_ATTRIB_STEREO */ |
| 71 | 0, /* __DRI_ATTRIB_AUX_BUFFERS */ |
| 72 | 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */ |
| 73 | 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */ |
| 74 | 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */ |
| 75 | 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */ |
| 76 | 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */ |
| 77 | 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */ |
| 78 | 0, /* __DRI_ATTRIB_FLOAT_MODE */ |
| 79 | 0, /* __DRI_ATTRIB_RED_MASK */ |
| 80 | 0, /* __DRI_ATTRIB_GREEN_MASK */ |
| 81 | 0, /* __DRI_ATTRIB_BLUE_MASK */ |
| 82 | 0, /* __DRI_ATTRIB_ALPHA_MASK */ |
| 83 | EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */ |
| 84 | EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */ |
| 85 | EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */ |
| 86 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */ |
| 87 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */ |
| 88 | 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */ |
| 89 | 0, /* __DRI_ATTRIB_SWAP_METHOD */ |
| 90 | EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */ |
| 91 | EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 92 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */ |
| 93 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 94 | 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */ |
| 95 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */ |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 96 | EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */ |
Benjamin Franzke | 4f6fbfa | 2011-03-07 00:29:13 +0100 | [diff] [blame] | 97 | 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 98 | }; |
| 99 | |
Benjamin Franzke | 16e3027 | 2011-06-11 23:00:40 +0200 | [diff] [blame] | 100 | static EGLBoolean |
| 101 | dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria) |
| 102 | { |
| 103 | if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0) |
| 104 | return EGL_FALSE; |
| 105 | |
| 106 | if (!_eglMatchConfig(conf, criteria)) |
| 107 | return EGL_FALSE; |
| 108 | |
| 109 | return EGL_TRUE; |
| 110 | } |
| 111 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 112 | struct dri2_egl_config * |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 113 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id, |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 114 | int depth, EGLint surface_type, const EGLint *attr_list) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 115 | { |
| 116 | struct dri2_egl_config *conf; |
| 117 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 118 | _EGLConfig base; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 119 | unsigned int attrib, value, double_buffer; |
| 120 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 121 | _EGLConfig *matching_config; |
| 122 | EGLint num_configs = 0; |
| 123 | EGLint config_id; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 124 | int i; |
| 125 | |
| 126 | dri2_dpy = disp->DriverData; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 127 | _eglInitConfig(&base, disp, id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 128 | |
| 129 | i = 0; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 130 | double_buffer = 0; |
| 131 | bind_to_texture_rgb = 0; |
| 132 | bind_to_texture_rgba = 0; |
| 133 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 134 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 135 | switch (attrib) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 136 | case __DRI_ATTRIB_RENDER_TYPE: |
| 137 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 138 | value = EGL_RGB_BUFFER; |
| 139 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 140 | value = EGL_LUMINANCE_BUFFER; |
| 141 | else |
| 142 | /* not valid */; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 143 | _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 147 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 148 | value = EGL_NON_CONFORMANT_CONFIG; |
| 149 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 150 | value = EGL_SLOW_CONFIG; |
| 151 | else |
| 152 | value = EGL_NONE; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 153 | _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 154 | break; |
| 155 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 156 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 157 | bind_to_texture_rgb = value; |
| 158 | break; |
| 159 | |
| 160 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 161 | bind_to_texture_rgba = value; |
| 162 | break; |
| 163 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 164 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 165 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | default: |
| 169 | key = dri2_to_egl_attribute_map[attrib]; |
| 170 | if (key != 0) |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 171 | _eglSetConfigKey(&base, key, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 176 | if (attr_list) |
| 177 | for (i = 0; attr_list[i] != EGL_NONE; i += 2) |
| 178 | _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 179 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 180 | if (depth > 0 && depth != base.BufferSize) |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 181 | return NULL; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 182 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 183 | base.NativeRenderable = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 184 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 185 | base.SurfaceType = surface_type; |
Benjamin Franzke | a8128d7 | 2011-02-04 12:32:30 +0100 | [diff] [blame] | 186 | if (surface_type & (EGL_PBUFFER_BIT | |
| 187 | (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) { |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 188 | base.BindToTextureRGB = bind_to_texture_rgb; |
| 189 | if (base.AlphaSize > 0) |
| 190 | base.BindToTextureRGBA = bind_to_texture_rgba; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 191 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 192 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 193 | base.RenderableType = disp->ClientAPIs; |
| 194 | base.Conformant = disp->ClientAPIs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 195 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 196 | if (!_eglValidateConfig(&base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 197 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 198 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 201 | config_id = base.ConfigID; |
| 202 | base.ConfigID = EGL_DONT_CARE; |
| 203 | base.SurfaceType = EGL_DONT_CARE; |
| 204 | num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1, |
Benjamin Franzke | 16e3027 | 2011-06-11 23:00:40 +0200 | [diff] [blame] | 205 | (_EGLArrayForEach) dri2_match_config, &base); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 206 | |
| 207 | if (num_configs == 1) { |
| 208 | conf = (struct dri2_egl_config *) matching_config; |
| 209 | |
| 210 | if (double_buffer && !conf->dri_double_config) |
| 211 | conf->dri_double_config = dri_config; |
| 212 | else if (!double_buffer && !conf->dri_single_config) |
| 213 | conf->dri_single_config = dri_config; |
| 214 | else |
Benjamin Franzke | 83c6875 | 2011-05-12 19:30:05 +0200 | [diff] [blame] | 215 | /* a similar config type is already added (unlikely) => discard */ |
| 216 | return NULL; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 217 | } |
José Fonseca | 4a47d6d | 2011-05-12 15:23:48 +0100 | [diff] [blame] | 218 | else if (num_configs == 0) { |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 219 | conf = malloc(sizeof *conf); |
| 220 | if (conf == NULL) |
| 221 | return NULL; |
| 222 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 223 | memcpy(&conf->base, &base, sizeof base); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 224 | if (double_buffer) { |
| 225 | conf->dri_double_config = dri_config; |
| 226 | conf->dri_single_config = NULL; |
| 227 | } else { |
| 228 | conf->dri_single_config = dri_config; |
| 229 | conf->dri_double_config = NULL; |
| 230 | } |
| 231 | conf->base.SurfaceType = 0; |
| 232 | conf->base.ConfigID = config_id; |
| 233 | |
Chia-I Wu | 8a6bdf3 | 2010-10-23 00:47:22 +0800 | [diff] [blame] | 234 | _eglLinkConfig(&conf->base); |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 235 | } |
José Fonseca | 4a47d6d | 2011-05-12 15:23:48 +0100 | [diff] [blame] | 236 | else { |
| 237 | assert(0); |
| 238 | return NULL; |
| 239 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 240 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 241 | conf->base.SurfaceType |= surface_type & (!double_buffer ? EGL_PIXMAP_BIT: |
| 242 | (EGL_WINDOW_BIT | EGL_PBUFFER_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT)); |
| 243 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 244 | return conf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 247 | __DRIimage * |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 248 | dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 249 | { |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 250 | _EGLDisplay *disp = data; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 251 | struct dri2_egl_image *dri2_img; |
| 252 | _EGLImage *img; |
| 253 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 254 | (void) screen; |
| 255 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 256 | img = _eglLookupImage(image, disp); |
| 257 | if (img == NULL) { |
| 258 | _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image"); |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | dri2_img = dri2_egl_image(image); |
| 263 | |
| 264 | return dri2_img->dri_image; |
| 265 | } |
| 266 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 267 | const __DRIimageLookupExtension image_lookup_extension = { |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 268 | { __DRI_IMAGE_LOOKUP, 1 }, |
| 269 | dri2_lookup_egl_image |
| 270 | }; |
| 271 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 272 | static const char dri_driver_path[] = DEFAULT_DRIVER_DIR; |
| 273 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 274 | struct dri2_extension_match { |
| 275 | const char *name; |
| 276 | int version; |
| 277 | int offset; |
| 278 | }; |
| 279 | |
| 280 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 281 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 282 | { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 283 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 287 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 288 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 289 | { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 290 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 291 | }; |
| 292 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 293 | static struct dri2_extension_match swrast_driver_extensions[] = { |
| 294 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 295 | { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) }, |
| 296 | { NULL } |
| 297 | }; |
| 298 | |
| 299 | static struct dri2_extension_match swrast_core_extensions[] = { |
| 300 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
| 301 | { NULL } |
| 302 | }; |
| 303 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 304 | static EGLBoolean |
| 305 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 306 | struct dri2_extension_match *matches, |
| 307 | const __DRIextension **extensions) |
| 308 | { |
| 309 | int i, j, ret = EGL_TRUE; |
| 310 | void *field; |
| 311 | |
| 312 | for (i = 0; extensions[i]; i++) { |
| 313 | _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name); |
| 314 | for (j = 0; matches[j].name; j++) { |
| 315 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 316 | extensions[i]->version >= matches[j].version) { |
| 317 | field = ((char *) dri2_dpy + matches[j].offset); |
| 318 | *(const __DRIextension **) field = extensions[i]; |
| 319 | _eglLog(_EGL_INFO, "DRI2: found extension %s version %d", |
| 320 | extensions[i]->name, extensions[i]->version); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | for (j = 0; matches[j].name; j++) { |
| 326 | field = ((char *) dri2_dpy + matches[j].offset); |
| 327 | if (*(const __DRIextension **) field == NULL) { |
| 328 | _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d", |
| 329 | matches[j].name, matches[j].version); |
| 330 | ret = EGL_FALSE; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 337 | static const __DRIextension ** |
| 338 | dri2_open_driver(_EGLDisplay *disp) |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 339 | { |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 340 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 341 | const __DRIextension **extensions; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 342 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 343 | |
| 344 | search_paths = NULL; |
| 345 | if (geteuid() == getuid()) { |
| 346 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 347 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 348 | } |
| 349 | if (search_paths == NULL) |
| 350 | search_paths = DEFAULT_DRIVER_DIR; |
| 351 | |
| 352 | dri2_dpy->driver = NULL; |
| 353 | end = search_paths + strlen(search_paths); |
| 354 | 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] | 355 | int len; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 356 | next = strchr(p, ':'); |
| 357 | if (next == NULL) |
| 358 | next = end; |
| 359 | |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 360 | len = next - p; |
| 361 | #if GLX_USE_TLS |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 362 | snprintf(path, sizeof path, |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 363 | "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 364 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 365 | #endif |
| 366 | if (dri2_dpy->driver == NULL) { |
| 367 | snprintf(path, sizeof path, |
| 368 | "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name); |
| 369 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 370 | if (dri2_dpy->driver == NULL) |
| 371 | _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror()); |
| 372 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | if (dri2_dpy->driver == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 376 | _eglLog(_EGL_WARNING, |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 377 | "DRI2: failed to open %s (search paths %s)", |
| 378 | dri2_dpy->driver_name, search_paths); |
| 379 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
| 383 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
| 384 | if (extensions == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 385 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 386 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 387 | dlclose(dri2_dpy->driver); |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return extensions; |
| 391 | } |
| 392 | |
| 393 | EGLBoolean |
| 394 | dri2_load_driver(_EGLDisplay *disp) |
| 395 | { |
| 396 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 397 | const __DRIextension **extensions; |
| 398 | |
| 399 | extensions = dri2_open_driver(disp); |
| 400 | if (!extensions) |
| 401 | return EGL_FALSE; |
| 402 | |
| 403 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) { |
| 404 | dlclose(dri2_dpy->driver); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 405 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 408 | return EGL_TRUE; |
| 409 | } |
| 410 | |
| 411 | EGLBoolean |
| 412 | dri2_load_driver_swrast(_EGLDisplay *disp) |
| 413 | { |
| 414 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
| 415 | const __DRIextension **extensions; |
| 416 | |
| 417 | dri2_dpy->driver_name = "swrast"; |
| 418 | extensions = dri2_open_driver(disp); |
Chia-I Wu | 77e031a | 2011-06-08 16:33:56 +0800 | [diff] [blame] | 419 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 420 | if (!extensions) |
| 421 | return EGL_FALSE; |
| 422 | |
| 423 | if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) { |
| 424 | dlclose(dri2_dpy->driver); |
| 425 | return EGL_FALSE; |
| 426 | } |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 427 | |
| 428 | return EGL_TRUE; |
| 429 | } |
| 430 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 431 | void |
| 432 | dri2_setup_screen(_EGLDisplay *disp) |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 433 | { |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 434 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 435 | unsigned int api_mask; |
| 436 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 437 | if (dri2_dpy->dri2) { |
| 438 | if (dri2_dpy->dri2->base.version >= 2) |
| 439 | api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen); |
| 440 | else |
| 441 | api_mask = 1 << __DRI_API_OPENGL; |
| 442 | } else { |
| 443 | assert(dri2_dpy->swrast); |
| 444 | if (dri2_dpy->swrast->base.version >= 2) |
| 445 | api_mask = 1 << __DRI_API_OPENGL | 1 << __DRI_API_GLES | 1 << __DRI_API_GLES2; |
| 446 | else |
| 447 | api_mask = 1 << __DRI_API_OPENGL; |
| 448 | } |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 449 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 450 | disp->ClientAPIs = 0; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 451 | if (api_mask & (1 <<__DRI_API_OPENGL)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 452 | disp->ClientAPIs |= EGL_OPENGL_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 453 | if (api_mask & (1 <<__DRI_API_GLES)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 454 | disp->ClientAPIs |= EGL_OPENGL_ES_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 455 | if (api_mask & (1 << __DRI_API_GLES2)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 456 | disp->ClientAPIs |= EGL_OPENGL_ES2_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 457 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 458 | if (dri2_dpy->dri2) { |
| 459 | if (dri2_dpy->dri2->base.version >= 2) { |
| 460 | disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE; |
| 461 | disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE; |
| 462 | disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE; |
| 463 | } |
| 464 | } else { |
| 465 | assert(dri2_dpy->swrast); |
| 466 | if (dri2_dpy->swrast->base.version >= 2) { |
| 467 | disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE; |
| 468 | disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE; |
| 469 | disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE; |
| 470 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 473 | if (dri2_dpy->image) { |
| 474 | disp->Extensions.MESA_drm_image = EGL_TRUE; |
| 475 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 476 | disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE; |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 477 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | EGLBoolean |
| 481 | dri2_create_screen(_EGLDisplay *disp) |
| 482 | { |
| 483 | const __DRIextension **extensions; |
| 484 | struct dri2_egl_display *dri2_dpy; |
| 485 | |
| 486 | dri2_dpy = disp->DriverData; |
| 487 | |
| 488 | if (dri2_dpy->dri2) { |
| 489 | dri2_dpy->dri_screen = |
| 490 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
| 491 | &dri2_dpy->driver_configs, disp); |
| 492 | } else { |
| 493 | assert(dri2_dpy->swrast); |
| 494 | dri2_dpy->dri_screen = |
| 495 | dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions, |
| 496 | &dri2_dpy->driver_configs, disp); |
| 497 | } |
| 498 | |
| 499 | if (dri2_dpy->dri_screen == NULL) { |
| 500 | _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen"); |
| 501 | return EGL_FALSE; |
| 502 | } |
| 503 | |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 504 | dri2_dpy->own_dri_screen = 1; |
| 505 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame] | 506 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
| 507 | |
| 508 | if (dri2_dpy->dri2) { |
| 509 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
| 510 | goto cleanup_dri_screen; |
| 511 | } else { |
| 512 | assert(dri2_dpy->swrast); |
| 513 | if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions)) |
| 514 | goto cleanup_dri_screen; |
| 515 | } |
| 516 | |
| 517 | dri2_setup_screen(disp); |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 518 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 519 | return EGL_TRUE; |
| 520 | |
| 521 | cleanup_dri_screen: |
| 522 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 523 | |
| 524 | return EGL_FALSE; |
| 525 | } |
| 526 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 527 | /** |
| 528 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 529 | */ |
| 530 | static EGLBoolean |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 531 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp) |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 532 | { |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 533 | /* not until swrast_dri is supported */ |
| 534 | if (disp->Options.UseFallback) |
| 535 | return EGL_FALSE; |
| 536 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 537 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 538 | #ifdef HAVE_X11_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 539 | case _EGL_PLATFORM_X11: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 540 | if (disp->Options.TestOnly) |
| 541 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 542 | return dri2_initialize_x11(drv, disp); |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 543 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 544 | |
| 545 | #ifdef HAVE_LIBUDEV |
Benjamin Franzke | 629c15a | 2011-06-24 21:45:05 +0200 | [diff] [blame] | 546 | #ifdef HAVE_DRM_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 547 | case _EGL_PLATFORM_DRM: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 548 | if (disp->Options.TestOnly) |
| 549 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 550 | return dri2_initialize_drm(drv, disp); |
Benjamin Franzke | 629c15a | 2011-06-24 21:45:05 +0200 | [diff] [blame] | 551 | #endif |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 552 | #ifdef HAVE_WAYLAND_PLATFORM |
| 553 | case _EGL_PLATFORM_WAYLAND: |
| 554 | if (disp->Options.TestOnly) |
| 555 | return EGL_TRUE; |
| 556 | return dri2_initialize_wayland(drv, disp); |
| 557 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 558 | #endif |
| 559 | |
| 560 | default: |
| 561 | return EGL_FALSE; |
| 562 | } |
| 563 | } |
| 564 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 565 | /** |
| 566 | * Called via eglTerminate(), drv->API.Terminate(). |
| 567 | */ |
| 568 | static EGLBoolean |
| 569 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 570 | { |
| 571 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 572 | |
| 573 | _eglReleaseDisplayResources(drv, disp); |
| 574 | _eglCleanupDisplay(disp); |
| 575 | |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 576 | if (dri2_dpy->own_dri_screen) |
| 577 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 578 | if (dri2_dpy->fd) |
| 579 | close(dri2_dpy->fd); |
Benjamin Franzke | 992680c | 2011-06-27 10:23:34 +0200 | [diff] [blame] | 580 | if (dri2_dpy->driver) |
| 581 | dlclose(dri2_dpy->driver); |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 582 | |
| 583 | if (disp->PlatformDisplay == NULL) { |
| 584 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 585 | #ifdef HAVE_X11_PLATFORM |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 586 | case _EGL_PLATFORM_X11: |
| 587 | xcb_disconnect(dri2_dpy->conn); |
| 588 | break; |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 589 | #endif |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 590 | #ifdef HAVE_WAYLAND_PLATFORM |
| 591 | case _EGL_PLATFORM_WAYLAND: |
| 592 | wl_display_destroy(dri2_dpy->wl_dpy); |
| 593 | break; |
| 594 | #endif |
Benjamin Franzke | 32f4cf3 | 2011-06-29 08:49:39 +0200 | [diff] [blame] | 595 | #ifdef HAVE_DRM_PLATFORM |
| 596 | case _EGL_PLATFORM_DRM: |
| 597 | if (dri2_dpy->own_gbm_device) { |
| 598 | gbm_device_destroy(&dri2_dpy->gbm_dri->base.base); |
| 599 | } |
| 600 | break; |
| 601 | #endif |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 602 | default: |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 607 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 608 | disp->DriverData = NULL; |
| 609 | |
| 610 | return EGL_TRUE; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | /** |
| 615 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 616 | */ |
| 617 | static _EGLContext * |
| 618 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 619 | _EGLContext *share_list, const EGLint *attrib_list) |
| 620 | { |
| 621 | struct dri2_egl_context *dri2_ctx; |
| 622 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 623 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 624 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 625 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 626 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 627 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 628 | (void) drv; |
| 629 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 630 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 631 | if (!dri2_ctx) { |
| 632 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 633 | return NULL; |
| 634 | } |
| 635 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 636 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 637 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 638 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 639 | switch (dri2_ctx->base.ClientAPI) { |
| 640 | case EGL_OPENGL_ES_API: |
| 641 | switch (dri2_ctx->base.ClientVersion) { |
| 642 | case 1: |
| 643 | api = __DRI_API_GLES; |
| 644 | break; |
| 645 | case 2: |
| 646 | api = __DRI_API_GLES2; |
| 647 | break; |
| 648 | default: |
| 649 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 650 | return NULL; |
| 651 | } |
| 652 | break; |
| 653 | case EGL_OPENGL_API: |
| 654 | api = __DRI_API_OPENGL; |
| 655 | break; |
| 656 | default: |
| 657 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 661 | if (conf != NULL) { |
| 662 | /* The config chosen here isn't necessarily |
| 663 | * used for surfaces later. |
| 664 | * A pixmap surface will use the single config. |
| 665 | * This opportunity depends on disabling the |
| 666 | * doubleBufferMode check in |
| 667 | * src/mesa/main/context.c:check_compatible() |
| 668 | */ |
| 669 | if (dri2_config->dri_double_config) |
| 670 | dri_config = dri2_config->dri_double_config; |
| 671 | else |
| 672 | dri_config = dri2_config->dri_single_config; |
| 673 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 674 | else |
| 675 | dri_config = NULL; |
| 676 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 677 | if (dri2_dpy->dri2) { |
| 678 | if (dri2_dpy->dri2->base.version >= 2) { |
| 679 | dri2_ctx->dri_context = |
| 680 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 681 | api, |
| 682 | dri_config, |
| 683 | dri2_ctx_shared ? |
| 684 | dri2_ctx_shared->dri_context : NULL, |
| 685 | dri2_ctx); |
| 686 | } else if (api == __DRI_API_OPENGL) { |
| 687 | dri2_ctx->dri_context = |
| 688 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
| 689 | dri_config, |
| 690 | dri2_ctx_shared ? |
| 691 | dri2_ctx_shared->dri_context : NULL, |
| 692 | dri2_ctx); |
| 693 | } else { |
| 694 | /* fail */ |
| 695 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 696 | } else { |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 697 | assert(dri2_dpy->swrast); |
| 698 | if (dri2_dpy->swrast->base.version >= 2) { |
| 699 | dri2_ctx->dri_context = |
| 700 | dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen, |
| 701 | api, |
| 702 | dri_config, |
| 703 | dri2_ctx_shared ? |
| 704 | dri2_ctx_shared->dri_context : NULL, |
| 705 | dri2_ctx); |
| 706 | } else if (api == __DRI_API_OPENGL) { |
| 707 | dri2_ctx->dri_context = |
| 708 | dri2_dpy->core->createNewContext(dri2_dpy->dri_screen, |
| 709 | dri_config, |
| 710 | dri2_ctx_shared ? |
| 711 | dri2_ctx_shared->dri_context : NULL, |
| 712 | dri2_ctx); |
| 713 | } else { |
| 714 | /* fail */ |
| 715 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 716 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 717 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 718 | if (!dri2_ctx->dri_context) |
| 719 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 720 | |
| 721 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 722 | |
| 723 | cleanup: |
| 724 | free(dri2_ctx); |
| 725 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 726 | } |
| 727 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 728 | /** |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame^] | 729 | * Called via eglDestroyContext(), drv->API.DestroyContext(). |
| 730 | */ |
| 731 | static EGLBoolean |
| 732 | dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
| 733 | { |
| 734 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 735 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 736 | |
| 737 | if (_eglPutContext(ctx)) { |
| 738 | dri2_dpy->core->destroyContext(dri2_ctx->dri_context); |
| 739 | free(dri2_ctx); |
| 740 | } |
| 741 | |
| 742 | return EGL_TRUE; |
| 743 | } |
| 744 | |
| 745 | /** |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 746 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 747 | */ |
| 748 | static EGLBoolean |
| 749 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 750 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 751 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 752 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 753 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 754 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 755 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 756 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 757 | _EGLContext *old_ctx; |
| 758 | _EGLSurface *old_dsurf, *old_rsurf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 759 | __DRIdrawable *ddraw, *rdraw; |
| 760 | __DRIcontext *cctx; |
| 761 | |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 762 | /* make new bindings */ |
| 763 | if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 764 | return EGL_FALSE; |
| 765 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 766 | /* flush before context switch */ |
Chia-I Wu | 547e761 | 2010-10-26 15:00:56 +0800 | [diff] [blame] | 767 | if (old_ctx && dri2_drv->glFlush) |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 768 | dri2_drv->glFlush(); |
| 769 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 770 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 771 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 772 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 773 | |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 774 | if (old_ctx) { |
| 775 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 776 | dri2_dpy->core->unbindContext(old_cctx); |
| 777 | } |
| 778 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 779 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 780 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 781 | if (old_dsurf) |
| 782 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 783 | if (old_rsurf) |
| 784 | drv->API.DestroySurface(drv, disp, old_rsurf); |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 785 | if (old_ctx) |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame^] | 786 | drv->API.DestroyContext(drv, disp, old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 787 | |
| 788 | return EGL_TRUE; |
| 789 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 790 | /* undo the previous _eglBindContext */ |
| 791 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf); |
| 792 | assert(&dri2_ctx->base == ctx && |
| 793 | &dri2_dsurf->base == dsurf && |
| 794 | &dri2_rsurf->base == rsurf); |
| 795 | |
| 796 | _eglPutSurface(dsurf); |
| 797 | _eglPutSurface(rsurf); |
| 798 | _eglPutContext(ctx); |
| 799 | |
| 800 | _eglPutSurface(old_dsurf); |
| 801 | _eglPutSurface(old_rsurf); |
| 802 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 803 | |
| 804 | return EGL_FALSE; |
| 805 | } |
| 806 | } |
| 807 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 808 | /* |
| 809 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 810 | */ |
| 811 | static _EGLProc |
| 812 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 813 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 814 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 815 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 816 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 820 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 821 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 822 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 823 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 824 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 825 | (void) drv; |
| 826 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 827 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 828 | * we need to copy fake to real here.*/ |
| 829 | |
| 830 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 831 | |
| 832 | return EGL_TRUE; |
| 833 | } |
| 834 | |
| 835 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 836 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 837 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 838 | (void) drv; |
| 839 | (void) disp; |
| 840 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 841 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 842 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 843 | /* glXWaitX(); */ |
| 844 | |
| 845 | return EGL_TRUE; |
| 846 | } |
| 847 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 848 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 849 | dri2_bind_tex_image(_EGLDriver *drv, |
| 850 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 851 | { |
| 852 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 853 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 854 | struct dri2_egl_context *dri2_ctx; |
| 855 | _EGLContext *ctx; |
| 856 | GLint format, target; |
| 857 | |
| 858 | ctx = _eglGetCurrentContext(); |
| 859 | dri2_ctx = dri2_egl_context(ctx); |
| 860 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 861 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 862 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 863 | |
| 864 | switch (dri2_surf->base.TextureFormat) { |
| 865 | case EGL_TEXTURE_RGB: |
| 866 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 867 | break; |
| 868 | case EGL_TEXTURE_RGBA: |
| 869 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 870 | break; |
| 871 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 872 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | switch (dri2_surf->base.TextureTarget) { |
| 876 | case EGL_TEXTURE_2D: |
| 877 | target = GL_TEXTURE_2D; |
| 878 | break; |
| 879 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 880 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 884 | target, format, |
| 885 | dri2_surf->dri_drawable); |
| 886 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 887 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static EGLBoolean |
| 891 | dri2_release_tex_image(_EGLDriver *drv, |
| 892 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 893 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 894 | #if __DRI_TEX_BUFFER_VERSION >= 3 |
| 895 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 896 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 897 | struct dri2_egl_context *dri2_ctx; |
| 898 | _EGLContext *ctx; |
| 899 | GLint target; |
| 900 | |
| 901 | ctx = _eglGetCurrentContext(); |
| 902 | dri2_ctx = dri2_egl_context(ctx); |
| 903 | |
| 904 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 905 | return EGL_FALSE; |
| 906 | |
| 907 | switch (dri2_surf->base.TextureTarget) { |
| 908 | case EGL_TEXTURE_2D: |
| 909 | target = GL_TEXTURE_2D; |
| 910 | break; |
| 911 | default: |
| 912 | assert(0); |
| 913 | } |
| 914 | if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL) |
| 915 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 916 | target, |
| 917 | dri2_surf->dri_drawable); |
| 918 | #endif |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 919 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 920 | return EGL_TRUE; |
| 921 | } |
| 922 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 923 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 924 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 925 | EGLClientBuffer buffer, |
| 926 | const EGLint *attr_list) |
| 927 | { |
| 928 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 929 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 930 | struct dri2_egl_image *dri2_img; |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 931 | GLuint renderbuffer = (GLuint) (uintptr_t) buffer; |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 932 | |
| 933 | if (renderbuffer == 0) { |
| 934 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 935 | return EGL_NO_IMAGE_KHR; |
| 936 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 937 | |
| 938 | dri2_img = malloc(sizeof *dri2_img); |
| 939 | if (!dri2_img) { |
| 940 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 941 | return EGL_NO_IMAGE_KHR; |
| 942 | } |
| 943 | |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 944 | if (!_eglInitImage(&dri2_img->base, disp)) { |
| 945 | free(dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 946 | return EGL_NO_IMAGE_KHR; |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 947 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 948 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 949 | dri2_img->dri_image = |
| 950 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
| 951 | renderbuffer, |
| 952 | dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 953 | |
| 954 | return &dri2_img->base; |
| 955 | } |
| 956 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 957 | static _EGLImage * |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 958 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 959 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 960 | { |
| 961 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 962 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 963 | EGLint format, name, pitch, err; |
| 964 | _EGLImageAttribs attrs; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 965 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 966 | (void) ctx; |
| 967 | |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 968 | name = (EGLint) (uintptr_t) buffer; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 969 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 970 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 971 | if (err != EGL_SUCCESS) |
| 972 | return NULL; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 973 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 974 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 975 | attrs.DRMBufferStrideMESA <= 0) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 976 | _eglError(EGL_BAD_PARAMETER, |
| 977 | "bad width, height or stride"); |
| 978 | return NULL; |
| 979 | } |
| 980 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 981 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 982 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 983 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 984 | pitch = attrs.DRMBufferStrideMESA; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 985 | break; |
| 986 | default: |
| 987 | _eglError(EGL_BAD_PARAMETER, |
| 988 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 989 | return NULL; |
| 990 | } |
| 991 | |
| 992 | dri2_img = malloc(sizeof *dri2_img); |
| 993 | if (!dri2_img) { |
| 994 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 995 | return NULL; |
| 996 | } |
| 997 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 998 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 999 | free(dri2_img); |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
| 1003 | dri2_img->dri_image = |
Kristian Høgsberg | 9ec0b2a | 2010-09-22 15:07:15 -0400 | [diff] [blame] | 1004 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1005 | attrs.Width, |
| 1006 | attrs.Height, |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1007 | format, |
| 1008 | name, |
| 1009 | pitch, |
| 1010 | dri2_img); |
| 1011 | if (dri2_img->dri_image == NULL) { |
| 1012 | free(dri2_img); |
| 1013 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | return &dri2_img->base; |
| 1018 | } |
| 1019 | |
José Fonseca | 5de2c67 | 2011-05-12 15:24:26 +0100 | [diff] [blame] | 1020 | #ifdef HAVE_WAYLAND_PLATFORM |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1021 | static _EGLImage * |
| 1022 | dri2_reference_drm_image(_EGLDisplay *disp, _EGLContext *ctx, |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1023 | __DRIimage *dri_image, EGLint width, EGLint height) |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1024 | { |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1025 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1026 | EGLint attr_list[] = { |
| 1027 | EGL_WIDTH, 0, |
| 1028 | EGL_HEIGHT, 0, |
| 1029 | EGL_DRM_BUFFER_STRIDE_MESA, 0, |
| 1030 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
| 1031 | EGL_NONE |
| 1032 | }; |
| 1033 | EGLint name, stride; |
| 1034 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1035 | dri2_dpy->image->queryImage(dri_image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 1036 | dri2_dpy->image->queryImage(dri_image, __DRI_IMAGE_ATTRIB_STRIDE, &stride); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1037 | |
| 1038 | attr_list[1] = width; |
| 1039 | attr_list[3] = height; |
| 1040 | attr_list[5] = stride / 4; |
| 1041 | |
| 1042 | return dri2_create_image_mesa_drm_buffer(disp, ctx, |
| 1043 | (EGLClientBuffer)(intptr_t) name, |
| 1044 | attr_list); |
| 1045 | } |
| 1046 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1047 | static _EGLImage * |
| 1048 | dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1049 | EGLClientBuffer _buffer, |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1050 | const EGLint *attr_list) |
| 1051 | { |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1052 | struct wl_buffer *buffer = (struct wl_buffer *) _buffer; |
| 1053 | (void) attr_list; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1054 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1055 | if (!wayland_buffer_is_drm(buffer)) |
| 1056 | return NULL; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1057 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1058 | return dri2_reference_drm_image(disp, ctx, |
| 1059 | wayland_drm_buffer_get_buffer(buffer), |
| 1060 | buffer->width, |
| 1061 | buffer->height); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1062 | } |
| 1063 | #endif |
| 1064 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 1065 | _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1066 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 1067 | _EGLContext *ctx, EGLenum target, |
| 1068 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1069 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1070 | (void) drv; |
| 1071 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1072 | switch (target) { |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1073 | case EGL_GL_RENDERBUFFER_KHR: |
| 1074 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1075 | case EGL_DRM_BUFFER_MESA: |
| 1076 | return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1077 | #ifdef HAVE_WAYLAND_PLATFORM |
| 1078 | case EGL_WAYLAND_BUFFER_WL: |
| 1079 | return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list); |
| 1080 | #endif |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1081 | default: |
| 1082 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1083 | return EGL_NO_IMAGE_KHR; |
| 1084 | } |
| 1085 | } |
| 1086 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1087 | static EGLBoolean |
| 1088 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 1089 | { |
| 1090 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1091 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 1092 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1093 | (void) drv; |
| 1094 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1095 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 1096 | free(dri2_img); |
| 1097 | |
| 1098 | return EGL_TRUE; |
| 1099 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1100 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1101 | static _EGLImage * |
| 1102 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 1103 | const EGLint *attr_list) |
| 1104 | { |
| 1105 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1106 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1107 | _EGLImageAttribs attrs; |
| 1108 | unsigned int dri_use, valid_mask; |
| 1109 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1110 | EGLint err = EGL_SUCCESS; |
| 1111 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1112 | (void) drv; |
| 1113 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1114 | dri2_img = malloc(sizeof *dri2_img); |
| 1115 | if (!dri2_img) { |
| 1116 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1117 | return EGL_NO_IMAGE_KHR; |
| 1118 | } |
| 1119 | |
| 1120 | if (!attr_list) { |
| 1121 | err = EGL_BAD_PARAMETER; |
| 1122 | goto cleanup_img; |
| 1123 | } |
| 1124 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 1125 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1126 | err = EGL_BAD_PARAMETER; |
| 1127 | goto cleanup_img; |
| 1128 | } |
| 1129 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1130 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1131 | if (err != EGL_SUCCESS) |
| 1132 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1133 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1134 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 1135 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 1136 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1137 | goto cleanup_img; |
| 1138 | } |
| 1139 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1140 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1141 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 1142 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 1143 | break; |
| 1144 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1145 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 1146 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1147 | goto cleanup_img; |
| 1148 | } |
| 1149 | |
| 1150 | valid_mask = |
| 1151 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 1152 | EGL_DRM_BUFFER_USE_SHARE_MESA | |
| 1153 | EGL_DRM_BUFFER_USE_CURSOR_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1154 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 1155 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 1156 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1157 | goto cleanup_img; |
| 1158 | } |
| 1159 | |
| 1160 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1161 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1162 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1163 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1164 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 1165 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) |
| 1166 | dri_use |= __DRI_IMAGE_USE_CURSOR; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1167 | |
| 1168 | dri2_img->dri_image = |
| 1169 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1170 | attrs.Width, attrs.Height, |
| 1171 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1172 | if (dri2_img->dri_image == NULL) { |
| 1173 | err = EGL_BAD_ALLOC; |
| 1174 | goto cleanup_img; |
| 1175 | } |
| 1176 | |
| 1177 | return &dri2_img->base; |
| 1178 | |
| 1179 | cleanup_img: |
| 1180 | free(dri2_img); |
| 1181 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 1182 | |
| 1183 | return EGL_NO_IMAGE_KHR; |
| 1184 | } |
| 1185 | |
| 1186 | static EGLBoolean |
| 1187 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 1188 | EGLint *name, EGLint *handle, EGLint *stride) |
| 1189 | { |
| 1190 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1191 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 1192 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1193 | (void) drv; |
| 1194 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1195 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1196 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 1197 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 1198 | return EGL_FALSE; |
| 1199 | } |
| 1200 | |
| 1201 | if (handle) |
| 1202 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1203 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 1204 | |
| 1205 | if (stride) |
| 1206 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1207 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 1208 | |
| 1209 | return EGL_TRUE; |
| 1210 | } |
| 1211 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1212 | #ifdef HAVE_WAYLAND_PLATFORM |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1213 | |
| 1214 | static void * |
| 1215 | dri2_wl_reference_buffer(void *user_data, uint32_t name, |
| 1216 | int32_t width, int32_t height, |
| 1217 | uint32_t stride, struct wl_visual *visual) |
| 1218 | { |
| 1219 | _EGLDisplay *disp = user_data; |
| 1220 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1221 | __DRIimage *image; |
| 1222 | |
| 1223 | image = dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 1224 | width, height, |
| 1225 | __DRI_IMAGE_FORMAT_ARGB8888, |
| 1226 | name, stride / 4, |
| 1227 | NULL); |
| 1228 | |
| 1229 | return image; |
| 1230 | } |
| 1231 | |
| 1232 | static void |
| 1233 | dri2_wl_release_buffer(void *user_data, void *buffer) |
| 1234 | { |
| 1235 | _EGLDisplay *disp = user_data; |
| 1236 | __DRIimage *image = buffer; |
| 1237 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1238 | |
| 1239 | dri2_dpy->image->destroyImage(image); |
| 1240 | } |
| 1241 | |
| 1242 | static struct wayland_drm_callbacks wl_drm_callbacks = { |
| 1243 | .authenticate = NULL, |
| 1244 | .reference_buffer = dri2_wl_reference_buffer, |
| 1245 | .release_buffer = dri2_wl_release_buffer |
| 1246 | }; |
| 1247 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1248 | static EGLBoolean |
| 1249 | dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1250 | struct wl_display *wl_dpy) |
| 1251 | { |
| 1252 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1253 | |
| 1254 | (void) drv; |
| 1255 | |
| 1256 | if (dri2_dpy->wl_server_drm) |
| 1257 | return EGL_FALSE; |
| 1258 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1259 | wl_drm_callbacks.authenticate = |
| 1260 | (int(*)(void *, uint32_t)) dri2_dpy->authenticate; |
| 1261 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1262 | dri2_dpy->wl_server_drm = |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1263 | wayland_drm_init(wl_dpy, dri2_dpy->device_name, |
| 1264 | &wl_drm_callbacks, disp); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1265 | |
| 1266 | if (!dri2_dpy->wl_server_drm) |
| 1267 | return EGL_FALSE; |
| 1268 | |
| 1269 | return EGL_TRUE; |
| 1270 | } |
| 1271 | |
| 1272 | static EGLBoolean |
| 1273 | dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1274 | struct wl_display *wl_dpy) |
| 1275 | { |
| 1276 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1277 | |
| 1278 | (void) drv; |
| 1279 | |
| 1280 | if (!dri2_dpy->wl_server_drm) |
| 1281 | return EGL_FALSE; |
| 1282 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1283 | wayland_drm_uninit(dri2_dpy->wl_server_drm); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1284 | dri2_dpy->wl_server_drm = NULL; |
| 1285 | |
| 1286 | return EGL_TRUE; |
| 1287 | } |
| 1288 | #endif |
| 1289 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1290 | static void |
| 1291 | dri2_unload(_EGLDriver *drv) |
| 1292 | { |
| 1293 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1294 | |
| 1295 | if (dri2_drv->handle) |
| 1296 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1297 | free(dri2_drv); |
| 1298 | } |
| 1299 | |
| 1300 | static EGLBoolean |
| 1301 | dri2_load(_EGLDriver *drv) |
| 1302 | { |
| 1303 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1304 | #ifdef HAVE_SHARED_GLAPI |
| 1305 | const char *libname = "libglapi.so.0"; |
| 1306 | #else |
| 1307 | /* |
| 1308 | * Both libGL.so and libglapi.so are glapi providers. There is no way to |
| 1309 | * tell which one to load. |
| 1310 | */ |
| 1311 | const char *libname = NULL; |
| 1312 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1313 | void *handle; |
| 1314 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1315 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 1316 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1317 | if (handle) { |
| 1318 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 1319 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1320 | if (!dri2_drv->get_proc_address || !libname) { |
| 1321 | /* no need to keep a reference */ |
| 1322 | dlclose(handle); |
| 1323 | handle = NULL; |
| 1324 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1325 | } |
| 1326 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1327 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1328 | if (!dri2_drv->get_proc_address) { |
| 1329 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 1330 | return EGL_FALSE; |
| 1331 | } |
| 1332 | |
| 1333 | dri2_drv->glFlush = (void (*)(void)) |
| 1334 | dri2_drv->get_proc_address("glFlush"); |
| 1335 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1336 | dri2_drv->handle = handle; |
| 1337 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1338 | return EGL_TRUE; |
| 1339 | } |
| 1340 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1341 | /** |
| 1342 | * This is the main entrypoint into the driver, called by libEGL. |
| 1343 | * Create a new _EGLDriver object and init its dispatch table. |
| 1344 | */ |
| 1345 | _EGLDriver * |
Chia-I Wu | c98ea26 | 2011-01-07 16:30:08 +0800 | [diff] [blame] | 1346 | _EGL_MAIN(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1347 | { |
| 1348 | struct dri2_egl_driver *dri2_drv; |
| 1349 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1350 | (void) args; |
| 1351 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1352 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1353 | if (!dri2_drv) |
| 1354 | return NULL; |
| 1355 | |
Chia-I Wu | 39812c4 | 2011-01-12 18:06:47 +0800 | [diff] [blame] | 1356 | memset(dri2_drv, 0, sizeof *dri2_drv); |
| 1357 | |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 1358 | if (!dri2_load(&dri2_drv->base)) { |
| 1359 | free(dri2_drv); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1360 | return NULL; |
Völgyes Dávid | f747d03 | 2011-07-06 10:10:20 -0400 | [diff] [blame] | 1361 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1362 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1363 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1364 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1365 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1366 | dri2_drv->base.API.CreateContext = dri2_create_context; |
Cooper Yuan | f5e757e | 2011-08-14 15:14:17 +0800 | [diff] [blame^] | 1367 | dri2_drv->base.API.DestroyContext = dri2_destroy_context; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1368 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1369 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1370 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1371 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1372 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1373 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1374 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1375 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1376 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 1377 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1378 | #ifdef HAVE_WAYLAND_PLATFORM |
| 1379 | dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl; |
| 1380 | dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl; |
| 1381 | #endif |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1382 | |
| 1383 | dri2_drv->base.Name = "DRI2"; |
| 1384 | dri2_drv->base.Unload = dri2_unload; |
| 1385 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1386 | return &dri2_drv->base; |
| 1387 | } |