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