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> |
| 43 | |
| 44 | #include <glapi/glapi.h> |
| 45 | #include "eglconfigutil.h" |
| 46 | #include "eglconfig.h" |
| 47 | #include "eglcontext.h" |
| 48 | #include "egldisplay.h" |
| 49 | #include "egldriver.h" |
| 50 | #include "eglcurrent.h" |
| 51 | #include "egllog.h" |
| 52 | #include "eglsurface.h" |
| 53 | |
| 54 | struct dri2_egl_driver |
| 55 | { |
| 56 | _EGLDriver base; |
| 57 | }; |
| 58 | |
| 59 | struct dri2_egl_display |
| 60 | { |
| 61 | xcb_connection_t *conn; |
| 62 | int dri2_major; |
| 63 | int dri2_minor; |
| 64 | __DRIscreen *dri_screen; |
| 65 | void *driver; |
| 66 | __DRIcoreExtension *core; |
| 67 | __DRIdri2Extension *dri2; |
| 68 | __DRI2flushExtension *flush; |
| 69 | int fd; |
| 70 | |
| 71 | __DRIdri2LoaderExtension loader_extension; |
| 72 | const __DRIextension *extensions[2]; |
| 73 | }; |
| 74 | |
| 75 | struct dri2_egl_context |
| 76 | { |
| 77 | _EGLContext base; |
| 78 | __DRIcontext *dri_context; |
| 79 | }; |
| 80 | |
| 81 | struct dri2_egl_surface |
| 82 | { |
| 83 | _EGLSurface base; |
| 84 | __DRIdrawable *dri_drawable; |
| 85 | xcb_drawable_t drawable; |
| 86 | __DRIbuffer buffers[5]; |
| 87 | int buffer_count; |
| 88 | xcb_xfixes_region_t region; |
| 89 | int have_back; |
| 90 | int have_fake_front; |
| 91 | int swap_interval; |
| 92 | }; |
| 93 | |
| 94 | struct dri2_egl_config |
| 95 | { |
| 96 | _EGLConfig base; |
| 97 | const __DRIconfig *dri_config; |
| 98 | }; |
| 99 | |
Chia-I Wu | a35e633 | 2010-02-05 11:38:34 +0800 | [diff] [blame] | 100 | /* standard typecasts */ |
| 101 | _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 102 | |
| 103 | EGLint dri2_to_egl_attribute_map[] = { |
| 104 | 0, |
| 105 | EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */ |
| 106 | EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */ |
| 107 | EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */ |
| 108 | EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */ |
| 109 | EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */ |
| 110 | 0, /* __DRI_ATTRIB_LUMINANCE_SIZE */ |
| 111 | EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */ |
| 112 | 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */ |
| 113 | EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */ |
| 114 | EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */ |
| 115 | 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */ |
| 116 | 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */ |
| 117 | 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */ |
| 118 | 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */ |
| 119 | EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */ |
| 120 | EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */ |
| 121 | 0, /* __DRI_ATTRIB_RENDER_TYPE, */ |
| 122 | 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */ |
| 123 | 0, /* __DRI_ATTRIB_CONFORMANT */ |
| 124 | 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */ |
| 125 | 0, /* __DRI_ATTRIB_STEREO */ |
| 126 | 0, /* __DRI_ATTRIB_AUX_BUFFERS */ |
| 127 | 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */ |
| 128 | 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */ |
| 129 | 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */ |
| 130 | 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */ |
| 131 | 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */ |
| 132 | 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */ |
| 133 | 0, /* __DRI_ATTRIB_FLOAT_MODE */ |
| 134 | 0, /* __DRI_ATTRIB_RED_MASK */ |
| 135 | 0, /* __DRI_ATTRIB_GREEN_MASK */ |
| 136 | 0, /* __DRI_ATTRIB_BLUE_MASK */ |
| 137 | 0, /* __DRI_ATTRIB_ALPHA_MASK */ |
| 138 | EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */ |
| 139 | EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */ |
| 140 | EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */ |
| 141 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */ |
| 142 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */ |
| 143 | 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */ |
| 144 | 0, /* __DRI_ATTRIB_SWAP_METHOD */ |
| 145 | EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */ |
| 146 | EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 147 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */ |
| 148 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 149 | 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */ |
| 150 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */ |
| 151 | 0, /* __DRI_ATTRIB_YINVERTED */ |
| 152 | }; |
| 153 | |
| 154 | static void |
| 155 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id) |
| 156 | { |
| 157 | struct dri2_egl_config *conf; |
| 158 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 159 | unsigned int attrib, value, double_buffer; |
| 160 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 161 | int i; |
| 162 | |
| 163 | dri2_dpy = disp->DriverData; |
| 164 | conf = malloc(sizeof *conf); |
| 165 | if (conf == NULL) |
| 166 | return; |
| 167 | |
| 168 | conf->dri_config = dri_config; |
| 169 | _eglInitConfig(&conf->base, disp, id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 170 | |
| 171 | i = 0; |
| 172 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 173 | switch (attrib) { |
| 174 | case 0: |
| 175 | break; |
| 176 | |
| 177 | case __DRI_ATTRIB_RENDER_TYPE: |
| 178 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 179 | value = EGL_RGB_BUFFER; |
| 180 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 181 | value = EGL_LUMINANCE_BUFFER; |
| 182 | else |
| 183 | /* not valid */; |
| 184 | _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, value); |
| 185 | break; |
| 186 | |
| 187 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 188 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 189 | value = EGL_NON_CONFORMANT_CONFIG; |
| 190 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 191 | value = EGL_SLOW_CONFIG; |
| 192 | else |
| 193 | value = EGL_NONE; |
| 194 | _eglSetConfigKey(&conf->base, EGL_CONFIG_CAVEAT, value); |
| 195 | break; |
| 196 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 197 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 198 | bind_to_texture_rgb = value; |
| 199 | break; |
| 200 | |
| 201 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 202 | bind_to_texture_rgba = value; |
| 203 | break; |
| 204 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 205 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 206 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | default: |
| 210 | key = dri2_to_egl_attribute_map[attrib]; |
| 211 | if (key != 0) |
| 212 | _eglSetConfigKey(&conf->base, key, value); |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* EGL_SWAP_BEHAVIOR_PRESERVED_BIT */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 218 | |
| 219 | if (double_buffer) { |
| 220 | /* FIXME: Figure out how to get the visual ID and types */ |
| 221 | _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT); |
| 222 | _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_ID, 0x21); |
| 223 | _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_TYPE, |
| 224 | XCB_VISUAL_CLASS_TRUE_COLOR); |
| 225 | } else { |
| 226 | _eglSetConfigKey(&conf->base, |
| 227 | EGL_SURFACE_TYPE, EGL_PIXMAP_BIT | EGL_PBUFFER_BIT); |
| 228 | _eglSetConfigKey(&conf->base, |
| 229 | EGL_BIND_TO_TEXTURE_RGB, bind_to_texture_rgb); |
| 230 | _eglSetConfigKey(&conf->base, |
| 231 | EGL_BIND_TO_TEXTURE_RGBA, bind_to_texture_rgba); |
| 232 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 233 | |
| 234 | /* EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT, EGL_OPENGL_ES2_BIT */ |
| 235 | _eglSetConfigKey(&conf->base, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT); |
| 236 | _eglSetConfigKey(&conf->base, EGL_CONFORMANT, EGL_OPENGL_BIT); |
| 237 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 238 | if (!_eglValidateConfig(&conf->base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 239 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 240 | free(conf); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | _eglAddConfig(disp, &conf->base); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Process list of buffer received from the server |
| 249 | * |
| 250 | * Processes the list of buffers received in a reply from the server to either |
| 251 | * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat. |
| 252 | */ |
| 253 | static void |
| 254 | dri2_process_buffers(struct dri2_egl_surface *dri2_surf, |
| 255 | xcb_dri2_dri2_buffer_t *buffers, unsigned count) |
| 256 | { |
| 257 | struct dri2_egl_display *dri2_dpy = |
| 258 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 259 | xcb_rectangle_t rectangle; |
| 260 | int i; |
| 261 | |
| 262 | dri2_surf->buffer_count = count; |
| 263 | dri2_surf->have_fake_front = 0; |
| 264 | dri2_surf->have_back = 0; |
| 265 | |
| 266 | /* This assumes the DRI2 buffer attachment tokens matches the |
| 267 | * __DRIbuffer tokens. */ |
| 268 | for (i = 0; i < count; i++) { |
| 269 | dri2_surf->buffers[i].attachment = buffers[i].attachment; |
| 270 | dri2_surf->buffers[i].name = buffers[i].name; |
| 271 | dri2_surf->buffers[i].pitch = buffers[i].pitch; |
| 272 | dri2_surf->buffers[i].cpp = buffers[i].cpp; |
| 273 | dri2_surf->buffers[i].flags = buffers[i].flags; |
| 274 | if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT) |
| 275 | dri2_surf->have_fake_front = 1; |
| 276 | if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT) |
| 277 | dri2_surf->have_back = 1; |
| 278 | } |
| 279 | |
| 280 | if (dri2_surf->region != XCB_NONE) |
| 281 | xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region); |
| 282 | |
| 283 | rectangle.x = 0; |
| 284 | rectangle.y = 0; |
| 285 | rectangle.width = dri2_surf->base.Width; |
| 286 | rectangle.height = dri2_surf->base.Height; |
| 287 | dri2_surf->region = xcb_generate_id(dri2_dpy->conn); |
| 288 | xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle); |
| 289 | } |
| 290 | |
| 291 | static __DRIbuffer * |
| 292 | dri2_get_buffers(__DRIdrawable * driDrawable, |
| 293 | int *width, int *height, |
| 294 | unsigned int *attachments, int count, |
| 295 | int *out_count, void *loaderPrivate) |
| 296 | { |
| 297 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 298 | struct dri2_egl_display *dri2_dpy = |
| 299 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 300 | xcb_dri2_dri2_buffer_t *buffers; |
| 301 | xcb_dri2_get_buffers_reply_t *reply; |
| 302 | xcb_dri2_get_buffers_cookie_t cookie; |
| 303 | |
| 304 | cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn, |
| 305 | dri2_surf->drawable, |
| 306 | count, count, attachments); |
| 307 | reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL); |
| 308 | buffers = xcb_dri2_get_buffers_buffers (reply); |
| 309 | if (buffers == NULL) |
| 310 | return NULL; |
| 311 | |
| 312 | *out_count = reply->count; |
| 313 | dri2_surf->base.Width = *width = reply->width; |
| 314 | dri2_surf->base.Height = *height = reply->height; |
| 315 | dri2_process_buffers(dri2_surf, buffers, *out_count); |
| 316 | |
| 317 | free(reply); |
| 318 | |
| 319 | return dri2_surf->buffers; |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
| 324 | { |
| 325 | /* FIXME: Does EGL support front buffer rendering at all? */ |
| 326 | |
| 327 | #if 0 |
| 328 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 329 | |
| 330 | dri2WaitGL(dri2_surf); |
| 331 | #endif |
| 332 | } |
| 333 | |
| 334 | static __DRIbuffer * |
| 335 | dri2_get_buffers_with_format(__DRIdrawable * driDrawable, |
| 336 | int *width, int *height, |
| 337 | unsigned int *attachments, int count, |
| 338 | int *out_count, void *loaderPrivate) |
| 339 | { |
| 340 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 341 | struct dri2_egl_display *dri2_dpy = |
| 342 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 343 | xcb_dri2_dri2_buffer_t *buffers; |
| 344 | xcb_dri2_get_buffers_with_format_reply_t *reply; |
| 345 | xcb_dri2_get_buffers_with_format_cookie_t cookie; |
| 346 | xcb_dri2_attach_format_t *format_attachments; |
| 347 | |
| 348 | format_attachments = (xcb_dri2_attach_format_t *) attachments; |
| 349 | cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn, |
| 350 | dri2_surf->drawable, |
| 351 | count, count, |
| 352 | format_attachments); |
| 353 | |
| 354 | reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn, |
| 355 | cookie, NULL); |
| 356 | if (reply == NULL) |
| 357 | return NULL; |
| 358 | |
| 359 | buffers = xcb_dri2_get_buffers_with_format_buffers (reply); |
| 360 | dri2_surf->base.Width = *width = reply->width; |
| 361 | dri2_surf->base.Height = *height = reply->height; |
| 362 | *out_count = reply->count; |
| 363 | dri2_process_buffers(dri2_surf, buffers, *out_count); |
| 364 | |
| 365 | free(reply); |
| 366 | |
| 367 | return dri2_surf->buffers; |
| 368 | } |
| 369 | |
| 370 | #ifdef GLX_USE_TLS |
| 371 | static const char dri_driver_format[] = "%.*s/tls/%.*s_dri.so"; |
| 372 | #else |
| 373 | static const char dri_driver_format[] = "%.*s/%.*s_dri.so"; |
| 374 | #endif |
| 375 | |
| 376 | static const char dri_driver_path[] = DEFAULT_DRIVER_DIR; |
| 377 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame^] | 378 | struct dri2_extension_match { |
| 379 | const char *name; |
| 380 | int version; |
| 381 | int offset; |
| 382 | }; |
| 383 | |
| 384 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 385 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 386 | { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) }, |
| 387 | { NULL } |
| 388 | }; |
| 389 | |
| 390 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 391 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
| 392 | { NULL } |
| 393 | }; |
| 394 | |
| 395 | static EGLBoolean |
| 396 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 397 | struct dri2_extension_match *matches, |
| 398 | const __DRIextension **extensions) |
| 399 | { |
| 400 | int i, j, ret = EGL_TRUE; |
| 401 | void *field; |
| 402 | |
| 403 | for (i = 0; extensions[i]; i++) { |
| 404 | _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name); |
| 405 | for (j = 0; matches[j].name; j++) { |
| 406 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 407 | extensions[i]->version >= matches[j].version) { |
| 408 | field = ((char *) dri2_dpy + matches[j].offset); |
| 409 | *(const __DRIextension **) field = extensions[i]; |
| 410 | _eglLog(_EGL_INFO, "DRI2: found extension %s version %d", |
| 411 | extensions[i]->name, extensions[i]->version); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | for (j = 0; matches[j].name; j++) { |
| 417 | field = ((char *) dri2_dpy + matches[j].offset); |
| 418 | if (*(const __DRIextension **) field == NULL) { |
| 419 | _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d", |
| 420 | matches[j].name, matches[j].version); |
| 421 | ret = EGL_FALSE; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 428 | /** |
| 429 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 430 | */ |
| 431 | static EGLBoolean |
| 432 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp, |
| 433 | EGLint *major, EGLint *minor) |
| 434 | { |
| 435 | const __DRIextension **extensions; |
| 436 | const __DRIconfig **driver_configs; |
| 437 | struct dri2_egl_display *dri2_dpy; |
| 438 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
| 439 | xcb_xfixes_query_version_reply_t *xfixes_query; |
| 440 | xcb_xfixes_query_version_cookie_t xfixes_query_cookie; |
| 441 | xcb_dri2_query_version_reply_t *dri2_query; |
| 442 | xcb_dri2_query_version_cookie_t dri2_query_cookie; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 443 | xcb_dri2_connect_reply_t *connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 444 | xcb_dri2_connect_cookie_t connect_cookie; |
| 445 | xcb_dri2_authenticate_reply_t *authenticate; |
| 446 | xcb_dri2_authenticate_cookie_t authenticate_cookie; |
| 447 | xcb_generic_error_t *error; |
| 448 | drm_magic_t magic; |
| 449 | xcb_screen_iterator_t s; |
| 450 | int i; |
| 451 | |
| 452 | dri2_dpy = malloc(sizeof *dri2_dpy); |
| 453 | if (!dri2_dpy) |
| 454 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 455 | |
| 456 | disp->DriverData = (void *) dri2_dpy; |
Kristian Høgsberg | 538539d | 2010-02-09 09:49:56 -0500 | [diff] [blame] | 457 | if (disp->NativeDisplay != NULL) |
| 458 | dri2_dpy->conn = XGetXCBConnection(disp->NativeDisplay); |
| 459 | else |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 460 | dri2_dpy->conn = xcb_connect(0, 0); |
Kristian Høgsberg | 538539d | 2010-02-09 09:49:56 -0500 | [diff] [blame] | 461 | if (!dri2_dpy->conn) { |
| 462 | _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed"); |
| 463 | goto cleanup_dpy; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id); |
| 467 | xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id); |
| 468 | |
| 469 | xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn, |
| 470 | XCB_XFIXES_MAJOR_VERSION, |
| 471 | XCB_XFIXES_MINOR_VERSION); |
| 472 | |
| 473 | dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn, |
| 474 | XCB_DRI2_MAJOR_VERSION, |
| 475 | XCB_DRI2_MINOR_VERSION); |
| 476 | |
| 477 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 478 | connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn, |
| 479 | s.data->root, |
| 480 | XCB_DRI2_DRIVER_TYPE_DRI); |
| 481 | |
| 482 | xfixes_query = |
| 483 | xcb_xfixes_query_version_reply (dri2_dpy->conn, |
| 484 | xfixes_query_cookie, &error); |
| 485 | if (xfixes_query == NULL || |
| 486 | error != NULL || xfixes_query->major_version < 2) { |
| 487 | _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version"); |
| 488 | free(error); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 489 | goto cleanup_conn; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 490 | } |
| 491 | free(xfixes_query); |
| 492 | |
| 493 | dri2_query = |
| 494 | xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error); |
| 495 | if (dri2_query == NULL || error != NULL) { |
| 496 | _eglLog(_EGL_FATAL, "DRI2: failed to query version"); |
| 497 | free(error); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 498 | goto cleanup_conn; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 499 | } |
| 500 | dri2_dpy->dri2_major = dri2_query->major_version; |
| 501 | dri2_dpy->dri2_minor = dri2_query->minor_version; |
| 502 | free(dri2_query); |
| 503 | |
| 504 | connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 505 | if (connect == NULL || |
| 506 | connect->driver_name_length + connect->device_name_length == 0) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 507 | _eglLog(_EGL_FATAL, "DRI2: failed to authenticate"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 508 | goto cleanup_connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | search_paths = NULL; |
| 512 | if (geteuid() == getuid()) { |
| 513 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 514 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 515 | } |
| 516 | if (search_paths == NULL) |
| 517 | search_paths = DEFAULT_DRIVER_DIR; |
| 518 | |
| 519 | dri2_dpy->driver = NULL; |
| 520 | end = search_paths + strlen(search_paths); |
| 521 | for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) { |
| 522 | next = strchr(p, ':'); |
| 523 | if (next == NULL) |
| 524 | next = end; |
| 525 | |
| 526 | snprintf(path, sizeof path, |
| 527 | dri_driver_format, |
Kristian Høgsberg | f27b4fa | 2010-02-05 09:58:27 -0500 | [diff] [blame] | 528 | (int) (next - p), p, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 529 | xcb_dri2_connect_driver_name_length (connect), |
| 530 | xcb_dri2_connect_driver_name (connect)); |
| 531 | |
| 532 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 533 | } |
| 534 | |
| 535 | if (dri2_dpy->driver == NULL) { |
| 536 | _eglLog(_EGL_FATAL, |
| 537 | "DRI2: failed to open any driver (search paths %s)", |
| 538 | search_paths); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 539 | goto cleanup_connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
| 543 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
| 544 | if (extensions == NULL) { |
| 545 | _eglLog(_EGL_FATAL, |
| 546 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 547 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 548 | } |
| 549 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame^] | 550 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 551 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 552 | |
| 553 | snprintf(path, sizeof path, "%.*s", |
| 554 | xcb_dri2_connect_device_name_length (connect), |
| 555 | xcb_dri2_connect_device_name (connect)); |
| 556 | dri2_dpy->fd = open (path, O_RDWR); |
| 557 | if (dri2_dpy->fd == -1) { |
| 558 | _eglLog(_EGL_FATAL, |
| 559 | "DRI2: could not open %s (%s)", path, strerror(errno)); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 560 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (drmGetMagic(dri2_dpy->fd, &magic)) { |
| 564 | _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 565 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | authenticate_cookie = xcb_dri2_authenticate_unchecked (dri2_dpy->conn, |
| 569 | s.data->root, magic); |
| 570 | authenticate = xcb_dri2_authenticate_reply (dri2_dpy->conn, |
| 571 | authenticate_cookie, NULL); |
| 572 | if (authenticate == NULL || !authenticate->authenticated) { |
| 573 | _eglLog(_EGL_FATAL, "DRI2: failed to authenticate"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 574 | free(authenticate); |
| 575 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 576 | } |
| 577 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 578 | free(authenticate); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 579 | if (dri2_dpy->dri2_minor >= 1) { |
| 580 | dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER; |
| 581 | dri2_dpy->loader_extension.base.version = 3; |
| 582 | dri2_dpy->loader_extension.getBuffers = dri2_get_buffers; |
| 583 | dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer; |
| 584 | dri2_dpy->loader_extension.getBuffersWithFormat = |
| 585 | dri2_get_buffers_with_format; |
| 586 | } else { |
| 587 | dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER; |
| 588 | dri2_dpy->loader_extension.base.version = 2; |
| 589 | dri2_dpy->loader_extension.getBuffers = dri2_get_buffers; |
| 590 | dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer; |
| 591 | dri2_dpy->loader_extension.getBuffersWithFormat = NULL; |
| 592 | } |
| 593 | |
| 594 | dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base; |
| 595 | dri2_dpy->extensions[1] = NULL; |
| 596 | |
| 597 | dri2_dpy->dri_screen = |
| 598 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
| 599 | &driver_configs, dri2_dpy); |
| 600 | |
| 601 | if (dri2_dpy->dri_screen == NULL) { |
| 602 | _eglLog(_EGL_FATAL, "DRI2: failed to create dri screen"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 603 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame^] | 607 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 608 | goto cleanup_dri_screen; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 609 | |
| 610 | for (i = 0; driver_configs[i]; i++) |
| 611 | dri2_add_config(disp, driver_configs[i], i + 1); |
| 612 | if (!disp->NumConfigs) { |
| 613 | _eglLog(_EGL_WARNING, "DRI2: failed to create any config"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 614 | goto cleanup_configs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | disp->ClientAPIsMask = EGL_OPENGL_BIT; |
| 618 | |
| 619 | /* we're supporting EGL 1.4 */ |
| 620 | *major = 1; |
| 621 | *minor = 4; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 622 | free (connect); |
| 623 | return EGL_TRUE; |
| 624 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 625 | cleanup_configs: |
| 626 | _eglCleanupDisplay(disp); |
| 627 | cleanup_dri_screen: |
| 628 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 629 | cleanup_fd: |
| 630 | close(dri2_dpy->fd); |
| 631 | cleanup_driver: |
| 632 | dlclose(dri2_dpy->driver); |
| 633 | cleanup_connect: |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 634 | free(connect); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 635 | cleanup_conn: |
| 636 | if (disp->NativeDisplay == NULL) |
| 637 | xcb_disconnect(dri2_dpy->conn); |
| 638 | cleanup_dpy: |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 639 | free(dri2_dpy); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 640 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 641 | return EGL_FALSE; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Called via eglTerminate(), drv->API.Terminate(). |
| 646 | */ |
| 647 | static EGLBoolean |
| 648 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 649 | { |
| 650 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 651 | |
| 652 | _eglReleaseDisplayResources(drv, disp); |
| 653 | _eglCleanupDisplay(disp); |
| 654 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 655 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 656 | close(dri2_dpy->fd); |
| 657 | dlclose(dri2_dpy->driver); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 658 | if (disp->NativeDisplay == NULL) |
| 659 | xcb_disconnect(dri2_dpy->conn); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 660 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 661 | disp->DriverData = NULL; |
| 662 | |
| 663 | return EGL_TRUE; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /** |
| 668 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 669 | */ |
| 670 | static _EGLContext * |
| 671 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 672 | _EGLContext *share_list, const EGLint *attrib_list) |
| 673 | { |
| 674 | struct dri2_egl_context *dri2_ctx; |
| 675 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 676 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 677 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
| 678 | |
| 679 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 680 | if (!dri2_ctx) { |
| 681 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 682 | return NULL; |
| 683 | } |
| 684 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 685 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 686 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 687 | |
| 688 | dri2_ctx->dri_context = |
| 689 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
| 690 | dri2_config->dri_config, |
| 691 | dri2_ctx_shared ? |
| 692 | dri2_ctx_shared->dri_context : NULL, |
| 693 | dri2_ctx); |
| 694 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 695 | if (!dri2_ctx->dri_context) |
| 696 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 697 | |
| 698 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 699 | |
| 700 | cleanup: |
| 701 | free(dri2_ctx); |
| 702 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static EGLBoolean |
| 706 | dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
| 707 | { |
| 708 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 709 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 710 | |
| 711 | if (_eglIsSurfaceBound(surf)) |
| 712 | return EGL_TRUE; |
| 713 | |
| 714 | (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); |
| 715 | |
| 716 | xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable); |
| 717 | |
| 718 | if (surf->Type == EGL_PBUFFER_BIT) |
| 719 | xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable); |
| 720 | |
| 721 | free(surf); |
| 722 | |
| 723 | return EGL_TRUE; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 728 | */ |
| 729 | static EGLBoolean |
| 730 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 731 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 732 | { |
| 733 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 734 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 735 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 736 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 737 | __DRIdrawable *ddraw, *rdraw; |
| 738 | __DRIcontext *cctx; |
| 739 | |
| 740 | /* bind the new context and return the "orphaned" one */ |
| 741 | if (!_eglBindContext(&ctx, &dsurf, &rsurf)) |
| 742 | return EGL_FALSE; |
| 743 | |
| 744 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 745 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 746 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 747 | |
| 748 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 749 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 750 | if (dsurf && !_eglIsSurfaceLinked(dsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 751 | dri2_destroy_surface(drv, disp, dsurf); |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 752 | if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 753 | dri2_destroy_surface(drv, disp, rsurf); |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 754 | if (ctx != NULL && !_eglIsContextLinked(ctx)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 755 | dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context); |
| 756 | |
| 757 | return EGL_TRUE; |
| 758 | } else { |
| 759 | _eglBindContext(&ctx, &dsurf, &rsurf); |
| 760 | |
| 761 | return EGL_FALSE; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 767 | */ |
| 768 | static _EGLSurface * |
| 769 | dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
| 770 | _EGLConfig *conf, EGLNativeWindowType window, |
| 771 | const EGLint *attrib_list) |
| 772 | { |
| 773 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 774 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 775 | struct dri2_egl_surface *dri2_surf; |
| 776 | xcb_get_geometry_cookie_t cookie; |
| 777 | xcb_get_geometry_reply_t *reply; |
| 778 | xcb_screen_iterator_t s; |
| 779 | xcb_generic_error_t *error; |
| 780 | |
| 781 | dri2_surf = malloc(sizeof *dri2_surf); |
| 782 | if (!dri2_surf) { |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 783 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 784 | return NULL; |
| 785 | } |
| 786 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 787 | if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) |
| 788 | goto cleanup_surf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 789 | |
| 790 | dri2_surf->region = XCB_NONE; |
| 791 | if (type == EGL_PBUFFER_BIT) { |
| 792 | dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn); |
| 793 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 794 | xcb_create_pixmap(dri2_dpy->conn, |
| 795 | _eglGetConfigKey(conf, EGL_BUFFER_SIZE), |
| 796 | dri2_surf->drawable, s.data->root, |
| 797 | dri2_surf->base.Width, dri2_surf->base.Height); |
| 798 | } else { |
| 799 | dri2_surf->drawable = window; |
| 800 | } |
| 801 | |
| 802 | dri2_surf->dri_drawable = |
| 803 | (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen, |
| 804 | dri2_conf->dri_config, dri2_surf); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 805 | if (dri2_surf->dri_drawable == NULL) { |
| 806 | _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable"); |
| 807 | goto cleanup_pixmap; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable); |
| 811 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 812 | if (type != EGL_PBUFFER_BIT) { |
| 813 | cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable); |
| 814 | reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error); |
| 815 | if (reply == NULL || error != NULL) { |
| 816 | _eglError(EGL_BAD_ALLOC, "xcb_get_geometry"); |
| 817 | free(error); |
| 818 | goto cleanup_dri_drawable; |
| 819 | } |
| 820 | |
| 821 | dri2_surf->base.Width = reply->width; |
| 822 | dri2_surf->base.Height = reply->height; |
| 823 | free(reply); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 824 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 825 | |
| 826 | return &dri2_surf->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 827 | |
| 828 | cleanup_dri_drawable: |
| 829 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
| 830 | cleanup_pixmap: |
| 831 | if (type == EGL_PBUFFER_BIT) |
| 832 | xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable); |
| 833 | cleanup_surf: |
| 834 | free(dri2_surf); |
| 835 | |
| 836 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 841 | */ |
| 842 | static _EGLSurface * |
| 843 | dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 844 | _EGLConfig *conf, EGLNativeWindowType window, |
| 845 | const EGLint *attrib_list) |
| 846 | { |
| 847 | return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
| 848 | window, attrib_list); |
| 849 | } |
| 850 | |
| 851 | static _EGLSurface * |
| 852 | dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 853 | _EGLConfig *conf, EGLNativePixmapType pixmap, |
| 854 | const EGLint *attrib_list) |
| 855 | { |
| 856 | return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf, |
| 857 | pixmap, attrib_list); |
| 858 | } |
| 859 | |
| 860 | static _EGLSurface * |
| 861 | dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 862 | _EGLConfig *conf, const EGLint *attrib_list) |
| 863 | { |
| 864 | return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf, |
| 865 | XCB_WINDOW_NONE, attrib_list); |
| 866 | } |
| 867 | |
| 868 | static EGLBoolean |
| 869 | dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
| 870 | { |
| 871 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 872 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 873 | xcb_dri2_copy_region_cookie_t cookie; |
| 874 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 875 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 876 | |
| 877 | #if 0 |
| 878 | /* FIXME: Add support for dri swapbuffers, that'll give us swap |
| 879 | * interval and page flipping (at least for fullscreen windows) as |
| 880 | * well as the page flip event. */ |
| 881 | #if __DRI2_FLUSH_VERSION >= 2 |
| 882 | if (pdraw->psc->f) |
| 883 | (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable); |
| 884 | #endif |
| 885 | #endif |
| 886 | |
| 887 | if (!dri2_surf->have_back) |
| 888 | return EGL_TRUE; |
| 889 | |
| 890 | cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn, |
| 891 | dri2_surf->drawable, |
| 892 | dri2_surf->region, |
| 893 | XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT, |
| 894 | XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT); |
| 895 | free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL)); |
| 896 | |
| 897 | return EGL_TRUE; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 902 | */ |
| 903 | static _EGLProc |
| 904 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 905 | { |
| 906 | /* FIXME: Do we need to support lookup of EGL symbols too? */ |
| 907 | |
| 908 | return (_EGLProc) _glapi_get_proc_address(procname); |
| 909 | } |
| 910 | |
| 911 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 912 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 913 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 914 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 915 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 916 | |
| 917 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 918 | * we need to copy fake to real here.*/ |
| 919 | |
| 920 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 921 | |
| 922 | return EGL_TRUE; |
| 923 | } |
| 924 | |
| 925 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 926 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 927 | { |
| 928 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 929 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 930 | /* glXWaitX(); */ |
| 931 | |
| 932 | return EGL_TRUE; |
| 933 | } |
| 934 | |
| 935 | static void |
| 936 | dri2_unload(_EGLDriver *drv) |
| 937 | { |
| 938 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
| 939 | free(dri2_drv); |
| 940 | } |
| 941 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 942 | static EGLBoolean |
| 943 | dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, |
| 944 | EGLNativePixmapType target) |
| 945 | { |
| 946 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 947 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 948 | xcb_gcontext_t gc; |
| 949 | |
| 950 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
| 951 | |
| 952 | gc = xcb_generate_id(dri2_dpy->conn); |
| 953 | xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL); |
| 954 | xcb_copy_area(dri2_dpy->conn, |
| 955 | dri2_surf->drawable, |
| 956 | target, |
| 957 | gc, |
| 958 | 0, 0, |
| 959 | 0, 0, |
| 960 | dri2_surf->base.Width, |
| 961 | dri2_surf->base.Height); |
| 962 | xcb_free_gc(dri2_dpy->conn, gc); |
| 963 | |
| 964 | return EGL_TRUE; |
| 965 | } |
| 966 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 967 | /** |
| 968 | * This is the main entrypoint into the driver, called by libEGL. |
| 969 | * Create a new _EGLDriver object and init its dispatch table. |
| 970 | */ |
| 971 | _EGLDriver * |
| 972 | _eglMain(const char *args) |
| 973 | { |
| 974 | struct dri2_egl_driver *dri2_drv; |
| 975 | |
| 976 | dri2_drv = malloc(sizeof *dri2_drv); |
| 977 | if (!dri2_drv) |
| 978 | return NULL; |
| 979 | |
| 980 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 981 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 982 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 983 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 984 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
| 985 | dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface; |
| 986 | dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface; |
| 987 | dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface; |
| 988 | dri2_drv->base.API.DestroySurface = dri2_destroy_surface; |
| 989 | dri2_drv->base.API.SwapBuffers = dri2_swap_buffers; |
| 990 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 991 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 992 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 993 | dri2_drv->base.API.CopyBuffers = dri2_copy_buffers; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 994 | |
| 995 | dri2_drv->base.Name = "DRI2"; |
| 996 | dri2_drv->base.Unload = dri2_unload; |
| 997 | |
| 998 | return &dri2_drv->base; |
| 999 | } |