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> |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 45 | #include "eglconfig.h" |
| 46 | #include "eglcontext.h" |
| 47 | #include "egldisplay.h" |
| 48 | #include "egldriver.h" |
| 49 | #include "eglcurrent.h" |
| 50 | #include "egllog.h" |
| 51 | #include "eglsurface.h" |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 52 | #include "eglimage.h" |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 53 | |
| 54 | struct dri2_egl_driver |
| 55 | { |
| 56 | _EGLDriver base; |
| 57 | }; |
| 58 | |
| 59 | struct dri2_egl_display |
| 60 | { |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 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 | __DRItexBufferExtension *tex_buffer; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 70 | __DRIimageExtension *image; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 71 | int fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 72 | |
| 73 | __DRIdri2LoaderExtension loader_extension; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 74 | __DRIimageLookupExtension image_lookup_extension; |
| 75 | const __DRIextension *extensions[3]; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct dri2_egl_context |
| 79 | { |
| 80 | _EGLContext base; |
| 81 | __DRIcontext *dri_context; |
| 82 | }; |
| 83 | |
| 84 | struct dri2_egl_surface |
| 85 | { |
| 86 | _EGLSurface base; |
| 87 | __DRIdrawable *dri_drawable; |
| 88 | xcb_drawable_t drawable; |
| 89 | __DRIbuffer buffers[5]; |
| 90 | int buffer_count; |
| 91 | xcb_xfixes_region_t region; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 92 | int have_fake_front; |
| 93 | int swap_interval; |
| 94 | }; |
| 95 | |
| 96 | struct dri2_egl_config |
| 97 | { |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 98 | _EGLConfig base; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 99 | const __DRIconfig *dri_config; |
| 100 | }; |
| 101 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 102 | struct dri2_egl_image |
| 103 | { |
| 104 | _EGLImage base; |
| 105 | __DRIimage *dri_image; |
| 106 | int width; |
| 107 | int height; |
| 108 | int name; |
| 109 | int pitch; |
| 110 | int cpp; |
| 111 | int format; |
| 112 | }; |
| 113 | |
Chia-I Wu | a35e633 | 2010-02-05 11:38:34 +0800 | [diff] [blame] | 114 | /* standard typecasts */ |
| 115 | _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl) |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 116 | _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 117 | |
| 118 | EGLint dri2_to_egl_attribute_map[] = { |
| 119 | 0, |
| 120 | EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */ |
| 121 | EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */ |
| 122 | EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */ |
| 123 | EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */ |
| 124 | EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 125 | EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 126 | EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */ |
| 127 | 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */ |
| 128 | EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */ |
| 129 | EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */ |
| 130 | 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */ |
| 131 | 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */ |
| 132 | 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */ |
| 133 | 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */ |
| 134 | EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */ |
| 135 | EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */ |
| 136 | 0, /* __DRI_ATTRIB_RENDER_TYPE, */ |
| 137 | 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */ |
| 138 | 0, /* __DRI_ATTRIB_CONFORMANT */ |
| 139 | 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */ |
| 140 | 0, /* __DRI_ATTRIB_STEREO */ |
| 141 | 0, /* __DRI_ATTRIB_AUX_BUFFERS */ |
| 142 | 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */ |
| 143 | 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */ |
| 144 | 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */ |
| 145 | 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */ |
| 146 | 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */ |
| 147 | 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */ |
| 148 | 0, /* __DRI_ATTRIB_FLOAT_MODE */ |
| 149 | 0, /* __DRI_ATTRIB_RED_MASK */ |
| 150 | 0, /* __DRI_ATTRIB_GREEN_MASK */ |
| 151 | 0, /* __DRI_ATTRIB_BLUE_MASK */ |
| 152 | 0, /* __DRI_ATTRIB_ALPHA_MASK */ |
| 153 | EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */ |
| 154 | EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */ |
| 155 | EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */ |
| 156 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */ |
| 157 | 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */ |
| 158 | 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */ |
| 159 | 0, /* __DRI_ATTRIB_SWAP_METHOD */ |
| 160 | EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */ |
| 161 | EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */ |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 162 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */ |
| 163 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 164 | 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */ |
| 165 | 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */ |
| 166 | 0, /* __DRI_ATTRIB_YINVERTED */ |
| 167 | }; |
| 168 | |
| 169 | static void |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 170 | dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id, |
| 171 | int depth, xcb_visualtype_t *visual) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 172 | { |
| 173 | struct dri2_egl_config *conf; |
| 174 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 175 | _EGLConfig base; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 176 | unsigned int attrib, value, double_buffer; |
| 177 | EGLint key, bind_to_texture_rgb, bind_to_texture_rgba; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 178 | int i; |
| 179 | |
| 180 | dri2_dpy = disp->DriverData; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 181 | _eglInitConfig(&base, disp, id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 182 | |
| 183 | i = 0; |
| 184 | while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) { |
| 185 | switch (attrib) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 186 | case __DRI_ATTRIB_RENDER_TYPE: |
| 187 | if (value & __DRI_ATTRIB_RGBA_BIT) |
| 188 | value = EGL_RGB_BUFFER; |
| 189 | else if (value & __DRI_ATTRIB_LUMINANCE_BIT) |
| 190 | value = EGL_LUMINANCE_BUFFER; |
| 191 | else |
| 192 | /* not valid */; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 193 | _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 194 | break; |
| 195 | |
| 196 | case __DRI_ATTRIB_CONFIG_CAVEAT: |
| 197 | if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG) |
| 198 | value = EGL_NON_CONFORMANT_CONFIG; |
| 199 | else if (value & __DRI_ATTRIB_SLOW_BIT) |
| 200 | value = EGL_SLOW_CONFIG; |
| 201 | else |
| 202 | value = EGL_NONE; |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 203 | _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 204 | break; |
| 205 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 206 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB: |
| 207 | bind_to_texture_rgb = value; |
| 208 | break; |
| 209 | |
| 210 | case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA: |
| 211 | bind_to_texture_rgba = value; |
| 212 | break; |
| 213 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 214 | case __DRI_ATTRIB_DOUBLE_BUFFER: |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 215 | double_buffer = value; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | default: |
| 219 | key = dri2_to_egl_attribute_map[attrib]; |
| 220 | if (key != 0) |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 221 | _eglSetConfigKey(&base, key, value); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 226 | /* In EGL, double buffer or not isn't a config attribute. Pixmaps |
| 227 | * surfaces are always single buffered, pbuffer surfaces are always |
| 228 | * back buffers and windows can be either, selected by passing an |
| 229 | * attribute at window surface construction time. To support this |
| 230 | * we ignore all double buffer configs and manipulate the buffer we |
| 231 | * return in the getBuffer callback to get the behaviour we want. */ |
| 232 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 233 | if (double_buffer) |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 234 | return; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 235 | |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 236 | if (visual != NULL) { |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 237 | if (depth != _eglGetConfigKey(&base, EGL_BUFFER_SIZE)) |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 238 | return; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 239 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 240 | _eglSetConfigKey(&base, EGL_SURFACE_TYPE, |
| 241 | EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT | |
| 242 | EGL_SWAP_BEHAVIOR_PRESERVED_BIT); |
| 243 | |
| 244 | _eglSetConfigKey(&base, EGL_NATIVE_VISUAL_ID, visual->visual_id); |
| 245 | _eglSetConfigKey(&base, EGL_NATIVE_VISUAL_TYPE, visual->_class); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 246 | } else { |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 247 | _eglSetConfigKey(&base, EGL_SURFACE_TYPE, |
Kristian Høgsberg | 360faf8 | 2010-02-09 16:06:23 -0500 | [diff] [blame] | 248 | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT); |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 249 | } |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 250 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 251 | _eglSetConfigKey(&base, EGL_NATIVE_RENDERABLE, EGL_TRUE); |
| 252 | _eglSetConfigKey(&base, EGL_BIND_TO_TEXTURE_RGB, bind_to_texture_rgb); |
| 253 | if (_eglGetConfigKey(&base, EGL_ALPHA_SIZE) > 0) |
| 254 | _eglSetConfigKey(&base, |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 255 | EGL_BIND_TO_TEXTURE_RGBA, bind_to_texture_rgba); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 256 | |
| 257 | /* EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT, EGL_OPENGL_ES2_BIT */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 258 | _eglSetConfigKey(&base, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT); |
| 259 | _eglSetConfigKey(&base, EGL_CONFORMANT, EGL_OPENGL_BIT); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 260 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 261 | if (!_eglValidateConfig(&base, EGL_FALSE)) { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 262 | _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 266 | conf = malloc(sizeof *conf); |
| 267 | if (conf != NULL) { |
| 268 | memcpy(&conf->base, &base, sizeof base); |
| 269 | conf->dri_config = dri_config; |
| 270 | _eglAddConfig(disp, &conf->base); |
| 271 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Process list of buffer received from the server |
| 276 | * |
| 277 | * Processes the list of buffers received in a reply from the server to either |
| 278 | * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat. |
| 279 | */ |
| 280 | static void |
| 281 | dri2_process_buffers(struct dri2_egl_surface *dri2_surf, |
| 282 | xcb_dri2_dri2_buffer_t *buffers, unsigned count) |
| 283 | { |
| 284 | struct dri2_egl_display *dri2_dpy = |
| 285 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 286 | xcb_rectangle_t rectangle; |
| 287 | int i; |
| 288 | |
| 289 | dri2_surf->buffer_count = count; |
| 290 | dri2_surf->have_fake_front = 0; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 291 | |
| 292 | /* This assumes the DRI2 buffer attachment tokens matches the |
| 293 | * __DRIbuffer tokens. */ |
| 294 | for (i = 0; i < count; i++) { |
| 295 | dri2_surf->buffers[i].attachment = buffers[i].attachment; |
| 296 | dri2_surf->buffers[i].name = buffers[i].name; |
| 297 | dri2_surf->buffers[i].pitch = buffers[i].pitch; |
| 298 | dri2_surf->buffers[i].cpp = buffers[i].cpp; |
| 299 | dri2_surf->buffers[i].flags = buffers[i].flags; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 300 | |
| 301 | /* We only use the DRI drivers single buffer configs. This |
| 302 | * means that if we try to render to a window, DRI2 will give us |
| 303 | * the fake front buffer, which we'll use as a back buffer. |
| 304 | * Note that EGL doesn't require that several clients rendering |
| 305 | * to the same window must see the same aux buffers. */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 306 | if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT) |
| 307 | dri2_surf->have_fake_front = 1; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (dri2_surf->region != XCB_NONE) |
| 311 | xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region); |
| 312 | |
| 313 | rectangle.x = 0; |
| 314 | rectangle.y = 0; |
| 315 | rectangle.width = dri2_surf->base.Width; |
| 316 | rectangle.height = dri2_surf->base.Height; |
| 317 | dri2_surf->region = xcb_generate_id(dri2_dpy->conn); |
| 318 | xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle); |
| 319 | } |
| 320 | |
| 321 | static __DRIbuffer * |
| 322 | dri2_get_buffers(__DRIdrawable * driDrawable, |
| 323 | int *width, int *height, |
| 324 | unsigned int *attachments, int count, |
| 325 | int *out_count, void *loaderPrivate) |
| 326 | { |
| 327 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 328 | struct dri2_egl_display *dri2_dpy = |
| 329 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 330 | xcb_dri2_dri2_buffer_t *buffers; |
| 331 | xcb_dri2_get_buffers_reply_t *reply; |
| 332 | xcb_dri2_get_buffers_cookie_t cookie; |
| 333 | |
| 334 | cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn, |
| 335 | dri2_surf->drawable, |
| 336 | count, count, attachments); |
| 337 | reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL); |
| 338 | buffers = xcb_dri2_get_buffers_buffers (reply); |
| 339 | if (buffers == NULL) |
| 340 | return NULL; |
| 341 | |
| 342 | *out_count = reply->count; |
| 343 | dri2_surf->base.Width = *width = reply->width; |
| 344 | dri2_surf->base.Height = *height = reply->height; |
| 345 | dri2_process_buffers(dri2_surf, buffers, *out_count); |
| 346 | |
| 347 | free(reply); |
| 348 | |
| 349 | return dri2_surf->buffers; |
| 350 | } |
| 351 | |
| 352 | static void |
| 353 | dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
| 354 | { |
| 355 | /* FIXME: Does EGL support front buffer rendering at all? */ |
| 356 | |
| 357 | #if 0 |
| 358 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 359 | |
| 360 | dri2WaitGL(dri2_surf); |
| 361 | #endif |
| 362 | } |
| 363 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 364 | static __DRIimage * |
| 365 | dri2_lookup_egl_image(__DRIcontext *context, void *image, void *data) |
| 366 | { |
| 367 | struct dri2_egl_context *dri2_ctx = data; |
| 368 | _EGLDisplay *disp = dri2_ctx->base.Resource.Display; |
| 369 | struct dri2_egl_image *dri2_img; |
| 370 | _EGLImage *img; |
| 371 | |
| 372 | img = _eglLookupImage(image, disp); |
| 373 | if (img == NULL) { |
| 374 | _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image"); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | dri2_img = dri2_egl_image(image); |
| 379 | |
| 380 | return dri2_img->dri_image; |
| 381 | } |
| 382 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 383 | static __DRIbuffer * |
| 384 | dri2_get_buffers_with_format(__DRIdrawable * driDrawable, |
| 385 | int *width, int *height, |
| 386 | unsigned int *attachments, int count, |
| 387 | int *out_count, void *loaderPrivate) |
| 388 | { |
| 389 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 390 | struct dri2_egl_display *dri2_dpy = |
| 391 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 392 | xcb_dri2_dri2_buffer_t *buffers; |
| 393 | xcb_dri2_get_buffers_with_format_reply_t *reply; |
| 394 | xcb_dri2_get_buffers_with_format_cookie_t cookie; |
| 395 | xcb_dri2_attach_format_t *format_attachments; |
| 396 | |
| 397 | format_attachments = (xcb_dri2_attach_format_t *) attachments; |
| 398 | cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn, |
| 399 | dri2_surf->drawable, |
| 400 | count, count, |
| 401 | format_attachments); |
| 402 | |
| 403 | reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn, |
| 404 | cookie, NULL); |
| 405 | if (reply == NULL) |
| 406 | return NULL; |
| 407 | |
| 408 | buffers = xcb_dri2_get_buffers_with_format_buffers (reply); |
| 409 | dri2_surf->base.Width = *width = reply->width; |
| 410 | dri2_surf->base.Height = *height = reply->height; |
| 411 | *out_count = reply->count; |
| 412 | dri2_process_buffers(dri2_surf, buffers, *out_count); |
| 413 | |
| 414 | free(reply); |
| 415 | |
| 416 | return dri2_surf->buffers; |
| 417 | } |
| 418 | |
| 419 | #ifdef GLX_USE_TLS |
| 420 | static const char dri_driver_format[] = "%.*s/tls/%.*s_dri.so"; |
| 421 | #else |
| 422 | static const char dri_driver_format[] = "%.*s/%.*s_dri.so"; |
| 423 | #endif |
| 424 | |
| 425 | static const char dri_driver_path[] = DEFAULT_DRIVER_DIR; |
| 426 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 427 | struct dri2_extension_match { |
| 428 | const char *name; |
| 429 | int version; |
| 430 | int offset; |
| 431 | }; |
| 432 | |
| 433 | static struct dri2_extension_match dri2_driver_extensions[] = { |
| 434 | { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) }, |
| 435 | { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) }, |
| 436 | { NULL } |
| 437 | }; |
| 438 | |
| 439 | static struct dri2_extension_match dri2_core_extensions[] = { |
| 440 | { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) }, |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 441 | { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) }, |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 442 | { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) }, |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 443 | { NULL } |
| 444 | }; |
| 445 | |
| 446 | static EGLBoolean |
| 447 | dri2_bind_extensions(struct dri2_egl_display *dri2_dpy, |
| 448 | struct dri2_extension_match *matches, |
| 449 | const __DRIextension **extensions) |
| 450 | { |
| 451 | int i, j, ret = EGL_TRUE; |
| 452 | void *field; |
| 453 | |
| 454 | for (i = 0; extensions[i]; i++) { |
| 455 | _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name); |
| 456 | for (j = 0; matches[j].name; j++) { |
| 457 | if (strcmp(extensions[i]->name, matches[j].name) == 0 && |
| 458 | extensions[i]->version >= matches[j].version) { |
| 459 | field = ((char *) dri2_dpy + matches[j].offset); |
| 460 | *(const __DRIextension **) field = extensions[i]; |
| 461 | _eglLog(_EGL_INFO, "DRI2: found extension %s version %d", |
| 462 | extensions[i]->name, extensions[i]->version); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | for (j = 0; matches[j].name; j++) { |
| 468 | field = ((char *) dri2_dpy + matches[j].offset); |
| 469 | if (*(const __DRIextension **) field == NULL) { |
| 470 | _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d", |
| 471 | matches[j].name, matches[j].version); |
| 472 | ret = EGL_FALSE; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 479 | /** |
| 480 | * Called via eglInitialize(), GLX_drv->API.Initialize(). |
| 481 | */ |
| 482 | static EGLBoolean |
| 483 | dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp, |
| 484 | EGLint *major, EGLint *minor) |
| 485 | { |
| 486 | const __DRIextension **extensions; |
| 487 | const __DRIconfig **driver_configs; |
| 488 | struct dri2_egl_display *dri2_dpy; |
| 489 | char path[PATH_MAX], *search_paths, *p, *next, *end; |
| 490 | xcb_xfixes_query_version_reply_t *xfixes_query; |
| 491 | xcb_xfixes_query_version_cookie_t xfixes_query_cookie; |
| 492 | xcb_dri2_query_version_reply_t *dri2_query; |
| 493 | xcb_dri2_query_version_cookie_t dri2_query_cookie; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 494 | xcb_dri2_connect_reply_t *connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 495 | xcb_dri2_connect_cookie_t connect_cookie; |
| 496 | xcb_dri2_authenticate_reply_t *authenticate; |
| 497 | xcb_dri2_authenticate_cookie_t authenticate_cookie; |
| 498 | xcb_generic_error_t *error; |
| 499 | drm_magic_t magic; |
| 500 | xcb_screen_iterator_t s; |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 501 | xcb_depth_iterator_t d; |
| 502 | xcb_visualtype_t *visuals; |
| 503 | int i, j, id; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 504 | |
| 505 | dri2_dpy = malloc(sizeof *dri2_dpy); |
| 506 | if (!dri2_dpy) |
| 507 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 508 | |
| 509 | disp->DriverData = (void *) dri2_dpy; |
Kristian Høgsberg | 538539d | 2010-02-09 09:49:56 -0500 | [diff] [blame] | 510 | if (disp->NativeDisplay != NULL) |
| 511 | dri2_dpy->conn = XGetXCBConnection(disp->NativeDisplay); |
| 512 | else |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 513 | dri2_dpy->conn = xcb_connect(0, 0); |
Kristian Høgsberg | 538539d | 2010-02-09 09:49:56 -0500 | [diff] [blame] | 514 | if (!dri2_dpy->conn) { |
| 515 | _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed"); |
| 516 | goto cleanup_dpy; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id); |
| 520 | xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id); |
| 521 | |
| 522 | xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn, |
| 523 | XCB_XFIXES_MAJOR_VERSION, |
| 524 | XCB_XFIXES_MINOR_VERSION); |
| 525 | |
| 526 | dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn, |
| 527 | XCB_DRI2_MAJOR_VERSION, |
| 528 | XCB_DRI2_MINOR_VERSION); |
| 529 | |
| 530 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 531 | connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn, |
| 532 | s.data->root, |
| 533 | XCB_DRI2_DRIVER_TYPE_DRI); |
| 534 | |
| 535 | xfixes_query = |
| 536 | xcb_xfixes_query_version_reply (dri2_dpy->conn, |
| 537 | xfixes_query_cookie, &error); |
| 538 | if (xfixes_query == NULL || |
| 539 | error != NULL || xfixes_query->major_version < 2) { |
| 540 | _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version"); |
| 541 | free(error); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 542 | goto cleanup_conn; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 543 | } |
| 544 | free(xfixes_query); |
| 545 | |
| 546 | dri2_query = |
| 547 | xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error); |
| 548 | if (dri2_query == NULL || error != NULL) { |
| 549 | _eglLog(_EGL_FATAL, "DRI2: failed to query version"); |
| 550 | free(error); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 551 | goto cleanup_conn; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 552 | } |
| 553 | dri2_dpy->dri2_major = dri2_query->major_version; |
| 554 | dri2_dpy->dri2_minor = dri2_query->minor_version; |
| 555 | free(dri2_query); |
| 556 | |
| 557 | connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 558 | if (connect == NULL || |
| 559 | connect->driver_name_length + connect->device_name_length == 0) { |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 560 | _eglLog(_EGL_FATAL, "DRI2: failed to authenticate"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 561 | goto cleanup_connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | search_paths = NULL; |
| 565 | if (geteuid() == getuid()) { |
| 566 | /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */ |
| 567 | search_paths = getenv("LIBGL_DRIVERS_PATH"); |
| 568 | } |
| 569 | if (search_paths == NULL) |
| 570 | search_paths = DEFAULT_DRIVER_DIR; |
| 571 | |
| 572 | dri2_dpy->driver = NULL; |
| 573 | end = search_paths + strlen(search_paths); |
| 574 | for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) { |
| 575 | next = strchr(p, ':'); |
| 576 | if (next == NULL) |
| 577 | next = end; |
| 578 | |
| 579 | snprintf(path, sizeof path, |
| 580 | dri_driver_format, |
Kristian Høgsberg | f27b4fa | 2010-02-05 09:58:27 -0500 | [diff] [blame] | 581 | (int) (next - p), p, |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 582 | xcb_dri2_connect_driver_name_length (connect), |
| 583 | xcb_dri2_connect_driver_name (connect)); |
| 584 | |
| 585 | dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
| 586 | } |
| 587 | |
| 588 | if (dri2_dpy->driver == NULL) { |
| 589 | _eglLog(_EGL_FATAL, |
| 590 | "DRI2: failed to open any driver (search paths %s)", |
| 591 | search_paths); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 592 | goto cleanup_connect; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path); |
| 596 | extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS); |
| 597 | if (extensions == NULL) { |
| 598 | _eglLog(_EGL_FATAL, |
| 599 | "DRI2: driver exports no extensions (%s)", dlerror()); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 600 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 601 | } |
| 602 | |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 603 | if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 604 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 605 | |
| 606 | snprintf(path, sizeof path, "%.*s", |
| 607 | xcb_dri2_connect_device_name_length (connect), |
| 608 | xcb_dri2_connect_device_name (connect)); |
| 609 | dri2_dpy->fd = open (path, O_RDWR); |
| 610 | if (dri2_dpy->fd == -1) { |
| 611 | _eglLog(_EGL_FATAL, |
| 612 | "DRI2: could not open %s (%s)", path, strerror(errno)); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 613 | goto cleanup_driver; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (drmGetMagic(dri2_dpy->fd, &magic)) { |
| 617 | _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 618 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | authenticate_cookie = xcb_dri2_authenticate_unchecked (dri2_dpy->conn, |
| 622 | s.data->root, magic); |
| 623 | authenticate = xcb_dri2_authenticate_reply (dri2_dpy->conn, |
| 624 | authenticate_cookie, NULL); |
| 625 | if (authenticate == NULL || !authenticate->authenticated) { |
| 626 | _eglLog(_EGL_FATAL, "DRI2: failed to authenticate"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 627 | free(authenticate); |
| 628 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 631 | free(authenticate); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 632 | if (dri2_dpy->dri2_minor >= 1) { |
| 633 | dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER; |
| 634 | dri2_dpy->loader_extension.base.version = 3; |
| 635 | dri2_dpy->loader_extension.getBuffers = dri2_get_buffers; |
| 636 | dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer; |
| 637 | dri2_dpy->loader_extension.getBuffersWithFormat = |
| 638 | dri2_get_buffers_with_format; |
| 639 | } else { |
| 640 | dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER; |
| 641 | dri2_dpy->loader_extension.base.version = 2; |
| 642 | dri2_dpy->loader_extension.getBuffers = dri2_get_buffers; |
| 643 | dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer; |
| 644 | dri2_dpy->loader_extension.getBuffersWithFormat = NULL; |
| 645 | } |
| 646 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 647 | dri2_dpy->image_lookup_extension.base.name = __DRI_IMAGE_LOOKUP; |
| 648 | dri2_dpy->image_lookup_extension.base.version = 1; |
| 649 | dri2_dpy->image_lookup_extension.lookupEGLImage = dri2_lookup_egl_image; |
| 650 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 651 | dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 652 | dri2_dpy->extensions[1] = &dri2_dpy->image_lookup_extension.base; |
| 653 | dri2_dpy->extensions[2] = NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 654 | |
| 655 | dri2_dpy->dri_screen = |
| 656 | dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions, |
| 657 | &driver_configs, dri2_dpy); |
| 658 | |
| 659 | if (dri2_dpy->dri_screen == NULL) { |
| 660 | _eglLog(_EGL_FATAL, "DRI2: failed to create dri screen"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 661 | goto cleanup_fd; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen); |
Kristian Høgsberg | 8c593d3 | 2010-02-09 10:57:43 -0500 | [diff] [blame] | 665 | if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions)) |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 666 | goto cleanup_dri_screen; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 667 | |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 668 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 669 | d = xcb_screen_allowed_depths_iterator(s.data); |
| 670 | id = 1; |
| 671 | while (d.rem > 0) { |
| 672 | EGLBoolean class_added[6] = { 0, }; |
| 673 | |
| 674 | visuals = xcb_depth_visuals(d.data); |
| 675 | for (i = 0; i < xcb_depth_visuals_length(d.data); i++) { |
| 676 | if (class_added[visuals[i]._class]) |
| 677 | continue; |
| 678 | |
| 679 | class_added[visuals[i]._class] = EGL_TRUE; |
| 680 | for (j = 0; driver_configs[j]; j++) |
| 681 | dri2_add_config(disp, driver_configs[j], |
| 682 | id++, d.data->depth, &visuals[i]); |
| 683 | |
| 684 | } |
| 685 | |
| 686 | xcb_depth_next(&d); |
| 687 | } |
| 688 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 689 | if (!disp->NumConfigs) { |
| 690 | _eglLog(_EGL_WARNING, "DRI2: failed to create any config"); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 691 | goto cleanup_configs; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | disp->ClientAPIsMask = EGL_OPENGL_BIT; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 695 | disp->Extensions.KHR_image_base = EGL_TRUE; |
| 696 | disp->Extensions.KHR_image_pixmap = EGL_TRUE; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 697 | |
| 698 | /* we're supporting EGL 1.4 */ |
| 699 | *major = 1; |
| 700 | *minor = 4; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 701 | free (connect); |
| 702 | return EGL_TRUE; |
| 703 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 704 | cleanup_configs: |
| 705 | _eglCleanupDisplay(disp); |
| 706 | cleanup_dri_screen: |
| 707 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
| 708 | cleanup_fd: |
| 709 | close(dri2_dpy->fd); |
| 710 | cleanup_driver: |
| 711 | dlclose(dri2_dpy->driver); |
| 712 | cleanup_connect: |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 713 | free(connect); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 714 | cleanup_conn: |
| 715 | if (disp->NativeDisplay == NULL) |
| 716 | xcb_disconnect(dri2_dpy->conn); |
| 717 | cleanup_dpy: |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 718 | free(dri2_dpy); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 719 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 720 | return EGL_FALSE; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Called via eglTerminate(), drv->API.Terminate(). |
| 725 | */ |
| 726 | static EGLBoolean |
| 727 | dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp) |
| 728 | { |
| 729 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 730 | |
| 731 | _eglReleaseDisplayResources(drv, disp); |
| 732 | _eglCleanupDisplay(disp); |
| 733 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 734 | dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 735 | close(dri2_dpy->fd); |
| 736 | dlclose(dri2_dpy->driver); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 737 | if (disp->NativeDisplay == NULL) |
| 738 | xcb_disconnect(dri2_dpy->conn); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 739 | free(dri2_dpy); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 740 | disp->DriverData = NULL; |
| 741 | |
| 742 | return EGL_TRUE; |
| 743 | } |
| 744 | |
| 745 | |
| 746 | /** |
| 747 | * Called via eglCreateContext(), drv->API.CreateContext(). |
| 748 | */ |
| 749 | static _EGLContext * |
| 750 | dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf, |
| 751 | _EGLContext *share_list, const EGLint *attrib_list) |
| 752 | { |
| 753 | struct dri2_egl_context *dri2_ctx; |
| 754 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 755 | struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list); |
| 756 | struct dri2_egl_config *dri2_config = dri2_egl_config(conf); |
| 757 | |
| 758 | dri2_ctx = malloc(sizeof *dri2_ctx); |
| 759 | if (!dri2_ctx) { |
| 760 | _eglError(EGL_BAD_ALLOC, "eglCreateContext"); |
| 761 | return NULL; |
| 762 | } |
| 763 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 764 | if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list)) |
| 765 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 766 | |
| 767 | dri2_ctx->dri_context = |
| 768 | dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen, |
| 769 | dri2_config->dri_config, |
| 770 | dri2_ctx_shared ? |
| 771 | dri2_ctx_shared->dri_context : NULL, |
| 772 | dri2_ctx); |
| 773 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 774 | if (!dri2_ctx->dri_context) |
| 775 | goto cleanup; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 776 | |
| 777 | return &dri2_ctx->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 778 | |
| 779 | cleanup: |
| 780 | free(dri2_ctx); |
| 781 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static EGLBoolean |
| 785 | dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
| 786 | { |
| 787 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 788 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 789 | |
| 790 | if (_eglIsSurfaceBound(surf)) |
| 791 | return EGL_TRUE; |
| 792 | |
| 793 | (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); |
| 794 | |
| 795 | xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable); |
| 796 | |
| 797 | if (surf->Type == EGL_PBUFFER_BIT) |
| 798 | xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable); |
| 799 | |
| 800 | free(surf); |
| 801 | |
| 802 | return EGL_TRUE; |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Called via eglMakeCurrent(), drv->API.MakeCurrent(). |
| 807 | */ |
| 808 | static EGLBoolean |
| 809 | dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf, |
| 810 | _EGLSurface *rsurf, _EGLContext *ctx) |
| 811 | { |
| 812 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 813 | struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf); |
| 814 | struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf); |
| 815 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 816 | __DRIdrawable *ddraw, *rdraw; |
| 817 | __DRIcontext *cctx; |
| 818 | |
| 819 | /* bind the new context and return the "orphaned" one */ |
| 820 | if (!_eglBindContext(&ctx, &dsurf, &rsurf)) |
| 821 | return EGL_FALSE; |
| 822 | |
| 823 | ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL; |
| 824 | rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL; |
| 825 | cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL; |
| 826 | |
| 827 | if ((cctx == NULL && ddraw == NULL && rdraw == NULL) || |
| 828 | dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) { |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 829 | if (dsurf && !_eglIsSurfaceLinked(dsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 830 | dri2_destroy_surface(drv, disp, dsurf); |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 831 | if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 832 | dri2_destroy_surface(drv, disp, rsurf); |
Chia-I Wu | fc5ca85 | 2010-02-05 11:27:55 +0800 | [diff] [blame] | 833 | if (ctx != NULL && !_eglIsContextLinked(ctx)) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 834 | dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context); |
| 835 | |
| 836 | return EGL_TRUE; |
| 837 | } else { |
| 838 | _eglBindContext(&ctx, &dsurf, &rsurf); |
| 839 | |
| 840 | return EGL_FALSE; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 846 | */ |
| 847 | static _EGLSurface * |
| 848 | dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
| 849 | _EGLConfig *conf, EGLNativeWindowType window, |
| 850 | const EGLint *attrib_list) |
| 851 | { |
| 852 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 853 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
| 854 | struct dri2_egl_surface *dri2_surf; |
| 855 | xcb_get_geometry_cookie_t cookie; |
| 856 | xcb_get_geometry_reply_t *reply; |
| 857 | xcb_screen_iterator_t s; |
| 858 | xcb_generic_error_t *error; |
| 859 | |
| 860 | dri2_surf = malloc(sizeof *dri2_surf); |
| 861 | if (!dri2_surf) { |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 862 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 863 | return NULL; |
| 864 | } |
| 865 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 866 | if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) |
| 867 | goto cleanup_surf; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 868 | |
| 869 | dri2_surf->region = XCB_NONE; |
| 870 | if (type == EGL_PBUFFER_BIT) { |
| 871 | dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn); |
| 872 | s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn)); |
| 873 | xcb_create_pixmap(dri2_dpy->conn, |
| 874 | _eglGetConfigKey(conf, EGL_BUFFER_SIZE), |
| 875 | dri2_surf->drawable, s.data->root, |
| 876 | dri2_surf->base.Width, dri2_surf->base.Height); |
| 877 | } else { |
| 878 | dri2_surf->drawable = window; |
| 879 | } |
| 880 | |
| 881 | dri2_surf->dri_drawable = |
| 882 | (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen, |
| 883 | dri2_conf->dri_config, dri2_surf); |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 884 | if (dri2_surf->dri_drawable == NULL) { |
| 885 | _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable"); |
| 886 | goto cleanup_pixmap; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable); |
| 890 | |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 891 | if (type != EGL_PBUFFER_BIT) { |
| 892 | cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable); |
| 893 | reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error); |
| 894 | if (reply == NULL || error != NULL) { |
| 895 | _eglError(EGL_BAD_ALLOC, "xcb_get_geometry"); |
| 896 | free(error); |
| 897 | goto cleanup_dri_drawable; |
| 898 | } |
| 899 | |
| 900 | dri2_surf->base.Width = reply->width; |
| 901 | dri2_surf->base.Height = reply->height; |
| 902 | free(reply); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 903 | } |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 904 | |
| 905 | return &dri2_surf->base; |
Kristian Høgsberg | 9c94fae | 2010-02-05 10:49:33 -0500 | [diff] [blame] | 906 | |
| 907 | cleanup_dri_drawable: |
| 908 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
| 909 | cleanup_pixmap: |
| 910 | if (type == EGL_PBUFFER_BIT) |
| 911 | xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable); |
| 912 | cleanup_surf: |
| 913 | free(dri2_surf); |
| 914 | |
| 915 | return NULL; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 920 | */ |
| 921 | static _EGLSurface * |
| 922 | dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 923 | _EGLConfig *conf, EGLNativeWindowType window, |
| 924 | const EGLint *attrib_list) |
| 925 | { |
| 926 | return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
| 927 | window, attrib_list); |
| 928 | } |
| 929 | |
| 930 | static _EGLSurface * |
| 931 | dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 932 | _EGLConfig *conf, EGLNativePixmapType pixmap, |
| 933 | const EGLint *attrib_list) |
| 934 | { |
| 935 | return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf, |
| 936 | pixmap, attrib_list); |
| 937 | } |
| 938 | |
| 939 | static _EGLSurface * |
| 940 | dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 941 | _EGLConfig *conf, const EGLint *attrib_list) |
| 942 | { |
| 943 | return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf, |
| 944 | XCB_WINDOW_NONE, attrib_list); |
| 945 | } |
| 946 | |
| 947 | static EGLBoolean |
| 948 | dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
| 949 | { |
| 950 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 951 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 952 | xcb_dri2_copy_region_cookie_t cookie; |
| 953 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 954 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 955 | |
| 956 | #if 0 |
| 957 | /* FIXME: Add support for dri swapbuffers, that'll give us swap |
| 958 | * interval and page flipping (at least for fullscreen windows) as |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 959 | * well as the page flip event. Unless surface->SwapBehavior is |
| 960 | * EGL_BUFFER_PRESERVED. */ |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 961 | #if __DRI2_FLUSH_VERSION >= 2 |
| 962 | if (pdraw->psc->f) |
| 963 | (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable); |
| 964 | #endif |
| 965 | #endif |
| 966 | |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 967 | if (!dri2_surf->have_fake_front) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 968 | return EGL_TRUE; |
| 969 | |
| 970 | cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn, |
| 971 | dri2_surf->drawable, |
| 972 | dri2_surf->region, |
| 973 | XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT, |
Kristian Høgsberg | c3c1a7e | 2010-02-09 20:49:40 -0500 | [diff] [blame] | 974 | XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 975 | free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL)); |
| 976 | |
| 977 | return EGL_TRUE; |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Called from eglGetProcAddress() via drv->API.GetProcAddress(). |
| 982 | */ |
| 983 | static _EGLProc |
| 984 | dri2_get_proc_address(_EGLDriver *drv, const char *procname) |
| 985 | { |
| 986 | /* FIXME: Do we need to support lookup of EGL symbols too? */ |
| 987 | |
| 988 | return (_EGLProc) _glapi_get_proc_address(procname); |
| 989 | } |
| 990 | |
| 991 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 992 | dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 993 | { |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 994 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 995 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface); |
| 996 | |
| 997 | /* FIXME: If EGL allows frontbuffer rendering for window surfaces, |
| 998 | * we need to copy fake to real here.*/ |
| 999 | |
| 1000 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1001 | |
| 1002 | return EGL_TRUE; |
| 1003 | } |
| 1004 | |
| 1005 | static EGLBoolean |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1006 | dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine) |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1007 | { |
| 1008 | if (engine != EGL_CORE_NATIVE_ENGINE) |
| 1009 | return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); |
| 1010 | /* glXWaitX(); */ |
| 1011 | |
| 1012 | return EGL_TRUE; |
| 1013 | } |
| 1014 | |
| 1015 | static void |
| 1016 | dri2_unload(_EGLDriver *drv) |
| 1017 | { |
| 1018 | struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv); |
| 1019 | free(dri2_drv); |
| 1020 | } |
| 1021 | |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1022 | static EGLBoolean |
| 1023 | dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, |
| 1024 | EGLNativePixmapType target) |
| 1025 | { |
| 1026 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1027 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1028 | xcb_gcontext_t gc; |
| 1029 | |
| 1030 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
| 1031 | |
| 1032 | gc = xcb_generate_id(dri2_dpy->conn); |
| 1033 | xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL); |
| 1034 | xcb_copy_area(dri2_dpy->conn, |
| 1035 | dri2_surf->drawable, |
| 1036 | target, |
| 1037 | gc, |
| 1038 | 0, 0, |
| 1039 | 0, 0, |
| 1040 | dri2_surf->base.Width, |
| 1041 | dri2_surf->base.Height); |
| 1042 | xcb_free_gc(dri2_dpy->conn, gc); |
| 1043 | |
| 1044 | return EGL_TRUE; |
| 1045 | } |
| 1046 | |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1047 | static EGLBoolean |
| 1048 | dri2_bind_tex_image(_EGLDriver *drv, |
| 1049 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1050 | { |
| 1051 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1052 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 1053 | struct dri2_egl_context *dri2_ctx; |
| 1054 | _EGLContext *ctx; |
| 1055 | GLint format, target; |
| 1056 | |
| 1057 | ctx = _eglGetCurrentContext(); |
| 1058 | dri2_ctx = dri2_egl_context(ctx); |
| 1059 | |
| 1060 | if (buffer != EGL_BACK_BUFFER) { |
| 1061 | _eglError(EGL_BAD_PARAMETER, "eglBindTexImage"); |
| 1062 | return EGL_FALSE; |
| 1063 | } |
| 1064 | |
| 1065 | /* We allow binding pixmaps too... Not conformat, but we can do it |
| 1066 | * for free and it's useful for X compositors. Supposedly there's |
| 1067 | * a EGL_NOKIA_texture_from_pixmap extension that allows that, but |
| 1068 | * I couldn't find it at this time. */ |
Kristian Høgsberg | c19dab7 | 2010-02-09 21:01:04 -0500 | [diff] [blame] | 1069 | if ((dri2_surf->base.Type & (EGL_PBUFFER_BIT | EGL_PIXMAP_BIT)) == 0) { |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1070 | _eglError(EGL_BAD_SURFACE, "eglBindTexImage"); |
| 1071 | return EGL_FALSE; |
| 1072 | } |
| 1073 | |
| 1074 | switch (dri2_surf->base.TextureFormat) { |
| 1075 | case EGL_TEXTURE_RGB: |
| 1076 | format = __DRI_TEXTURE_FORMAT_RGB; |
| 1077 | break; |
| 1078 | case EGL_TEXTURE_RGBA: |
| 1079 | format = __DRI_TEXTURE_FORMAT_RGBA; |
| 1080 | break; |
| 1081 | default: |
| 1082 | _eglError(EGL_BAD_MATCH, "eglBindTexImage"); |
| 1083 | return EGL_FALSE; |
| 1084 | } |
| 1085 | |
| 1086 | switch (dri2_surf->base.TextureTarget) { |
| 1087 | case EGL_TEXTURE_2D: |
| 1088 | target = GL_TEXTURE_2D; |
| 1089 | break; |
| 1090 | default: |
| 1091 | _eglError(EGL_BAD_PARAMETER, "eglBindTexImage"); |
| 1092 | return EGL_FALSE; |
| 1093 | } |
| 1094 | |
| 1095 | (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context, |
| 1096 | target, format, |
| 1097 | dri2_surf->dri_drawable); |
| 1098 | |
| 1099 | return dri2_surf->base.BoundToTexture = EGL_TRUE; |
| 1100 | } |
| 1101 | |
| 1102 | static EGLBoolean |
| 1103 | dri2_release_tex_image(_EGLDriver *drv, |
| 1104 | _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer) |
| 1105 | { |
| 1106 | return EGL_TRUE; |
| 1107 | } |
| 1108 | |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 1109 | static _EGLImage * |
| 1110 | dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp, |
| 1111 | _EGLContext *ctx, EGLenum target, |
| 1112 | EGLClientBuffer buffer, const EGLint *attr_list) |
| 1113 | { |
| 1114 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1115 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1116 | struct dri2_egl_image *dri2_img; |
| 1117 | unsigned int attachments[1]; |
| 1118 | xcb_drawable_t drawable; |
| 1119 | xcb_dri2_get_buffers_cookie_t buffers_cookie; |
| 1120 | xcb_dri2_get_buffers_reply_t *buffers_reply; |
| 1121 | xcb_dri2_dri2_buffer_t *buffers; |
| 1122 | xcb_get_geometry_cookie_t geometry_cookie; |
| 1123 | xcb_get_geometry_reply_t *geometry_reply; |
| 1124 | xcb_generic_error_t *error; |
| 1125 | |
| 1126 | dri2_img = malloc(sizeof *dri2_img); |
| 1127 | if (!dri2_img) { |
| 1128 | _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr"); |
| 1129 | return EGL_NO_IMAGE_KHR; |
| 1130 | } |
| 1131 | |
| 1132 | if (!_eglInitImage(&dri2_img->base, disp, attr_list)) |
| 1133 | return EGL_NO_IMAGE_KHR; |
| 1134 | |
| 1135 | switch (target) { |
| 1136 | case EGL_NATIVE_PIXMAP_KHR: |
| 1137 | drawable = (xcb_drawable_t) buffer; |
| 1138 | xcb_dri2_create_drawable (dri2_dpy->conn, drawable); |
| 1139 | attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT; |
| 1140 | buffers_cookie = |
| 1141 | xcb_dri2_get_buffers_unchecked (dri2_dpy->conn, |
| 1142 | drawable, 1, 1, attachments); |
| 1143 | geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable); |
| 1144 | buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, |
| 1145 | buffers_cookie, NULL); |
| 1146 | buffers = xcb_dri2_get_buffers_buffers (buffers_reply); |
| 1147 | if (buffers == NULL) { |
| 1148 | free(dri2_img); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
| 1152 | geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn, |
| 1153 | geometry_cookie, &error); |
| 1154 | if (geometry_reply == NULL || error != NULL) { |
| 1155 | _eglError(EGL_BAD_ALLOC, "xcb_get_geometry"); |
| 1156 | free(error); |
| 1157 | free(dri2_img); |
| 1158 | free(buffers_reply); |
| 1159 | } |
| 1160 | |
| 1161 | dri2_img->width = buffers_reply->width; |
| 1162 | dri2_img->height = buffers_reply->height; |
| 1163 | dri2_img->name = buffers[0].name; |
| 1164 | dri2_img->pitch = buffers[0].pitch / buffers[0].cpp; |
| 1165 | dri2_img->cpp = buffers[0].cpp; |
| 1166 | switch (geometry_reply->depth) { |
| 1167 | case 16: |
| 1168 | dri2_img->format = __DRI_IMAGE_FORMAT_RGB565; |
| 1169 | break; |
| 1170 | case 24: |
| 1171 | dri2_img->format = __DRI_IMAGE_FORMAT_XRGB8888; |
| 1172 | break; |
| 1173 | case 32: |
| 1174 | dri2_img->format = __DRI_IMAGE_FORMAT_ARGB8888; |
| 1175 | break; |
| 1176 | } |
| 1177 | free(buffers_reply); |
| 1178 | free(geometry_reply); |
| 1179 | break; |
| 1180 | |
| 1181 | default: |
| 1182 | _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr"); |
| 1183 | free(dri2_img); |
| 1184 | return EGL_NO_IMAGE_KHR; |
| 1185 | } |
| 1186 | |
| 1187 | dri2_img->dri_image = |
| 1188 | dri2_dpy->image->createImageFromName(dri2_ctx->dri_context, |
| 1189 | dri2_img->width, |
| 1190 | dri2_img->height, |
| 1191 | dri2_img->format, |
| 1192 | dri2_img->name, |
| 1193 | dri2_img->pitch, |
| 1194 | dri2_img); |
| 1195 | |
| 1196 | return &dri2_img->base; |
| 1197 | } |
| 1198 | |
| 1199 | static EGLBoolean |
| 1200 | dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image) |
| 1201 | { |
| 1202 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1203 | struct dri2_egl_image *dri2_img = dri2_egl_image(image); |
| 1204 | |
| 1205 | dri2_dpy->image->destroyImage(dri2_img->dri_image); |
| 1206 | free(dri2_img); |
| 1207 | |
| 1208 | return EGL_TRUE; |
| 1209 | } |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1210 | |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1211 | /** |
| 1212 | * This is the main entrypoint into the driver, called by libEGL. |
| 1213 | * Create a new _EGLDriver object and init its dispatch table. |
| 1214 | */ |
| 1215 | _EGLDriver * |
| 1216 | _eglMain(const char *args) |
| 1217 | { |
| 1218 | struct dri2_egl_driver *dri2_drv; |
| 1219 | |
| 1220 | dri2_drv = malloc(sizeof *dri2_drv); |
| 1221 | if (!dri2_drv) |
| 1222 | return NULL; |
| 1223 | |
| 1224 | _eglInitDriverFallbacks(&dri2_drv->base); |
| 1225 | dri2_drv->base.API.Initialize = dri2_initialize; |
| 1226 | dri2_drv->base.API.Terminate = dri2_terminate; |
| 1227 | dri2_drv->base.API.CreateContext = dri2_create_context; |
| 1228 | dri2_drv->base.API.MakeCurrent = dri2_make_current; |
| 1229 | dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface; |
| 1230 | dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface; |
| 1231 | dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface; |
| 1232 | dri2_drv->base.API.DestroySurface = dri2_destroy_surface; |
| 1233 | dri2_drv->base.API.SwapBuffers = dri2_swap_buffers; |
| 1234 | dri2_drv->base.API.GetProcAddress = dri2_get_proc_address; |
| 1235 | dri2_drv->base.API.WaitClient = dri2_wait_client; |
| 1236 | dri2_drv->base.API.WaitNative = dri2_wait_native; |
Kristian Høgsberg | c6e830c | 2010-02-04 21:49:44 -0500 | [diff] [blame] | 1237 | dri2_drv->base.API.CopyBuffers = dri2_copy_buffers; |
Kristian Høgsberg | bba5a98 | 2010-02-09 15:49:28 -0500 | [diff] [blame] | 1238 | dri2_drv->base.API.BindTexImage = dri2_bind_tex_image; |
| 1239 | dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image; |
Kristian Høgsberg | f4e7d1b | 2010-02-11 19:28:26 -0500 | [diff] [blame^] | 1240 | dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr; |
| 1241 | dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr; |
Kristian Høgsberg | 42fa009 | 2010-02-03 10:18:28 -0500 | [diff] [blame] | 1242 | |
| 1243 | dri2_drv->base.Name = "DRI2"; |
| 1244 | dri2_drv->base.Unload = dri2_unload; |
| 1245 | |
| 1246 | return &dri2_drv->base; |
| 1247 | } |