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 */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 97 | }; |
| 98 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 99 | struct dri2_egl_config * |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 100 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id, |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 101 | int depth, EGLint surface_type, const EGLint *attr_list) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 102 | { |
| 103 | struct dri2_egl_config *conf; |
| 104 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 105 | _EGLConfig base; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 106 | unsigned int attrib, value, double_buffer; |
| 107 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 108 | _EGLConfig *matching_config; |
| 109 | EGLint num_configs = 0; |
| 110 | EGLint config_id; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 111 | int i; |
| 112 | |
| 113 | dri2_dpy = disp->DriverData; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 114 | _eglInitConfig(&base, disp, id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 115 | |
| 116 | i = 0; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 117 | double_buffer = 0; |
| 118 | bind_to_texture_rgb = 0; |
| 119 | bind_to_texture_rgba = 0; |
| 120 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 121 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 122 | switch (attrib) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 123 | case __DRI_ATTRIB_RENDER_TYPE: |
| 124 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 125 | value = EGL_RGB_BUFFER; |
| 126 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 127 | value = EGL_LUMINANCE_BUFFER; |
| 128 | else |
| 129 | /* not valid */; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 130 | _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 134 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 135 | value = EGL_NON_CONFORMANT_CONFIG; |
| 136 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 137 | value = EGL_SLOW_CONFIG; |
| 138 | else |
| 139 | value = EGL_NONE; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 140 | _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 141 | break; |
| 142 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 143 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 144 | bind_to_texture_rgb = value; |
| 145 | break; |
| 146 | |
| 147 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 148 | bind_to_texture_rgba = value; |
| 149 | break; |
| 150 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 151 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 152 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 153 | break; |
| 154 | |
| 155 | default: |
| 156 | key = dri2_to_egl_attribute_map[attrib]; |
| 157 | if (key != 0) |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 158 | _eglSetConfigKey(&base, key, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 163 | if (attr_list) |
| 164 | for (i = 0; attr_list[i] != EGL_NONE; i += 2) |
| 165 | _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 166 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 167 | if (depth > 0 && depth != base.BufferSize) |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 168 | return NULL; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 169 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 170 | base.NativeRenderable = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 171 | |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 172 | base.SurfaceType = surface_type; |
Benjamin Franzke | a8128d7 | 2011-02-04 12:32:30 +0100 | [diff] [blame] | 173 | if (surface_type & (EGL_PBUFFER_BIT | |
| 174 | (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) { |
Chia-I Wu | b67f729 | 2010-10-22 15:03:11 +0800 | [diff] [blame] | 175 | base.BindToTextureRGB = bind_to_texture_rgb; |
| 176 | if (base.AlphaSize > 0) |
| 177 | base.BindToTextureRGBA = bind_to_texture_rgba; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 178 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 179 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 180 | base.RenderableType = disp->ClientAPIs; |
| 181 | base.Conformant = disp->ClientAPIs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 182 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 183 | if (!_eglValidateConfig(&base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 184 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 185 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 188 | config_id = base.ConfigID; |
| 189 | base.ConfigID = EGL_DONT_CARE; |
| 190 | base.SurfaceType = EGL_DONT_CARE; |
| 191 | num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1, |
| 192 | (_EGLArrayForEach) _eglMatchConfig, &base); |
| 193 | |
| 194 | if (num_configs == 1) { |
| 195 | conf = (struct dri2_egl_config *) matching_config; |
| 196 | |
| 197 | if (double_buffer && !conf->dri_double_config) |
| 198 | conf->dri_double_config = dri_config; |
| 199 | else if (!double_buffer && !conf->dri_single_config) |
| 200 | conf->dri_single_config = dri_config; |
| 201 | else |
| 202 | /* a similar config type is already added |
| 203 | * => attach it as new config |
| 204 | */ |
| 205 | num_configs = 0; |
| 206 | } |
| 207 | |
| 208 | if (num_configs == 0) { |
| 209 | conf = malloc(sizeof *conf); |
| 210 | if (conf == NULL) |
| 211 | return NULL; |
| 212 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 213 | memcpy(&conf->base, &base, sizeof base); |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 214 | if (double_buffer) { |
| 215 | conf->dri_double_config = dri_config; |
| 216 | conf->dri_single_config = NULL; |
| 217 | } else { |
| 218 | conf->dri_single_config = dri_config; |
| 219 | conf->dri_double_config = NULL; |
| 220 | } |
| 221 | conf->base.SurfaceType = 0; |
| 222 | conf->base.ConfigID = config_id; |
| 223 | |
Chia-I Wu | 8a6bdf3 | 2010-10-23 00:47:22 +0800 | [diff] [blame] | 224 | _eglLinkConfig(&conf->base); |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 225 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 226 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 227 | conf->base.SurfaceType |= surface_type & (!double_buffer ? EGL_PIXMAP_BIT: |
| 228 | (EGL_WINDOW_BIT | EGL_PBUFFER_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT)); |
| 229 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 230 | return conf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 233 | static __DRIimage * |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 234 | dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 235 | { |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 236 | _EGLDisplay *disp = data; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 237 | struct dri2_egl_image *dri2_img; |
| 238 | _EGLImage *img; |
| 239 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 240 | (void) screen; |
| 241 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 242 | img = _eglLookupImage(image, disp); |
| 243 | if (img == NULL) { |
| 244 | _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image"); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | dri2_img = dri2_egl_image(image); |
| 249 | |
| 250 | return dri2_img->dri_image; |
| 251 | } |
| 252 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 253 | const __DRIimageLookupExtension image_lookup_extension = { |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 254 | { __DRI_IMAGE_LOOKUP, 1 }, |
| 255 | dri2_lookup_egl_image |
| 256 | }; |
| 257 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 258 | static const char dri_driver_path[] = DEFAULT_DRIVER_DIR; |
| 259 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 260 | struct dri2_extension_match { |
| 261 | const char *name; |
| 262 | int version; |
| 263 | int offset; |
| 264 | }; |
| 265 | |
| 266 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 267 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 268 | { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 269 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 273 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 274 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 275 | { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) }, |
Ian Romanick | 137fce2 | 2010-09-23 09:40:58 -0700 | [diff] [blame] | 276 | { NULL, 0, 0 } |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | static EGLBoolean |
| 280 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 281 | struct dri2_extension_match *matches, |
| 282 | const __DRIextension **extensions) |
| 283 | { |
| 284 | int i, j, ret = EGL_TRUE; |
| 285 | void *field; |
| 286 | |
| 287 | for (i = 0; extensions[i]; i++) { |
| 288 | _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name); |
| 289 | for (j = 0; matches[j].name; j++) { |
| 290 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 291 | extensions[i]->version >= matches[j].version) { |
| 292 | field = ((char *) dri2_dpy + matches[j].offset); |
| 293 | *(const __DRIextension **) field = extensions[i]; |
| 294 | _eglLog(_EGL_INFO, "DRI2: found extension %s version %d", |
| 295 | extensions[i]->name, extensions[i]->version); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | for (j = 0; matches[j].name; j++) { |
| 301 | field = ((char *) dri2_dpy + matches[j].offset); |
| 302 | if (*(const __DRIextension **) field == NULL) { |
| 303 | _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d", |
| 304 | matches[j].name, matches[j].version); |
| 305 | ret = EGL_FALSE; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 312 | EGLBoolean |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 313 | dri2_load_driver(_EGLDisplay *disp) |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 314 | { |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 315 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 316 | const __DRIextension **extensions; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 317 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 318 | |
| 319 | search_paths = NULL; |
| 320 | if (geteuid() == getuid()) { |
| 321 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 322 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 323 | } |
| 324 | if (search_paths == NULL) |
| 325 | search_paths = DEFAULT_DRIVER_DIR; |
| 326 | |
| 327 | dri2_dpy->driver = NULL; |
| 328 | end = search_paths + strlen(search_paths); |
| 329 | 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] | 330 | int len; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 331 | next = strchr(p, ':'); |
| 332 | if (next == NULL) |
| 333 | next = end; |
| 334 | |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 335 | len = next - p; |
| 336 | #if GLX_USE_TLS |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 337 | snprintf(path, sizeof path, |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 338 | "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 339 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 340 | #endif |
| 341 | if (dri2_dpy->driver == NULL) { |
| 342 | snprintf(path, sizeof path, |
| 343 | "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name); |
| 344 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 345 | if (dri2_dpy->driver == NULL) |
| 346 | _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror()); |
| 347 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (dri2_dpy->driver == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 351 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 352 | "DRI2: failed to open any driver (search paths %s)", |
| 353 | search_paths); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 354 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
| 358 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
| 359 | if (extensions == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 360 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 361 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 362 | dlclose(dri2_dpy->driver); |
| 363 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 366 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) { |
| 367 | dlclose(dri2_dpy->driver); |
| 368 | return EGL_FALSE; |
| 369 | } |
| 370 | |
| 371 | return EGL_TRUE; |
| 372 | } |
| 373 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 374 | EGLBoolean |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 375 | dri2_create_screen(_EGLDisplay *disp) |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 376 | { |
| 377 | const __DRIextension **extensions; |
| 378 | struct dri2_egl_display *dri2_dpy; |
| 379 | unsigned int api_mask; |
| 380 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 381 | dri2_dpy = disp->DriverData; |
| 382 | dri2_dpy->dri_screen = |
| 383 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
Kristian Høgsberg | 17eace5 | 2010-09-22 22:01:17 -0400 | [diff] [blame] | 384 | &dri2_dpy->driver_configs, disp); |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 385 | |
| 386 | if (dri2_dpy->dri_screen == NULL) { |
| 387 | _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen"); |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 388 | return EGL_FALSE; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
| 392 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
| 393 | goto cleanup_dri_screen; |
| 394 | |
| 395 | if (dri2_dpy->dri2->base.version >= 2) |
| 396 | api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen); |
| 397 | else |
Chia-I Wu | aa139a1 | 2010-11-10 23:54:45 +0800 | [diff] [blame] | 398 | api_mask = 1 << __DRI_API_OPENGL; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 399 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 400 | disp->ClientAPIs = 0; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 401 | if (api_mask & (1 <<__DRI_API_OPENGL)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 402 | disp->ClientAPIs |= EGL_OPENGL_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 403 | if (api_mask & (1 <<__DRI_API_GLES)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 404 | disp->ClientAPIs |= EGL_OPENGL_ES_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 405 | if (api_mask & (1 << __DRI_API_GLES2)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 406 | disp->ClientAPIs |= EGL_OPENGL_ES2_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 407 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 408 | if (dri2_dpy->dri2->base.version >= 2) { |
| 409 | disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE; |
| 410 | disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE; |
| 411 | disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE; |
| 412 | } |
| 413 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 414 | return EGL_TRUE; |
| 415 | |
| 416 | cleanup_dri_screen: |
| 417 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 418 | |
| 419 | return EGL_FALSE; |
| 420 | } |
| 421 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 422 | /** |
| 423 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 424 | */ |
| 425 | static EGLBoolean |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 426 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp) |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 427 | { |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 428 | /* not until swrast_dri is supported */ |
| 429 | if (disp->Options.UseFallback) |
| 430 | return EGL_FALSE; |
| 431 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 432 | switch (disp->Platform) { |
| 433 | case _EGL_PLATFORM_X11: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 434 | if (disp->Options.TestOnly) |
| 435 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 436 | return dri2_initialize_x11(drv, disp); |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 437 | |
| 438 | #ifdef HAVE_LIBUDEV |
| 439 | case _EGL_PLATFORM_DRM: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 440 | if (disp->Options.TestOnly) |
| 441 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 442 | return dri2_initialize_drm(drv, disp); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 443 | #ifdef HAVE_WAYLAND_PLATFORM |
| 444 | case _EGL_PLATFORM_WAYLAND: |
| 445 | if (disp->Options.TestOnly) |
| 446 | return EGL_TRUE; |
| 447 | return dri2_initialize_wayland(drv, disp); |
| 448 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 449 | #endif |
| 450 | |
| 451 | default: |
| 452 | return EGL_FALSE; |
| 453 | } |
| 454 | } |
| 455 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 456 | /** |
| 457 | * Called via eglTerminate(), drv->API.Terminate(). |
| 458 | */ |
| 459 | static EGLBoolean |
| 460 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 461 | { |
| 462 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 463 | |
| 464 | _eglReleaseDisplayResources(drv, disp); |
| 465 | _eglCleanupDisplay(disp); |
| 466 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 467 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 468 | close(dri2_dpy->fd); |
| 469 | dlclose(dri2_dpy->driver); |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 470 | if (disp->PlatformDisplay == NULL) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 471 | xcb_disconnect(dri2_dpy->conn); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 472 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 473 | disp->DriverData = NULL; |
| 474 | |
| 475 | return EGL_TRUE; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /** |
| 480 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 481 | */ |
| 482 | static _EGLContext * |
| 483 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 484 | _EGLContext *share_list, const EGLint *attrib_list) |
| 485 | { |
| 486 | struct dri2_egl_context *dri2_ctx; |
| 487 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 488 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 489 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 490 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 491 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 492 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 493 | (void) drv; |
| 494 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 495 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 496 | if (!dri2_ctx) { |
| 497 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 501 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 502 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 503 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 504 | switch (dri2_ctx->base.ClientAPI) { |
| 505 | case EGL_OPENGL_ES_API: |
| 506 | switch (dri2_ctx->base.ClientVersion) { |
| 507 | case 1: |
| 508 | api = __DRI_API_GLES; |
| 509 | break; |
| 510 | case 2: |
| 511 | api = __DRI_API_GLES2; |
| 512 | break; |
| 513 | default: |
| 514 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 515 | return NULL; |
| 516 | } |
| 517 | break; |
| 518 | case EGL_OPENGL_API: |
| 519 | api = __DRI_API_OPENGL; |
| 520 | break; |
| 521 | default: |
| 522 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 526 | if (conf != NULL) { |
| 527 | /* The config chosen here isn't necessarily |
| 528 | * used for surfaces later. |
| 529 | * A pixmap surface will use the single config. |
| 530 | * This opportunity depends on disabling the |
| 531 | * doubleBufferMode check in |
| 532 | * src/mesa/main/context.c:check_compatible() |
| 533 | */ |
| 534 | if (dri2_config->dri_double_config) |
| 535 | dri_config = dri2_config->dri_double_config; |
| 536 | else |
| 537 | dri_config = dri2_config->dri_single_config; |
| 538 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 539 | else |
| 540 | dri_config = NULL; |
| 541 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 542 | if (dri2_dpy->dri2->base.version >= 2) { |
| 543 | dri2_ctx->dri_context = |
| 544 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 545 | api, |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 546 | dri_config, |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 547 | dri2_ctx_shared ? |
| 548 | dri2_ctx_shared->dri_context : NULL, |
| 549 | dri2_ctx); |
| 550 | } else if (api == __DRI_API_OPENGL) { |
| 551 | dri2_ctx->dri_context = |
| 552 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 553 | dri_config, |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 554 | dri2_ctx_shared ? |
| 555 | dri2_ctx_shared->dri_context : NULL, |
| 556 | dri2_ctx); |
| 557 | } else { |
| 558 | /* fail */ |
| 559 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 560 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 561 | if (!dri2_ctx->dri_context) |
| 562 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 563 | |
| 564 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 565 | |
| 566 | cleanup: |
| 567 | free(dri2_ctx); |
| 568 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 571 | /** |
| 572 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 573 | */ |
| 574 | static EGLBoolean |
| 575 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 576 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 577 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 578 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 579 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 580 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 581 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 582 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 583 | _EGLContext *old_ctx; |
| 584 | _EGLSurface *old_dsurf, *old_rsurf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 585 | __DRIdrawable *ddraw, *rdraw; |
| 586 | __DRIcontext *cctx; |
| 587 | |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 588 | /* make new bindings */ |
| 589 | if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 590 | return EGL_FALSE; |
| 591 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 592 | /* flush before context switch */ |
Chia-I Wu | 547e761 | 2010-10-26 15:00:56 +0800 | [diff] [blame] | 593 | if (old_ctx && dri2_drv->glFlush) |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 594 | dri2_drv->glFlush(); |
| 595 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 596 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 597 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 598 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 599 | |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame^] | 600 | if (old_ctx) { |
| 601 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 602 | dri2_dpy->core->unbindContext(old_cctx); |
| 603 | } |
| 604 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 605 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 606 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame^] | 607 | if (old_dsurf) |
| 608 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 609 | if (old_rsurf) |
| 610 | drv->API.DestroySurface(drv, disp, old_rsurf); |
| 611 | /* no destroy? */ |
| 612 | if (old_ctx) |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 613 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 614 | |
| 615 | return EGL_TRUE; |
| 616 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 617 | /* undo the previous _eglBindContext */ |
| 618 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf); |
| 619 | assert(&dri2_ctx->base == ctx && |
| 620 | &dri2_dsurf->base == dsurf && |
| 621 | &dri2_rsurf->base == rsurf); |
| 622 | |
| 623 | _eglPutSurface(dsurf); |
| 624 | _eglPutSurface(rsurf); |
| 625 | _eglPutContext(ctx); |
| 626 | |
| 627 | _eglPutSurface(old_dsurf); |
| 628 | _eglPutSurface(old_rsurf); |
| 629 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 630 | |
| 631 | return EGL_FALSE; |
| 632 | } |
| 633 | } |
| 634 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 635 | /* |
| 636 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 637 | */ |
| 638 | static _EGLProc |
| 639 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 640 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 641 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 642 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 643 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 647 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 648 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 649 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 650 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 651 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 652 | (void) drv; |
| 653 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 654 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 655 | * we need to copy fake to real here.*/ |
| 656 | |
| 657 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 658 | |
| 659 | return EGL_TRUE; |
| 660 | } |
| 661 | |
| 662 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 663 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 664 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 665 | (void) drv; |
| 666 | (void) disp; |
| 667 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 668 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 669 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 670 | /* glXWaitX(); */ |
| 671 | |
| 672 | return EGL_TRUE; |
| 673 | } |
| 674 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 675 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 676 | dri2_bind_tex_image(_EGLDriver *drv, |
| 677 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 678 | { |
| 679 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 680 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 681 | struct dri2_egl_context *dri2_ctx; |
| 682 | _EGLContext *ctx; |
| 683 | GLint format, target; |
| 684 | |
| 685 | ctx = _eglGetCurrentContext(); |
| 686 | dri2_ctx = dri2_egl_context(ctx); |
| 687 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 688 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 689 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 690 | |
| 691 | switch (dri2_surf->base.TextureFormat) { |
| 692 | case EGL_TEXTURE_RGB: |
| 693 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 694 | break; |
| 695 | case EGL_TEXTURE_RGBA: |
| 696 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 697 | break; |
| 698 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 699 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | switch (dri2_surf->base.TextureTarget) { |
| 703 | case EGL_TEXTURE_2D: |
| 704 | target = GL_TEXTURE_2D; |
| 705 | break; |
| 706 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 707 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 711 | target, format, |
| 712 | dri2_surf->dri_drawable); |
| 713 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 714 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static EGLBoolean |
| 718 | dri2_release_tex_image(_EGLDriver *drv, |
| 719 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 720 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 721 | #if __DRI_TEX_BUFFER_VERSION >= 3 |
| 722 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 723 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 724 | struct dri2_egl_context *dri2_ctx; |
| 725 | _EGLContext *ctx; |
| 726 | GLint target; |
| 727 | |
| 728 | ctx = _eglGetCurrentContext(); |
| 729 | dri2_ctx = dri2_egl_context(ctx); |
| 730 | |
| 731 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 732 | return EGL_FALSE; |
| 733 | |
| 734 | switch (dri2_surf->base.TextureTarget) { |
| 735 | case EGL_TEXTURE_2D: |
| 736 | target = GL_TEXTURE_2D; |
| 737 | break; |
| 738 | default: |
| 739 | assert(0); |
| 740 | } |
| 741 | if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL) |
| 742 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 743 | target, |
| 744 | dri2_surf->dri_drawable); |
| 745 | #endif |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 746 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 747 | return EGL_TRUE; |
| 748 | } |
| 749 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 750 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 751 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 752 | EGLClientBuffer buffer, |
| 753 | const EGLint *attr_list) |
| 754 | { |
| 755 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 756 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 757 | struct dri2_egl_image *dri2_img; |
| 758 | GLuint renderbuffer = (GLuint) buffer; |
| 759 | |
| 760 | if (renderbuffer == 0) { |
| 761 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 762 | return EGL_NO_IMAGE_KHR; |
| 763 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 764 | |
| 765 | dri2_img = malloc(sizeof *dri2_img); |
| 766 | if (!dri2_img) { |
| 767 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 768 | return EGL_NO_IMAGE_KHR; |
| 769 | } |
| 770 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 771 | if (!_eglInitImage(&dri2_img->base, disp)) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 772 | return EGL_NO_IMAGE_KHR; |
| 773 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 774 | dri2_img->dri_image = |
| 775 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
| 776 | renderbuffer, |
| 777 | dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 778 | |
| 779 | return &dri2_img->base; |
| 780 | } |
| 781 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 782 | static _EGLImage * |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 783 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 784 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 785 | { |
| 786 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 787 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 788 | EGLint format, name, pitch, err; |
| 789 | _EGLImageAttribs attrs; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 790 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 791 | (void) ctx; |
| 792 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 793 | name = (EGLint) buffer; |
| 794 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 795 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 796 | if (err != EGL_SUCCESS) |
| 797 | return NULL; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 798 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 799 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 800 | attrs.DRMBufferStrideMESA <= 0) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 801 | _eglError(EGL_BAD_PARAMETER, |
| 802 | "bad width, height or stride"); |
| 803 | return NULL; |
| 804 | } |
| 805 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 806 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 807 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 808 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 809 | pitch = attrs.DRMBufferStrideMESA; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 810 | break; |
| 811 | default: |
| 812 | _eglError(EGL_BAD_PARAMETER, |
| 813 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 814 | return NULL; |
| 815 | } |
| 816 | |
| 817 | dri2_img = malloc(sizeof *dri2_img); |
| 818 | if (!dri2_img) { |
| 819 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 820 | return NULL; |
| 821 | } |
| 822 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 823 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 824 | free(dri2_img); |
| 825 | return NULL; |
| 826 | } |
| 827 | |
| 828 | dri2_img->dri_image = |
Kristian Høgsberg | 9ec0b2a | 2010-09-22 15:07:15 -0400 | [diff] [blame] | 829 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 830 | attrs.Width, |
| 831 | attrs.Height, |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 832 | format, |
| 833 | name, |
| 834 | pitch, |
| 835 | dri2_img); |
| 836 | if (dri2_img->dri_image == NULL) { |
| 837 | free(dri2_img); |
| 838 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | return &dri2_img->base; |
| 843 | } |
| 844 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 845 | _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 846 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 847 | _EGLContext *ctx, EGLenum target, |
| 848 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 849 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 850 | (void) drv; |
| 851 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 852 | switch (target) { |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 853 | case EGL_GL_RENDERBUFFER_KHR: |
| 854 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 855 | case EGL_DRM_BUFFER_MESA: |
| 856 | return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 857 | default: |
| 858 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 859 | return EGL_NO_IMAGE_KHR; |
| 860 | } |
| 861 | } |
| 862 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 863 | static EGLBoolean |
| 864 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 865 | { |
| 866 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 867 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 868 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 869 | (void) drv; |
| 870 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 871 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 872 | free(dri2_img); |
| 873 | |
| 874 | return EGL_TRUE; |
| 875 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 876 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 877 | static _EGLImage * |
| 878 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 879 | const EGLint *attr_list) |
| 880 | { |
| 881 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 882 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 883 | _EGLImageAttribs attrs; |
| 884 | unsigned int dri_use, valid_mask; |
| 885 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 886 | EGLint err = EGL_SUCCESS; |
| 887 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 888 | (void) drv; |
| 889 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 890 | dri2_img = malloc(sizeof *dri2_img); |
| 891 | if (!dri2_img) { |
| 892 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 893 | return EGL_NO_IMAGE_KHR; |
| 894 | } |
| 895 | |
| 896 | if (!attr_list) { |
| 897 | err = EGL_BAD_PARAMETER; |
| 898 | goto cleanup_img; |
| 899 | } |
| 900 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 901 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 902 | err = EGL_BAD_PARAMETER; |
| 903 | goto cleanup_img; |
| 904 | } |
| 905 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 906 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 907 | if (err != EGL_SUCCESS) |
| 908 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 909 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 910 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 911 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 912 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 913 | goto cleanup_img; |
| 914 | } |
| 915 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 916 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 917 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 918 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 919 | break; |
| 920 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 921 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 922 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 923 | goto cleanup_img; |
| 924 | } |
| 925 | |
| 926 | valid_mask = |
| 927 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
| 928 | EGL_DRM_BUFFER_USE_SHARE_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 929 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 930 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 931 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 932 | goto cleanup_img; |
| 933 | } |
| 934 | |
| 935 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 936 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 937 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 938 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 939 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
| 940 | |
| 941 | dri2_img->dri_image = |
| 942 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 943 | attrs.Width, attrs.Height, |
| 944 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 945 | if (dri2_img->dri_image == NULL) { |
| 946 | err = EGL_BAD_ALLOC; |
| 947 | goto cleanup_img; |
| 948 | } |
| 949 | |
| 950 | return &dri2_img->base; |
| 951 | |
| 952 | cleanup_img: |
| 953 | free(dri2_img); |
| 954 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 955 | |
| 956 | return EGL_NO_IMAGE_KHR; |
| 957 | } |
| 958 | |
| 959 | static EGLBoolean |
| 960 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 961 | EGLint *name, EGLint *handle, EGLint *stride) |
| 962 | { |
| 963 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 964 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 965 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 966 | (void) drv; |
| 967 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 968 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 969 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 970 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 971 | return EGL_FALSE; |
| 972 | } |
| 973 | |
| 974 | if (handle) |
| 975 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 976 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 977 | |
| 978 | if (stride) |
| 979 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 980 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 981 | |
| 982 | return EGL_TRUE; |
| 983 | } |
| 984 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 985 | static void |
| 986 | dri2_unload(_EGLDriver *drv) |
| 987 | { |
| 988 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 989 | |
| 990 | if (dri2_drv->handle) |
| 991 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 992 | free(dri2_drv); |
| 993 | } |
| 994 | |
| 995 | static EGLBoolean |
| 996 | dri2_load(_EGLDriver *drv) |
| 997 | { |
| 998 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 999 | #ifdef HAVE_SHARED_GLAPI |
| 1000 | const char *libname = "libglapi.so.0"; |
| 1001 | #else |
| 1002 | /* |
| 1003 | * Both libGL.so and libglapi.so are glapi providers. There is no way to |
| 1004 | * tell which one to load. |
| 1005 | */ |
| 1006 | const char *libname = NULL; |
| 1007 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1008 | void *handle; |
| 1009 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1010 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 1011 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1012 | if (handle) { |
| 1013 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 1014 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1015 | if (!dri2_drv->get_proc_address || !libname) { |
| 1016 | /* no need to keep a reference */ |
| 1017 | dlclose(handle); |
| 1018 | handle = NULL; |
| 1019 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1022 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1023 | if (!dri2_drv->get_proc_address) { |
| 1024 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 1025 | return EGL_FALSE; |
| 1026 | } |
| 1027 | |
| 1028 | dri2_drv->glFlush = (void (*)(void)) |
| 1029 | dri2_drv->get_proc_address("glFlush"); |
| 1030 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1031 | dri2_drv->handle = handle; |
| 1032 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1033 | return EGL_TRUE; |
| 1034 | } |
| 1035 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1036 | /** |
| 1037 | * This is the main entrypoint into the driver, called by libEGL. |
| 1038 | * Create a new _EGLDriver object and init its dispatch table. |
| 1039 | */ |
| 1040 | _EGLDriver * |
Chia-I Wu | c98ea26 | 2011-01-07 16:30:08 +0800 | [diff] [blame] | 1041 | _EGL_MAIN(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1042 | { |
| 1043 | struct dri2_egl_driver *dri2_drv; |
| 1044 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1045 | (void) args; |
| 1046 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1047 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1048 | if (!dri2_drv) |
| 1049 | return NULL; |
| 1050 | |
Chia-I Wu | 39812c4 | 2011-01-12 18:06:47 +0800 | [diff] [blame] | 1051 | memset(dri2_drv, 0, sizeof *dri2_drv); |
| 1052 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1053 | if (!dri2_load(&dri2_drv->base)) |
| 1054 | return NULL; |
| 1055 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1056 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1057 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1058 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1059 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 1060 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1061 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1062 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1063 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1064 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1065 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1066 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1067 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1068 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 1069 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1070 | |
| 1071 | dri2_drv->base.Name = "DRI2"; |
| 1072 | dri2_drv->base.Unload = dri2_unload; |
| 1073 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1074 | return &dri2_drv->base; |
| 1075 | } |