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 | |
| 600 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 601 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 602 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 603 | drv->API.DestroySurface(drv, disp, old_rsurf); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 604 | if (old_ctx) { |
Chia-I Wu | 1f4c551 | 2010-11-24 13:42:06 +0800 | [diff] [blame] | 605 | /* unbind the old context only when there is no new context bound */ |
| 606 | if (!ctx) { |
| 607 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 608 | dri2_dpy->core->unbindContext(old_cctx); |
| 609 | } |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 610 | /* no destroy? */ |
| 611 | _eglPutContext(old_ctx); |
| 612 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 613 | |
| 614 | return EGL_TRUE; |
| 615 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 616 | /* undo the previous _eglBindContext */ |
| 617 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf); |
| 618 | assert(&dri2_ctx->base == ctx && |
| 619 | &dri2_dsurf->base == dsurf && |
| 620 | &dri2_rsurf->base == rsurf); |
| 621 | |
| 622 | _eglPutSurface(dsurf); |
| 623 | _eglPutSurface(rsurf); |
| 624 | _eglPutContext(ctx); |
| 625 | |
| 626 | _eglPutSurface(old_dsurf); |
| 627 | _eglPutSurface(old_rsurf); |
| 628 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 629 | |
| 630 | return EGL_FALSE; |
| 631 | } |
| 632 | } |
| 633 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 634 | /* |
| 635 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 636 | */ |
| 637 | static _EGLProc |
| 638 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 639 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 640 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 641 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 642 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 646 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 647 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 648 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 649 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 650 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 651 | (void) drv; |
| 652 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 653 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 654 | * we need to copy fake to real here.*/ |
| 655 | |
| 656 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 657 | |
| 658 | return EGL_TRUE; |
| 659 | } |
| 660 | |
| 661 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 662 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 663 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 664 | (void) drv; |
| 665 | (void) disp; |
| 666 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 667 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 668 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 669 | /* glXWaitX(); */ |
| 670 | |
| 671 | return EGL_TRUE; |
| 672 | } |
| 673 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 674 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 675 | dri2_bind_tex_image(_EGLDriver *drv, |
| 676 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 677 | { |
| 678 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 679 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 680 | struct dri2_egl_context *dri2_ctx; |
| 681 | _EGLContext *ctx; |
| 682 | GLint format, target; |
| 683 | |
| 684 | ctx = _eglGetCurrentContext(); |
| 685 | dri2_ctx = dri2_egl_context(ctx); |
| 686 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 687 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 688 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 689 | |
| 690 | switch (dri2_surf->base.TextureFormat) { |
| 691 | case EGL_TEXTURE_RGB: |
| 692 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 693 | break; |
| 694 | case EGL_TEXTURE_RGBA: |
| 695 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 696 | break; |
| 697 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 698 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | switch (dri2_surf->base.TextureTarget) { |
| 702 | case EGL_TEXTURE_2D: |
| 703 | target = GL_TEXTURE_2D; |
| 704 | break; |
| 705 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 706 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 710 | target, format, |
| 711 | dri2_surf->dri_drawable); |
| 712 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 713 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static EGLBoolean |
| 717 | dri2_release_tex_image(_EGLDriver *drv, |
| 718 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 719 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 720 | #if __DRI_TEX_BUFFER_VERSION >= 3 |
| 721 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 722 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 723 | struct dri2_egl_context *dri2_ctx; |
| 724 | _EGLContext *ctx; |
| 725 | GLint target; |
| 726 | |
| 727 | ctx = _eglGetCurrentContext(); |
| 728 | dri2_ctx = dri2_egl_context(ctx); |
| 729 | |
| 730 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 731 | return EGL_FALSE; |
| 732 | |
| 733 | switch (dri2_surf->base.TextureTarget) { |
| 734 | case EGL_TEXTURE_2D: |
| 735 | target = GL_TEXTURE_2D; |
| 736 | break; |
| 737 | default: |
| 738 | assert(0); |
| 739 | } |
| 740 | if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL) |
| 741 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 742 | target, |
| 743 | dri2_surf->dri_drawable); |
| 744 | #endif |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 745 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 746 | return EGL_TRUE; |
| 747 | } |
| 748 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 749 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 750 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 751 | EGLClientBuffer buffer, |
| 752 | const EGLint *attr_list) |
| 753 | { |
| 754 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 755 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 756 | struct dri2_egl_image *dri2_img; |
| 757 | GLuint renderbuffer = (GLuint) buffer; |
| 758 | |
| 759 | if (renderbuffer == 0) { |
| 760 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 761 | return EGL_NO_IMAGE_KHR; |
| 762 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 763 | |
| 764 | dri2_img = malloc(sizeof *dri2_img); |
| 765 | if (!dri2_img) { |
| 766 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 767 | return EGL_NO_IMAGE_KHR; |
| 768 | } |
| 769 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 770 | if (!_eglInitImage(&dri2_img->base, disp)) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 771 | return EGL_NO_IMAGE_KHR; |
| 772 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 773 | dri2_img->dri_image = |
| 774 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
| 775 | renderbuffer, |
| 776 | dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 777 | |
| 778 | return &dri2_img->base; |
| 779 | } |
| 780 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 781 | static _EGLImage * |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 782 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 783 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 784 | { |
| 785 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 786 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 787 | EGLint format, name, pitch, err; |
| 788 | _EGLImageAttribs attrs; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 789 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 790 | (void) ctx; |
| 791 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 792 | name = (EGLint) buffer; |
| 793 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 794 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 795 | if (err != EGL_SUCCESS) |
| 796 | return NULL; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 797 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 798 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 799 | attrs.DRMBufferStrideMESA <= 0) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 800 | _eglError(EGL_BAD_PARAMETER, |
| 801 | "bad width, height or stride"); |
| 802 | return NULL; |
| 803 | } |
| 804 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 805 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 806 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 807 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 808 | pitch = attrs.DRMBufferStrideMESA; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 809 | break; |
| 810 | default: |
| 811 | _eglError(EGL_BAD_PARAMETER, |
| 812 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 813 | return NULL; |
| 814 | } |
| 815 | |
| 816 | dri2_img = malloc(sizeof *dri2_img); |
| 817 | if (!dri2_img) { |
| 818 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 819 | return NULL; |
| 820 | } |
| 821 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 822 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 823 | free(dri2_img); |
| 824 | return NULL; |
| 825 | } |
| 826 | |
| 827 | dri2_img->dri_image = |
Kristian Høgsberg | 9ec0b2a | 2010-09-22 15:07:15 -0400 | [diff] [blame] | 828 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 829 | attrs.Width, |
| 830 | attrs.Height, |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 831 | format, |
| 832 | name, |
| 833 | pitch, |
| 834 | dri2_img); |
| 835 | if (dri2_img->dri_image == NULL) { |
| 836 | free(dri2_img); |
| 837 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 838 | return NULL; |
| 839 | } |
| 840 | |
| 841 | return &dri2_img->base; |
| 842 | } |
| 843 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 844 | _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 845 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 846 | _EGLContext *ctx, EGLenum target, |
| 847 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 848 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 849 | (void) drv; |
| 850 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 851 | switch (target) { |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 852 | case EGL_GL_RENDERBUFFER_KHR: |
| 853 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 854 | case EGL_DRM_BUFFER_MESA: |
| 855 | 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] | 856 | default: |
| 857 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 858 | return EGL_NO_IMAGE_KHR; |
| 859 | } |
| 860 | } |
| 861 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 862 | static EGLBoolean |
| 863 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 864 | { |
| 865 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 866 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 867 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 868 | (void) drv; |
| 869 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 870 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 871 | free(dri2_img); |
| 872 | |
| 873 | return EGL_TRUE; |
| 874 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 875 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 876 | static _EGLImage * |
| 877 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 878 | const EGLint *attr_list) |
| 879 | { |
| 880 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 881 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 882 | _EGLImageAttribs attrs; |
| 883 | unsigned int dri_use, valid_mask; |
| 884 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 885 | EGLint err = EGL_SUCCESS; |
| 886 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 887 | (void) drv; |
| 888 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 889 | dri2_img = malloc(sizeof *dri2_img); |
| 890 | if (!dri2_img) { |
| 891 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 892 | return EGL_NO_IMAGE_KHR; |
| 893 | } |
| 894 | |
| 895 | if (!attr_list) { |
| 896 | err = EGL_BAD_PARAMETER; |
| 897 | goto cleanup_img; |
| 898 | } |
| 899 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 900 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 901 | err = EGL_BAD_PARAMETER; |
| 902 | goto cleanup_img; |
| 903 | } |
| 904 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 905 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 906 | if (err != EGL_SUCCESS) |
| 907 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 908 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 909 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 910 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 911 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 912 | goto cleanup_img; |
| 913 | } |
| 914 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 915 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 916 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 917 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 918 | break; |
| 919 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 920 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 921 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 922 | goto cleanup_img; |
| 923 | } |
| 924 | |
| 925 | valid_mask = |
| 926 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
| 927 | EGL_DRM_BUFFER_USE_SHARE_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 928 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 929 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 930 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 931 | goto cleanup_img; |
| 932 | } |
| 933 | |
| 934 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 935 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 936 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 937 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 938 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
| 939 | |
| 940 | dri2_img->dri_image = |
| 941 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 942 | attrs.Width, attrs.Height, |
| 943 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 944 | if (dri2_img->dri_image == NULL) { |
| 945 | err = EGL_BAD_ALLOC; |
| 946 | goto cleanup_img; |
| 947 | } |
| 948 | |
| 949 | return &dri2_img->base; |
| 950 | |
| 951 | cleanup_img: |
| 952 | free(dri2_img); |
| 953 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 954 | |
| 955 | return EGL_NO_IMAGE_KHR; |
| 956 | } |
| 957 | |
| 958 | static EGLBoolean |
| 959 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 960 | EGLint *name, EGLint *handle, EGLint *stride) |
| 961 | { |
| 962 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 963 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 964 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 965 | (void) drv; |
| 966 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 967 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 968 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 969 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 970 | return EGL_FALSE; |
| 971 | } |
| 972 | |
| 973 | if (handle) |
| 974 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 975 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 976 | |
| 977 | if (stride) |
| 978 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 979 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 980 | |
| 981 | return EGL_TRUE; |
| 982 | } |
| 983 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 984 | static void |
| 985 | dri2_unload(_EGLDriver *drv) |
| 986 | { |
| 987 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 988 | |
| 989 | if (dri2_drv->handle) |
| 990 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 991 | free(dri2_drv); |
| 992 | } |
| 993 | |
| 994 | static EGLBoolean |
| 995 | dri2_load(_EGLDriver *drv) |
| 996 | { |
| 997 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 998 | #ifdef HAVE_SHARED_GLAPI |
| 999 | const char *libname = "libglapi.so.0"; |
| 1000 | #else |
| 1001 | /* |
| 1002 | * Both libGL.so and libglapi.so are glapi providers. There is no way to |
| 1003 | * tell which one to load. |
| 1004 | */ |
| 1005 | const char *libname = NULL; |
| 1006 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1007 | void *handle; |
| 1008 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1009 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 1010 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1011 | if (handle) { |
| 1012 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 1013 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1014 | if (!dri2_drv->get_proc_address || !libname) { |
| 1015 | /* no need to keep a reference */ |
| 1016 | dlclose(handle); |
| 1017 | handle = NULL; |
| 1018 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1021 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1022 | if (!dri2_drv->get_proc_address) { |
| 1023 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 1024 | return EGL_FALSE; |
| 1025 | } |
| 1026 | |
| 1027 | dri2_drv->glFlush = (void (*)(void)) |
| 1028 | dri2_drv->get_proc_address("glFlush"); |
| 1029 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1030 | dri2_drv->handle = handle; |
| 1031 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1032 | return EGL_TRUE; |
| 1033 | } |
| 1034 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1035 | /** |
| 1036 | * This is the main entrypoint into the driver, called by libEGL. |
| 1037 | * Create a new _EGLDriver object and init its dispatch table. |
| 1038 | */ |
| 1039 | _EGLDriver * |
Chia-I Wu | c98ea26 | 2011-01-07 16:30:08 +0800 | [diff] [blame] | 1040 | _EGL_MAIN(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1041 | { |
| 1042 | struct dri2_egl_driver *dri2_drv; |
| 1043 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1044 | (void) args; |
| 1045 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1046 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1047 | if (!dri2_drv) |
| 1048 | return NULL; |
| 1049 | |
Chia-I Wu | 39812c4 | 2011-01-12 18:06:47 +0800 | [diff] [blame] | 1050 | memset(dri2_drv, 0, sizeof *dri2_drv); |
| 1051 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1052 | if (!dri2_load(&dri2_drv->base)) |
| 1053 | return NULL; |
| 1054 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1055 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1056 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1057 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1058 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 1059 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1060 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1061 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1062 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1063 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1064 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1065 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1066 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1067 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 1068 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1069 | |
| 1070 | dri2_drv->base.Name = "DRI2"; |
| 1071 | dri2_drv->base.Unload = dri2_unload; |
| 1072 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1073 | return &dri2_drv->base; |
| 1074 | } |