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