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> |
| 39 | #include <xcb/xcb.h> |
| 40 | #include <xcb/dri2.h> |
| 41 | #include <xcb/xfixes.h> |
| 42 | #include <X11/Xlib-xcb.h> |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
| 45 | |
| 46 | #ifdef HAVE_LIBUDEV |
| 47 | #include <libudev.h> |
| 48 | #endif |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 49 | |
| 50 | #include <glapi/glapi.h> |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 51 | #include "eglconfig.h" |
| 52 | #include "eglcontext.h" |
| 53 | #include "egldisplay.h" |
| 54 | #include "egldriver.h" |
| 55 | #include "eglcurrent.h" |
| 56 | #include "egllog.h" |
| 57 | #include "eglsurface.h" |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 58 | #include "eglimage.h" |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 59 | |
Kristian Høgsberg | 52c554a | 2010-05-06 22:01:35 -0400 | [diff] [blame] | 60 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 61 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 62 | struct dri2_egl_driver |
| 63 | { |
| 64 | _EGLDriver base; |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 65 | |
| 66 | void (*glFlush)(void); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct dri2_egl_display |
| 70 | { |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 71 | xcb_connection_t *conn; |
| 72 | int dri2_major; |
| 73 | int dri2_minor; |
| 74 | __DRIscreen *dri_screen; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 75 | const __DRIconfig **driver_configs; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 76 | void *driver; |
| 77 | __DRIcoreExtension *core; |
| 78 | __DRIdri2Extension *dri2; |
| 79 | __DRI2flushExtension *flush; |
| 80 | __DRItexBufferExtension *tex_buffer; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 81 | __DRIimageExtension *image; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 82 | int fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 83 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 84 | char *device_name; |
| 85 | char *driver_name; |
| 86 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 87 | __DRIdri2LoaderExtension loader_extension; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 88 | const __DRIextension *extensions[3]; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct dri2_egl_context |
| 92 | { |
| 93 | _EGLContext base; |
| 94 | __DRIcontext *dri_context; |
| 95 | }; |
| 96 | |
| 97 | struct dri2_egl_surface |
| 98 | { |
| 99 | _EGLSurface base; |
| 100 | __DRIdrawable *dri_drawable; |
| 101 | xcb_drawable_t drawable; |
| 102 | __DRIbuffer buffers[5]; |
| 103 | int buffer_count; |
| 104 | xcb_xfixes_region_t region; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 105 | int have_fake_front; |
| 106 | int swap_interval; |
| 107 | }; |
| 108 | |
| 109 | struct dri2_egl_config |
| 110 | { |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 111 | _EGLConfig base; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 112 | const __DRIconfig *dri_config; |
| 113 | }; |
| 114 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 115 | struct dri2_egl_image |
| 116 | { |
| 117 | _EGLImage base; |
| 118 | __DRIimage *dri_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 119 | }; |
| 120 | |
Chia-I Wu | a35e633 | 2010-02-05 11:38:34 +0800 | [diff] [blame] | 121 | /* standard typecasts */ |
| 122 | _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 123 | _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 124 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 125 | static const __DRIuseInvalidateExtension use_invalidate = { |
| 126 | { __DRI_USE_INVALIDATE, 1 } |
| 127 | }; |
| 128 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 129 | EGLint dri2_to_egl_attribute_map[] = { |
| 130 | 0, |
| 131 | EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */ |
| 132 | EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */ |
| 133 | EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */ |
| 134 | EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */ |
| 135 | EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 136 | EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 137 | EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */ |
| 138 | 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */ |
| 139 | EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */ |
| 140 | EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */ |
| 141 | 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */ |
| 142 | 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */ |
| 143 | 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */ |
| 144 | 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */ |
| 145 | EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */ |
| 146 | EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */ |
| 147 | 0, /* __DRI_ATTRIB_RENDER_TYPE, */ |
| 148 | 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */ |
| 149 | 0, /* __DRI_ATTRIB_CONFORMANT */ |
| 150 | 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */ |
| 151 | 0, /* __DRI_ATTRIB_STEREO */ |
| 152 | 0, /* __DRI_ATTRIB_AUX_BUFFERS */ |
| 153 | 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */ |
| 154 | 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */ |
| 155 | 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */ |
| 156 | 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */ |
| 157 | 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */ |
| 158 | 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */ |
| 159 | 0, /* __DRI_ATTRIB_FLOAT_MODE */ |
| 160 | 0, /* __DRI_ATTRIB_RED_MASK */ |
| 161 | 0, /* __DRI_ATTRIB_GREEN_MASK */ |
| 162 | 0, /* __DRI_ATTRIB_BLUE_MASK */ |
| 163 | 0, /* __DRI_ATTRIB_ALPHA_MASK */ |
| 164 | EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */ |
| 165 | EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */ |
| 166 | EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */ |
| 167 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */ |
| 168 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */ |
| 169 | 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */ |
| 170 | 0, /* __DRI_ATTRIB_SWAP_METHOD */ |
| 171 | EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */ |
| 172 | EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 173 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */ |
| 174 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 175 | 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */ |
| 176 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */ |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 177 | EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 178 | }; |
| 179 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 180 | static struct dri2_egl_config * |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 181 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id, |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 182 | int depth, EGLint surface_type) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 183 | { |
| 184 | struct dri2_egl_config *conf; |
| 185 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 186 | _EGLConfig base; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 187 | unsigned int attrib, value, double_buffer; |
| 188 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 189 | int i; |
| 190 | |
| 191 | dri2_dpy = disp->DriverData; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 192 | _eglInitConfig(&base, disp, id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 193 | |
| 194 | i = 0; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 195 | double_buffer = 0; |
| 196 | bind_to_texture_rgb = 0; |
| 197 | bind_to_texture_rgba = 0; |
| 198 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 199 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 200 | switch (attrib) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 201 | case __DRI_ATTRIB_RENDER_TYPE: |
| 202 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 203 | value = EGL_RGB_BUFFER; |
| 204 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 205 | value = EGL_LUMINANCE_BUFFER; |
| 206 | else |
| 207 | /* not valid */; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 208 | _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 209 | break; |
| 210 | |
| 211 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 212 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 213 | value = EGL_NON_CONFORMANT_CONFIG; |
| 214 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 215 | value = EGL_SLOW_CONFIG; |
| 216 | else |
| 217 | value = EGL_NONE; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 218 | _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 219 | break; |
| 220 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 221 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 222 | bind_to_texture_rgb = value; |
| 223 | break; |
| 224 | |
| 225 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 226 | bind_to_texture_rgba = value; |
| 227 | break; |
| 228 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 229 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 230 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 231 | break; |
| 232 | |
| 233 | default: |
| 234 | key = dri2_to_egl_attribute_map[attrib]; |
| 235 | if (key != 0) |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 236 | _eglSetConfigKey(&base, key, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 241 | /* In EGL, double buffer or not isn't a config attribute. Pixmaps |
| 242 | * surfaces are always single buffered, pbuffer surfaces are always |
| 243 | * back buffers and windows can be either, selected by passing an |
| 244 | * attribute at window surface construction time. To support this |
| 245 | * we ignore all double buffer configs and manipulate the buffer we |
| 246 | * return in the getBuffer callback to get the behaviour we want. */ |
| 247 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 248 | if (double_buffer) |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 249 | return NULL; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 250 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 251 | if (depth > 0 && depth != _eglGetConfigKey(&base, EGL_BUFFER_SIZE)) |
| 252 | return NULL; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 253 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 254 | _eglSetConfigKey(&base, EGL_NATIVE_RENDERABLE, EGL_TRUE); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 255 | |
| 256 | _eglSetConfigKey(&base, EGL_SURFACE_TYPE, surface_type); |
| 257 | if (surface_type & (EGL_PIXMAP_BIT | EGL_PBUFFER_BIT)) { |
| 258 | _eglSetConfigKey(&base, EGL_BIND_TO_TEXTURE_RGB, bind_to_texture_rgb); |
| 259 | if (_eglGetConfigKey(&base, EGL_ALPHA_SIZE) > 0) |
| 260 | _eglSetConfigKey(&base, |
| 261 | EGL_BIND_TO_TEXTURE_RGBA, bind_to_texture_rgba); |
| 262 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 263 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 264 | _eglSetConfigKey(&base, EGL_RENDERABLE_TYPE, disp->ClientAPIsMask); |
| 265 | _eglSetConfigKey(&base, EGL_CONFORMANT, disp->ClientAPIsMask); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 266 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 267 | if (!_eglValidateConfig(&base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 268 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 269 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 272 | conf = malloc(sizeof *conf); |
| 273 | if (conf != NULL) { |
| 274 | memcpy(&conf->base, &base, sizeof base); |
| 275 | conf->dri_config = dri_config; |
| 276 | _eglAddConfig(disp, &conf->base); |
| 277 | } |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 278 | |
| 279 | return conf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Process list of buffer received from the server |
| 284 | * |
| 285 | * Processes the list of buffers received in a reply from the server to either |
| 286 | * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat. |
| 287 | */ |
| 288 | static void |
| 289 | dri2_process_buffers(struct dri2_egl_surface *dri2_surf, |
| 290 | xcb_dri2_dri2_buffer_t *buffers, unsigned count) |
| 291 | { |
| 292 | struct dri2_egl_display *dri2_dpy = |
| 293 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 294 | xcb_rectangle_t rectangle; |
| 295 | int i; |
| 296 | |
| 297 | dri2_surf->buffer_count = count; |
| 298 | dri2_surf->have_fake_front = 0; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 299 | |
| 300 | /* This assumes the DRI2 buffer attachment tokens matches the |
| 301 | * __DRIbuffer tokens. */ |
| 302 | for (i = 0; i < count; i++) { |
| 303 | dri2_surf->buffers[i].attachment = buffers[i].attachment; |
| 304 | dri2_surf->buffers[i].name = buffers[i].name; |
| 305 | dri2_surf->buffers[i].pitch = buffers[i].pitch; |
| 306 | dri2_surf->buffers[i].cpp = buffers[i].cpp; |
| 307 | dri2_surf->buffers[i].flags = buffers[i].flags; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 308 | |
| 309 | /* We only use the DRI drivers single buffer configs. This |
| 310 | * means that if we try to render to a window, DRI2 will give us |
| 311 | * the fake front buffer, which we'll use as a back buffer. |
| 312 | * Note that EGL doesn't require that several clients rendering |
| 313 | * to the same window must see the same aux buffers. */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 314 | if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT) |
| 315 | dri2_surf->have_fake_front = 1; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (dri2_surf->region != XCB_NONE) |
| 319 | xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region); |
| 320 | |
| 321 | rectangle.x = 0; |
| 322 | rectangle.y = 0; |
| 323 | rectangle.width = dri2_surf->base.Width; |
| 324 | rectangle.height = dri2_surf->base.Height; |
| 325 | dri2_surf->region = xcb_generate_id(dri2_dpy->conn); |
| 326 | xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle); |
| 327 | } |
| 328 | |
| 329 | static __DRIbuffer * |
| 330 | dri2_get_buffers(__DRIdrawable * driDrawable, |
| 331 | int *width, int *height, |
| 332 | unsigned int *attachments, int count, |
| 333 | int *out_count, void *loaderPrivate) |
| 334 | { |
| 335 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 336 | struct dri2_egl_display *dri2_dpy = |
| 337 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 338 | xcb_dri2_dri2_buffer_t *buffers; |
| 339 | xcb_dri2_get_buffers_reply_t *reply; |
| 340 | xcb_dri2_get_buffers_cookie_t cookie; |
| 341 | |
| 342 | cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn, |
| 343 | dri2_surf->drawable, |
| 344 | count, count, attachments); |
| 345 | reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL); |
| 346 | buffers = xcb_dri2_get_buffers_buffers (reply); |
| 347 | if (buffers == NULL) |
| 348 | return NULL; |
| 349 | |
| 350 | *out_count = reply->count; |
| 351 | dri2_surf->base.Width = *width = reply->width; |
| 352 | dri2_surf->base.Height = *height = reply->height; |
| 353 | dri2_process_buffers(dri2_surf, buffers, *out_count); |
| 354 | |
| 355 | free(reply); |
| 356 | |
| 357 | return dri2_surf->buffers; |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
| 362 | { |
| 363 | /* FIXME: Does EGL support front buffer rendering at all? */ |
| 364 | |
| 365 | #if 0 |
| 366 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 367 | |
| 368 | dri2WaitGL(dri2_surf); |
| 369 | #endif |
| 370 | } |
| 371 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 372 | static __DRIimage * |
| 373 | dri2_lookup_egl_image(__DRIcontext *context, void *image, void *data) |
| 374 | { |
| 375 | struct dri2_egl_context *dri2_ctx = data; |
| 376 | _EGLDisplay *disp = dri2_ctx->base.Resource.Display; |
| 377 | struct dri2_egl_image *dri2_img; |
| 378 | _EGLImage *img; |
| 379 | |
| 380 | img = _eglLookupImage(image, disp); |
| 381 | if (img == NULL) { |
| 382 | _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image"); |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | dri2_img = dri2_egl_image(image); |
| 387 | |
| 388 | return dri2_img->dri_image; |
| 389 | } |
| 390 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 391 | static const __DRIimageLookupExtension image_lookup_extension = { |
| 392 | { __DRI_IMAGE_LOOKUP, 1 }, |
| 393 | dri2_lookup_egl_image |
| 394 | }; |
| 395 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 396 | static __DRIbuffer * |
| 397 | dri2_get_buffers_with_format(__DRIdrawable * driDrawable, |
| 398 | int *width, int *height, |
| 399 | unsigned int *attachments, int count, |
| 400 | int *out_count, void *loaderPrivate) |
| 401 | { |
| 402 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 403 | struct dri2_egl_display *dri2_dpy = |
| 404 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 405 | xcb_dri2_dri2_buffer_t *buffers; |
| 406 | xcb_dri2_get_buffers_with_format_reply_t *reply; |
| 407 | xcb_dri2_get_buffers_with_format_cookie_t cookie; |
| 408 | xcb_dri2_attach_format_t *format_attachments; |
| 409 | |
| 410 | format_attachments = (xcb_dri2_attach_format_t *) attachments; |
| 411 | cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn, |
| 412 | dri2_surf->drawable, |
| 413 | count, count, |
| 414 | format_attachments); |
| 415 | |
| 416 | reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn, |
| 417 | cookie, NULL); |
| 418 | if (reply == NULL) |
| 419 | return NULL; |
| 420 | |
| 421 | buffers = xcb_dri2_get_buffers_with_format_buffers (reply); |
| 422 | dri2_surf->base.Width = *width = reply->width; |
| 423 | dri2_surf->base.Height = *height = reply->height; |
| 424 | *out_count = reply->count; |
| 425 | dri2_process_buffers(dri2_surf, buffers, *out_count); |
| 426 | |
| 427 | free(reply); |
| 428 | |
| 429 | return dri2_surf->buffers; |
| 430 | } |
| 431 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 432 | static const char dri_driver_path[] = DEFAULT_DRIVER_DIR; |
| 433 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 434 | struct dri2_extension_match { |
| 435 | const char *name; |
| 436 | int version; |
| 437 | int offset; |
| 438 | }; |
| 439 | |
| 440 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 441 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 442 | { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) }, |
| 443 | { NULL } |
| 444 | }; |
| 445 | |
| 446 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 447 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 448 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 449 | { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) }, |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 450 | { NULL } |
| 451 | }; |
| 452 | |
| 453 | static EGLBoolean |
| 454 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 455 | struct dri2_extension_match *matches, |
| 456 | const __DRIextension **extensions) |
| 457 | { |
| 458 | int i, j, ret = EGL_TRUE; |
| 459 | void *field; |
| 460 | |
| 461 | for (i = 0; extensions[i]; i++) { |
| 462 | _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name); |
| 463 | for (j = 0; matches[j].name; j++) { |
| 464 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 465 | extensions[i]->version >= matches[j].version) { |
| 466 | field = ((char *) dri2_dpy + matches[j].offset); |
| 467 | *(const __DRIextension **) field = extensions[i]; |
| 468 | _eglLog(_EGL_INFO, "DRI2: found extension %s version %d", |
| 469 | extensions[i]->name, extensions[i]->version); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | for (j = 0; matches[j].name; j++) { |
| 475 | field = ((char *) dri2_dpy + matches[j].offset); |
| 476 | if (*(const __DRIextension **) field == NULL) { |
| 477 | _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d", |
| 478 | matches[j].name, matches[j].version); |
| 479 | ret = EGL_FALSE; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 486 | static char * |
| 487 | dri2_strndup(const char *s, int length) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 488 | { |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 489 | char *d; |
| 490 | |
| 491 | d = malloc(length + 1); |
| 492 | if (d == NULL) |
| 493 | return NULL; |
| 494 | |
| 495 | memcpy(d, s, length); |
| 496 | d[length] = '\0'; |
| 497 | |
| 498 | return d; |
| 499 | } |
| 500 | |
| 501 | static EGLBoolean |
| 502 | dri2_connect(struct dri2_egl_display *dri2_dpy) |
| 503 | { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 504 | xcb_xfixes_query_version_reply_t *xfixes_query; |
| 505 | xcb_xfixes_query_version_cookie_t xfixes_query_cookie; |
| 506 | xcb_dri2_query_version_reply_t *dri2_query; |
| 507 | xcb_dri2_query_version_cookie_t dri2_query_cookie; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 508 | xcb_dri2_connect_reply_t *connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 509 | xcb_dri2_connect_cookie_t connect_cookie; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 510 | xcb_generic_error_t *error; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 511 | xcb_screen_iterator_t s; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 512 | |
| 513 | xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id); |
| 514 | xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id); |
| 515 | |
| 516 | xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn, |
| 517 | XCB_XFIXES_MAJOR_VERSION, |
| 518 | XCB_XFIXES_MINOR_VERSION); |
| 519 | |
| 520 | dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn, |
| 521 | XCB_DRI2_MAJOR_VERSION, |
| 522 | XCB_DRI2_MINOR_VERSION); |
| 523 | |
| 524 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 525 | connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn, |
| 526 | s.data->root, |
| 527 | XCB_DRI2_DRIVER_TYPE_DRI); |
| 528 | |
| 529 | xfixes_query = |
| 530 | xcb_xfixes_query_version_reply (dri2_dpy->conn, |
| 531 | xfixes_query_cookie, &error); |
| 532 | if (xfixes_query == NULL || |
| 533 | error != NULL || xfixes_query->major_version < 2) { |
| 534 | _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version"); |
| 535 | free(error); |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 536 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 537 | } |
| 538 | free(xfixes_query); |
| 539 | |
| 540 | dri2_query = |
| 541 | xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error); |
| 542 | if (dri2_query == NULL || error != NULL) { |
| 543 | _eglLog(_EGL_FATAL, "DRI2: failed to query version"); |
| 544 | free(error); |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 545 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 546 | } |
| 547 | dri2_dpy->dri2_major = dri2_query->major_version; |
| 548 | dri2_dpy->dri2_minor = dri2_query->minor_version; |
| 549 | free(dri2_query); |
| 550 | |
| 551 | connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 552 | if (connect == NULL || |
| 553 | connect->driver_name_length + connect->device_name_length == 0) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 554 | _eglLog(_EGL_FATAL, "DRI2: failed to authenticate"); |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 555 | return EGL_FALSE; |
| 556 | } |
| 557 | |
| 558 | dri2_dpy->device_name = |
| 559 | dri2_strndup(xcb_dri2_connect_device_name (connect), |
| 560 | xcb_dri2_connect_device_name_length (connect)); |
| 561 | |
| 562 | dri2_dpy->driver_name = |
| 563 | dri2_strndup(xcb_dri2_connect_driver_name (connect), |
| 564 | xcb_dri2_connect_driver_name_length (connect)); |
| 565 | |
| 566 | if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) { |
| 567 | free(dri2_dpy->device_name); |
| 568 | free(dri2_dpy->driver_name); |
| 569 | free(connect); |
| 570 | return EGL_FALSE; |
| 571 | } |
| 572 | free(connect); |
| 573 | |
| 574 | return EGL_TRUE; |
| 575 | } |
| 576 | |
| 577 | static EGLBoolean |
| 578 | dri2_authenticate(struct dri2_egl_display *dri2_dpy) |
| 579 | { |
| 580 | xcb_dri2_authenticate_reply_t *authenticate; |
| 581 | xcb_dri2_authenticate_cookie_t authenticate_cookie; |
| 582 | xcb_screen_iterator_t s; |
| 583 | drm_magic_t magic; |
| 584 | |
| 585 | if (drmGetMagic(dri2_dpy->fd, &magic)) { |
| 586 | _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic"); |
| 587 | return EGL_FALSE; |
| 588 | } |
| 589 | |
| 590 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 591 | authenticate_cookie = |
| 592 | xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, magic); |
| 593 | authenticate = |
| 594 | xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL); |
| 595 | if (authenticate == NULL || !authenticate->authenticated) { |
| 596 | _eglLog(_EGL_FATAL, "DRI2: failed to authenticate"); |
| 597 | free(authenticate); |
| 598 | return EGL_FALSE; |
| 599 | } |
| 600 | |
| 601 | free(authenticate); |
| 602 | |
| 603 | return EGL_TRUE; |
| 604 | } |
| 605 | |
| 606 | static EGLBoolean |
| 607 | dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy, |
| 608 | _EGLDisplay *disp) |
| 609 | { |
| 610 | xcb_screen_iterator_t s; |
| 611 | xcb_depth_iterator_t d; |
| 612 | xcb_visualtype_t *visuals; |
| 613 | int i, j, id; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 614 | struct dri2_egl_config *conf; |
| 615 | EGLint surface_type; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 616 | |
| 617 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 618 | d = xcb_screen_allowed_depths_iterator(s.data); |
| 619 | id = 1; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 620 | |
| 621 | surface_type = |
| 622 | EGL_WINDOW_BIT | |
| 623 | EGL_PIXMAP_BIT | |
| 624 | EGL_PBUFFER_BIT | |
| 625 | EGL_SWAP_BEHAVIOR_PRESERVED_BIT; |
| 626 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 627 | while (d.rem > 0) { |
| 628 | EGLBoolean class_added[6] = { 0, }; |
| 629 | |
| 630 | visuals = xcb_depth_visuals(d.data); |
| 631 | for (i = 0; i < xcb_depth_visuals_length(d.data); i++) { |
| 632 | if (class_added[visuals[i]._class]) |
| 633 | continue; |
| 634 | |
| 635 | class_added[visuals[i]._class] = EGL_TRUE; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 636 | for (j = 0; dri2_dpy->driver_configs[j]; j++) { |
| 637 | conf = dri2_add_config(disp, dri2_dpy->driver_configs[j], |
| 638 | id++, d.data->depth, surface_type); |
| 639 | if (conf == NULL) |
| 640 | continue; |
| 641 | _eglSetConfigKey(&conf->base, |
| 642 | EGL_NATIVE_VISUAL_ID, visuals[i].visual_id); |
| 643 | _eglSetConfigKey(&conf->base, |
| 644 | EGL_NATIVE_VISUAL_TYPE, visuals[i]._class); |
| 645 | } |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | xcb_depth_next(&d); |
| 649 | } |
| 650 | |
Chia-I Wu | 6717a31 | 2010-06-30 17:10:10 +0800 | [diff] [blame] | 651 | if (!_eglGetArraySize(disp->Configs)) { |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 652 | _eglLog(_EGL_WARNING, "DRI2: failed to create any config"); |
| 653 | return EGL_FALSE; |
| 654 | } |
| 655 | |
| 656 | return EGL_TRUE; |
| 657 | } |
| 658 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 659 | static EGLBoolean |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 660 | dri2_load_driver(_EGLDisplay *disp) |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 661 | { |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 662 | struct dri2_egl_display *dri2_dpy = disp->DriverData; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 663 | const __DRIextension **extensions; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 664 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 665 | |
| 666 | search_paths = NULL; |
| 667 | if (geteuid() == getuid()) { |
| 668 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 669 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 670 | } |
| 671 | if (search_paths == NULL) |
| 672 | search_paths = DEFAULT_DRIVER_DIR; |
| 673 | |
| 674 | dri2_dpy->driver = NULL; |
| 675 | end = search_paths + strlen(search_paths); |
| 676 | 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] | 677 | int len; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 678 | next = strchr(p, ':'); |
| 679 | if (next == NULL) |
| 680 | next = end; |
| 681 | |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 682 | len = next - p; |
| 683 | #if GLX_USE_TLS |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 684 | snprintf(path, sizeof path, |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 685 | "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 686 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
Eric Anholt | 6f77b75 | 2010-05-20 14:44:43 -0700 | [diff] [blame] | 687 | #endif |
| 688 | if (dri2_dpy->driver == NULL) { |
| 689 | snprintf(path, sizeof path, |
| 690 | "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name); |
| 691 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 692 | if (dri2_dpy->driver == NULL) |
| 693 | _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror()); |
| 694 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | if (dri2_dpy->driver == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 698 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 699 | "DRI2: failed to open any driver (search paths %s)", |
| 700 | search_paths); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 701 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
| 705 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
| 706 | if (extensions == NULL) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 707 | _eglLog(_EGL_WARNING, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 708 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 709 | dlclose(dri2_dpy->driver); |
| 710 | return EGL_FALSE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 711 | } |
| 712 | |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 713 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) { |
| 714 | dlclose(dri2_dpy->driver); |
| 715 | return EGL_FALSE; |
| 716 | } |
| 717 | |
| 718 | return EGL_TRUE; |
| 719 | } |
| 720 | |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 721 | static EGLBoolean |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 722 | dri2_create_screen(_EGLDisplay *disp) |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 723 | { |
| 724 | const __DRIextension **extensions; |
| 725 | struct dri2_egl_display *dri2_dpy; |
| 726 | unsigned int api_mask; |
| 727 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 728 | dri2_dpy = disp->DriverData; |
| 729 | dri2_dpy->dri_screen = |
| 730 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
| 731 | &dri2_dpy->driver_configs, dri2_dpy); |
| 732 | |
| 733 | if (dri2_dpy->dri_screen == NULL) { |
| 734 | _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen"); |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 735 | return EGL_FALSE; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
| 739 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
| 740 | goto cleanup_dri_screen; |
| 741 | |
| 742 | if (dri2_dpy->dri2->base.version >= 2) |
| 743 | api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen); |
| 744 | else |
| 745 | api_mask = __DRI_API_OPENGL; |
| 746 | |
| 747 | disp->ClientAPIsMask = 0; |
| 748 | if (api_mask & (1 <<__DRI_API_OPENGL)) |
| 749 | disp->ClientAPIsMask |= EGL_OPENGL_BIT; |
| 750 | if (api_mask & (1 <<__DRI_API_GLES)) |
| 751 | disp->ClientAPIsMask |= EGL_OPENGL_ES_BIT; |
| 752 | if (api_mask & (1 << __DRI_API_GLES2)) |
| 753 | disp->ClientAPIsMask |= EGL_OPENGL_ES2_BIT; |
| 754 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 755 | if (dri2_dpy->dri2->base.version >= 2) { |
| 756 | disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE; |
| 757 | disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE; |
| 758 | disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE; |
| 759 | } |
| 760 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 761 | return EGL_TRUE; |
| 762 | |
| 763 | cleanup_dri_screen: |
| 764 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 765 | |
| 766 | return EGL_FALSE; |
| 767 | } |
| 768 | |
| 769 | static EGLBoolean |
| 770 | dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp, |
| 771 | EGLint *major, EGLint *minor) |
| 772 | { |
| 773 | struct dri2_egl_display *dri2_dpy; |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 774 | |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 775 | dri2_dpy = malloc(sizeof *dri2_dpy); |
| 776 | if (!dri2_dpy) |
| 777 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 778 | |
| 779 | disp->DriverData = (void *) dri2_dpy; |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 780 | if (disp->PlatformDisplay == NULL) { |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 781 | dri2_dpy->conn = xcb_connect(0, 0); |
| 782 | } else { |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 783 | dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay); |
Kristian Høgsberg | 0defc25 | 2010-05-28 16:16:12 -0400 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (xcb_connection_has_error(dri2_dpy->conn)) { |
| 787 | _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed"); |
| 788 | goto cleanup_dpy; |
| 789 | } |
| 790 | |
| 791 | if (dri2_dpy->conn) { |
| 792 | if (!dri2_connect(dri2_dpy)) |
| 793 | goto cleanup_conn; |
| 794 | } |
| 795 | |
| 796 | if (!dri2_load_driver(disp)) |
| 797 | goto cleanup_conn; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 798 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 799 | dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 800 | if (dri2_dpy->fd == -1) { |
Kristian Høgsberg | 7413d9a | 2010-05-13 15:42:51 -0400 | [diff] [blame] | 801 | _eglLog(_EGL_WARNING, |
Micah Fedke | 9cfaaa2 | 2010-04-28 07:25:58 -0600 | [diff] [blame] | 802 | "DRI2: could not open %s (%s)", dri2_dpy->device_name, |
| 803 | strerror(errno)); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 804 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 805 | } |
| 806 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 807 | if (dri2_dpy->conn) { |
| 808 | if (!dri2_authenticate(dri2_dpy)) |
| 809 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 812 | if (dri2_dpy->dri2_minor >= 1) { |
| 813 | dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER; |
| 814 | dri2_dpy->loader_extension.base.version = 3; |
| 815 | dri2_dpy->loader_extension.getBuffers = dri2_get_buffers; |
| 816 | dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer; |
| 817 | dri2_dpy->loader_extension.getBuffersWithFormat = |
| 818 | dri2_get_buffers_with_format; |
| 819 | } else { |
| 820 | dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER; |
| 821 | dri2_dpy->loader_extension.base.version = 2; |
| 822 | dri2_dpy->loader_extension.getBuffers = dri2_get_buffers; |
| 823 | dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer; |
| 824 | dri2_dpy->loader_extension.getBuffersWithFormat = NULL; |
| 825 | } |
| 826 | |
| 827 | dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 828 | dri2_dpy->extensions[1] = &image_lookup_extension.base; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 829 | dri2_dpy->extensions[2] = NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 830 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 831 | if (!dri2_create_screen(disp)) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 832 | goto cleanup_fd; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 833 | |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 834 | if (dri2_dpy->conn) { |
| 835 | if (!dri2_add_configs_for_visuals(dri2_dpy, disp)) |
| 836 | goto cleanup_configs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 837 | } |
| 838 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 839 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 840 | disp->Extensions.KHR_image_pixmap = EGL_TRUE; |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 841 | disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE; |
Kristian Høgsberg | 3a82c8c | 2010-04-14 16:22:27 -0400 | [diff] [blame] | 842 | disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE; |
Kristian Høgsberg | 52c554a | 2010-05-06 22:01:35 -0400 | [diff] [blame] | 843 | disp->Extensions.NOK_swap_region = EGL_TRUE; |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 844 | disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 845 | |
| 846 | /* we're supporting EGL 1.4 */ |
| 847 | *major = 1; |
| 848 | *minor = 4; |
Kristian Høgsberg | 4007342 | 2010-03-02 15:34:18 -0500 | [diff] [blame] | 849 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 850 | return EGL_TRUE; |
| 851 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 852 | cleanup_configs: |
| 853 | _eglCleanupDisplay(disp); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 854 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 855 | cleanup_fd: |
| 856 | close(dri2_dpy->fd); |
| 857 | cleanup_driver: |
| 858 | dlclose(dri2_dpy->driver); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 859 | cleanup_conn: |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 860 | if (disp->PlatformDisplay == NULL) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 861 | xcb_disconnect(dri2_dpy->conn); |
| 862 | cleanup_dpy: |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 863 | free(dri2_dpy); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 864 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 865 | return EGL_FALSE; |
| 866 | } |
| 867 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 868 | #ifdef HAVE_LIBUDEV |
| 869 | |
| 870 | struct dri2_driver_map { |
| 871 | int vendor_id; |
| 872 | const char *driver; |
| 873 | const int *chip_ids; |
| 874 | int num_chips_ids; |
| 875 | }; |
| 876 | |
| 877 | const int i915_chip_ids[] = { |
| 878 | 0x3577, /* PCI_CHIP_I830_M */ |
| 879 | 0x2562, /* PCI_CHIP_845_G */ |
| 880 | 0x3582, /* PCI_CHIP_I855_GM */ |
| 881 | 0x2572, /* PCI_CHIP_I865_G */ |
| 882 | 0x2582, /* PCI_CHIP_I915_G */ |
| 883 | 0x258a, /* PCI_CHIP_E7221_G */ |
| 884 | 0x2592, /* PCI_CHIP_I915_GM */ |
| 885 | 0x2772, /* PCI_CHIP_I945_G */ |
| 886 | 0x27a2, /* PCI_CHIP_I945_GM */ |
| 887 | 0x27ae, /* PCI_CHIP_I945_GME */ |
| 888 | 0x29b2, /* PCI_CHIP_Q35_G */ |
| 889 | 0x29c2, /* PCI_CHIP_G33_G */ |
| 890 | 0x29d2, /* PCI_CHIP_Q33_G */ |
| 891 | }; |
| 892 | |
| 893 | const int i965_chip_ids[] = { |
| 894 | 0x29a2, /* PCI_CHIP_I965_G */ |
| 895 | 0x2992, /* PCI_CHIP_I965_Q */ |
| 896 | 0x2982, /* PCI_CHIP_I965_G_1 */ |
| 897 | 0x2972, /* PCI_CHIP_I946_GZ */ |
| 898 | 0x2a02, /* PCI_CHIP_I965_GM */ |
| 899 | 0x2a12, /* PCI_CHIP_I965_GME */ |
| 900 | 0x2a42, /* PCI_CHIP_GM45_GM */ |
| 901 | 0x2e02, /* PCI_CHIP_IGD_E_G */ |
| 902 | 0x2e12, /* PCI_CHIP_Q45_G */ |
| 903 | 0x2e22, /* PCI_CHIP_G45_G */ |
| 904 | 0x2e32, /* PCI_CHIP_G41_G */ |
| 905 | }; |
| 906 | |
| 907 | const struct dri2_driver_map driver_map[] = { |
| 908 | { 0x8086, "i915", i915_chip_ids, ARRAY_SIZE(i915_chip_ids) }, |
| 909 | { 0x8086, "i965", i965_chip_ids, ARRAY_SIZE(i965_chip_ids) }, |
| 910 | }; |
| 911 | |
| 912 | static char * |
| 913 | dri2_get_driver_for_fd(int fd) |
| 914 | { |
| 915 | struct udev *udev; |
| 916 | struct udev_device *device, *parent; |
| 917 | struct stat buf; |
| 918 | const char *pci_id; |
| 919 | char *driver = NULL; |
| 920 | int vendor_id, chip_id, i, j; |
| 921 | |
| 922 | udev = udev_new(); |
| 923 | if (fstat(fd, &buf) < 0) { |
| 924 | _eglLog(_EGL_WARNING, "EGL-DRI2: failed to stat fd %d", fd); |
| 925 | goto out; |
| 926 | } |
| 927 | |
| 928 | device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev); |
| 929 | if (device == NULL) { |
| 930 | _eglLog(_EGL_WARNING, |
| 931 | "EGL-DRI2: could not create udev device for fd %d", fd); |
| 932 | goto out; |
| 933 | } |
| 934 | |
| 935 | parent = udev_device_get_parent(device); |
| 936 | if (parent == NULL) { |
| 937 | _eglLog(_EGL_WARNING, "DRI2: could not get parent device"); |
| 938 | goto out; |
| 939 | } |
| 940 | |
| 941 | pci_id = udev_device_get_property_value(parent, "PCI_ID"); |
| 942 | if (pci_id == NULL || sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) { |
| 943 | _eglLog(_EGL_WARNING, "EGL-DRI2: malformed or no PCI ID"); |
| 944 | goto out; |
| 945 | } |
| 946 | |
| 947 | for (i = 0; i < ARRAY_SIZE(driver_map); i++) { |
| 948 | if (vendor_id != driver_map[i].vendor_id) |
| 949 | continue; |
| 950 | for (j = 0; j < driver_map[i].num_chips_ids; j++) |
| 951 | if (driver_map[i].chip_ids[j] == chip_id) { |
| 952 | driver = strdup(driver_map[i].driver); |
| 953 | _eglLog(_EGL_DEBUG, "pci id for %d: %04x:%04x, driver %s", |
| 954 | fd, vendor_id, chip_id, driver); |
| 955 | goto out; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | out: |
| 960 | udev_device_unref(device); |
| 961 | udev_unref(udev); |
| 962 | |
| 963 | return driver; |
| 964 | } |
| 965 | |
| 966 | static EGLBoolean |
| 967 | dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp, |
| 968 | EGLint *major, EGLint *minor) |
| 969 | { |
| 970 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 971 | int i; |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 972 | |
| 973 | dri2_dpy = malloc(sizeof *dri2_dpy); |
| 974 | if (!dri2_dpy) |
| 975 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 976 | |
| 977 | disp->DriverData = (void *) dri2_dpy; |
| 978 | dri2_dpy->fd = (int) disp->PlatformDisplay; |
| 979 | |
| 980 | dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd); |
| 981 | if (dri2_dpy->driver_name == NULL) |
| 982 | return _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name"); |
| 983 | |
| 984 | if (!dri2_load_driver(disp)) |
| 985 | goto cleanup_driver_name; |
| 986 | |
| 987 | dri2_dpy->extensions[0] = &image_lookup_extension.base; |
| 988 | dri2_dpy->extensions[1] = &use_invalidate.base; |
| 989 | dri2_dpy->extensions[2] = NULL; |
| 990 | |
| 991 | if (!dri2_create_screen(disp)) |
| 992 | goto cleanup_driver; |
| 993 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 994 | for (i = 0; dri2_dpy->driver_configs[i]; i++) |
| 995 | dri2_add_config(disp, dri2_dpy->driver_configs[i], i + 1, 0, 0); |
| 996 | |
Kristian Høgsberg | 2168b87 | 2010-06-02 22:48:06 -0400 | [diff] [blame] | 997 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 998 | disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE; |
| 999 | disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE; |
| 1000 | |
| 1001 | return EGL_TRUE; |
| 1002 | |
| 1003 | cleanup_driver: |
| 1004 | dlclose(dri2_dpy->driver); |
| 1005 | cleanup_driver_name: |
| 1006 | free(dri2_dpy->driver_name); |
| 1007 | |
| 1008 | return EGL_FALSE; |
| 1009 | } |
| 1010 | |
| 1011 | #endif |
| 1012 | |
| 1013 | /** |
| 1014 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 1015 | */ |
| 1016 | static EGLBoolean |
| 1017 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp, |
| 1018 | EGLint *major, EGLint *minor) |
| 1019 | { |
| 1020 | switch (disp->Platform) { |
| 1021 | case _EGL_PLATFORM_X11: |
| 1022 | return dri2_initialize_x11(drv, disp, major, minor); |
| 1023 | |
| 1024 | #ifdef HAVE_LIBUDEV |
| 1025 | case _EGL_PLATFORM_DRM: |
| 1026 | return dri2_initialize_drm(drv, disp, major, minor); |
| 1027 | #endif |
| 1028 | |
| 1029 | default: |
| 1030 | return EGL_FALSE; |
| 1031 | } |
| 1032 | } |
| 1033 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1034 | /** |
| 1035 | * Called via eglTerminate(), drv->API.Terminate(). |
| 1036 | */ |
| 1037 | static EGLBoolean |
| 1038 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 1039 | { |
| 1040 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1041 | |
| 1042 | _eglReleaseDisplayResources(drv, disp); |
| 1043 | _eglCleanupDisplay(disp); |
| 1044 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1045 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1046 | close(dri2_dpy->fd); |
| 1047 | dlclose(dri2_dpy->driver); |
Chia-I Wu | f22665d | 2010-06-17 17:14:03 +0800 | [diff] [blame] | 1048 | if (disp->PlatformDisplay == NULL) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1049 | xcb_disconnect(dri2_dpy->conn); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1050 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1051 | disp->DriverData = NULL; |
| 1052 | |
| 1053 | return EGL_TRUE; |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | /** |
| 1058 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 1059 | */ |
| 1060 | static _EGLContext * |
| 1061 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 1062 | _EGLContext *share_list, const EGLint *attrib_list) |
| 1063 | { |
| 1064 | struct dri2_egl_context *dri2_ctx; |
| 1065 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1066 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 1067 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 1068 | const __DRIconfig *dri_config; |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1069 | int api; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1070 | |
| 1071 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 1072 | if (!dri2_ctx) { |
| 1073 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1077 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 1078 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1079 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1080 | switch (dri2_ctx->base.ClientAPI) { |
| 1081 | case EGL_OPENGL_ES_API: |
| 1082 | switch (dri2_ctx->base.ClientVersion) { |
| 1083 | case 1: |
| 1084 | api = __DRI_API_GLES; |
| 1085 | break; |
| 1086 | case 2: |
| 1087 | api = __DRI_API_GLES2; |
| 1088 | break; |
| 1089 | default: |
| 1090 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | break; |
| 1094 | case EGL_OPENGL_API: |
| 1095 | api = __DRI_API_OPENGL; |
| 1096 | break; |
| 1097 | default: |
| 1098 | _eglError(EGL_BAD_PARAMETER, "eglCreateContext"); |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 1102 | if (conf != NULL) |
| 1103 | dri_config = dri2_config->dri_config; |
| 1104 | else |
| 1105 | dri_config = NULL; |
| 1106 | |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1107 | if (dri2_dpy->dri2->base.version >= 2) { |
| 1108 | dri2_ctx->dri_context = |
| 1109 | dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen, |
| 1110 | api, |
Kristian Høgsberg | efd4a8c | 2010-07-27 18:25:54 -0400 | [diff] [blame^] | 1111 | dri_config, |
Kristian Høgsberg | e2fd98d | 2010-04-27 11:38:46 -0400 | [diff] [blame] | 1112 | dri2_ctx_shared ? |
| 1113 | dri2_ctx_shared->dri_context : NULL, |
| 1114 | dri2_ctx); |
| 1115 | } else if (api == __DRI_API_OPENGL) { |
| 1116 | dri2_ctx->dri_context = |
| 1117 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
| 1118 | dri2_config->dri_config, |
| 1119 | dri2_ctx_shared ? |
| 1120 | dri2_ctx_shared->dri_context : NULL, |
| 1121 | dri2_ctx); |
| 1122 | } else { |
| 1123 | /* fail */ |
| 1124 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1125 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1126 | if (!dri2_ctx->dri_context) |
| 1127 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1128 | |
| 1129 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1130 | |
| 1131 | cleanup: |
| 1132 | free(dri2_ctx); |
| 1133 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static EGLBoolean |
| 1137 | dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
| 1138 | { |
| 1139 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1140 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1141 | |
| 1142 | if (_eglIsSurfaceBound(surf)) |
| 1143 | return EGL_TRUE; |
| 1144 | |
| 1145 | (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); |
| 1146 | |
| 1147 | xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable); |
| 1148 | |
| 1149 | if (surf->Type == EGL_PBUFFER_BIT) |
| 1150 | xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable); |
| 1151 | |
| 1152 | free(surf); |
| 1153 | |
| 1154 | return EGL_TRUE; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 1159 | */ |
| 1160 | static EGLBoolean |
| 1161 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 1162 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 1163 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1164 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1165 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1166 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 1167 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 1168 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1169 | __DRIdrawable *ddraw, *rdraw; |
| 1170 | __DRIcontext *cctx; |
| 1171 | |
| 1172 | /* bind the new context and return the "orphaned" one */ |
| 1173 | if (!_eglBindContext(&ctx, &dsurf, &rsurf)) |
| 1174 | return EGL_FALSE; |
| 1175 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1176 | /* flush before context switch */ |
| 1177 | if (ctx && dri2_drv->glFlush) |
| 1178 | dri2_drv->glFlush(); |
| 1179 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1180 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 1181 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 1182 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 1183 | |
| 1184 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 1185 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 1186 | if (dsurf && !_eglIsSurfaceLinked(dsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1187 | dri2_destroy_surface(drv, disp, dsurf); |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 1188 | if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1189 | dri2_destroy_surface(drv, disp, rsurf); |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 1190 | if (ctx != NULL && !_eglIsContextLinked(ctx)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1191 | dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context); |
| 1192 | |
| 1193 | return EGL_TRUE; |
| 1194 | } else { |
| 1195 | _eglBindContext(&ctx, &dsurf, &rsurf); |
| 1196 | |
| 1197 | return EGL_FALSE; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | /** |
| 1202 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 1203 | */ |
| 1204 | static _EGLSurface * |
| 1205 | dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
| 1206 | _EGLConfig *conf, EGLNativeWindowType window, |
| 1207 | const EGLint *attrib_list) |
| 1208 | { |
| 1209 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1210 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 1211 | struct dri2_egl_surface *dri2_surf; |
| 1212 | xcb_get_geometry_cookie_t cookie; |
| 1213 | xcb_get_geometry_reply_t *reply; |
| 1214 | xcb_screen_iterator_t s; |
| 1215 | xcb_generic_error_t *error; |
| 1216 | |
| 1217 | dri2_surf = malloc(sizeof *dri2_surf); |
| 1218 | if (!dri2_surf) { |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1219 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1220 | return NULL; |
| 1221 | } |
| 1222 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1223 | if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) |
| 1224 | goto cleanup_surf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1225 | |
| 1226 | dri2_surf->region = XCB_NONE; |
| 1227 | if (type == EGL_PBUFFER_BIT) { |
| 1228 | dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn); |
| 1229 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 1230 | xcb_create_pixmap(dri2_dpy->conn, |
| 1231 | _eglGetConfigKey(conf, EGL_BUFFER_SIZE), |
| 1232 | dri2_surf->drawable, s.data->root, |
| 1233 | dri2_surf->base.Width, dri2_surf->base.Height); |
| 1234 | } else { |
| 1235 | dri2_surf->drawable = window; |
| 1236 | } |
| 1237 | |
| 1238 | dri2_surf->dri_drawable = |
| 1239 | (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen, |
| 1240 | dri2_conf->dri_config, dri2_surf); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1241 | if (dri2_surf->dri_drawable == NULL) { |
| 1242 | _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable"); |
| 1243 | goto cleanup_pixmap; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable); |
| 1247 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1248 | if (type != EGL_PBUFFER_BIT) { |
| 1249 | cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable); |
| 1250 | reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error); |
| 1251 | if (reply == NULL || error != NULL) { |
| 1252 | _eglError(EGL_BAD_ALLOC, "xcb_get_geometry"); |
| 1253 | free(error); |
| 1254 | goto cleanup_dri_drawable; |
| 1255 | } |
| 1256 | |
| 1257 | dri2_surf->base.Width = reply->width; |
| 1258 | dri2_surf->base.Height = reply->height; |
| 1259 | free(reply); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1260 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1261 | |
| 1262 | return &dri2_surf->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 1263 | |
| 1264 | cleanup_dri_drawable: |
| 1265 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
| 1266 | cleanup_pixmap: |
| 1267 | if (type == EGL_PBUFFER_BIT) |
| 1268 | xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable); |
| 1269 | cleanup_surf: |
| 1270 | free(dri2_surf); |
| 1271 | |
| 1272 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 1277 | */ |
| 1278 | static _EGLSurface * |
| 1279 | dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 1280 | _EGLConfig *conf, EGLNativeWindowType window, |
| 1281 | const EGLint *attrib_list) |
| 1282 | { |
| 1283 | return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
| 1284 | window, attrib_list); |
| 1285 | } |
| 1286 | |
| 1287 | static _EGLSurface * |
| 1288 | dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 1289 | _EGLConfig *conf, EGLNativePixmapType pixmap, |
| 1290 | const EGLint *attrib_list) |
| 1291 | { |
| 1292 | return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf, |
| 1293 | pixmap, attrib_list); |
| 1294 | } |
| 1295 | |
| 1296 | static _EGLSurface * |
| 1297 | dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 1298 | _EGLConfig *conf, const EGLint *attrib_list) |
| 1299 | { |
| 1300 | return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf, |
| 1301 | XCB_WINDOW_NONE, attrib_list); |
| 1302 | } |
| 1303 | |
| 1304 | static EGLBoolean |
Kristian Høgsberg | 52c554a | 2010-05-06 22:01:35 -0400 | [diff] [blame] | 1305 | dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp, |
| 1306 | _EGLSurface *draw, xcb_xfixes_region_t region) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1307 | { |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1308 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1309 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1310 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1311 | _EGLContext *ctx; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1312 | xcb_dri2_copy_region_cookie_t cookie; |
| 1313 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1314 | if (dri2_drv->glFlush) { |
| 1315 | ctx = _eglGetCurrentContext(); |
| 1316 | if (ctx && ctx->DrawSurface == &dri2_surf->base) |
| 1317 | dri2_drv->glFlush(); |
| 1318 | } |
| 1319 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1320 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1321 | |
| 1322 | #if 0 |
| 1323 | /* FIXME: Add support for dri swapbuffers, that'll give us swap |
| 1324 | * interval and page flipping (at least for fullscreen windows) as |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 1325 | * well as the page flip event. Unless surface->SwapBehavior is |
| 1326 | * EGL_BUFFER_PRESERVED. */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1327 | #if __DRI2_FLUSH_VERSION >= 2 |
| 1328 | if (pdraw->psc->f) |
| 1329 | (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable); |
| 1330 | #endif |
| 1331 | #endif |
| 1332 | |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 1333 | if (!dri2_surf->have_fake_front) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1334 | return EGL_TRUE; |
| 1335 | |
| 1336 | cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn, |
| 1337 | dri2_surf->drawable, |
Kristian Høgsberg | 52c554a | 2010-05-06 22:01:35 -0400 | [diff] [blame] | 1338 | region, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1339 | XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT, |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 1340 | XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1341 | free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL)); |
| 1342 | |
| 1343 | return EGL_TRUE; |
| 1344 | } |
| 1345 | |
Kristian Høgsberg | 52c554a | 2010-05-06 22:01:35 -0400 | [diff] [blame] | 1346 | static EGLBoolean |
| 1347 | dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
| 1348 | { |
| 1349 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 1350 | |
| 1351 | return dri2_copy_region(drv, disp, draw, dri2_surf->region); |
| 1352 | } |
| 1353 | |
| 1354 | static EGLBoolean |
| 1355 | dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw, |
| 1356 | EGLint numRects, const EGLint *rects) |
| 1357 | { |
| 1358 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1359 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 1360 | EGLBoolean ret; |
| 1361 | xcb_xfixes_region_t region; |
| 1362 | xcb_rectangle_t rectangles[16]; |
| 1363 | int i; |
| 1364 | |
| 1365 | if (numRects > ARRAY_SIZE(rectangles)) |
| 1366 | return dri2_copy_region(drv, disp, draw, dri2_surf->region); |
| 1367 | |
| 1368 | /* FIXME: Invert y here? */ |
| 1369 | for (i = 0; i < numRects; i++) { |
| 1370 | rectangles[i].x = rects[i * 4]; |
| 1371 | rectangles[i].y = rects[i * 4 + 1]; |
| 1372 | rectangles[i].width = rects[i * 4 + 2]; |
| 1373 | rectangles[i].height = rects[i * 4 + 3]; |
| 1374 | } |
| 1375 | |
| 1376 | region = xcb_generate_id(dri2_dpy->conn); |
| 1377 | xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles); |
| 1378 | ret = dri2_copy_region(drv, disp, draw, region); |
| 1379 | xcb_xfixes_destroy_region(dri2_dpy->conn, region); |
| 1380 | |
| 1381 | return ret; |
| 1382 | } |
| 1383 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1384 | /* |
| 1385 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 1386 | */ |
| 1387 | static _EGLProc |
| 1388 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 1389 | { |
| 1390 | /* FIXME: Do we need to support lookup of EGL symbols too? */ |
| 1391 | |
| 1392 | return (_EGLProc) _glapi_get_proc_address(procname); |
| 1393 | } |
| 1394 | |
| 1395 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1396 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1397 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1398 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1399 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 1400 | |
| 1401 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 1402 | * we need to copy fake to real here.*/ |
| 1403 | |
| 1404 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1405 | |
| 1406 | return EGL_TRUE; |
| 1407 | } |
| 1408 | |
| 1409 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1410 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1411 | { |
| 1412 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 1413 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 1414 | /* glXWaitX(); */ |
| 1415 | |
| 1416 | return EGL_TRUE; |
| 1417 | } |
| 1418 | |
| 1419 | static void |
| 1420 | dri2_unload(_EGLDriver *drv) |
| 1421 | { |
| 1422 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
| 1423 | free(dri2_drv); |
| 1424 | } |
| 1425 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1426 | static EGLBoolean |
| 1427 | dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, |
| 1428 | EGLNativePixmapType target) |
| 1429 | { |
| 1430 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1431 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1432 | xcb_gcontext_t gc; |
| 1433 | |
| 1434 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
| 1435 | |
| 1436 | gc = xcb_generate_id(dri2_dpy->conn); |
| 1437 | xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL); |
| 1438 | xcb_copy_area(dri2_dpy->conn, |
| 1439 | dri2_surf->drawable, |
| 1440 | target, |
| 1441 | gc, |
| 1442 | 0, 0, |
| 1443 | 0, 0, |
| 1444 | dri2_surf->base.Width, |
| 1445 | dri2_surf->base.Height); |
| 1446 | xcb_free_gc(dri2_dpy->conn, gc); |
| 1447 | |
| 1448 | return EGL_TRUE; |
| 1449 | } |
| 1450 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1451 | static EGLBoolean |
| 1452 | dri2_bind_tex_image(_EGLDriver *drv, |
| 1453 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1454 | { |
| 1455 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1456 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1457 | struct dri2_egl_context *dri2_ctx; |
| 1458 | _EGLContext *ctx; |
| 1459 | GLint format, target; |
| 1460 | |
| 1461 | ctx = _eglGetCurrentContext(); |
| 1462 | dri2_ctx = dri2_egl_context(ctx); |
| 1463 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1464 | if (!_eglBindTexImage(drv, disp, surf, buffer)) |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1465 | return EGL_FALSE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1466 | |
| 1467 | switch (dri2_surf->base.TextureFormat) { |
| 1468 | case EGL_TEXTURE_RGB: |
| 1469 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 1470 | break; |
| 1471 | case EGL_TEXTURE_RGBA: |
| 1472 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 1473 | break; |
| 1474 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1475 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | switch (dri2_surf->base.TextureTarget) { |
| 1479 | case EGL_TEXTURE_2D: |
| 1480 | target = GL_TEXTURE_2D; |
| 1481 | break; |
| 1482 | default: |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1483 | assert(0); |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 1487 | target, format, |
| 1488 | dri2_surf->dri_drawable); |
| 1489 | |
Kristian Høgsberg | 5dc0f49 | 2010-05-06 22:40:25 -0400 | [diff] [blame] | 1490 | return EGL_TRUE; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | static EGLBoolean |
| 1494 | dri2_release_tex_image(_EGLDriver *drv, |
| 1495 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1496 | { |
| 1497 | return EGL_TRUE; |
| 1498 | } |
| 1499 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1500 | static _EGLImage * |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1501 | dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx, |
| 1502 | EGLClientBuffer buffer, const EGLint *attr_list) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1503 | { |
| 1504 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1505 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1506 | struct dri2_egl_image *dri2_img; |
| 1507 | unsigned int attachments[1]; |
| 1508 | xcb_drawable_t drawable; |
| 1509 | xcb_dri2_get_buffers_cookie_t buffers_cookie; |
| 1510 | xcb_dri2_get_buffers_reply_t *buffers_reply; |
| 1511 | xcb_dri2_dri2_buffer_t *buffers; |
| 1512 | xcb_get_geometry_cookie_t geometry_cookie; |
| 1513 | xcb_get_geometry_reply_t *geometry_reply; |
| 1514 | xcb_generic_error_t *error; |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1515 | int stride, format; |
| 1516 | |
| 1517 | drawable = (xcb_drawable_t) buffer; |
| 1518 | xcb_dri2_create_drawable (dri2_dpy->conn, drawable); |
| 1519 | attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT; |
| 1520 | buffers_cookie = |
| 1521 | xcb_dri2_get_buffers_unchecked (dri2_dpy->conn, |
| 1522 | drawable, 1, 1, attachments); |
| 1523 | geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable); |
| 1524 | buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, |
| 1525 | buffers_cookie, NULL); |
| 1526 | buffers = xcb_dri2_get_buffers_buffers (buffers_reply); |
| 1527 | if (buffers == NULL) { |
| 1528 | return NULL; |
| 1529 | } |
| 1530 | |
| 1531 | geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn, |
| 1532 | geometry_cookie, &error); |
| 1533 | if (geometry_reply == NULL || error != NULL) { |
| 1534 | _eglError(EGL_BAD_ALLOC, "xcb_get_geometry"); |
| 1535 | free(error); |
| 1536 | free(buffers_reply); |
| 1537 | } |
| 1538 | |
| 1539 | switch (geometry_reply->depth) { |
| 1540 | case 16: |
| 1541 | format = __DRI_IMAGE_FORMAT_RGB565; |
| 1542 | break; |
| 1543 | case 24: |
| 1544 | format = __DRI_IMAGE_FORMAT_XRGB8888; |
| 1545 | break; |
| 1546 | case 32: |
| 1547 | format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 1548 | break; |
| 1549 | default: |
| 1550 | _eglError(EGL_BAD_PARAMETER, |
| 1551 | "dri2_create_image_khr: unsupported pixmap depth"); |
| 1552 | free(buffers_reply); |
| 1553 | free(geometry_reply); |
| 1554 | return NULL; |
| 1555 | } |
| 1556 | |
| 1557 | dri2_img = malloc(sizeof *dri2_img); |
| 1558 | if (!dri2_img) { |
| 1559 | free(buffers_reply); |
| 1560 | free(geometry_reply); |
| 1561 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1562 | return EGL_NO_IMAGE_KHR; |
| 1563 | } |
| 1564 | |
| 1565 | if (!_eglInitImage(&dri2_img->base, disp, attr_list)) { |
| 1566 | free(buffers_reply); |
| 1567 | free(geometry_reply); |
| 1568 | return EGL_NO_IMAGE_KHR; |
| 1569 | } |
| 1570 | |
| 1571 | stride = buffers[0].pitch / buffers[0].cpp; |
| 1572 | dri2_img->dri_image = |
| 1573 | dri2_dpy->image->createImageFromName(dri2_ctx->dri_context, |
| 1574 | buffers_reply->width, |
| 1575 | buffers_reply->height, |
| 1576 | format, |
| 1577 | buffers[0].name, |
| 1578 | stride, |
| 1579 | dri2_img); |
| 1580 | |
| 1581 | free(buffers_reply); |
| 1582 | free(geometry_reply); |
| 1583 | |
| 1584 | return &dri2_img->base; |
| 1585 | } |
| 1586 | |
| 1587 | static _EGLImage * |
| 1588 | dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx, |
| 1589 | EGLClientBuffer buffer, |
| 1590 | const EGLint *attr_list) |
| 1591 | { |
| 1592 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1593 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1594 | struct dri2_egl_image *dri2_img; |
| 1595 | GLuint renderbuffer = (GLuint) buffer; |
| 1596 | |
| 1597 | if (renderbuffer == 0) { |
| 1598 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1599 | return EGL_NO_IMAGE_KHR; |
| 1600 | } |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1601 | |
| 1602 | dri2_img = malloc(sizeof *dri2_img); |
| 1603 | if (!dri2_img) { |
| 1604 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1605 | return EGL_NO_IMAGE_KHR; |
| 1606 | } |
| 1607 | |
| 1608 | if (!_eglInitImage(&dri2_img->base, disp, attr_list)) |
| 1609 | return EGL_NO_IMAGE_KHR; |
| 1610 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1611 | dri2_img->dri_image = |
| 1612 | dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context, |
| 1613 | renderbuffer, |
| 1614 | dri2_img); |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1615 | |
| 1616 | return &dri2_img->base; |
| 1617 | } |
| 1618 | |
Kristian Høgsberg | 557c20b | 2010-02-26 14:51:11 -0500 | [diff] [blame] | 1619 | static _EGLImage * |
| 1620 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 1621 | _EGLContext *ctx, EGLenum target, |
| 1622 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1623 | { |
| 1624 | switch (target) { |
| 1625 | case EGL_NATIVE_PIXMAP_KHR: |
| 1626 | return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list); |
| 1627 | case EGL_GL_RENDERBUFFER_KHR: |
| 1628 | return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list); |
| 1629 | default: |
| 1630 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1631 | return EGL_NO_IMAGE_KHR; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1635 | static EGLBoolean |
| 1636 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 1637 | { |
| 1638 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1639 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 1640 | |
| 1641 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 1642 | free(dri2_img); |
| 1643 | |
| 1644 | return EGL_TRUE; |
| 1645 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1646 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1647 | /** |
| 1648 | * This is the main entrypoint into the driver, called by libEGL. |
| 1649 | * Create a new _EGLDriver object and init its dispatch table. |
| 1650 | */ |
| 1651 | _EGLDriver * |
| 1652 | _eglMain(const char *args) |
| 1653 | { |
| 1654 | struct dri2_egl_driver *dri2_drv; |
| 1655 | |
| 1656 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1657 | if (!dri2_drv) |
| 1658 | return NULL; |
| 1659 | |
Kristian Høgsberg | 880acbe | 2010-05-12 14:34:55 -0400 | [diff] [blame] | 1660 | memset(dri2_drv, 0, sizeof *dri2_drv); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1661 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1662 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1663 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1664 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 1665 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
| 1666 | dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface; |
| 1667 | dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface; |
| 1668 | dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface; |
| 1669 | dri2_drv->base.API.DestroySurface = dri2_destroy_surface; |
| 1670 | dri2_drv->base.API.SwapBuffers = dri2_swap_buffers; |
| 1671 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1672 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1673 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1674 | dri2_drv->base.API.CopyBuffers = dri2_copy_buffers; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1675 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1676 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame] | 1677 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1678 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 52c554a | 2010-05-06 22:01:35 -0400 | [diff] [blame] | 1679 | dri2_drv->base.API.SwapBuffersRegionNOK = dri2_swap_buffers_region; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1680 | |
| 1681 | dri2_drv->base.Name = "DRI2"; |
| 1682 | dri2_drv->base.Unload = dri2_unload; |
| 1683 | |
Chia-I Wu | d18d0fd | 2010-04-06 19:52:39 +0800 | [diff] [blame] | 1684 | dri2_drv->glFlush = |
| 1685 | (void (*)(void)) dri2_get_proc_address(&dri2_drv->base, "glFlush"); |
| 1686 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1687 | return &dri2_drv->base; |
| 1688 | } |