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 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame^] | 247 | __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); |
Chia-I Wu | 77e031a | 2011-06-08 16:33:56 +0800 | [diff] [blame] | 419 | if (!extensions) { |
| 420 | /* try again with swrastg */ |
| 421 | dri2_dpy->driver_name = "swrastg"; |
| 422 | extensions = dri2_open_driver(disp); |
| 423 | } |
| 424 | |
Chia-I Wu | cf69eea | 2011-06-08 16:33:55 +0800 | [diff] [blame] | 425 | if (!extensions) |
| 426 | return EGL_FALSE; |
| 427 | |
| 428 | if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) { |
| 429 | dlclose(dri2_dpy->driver); |
| 430 | return EGL_FALSE; |
| 431 | } |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 432 | |
| 433 | return EGL_TRUE; |
| 434 | } |
| 435 | |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame^] | 436 | void |
| 437 | dri2_setup_screen(_EGLDisplay *disp) |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 438 | { |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame^] | 439 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 440 | unsigned int api_mask; |
| 441 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 442 | if (dri2_dpy->dri2) { |
| 443 | if (dri2_dpy->dri2->base.version >= 2) |
| 444 | api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen); |
| 445 | else |
| 446 | api_mask = 1 << __DRI_API_OPENGL; |
| 447 | } else { |
| 448 | assert(dri2_dpy->swrast); |
| 449 | if (dri2_dpy->swrast->base.version >= 2) |
| 450 | api_mask = 1 << __DRI_API_OPENGL | 1 << __DRI_API_GLES | 1 << __DRI_API_GLES2; |
| 451 | else |
| 452 | api_mask = 1 << __DRI_API_OPENGL; |
| 453 | } |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 454 | |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 455 | disp->ClientAPIs = 0; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 456 | if (api_mask & (1 <<__DRI_API_OPENGL)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 457 | disp->ClientAPIs |= EGL_OPENGL_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 458 | if (api_mask & (1 <<__DRI_API_GLES)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 459 | disp->ClientAPIs |= EGL_OPENGL_ES_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 460 | if (api_mask & (1 << __DRI_API_GLES2)) |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 461 | disp->ClientAPIs |= EGL_OPENGL_ES2_BIT; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 462 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 463 | if (dri2_dpy->dri2) { |
| 464 | if (dri2_dpy->dri2->base.version >= 2) { |
| 465 | disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE; |
| 466 | disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE; |
| 467 | disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE; |
| 468 | } |
| 469 | } else { |
| 470 | assert(dri2_dpy->swrast); |
| 471 | if (dri2_dpy->swrast->base.version >= 2) { |
| 472 | disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE; |
| 473 | disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE; |
| 474 | disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE; |
| 475 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 478 | if (dri2_dpy->image) { |
| 479 | disp->Extensions.MESA_drm_image = EGL_TRUE; |
| 480 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 481 | disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE; |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 482 | } |
Benjamin Franzke | e5fc4c8 | 2011-05-30 10:50:52 +0200 | [diff] [blame^] | 483 | } |
| 484 | |
| 485 | EGLBoolean |
| 486 | dri2_create_screen(_EGLDisplay *disp) |
| 487 | { |
| 488 | const __DRIextension **extensions; |
| 489 | struct dri2_egl_display *dri2_dpy; |
| 490 | |
| 491 | dri2_dpy = disp->DriverData; |
| 492 | |
| 493 | if (dri2_dpy->dri2) { |
| 494 | dri2_dpy->dri_screen = |
| 495 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
| 496 | &dri2_dpy->driver_configs, disp); |
| 497 | } else { |
| 498 | assert(dri2_dpy->swrast); |
| 499 | dri2_dpy->dri_screen = |
| 500 | dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions, |
| 501 | &dri2_dpy->driver_configs, disp); |
| 502 | } |
| 503 | |
| 504 | if (dri2_dpy->dri_screen == NULL) { |
| 505 | _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen"); |
| 506 | return EGL_FALSE; |
| 507 | } |
| 508 | |
| 509 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
| 510 | |
| 511 | if (dri2_dpy->dri2) { |
| 512 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
| 513 | goto cleanup_dri_screen; |
| 514 | } else { |
| 515 | assert(dri2_dpy->swrast); |
| 516 | if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions)) |
| 517 | goto cleanup_dri_screen; |
| 518 | } |
| 519 | |
| 520 | dri2_setup_screen(disp); |
Kristian Høgsberg | 981e338 | 2011-03-07 20:18:29 -0500 | [diff] [blame] | 521 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 522 | return EGL_TRUE; |
| 523 | |
| 524 | cleanup_dri_screen: |
| 525 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 526 | |
| 527 | return EGL_FALSE; |
| 528 | } |
| 529 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 530 | /** |
| 531 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 532 | */ |
| 533 | static EGLBoolean |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 534 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp) |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 535 | { |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 536 | /* not until swrast_dri is supported */ |
| 537 | if (disp->Options.UseFallback) |
| 538 | return EGL_FALSE; |
| 539 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 540 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 541 | #ifdef HAVE_X11_PLATFORM |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 542 | case _EGL_PLATFORM_X11: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 543 | if (disp->Options.TestOnly) |
| 544 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 545 | return dri2_initialize_x11(drv, disp); |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 546 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 547 | |
| 548 | #ifdef HAVE_LIBUDEV |
| 549 | case _EGL_PLATFORM_DRM: |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 550 | if (disp->Options.TestOnly) |
| 551 | return EGL_TRUE; |
Chia-I Wu | a4a38dc | 2011-01-13 16:53:13 +0800 | [diff] [blame] | 552 | return dri2_initialize_drm(drv, disp); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 553 | #ifdef HAVE_WAYLAND_PLATFORM |
| 554 | case _EGL_PLATFORM_WAYLAND: |
| 555 | if (disp->Options.TestOnly) |
| 556 | return EGL_TRUE; |
| 557 | return dri2_initialize_wayland(drv, disp); |
| 558 | #endif |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 559 | #endif |
| 560 | |
| 561 | default: |
| 562 | return EGL_FALSE; |
| 563 | } |
| 564 | } |
| 565 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 566 | /** |
| 567 | * Called via eglTerminate(), drv->API.Terminate(). |
| 568 | */ |
| 569 | static EGLBoolean |
| 570 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 571 | { |
| 572 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 573 | |
| 574 | _eglReleaseDisplayResources(drv, disp); |
| 575 | _eglCleanupDisplay(disp); |
| 576 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 577 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 578 | if (dri2_dpy->fd) |
| 579 | close(dri2_dpy->fd); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 580 | dlclose(dri2_dpy->driver); |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 581 | |
| 582 | if (disp->PlatformDisplay == NULL) { |
| 583 | switch (disp->Platform) { |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 584 | #ifdef HAVE_X11_PLATFORM |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 585 | case _EGL_PLATFORM_X11: |
| 586 | xcb_disconnect(dri2_dpy->conn); |
| 587 | break; |
Kristian Høgsberg | 93aea63 | 2011-05-11 13:58:37 -0400 | [diff] [blame] | 588 | #endif |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 589 | #ifdef HAVE_WAYLAND_PLATFORM |
| 590 | case _EGL_PLATFORM_WAYLAND: |
| 591 | wl_display_destroy(dri2_dpy->wl_dpy); |
| 592 | break; |
| 593 | #endif |
| 594 | default: |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 599 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 600 | disp->DriverData = NULL; |
| 601 | |
| 602 | return EGL_TRUE; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /** |
| 607 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 608 | */ |
| 609 | static _EGLContext * |
| 610 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 611 | _EGLContext *share_list, const EGLint *attrib_list) |
| 612 | { |
| 613 | struct dri2_egl_context *dri2_ctx; |
| 614 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 615 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 616 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 617 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 618 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 619 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 620 | (void) drv; |
| 621 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 622 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 623 | if (!dri2_ctx) { |
| 624 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 625 | return NULL; |
| 626 | } |
| 627 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 628 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 629 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 630 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 631 | switch (dri2_ctx->base.ClientAPI) { |
| 632 | case EGL_OPENGL_ES_API: |
| 633 | switch (dri2_ctx->base.ClientVersion) { |
| 634 | case 1: |
| 635 | api = __DRI_API_GLES; |
| 636 | break; |
| 637 | case 2: |
| 638 | api = __DRI_API_GLES2; |
| 639 | break; |
| 640 | default: |
| 641 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 642 | return NULL; |
| 643 | } |
| 644 | break; |
| 645 | case EGL_OPENGL_API: |
| 646 | api = __DRI_API_OPENGL; |
| 647 | break; |
| 648 | default: |
| 649 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 650 | return NULL; |
| 651 | } |
| 652 | |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 653 | if (conf != NULL) { |
| 654 | /* The config chosen here isn't necessarily |
| 655 | * used for surfaces later. |
| 656 | * A pixmap surface will use the single config. |
| 657 | * This opportunity depends on disabling the |
| 658 | * doubleBufferMode check in |
| 659 | * src/mesa/main/context.c:check_compatible() |
| 660 | */ |
| 661 | if (dri2_config->dri_double_config) |
| 662 | dri_config = dri2_config->dri_double_config; |
| 663 | else |
| 664 | dri_config = dri2_config->dri_single_config; |
| 665 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame] | 666 | else |
| 667 | dri_config = NULL; |
| 668 | |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 669 | if (dri2_dpy->dri2) { |
| 670 | if (dri2_dpy->dri2->base.version >= 2) { |
| 671 | dri2_ctx->dri_context = |
| 672 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 673 | api, |
| 674 | dri_config, |
| 675 | dri2_ctx_shared ? |
| 676 | dri2_ctx_shared->dri_context : NULL, |
| 677 | dri2_ctx); |
| 678 | } else if (api == __DRI_API_OPENGL) { |
| 679 | dri2_ctx->dri_context = |
| 680 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
| 681 | dri_config, |
| 682 | dri2_ctx_shared ? |
| 683 | dri2_ctx_shared->dri_context : NULL, |
| 684 | dri2_ctx); |
| 685 | } else { |
| 686 | /* fail */ |
| 687 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 688 | } else { |
Haitao Feng | f55d027 | 2011-02-16 23:05:15 -0500 | [diff] [blame] | 689 | assert(dri2_dpy->swrast); |
| 690 | if (dri2_dpy->swrast->base.version >= 2) { |
| 691 | dri2_ctx->dri_context = |
| 692 | dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen, |
| 693 | api, |
| 694 | dri_config, |
| 695 | dri2_ctx_shared ? |
| 696 | dri2_ctx_shared->dri_context : NULL, |
| 697 | dri2_ctx); |
| 698 | } else if (api == __DRI_API_OPENGL) { |
| 699 | dri2_ctx->dri_context = |
| 700 | dri2_dpy->core->createNewContext(dri2_dpy->dri_screen, |
| 701 | dri_config, |
| 702 | dri2_ctx_shared ? |
| 703 | dri2_ctx_shared->dri_context : NULL, |
| 704 | dri2_ctx); |
| 705 | } else { |
| 706 | /* fail */ |
| 707 | } |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 708 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 709 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 710 | if (!dri2_ctx->dri_context) |
| 711 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 712 | |
| 713 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 714 | |
| 715 | cleanup: |
| 716 | free(dri2_ctx); |
| 717 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 718 | } |
| 719 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 720 | /** |
| 721 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 722 | */ |
| 723 | static EGLBoolean |
| 724 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 725 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 726 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 727 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 728 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 729 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 730 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 731 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 732 | _EGLContext *old_ctx; |
| 733 | _EGLSurface *old_dsurf, *old_rsurf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 734 | __DRIdrawable *ddraw, *rdraw; |
| 735 | __DRIcontext *cctx; |
| 736 | |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 737 | /* make new bindings */ |
| 738 | if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 739 | return EGL_FALSE; |
| 740 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 741 | /* flush before context switch */ |
Chia-I Wu | 547e761 | 2010-10-26 15:00:56 +0800 | [diff] [blame] | 742 | if (old_ctx && dri2_drv->glFlush) |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 743 | dri2_drv->glFlush(); |
| 744 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 745 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 746 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 747 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 748 | |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 749 | if (old_ctx) { |
| 750 | __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context; |
| 751 | dri2_dpy->core->unbindContext(old_cctx); |
| 752 | } |
| 753 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 754 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 755 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Benjamin Franzke | 4e8f95f | 2011-02-09 21:19:45 +0100 | [diff] [blame] | 756 | if (old_dsurf) |
| 757 | drv->API.DestroySurface(drv, disp, old_dsurf); |
| 758 | if (old_rsurf) |
| 759 | drv->API.DestroySurface(drv, disp, old_rsurf); |
| 760 | /* no destroy? */ |
| 761 | if (old_ctx) |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 762 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 763 | |
| 764 | return EGL_TRUE; |
| 765 | } else { |
Chia-I Wu | d19afc5 | 2010-10-23 12:52:26 +0800 | [diff] [blame] | 766 | /* undo the previous _eglBindContext */ |
| 767 | _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf); |
| 768 | assert(&dri2_ctx->base == ctx && |
| 769 | &dri2_dsurf->base == dsurf && |
| 770 | &dri2_rsurf->base == rsurf); |
| 771 | |
| 772 | _eglPutSurface(dsurf); |
| 773 | _eglPutSurface(rsurf); |
| 774 | _eglPutContext(ctx); |
| 775 | |
| 776 | _eglPutSurface(old_dsurf); |
| 777 | _eglPutSurface(old_rsurf); |
| 778 | _eglPutContext(old_ctx); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 779 | |
| 780 | return EGL_FALSE; |
| 781 | } |
| 782 | } |
| 783 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 784 | /* |
| 785 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 786 | */ |
| 787 | static _EGLProc |
| 788 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 789 | { |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 790 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 791 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 792 | return dri2_drv->get_proc_address(procname); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 796 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 797 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 798 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 799 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 800 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 801 | (void) drv; |
| 802 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 803 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 804 | * we need to copy fake to real here.*/ |
| 805 | |
| 806 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 807 | |
| 808 | return EGL_TRUE; |
| 809 | } |
| 810 | |
| 811 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 812 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 813 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 814 | (void) drv; |
| 815 | (void) disp; |
| 816 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 817 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 818 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 819 | /* glXWaitX(); */ |
| 820 | |
| 821 | return EGL_TRUE; |
| 822 | } |
| 823 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 824 | static EGLBoolean |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 825 | dri2_bind_tex_image(_EGLDriver *drv, |
| 826 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 827 | { |
| 828 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 829 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 830 | struct dri2_egl_context *dri2_ctx; |
| 831 | _EGLContext *ctx; |
| 832 | GLint format, target; |
| 833 | |
| 834 | ctx = _eglGetCurrentContext(); |
| 835 | dri2_ctx = dri2_egl_context(ctx); |
| 836 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 837 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 838 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 839 | |
| 840 | switch (dri2_surf->base.TextureFormat) { |
| 841 | case EGL_TEXTURE_RGB: |
| 842 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 843 | break; |
| 844 | case EGL_TEXTURE_RGBA: |
| 845 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 846 | break; |
| 847 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 848 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | switch (dri2_surf->base.TextureTarget) { |
| 852 | case EGL_TEXTURE_2D: |
| 853 | target = GL_TEXTURE_2D; |
| 854 | break; |
| 855 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 856 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 860 | target, format, |
| 861 | dri2_surf->dri_drawable); |
| 862 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 863 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static EGLBoolean |
| 867 | dri2_release_tex_image(_EGLDriver *drv, |
| 868 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 869 | { |
Juan Zhao | e59fa4c | 2011-01-09 12:03:02 -0500 | [diff] [blame] | 870 | #if __DRI_TEX_BUFFER_VERSION >= 3 |
| 871 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 872 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 873 | struct dri2_egl_context *dri2_ctx; |
| 874 | _EGLContext *ctx; |
| 875 | GLint target; |
| 876 | |
| 877 | ctx = _eglGetCurrentContext(); |
| 878 | dri2_ctx = dri2_egl_context(ctx); |
| 879 | |
| 880 | if (!_eglReleaseTexImage(drv, disp, surf, buffer)) |
| 881 | return EGL_FALSE; |
| 882 | |
| 883 | switch (dri2_surf->base.TextureTarget) { |
| 884 | case EGL_TEXTURE_2D: |
| 885 | target = GL_TEXTURE_2D; |
| 886 | break; |
| 887 | default: |
| 888 | assert(0); |
| 889 | } |
| 890 | if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL) |
| 891 | (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context, |
| 892 | target, |
| 893 | dri2_surf->dri_drawable); |
| 894 | #endif |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 895 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 896 | return EGL_TRUE; |
| 897 | } |
| 898 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 899 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 900 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 901 | EGLClientBuffer buffer, |
| 902 | const EGLint *attr_list) |
| 903 | { |
| 904 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 905 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 906 | struct dri2_egl_image *dri2_img; |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 907 | GLuint renderbuffer = (GLuint) (uintptr_t) buffer; |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 908 | |
| 909 | if (renderbuffer == 0) { |
| 910 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 911 | return EGL_NO_IMAGE_KHR; |
| 912 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 913 | |
| 914 | dri2_img = malloc(sizeof *dri2_img); |
| 915 | if (!dri2_img) { |
| 916 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 917 | return EGL_NO_IMAGE_KHR; |
| 918 | } |
| 919 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 920 | if (!_eglInitImage(&dri2_img->base, disp)) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 921 | return EGL_NO_IMAGE_KHR; |
| 922 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 923 | dri2_img->dri_image = |
| 924 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
| 925 | renderbuffer, |
| 926 | dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 927 | |
| 928 | return &dri2_img->base; |
| 929 | } |
| 930 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 931 | static _EGLImage * |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 932 | dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 933 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 934 | { |
| 935 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 936 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 937 | EGLint format, name, pitch, err; |
| 938 | _EGLImageAttribs attrs; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 939 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 940 | (void) ctx; |
| 941 | |
José Fonseca | 6a66189 | 2011-05-12 15:12:16 +0100 | [diff] [blame] | 942 | name = (EGLint) (uintptr_t) buffer; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 943 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 944 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 945 | if (err != EGL_SUCCESS) |
| 946 | return NULL; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 947 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 948 | if (attrs.Width <= 0 || attrs.Height <= 0 || |
| 949 | attrs.DRMBufferStrideMESA <= 0) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 950 | _eglError(EGL_BAD_PARAMETER, |
| 951 | "bad width, height or stride"); |
| 952 | return NULL; |
| 953 | } |
| 954 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 955 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 956 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 957 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 958 | pitch = attrs.DRMBufferStrideMESA; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 959 | break; |
| 960 | default: |
| 961 | _eglError(EGL_BAD_PARAMETER, |
| 962 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | dri2_img = malloc(sizeof *dri2_img); |
| 967 | if (!dri2_img) { |
| 968 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 969 | return NULL; |
| 970 | } |
| 971 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 972 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 973 | free(dri2_img); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | dri2_img->dri_image = |
Kristian Høgsberg | 9ec0b2a | 2010-09-22 15:07:15 -0400 | [diff] [blame] | 978 | dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 979 | attrs.Width, |
| 980 | attrs.Height, |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 981 | format, |
| 982 | name, |
| 983 | pitch, |
| 984 | dri2_img); |
| 985 | if (dri2_img->dri_image == NULL) { |
| 986 | free(dri2_img); |
| 987 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm"); |
| 988 | return NULL; |
| 989 | } |
| 990 | |
| 991 | return &dri2_img->base; |
| 992 | } |
| 993 | |
José Fonseca | 5de2c67 | 2011-05-12 15:24:26 +0100 | [diff] [blame] | 994 | #ifdef HAVE_WAYLAND_PLATFORM |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 995 | static _EGLImage * |
| 996 | dri2_reference_drm_image(_EGLDisplay *disp, _EGLContext *ctx, |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 997 | __DRIimage *dri_image, EGLint width, EGLint height) |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 998 | { |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 999 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1000 | EGLint attr_list[] = { |
| 1001 | EGL_WIDTH, 0, |
| 1002 | EGL_HEIGHT, 0, |
| 1003 | EGL_DRM_BUFFER_STRIDE_MESA, 0, |
| 1004 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
| 1005 | EGL_NONE |
| 1006 | }; |
| 1007 | EGLint name, stride; |
| 1008 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1009 | dri2_dpy->image->queryImage(dri_image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 1010 | dri2_dpy->image->queryImage(dri_image, __DRI_IMAGE_ATTRIB_STRIDE, &stride); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1011 | |
| 1012 | attr_list[1] = width; |
| 1013 | attr_list[3] = height; |
| 1014 | attr_list[5] = stride / 4; |
| 1015 | |
| 1016 | return dri2_create_image_mesa_drm_buffer(disp, ctx, |
| 1017 | (EGLClientBuffer)(intptr_t) name, |
| 1018 | attr_list); |
| 1019 | } |
| 1020 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1021 | static _EGLImage * |
| 1022 | dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx, |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1023 | EGLClientBuffer _buffer, |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1024 | const EGLint *attr_list) |
| 1025 | { |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1026 | struct wl_buffer *buffer = (struct wl_buffer *) _buffer; |
| 1027 | (void) attr_list; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1028 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1029 | if (!wayland_buffer_is_drm(buffer)) |
| 1030 | return NULL; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1031 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1032 | return dri2_reference_drm_image(disp, ctx, |
| 1033 | wayland_drm_buffer_get_buffer(buffer), |
| 1034 | buffer->width, |
| 1035 | buffer->height); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1036 | } |
| 1037 | #endif |
| 1038 | |
Kristian Høgsberg | 2889d96 | 2011-02-02 22:10:40 -0500 | [diff] [blame] | 1039 | _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1040 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 1041 | _EGLContext *ctx, EGLenum target, |
| 1042 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1043 | { |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1044 | (void) drv; |
| 1045 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1046 | switch (target) { |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1047 | case EGL_GL_RENDERBUFFER_KHR: |
| 1048 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1049 | case EGL_DRM_BUFFER_MESA: |
| 1050 | return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1051 | #ifdef HAVE_WAYLAND_PLATFORM |
| 1052 | case EGL_WAYLAND_BUFFER_WL: |
| 1053 | return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list); |
| 1054 | #endif |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1055 | default: |
| 1056 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1057 | return EGL_NO_IMAGE_KHR; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1061 | static EGLBoolean |
| 1062 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 1063 | { |
| 1064 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1065 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 1066 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1067 | (void) drv; |
| 1068 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1069 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 1070 | free(dri2_img); |
| 1071 | |
| 1072 | return EGL_TRUE; |
| 1073 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1074 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1075 | static _EGLImage * |
| 1076 | dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, |
| 1077 | const EGLint *attr_list) |
| 1078 | { |
| 1079 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1080 | struct dri2_egl_image *dri2_img; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1081 | _EGLImageAttribs attrs; |
| 1082 | unsigned int dri_use, valid_mask; |
| 1083 | int format; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1084 | EGLint err = EGL_SUCCESS; |
| 1085 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1086 | (void) drv; |
| 1087 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1088 | dri2_img = malloc(sizeof *dri2_img); |
| 1089 | if (!dri2_img) { |
| 1090 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1091 | return EGL_NO_IMAGE_KHR; |
| 1092 | } |
| 1093 | |
| 1094 | if (!attr_list) { |
| 1095 | err = EGL_BAD_PARAMETER; |
| 1096 | goto cleanup_img; |
| 1097 | } |
| 1098 | |
Chia-I Wu | 713c873 | 2010-10-22 16:36:47 +0800 | [diff] [blame] | 1099 | if (!_eglInitImage(&dri2_img->base, disp)) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1100 | err = EGL_BAD_PARAMETER; |
| 1101 | goto cleanup_img; |
| 1102 | } |
| 1103 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1104 | err = _eglParseImageAttribList(&attrs, disp, attr_list); |
| 1105 | if (err != EGL_SUCCESS) |
| 1106 | goto cleanup_img; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1107 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1108 | if (attrs.Width <= 0 || attrs.Height <= 0) { |
| 1109 | _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", |
| 1110 | attrs.Width, attrs.Height); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1111 | goto cleanup_img; |
| 1112 | } |
| 1113 | |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1114 | switch (attrs.DRMBufferFormatMESA) { |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1115 | case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: |
| 1116 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 1117 | break; |
| 1118 | default: |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1119 | _eglLog(_EGL_WARNING, "bad image format value 0x%04x", |
| 1120 | attrs.DRMBufferFormatMESA); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1121 | goto cleanup_img; |
| 1122 | } |
| 1123 | |
| 1124 | valid_mask = |
| 1125 | EGL_DRM_BUFFER_USE_SCANOUT_MESA | |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 1126 | EGL_DRM_BUFFER_USE_SHARE_MESA | |
| 1127 | EGL_DRM_BUFFER_USE_CURSOR_MESA; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1128 | if (attrs.DRMBufferUseMESA & ~valid_mask) { |
| 1129 | _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", |
| 1130 | attrs.DRMBufferUseMESA & ~valid_mask); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1131 | goto cleanup_img; |
| 1132 | } |
| 1133 | |
| 1134 | dri_use = 0; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1135 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1136 | dri_use |= __DRI_IMAGE_USE_SHARE; |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1137 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1138 | dri_use |= __DRI_IMAGE_USE_SCANOUT; |
Kristian Høgsberg | e5169e9 | 2011-05-06 10:31:18 -0400 | [diff] [blame] | 1139 | if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) |
| 1140 | dri_use |= __DRI_IMAGE_USE_CURSOR; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1141 | |
| 1142 | dri2_img->dri_image = |
| 1143 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
Chia-I Wu | 5664a983 | 2010-10-22 17:09:40 +0800 | [diff] [blame] | 1144 | attrs.Width, attrs.Height, |
| 1145 | format, dri_use, dri2_img); |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1146 | if (dri2_img->dri_image == NULL) { |
| 1147 | err = EGL_BAD_ALLOC; |
| 1148 | goto cleanup_img; |
| 1149 | } |
| 1150 | |
| 1151 | return &dri2_img->base; |
| 1152 | |
| 1153 | cleanup_img: |
| 1154 | free(dri2_img); |
| 1155 | _eglError(err, "dri2_create_drm_image_mesa"); |
| 1156 | |
| 1157 | return EGL_NO_IMAGE_KHR; |
| 1158 | } |
| 1159 | |
| 1160 | static EGLBoolean |
| 1161 | dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img, |
| 1162 | EGLint *name, EGLint *handle, EGLint *stride) |
| 1163 | { |
| 1164 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1165 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 1166 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1167 | (void) drv; |
| 1168 | |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1169 | if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1170 | __DRI_IMAGE_ATTRIB_NAME, name)) { |
| 1171 | _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa"); |
| 1172 | return EGL_FALSE; |
| 1173 | } |
| 1174 | |
| 1175 | if (handle) |
| 1176 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1177 | __DRI_IMAGE_ATTRIB_HANDLE, handle); |
| 1178 | |
| 1179 | if (stride) |
| 1180 | dri2_dpy->image->queryImage(dri2_img->dri_image, |
| 1181 | __DRI_IMAGE_ATTRIB_STRIDE, stride); |
| 1182 | |
| 1183 | return EGL_TRUE; |
| 1184 | } |
| 1185 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1186 | #ifdef HAVE_WAYLAND_PLATFORM |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1187 | |
| 1188 | static void * |
| 1189 | dri2_wl_reference_buffer(void *user_data, uint32_t name, |
| 1190 | int32_t width, int32_t height, |
| 1191 | uint32_t stride, struct wl_visual *visual) |
| 1192 | { |
| 1193 | _EGLDisplay *disp = user_data; |
| 1194 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1195 | __DRIimage *image; |
| 1196 | |
| 1197 | image = dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen, |
| 1198 | width, height, |
| 1199 | __DRI_IMAGE_FORMAT_ARGB8888, |
| 1200 | name, stride / 4, |
| 1201 | NULL); |
| 1202 | |
| 1203 | return image; |
| 1204 | } |
| 1205 | |
| 1206 | static void |
| 1207 | dri2_wl_release_buffer(void *user_data, void *buffer) |
| 1208 | { |
| 1209 | _EGLDisplay *disp = user_data; |
| 1210 | __DRIimage *image = buffer; |
| 1211 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1212 | |
| 1213 | dri2_dpy->image->destroyImage(image); |
| 1214 | } |
| 1215 | |
| 1216 | static struct wayland_drm_callbacks wl_drm_callbacks = { |
| 1217 | .authenticate = NULL, |
| 1218 | .reference_buffer = dri2_wl_reference_buffer, |
| 1219 | .release_buffer = dri2_wl_release_buffer |
| 1220 | }; |
| 1221 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1222 | static EGLBoolean |
| 1223 | dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1224 | struct wl_display *wl_dpy) |
| 1225 | { |
| 1226 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1227 | |
| 1228 | (void) drv; |
| 1229 | |
| 1230 | if (dri2_dpy->wl_server_drm) |
| 1231 | return EGL_FALSE; |
| 1232 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1233 | wl_drm_callbacks.authenticate = |
| 1234 | (int(*)(void *, uint32_t)) dri2_dpy->authenticate; |
| 1235 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1236 | dri2_dpy->wl_server_drm = |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1237 | wayland_drm_init(wl_dpy, dri2_dpy->device_name, |
| 1238 | &wl_drm_callbacks, disp); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1239 | |
| 1240 | if (!dri2_dpy->wl_server_drm) |
| 1241 | return EGL_FALSE; |
| 1242 | |
| 1243 | return EGL_TRUE; |
| 1244 | } |
| 1245 | |
| 1246 | static EGLBoolean |
| 1247 | dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp, |
| 1248 | struct wl_display *wl_dpy) |
| 1249 | { |
| 1250 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1251 | |
| 1252 | (void) drv; |
| 1253 | |
| 1254 | if (!dri2_dpy->wl_server_drm) |
| 1255 | return EGL_FALSE; |
| 1256 | |
Benjamin Franzke | 834b841 | 2011-04-30 11:17:01 +0200 | [diff] [blame] | 1257 | wayland_drm_uninit(dri2_dpy->wl_server_drm); |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1258 | dri2_dpy->wl_server_drm = NULL; |
| 1259 | |
| 1260 | return EGL_TRUE; |
| 1261 | } |
| 1262 | #endif |
| 1263 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1264 | static void |
| 1265 | dri2_unload(_EGLDriver *drv) |
| 1266 | { |
| 1267 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1268 | |
| 1269 | if (dri2_drv->handle) |
| 1270 | dlclose(dri2_drv->handle); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1271 | free(dri2_drv); |
| 1272 | } |
| 1273 | |
| 1274 | static EGLBoolean |
| 1275 | dri2_load(_EGLDriver *drv) |
| 1276 | { |
| 1277 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1278 | #ifdef HAVE_SHARED_GLAPI |
| 1279 | const char *libname = "libglapi.so.0"; |
| 1280 | #else |
| 1281 | /* |
| 1282 | * Both libGL.so and libglapi.so are glapi providers. There is no way to |
| 1283 | * tell which one to load. |
| 1284 | */ |
| 1285 | const char *libname = NULL; |
| 1286 | #endif |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1287 | void *handle; |
| 1288 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1289 | /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */ |
| 1290 | handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL); |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1291 | if (handle) { |
| 1292 | dri2_drv->get_proc_address = (_EGLProc (*)(const char *)) |
| 1293 | dlsym(handle, "_glapi_get_proc_address"); |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1294 | if (!dri2_drv->get_proc_address || !libname) { |
| 1295 | /* no need to keep a reference */ |
| 1296 | dlclose(handle); |
| 1297 | handle = NULL; |
| 1298 | } |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1301 | /* if glapi is not available, loading DRI drivers will fail */ |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1302 | if (!dri2_drv->get_proc_address) { |
| 1303 | _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address"); |
| 1304 | return EGL_FALSE; |
| 1305 | } |
| 1306 | |
| 1307 | dri2_drv->glFlush = (void (*)(void)) |
| 1308 | dri2_drv->get_proc_address("glFlush"); |
| 1309 | |
Chia-I Wu | 218381d | 2011-01-30 05:09:06 +0800 | [diff] [blame] | 1310 | dri2_drv->handle = handle; |
| 1311 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1312 | return EGL_TRUE; |
| 1313 | } |
| 1314 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1315 | /** |
| 1316 | * This is the main entrypoint into the driver, called by libEGL. |
| 1317 | * Create a new _EGLDriver object and init its dispatch table. |
| 1318 | */ |
| 1319 | _EGLDriver * |
Chia-I Wu | c98ea26 | 2011-01-07 16:30:08 +0800 | [diff] [blame] | 1320 | _EGL_MAIN(const char *args) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1321 | { |
| 1322 | struct dri2_egl_driver *dri2_drv; |
| 1323 | |
Ian Romanick | eade946 | 2010-09-23 09:39:42 -0700 | [diff] [blame] | 1324 | (void) args; |
| 1325 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1326 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1327 | if (!dri2_drv) |
| 1328 | return NULL; |
| 1329 | |
Chia-I Wu | 39812c4 | 2011-01-12 18:06:47 +0800 | [diff] [blame] | 1330 | memset(dri2_drv, 0, sizeof *dri2_drv); |
| 1331 | |
Chia-I Wu | fef5d14 | 2011-01-07 15:02:41 +0800 | [diff] [blame] | 1332 | if (!dri2_load(&dri2_drv->base)) |
| 1333 | return NULL; |
| 1334 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1335 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1336 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1337 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1338 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 1339 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1340 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1341 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1342 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1343 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1344 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1345 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1346 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 5aaa53e | 2010-06-03 21:36:40 -0400 | [diff] [blame] | 1347 | dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa; |
| 1348 | dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1349 | #ifdef HAVE_WAYLAND_PLATFORM |
| 1350 | dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl; |
| 1351 | dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl; |
| 1352 | #endif |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1353 | |
| 1354 | dri2_drv->base.Name = "DRI2"; |
| 1355 | dri2_drv->base.Unload = dri2_unload; |
| 1356 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1357 | return &dri2_drv->base; |
| 1358 | } |