Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 2 | * Copyright © 2011-2012 Intel Corporation |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 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 | * Benjamin Franzke <benjaminfranzke@googlemail.com> |
| 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <limits.h> |
| 32 | #include <dlfcn.h> |
| 33 | #include <errno.h> |
| 34 | #include <unistd.h> |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <xf86drm.h> |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 37 | |
| 38 | #include "egl_dri2.h" |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 39 | #include "egl_dri2_fallbacks.h" |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 40 | #include "loader.h" |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 41 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 42 | #include <wayland-client.h> |
| 43 | #include "wayland-drm-client-protocol.h" |
| 44 | |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 45 | enum wl_drm_format_flags { |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 46 | HAS_ARGB8888 = 1, |
Singh, Satyeshwar | 2efc97d | 2013-10-16 01:10:12 +0000 | [diff] [blame] | 47 | HAS_XRGB8888 = 2, |
| 48 | HAS_RGB565 = 4, |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Chad Versace | a218765 | 2014-01-27 16:42:10 -0800 | [diff] [blame] | 51 | static EGLBoolean |
| 52 | dri2_wl_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, |
| 53 | EGLint interval); |
| 54 | |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 55 | static void |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 56 | sync_callback(void *data, struct wl_callback *callback, uint32_t serial) |
| 57 | { |
| 58 | int *done = data; |
| 59 | |
| 60 | *done = 1; |
| 61 | wl_callback_destroy(callback); |
| 62 | } |
| 63 | |
| 64 | static const struct wl_callback_listener sync_listener = { |
| 65 | sync_callback |
| 66 | }; |
| 67 | |
| 68 | static int |
| 69 | roundtrip(struct dri2_egl_display *dri2_dpy) |
| 70 | { |
| 71 | struct wl_callback *callback; |
| 72 | int done = 0, ret = 0; |
| 73 | |
| 74 | callback = wl_display_sync(dri2_dpy->wl_dpy); |
| 75 | wl_callback_add_listener(callback, &sync_listener, &done); |
| 76 | wl_proxy_set_queue((struct wl_proxy *) callback, dri2_dpy->wl_queue); |
| 77 | while (ret != -1 && !done) |
| 78 | ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue); |
| 79 | |
Jonas Ådahl | 800ed95 | 2012-12-25 13:01:08 +0100 | [diff] [blame] | 80 | if (!done) |
| 81 | wl_callback_destroy(callback); |
| 82 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static void |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 87 | wl_buffer_release(void *data, struct wl_buffer *buffer) |
| 88 | { |
| 89 | struct dri2_egl_surface *dri2_surf = data; |
| 90 | int i; |
| 91 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 92 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i) |
| 93 | if (dri2_surf->color_buffers[i].wl_buffer == buffer) |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 94 | break; |
| 95 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 96 | if (i == ARRAY_SIZE(dri2_surf->color_buffers)) { |
| 97 | wl_buffer_destroy(buffer); |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 98 | return; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 99 | } |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 100 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 101 | dri2_surf->color_buffers[i].locked = 0; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static struct wl_buffer_listener wl_buffer_listener = { |
| 105 | wl_buffer_release |
| 106 | }; |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 107 | |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 108 | static void |
| 109 | resize_callback(struct wl_egl_window *wl_win, void *data) |
| 110 | { |
| 111 | struct dri2_egl_surface *dri2_surf = data; |
| 112 | struct dri2_egl_display *dri2_dpy = |
| 113 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 114 | |
| 115 | (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable); |
| 116 | } |
| 117 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 118 | /** |
| 119 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 120 | */ |
| 121 | static _EGLSurface * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 122 | dri2_wl_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 123 | _EGLConfig *conf, void *native_window, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 124 | const EGLint *attrib_list) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 125 | { |
| 126 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 127 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 128 | struct wl_egl_window *window = native_window; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 129 | struct dri2_egl_surface *dri2_surf; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 130 | |
| 131 | (void) drv; |
| 132 | |
| 133 | dri2_surf = malloc(sizeof *dri2_surf); |
| 134 | if (!dri2_surf) { |
| 135 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 139 | memset(dri2_surf, 0, sizeof *dri2_surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 140 | if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list)) |
| 141 | goto cleanup_surf; |
| 142 | |
Singh, Satyeshwar | 2efc97d | 2013-10-16 01:10:12 +0000 | [diff] [blame] | 143 | if (conf->RedSize == 5) |
| 144 | dri2_surf->format = WL_DRM_FORMAT_RGB565; |
| 145 | else if (conf->AlphaSize == 0) |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 146 | dri2_surf->format = WL_DRM_FORMAT_XRGB8888; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 147 | else |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 148 | dri2_surf->format = WL_DRM_FORMAT_ARGB8888; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 149 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 150 | switch (type) { |
| 151 | case EGL_WINDOW_BIT: |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 152 | dri2_surf->wl_win = window; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 153 | |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 154 | dri2_surf->wl_win->private = dri2_surf; |
| 155 | dri2_surf->wl_win->resize_callback = resize_callback; |
| 156 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 157 | dri2_surf->base.Width = -1; |
| 158 | dri2_surf->base.Height = -1; |
| 159 | break; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 160 | default: |
| 161 | goto cleanup_surf; |
| 162 | } |
| 163 | |
| 164 | dri2_surf->dri_drawable = |
| 165 | (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen, |
Benjamin Franzke | 87dde5b | 2011-02-09 15:30:20 +0100 | [diff] [blame] | 166 | type == EGL_WINDOW_BIT ? |
| 167 | dri2_conf->dri_double_config : |
| 168 | dri2_conf->dri_single_config, |
| 169 | dri2_surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 170 | if (dri2_surf->dri_drawable == NULL) { |
| 171 | _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable"); |
| 172 | goto cleanup_dri_drawable; |
| 173 | } |
| 174 | |
| 175 | return &dri2_surf->base; |
| 176 | |
| 177 | cleanup_dri_drawable: |
| 178 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
| 179 | cleanup_surf: |
| 180 | free(dri2_surf); |
| 181 | |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface(). |
| 187 | */ |
| 188 | static _EGLSurface * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 189 | dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 190 | _EGLConfig *conf, void *native_window, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 191 | const EGLint *attrib_list) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 192 | { |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 193 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 194 | _EGLSurface *surf; |
| 195 | |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 196 | surf = dri2_wl_create_surface(drv, disp, EGL_WINDOW_BIT, conf, |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 197 | native_window, attrib_list); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 198 | |
| 199 | if (surf != NULL) |
Chad Versace | a218765 | 2014-01-27 16:42:10 -0800 | [diff] [blame] | 200 | dri2_wl_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 201 | |
| 202 | return surf; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Chad Versace | 9a40ee1 | 2014-02-09 09:13:27 -0800 | [diff] [blame] | 205 | static _EGLSurface * |
| 206 | dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp, |
| 207 | _EGLConfig *conf, void *native_window, |
| 208 | const EGLint *attrib_list) |
| 209 | { |
| 210 | /* From the EGL_EXT_platform_wayland spec, version 3: |
| 211 | * |
| 212 | * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy> |
| 213 | * that belongs to Wayland. Any such call fails and generates |
| 214 | * EGL_BAD_PARAMETER. |
| 215 | */ |
| 216 | _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on " |
| 217 | "Wayland"); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 221 | /** |
| 222 | * Called via eglDestroySurface(), drv->API.DestroySurface(). |
| 223 | */ |
| 224 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 225 | dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 226 | { |
| 227 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 228 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
| 229 | int i; |
| 230 | |
| 231 | (void) drv; |
| 232 | |
| 233 | if (!_eglPutSurface(surf)) |
| 234 | return EGL_TRUE; |
| 235 | |
| 236 | (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable); |
| 237 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 238 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 239 | if (dri2_surf->color_buffers[i].wl_buffer) |
| 240 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 241 | if (dri2_surf->color_buffers[i].dri_image) |
| 242 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 243 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 244 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 245 | for (i = 0; i < __DRI_BUFFER_COUNT; i++) |
| 246 | if (dri2_surf->dri_buffers[i] && |
| 247 | dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 248 | dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen, |
| 249 | dri2_surf->dri_buffers[i]); |
| 250 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 251 | if (dri2_surf->throttle_callback) |
| 252 | wl_callback_destroy(dri2_surf->throttle_callback); |
Jonas Ådahl | a3b6b2d | 2012-10-28 00:50:12 +0200 | [diff] [blame] | 253 | |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 254 | if (dri2_surf->base.Type == EGL_WINDOW_BIT) { |
| 255 | dri2_surf->wl_win->private = NULL; |
| 256 | dri2_surf->wl_win->resize_callback = NULL; |
| 257 | } |
| 258 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 259 | free(surf); |
| 260 | |
| 261 | return EGL_TRUE; |
| 262 | } |
| 263 | |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 264 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 265 | dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf) |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 266 | { |
| 267 | struct dri2_egl_display *dri2_dpy = |
| 268 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 269 | int i; |
| 270 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 271 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 272 | if (dri2_surf->color_buffers[i].wl_buffer && |
| 273 | !dri2_surf->color_buffers[i].locked) |
| 274 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 275 | if (dri2_surf->color_buffers[i].dri_image) |
| 276 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 277 | |
| 278 | dri2_surf->color_buffers[i].wl_buffer = NULL; |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 279 | dri2_surf->color_buffers[i].dri_image = NULL; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 280 | dri2_surf->color_buffers[i].locked = 0; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 283 | for (i = 0; i < __DRI_BUFFER_COUNT; i++) |
| 284 | if (dri2_surf->dri_buffers[i] && |
| 285 | dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT) |
| 286 | dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen, |
| 287 | dri2_surf->dri_buffers[i]); |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 290 | static int |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 291 | get_back_bo(struct dri2_egl_surface *dri2_surf) |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 292 | { |
| 293 | struct dri2_egl_display *dri2_dpy = |
| 294 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 295 | int i; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 296 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 297 | /* We always want to throttle to some event (either a frame callback or |
| 298 | * a sync request) after the commit so that we can be sure the |
| 299 | * compositor has had a chance to handle it and send us a release event |
| 300 | * before we look for a free buffer */ |
| 301 | while (dri2_surf->throttle_callback != NULL) |
| 302 | if (wl_display_dispatch_queue(dri2_dpy->wl_dpy, |
| 303 | dri2_dpy->wl_queue) == -1) |
Kristian Høgsberg | 4ed055b | 2013-12-09 16:13:35 -0800 | [diff] [blame] | 304 | return -1; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 305 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 306 | if (dri2_surf->back == NULL) { |
| 307 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 308 | /* Get an unlocked buffer, preferrably one with a dri_buffer |
| 309 | * already allocated. */ |
| 310 | if (dri2_surf->color_buffers[i].locked) |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 311 | continue; |
| 312 | if (dri2_surf->back == NULL) |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 313 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 314 | else if (dri2_surf->back->dri_image == NULL) |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 315 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 316 | } |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 317 | } |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 318 | |
| 319 | if (dri2_surf->back == NULL) |
| 320 | return -1; |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 321 | if (dri2_surf->back->dri_image == NULL) { |
| 322 | dri2_surf->back->dri_image = |
| 323 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
| 324 | dri2_surf->base.Width, |
| 325 | dri2_surf->base.Height, |
| 326 | __DRI_IMAGE_FORMAT_ARGB8888, |
| 327 | __DRI_IMAGE_USE_SHARE, |
| 328 | NULL); |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 329 | dri2_surf->back->age = 0; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 330 | } |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 331 | if (dri2_surf->back->dri_image == NULL) |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 332 | return -1; |
| 333 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 334 | dri2_surf->back->locked = 1; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | static void |
| 341 | back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer) |
| 342 | { |
| 343 | struct dri2_egl_display *dri2_dpy = |
| 344 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 345 | __DRIimage *image; |
| 346 | int name, pitch; |
| 347 | |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 348 | image = dri2_surf->back->dri_image; |
| 349 | |
| 350 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 351 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch); |
| 352 | |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 353 | buffer->attachment = __DRI_BUFFER_BACK_LEFT; |
| 354 | buffer->name = name; |
| 355 | buffer->pitch = pitch; |
| 356 | buffer->cpp = 4; |
| 357 | buffer->flags = 0; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int |
| 361 | get_aux_bo(struct dri2_egl_surface *dri2_surf, |
| 362 | unsigned int attachment, unsigned int format, __DRIbuffer *buffer) |
| 363 | { |
| 364 | struct dri2_egl_display *dri2_dpy = |
| 365 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 366 | __DRIbuffer *b = dri2_surf->dri_buffers[attachment]; |
| 367 | |
| 368 | if (b == NULL) { |
| 369 | b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, |
| 370 | attachment, format, |
| 371 | dri2_surf->base.Width, |
| 372 | dri2_surf->base.Height); |
| 373 | dri2_surf->dri_buffers[attachment] = b; |
| 374 | } |
| 375 | if (b == NULL) |
| 376 | return -1; |
| 377 | |
| 378 | memcpy(buffer, b, sizeof *buffer); |
| 379 | |
| 380 | return 0; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 383 | static int |
| 384 | update_buffers(struct dri2_egl_surface *dri2_surf) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 385 | { |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 386 | struct dri2_egl_display *dri2_dpy = |
| 387 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 388 | int i; |
Ander Conselvan de Oliveira | 60a11e2 | 2012-11-22 15:34:49 +0200 | [diff] [blame] | 389 | |
Benjamin Franzke | f811c1e | 2011-08-31 11:40:50 +0200 | [diff] [blame] | 390 | if (dri2_surf->base.Type == EGL_WINDOW_BIT && |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 391 | (dri2_surf->base.Width != dri2_surf->wl_win->width || |
| 392 | dri2_surf->base.Height != dri2_surf->wl_win->height)) { |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 393 | |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 394 | dri2_wl_release_buffers(dri2_surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 395 | |
| 396 | dri2_surf->base.Width = dri2_surf->wl_win->width; |
| 397 | dri2_surf->base.Height = dri2_surf->wl_win->height; |
| 398 | dri2_surf->dx = dri2_surf->wl_win->dx; |
| 399 | dri2_surf->dy = dri2_surf->wl_win->dy; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 400 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 401 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 402 | if (get_back_bo(dri2_surf) < 0) { |
| 403 | _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer"); |
| 404 | return -1; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 407 | /* If we have an extra unlocked buffer at this point, we had to do triple |
| 408 | * buffering for a while, but now can go back to just double buffering. |
| 409 | * That means we can free any unlocked buffer now. */ |
| 410 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
| 411 | if (!dri2_surf->color_buffers[i].locked && |
| 412 | dri2_surf->color_buffers[i].wl_buffer) { |
| 413 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 414 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 415 | dri2_surf->color_buffers[i].wl_buffer = NULL; |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 416 | dri2_surf->color_buffers[i].dri_image = NULL; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 417 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 424 | dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable, |
| 425 | int *width, int *height, |
| 426 | unsigned int *attachments, int count, |
| 427 | int *out_count, void *loaderPrivate) |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 428 | { |
| 429 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 430 | int i, j; |
| 431 | |
| 432 | if (update_buffers(dri2_surf) < 0) |
| 433 | return NULL; |
| 434 | |
| 435 | for (i = 0, j = 0; i < 2 * count; i += 2, j++) { |
| 436 | switch (attachments[i]) { |
| 437 | case __DRI_BUFFER_BACK_LEFT: |
| 438 | back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]); |
| 439 | break; |
| 440 | default: |
| 441 | if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1], |
| 442 | &dri2_surf->buffers[j]) < 0) { |
| 443 | _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer"); |
| 444 | return NULL; |
| 445 | } |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 450 | *out_count = j; |
| 451 | if (j == 0) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 452 | return NULL; |
| 453 | |
| 454 | *width = dri2_surf->base.Width; |
| 455 | *height = dri2_surf->base.Height; |
| 456 | |
| 457 | return dri2_surf->buffers; |
| 458 | } |
| 459 | |
| 460 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 461 | dri2_wl_get_buffers(__DRIdrawable * driDrawable, |
| 462 | int *width, int *height, |
| 463 | unsigned int *attachments, int count, |
| 464 | int *out_count, void *loaderPrivate) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 465 | { |
| 466 | unsigned int *attachments_with_format; |
| 467 | __DRIbuffer *buffer; |
| 468 | const unsigned int format = 32; |
| 469 | int i; |
| 470 | |
| 471 | attachments_with_format = calloc(count * 2, sizeof(unsigned int)); |
| 472 | if (!attachments_with_format) { |
| 473 | *out_count = 0; |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | for (i = 0; i < count; ++i) { |
| 478 | attachments_with_format[2*i] = attachments[i]; |
| 479 | attachments_with_format[2*i + 1] = format; |
| 480 | } |
| 481 | |
| 482 | buffer = |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 483 | dri2_wl_get_buffers_with_format(driDrawable, |
| 484 | width, height, |
| 485 | attachments_with_format, count, |
| 486 | out_count, loaderPrivate); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 487 | |
| 488 | free(attachments_with_format); |
| 489 | |
| 490 | return buffer; |
| 491 | } |
| 492 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 493 | static int |
| 494 | image_get_buffers(__DRIdrawable *driDrawable, |
| 495 | unsigned int format, |
| 496 | uint32_t *stamp, |
| 497 | void *loaderPrivate, |
| 498 | uint32_t buffer_mask, |
| 499 | struct __DRIimageList *buffers) |
| 500 | { |
| 501 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 502 | |
| 503 | if (update_buffers(dri2_surf) < 0) |
| 504 | return 0; |
| 505 | |
| 506 | buffers->image_mask = __DRI_IMAGE_BUFFER_BACK; |
| 507 | buffers->back = dri2_surf->back->dri_image; |
| 508 | |
| 509 | return 1; |
| 510 | } |
| 511 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 512 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 513 | dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 514 | { |
| 515 | (void) driDrawable; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 516 | (void) loaderPrivate; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 519 | static const __DRIimageLoaderExtension image_loader_extension = { |
Emil Velikov | 9e627cc | 2014-02-12 18:32:59 +0000 | [diff] [blame] | 520 | .base = { __DRI_IMAGE_LOADER, 1 }, |
| 521 | |
| 522 | .getBuffers = image_get_buffers, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 523 | .flushFrontBuffer = dri2_wl_flush_front_buffer, |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 524 | }; |
| 525 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 526 | static void |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 527 | wayland_throttle_callback(void *data, |
| 528 | struct wl_callback *callback, |
| 529 | uint32_t time) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 530 | { |
| 531 | struct dri2_egl_surface *dri2_surf = data; |
| 532 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 533 | dri2_surf->throttle_callback = NULL; |
Kristian Høgsberg | 6602bda | 2011-08-16 22:38:22 -0400 | [diff] [blame] | 534 | wl_callback_destroy(callback); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 537 | static const struct wl_callback_listener throttle_listener = { |
| 538 | wayland_throttle_callback |
Kristian Høgsberg | 6602bda | 2011-08-16 22:38:22 -0400 | [diff] [blame] | 539 | }; |
| 540 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 541 | static void |
| 542 | create_wl_buffer(struct dri2_egl_surface *dri2_surf) |
| 543 | { |
| 544 | struct dri2_egl_display *dri2_dpy = |
| 545 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 546 | int fd, stride, name; |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 547 | |
| 548 | if (dri2_surf->current->wl_buffer != NULL) |
| 549 | return; |
| 550 | |
| 551 | if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) { |
| 552 | dri2_dpy->image->queryImage(dri2_surf->current->dri_image, |
| 553 | __DRI_IMAGE_ATTRIB_FD, &fd); |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 554 | dri2_dpy->image->queryImage(dri2_surf->current->dri_image, |
| 555 | __DRI_IMAGE_ATTRIB_STRIDE, &stride); |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 556 | |
| 557 | dri2_surf->current->wl_buffer = |
| 558 | wl_drm_create_prime_buffer(dri2_dpy->wl_drm, |
| 559 | fd, |
| 560 | dri2_surf->base.Width, |
| 561 | dri2_surf->base.Height, |
| 562 | dri2_surf->format, |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 563 | 0, stride, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 564 | 0, 0, |
| 565 | 0, 0); |
| 566 | close(fd); |
| 567 | } else { |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 568 | dri2_dpy->image->queryImage(dri2_surf->current->dri_image, |
| 569 | __DRI_IMAGE_ATTRIB_NAME, &name); |
| 570 | dri2_dpy->image->queryImage(dri2_surf->current->dri_image, |
| 571 | __DRI_IMAGE_ATTRIB_STRIDE, &stride); |
| 572 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 573 | dri2_surf->current->wl_buffer = |
| 574 | wl_drm_create_buffer(dri2_dpy->wl_drm, |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 575 | name, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 576 | dri2_surf->base.Width, |
| 577 | dri2_surf->base.Height, |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 578 | stride, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 579 | dri2_surf->format); |
| 580 | } |
| 581 | |
| 582 | wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer, |
| 583 | dri2_dpy->wl_queue); |
| 584 | wl_buffer_add_listener(dri2_surf->current->wl_buffer, |
| 585 | &wl_buffer_listener, dri2_surf); |
| 586 | } |
| 587 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 588 | /** |
| 589 | * Called via eglSwapBuffers(), drv->API.SwapBuffers(). |
| 590 | */ |
| 591 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 592 | dri2_wl_swap_buffers_with_damage(_EGLDriver *drv, |
| 593 | _EGLDisplay *disp, |
| 594 | _EGLSurface *draw, |
| 595 | const EGLint *rects, |
| 596 | EGLint n_rects) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 597 | { |
| 598 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 599 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Axel Davy | afcce46 | 2013-12-03 17:04:10 +0100 | [diff] [blame] | 600 | struct dri2_egl_context *dri2_ctx; |
| 601 | _EGLContext *ctx; |
Neil Roberts | 25cc889 | 2013-11-15 13:50:49 +0000 | [diff] [blame] | 602 | int i; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 603 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 604 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
| 605 | if (dri2_surf->color_buffers[i].age > 0) |
| 606 | dri2_surf->color_buffers[i].age++; |
| 607 | |
Kristian Høgsberg | 1fe0073 | 2013-02-06 15:41:54 -0500 | [diff] [blame] | 608 | /* Make sure we have a back buffer in case we're swapping without ever |
| 609 | * rendering. */ |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 610 | if (get_back_bo(dri2_surf) < 0) { |
Kristian Høgsberg | 1fe0073 | 2013-02-06 15:41:54 -0500 | [diff] [blame] | 611 | _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers"); |
| 612 | return EGL_FALSE; |
| 613 | } |
| 614 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 615 | if (draw->SwapInterval > 0) { |
| 616 | dri2_surf->throttle_callback = |
| 617 | wl_surface_frame(dri2_surf->wl_win->surface); |
| 618 | wl_callback_add_listener(dri2_surf->throttle_callback, |
| 619 | &throttle_listener, dri2_surf); |
| 620 | wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback, |
| 621 | dri2_dpy->wl_queue); |
| 622 | } |
Neil Roberts | 25cc889 | 2013-11-15 13:50:49 +0000 | [diff] [blame] | 623 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 624 | dri2_surf->back->age = 1; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 625 | dri2_surf->current = dri2_surf->back; |
| 626 | dri2_surf->back = NULL; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 627 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 628 | create_wl_buffer(dri2_surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 629 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 630 | wl_surface_attach(dri2_surf->wl_win->surface, |
| 631 | dri2_surf->current->wl_buffer, |
| 632 | dri2_surf->dx, dri2_surf->dy); |
| 633 | |
| 634 | dri2_surf->wl_win->attached_width = dri2_surf->base.Width; |
| 635 | dri2_surf->wl_win->attached_height = dri2_surf->base.Height; |
| 636 | /* reset resize growing parameters */ |
| 637 | dri2_surf->dx = 0; |
| 638 | dri2_surf->dy = 0; |
| 639 | |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 640 | if (n_rects == 0) { |
Kristian Høgsberg | bce64c6 | 2013-12-04 16:13:35 -0800 | [diff] [blame] | 641 | wl_surface_damage(dri2_surf->wl_win->surface, |
| 642 | 0, 0, INT32_MAX, INT32_MAX); |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 643 | } else { |
| 644 | for (i = 0; i < n_rects; i++) { |
| 645 | const int *rect = &rects[i * 4]; |
| 646 | wl_surface_damage(dri2_surf->wl_win->surface, |
| 647 | rect[0], |
| 648 | dri2_surf->base.Height - rect[1] - rect[3], |
| 649 | rect[2], rect[3]); |
| 650 | } |
| 651 | } |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 652 | |
Axel Davy | afcce46 | 2013-12-03 17:04:10 +0100 | [diff] [blame] | 653 | if (dri2_dpy->flush->base.version >= 4) { |
| 654 | ctx = _eglGetCurrentContext(); |
| 655 | dri2_ctx = dri2_egl_context(ctx); |
| 656 | (*dri2_dpy->flush->flush_with_flags)(dri2_ctx->dri_context, |
| 657 | dri2_surf->dri_drawable, |
| 658 | __DRI2_FLUSH_DRAWABLE, |
| 659 | __DRI2_THROTTLE_SWAPBUFFER); |
| 660 | } else { |
| 661 | (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable); |
| 662 | } |
| 663 | |
Kristian Høgsberg | 89ba436 | 2012-11-30 13:29:17 -0500 | [diff] [blame] | 664 | (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 665 | |
Kristian Høgsberg | 33eb5ea | 2013-12-04 12:08:35 -0800 | [diff] [blame] | 666 | wl_surface_commit(dri2_surf->wl_win->surface); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 667 | |
| 668 | /* If we're not waiting for a frame callback then we'll at least throttle |
| 669 | * to a sync callback so that we always give a chance for the compositor to |
| 670 | * handle the commit and send a release event before checking for a free |
| 671 | * buffer */ |
| 672 | if (dri2_surf->throttle_callback == NULL) { |
| 673 | dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy); |
| 674 | wl_callback_add_listener(dri2_surf->throttle_callback, |
| 675 | &throttle_listener, dri2_surf); |
| 676 | wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback, |
| 677 | dri2_dpy->wl_queue); |
| 678 | } |
| 679 | |
Axel Davy | 402bf6e | 2013-12-03 17:38:09 +0100 | [diff] [blame] | 680 | wl_display_flush(dri2_dpy->wl_dpy); |
| 681 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 682 | return EGL_TRUE; |
| 683 | } |
| 684 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 685 | static EGLint |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 686 | dri2_wl_query_buffer_age(_EGLDriver *drv, |
| 687 | _EGLDisplay *disp, _EGLSurface *surface) |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 688 | { |
| 689 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 690 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 691 | if (get_back_bo(dri2_surf) < 0) { |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 692 | _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age"); |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | return dri2_surf->back->age; |
| 697 | } |
| 698 | |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 699 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 700 | dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw) |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 701 | { |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 702 | return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0); |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 705 | static struct wl_buffer * |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 706 | dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv, |
| 707 | _EGLDisplay *disp, |
| 708 | _EGLImage *img) |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 709 | { |
| 710 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 711 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 712 | __DRIimage *image = dri2_img->dri_image; |
| 713 | struct wl_buffer *buffer; |
| 714 | int width, height, format, pitch; |
| 715 | enum wl_drm_format wl_format; |
| 716 | |
| 717 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format); |
| 718 | |
| 719 | switch (format) { |
| 720 | case __DRI_IMAGE_FORMAT_ARGB8888: |
| 721 | if (!(dri2_dpy->formats & HAS_ARGB8888)) |
| 722 | goto bad_format; |
| 723 | wl_format = WL_DRM_FORMAT_ARGB8888; |
| 724 | break; |
| 725 | case __DRI_IMAGE_FORMAT_XRGB8888: |
| 726 | if (!(dri2_dpy->formats & HAS_XRGB8888)) |
| 727 | goto bad_format; |
| 728 | wl_format = WL_DRM_FORMAT_XRGB8888; |
| 729 | break; |
| 730 | default: |
| 731 | goto bad_format; |
| 732 | } |
| 733 | |
| 734 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width); |
| 735 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height); |
| 736 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch); |
| 737 | |
| 738 | if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) { |
| 739 | int fd; |
| 740 | |
| 741 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd); |
| 742 | |
| 743 | buffer = |
| 744 | wl_drm_create_prime_buffer(dri2_dpy->wl_drm, |
| 745 | fd, |
| 746 | width, height, |
| 747 | wl_format, |
| 748 | 0, pitch, |
| 749 | 0, 0, |
| 750 | 0, 0); |
| 751 | |
| 752 | close(fd); |
| 753 | } else { |
| 754 | int name; |
| 755 | |
| 756 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 757 | |
| 758 | buffer = |
| 759 | wl_drm_create_buffer(dri2_dpy->wl_drm, |
| 760 | name, |
| 761 | width, height, |
| 762 | pitch, |
| 763 | wl_format); |
| 764 | } |
| 765 | |
| 766 | /* The buffer object will have been created with our internal event queue |
| 767 | * because it is using the wl_drm object as a proxy factory. We want the |
| 768 | * buffer to be used by the application so we'll reset it to the display's |
| 769 | * default event queue */ |
| 770 | if (buffer) |
| 771 | wl_proxy_set_queue((struct wl_proxy *) buffer, NULL); |
| 772 | |
| 773 | return buffer; |
| 774 | |
| 775 | bad_format: |
| 776 | _eglError(EGL_BAD_MATCH, "unsupported image format"); |
| 777 | return NULL; |
| 778 | } |
| 779 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 780 | static int |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 781 | dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id) |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 782 | { |
| 783 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 784 | int ret = 0; |
| 785 | |
Benjamin Franzke | aa87a93 | 2011-05-31 11:45:51 +0200 | [diff] [blame] | 786 | dri2_dpy->authenticated = 0; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 787 | |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 788 | wl_drm_authenticate(dri2_dpy->wl_drm, id); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 789 | if (roundtrip(dri2_dpy) < 0) |
| 790 | ret = -1; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 791 | |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 792 | if (!dri2_dpy->authenticated) |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 793 | ret = -1; |
| 794 | |
| 795 | /* reset authenticated */ |
Benjamin Franzke | aa87a93 | 2011-05-31 11:45:51 +0200 | [diff] [blame] | 796 | dri2_dpy->authenticated = 1; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 797 | |
| 798 | return ret; |
| 799 | } |
| 800 | |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 801 | static void |
| 802 | drm_handle_device(void *data, struct wl_drm *drm, const char *device) |
| 803 | { |
| 804 | struct dri2_egl_display *dri2_dpy = data; |
| 805 | drm_magic_t magic; |
| 806 | |
| 807 | dri2_dpy->device_name = strdup(device); |
| 808 | if (!dri2_dpy->device_name) |
| 809 | return; |
| 810 | |
David Fries | b601206 | 2011-12-10 11:28:45 -0600 | [diff] [blame] | 811 | #ifdef O_CLOEXEC |
| 812 | dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC); |
| 813 | if (dri2_dpy->fd == -1 && errno == EINVAL) |
| 814 | #endif |
| 815 | { |
| 816 | dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR); |
| 817 | if (dri2_dpy->fd != -1) |
| 818 | fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) | |
| 819 | FD_CLOEXEC); |
| 820 | } |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 821 | if (dri2_dpy->fd == -1) { |
| 822 | _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)", |
| 823 | dri2_dpy->device_name, strerror(errno)); |
| 824 | return; |
| 825 | } |
| 826 | |
| 827 | drmGetMagic(dri2_dpy->fd, &magic); |
| 828 | wl_drm_authenticate(dri2_dpy->wl_drm, magic); |
| 829 | } |
| 830 | |
| 831 | static void |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 832 | drm_handle_format(void *data, struct wl_drm *drm, uint32_t format) |
| 833 | { |
| 834 | struct dri2_egl_display *dri2_dpy = data; |
| 835 | |
| 836 | switch (format) { |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 837 | case WL_DRM_FORMAT_ARGB8888: |
| 838 | dri2_dpy->formats |= HAS_ARGB8888; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 839 | break; |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 840 | case WL_DRM_FORMAT_XRGB8888: |
| 841 | dri2_dpy->formats |= HAS_XRGB8888; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 842 | break; |
Singh, Satyeshwar | 2efc97d | 2013-10-16 01:10:12 +0000 | [diff] [blame] | 843 | case WL_DRM_FORMAT_RGB565: |
| 844 | dri2_dpy->formats |= HAS_RGB565; |
| 845 | break; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | static void |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 850 | drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value) |
| 851 | { |
| 852 | struct dri2_egl_display *dri2_dpy = data; |
| 853 | |
| 854 | dri2_dpy->capabilities = value; |
| 855 | } |
| 856 | |
| 857 | static void |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 858 | drm_handle_authenticated(void *data, struct wl_drm *drm) |
| 859 | { |
| 860 | struct dri2_egl_display *dri2_dpy = data; |
| 861 | |
Benjamin Franzke | aa87a93 | 2011-05-31 11:45:51 +0200 | [diff] [blame] | 862 | dri2_dpy->authenticated = 1; |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static const struct wl_drm_listener drm_listener = { |
| 866 | drm_handle_device, |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 867 | drm_handle_format, |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 868 | drm_handle_authenticated, |
| 869 | drm_handle_capabilities |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 870 | }; |
| 871 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 872 | static void |
| 873 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 874 | const char *interface, uint32_t version) |
| 875 | { |
| 876 | struct dri2_egl_display *dri2_dpy = data; |
| 877 | |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 878 | if (version > 1) |
| 879 | version = 2; |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 880 | if (strcmp(interface, "wl_drm") == 0) { |
| 881 | dri2_dpy->wl_drm = |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 882 | wl_registry_bind(registry, name, &wl_drm_interface, version); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 883 | wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy); |
| 884 | } |
| 885 | } |
| 886 | |
Kristian Høgsberg | 712269d | 2013-06-18 16:53:46 -0400 | [diff] [blame] | 887 | static void |
| 888 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 889 | uint32_t name) |
| 890 | { |
| 891 | } |
| 892 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 893 | static const struct wl_registry_listener registry_listener = { |
Kristian Høgsberg | 712269d | 2013-06-18 16:53:46 -0400 | [diff] [blame] | 894 | registry_handle_global, |
| 895 | registry_handle_global_remove |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 896 | }; |
| 897 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 898 | static EGLBoolean |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 899 | dri2_wl_swap_interval(_EGLDriver *drv, |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 900 | _EGLDisplay *disp, |
| 901 | _EGLSurface *surf, |
| 902 | EGLint interval) |
| 903 | { |
| 904 | if (interval > surf->Config->MaxSwapInterval) |
| 905 | interval = surf->Config->MaxSwapInterval; |
| 906 | else if (interval < surf->Config->MinSwapInterval) |
| 907 | interval = surf->Config->MinSwapInterval; |
| 908 | |
| 909 | surf->SwapInterval = interval; |
| 910 | |
| 911 | return EGL_TRUE; |
| 912 | } |
| 913 | |
| 914 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 915 | dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy) |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 916 | { |
| 917 | GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1; |
| 918 | |
| 919 | /* We can't use values greater than 1 on Wayland because we are using the |
| 920 | * frame callback to synchronise the frame and the only way we be sure to |
| 921 | * get a frame callback is to attach a new buffer. Therefore we can't just |
| 922 | * sit drawing nothing to wait until the next ‘n’ frame callbacks */ |
| 923 | |
| 924 | if (dri2_dpy->config) |
| 925 | dri2_dpy->config->configQueryi(dri2_dpy->dri_screen, |
| 926 | "vblank_mode", &vblank_mode); |
| 927 | switch (vblank_mode) { |
| 928 | case DRI_CONF_VBLANK_NEVER: |
| 929 | dri2_dpy->min_swap_interval = 0; |
| 930 | dri2_dpy->max_swap_interval = 0; |
| 931 | dri2_dpy->default_swap_interval = 0; |
| 932 | break; |
| 933 | case DRI_CONF_VBLANK_ALWAYS_SYNC: |
| 934 | dri2_dpy->min_swap_interval = 1; |
| 935 | dri2_dpy->max_swap_interval = 1; |
| 936 | dri2_dpy->default_swap_interval = 1; |
| 937 | break; |
| 938 | case DRI_CONF_VBLANK_DEF_INTERVAL_0: |
| 939 | dri2_dpy->min_swap_interval = 0; |
| 940 | dri2_dpy->max_swap_interval = 1; |
| 941 | dri2_dpy->default_swap_interval = 0; |
| 942 | break; |
| 943 | default: |
| 944 | case DRI_CONF_VBLANK_DEF_INTERVAL_1: |
| 945 | dri2_dpy->min_swap_interval = 0; |
| 946 | dri2_dpy->max_swap_interval = 1; |
| 947 | dri2_dpy->default_swap_interval = 1; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 952 | static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = { |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 953 | .authenticate = dri2_wl_authenticate, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 954 | .create_window_surface = dri2_wl_create_window_surface, |
Chad Versace | 9a40ee1 | 2014-02-09 09:13:27 -0800 | [diff] [blame] | 955 | .create_pixmap_surface = dri2_wl_create_pixmap_surface, |
Chad Versace | bf20076 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 956 | .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface, |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 957 | .destroy_surface = dri2_wl_destroy_surface, |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 958 | .create_image = dri2_create_image_khr, |
Chad Versace | 8b9298a | 2014-01-28 12:34:19 -0800 | [diff] [blame] | 959 | .swap_interval = dri2_wl_swap_interval, |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 960 | .swap_buffers = dri2_wl_swap_buffers, |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 961 | .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage, |
Chad Versace | 75d398e | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 962 | .swap_buffers_region = dri2_fallback_swap_buffers_region, |
Chad Versace | 688a0e8 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 963 | .post_sub_buffer = dri2_fallback_post_sub_buffer, |
Chad Versace | bc2cbc0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 964 | .copy_buffers = dri2_fallback_copy_buffers, |
Chad Versace | 3fdfbd2 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 965 | .query_buffer_age = dri2_wl_query_buffer_age, |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 966 | .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image, |
Sarah Sharp | c524f3e | 2014-05-06 12:10:57 -0700 | [diff] [blame^] | 967 | .get_sync_values = dri2_fallback_get_sync_values, |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 968 | }; |
| 969 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 970 | EGLBoolean |
| 971 | dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp) |
| 972 | { |
| 973 | struct dri2_egl_display *dri2_dpy; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 974 | const __DRIconfig *config; |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 975 | uint32_t types; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 976 | int i; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 977 | static const unsigned int argb_masks[4] = |
| 978 | { 0xff0000, 0xff00, 0xff, 0xff000000 }; |
| 979 | static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 }; |
Singh, Satyeshwar | 2efc97d | 2013-10-16 01:10:12 +0000 | [diff] [blame] | 980 | static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 }; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 981 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 982 | loader_set_logger(_eglLog); |
| 983 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 984 | dri2_dpy = calloc(1, sizeof *dri2_dpy); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 985 | if (!dri2_dpy) |
| 986 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 987 | |
| 988 | disp->DriverData = (void *) dri2_dpy; |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 989 | if (disp->PlatformDisplay == NULL) { |
| 990 | dri2_dpy->wl_dpy = wl_display_connect(NULL); |
| 991 | if (dri2_dpy->wl_dpy == NULL) |
| 992 | goto cleanup_dpy; |
Benjamin Franzke | 2a58453 | 2011-12-13 14:43:48 +0100 | [diff] [blame] | 993 | dri2_dpy->own_device = 1; |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 994 | } else { |
| 995 | dri2_dpy->wl_dpy = disp->PlatformDisplay; |
| 996 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 997 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 998 | dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy); |
Kristian Høgsberg | 112ccfa | 2013-02-26 12:49:40 -0500 | [diff] [blame] | 999 | |
| 1000 | if (dri2_dpy->own_device) |
| 1001 | wl_display_dispatch_pending(dri2_dpy->wl_dpy); |
| 1002 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1003 | dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy); |
| 1004 | wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry, |
| 1005 | dri2_dpy->wl_queue); |
| 1006 | wl_registry_add_listener(dri2_dpy->wl_registry, |
| 1007 | ®istry_listener, dri2_dpy); |
| 1008 | if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL) |
Kristian Høgsberg | 56758c8 | 2011-02-04 15:37:51 -0500 | [diff] [blame] | 1009 | goto cleanup_dpy; |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1010 | |
| 1011 | if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1) |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1012 | goto cleanup_drm; |
Kristian Høgsberg | 56758c8 | 2011-02-04 15:37:51 -0500 | [diff] [blame] | 1013 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1014 | if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated) |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1015 | goto cleanup_fd; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1016 | |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 1017 | dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1018 | if (dri2_dpy->driver_name == NULL) { |
| 1019 | _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name"); |
| 1020 | goto cleanup_fd; |
| 1021 | } |
| 1022 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1023 | if (!dri2_load_driver(disp)) |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1024 | goto cleanup_driver_name; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1025 | |
Haitao Feng | 3104e5c | 2011-02-09 15:05:11 +0800 | [diff] [blame] | 1026 | dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER; |
| 1027 | dri2_dpy->dri2_loader_extension.base.version = 3; |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 1028 | dri2_dpy->dri2_loader_extension.getBuffers = dri2_wl_get_buffers; |
| 1029 | dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_wl_flush_front_buffer; |
Haitao Feng | 3104e5c | 2011-02-09 15:05:11 +0800 | [diff] [blame] | 1030 | dri2_dpy->dri2_loader_extension.getBuffersWithFormat = |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 1031 | dri2_wl_get_buffers_with_format; |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 1032 | |
Haitao Feng | 3104e5c | 2011-02-09 15:05:11 +0800 | [diff] [blame] | 1033 | dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base; |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 1034 | dri2_dpy->extensions[1] = &image_loader_extension.base; |
| 1035 | dri2_dpy->extensions[2] = &image_lookup_extension.base; |
| 1036 | dri2_dpy->extensions[3] = &use_invalidate.base; |
| 1037 | dri2_dpy->extensions[4] = NULL; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1038 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1039 | dri2_dpy->swap_available = EGL_TRUE; |
| 1040 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1041 | if (!dri2_create_screen(disp)) |
| 1042 | goto cleanup_driver; |
| 1043 | |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 1044 | dri2_wl_setup_swap_interval(dri2_dpy); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1045 | |
Kristian Høgsberg | 1670737 | 2013-03-19 13:20:36 -0400 | [diff] [blame] | 1046 | /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver |
| 1047 | * doesn't have createImageFromFds, since we're using the same driver on |
| 1048 | * both sides. We don't want crash if that happens anyway, so fall back to |
| 1049 | * gem names if we don't have prime support. */ |
| 1050 | |
| 1051 | if (dri2_dpy->image->base.version < 7 || |
| 1052 | dri2_dpy->image->createImageFromFds == NULL) |
Neil Roberts | 63d4661 | 2014-04-08 23:28:40 +0300 | [diff] [blame] | 1053 | dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME; |
Kristian Høgsberg | 1670737 | 2013-03-19 13:20:36 -0400 | [diff] [blame] | 1054 | |
Kristian Høgsberg | e20a0f1 | 2012-10-16 14:30:53 -0400 | [diff] [blame] | 1055 | types = EGL_WINDOW_BIT; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1056 | for (i = 0; dri2_dpy->driver_configs[i]; i++) { |
| 1057 | config = dri2_dpy->driver_configs[i]; |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 1058 | if (dri2_dpy->formats & HAS_XRGB8888) |
Kristian Høgsberg | 44e584a | 2013-09-14 23:13:22 -0700 | [diff] [blame] | 1059 | dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks); |
Kristian Høgsberg | 58dc1b2 | 2012-01-11 14:23:24 -0500 | [diff] [blame] | 1060 | if (dri2_dpy->formats & HAS_ARGB8888) |
Kristian Høgsberg | 44e584a | 2013-09-14 23:13:22 -0700 | [diff] [blame] | 1061 | dri2_add_config(disp, config, i + 1, types, NULL, argb_masks); |
Singh, Satyeshwar | 2efc97d | 2013-10-16 01:10:12 +0000 | [diff] [blame] | 1062 | if (dri2_dpy->formats & HAS_RGB565) |
Kristian Høgsberg | 44e584a | 2013-09-14 23:13:22 -0700 | [diff] [blame] | 1063 | dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks); |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1064 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1065 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1066 | disp->Extensions.WL_bind_wayland_display = EGL_TRUE; |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1067 | disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE; |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1068 | disp->Extensions.EXT_buffer_age = EGL_TRUE; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1069 | |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 1070 | disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE; |
| 1071 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1072 | /* we're supporting EGL 1.4 */ |
| 1073 | disp->VersionMajor = 1; |
| 1074 | disp->VersionMinor = 4; |
| 1075 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1076 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 1077 | * initialization. |
| 1078 | */ |
| 1079 | dri2_dpy->vtbl = &dri2_wl_display_vtbl; |
| 1080 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1081 | return EGL_TRUE; |
| 1082 | |
| 1083 | cleanup_driver: |
| 1084 | dlclose(dri2_dpy->driver); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1085 | cleanup_driver_name: |
| 1086 | free(dri2_dpy->driver_name); |
| 1087 | cleanup_fd: |
| 1088 | close(dri2_dpy->fd); |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1089 | cleanup_drm: |
| 1090 | free(dri2_dpy->device_name); |
| 1091 | wl_drm_destroy(dri2_dpy->wl_drm); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1092 | cleanup_dpy: |
| 1093 | free(dri2_dpy); |
| 1094 | |
| 1095 | return EGL_FALSE; |
| 1096 | } |