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