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); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame^] | 411 | #ifdef HAVE_WAYLAND_PLATFORM |
| 412 | case _EGL_PLATFORM_WAYLAND: |
| 413 | if (disp->Options.TestOnly) |
| 414 | return EGL_TRUE; |
| 415 | return dri2_initialize_wayland(drv, disp); |
| 416 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 417 | #endif |
| 418 | |
| 419 | default: |
| 420 | return EGL_FALSE; |
| 421 | } |
| 422 | } |
| 423 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 424 | /** |
| 425 | * Called via eglTerminate(), drv->API.Terminate(). |
| 426 | */ |
| 427 | static EGLBoolean |
| 428 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 429 | { |
| 430 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 431 | |
| 432 | _eglReleaseDisplayResources(drv, disp); |
| 433 | _eglCleanupDisplay(disp); |
| 434 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 435 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 436 | close(dri2_dpy->fd); |
| 437 | dlclose(dri2_dpy->driver); |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 438 | if (disp->PlatformDisplay == NULL) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 439 | xcb_disconnect(dri2_dpy->conn); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 440 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 441 | disp->DriverData = NULL; |
| 442 | |
| 443 | return EGL_TRUE; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /** |
| 448 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 449 | */ |
| 450 | static _EGLContext * |
| 451 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 452 | _EGLContext *share_list, const EGLint *attrib_list) |
| 453 | { |
| 454 | struct dri2_egl_context *dri2_ctx; |
| 455 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 456 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 457 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 458 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 459 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 460 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 461 | (void) drv; |
| 462 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 463 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 464 | if (!dri2_ctx) { |
| 465 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 466 | return NULL; |
| 467 | } |
| 468 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 469 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 470 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 471 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 472 | switch (dri2_ctx->base.ClientAPI) { |
| 473 | case EGL_OPENGL_ES_API: |
| 474 | switch (dri2_ctx->base.ClientVersion) { |
| 475 | case 1: |
| 476 | api = __DRI_API_GLES; |
| 477 | break; |
| 478 | case 2: |
| 479 | api = __DRI_API_GLES2; |
| 480 | break; |
| 481 | default: |
| 482 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 483 | return NULL; |
| 484 | } |
| 485 | break; |
| 486 | case EGL_OPENGL_API: |
| 487 | api = __DRI_API_OPENGL; |
| 488 | break; |
| 489 | default: |
| 490 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 494 | if (conf != NULL) |
| 495 | dri_config = dri2_config->dri_config; |
| 496 | else |
| 497 | dri_config = NULL; |
| 498 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 499 | if (dri2_dpy->dri2->base.version >= 2) { |
| 500 | dri2_ctx->dri_context = |
| 501 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 502 | api, |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 503 | dri_config, |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 504 | dri2_ctx_shared ? |
| 505 | dri2_ctx_shared->dri_context : NULL, |
| 506 | dri2_ctx); |
| 507 | } else if (api == __DRI_API_OPENGL) { |
| 508 | dri2_ctx->dri_context = |
| 509 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
| 510 | dri2_config->dri_config, |
| 511 | dri2_ctx_shared ? |
| 512 | dri2_ctx_shared->dri_context : NULL, |
| 513 | dri2_ctx); |
| 514 | } else { |
| 515 | /* fail */ |
| 516 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 517 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 518 | if (!dri2_ctx->dri_context) |
| 519 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 520 | |
| 521 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 522 | |
| 523 | cleanup: |
| 524 | free(dri2_ctx); |
| 525 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 528 | /** |
| 529 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 530 | */ |
| 531 | static EGLBoolean |
| 532 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 533 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 534 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 535 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 536 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 537 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 538 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 539 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 540 | _EGLContext *old_ctx; |
| 541 | _EGLSurface *old_dsurf, *old_rsurf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 542 | __DRIdrawable *ddraw, *rdraw; |
| 543 | __DRIcontext *cctx; |
| 544 | |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 545 | /* make new bindings */ |
| 546 | if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 547 | return EGL_FALSE; |
| 548 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 549 | /* flush before context switch */ |
Chia-I Wu | 547e761 | 2010-10-26 15:00:56 +0800 | [diff] [blame] | 550 | if (old_ctx && dri2_drv->glFlush) |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 551 | dri2_drv->glFlush(); |
| 552 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 553 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 554 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 555 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 556 | |
| 557 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 558 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 559 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 560 | drv->API.DestroySurface(drv, disp, old_rsurf); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 561 | if (old_ctx) { |
Chia-I Wu | 1f4c551 | 2010-11-24 13:42:06 +0800 | [diff] [blame] | 562 | /* unbind the old context only when there is no new context bound */ |
| 563 | if (!ctx) { |
| 564 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 565 | dri2_dpy->core->unbindContext(old_cctx); |
| 566 | } |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 567 | /* no destroy? */ |
| 568 | _eglPutContext(old_ctx); |
| 569 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 570 | |
| 571 | return EGL_TRUE; |
| 572 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 573 | /* undo the previous _eglBindContext */ |
| 574 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf); |
| 575 | assert(&dri2_ctx->base == ctx && |
| 576 | &dri2_dsurf->base == dsurf && |
| 577 | &dri2_rsurf->base == rsurf); |
| 578 | |
| 579 | _eglPutSurface(dsurf); |
| 580 | _eglPutSurface(rsurf); |
| 581 | _eglPutContext(ctx); |
| 582 | |
| 583 | _eglPutSurface(old_dsurf); |
| 584 | _eglPutSurface(old_rsurf); |
| 585 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 586 | |
| 587 | return EGL_FALSE; |
| 588 | } |
| 589 | } |
| 590 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 591 | /* |
| 592 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 593 | */ |
| 594 | static _EGLProc |
| 595 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 596 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 597 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 598 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 599 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 603 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 604 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 605 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 606 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 607 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 608 | (void) drv; |
| 609 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 610 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 611 | * we need to copy fake to real here.*/ |
| 612 | |
| 613 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 614 | |
| 615 | return EGL_TRUE; |
| 616 | } |
| 617 | |
| 618 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 619 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 620 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 621 | (void) drv; |
| 622 | (void) disp; |
| 623 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 624 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 625 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 626 | /* glXWaitX(); */ |
| 627 | |
| 628 | return EGL_TRUE; |
| 629 | } |
| 630 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 631 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 632 | dri2_bind_tex_image(_EGLDriver *drv, |
| 633 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 634 | { |
| 635 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 636 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 637 | struct dri2_egl_context *dri2_ctx; |
| 638 | _EGLContext *ctx; |
| 639 | GLint format, target; |
| 640 | |
| 641 | ctx = _eglGetCurrentContext(); |
| 642 | dri2_ctx = dri2_egl_context(ctx); |
| 643 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 644 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 645 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 646 | |
| 647 | switch (dri2_surf->base.TextureFormat) { |
| 648 | case EGL_TEXTURE_RGB: |
| 649 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 650 | break; |
| 651 | case EGL_TEXTURE_RGBA: |
| 652 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 653 | break; |
| 654 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 655 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | switch (dri2_surf->base.TextureTarget) { |
| 659 | case EGL_TEXTURE_2D: |
| 660 | target = GL_TEXTURE_2D; |
| 661 | break; |
| 662 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 663 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 667 | target, format, |
| 668 | dri2_surf->dri_drawable); |
| 669 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 670 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static EGLBoolean |
| 674 | dri2_release_tex_image(_EGLDriver *drv, |
| 675 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 676 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 677 | #if __DRI_TEX_BUFFER_VERSION >= 3 |
| 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 target; |
| 683 | |
| 684 | ctx = _eglGetCurrentContext(); |
| 685 | dri2_ctx = dri2_egl_context(ctx); |
| 686 | |
| 687 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 688 | return EGL_FALSE; |
| 689 | |
| 690 | switch (dri2_surf->base.TextureTarget) { |
| 691 | case EGL_TEXTURE_2D: |
| 692 | target = GL_TEXTURE_2D; |
| 693 | break; |
| 694 | default: |
| 695 | assert(0); |
| 696 | } |
| 697 | if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL) |
| 698 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 699 | target, |
| 700 | dri2_surf->dri_drawable); |
| 701 | #endif |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 702 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 703 | return EGL_TRUE; |
| 704 | } |
| 705 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 706 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 707 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 708 | EGLClientBuffer buffer, |
| 709 | const EGLint *attr_list) |
| 710 | { |
| 711 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 712 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 713 | struct dri2_egl_image *dri2_img; |
| 714 | GLuint renderbuffer = (GLuint) buffer; |
| 715 | |
| 716 | if (renderbuffer == 0) { |
| 717 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 718 | return EGL_NO_IMAGE_KHR; |
| 719 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 720 | |
| 721 | dri2_img = malloc(sizeof *dri2_img); |
| 722 | if (!dri2_img) { |
| 723 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 724 | return EGL_NO_IMAGE_KHR; |
| 725 | } |
| 726 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 727 | if (!_eglInitImage(&dri2_img->base, disp)) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 728 | return EGL_NO_IMAGE_KHR; |
| 729 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 730 | dri2_img->dri_image = |
| 731 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
| 732 | renderbuffer, |
| 733 | dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 734 | |
| 735 | return &dri2_img->base; |
| 736 | } |
| 737 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 738 | static _EGLImage * |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 739 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 740 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 741 | { |
| 742 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 743 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 744 | EGLint format, name, pitch, err; |
| 745 | _EGLImageAttribs attrs; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 746 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 747 | (void) ctx; |
| 748 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 749 | name = (EGLint) buffer; |
| 750 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 751 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 752 | if (err != EGL_SUCCESS) |
| 753 | return NULL; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 754 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 755 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 756 | attrs.DRMBufferStrideMESA <= 0) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 757 | _eglError(EGL_BAD_PARAMETER, |
| 758 | "bad width, height or stride"); |
| 759 | return NULL; |
| 760 | } |
| 761 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 762 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 763 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 764 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 765 | pitch = attrs.DRMBufferStrideMESA; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 766 | break; |
| 767 | default: |
| 768 | _eglError(EGL_BAD_PARAMETER, |
| 769 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | dri2_img = malloc(sizeof *dri2_img); |
| 774 | if (!dri2_img) { |
| 775 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 776 | return NULL; |
| 777 | } |
| 778 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 779 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 780 | free(dri2_img); |
| 781 | return NULL; |
| 782 | } |
| 783 | |
| 784 | dri2_img->dri_image = |
Kristian Høgsberg | 9ec0b2a | 2010-09-22 15:07:15 -0400 | [diff] [blame] | 785 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 786 | attrs.Width, |
| 787 | attrs.Height, |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 788 | format, |
| 789 | name, |
| 790 | pitch, |
| 791 | dri2_img); |
| 792 | if (dri2_img->dri_image == NULL) { |
| 793 | free(dri2_img); |
| 794 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 795 | return NULL; |
| 796 | } |
| 797 | |
| 798 | return &dri2_img->base; |
| 799 | } |
| 800 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 801 | _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 802 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 803 | _EGLContext *ctx, EGLenum target, |
| 804 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 805 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 806 | (void) drv; |
| 807 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 808 | switch (target) { |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 809 | case EGL_GL_RENDERBUFFER_KHR: |
| 810 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 811 | case EGL_DRM_BUFFER_MESA: |
| 812 | 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] | 813 | default: |
| 814 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 815 | return EGL_NO_IMAGE_KHR; |
| 816 | } |
| 817 | } |
| 818 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 819 | static EGLBoolean |
| 820 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 821 | { |
| 822 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 823 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 824 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 825 | (void) drv; |
| 826 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 827 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 828 | free(dri2_img); |
| 829 | |
| 830 | return EGL_TRUE; |
| 831 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 832 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 833 | static _EGLImage * |
| 834 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 835 | const EGLint *attr_list) |
| 836 | { |
| 837 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 838 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 839 | _EGLImageAttribs attrs; |
| 840 | unsigned int dri_use, valid_mask; |
| 841 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 842 | EGLint err = EGL_SUCCESS; |
| 843 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 844 | (void) drv; |
| 845 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 846 | dri2_img = malloc(sizeof *dri2_img); |
| 847 | if (!dri2_img) { |
| 848 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 849 | return EGL_NO_IMAGE_KHR; |
| 850 | } |
| 851 | |
| 852 | if (!attr_list) { |
| 853 | err = EGL_BAD_PARAMETER; |
| 854 | goto cleanup_img; |
| 855 | } |
| 856 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 857 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 858 | err = EGL_BAD_PARAMETER; |
| 859 | goto cleanup_img; |
| 860 | } |
| 861 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 862 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 863 | if (err != EGL_SUCCESS) |
| 864 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 865 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 866 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 867 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 868 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 869 | goto cleanup_img; |
| 870 | } |
| 871 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 872 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 873 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 874 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 875 | break; |
| 876 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 877 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 878 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 879 | goto cleanup_img; |
| 880 | } |
| 881 | |
| 882 | valid_mask = |
| 883 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
| 884 | EGL_DRM_BUFFER_USE_SHARE_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 885 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 886 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 887 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 888 | goto cleanup_img; |
| 889 | } |
| 890 | |
| 891 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 892 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 893 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 894 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 895 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
| 896 | |
| 897 | dri2_img->dri_image = |
| 898 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 899 | attrs.Width, attrs.Height, |
| 900 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 901 | if (dri2_img->dri_image == NULL) { |
| 902 | err = EGL_BAD_ALLOC; |
| 903 | goto cleanup_img; |
| 904 | } |
| 905 | |
| 906 | return &dri2_img->base; |
| 907 | |
| 908 | cleanup_img: |
| 909 | free(dri2_img); |
| 910 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 911 | |
| 912 | return EGL_NO_IMAGE_KHR; |
| 913 | } |
| 914 | |
| 915 | static EGLBoolean |
| 916 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 917 | EGLint *name, EGLint *handle, EGLint *stride) |
| 918 | { |
| 919 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 920 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 921 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 922 | (void) drv; |
| 923 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 924 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 925 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 926 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 927 | return EGL_FALSE; |
| 928 | } |
| 929 | |
| 930 | if (handle) |
| 931 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 932 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 933 | |
| 934 | if (stride) |
| 935 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 936 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 937 | |
| 938 | return EGL_TRUE; |
| 939 | } |
| 940 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 941 | static void |
| 942 | dri2_unload(_EGLDriver *drv) |
| 943 | { |
| 944 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 945 | |
| 946 | if (dri2_drv->handle) |
| 947 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 948 | free(dri2_drv); |
| 949 | } |
| 950 | |
| 951 | static EGLBoolean |
| 952 | dri2_load(_EGLDriver *drv) |
| 953 | { |
| 954 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 955 | #ifdef HAVE_SHARED_GLAPI |
| 956 | const char *libname = "libglapi.so.0"; |
| 957 | #else |
| 958 | /* |
| 959 | * Both libGL.so and libglapi.so are glapi providers. There is no way to |
| 960 | * tell which one to load. |
| 961 | */ |
| 962 | const char *libname = NULL; |
| 963 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 964 | void *handle; |
| 965 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 966 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 967 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 968 | if (handle) { |
| 969 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 970 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 971 | if (!dri2_drv->get_proc_address || !libname) { |
| 972 | /* no need to keep a reference */ |
| 973 | dlclose(handle); |
| 974 | handle = NULL; |
| 975 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 976 | } |
| 977 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 978 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 979 | if (!dri2_drv->get_proc_address) { |
| 980 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 981 | return EGL_FALSE; |
| 982 | } |
| 983 | |
| 984 | dri2_drv->glFlush = (void (*)(void)) |
| 985 | dri2_drv->get_proc_address("glFlush"); |
| 986 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 987 | dri2_drv->handle = handle; |
| 988 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 989 | return EGL_TRUE; |
| 990 | } |
| 991 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 992 | /** |
| 993 | * This is the main entrypoint into the driver, called by libEGL. |
| 994 | * Create a new _EGLDriver object and init its dispatch table. |
| 995 | */ |
| 996 | _EGLDriver * |
Chia-I Wu | c98ea26 | 2011-01-07 16:30:08 +0800 | [diff] [blame] | 997 | _EGL_MAIN(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 998 | { |
| 999 | struct dri2_egl_driver *dri2_drv; |
| 1000 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1001 | (void) args; |
| 1002 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1003 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1004 | if (!dri2_drv) |
| 1005 | return NULL; |
| 1006 | |
Chia-I Wu | 39812c4 | 2011-01-12 18:06:47 +0800 | [diff] [blame] | 1007 | memset(dri2_drv, 0, sizeof *dri2_drv); |
| 1008 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1009 | if (!dri2_load(&dri2_drv->base)) |
| 1010 | return NULL; |
| 1011 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1012 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1013 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1014 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1015 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 1016 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1017 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1018 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1019 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1020 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1021 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1022 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1023 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1024 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 1025 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1026 | |
| 1027 | dri2_drv->base.Name = "DRI2"; |
| 1028 | dri2_drv->base.Unload = dri2_unload; |
| 1029 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1030 | return &dri2_drv->base; |
| 1031 | } |