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 |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Collabora, Ltd. |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
| 24 | * |
| 25 | * Authors: |
| 26 | * Kristian Høgsberg <krh@bitplanet.net> |
| 27 | * Benjamin Franzke <benjaminfranzke@googlemail.com> |
| 28 | */ |
| 29 | |
Emil Velikov | bf0e4d2 | 2015-02-28 17:12:40 +0000 | [diff] [blame] | 30 | #include <stdint.h> |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <limits.h> |
| 34 | #include <dlfcn.h> |
| 35 | #include <errno.h> |
| 36 | #include <unistd.h> |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 37 | #include <fcntl.h> |
| 38 | #include <xf86drm.h> |
Eric Engestrom | f137480 | 2019-02-12 18:18:03 +0000 | [diff] [blame] | 39 | #include "drm-uapi/drm_fourcc.h" |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 40 | #include <sys/mman.h> |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 41 | |
| 42 | #include "egl_dri2.h" |
Emil Velikov | 8d4357b | 2014-01-11 04:52:48 +0000 | [diff] [blame] | 43 | #include "loader.h" |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 44 | #include "util/u_vector.h" |
Greg V | c0376a1 | 2018-01-18 23:29:14 +0300 | [diff] [blame] | 45 | #include "util/anon_file.h" |
Miguel A. Vico | 2d5d61b | 2017-07-19 17:27:12 -0700 | [diff] [blame] | 46 | #include "eglglobals.h" |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 47 | |
Eric Engestrom | 1db4ec0 | 2018-05-29 15:41:28 +0100 | [diff] [blame] | 48 | #include <wayland-egl-backend.h> |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 49 | #include <wayland-client.h> |
| 50 | #include "wayland-drm-client-protocol.h" |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 51 | #include "linux-dmabuf-unstable-v1-client-protocol.h" |
| 52 | |
Adam Jackson | e74c947 | 2019-08-30 15:35:22 -0400 | [diff] [blame] | 53 | /* cheesy workaround until wayland 1.18 is released */ |
| 54 | #if WAYLAND_VERSION_MAJOR > 1 || \ |
| 55 | (WAYLAND_VERSION_MAJOR == 1 && WAYLAND_VERSION_MINOR < 18) |
| 56 | #define WL_SHM_FORMAT_ABGR16161616F 0x48344241 |
| 57 | #define WL_SHM_FORMAT_XBGR16161616F 0x48344258 |
| 58 | #endif |
| 59 | |
Daniel Stone | 5bc49d4 | 2018-02-06 11:14:32 +0000 | [diff] [blame] | 60 | /* |
| 61 | * The index of entries in this table is used as a bitmask in |
| 62 | * dri2_dpy->formats, which tracks the formats supported by our server. |
| 63 | */ |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 64 | static const struct dri2_wl_visual { |
Daniel Stone | 19cbca3 | 2018-02-06 09:32:22 +0000 | [diff] [blame] | 65 | const char *format_name; |
Daniel Stone | 1dc013f | 2018-02-06 09:42:27 +0000 | [diff] [blame] | 66 | uint32_t wl_drm_format; |
| 67 | uint32_t wl_shm_format; |
| 68 | int dri_image_format; |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 69 | /* alt_dri_image_format is a substitute wl_buffer format to use for a |
| 70 | * wl-server unsupported dri_image_format, ie. some other dri_image_format in |
| 71 | * the table, of the same precision but with different channel ordering, or |
| 72 | * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported. |
| 73 | * The code checks if alt_dri_image_format can be used as a fallback for a |
| 74 | * dri_image_format for a given wl-server implementation. |
| 75 | */ |
| 76 | int alt_dri_image_format; |
Daniel Stone | d32b23f | 2018-02-06 11:58:45 +0000 | [diff] [blame] | 77 | int bpp; |
Kevin Strasser | 7b4ed2b | 2019-01-24 16:32:48 -0800 | [diff] [blame] | 78 | int rgba_shifts[4]; |
| 79 | unsigned int rgba_sizes[4]; |
Daniel Stone | 19cbca3 | 2018-02-06 09:32:22 +0000 | [diff] [blame] | 80 | } dri2_wl_visuals[] = { |
Daniel Stone | 845c2f6 | 2018-02-06 18:06:52 +0000 | [diff] [blame] | 81 | { |
Adam Jackson | e74c947 | 2019-08-30 15:35:22 -0400 | [diff] [blame] | 82 | "ABGR16F", |
| 83 | WL_DRM_FORMAT_ABGR16F, WL_SHM_FORMAT_ABGR16161616F, |
| 84 | __DRI_IMAGE_FORMAT_ABGR16161616F, 0, 64, |
| 85 | { 0, 16, 32, 48 }, |
| 86 | { 16, 16, 16, 16 }, |
| 87 | }, |
| 88 | { |
| 89 | "XBGR16F", |
| 90 | WL_DRM_FORMAT_XBGR16F, WL_SHM_FORMAT_XBGR16161616F, |
| 91 | __DRI_IMAGE_FORMAT_XBGR16161616F, 0, 64, |
| 92 | { 0, 16, 32, -1 }, |
| 93 | { 16, 16, 16, 0 }, |
| 94 | }, |
| 95 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 96 | "XRGB2101010", |
| 97 | WL_DRM_FORMAT_XRGB2101010, WL_SHM_FORMAT_XRGB2101010, |
| 98 | __DRI_IMAGE_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XBGR2101010, 32, |
| 99 | { 20, 10, 0, -1 }, |
| 100 | { 10, 10, 10, 0 }, |
Daniel Stone | 845c2f6 | 2018-02-06 18:06:52 +0000 | [diff] [blame] | 101 | }, |
| 102 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 103 | "ARGB2101010", |
| 104 | WL_DRM_FORMAT_ARGB2101010, WL_SHM_FORMAT_ARGB2101010, |
| 105 | __DRI_IMAGE_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ABGR2101010, 32, |
| 106 | { 20, 10, 0, 30 }, |
| 107 | { 10, 10, 10, 2 }, |
Daniel Stone | 845c2f6 | 2018-02-06 18:06:52 +0000 | [diff] [blame] | 108 | }, |
| 109 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 110 | "XBGR2101010", |
| 111 | WL_DRM_FORMAT_XBGR2101010, WL_SHM_FORMAT_XBGR2101010, |
| 112 | __DRI_IMAGE_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XRGB2101010, 32, |
| 113 | { 0, 10, 20, -1 }, |
| 114 | { 10, 10, 10, 0 }, |
Daniel Stone | 275b23e | 2018-06-13 06:04:11 +0200 | [diff] [blame] | 115 | }, |
| 116 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 117 | "ABGR2101010", |
| 118 | WL_DRM_FORMAT_ABGR2101010, WL_SHM_FORMAT_ABGR2101010, |
| 119 | __DRI_IMAGE_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ARGB2101010, 32, |
| 120 | { 0, 10, 20, 30 }, |
| 121 | { 10, 10, 10, 2 }, |
Daniel Stone | 275b23e | 2018-06-13 06:04:11 +0200 | [diff] [blame] | 122 | }, |
| 123 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 124 | "XRGB8888", |
| 125 | WL_DRM_FORMAT_XRGB8888, WL_SHM_FORMAT_XRGB8888, |
| 126 | __DRI_IMAGE_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_NONE, 32, |
| 127 | { 16, 8, 0, -1 }, |
| 128 | { 8, 8, 8, 0 }, |
Daniel Stone | 845c2f6 | 2018-02-06 18:06:52 +0000 | [diff] [blame] | 129 | }, |
| 130 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 131 | "ARGB8888", |
| 132 | WL_DRM_FORMAT_ARGB8888, WL_SHM_FORMAT_ARGB8888, |
| 133 | __DRI_IMAGE_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_NONE, 32, |
| 134 | { 16, 8, 0, 24 }, |
| 135 | { 8, 8, 8, 8 }, |
Daniel Stone | 845c2f6 | 2018-02-06 18:06:52 +0000 | [diff] [blame] | 136 | }, |
| 137 | { |
Adam Jackson | cb8bbbe | 2019-08-30 15:27:23 -0400 | [diff] [blame] | 138 | "RGB565", |
| 139 | WL_DRM_FORMAT_RGB565, WL_SHM_FORMAT_RGB565, |
| 140 | __DRI_IMAGE_FORMAT_RGB565, __DRI_IMAGE_FORMAT_NONE, 16, |
| 141 | { 11, 5, 0, -1 }, |
| 142 | { 5, 6, 5, 0 }, |
Daniel Stone | 845c2f6 | 2018-02-06 18:06:52 +0000 | [diff] [blame] | 143 | }, |
Daniel Stone | 19cbca3 | 2018-02-06 09:32:22 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 146 | static_assert(ARRAY_SIZE(dri2_wl_visuals) <= EGL_DRI2_MAX_FORMATS, |
| 147 | "dri2_egl_display::formats is not large enough for " |
| 148 | "the formats in dri2_wl_visuals"); |
| 149 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 150 | static int |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 151 | dri2_wl_visual_idx_from_config(struct dri2_egl_display *dri2_dpy, |
| 152 | const __DRIconfig *config) |
| 153 | { |
Kevin Strasser | 7b4ed2b | 2019-01-24 16:32:48 -0800 | [diff] [blame] | 154 | int shifts[4]; |
| 155 | unsigned int sizes[4]; |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 156 | |
Kevin Strasser | 7b4ed2b | 2019-01-24 16:32:48 -0800 | [diff] [blame] | 157 | dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes); |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 158 | |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 159 | for (unsigned int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) { |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 160 | const struct dri2_wl_visual *wl_visual = &dri2_wl_visuals[i]; |
| 161 | |
Kevin Strasser | 7b4ed2b | 2019-01-24 16:32:48 -0800 | [diff] [blame] | 162 | if (shifts[0] == wl_visual->rgba_shifts[0] && |
| 163 | shifts[1] == wl_visual->rgba_shifts[1] && |
| 164 | shifts[2] == wl_visual->rgba_shifts[2] && |
| 165 | shifts[3] == wl_visual->rgba_shifts[3] && |
| 166 | sizes[0] == wl_visual->rgba_sizes[0] && |
| 167 | sizes[1] == wl_visual->rgba_sizes[1] && |
| 168 | sizes[2] == wl_visual->rgba_sizes[2] && |
| 169 | sizes[3] == wl_visual->rgba_sizes[3]) { |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 170 | return i; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | static int |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 178 | dri2_wl_visual_idx_from_fourcc(uint32_t fourcc) |
| 179 | { |
| 180 | for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) { |
| 181 | /* wl_drm format codes overlap with DRIImage FourCC codes for all formats |
| 182 | * we support. */ |
| 183 | if (dri2_wl_visuals[i].wl_drm_format == fourcc) |
| 184 | return i; |
| 185 | } |
| 186 | |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | static int |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 191 | dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format) |
| 192 | { |
| 193 | for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) { |
| 194 | if (dri2_wl_visuals[i].dri_image_format == dri_image_format) |
| 195 | return i; |
| 196 | } |
| 197 | |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | static int |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 202 | dri2_wl_visual_idx_from_shm_format(uint32_t shm_format) |
| 203 | { |
| 204 | for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) { |
| 205 | if (dri2_wl_visuals[i].wl_shm_format == shm_format) |
| 206 | return i; |
| 207 | } |
| 208 | |
| 209 | return -1; |
| 210 | } |
| 211 | |
Mario Kleiner | 820dfce | 2018-06-13 06:04:15 +0200 | [diff] [blame] | 212 | bool |
| 213 | dri2_wl_is_format_supported(void* user_data, uint32_t format) |
| 214 | { |
| 215 | _EGLDisplay *disp = (_EGLDisplay *) user_data; |
| 216 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 217 | int j = dri2_wl_visual_idx_from_fourcc(format); |
| 218 | |
| 219 | if (j == -1) |
| 220 | return false; |
| 221 | |
| 222 | for (int i = 0; dri2_dpy->driver_configs[i]; i++) |
| 223 | if (j == dri2_wl_visual_idx_from_config(dri2_dpy, |
| 224 | dri2_dpy->driver_configs[i])) |
| 225 | return true; |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 230 | static int |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 231 | roundtrip(struct dri2_egl_display *dri2_dpy) |
| 232 | { |
Daniel Stone | 8118bc2 | 2017-05-05 14:44:20 +0100 | [diff] [blame] | 233 | return wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 237 | wl_buffer_release(void *data, struct wl_buffer *buffer) |
| 238 | { |
| 239 | struct dri2_egl_surface *dri2_surf = data; |
| 240 | int i; |
| 241 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 242 | for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i) |
| 243 | if (dri2_surf->color_buffers[i].wl_buffer == buffer) |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 244 | break; |
| 245 | |
Juan A. Suarez Romero | 54a9622 | 2018-08-30 10:14:49 +0200 | [diff] [blame] | 246 | assert (i < ARRAY_SIZE(dri2_surf->color_buffers)); |
| 247 | |
| 248 | if (dri2_surf->color_buffers[i].wl_release) { |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 249 | wl_buffer_destroy(buffer); |
Juan A. Suarez Romero | 54a9622 | 2018-08-30 10:14:49 +0200 | [diff] [blame] | 250 | dri2_surf->color_buffers[i].wl_release = false; |
| 251 | dri2_surf->color_buffers[i].wl_buffer = NULL; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 252 | } |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 253 | |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 254 | dri2_surf->color_buffers[i].locked = false; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Emil Velikov | 5567494 | 2015-07-10 12:24:11 +0100 | [diff] [blame] | 257 | static const struct wl_buffer_listener wl_buffer_listener = { |
| 258 | .release = wl_buffer_release |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 259 | }; |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 260 | |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 261 | static void |
| 262 | resize_callback(struct wl_egl_window *wl_win, void *data) |
| 263 | { |
| 264 | struct dri2_egl_surface *dri2_surf = data; |
| 265 | struct dri2_egl_display *dri2_dpy = |
| 266 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 267 | |
Daniel Stone | e5b0118 | 2020-03-03 10:52:32 +0000 | [diff] [blame] | 268 | if (dri2_surf->base.Width == wl_win->width && |
| 269 | dri2_surf->base.Height == wl_win->height) |
| 270 | return; |
| 271 | |
Juan A. Suarez Romero | a9fb331 | 2018-06-06 10:13:05 +0000 | [diff] [blame] | 272 | /* Update the surface size as soon as native window is resized; from user |
Eric Engestrom | 882ed53 | 2018-08-10 12:37:17 +0100 | [diff] [blame] | 273 | * pov, this makes the effect that resize is done immediately after native |
Juan A. Suarez Romero | a9fb331 | 2018-06-06 10:13:05 +0000 | [diff] [blame] | 274 | * window resize, without requiring to wait until the first draw. |
| 275 | * |
| 276 | * A more detailed and lengthy explanation can be found at |
| 277 | * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html |
| 278 | */ |
| 279 | if (!dri2_surf->back) { |
| 280 | dri2_surf->base.Width = wl_win->width; |
| 281 | dri2_surf->base.Height = wl_win->height; |
| 282 | } |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 283 | dri2_dpy->flush->invalidate(dri2_surf->dri_drawable); |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Stencel, Joanna | 2e0ab61 | 2016-10-24 09:48:11 +0100 | [diff] [blame] | 286 | static void |
| 287 | destroy_window_callback(void *data) |
| 288 | { |
| 289 | struct dri2_egl_surface *dri2_surf = data; |
| 290 | dri2_surf->wl_win = NULL; |
| 291 | } |
| 292 | |
Miguel A. Vico | 2d5d61b | 2017-07-19 17:27:12 -0700 | [diff] [blame] | 293 | static struct wl_surface * |
| 294 | get_wl_surface_proxy(struct wl_egl_window *window) |
| 295 | { |
| 296 | /* Version 3 of wl_egl_window introduced a version field at the same |
| 297 | * location where a pointer to wl_surface was stored. Thus, if |
Eric Engestrom | 882ed53 | 2018-08-10 12:37:17 +0100 | [diff] [blame] | 298 | * window->version is dereferenceable, we've been given an older version of |
Miguel A. Vico | 2d5d61b | 2017-07-19 17:27:12 -0700 | [diff] [blame] | 299 | * wl_egl_window, and window->version points to wl_surface */ |
| 300 | if (_eglPointerIsDereferencable((void *)(window->version))) { |
| 301 | return wl_proxy_create_wrapper((void *)(window->version)); |
| 302 | } |
| 303 | return wl_proxy_create_wrapper(window->surface); |
| 304 | } |
| 305 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 306 | /** |
Eric Engestrom | cc03448 | 2020-07-20 13:38:24 +0200 | [diff] [blame] | 307 | * Called via eglCreateWindowSurface(), drv->CreateWindowSurface(). |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 308 | */ |
| 309 | static _EGLSurface * |
Eric Engestrom | 6b4c474 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 310 | dri2_wl_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf, |
| 311 | void *native_window, const EGLint *attrib_list) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 312 | { |
| 313 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 314 | struct dri2_egl_config *dri2_conf = dri2_egl_config(conf); |
Chad Versace | 6d1f83e | 2014-01-07 14:54:51 -0800 | [diff] [blame] | 315 | struct wl_egl_window *window = native_window; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 316 | struct dri2_egl_surface *dri2_surf; |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 317 | int visual_idx; |
Marek Olšák | c2c2e9a | 2015-06-10 02:49:29 +0200 | [diff] [blame] | 318 | const __DRIconfig *config; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 319 | |
Matt Turner | f0a8bcd | 2014-09-21 21:15:26 -0700 | [diff] [blame] | 320 | dri2_surf = calloc(1, sizeof *dri2_surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 321 | if (!dri2_surf) { |
| 322 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 323 | return NULL; |
| 324 | } |
Boyan Ding | 052b3d4 | 2015-06-13 15:36:27 +0800 | [diff] [blame] | 325 | |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 326 | if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, |
Paulo Zanoni | 04ecda3 | 2019-05-01 15:42:26 -0700 | [diff] [blame] | 327 | attrib_list, false, native_window)) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 328 | goto cleanup_surf; |
| 329 | |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 330 | config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT, |
| 331 | dri2_surf->base.GLColorspace); |
Juan A. Suarez Romero | fd4eba4 | 2018-05-02 16:20:16 +0000 | [diff] [blame] | 332 | |
| 333 | if (!config) { |
| 334 | _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration"); |
| 335 | goto cleanup_surf; |
| 336 | } |
| 337 | |
Juan A. Suarez Romero | 1fe7cbd | 2018-06-04 10:22:49 +0000 | [diff] [blame] | 338 | dri2_surf->base.Width = window->width; |
| 339 | dri2_surf->base.Height = window->height; |
| 340 | |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 341 | visual_idx = dri2_wl_visual_idx_from_config(dri2_dpy, config); |
| 342 | assert(visual_idx != -1); |
| 343 | |
Emil Velikov | da100fe | 2017-08-27 11:20:32 +0100 | [diff] [blame] | 344 | if (dri2_dpy->wl_dmabuf || dri2_dpy->wl_drm) { |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 345 | dri2_surf->format = dri2_wl_visuals[visual_idx].wl_drm_format; |
Daniel Stone | a1727aa | 2017-02-13 14:06:10 +0000 | [diff] [blame] | 346 | } else { |
Emil Velikov | 6ef0fc4 | 2017-05-12 14:19:59 +0100 | [diff] [blame] | 347 | assert(dri2_dpy->wl_shm); |
Daniel Stone | a9cc4ed | 2018-02-06 10:07:23 +0000 | [diff] [blame] | 348 | dri2_surf->format = dri2_wl_visuals[visual_idx].wl_shm_format; |
Daniel Stone | a1727aa | 2017-02-13 14:06:10 +0000 | [diff] [blame] | 349 | } |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 350 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 351 | dri2_surf->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy); |
Lionel Landwerlin | 30dc56b | 2017-05-22 11:07:53 +0100 | [diff] [blame] | 352 | if (!dri2_surf->wl_queue) { |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 353 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 354 | goto cleanup_surf; |
| 355 | } |
| 356 | |
| 357 | if (dri2_dpy->wl_drm) { |
| 358 | dri2_surf->wl_drm_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_drm); |
| 359 | if (!dri2_surf->wl_drm_wrapper) { |
| 360 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 361 | goto cleanup_queue; |
| 362 | } |
| 363 | wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_drm_wrapper, |
| 364 | dri2_surf->wl_queue); |
| 365 | } |
| 366 | |
| 367 | dri2_surf->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy); |
| 368 | if (!dri2_surf->wl_dpy_wrapper) { |
| 369 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
| 370 | goto cleanup_drm; |
| 371 | } |
| 372 | wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_dpy_wrapper, |
| 373 | dri2_surf->wl_queue); |
| 374 | |
Miguel A. Vico | 2d5d61b | 2017-07-19 17:27:12 -0700 | [diff] [blame] | 375 | dri2_surf->wl_surface_wrapper = get_wl_surface_proxy(window); |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 376 | if (!dri2_surf->wl_surface_wrapper) { |
| 377 | _eglError(EGL_BAD_ALLOC, "dri2_create_surface"); |
Emil Velikov | 8344211 | 2017-08-27 11:20:27 +0100 | [diff] [blame] | 378 | goto cleanup_dpy_wrapper; |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 379 | } |
| 380 | wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_surface_wrapper, |
| 381 | dri2_surf->wl_queue); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 382 | |
Emil Velikov | 293d64e | 2017-08-27 11:20:34 +0100 | [diff] [blame] | 383 | dri2_surf->wl_win = window; |
Eric Engestrom | 1db4ec0 | 2018-05-29 15:41:28 +0100 | [diff] [blame] | 384 | dri2_surf->wl_win->driver_private = dri2_surf; |
Stencel, Joanna | 2e0ab61 | 2016-10-24 09:48:11 +0100 | [diff] [blame] | 385 | dri2_surf->wl_win->destroy_window_callback = destroy_window_callback; |
Emil Velikov | 293d64e | 2017-08-27 11:20:34 +0100 | [diff] [blame] | 386 | if (dri2_dpy->flush) |
| 387 | dri2_surf->wl_win->resize_callback = resize_callback; |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 388 | |
Mathias Fröhlich | a7ecf78 | 2019-06-07 07:12:42 +0200 | [diff] [blame] | 389 | if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf)) |
Emil Velikov | 8344211 | 2017-08-27 11:20:27 +0100 | [diff] [blame] | 390 | goto cleanup_surf_wrapper; |
Emil Velikov | cb5e799 | 2016-11-28 18:25:19 +0000 | [diff] [blame] | 391 | |
Eric Engestrom | 2714a8f | 2017-07-31 14:49:31 +0100 | [diff] [blame] | 392 | dri2_surf->base.SwapInterval = dri2_dpy->default_swap_interval; |
Emil Velikov | cb5e799 | 2016-11-28 18:25:19 +0000 | [diff] [blame] | 393 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 394 | return &dri2_surf->base; |
| 395 | |
Emil Velikov | 8344211 | 2017-08-27 11:20:27 +0100 | [diff] [blame] | 396 | cleanup_surf_wrapper: |
| 397 | wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper); |
| 398 | cleanup_dpy_wrapper: |
| 399 | wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper); |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 400 | cleanup_drm: |
| 401 | if (dri2_surf->wl_drm_wrapper) |
| 402 | wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper); |
| 403 | cleanup_queue: |
| 404 | wl_event_queue_destroy(dri2_surf->wl_queue); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 405 | cleanup_surf: |
| 406 | free(dri2_surf); |
| 407 | |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Chad Versace | 9a40ee1 | 2014-02-09 09:13:27 -0800 | [diff] [blame] | 411 | static _EGLSurface * |
Eric Engestrom | 6b4c474 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 412 | dri2_wl_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf, |
| 413 | void *native_window, const EGLint *attrib_list) |
Chad Versace | 9a40ee1 | 2014-02-09 09:13:27 -0800 | [diff] [blame] | 414 | { |
| 415 | /* From the EGL_EXT_platform_wayland spec, version 3: |
| 416 | * |
| 417 | * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy> |
| 418 | * that belongs to Wayland. Any such call fails and generates |
| 419 | * EGL_BAD_PARAMETER. |
| 420 | */ |
| 421 | _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on " |
| 422 | "Wayland"); |
| 423 | return NULL; |
| 424 | } |
| 425 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 426 | /** |
Eric Engestrom | cc03448 | 2020-07-20 13:38:24 +0200 | [diff] [blame] | 427 | * Called via eglDestroySurface(), drv->DestroySurface(). |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 428 | */ |
| 429 | static EGLBoolean |
Eric Engestrom | 6b4c474 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 430 | dri2_wl_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 431 | { |
| 432 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 433 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 434 | |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 435 | dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 436 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 437 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 438 | if (dri2_surf->color_buffers[i].wl_buffer) |
| 439 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 440 | if (dri2_surf->color_buffers[i].dri_image) |
| 441 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image); |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 442 | if (dri2_surf->color_buffers[i].linear_copy) |
| 443 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 444 | if (dri2_surf->color_buffers[i].data) |
| 445 | munmap(dri2_surf->color_buffers[i].data, |
| 446 | dri2_surf->color_buffers[i].data_size); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 447 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 448 | |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 449 | if (dri2_dpy->dri2) |
| 450 | dri2_egl_surface_free_local_buffers(dri2_surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 451 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 452 | if (dri2_surf->throttle_callback) |
| 453 | wl_callback_destroy(dri2_surf->throttle_callback); |
Jonas Ådahl | a3b6b2d | 2012-10-28 00:50:12 +0200 | [diff] [blame] | 454 | |
Stencel, Joanna | 2e0ab61 | 2016-10-24 09:48:11 +0100 | [diff] [blame] | 455 | if (dri2_surf->wl_win) { |
Eric Engestrom | 1db4ec0 | 2018-05-29 15:41:28 +0100 | [diff] [blame] | 456 | dri2_surf->wl_win->driver_private = NULL; |
Stencel, Joanna | 2e0ab61 | 2016-10-24 09:48:11 +0100 | [diff] [blame] | 457 | dri2_surf->wl_win->resize_callback = NULL; |
| 458 | dri2_surf->wl_win->destroy_window_callback = NULL; |
| 459 | } |
Ander Conselvan de Oliveira | ca3ed3e | 2012-11-30 17:41:02 +0200 | [diff] [blame] | 460 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 461 | wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper); |
| 462 | wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper); |
Emil Velikov | 1a8015e | 2017-08-27 11:20:28 +0100 | [diff] [blame] | 463 | if (dri2_surf->wl_drm_wrapper) |
| 464 | wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper); |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 465 | wl_event_queue_destroy(dri2_surf->wl_queue); |
| 466 | |
Zhongmin Wu | e013ce8 | 2017-09-15 18:32:42 +0100 | [diff] [blame] | 467 | dri2_fini_surface(surf); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 468 | free(surf); |
| 469 | |
| 470 | return EGL_TRUE; |
| 471 | } |
| 472 | |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 473 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 474 | dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf) |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 475 | { |
| 476 | struct dri2_egl_display *dri2_dpy = |
| 477 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 478 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 479 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Juan A. Suarez Romero | 54a9622 | 2018-08-30 10:14:49 +0200 | [diff] [blame] | 480 | if (dri2_surf->color_buffers[i].wl_buffer) { |
| 481 | if (dri2_surf->color_buffers[i].locked) { |
| 482 | dri2_surf->color_buffers[i].wl_release = true; |
| 483 | } else { |
| 484 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
| 485 | dri2_surf->color_buffers[i].wl_buffer = NULL; |
| 486 | } |
| 487 | } |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 488 | if (dri2_surf->color_buffers[i].dri_image) |
| 489 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image); |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 490 | if (dri2_surf->color_buffers[i].linear_copy) |
| 491 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 492 | if (dri2_surf->color_buffers[i].data) |
| 493 | munmap(dri2_surf->color_buffers[i].data, |
| 494 | dri2_surf->color_buffers[i].data_size); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 495 | |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 496 | dri2_surf->color_buffers[i].dri_image = NULL; |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 497 | dri2_surf->color_buffers[i].linear_copy = NULL; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 498 | dri2_surf->color_buffers[i].data = NULL; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 501 | if (dri2_dpy->dri2) |
| 502 | dri2_egl_surface_free_local_buffers(dri2_surf); |
Benjamin Franzke | 51f2820 | 2011-02-11 02:23:14 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 505 | static int |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 506 | get_back_bo(struct dri2_egl_surface *dri2_surf) |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 507 | { |
| 508 | struct dri2_egl_display *dri2_dpy = |
| 509 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 510 | int use_flags; |
Daniel Stone | 4de98a9 | 2018-02-06 11:51:17 +0000 | [diff] [blame] | 511 | int visual_idx; |
Vivek Kasireddy | 1e96eec | 2015-02-10 19:15:31 -0800 | [diff] [blame] | 512 | unsigned int dri_image_format; |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 513 | unsigned int linear_dri_image_format; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 514 | uint64_t *modifiers; |
| 515 | int num_modifiers; |
Vivek Kasireddy | 1e96eec | 2015-02-10 19:15:31 -0800 | [diff] [blame] | 516 | |
Daniel Stone | 4de98a9 | 2018-02-06 11:51:17 +0000 | [diff] [blame] | 517 | visual_idx = dri2_wl_visual_idx_from_fourcc(dri2_surf->format); |
| 518 | assert(visual_idx != -1); |
| 519 | dri_image_format = dri2_wl_visuals[visual_idx].dri_image_format; |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 520 | linear_dri_image_format = dri_image_format; |
Daniel Stone | 4732094 | 2018-02-06 10:29:13 +0000 | [diff] [blame] | 521 | modifiers = u_vector_tail(&dri2_dpy->wl_modifiers[visual_idx]); |
| 522 | num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers[visual_idx]); |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 523 | |
Ivan Molodetskikh | c376865 | 2019-09-27 00:45:39 +0300 | [diff] [blame] | 524 | if (num_modifiers == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) { |
| 525 | /* For the purposes of this function, an INVALID modifier on its own |
| 526 | * means the modifiers aren't supported. |
| 527 | */ |
| 528 | num_modifiers = 0; |
| 529 | } |
| 530 | |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 531 | /* Substitute dri image format if server does not support original format */ |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 532 | if (!BITSET_TEST(dri2_dpy->formats, visual_idx)) |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 533 | linear_dri_image_format = dri2_wl_visuals[visual_idx].alt_dri_image_format; |
| 534 | |
| 535 | /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and |
| 536 | * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free |
| 537 | * of bugs. |
| 538 | */ |
| 539 | assert(linear_dri_image_format != __DRI_IMAGE_FORMAT_NONE); |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 540 | assert(BITSET_TEST(dri2_dpy->formats, |
| 541 | dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format))); |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 542 | |
Daniel Stone | 9ca6711 | 2016-06-01 09:59:06 +0100 | [diff] [blame] | 543 | /* There might be a buffer release already queued that wasn't processed */ |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 544 | wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue); |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 545 | |
Daniel Stone | 1f2d009 | 2017-05-15 22:22:32 +0100 | [diff] [blame] | 546 | while (dri2_surf->back == NULL) { |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 547 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Eric Engestrom | 882ed53 | 2018-08-10 12:37:17 +0100 | [diff] [blame] | 548 | /* Get an unlocked buffer, preferably one with a dri_buffer |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 549 | * already allocated. */ |
| 550 | if (dri2_surf->color_buffers[i].locked) |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 551 | continue; |
| 552 | if (dri2_surf->back == NULL) |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 553 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 554 | else if (dri2_surf->back->dri_image == NULL) |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 555 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 556 | } |
Daniel Stone | 1f2d009 | 2017-05-15 22:22:32 +0100 | [diff] [blame] | 557 | |
| 558 | if (dri2_surf->back) |
| 559 | break; |
| 560 | |
| 561 | /* If we don't have a buffer, then block on the server to release one for |
Kai Chen | 151188d | 2017-08-07 08:34:51 -0700 | [diff] [blame] | 562 | * us, and try again. wl_display_dispatch_queue will process any pending |
| 563 | * events, however not all servers flush on issuing a buffer release |
| 564 | * event. So, we spam the server with roundtrips as they always cause a |
| 565 | * client flush. |
| 566 | */ |
| 567 | if (wl_display_roundtrip_queue(dri2_dpy->wl_dpy, |
| 568 | dri2_surf->wl_queue) < 0) |
Daniel Stone | 1f2d009 | 2017-05-15 22:22:32 +0100 | [diff] [blame] | 569 | return -1; |
Benjamin Franzke | 0cb356d | 2011-05-06 19:13:29 +0200 | [diff] [blame] | 570 | } |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 571 | |
| 572 | if (dri2_surf->back == NULL) |
| 573 | return -1; |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 574 | |
Axel Davy | d943ac4 | 2015-10-21 12:28:00 +0200 | [diff] [blame] | 575 | use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER; |
| 576 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 577 | if (dri2_dpy->is_different_gpu && |
| 578 | dri2_surf->back->linear_copy == NULL) { |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 579 | /* The LINEAR modifier should be a perfect alias of the LINEAR use |
| 580 | * flag; try the new interface first before the old, then fall back. */ |
| 581 | if (dri2_dpy->image->base.version >= 15 && |
| 582 | dri2_dpy->image->createImageWithModifiers) { |
| 583 | uint64_t linear_mod = DRM_FORMAT_MOD_LINEAR; |
| 584 | |
| 585 | dri2_surf->back->linear_copy = |
| 586 | dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen, |
| 587 | dri2_surf->base.Width, |
| 588 | dri2_surf->base.Height, |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 589 | linear_dri_image_format, |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 590 | &linear_mod, |
| 591 | 1, |
| 592 | NULL); |
| 593 | } else { |
| 594 | dri2_surf->back->linear_copy = |
| 595 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
| 596 | dri2_surf->base.Width, |
| 597 | dri2_surf->base.Height, |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 598 | linear_dri_image_format, |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 599 | use_flags | |
| 600 | __DRI_IMAGE_USE_LINEAR, |
| 601 | NULL); |
| 602 | } |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 603 | if (dri2_surf->back->linear_copy == NULL) |
| 604 | return -1; |
| 605 | } |
| 606 | |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 607 | if (dri2_surf->back->dri_image == NULL) { |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 608 | /* If our DRIImage implementation does not support |
| 609 | * createImageWithModifiers, then fall back to the old createImage, |
| 610 | * and hope it allocates an image which is acceptable to the winsys. |
| 611 | */ |
| 612 | if (num_modifiers && dri2_dpy->image->base.version >= 15 && |
| 613 | dri2_dpy->image->createImageWithModifiers) { |
| 614 | dri2_surf->back->dri_image = |
| 615 | dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen, |
| 616 | dri2_surf->base.Width, |
| 617 | dri2_surf->base.Height, |
| 618 | dri_image_format, |
| 619 | modifiers, |
| 620 | num_modifiers, |
| 621 | NULL); |
| 622 | } else { |
| 623 | dri2_surf->back->dri_image = |
| 624 | dri2_dpy->image->createImage(dri2_dpy->dri_screen, |
| 625 | dri2_surf->base.Width, |
| 626 | dri2_surf->base.Height, |
| 627 | dri_image_format, |
| 628 | dri2_dpy->is_different_gpu ? |
| 629 | 0 : use_flags, |
| 630 | NULL); |
| 631 | } |
| 632 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 633 | dri2_surf->back->age = 0; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 634 | } |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 635 | if (dri2_surf->back->dri_image == NULL) |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 636 | return -1; |
| 637 | |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 638 | dri2_surf->back->locked = true; |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | static void |
| 645 | back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer) |
| 646 | { |
| 647 | struct dri2_egl_display *dri2_dpy = |
| 648 | dri2_egl_display(dri2_surf->base.Resource.Display); |
| 649 | __DRIimage *image; |
| 650 | int name, pitch; |
| 651 | |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 652 | image = dri2_surf->back->dri_image; |
| 653 | |
| 654 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 655 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch); |
| 656 | |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 657 | buffer->attachment = __DRI_BUFFER_BACK_LEFT; |
| 658 | buffer->name = name; |
| 659 | buffer->pitch = pitch; |
| 660 | buffer->cpp = 4; |
| 661 | buffer->flags = 0; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static int |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 665 | update_buffers(struct dri2_egl_surface *dri2_surf) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 666 | { |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 667 | struct dri2_egl_display *dri2_dpy = |
| 668 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Ander Conselvan de Oliveira | 60a11e2 | 2012-11-22 15:34:49 +0200 | [diff] [blame] | 669 | |
Carlos Garnacho | 30a01cd | 2018-11-20 18:06:38 +0100 | [diff] [blame] | 670 | if (dri2_surf->base.Width != dri2_surf->wl_win->width || |
| 671 | dri2_surf->base.Height != dri2_surf->wl_win->height) { |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 672 | |
| 673 | dri2_surf->base.Width = dri2_surf->wl_win->width; |
| 674 | dri2_surf->base.Height = dri2_surf->wl_win->height; |
| 675 | dri2_surf->dx = dri2_surf->wl_win->dx; |
| 676 | dri2_surf->dy = dri2_surf->wl_win->dy; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 677 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 678 | |
Carlos Garnacho | 30a01cd | 2018-11-20 18:06:38 +0100 | [diff] [blame] | 679 | if (dri2_surf->base.Width != dri2_surf->wl_win->attached_width || |
| 680 | dri2_surf->base.Height != dri2_surf->wl_win->attached_height) { |
| 681 | dri2_wl_release_buffers(dri2_surf); |
| 682 | } |
| 683 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 684 | if (get_back_bo(dri2_surf) < 0) { |
| 685 | _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer"); |
| 686 | return -1; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 687 | } |
| 688 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 689 | /* If we have an extra unlocked buffer at this point, we had to do triple |
| 690 | * buffering for a while, but now can go back to just double buffering. |
| 691 | * That means we can free any unlocked buffer now. */ |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 692 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 693 | if (!dri2_surf->color_buffers[i].locked && |
| 694 | dri2_surf->color_buffers[i].wl_buffer) { |
| 695 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 696 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image); |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 697 | if (dri2_dpy->is_different_gpu) |
| 698 | dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 699 | dri2_surf->color_buffers[i].wl_buffer = NULL; |
Kristian Høgsberg | 664fe6d | 2013-02-02 07:40:51 -0500 | [diff] [blame] | 700 | dri2_surf->color_buffers[i].dri_image = NULL; |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 701 | dri2_surf->color_buffers[i].linear_copy = NULL; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 702 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Jonas Ådahl | 903ad59 | 2019-05-06 09:54:27 +0200 | [diff] [blame] | 708 | static int |
| 709 | update_buffers_if_needed(struct dri2_egl_surface *dri2_surf) |
| 710 | { |
| 711 | if (dri2_surf->back != NULL) |
| 712 | return 0; |
| 713 | |
| 714 | return update_buffers(dri2_surf); |
| 715 | } |
| 716 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 717 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 718 | dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable, |
| 719 | int *width, int *height, |
| 720 | unsigned int *attachments, int count, |
| 721 | int *out_count, void *loaderPrivate) |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 722 | { |
| 723 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 724 | int i, j; |
| 725 | |
| 726 | if (update_buffers(dri2_surf) < 0) |
| 727 | return NULL; |
| 728 | |
| 729 | for (i = 0, j = 0; i < 2 * count; i += 2, j++) { |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 730 | __DRIbuffer *local; |
| 731 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 732 | switch (attachments[i]) { |
| 733 | case __DRI_BUFFER_BACK_LEFT: |
| 734 | back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]); |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 735 | break; |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 736 | default: |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 737 | local = dri2_egl_surface_alloc_local_buffer(dri2_surf, attachments[i], |
| 738 | attachments[i + 1]); |
| 739 | |
| 740 | if (!local) { |
| 741 | _eglError(EGL_BAD_ALLOC, "failed to allocate local buffer"); |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 742 | return NULL; |
| 743 | } |
Gwan-gyeong Mun | 640b6e6 | 2017-08-05 00:16:11 +0900 | [diff] [blame] | 744 | dri2_surf->buffers[j] = *local; |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 745 | break; |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 749 | *out_count = j; |
| 750 | if (j == 0) |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 751 | return NULL; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 752 | |
| 753 | *width = dri2_surf->base.Width; |
| 754 | *height = dri2_surf->base.Height; |
| 755 | |
| 756 | return dri2_surf->buffers; |
| 757 | } |
| 758 | |
| 759 | static __DRIbuffer * |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 760 | dri2_wl_get_buffers(__DRIdrawable * driDrawable, |
| 761 | int *width, int *height, |
| 762 | unsigned int *attachments, int count, |
| 763 | int *out_count, void *loaderPrivate) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 764 | { |
Vivek Kasireddy | 1e96eec | 2015-02-10 19:15:31 -0800 | [diff] [blame] | 765 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 766 | unsigned int *attachments_with_format; |
| 767 | __DRIbuffer *buffer; |
Daniel Stone | d32b23f | 2018-02-06 11:58:45 +0000 | [diff] [blame] | 768 | int visual_idx = dri2_wl_visual_idx_from_fourcc(dri2_surf->format); |
Vivek Kasireddy | 1e96eec | 2015-02-10 19:15:31 -0800 | [diff] [blame] | 769 | |
Daniel Stone | d32b23f | 2018-02-06 11:58:45 +0000 | [diff] [blame] | 770 | if (visual_idx == -1) |
Vivek Kasireddy | 1e96eec | 2015-02-10 19:15:31 -0800 | [diff] [blame] | 771 | return NULL; |
Vivek Kasireddy | 1e96eec | 2015-02-10 19:15:31 -0800 | [diff] [blame] | 772 | |
Carl Worth | ecc89e4 | 2014-09-03 14:33:18 -0700 | [diff] [blame] | 773 | attachments_with_format = calloc(count, 2 * sizeof(unsigned int)); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 774 | if (!attachments_with_format) { |
| 775 | *out_count = 0; |
| 776 | return NULL; |
| 777 | } |
| 778 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 779 | for (int i = 0; i < count; ++i) { |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 780 | attachments_with_format[2*i] = attachments[i]; |
Daniel Stone | d32b23f | 2018-02-06 11:58:45 +0000 | [diff] [blame] | 781 | attachments_with_format[2*i + 1] = dri2_wl_visuals[visual_idx].bpp; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | buffer = |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 785 | dri2_wl_get_buffers_with_format(driDrawable, |
| 786 | width, height, |
| 787 | attachments_with_format, count, |
| 788 | out_count, loaderPrivate); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 789 | |
| 790 | free(attachments_with_format); |
| 791 | |
| 792 | return buffer; |
| 793 | } |
| 794 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 795 | static int |
| 796 | image_get_buffers(__DRIdrawable *driDrawable, |
| 797 | unsigned int format, |
| 798 | uint32_t *stamp, |
| 799 | void *loaderPrivate, |
| 800 | uint32_t buffer_mask, |
| 801 | struct __DRIimageList *buffers) |
| 802 | { |
| 803 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 804 | |
| 805 | if (update_buffers(dri2_surf) < 0) |
| 806 | return 0; |
| 807 | |
| 808 | buffers->image_mask = __DRI_IMAGE_BUFFER_BACK; |
| 809 | buffers->back = dri2_surf->back->dri_image; |
| 810 | |
| 811 | return 1; |
| 812 | } |
| 813 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 814 | static void |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 815 | dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 816 | { |
| 817 | (void) driDrawable; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 818 | (void) loaderPrivate; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Adam Jackson | d014061 | 2019-09-10 11:53:11 -0400 | [diff] [blame] | 821 | static unsigned |
| 822 | dri2_wl_get_capability(void *loaderPrivate, enum dri_loader_cap cap) |
| 823 | { |
| 824 | switch (cap) { |
| 825 | case DRI_LOADER_CAP_FP16: |
| 826 | return 1; |
| 827 | default: |
| 828 | return 0; |
| 829 | } |
| 830 | } |
| 831 | |
Emil Velikov | d2d579d | 2016-08-16 18:21:48 +0100 | [diff] [blame] | 832 | static const __DRIdri2LoaderExtension dri2_loader_extension = { |
Adam Jackson | d014061 | 2019-09-10 11:53:11 -0400 | [diff] [blame] | 833 | .base = { __DRI_DRI2_LOADER, 4 }, |
Emil Velikov | d2d579d | 2016-08-16 18:21:48 +0100 | [diff] [blame] | 834 | |
| 835 | .getBuffers = dri2_wl_get_buffers, |
| 836 | .flushFrontBuffer = dri2_wl_flush_front_buffer, |
| 837 | .getBuffersWithFormat = dri2_wl_get_buffers_with_format, |
Adam Jackson | d014061 | 2019-09-10 11:53:11 -0400 | [diff] [blame] | 838 | .getCapability = dri2_wl_get_capability, |
Emil Velikov | d2d579d | 2016-08-16 18:21:48 +0100 | [diff] [blame] | 839 | }; |
| 840 | |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 841 | static const __DRIimageLoaderExtension image_loader_extension = { |
Adam Jackson | d014061 | 2019-09-10 11:53:11 -0400 | [diff] [blame] | 842 | .base = { __DRI_IMAGE_LOADER, 2 }, |
Emil Velikov | 9e627cc | 2014-02-12 18:32:59 +0000 | [diff] [blame] | 843 | |
| 844 | .getBuffers = image_get_buffers, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 845 | .flushFrontBuffer = dri2_wl_flush_front_buffer, |
Adam Jackson | d014061 | 2019-09-10 11:53:11 -0400 | [diff] [blame] | 846 | .getCapability = dri2_wl_get_capability, |
Kristian Høgsberg | 68bb26be | 2013-11-08 22:10:36 -0800 | [diff] [blame] | 847 | }; |
| 848 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 849 | static void |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 850 | wayland_throttle_callback(void *data, |
| 851 | struct wl_callback *callback, |
| 852 | uint32_t time) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 853 | { |
| 854 | struct dri2_egl_surface *dri2_surf = data; |
| 855 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 856 | dri2_surf->throttle_callback = NULL; |
Kristian Høgsberg | 6602bda | 2011-08-16 22:38:22 -0400 | [diff] [blame] | 857 | wl_callback_destroy(callback); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 860 | static const struct wl_callback_listener throttle_listener = { |
Emil Velikov | 5567494 | 2015-07-10 12:24:11 +0100 | [diff] [blame] | 861 | .done = wayland_throttle_callback |
Kristian Høgsberg | 6602bda | 2011-08-16 22:38:22 -0400 | [diff] [blame] | 862 | }; |
| 863 | |
Derek Foreman | 0db36ca | 2017-10-30 15:52:22 -0500 | [diff] [blame] | 864 | static EGLBoolean |
| 865 | get_fourcc(struct dri2_egl_display *dri2_dpy, |
| 866 | __DRIimage *image, int *fourcc) |
| 867 | { |
| 868 | EGLBoolean query; |
Eric Engestrom | ca95d7a | 2017-11-16 10:02:15 +0000 | [diff] [blame] | 869 | int dri_format; |
Daniel Stone | 4de98a9 | 2018-02-06 11:51:17 +0000 | [diff] [blame] | 870 | int visual_idx; |
Derek Foreman | 0db36ca | 2017-10-30 15:52:22 -0500 | [diff] [blame] | 871 | |
| 872 | query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FOURCC, |
| 873 | fourcc); |
| 874 | if (query) |
| 875 | return true; |
| 876 | |
| 877 | query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, |
| 878 | &dri_format); |
| 879 | if (!query) |
| 880 | return false; |
| 881 | |
Daniel Stone | 4de98a9 | 2018-02-06 11:51:17 +0000 | [diff] [blame] | 882 | visual_idx = dri2_wl_visual_idx_from_dri_image_format(dri_format); |
| 883 | if (visual_idx == -1) |
Derek Foreman | 0db36ca | 2017-10-30 15:52:22 -0500 | [diff] [blame] | 884 | return false; |
Daniel Stone | 4de98a9 | 2018-02-06 11:51:17 +0000 | [diff] [blame] | 885 | |
| 886 | *fourcc = dri2_wl_visuals[visual_idx].wl_drm_format; |
| 887 | return true; |
Derek Foreman | 0db36ca | 2017-10-30 15:52:22 -0500 | [diff] [blame] | 888 | } |
| 889 | |
Daniel Stone | c4a1c7a | 2017-06-16 17:29:42 +0100 | [diff] [blame] | 890 | static struct wl_buffer * |
| 891 | create_wl_buffer(struct dri2_egl_display *dri2_dpy, |
| 892 | struct dri2_egl_surface *dri2_surf, |
| 893 | __DRIimage *image) |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 894 | { |
Daniel Stone | c4a1c7a | 2017-06-16 17:29:42 +0100 | [diff] [blame] | 895 | struct wl_buffer *ret; |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 896 | EGLBoolean query; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 897 | int width, height, fourcc, num_planes; |
Daniel Stone | b65d6da | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 898 | uint64_t modifier = DRM_FORMAT_MOD_INVALID; |
Daniel Stone | 6595c69 | 2017-06-16 17:33:56 +0100 | [diff] [blame] | 899 | |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 900 | query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width); |
| 901 | query &= dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, |
| 902 | &height); |
Derek Foreman | 0db36ca | 2017-10-30 15:52:22 -0500 | [diff] [blame] | 903 | query &= get_fourcc(dri2_dpy, image, &fourcc); |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 904 | if (!query) |
| 905 | return NULL; |
| 906 | |
| 907 | query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES, |
| 908 | &num_planes); |
| 909 | if (!query) |
| 910 | num_planes = 1; |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 911 | |
Daniel Stone | b65d6da | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 912 | if (dri2_dpy->image->base.version >= 15) { |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 913 | int mod_hi, mod_lo; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 914 | |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 915 | query = dri2_dpy->image->queryImage(image, |
| 916 | __DRI_IMAGE_ATTRIB_MODIFIER_UPPER, |
| 917 | &mod_hi); |
| 918 | query &= dri2_dpy->image->queryImage(image, |
| 919 | __DRI_IMAGE_ATTRIB_MODIFIER_LOWER, |
| 920 | &mod_lo); |
Daniel Stone | b65d6da | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 921 | if (query) { |
Eric Engestrom | 2de9e84 | 2018-08-16 15:22:46 +0100 | [diff] [blame] | 922 | modifier = combine_u32_into_u64(mod_hi, mod_lo); |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 923 | } |
Daniel Stone | b65d6da | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 924 | } |
| 925 | |
Ivan Molodetskikh | c376865 | 2019-09-27 00:45:39 +0300 | [diff] [blame] | 926 | bool supported_modifier = false; |
Christopher James Halse Rogers | 98675d3 | 2020-03-24 14:19:51 +1100 | [diff] [blame] | 927 | bool mod_invalid_supported = false; |
| 928 | int visual_idx = dri2_wl_visual_idx_from_fourcc(fourcc); |
| 929 | assert(visual_idx != -1); |
Ivan Molodetskikh | c376865 | 2019-09-27 00:45:39 +0300 | [diff] [blame] | 930 | |
Christopher James Halse Rogers | 98675d3 | 2020-03-24 14:19:51 +1100 | [diff] [blame] | 931 | uint64_t *mod; |
| 932 | u_vector_foreach(mod, &dri2_dpy->wl_modifiers[visual_idx]) { |
| 933 | if (*mod == DRM_FORMAT_MOD_INVALID) { |
| 934 | mod_invalid_supported = true; |
Ivan Molodetskikh | c376865 | 2019-09-27 00:45:39 +0300 | [diff] [blame] | 935 | } |
Christopher James Halse Rogers | 98675d3 | 2020-03-24 14:19:51 +1100 | [diff] [blame] | 936 | if (*mod == modifier) { |
| 937 | supported_modifier = true; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | if (!supported_modifier && mod_invalid_supported) { |
| 942 | /* If the server has advertised DRM_FORMAT_MOD_INVALID then we trust |
| 943 | * that the client has allocated the buffer with the right implicit |
| 944 | * modifier for the format, even though it's allocated a buffer the |
| 945 | * server hasn't explicitly claimed to support. */ |
| 946 | modifier = DRM_FORMAT_MOD_INVALID; |
| 947 | supported_modifier = true; |
Ivan Molodetskikh | c376865 | 2019-09-27 00:45:39 +0300 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | if (dri2_dpy->wl_dmabuf && supported_modifier) { |
Daniel Stone | b65d6da | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 951 | struct zwp_linux_buffer_params_v1 *params; |
| 952 | int i; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 953 | |
| 954 | /* We don't need a wrapper for wl_dmabuf objects, because we have to |
| 955 | * create the intermediate params object; we can set the queue on this, |
| 956 | * and the wl_buffer inherits it race-free. */ |
| 957 | params = zwp_linux_dmabuf_v1_create_params(dri2_dpy->wl_dmabuf); |
| 958 | if (dri2_surf) |
| 959 | wl_proxy_set_queue((struct wl_proxy *) params, dri2_surf->wl_queue); |
| 960 | |
| 961 | for (i = 0; i < num_planes; i++) { |
| 962 | __DRIimage *p_image; |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 963 | int stride, offset; |
| 964 | int fd = -1; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 965 | |
Louis-Francis Ratté-Boulianne | a34715a | 2017-09-28 03:18:33 -0400 | [diff] [blame] | 966 | p_image = dri2_dpy->image->fromPlanar(image, i, NULL); |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 967 | if (!p_image) { |
Louis-Francis Ratté-Boulianne | a34715a | 2017-09-28 03:18:33 -0400 | [diff] [blame] | 968 | assert(i == 0); |
| 969 | p_image = image; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 972 | query = dri2_dpy->image->queryImage(p_image, |
| 973 | __DRI_IMAGE_ATTRIB_FD, |
| 974 | &fd); |
| 975 | query &= dri2_dpy->image->queryImage(p_image, |
| 976 | __DRI_IMAGE_ATTRIB_STRIDE, |
| 977 | &stride); |
| 978 | query &= dri2_dpy->image->queryImage(p_image, |
| 979 | __DRI_IMAGE_ATTRIB_OFFSET, |
| 980 | &offset); |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 981 | if (image != p_image) |
| 982 | dri2_dpy->image->destroyImage(p_image); |
| 983 | |
Daniel Stone | 6273d2f | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 984 | if (!query) { |
| 985 | if (fd >= 0) |
| 986 | close(fd); |
| 987 | zwp_linux_buffer_params_v1_destroy(params); |
| 988 | return NULL; |
| 989 | } |
| 990 | |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 991 | zwp_linux_buffer_params_v1_add(params, fd, i, offset, stride, |
Daniel Stone | b65d6da | 2017-10-02 16:40:53 +0100 | [diff] [blame] | 992 | modifier >> 32, modifier & 0xffffffff); |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 993 | close(fd); |
| 994 | } |
| 995 | |
| 996 | ret = zwp_linux_buffer_params_v1_create_immed(params, width, height, |
| 997 | fourcc, 0); |
| 998 | zwp_linux_buffer_params_v1_destroy(params); |
| 999 | } else if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) { |
Daniel Stone | cfaca57 | 2017-06-16 17:37:19 +0100 | [diff] [blame] | 1000 | struct wl_drm *wl_drm = |
| 1001 | dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm; |
Daniel Stone | 6595c69 | 2017-06-16 17:33:56 +0100 | [diff] [blame] | 1002 | int fd, stride; |
| 1003 | |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1004 | if (num_planes > 1) |
| 1005 | return NULL; |
| 1006 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1007 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd); |
| 1008 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride); |
Daniel Stone | cfaca57 | 2017-06-16 17:37:19 +0100 | [diff] [blame] | 1009 | ret = wl_drm_create_prime_buffer(wl_drm, fd, width, height, fourcc, 0, |
| 1010 | stride, 0, 0, 0, 0); |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 1011 | close(fd); |
| 1012 | } else { |
Daniel Stone | cfaca57 | 2017-06-16 17:37:19 +0100 | [diff] [blame] | 1013 | struct wl_drm *wl_drm = |
| 1014 | dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm; |
Daniel Stone | 6595c69 | 2017-06-16 17:33:56 +0100 | [diff] [blame] | 1015 | int name, stride; |
| 1016 | |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1017 | if (num_planes > 1) |
| 1018 | return NULL; |
| 1019 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1020 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name); |
| 1021 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride); |
Daniel Stone | cfaca57 | 2017-06-16 17:37:19 +0100 | [diff] [blame] | 1022 | ret = wl_drm_create_buffer(wl_drm, name, width, height, stride, fourcc); |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
Daniel Stone | c4a1c7a | 2017-06-16 17:29:42 +0100 | [diff] [blame] | 1025 | return ret; |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1028 | static EGLBoolean |
| 1029 | try_damage_buffer(struct dri2_egl_surface *dri2_surf, |
| 1030 | const EGLint *rects, |
| 1031 | EGLint n_rects) |
| 1032 | { |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1033 | if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper) |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1034 | < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) |
| 1035 | return EGL_FALSE; |
| 1036 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1037 | for (int i = 0; i < n_rects; i++) { |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1038 | const int *rect = &rects[i * 4]; |
| 1039 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1040 | wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper, |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1041 | rect[0], |
| 1042 | dri2_surf->base.Height - rect[1] - rect[3], |
| 1043 | rect[2], rect[3]); |
| 1044 | } |
| 1045 | return EGL_TRUE; |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1046 | } |
Eric Engestrom | 3900659 | 2020-05-23 01:44:52 +0200 | [diff] [blame] | 1047 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1048 | /** |
Eric Engestrom | cc03448 | 2020-07-20 13:38:24 +0200 | [diff] [blame] | 1049 | * Called via eglSwapBuffers(), drv->SwapBuffers(). |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1050 | */ |
| 1051 | static EGLBoolean |
Eric Engestrom | 82035b2 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1052 | dri2_wl_swap_buffers_with_damage(_EGLDisplay *disp, |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 1053 | _EGLSurface *draw, |
| 1054 | const EGLint *rects, |
| 1055 | EGLint n_rects) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1056 | { |
| 1057 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1058 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1059 | |
Daniel Stone | 9ca6711 | 2016-06-01 09:59:06 +0100 | [diff] [blame] | 1060 | while (dri2_surf->throttle_callback != NULL) |
| 1061 | if (wl_display_dispatch_queue(dri2_dpy->wl_dpy, |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1062 | dri2_surf->wl_queue) == -1) |
Daniel Stone | 9ca6711 | 2016-06-01 09:59:06 +0100 | [diff] [blame] | 1063 | return -1; |
| 1064 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1065 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1066 | if (dri2_surf->color_buffers[i].age > 0) |
| 1067 | dri2_surf->color_buffers[i].age++; |
| 1068 | |
Kristian Høgsberg | 1fe0073 | 2013-02-06 15:41:54 -0500 | [diff] [blame] | 1069 | /* Make sure we have a back buffer in case we're swapping without ever |
| 1070 | * rendering. */ |
Jonas Ådahl | 903ad59 | 2019-05-06 09:54:27 +0200 | [diff] [blame] | 1071 | if (update_buffers_if_needed(dri2_surf) < 0) |
Emil Velikov | c58af5c | 2017-06-20 15:40:28 +0100 | [diff] [blame] | 1072 | return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers"); |
Kristian Høgsberg | 1fe0073 | 2013-02-06 15:41:54 -0500 | [diff] [blame] | 1073 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1074 | if (draw->SwapInterval > 0) { |
| 1075 | dri2_surf->throttle_callback = |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1076 | wl_surface_frame(dri2_surf->wl_surface_wrapper); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1077 | wl_callback_add_listener(dri2_surf->throttle_callback, |
| 1078 | &throttle_listener, dri2_surf); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1079 | } |
Neil Roberts | 25cc889 | 2013-11-15 13:50:49 +0000 | [diff] [blame] | 1080 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1081 | dri2_surf->back->age = 1; |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 1082 | dri2_surf->current = dri2_surf->back; |
| 1083 | dri2_surf->back = NULL; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1084 | |
Daniel Stone | c4a1c7a | 2017-06-16 17:29:42 +0100 | [diff] [blame] | 1085 | if (!dri2_surf->current->wl_buffer) { |
| 1086 | __DRIimage *image; |
| 1087 | |
| 1088 | if (dri2_dpy->is_different_gpu) |
| 1089 | image = dri2_surf->current->linear_copy; |
| 1090 | else |
| 1091 | image = dri2_surf->current->dri_image; |
| 1092 | |
| 1093 | dri2_surf->current->wl_buffer = |
| 1094 | create_wl_buffer(dri2_dpy, dri2_surf, image); |
| 1095 | |
Juan A. Suarez Romero | 54a9622 | 2018-08-30 10:14:49 +0200 | [diff] [blame] | 1096 | dri2_surf->current->wl_release = false; |
| 1097 | |
Daniel Stone | c4a1c7a | 2017-06-16 17:29:42 +0100 | [diff] [blame] | 1098 | wl_buffer_add_listener(dri2_surf->current->wl_buffer, |
| 1099 | &wl_buffer_listener, dri2_surf); |
| 1100 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1101 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1102 | wl_surface_attach(dri2_surf->wl_surface_wrapper, |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 1103 | dri2_surf->current->wl_buffer, |
| 1104 | dri2_surf->dx, dri2_surf->dy); |
| 1105 | |
| 1106 | dri2_surf->wl_win->attached_width = dri2_surf->base.Width; |
| 1107 | dri2_surf->wl_win->attached_height = dri2_surf->base.Height; |
| 1108 | /* reset resize growing parameters */ |
| 1109 | dri2_surf->dx = 0; |
| 1110 | dri2_surf->dy = 0; |
| 1111 | |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1112 | /* If the compositor doesn't support damage_buffer, we deliberately |
| 1113 | * ignore the damage region and post maximum damage, due to |
Daniel Stone | d1314de | 2015-11-07 18:25:31 +0000 | [diff] [blame] | 1114 | * https://bugs.freedesktop.org/78190 */ |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1115 | if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects)) |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1116 | wl_surface_damage(dri2_surf->wl_surface_wrapper, |
Derek Foreman | d085a5d | 2016-02-16 10:34:39 -0600 | [diff] [blame] | 1117 | 0, 0, INT32_MAX, INT32_MAX); |
Kristian Høgsberg | 90804e8 | 2012-12-13 23:30:45 -0500 | [diff] [blame] | 1118 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1119 | if (dri2_dpy->is_different_gpu) { |
| 1120 | _EGLContext *ctx = _eglGetCurrentContext(); |
| 1121 | struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx); |
| 1122 | dri2_dpy->image->blitImage(dri2_ctx->dri_context, |
| 1123 | dri2_surf->current->linear_copy, |
| 1124 | dri2_surf->current->dri_image, |
| 1125 | 0, 0, dri2_surf->base.Width, |
| 1126 | dri2_surf->base.Height, |
| 1127 | 0, 0, dri2_surf->base.Width, |
| 1128 | dri2_surf->base.Height, 0); |
| 1129 | } |
| 1130 | |
Eric Anholt | 70e8ccc | 2014-12-21 11:51:33 -0800 | [diff] [blame] | 1131 | dri2_flush_drawable_for_swapbuffers(disp, draw); |
Boyan Ding | 868ae3e | 2015-11-25 13:27:02 +0800 | [diff] [blame] | 1132 | dri2_dpy->flush->invalidate(dri2_surf->dri_drawable); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1133 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1134 | wl_surface_commit(dri2_surf->wl_surface_wrapper); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* If we're not waiting for a frame callback then we'll at least throttle |
| 1137 | * to a sync callback so that we always give a chance for the compositor to |
| 1138 | * handle the commit and send a release event before checking for a free |
| 1139 | * buffer */ |
| 1140 | if (dri2_surf->throttle_callback == NULL) { |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1141 | dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1142 | wl_callback_add_listener(dri2_surf->throttle_callback, |
| 1143 | &throttle_listener, dri2_surf); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Axel Davy | 402bf6e | 2013-12-03 17:38:09 +0100 | [diff] [blame] | 1146 | wl_display_flush(dri2_dpy->wl_dpy); |
| 1147 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1148 | return EGL_TRUE; |
| 1149 | } |
| 1150 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1151 | static EGLint |
Eric Engestrom | 429e936 | 2018-04-22 16:48:15 +0200 | [diff] [blame^] | 1152 | dri2_wl_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface) |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1153 | { |
| 1154 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface); |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1155 | |
Jonas Ådahl | 903ad59 | 2019-05-06 09:54:27 +0200 | [diff] [blame] | 1156 | if (update_buffers_if_needed(dri2_surf) < 0) { |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1157 | _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age"); |
Tapani Pälli | 8fac894 | 2017-06-08 12:24:24 +0300 | [diff] [blame] | 1158 | return -1; |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return dri2_surf->back->age; |
| 1162 | } |
| 1163 | |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 1164 | static EGLBoolean |
Eric Engestrom | 82035b2 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1165 | dri2_wl_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw) |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 1166 | { |
Eric Engestrom | 82035b2 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1167 | return dri2_wl_swap_buffers_with_damage(disp, draw, NULL, 0); |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1170 | static struct wl_buffer * |
Eric Engestrom | f010568 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1171 | dri2_wl_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img) |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1172 | { |
| 1173 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1174 | struct dri2_egl_image *dri2_img = dri2_egl_image(img); |
| 1175 | __DRIimage *image = dri2_img->dri_image; |
| 1176 | struct wl_buffer *buffer; |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 1177 | int format, visual_idx; |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1178 | |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 1179 | /* Check the upstream display supports this buffer's format. */ |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1180 | dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format); |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 1181 | visual_idx = dri2_wl_visual_idx_from_dri_image_format(format); |
| 1182 | if (visual_idx == -1) |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1183 | goto bad_format; |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 1184 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 1185 | if (!BITSET_TEST(dri2_dpy->formats, visual_idx)) |
Daniel Stone | 3323ce7 | 2018-02-06 10:15:32 +0000 | [diff] [blame] | 1186 | goto bad_format; |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1187 | |
Daniel Stone | cfaca57 | 2017-06-16 17:37:19 +0100 | [diff] [blame] | 1188 | buffer = create_wl_buffer(dri2_dpy, NULL, image); |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1189 | |
| 1190 | /* The buffer object will have been created with our internal event queue |
Emil Velikov | 6a1b683 | 2017-08-27 11:20:30 +0100 | [diff] [blame] | 1191 | * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1192 | * buffer to be used by the application so we'll reset it to the display's |
Daniel Stone | cfaca57 | 2017-06-16 17:37:19 +0100 | [diff] [blame] | 1193 | * default event queue. This isn't actually racy, as the only event the |
| 1194 | * buffer can get is a buffer release, which doesn't happen with an explicit |
| 1195 | * attach. */ |
Neil Roberts | 5cddb1c | 2013-10-28 15:07:03 +0000 | [diff] [blame] | 1196 | if (buffer) |
| 1197 | wl_proxy_set_queue((struct wl_proxy *) buffer, NULL); |
| 1198 | |
| 1199 | return buffer; |
| 1200 | |
| 1201 | bad_format: |
| 1202 | _eglError(EGL_BAD_MATCH, "unsupported image format"); |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1206 | static int |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 1207 | dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id) |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1208 | { |
| 1209 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1210 | int ret = 0; |
| 1211 | |
Axel Davy | fb0960a | 2015-05-01 01:30:10 +0200 | [diff] [blame] | 1212 | if (dri2_dpy->is_render_node) { |
| 1213 | _eglLog(_EGL_WARNING, "wayland-egl: client asks server to " |
| 1214 | "authenticate for render-nodes"); |
| 1215 | return 0; |
| 1216 | } |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 1217 | dri2_dpy->authenticated = false; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1218 | |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1219 | wl_drm_authenticate(dri2_dpy->wl_drm, id); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1220 | if (roundtrip(dri2_dpy) < 0) |
| 1221 | ret = -1; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1222 | |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1223 | if (!dri2_dpy->authenticated) |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1224 | ret = -1; |
| 1225 | |
| 1226 | /* reset authenticated */ |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 1227 | dri2_dpy->authenticated = true; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1232 | static void |
| 1233 | drm_handle_device(void *data, struct wl_drm *drm, const char *device) |
| 1234 | { |
| 1235 | struct dri2_egl_display *dri2_dpy = data; |
| 1236 | drm_magic_t magic; |
| 1237 | |
| 1238 | dri2_dpy->device_name = strdup(device); |
| 1239 | if (!dri2_dpy->device_name) |
| 1240 | return; |
| 1241 | |
Derek Foreman | 4f8f790 | 2015-06-17 11:28:51 -0500 | [diff] [blame] | 1242 | dri2_dpy->fd = loader_open_device(dri2_dpy->device_name); |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1243 | if (dri2_dpy->fd == -1) { |
| 1244 | _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)", |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 1245 | dri2_dpy->device_name, strerror(errno)); |
Emil Velikov | ce74a7b | 2018-11-27 11:36:01 +0000 | [diff] [blame] | 1246 | free(dri2_dpy->device_name); |
Nicolai Hähnle | c02390f | 2018-11-28 18:30:36 +0100 | [diff] [blame] | 1247 | dri2_dpy->device_name = NULL; |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1248 | return; |
| 1249 | } |
| 1250 | |
Emil Velikov | 175d975 | 2015-07-10 12:27:06 +0100 | [diff] [blame] | 1251 | if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) { |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 1252 | dri2_dpy->authenticated = true; |
Axel Davy | fb0960a | 2015-05-01 01:30:10 +0200 | [diff] [blame] | 1253 | } else { |
Emil Velikov | c59d3aa | 2018-11-23 12:55:38 +0000 | [diff] [blame] | 1254 | if (drmGetMagic(dri2_dpy->fd, &magic)) { |
| 1255 | close(dri2_dpy->fd); |
| 1256 | dri2_dpy->fd = -1; |
| 1257 | free(dri2_dpy->device_name); |
Nicolai Hähnle | c02390f | 2018-11-28 18:30:36 +0100 | [diff] [blame] | 1258 | dri2_dpy->device_name = NULL; |
Emil Velikov | c59d3aa | 2018-11-23 12:55:38 +0000 | [diff] [blame] | 1259 | _eglLog(_EGL_WARNING, "wayland-egl: drmGetMagic failed"); |
| 1260 | return; |
| 1261 | } |
Axel Davy | fb0960a | 2015-05-01 01:30:10 +0200 | [diff] [blame] | 1262 | wl_drm_authenticate(dri2_dpy->wl_drm, magic); |
| 1263 | } |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static void |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1267 | drm_handle_format(void *data, struct wl_drm *drm, uint32_t format) |
| 1268 | { |
| 1269 | struct dri2_egl_display *dri2_dpy = data; |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 1270 | int visual_idx = dri2_wl_visual_idx_from_fourcc(format); |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1271 | |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 1272 | if (visual_idx == -1) |
| 1273 | return; |
| 1274 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 1275 | BITSET_SET(dri2_dpy->formats, visual_idx); |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static void |
Kristian Høgsberg | de315f7 | 2013-02-02 12:26:12 -0500 | [diff] [blame] | 1279 | drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value) |
| 1280 | { |
| 1281 | struct dri2_egl_display *dri2_dpy = data; |
| 1282 | |
| 1283 | dri2_dpy->capabilities = value; |
| 1284 | } |
| 1285 | |
| 1286 | static void |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1287 | drm_handle_authenticated(void *data, struct wl_drm *drm) |
| 1288 | { |
| 1289 | struct dri2_egl_display *dri2_dpy = data; |
| 1290 | |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 1291 | dri2_dpy->authenticated = true; |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | static const struct wl_drm_listener drm_listener = { |
Emil Velikov | 5567494 | 2015-07-10 12:24:11 +0100 | [diff] [blame] | 1295 | .device = drm_handle_device, |
| 1296 | .format = drm_handle_format, |
| 1297 | .authenticated = drm_handle_authenticated, |
| 1298 | .capabilities = drm_handle_capabilities |
Kristian Høgsberg | c0f8c99 | 2011-04-14 10:42:41 -0400 | [diff] [blame] | 1299 | }; |
| 1300 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1301 | static void |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1302 | dmabuf_ignore_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf, |
| 1303 | uint32_t format) |
| 1304 | { |
| 1305 | /* formats are implicitly advertised by the 'modifier' event, so ignore */ |
| 1306 | } |
| 1307 | |
| 1308 | static void |
| 1309 | dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf, |
| 1310 | uint32_t format, uint32_t modifier_hi, |
| 1311 | uint32_t modifier_lo) |
| 1312 | { |
| 1313 | struct dri2_egl_display *dri2_dpy = data; |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 1314 | int visual_idx = dri2_wl_visual_idx_from_fourcc(format); |
Daniel Stone | 4732094 | 2018-02-06 10:29:13 +0000 | [diff] [blame] | 1315 | uint64_t *mod; |
| 1316 | |
| 1317 | if (visual_idx == -1) |
| 1318 | return; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1319 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 1320 | BITSET_SET(dri2_dpy->formats, visual_idx); |
Daniel Stone | 4732094 | 2018-02-06 10:29:13 +0000 | [diff] [blame] | 1321 | |
| 1322 | mod = u_vector_add(&dri2_dpy->wl_modifiers[visual_idx]); |
Eric Engestrom | 2de9e84 | 2018-08-16 15:22:46 +0100 | [diff] [blame] | 1323 | *mod = combine_u32_into_u64(modifier_hi, modifier_lo); |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = { |
| 1327 | .format = dmabuf_ignore_format, |
| 1328 | .modifier = dmabuf_handle_modifier, |
| 1329 | }; |
| 1330 | |
| 1331 | static void |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 1332 | registry_handle_global_drm(void *data, struct wl_registry *registry, |
| 1333 | uint32_t name, const char *interface, |
| 1334 | uint32_t version) |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1335 | { |
| 1336 | struct dri2_egl_display *dri2_dpy = data; |
| 1337 | |
| 1338 | if (strcmp(interface, "wl_drm") == 0) { |
| 1339 | dri2_dpy->wl_drm = |
Daniel Stone | 5295df6 | 2017-06-10 14:54:44 +0100 | [diff] [blame] | 1340 | wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2)); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1341 | wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy); |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1342 | } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) { |
| 1343 | dri2_dpy->wl_dmabuf = |
| 1344 | wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface, |
| 1345 | MIN2(version, 3)); |
| 1346 | zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener, |
| 1347 | dri2_dpy); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
Kristian Høgsberg | 712269d | 2013-06-18 16:53:46 -0400 | [diff] [blame] | 1351 | static void |
| 1352 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 1353 | uint32_t name) |
Kristian Høgsberg | 712269d | 2013-06-18 16:53:46 -0400 | [diff] [blame] | 1354 | { |
| 1355 | } |
| 1356 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1357 | static const struct wl_registry_listener registry_listener_drm = { |
Emil Velikov | 5567494 | 2015-07-10 12:24:11 +0100 | [diff] [blame] | 1358 | .global = registry_handle_global_drm, |
| 1359 | .global_remove = registry_handle_global_remove |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1360 | }; |
| 1361 | |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1362 | static void |
Emil Velikov | 47b06f5 | 2017-08-05 00:25:46 +0100 | [diff] [blame] | 1363 | dri2_wl_setup_swap_interval(_EGLDisplay *disp) |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1364 | { |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1365 | /* We can't use values greater than 1 on Wayland because we are using the |
| 1366 | * frame callback to synchronise the frame and the only way we be sure to |
| 1367 | * get a frame callback is to attach a new buffer. Therefore we can't just |
| 1368 | * sit drawing nothing to wait until the next ‘n’ frame callbacks */ |
| 1369 | |
Emil Velikov | 47b06f5 | 2017-08-05 00:25:46 +0100 | [diff] [blame] | 1370 | dri2_setup_swap_interval(disp, 1); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Emil Velikov | 79d1fb9 | 2017-05-15 16:14:17 +0100 | [diff] [blame] | 1373 | static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = { |
Chad Versace | d019cd8 | 2014-01-28 12:47:38 -0800 | [diff] [blame] | 1374 | .authenticate = dri2_wl_authenticate, |
Chad Versace | 0a0c881 | 2014-01-28 16:39:09 -0800 | [diff] [blame] | 1375 | .create_window_surface = dri2_wl_create_window_surface, |
Chad Versace | 9a40ee1 | 2014-02-09 09:13:27 -0800 | [diff] [blame] | 1376 | .create_pixmap_surface = dri2_wl_create_pixmap_surface, |
Chad Versace | 958dd80 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1377 | .destroy_surface = dri2_wl_destroy_surface, |
Chad Versace | eef68a9 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1378 | .create_image = dri2_create_image_khr, |
Chad Versace | ad173bc | 2014-01-28 16:21:21 -0800 | [diff] [blame] | 1379 | .swap_buffers = dri2_wl_swap_buffers, |
Chad Versace | d03948a | 2014-01-28 16:26:44 -0800 | [diff] [blame] | 1380 | .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage, |
Chad Versace | 3fdfbd2 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1381 | .query_buffer_age = dri2_wl_query_buffer_age, |
Chad Versace | eadd5e0 | 2014-01-28 17:03:03 -0800 | [diff] [blame] | 1382 | .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1383 | .get_dri_drawable = dri2_surface_get_dri_drawable, |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1384 | }; |
| 1385 | |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1386 | static const __DRIextension *dri2_loader_extensions[] = { |
| 1387 | &dri2_loader_extension.base, |
Derek Foreman | 534ea2b | 2017-01-10 15:21:47 -0600 | [diff] [blame] | 1388 | &image_loader_extension.base, |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1389 | &image_lookup_extension.base, |
| 1390 | &use_invalidate.base, |
| 1391 | NULL, |
| 1392 | }; |
| 1393 | |
| 1394 | static const __DRIextension *image_loader_extensions[] = { |
| 1395 | &image_loader_extension.base, |
| 1396 | &image_lookup_extension.base, |
| 1397 | &use_invalidate.base, |
| 1398 | NULL, |
| 1399 | }; |
| 1400 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1401 | static EGLBoolean |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1402 | dri2_wl_add_configs_for_visuals(_EGLDisplay *disp) |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1403 | { |
| 1404 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
Daniel Stone | 19cbca3 | 2018-02-06 09:32:22 +0000 | [diff] [blame] | 1405 | unsigned int format_count[ARRAY_SIZE(dri2_wl_visuals)] = { 0 }; |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1406 | unsigned int count = 0; |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 1407 | bool assigned; |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1408 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1409 | for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) { |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 1410 | assigned = false; |
| 1411 | |
Daniel Stone | 19cbca3 | 2018-02-06 09:32:22 +0000 | [diff] [blame] | 1412 | for (unsigned j = 0; j < ARRAY_SIZE(dri2_wl_visuals); j++) { |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1413 | struct dri2_egl_config *dri2_conf; |
| 1414 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 1415 | if (!BITSET_TEST(dri2_dpy->formats, j)) |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1416 | continue; |
| 1417 | |
| 1418 | dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i], |
Kevin Strasser | 7b4ed2b | 2019-01-24 16:32:48 -0800 | [diff] [blame] | 1419 | count + 1, EGL_WINDOW_BIT, NULL, dri2_wl_visuals[j].rgba_shifts, dri2_wl_visuals[j].rgba_sizes); |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1420 | if (dri2_conf) { |
Eric Engestrom | c87f737 | 2017-06-21 21:55:56 +0100 | [diff] [blame] | 1421 | if (dri2_conf->base.ConfigID == count + 1) |
| 1422 | count++; |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1423 | format_count[j]++; |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 1424 | assigned = true; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | if (!assigned && dri2_dpy->is_different_gpu) { |
| 1429 | struct dri2_egl_config *dri2_conf; |
| 1430 | int alt_dri_image_format, c, s; |
| 1431 | |
| 1432 | /* No match for config. Try if we can blitImage convert to a visual */ |
| 1433 | c = dri2_wl_visual_idx_from_config(dri2_dpy, |
| 1434 | dri2_dpy->driver_configs[i]); |
| 1435 | |
| 1436 | if (c == -1) |
| 1437 | continue; |
| 1438 | |
| 1439 | /* Find optimal target visual for blitImage conversion, if any. */ |
| 1440 | alt_dri_image_format = dri2_wl_visuals[c].alt_dri_image_format; |
| 1441 | s = dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format); |
| 1442 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 1443 | if (s == -1 || !BITSET_TEST(dri2_dpy->formats, s)) |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 1444 | continue; |
| 1445 | |
| 1446 | /* Visual s works for the Wayland server, and c can be converted into s |
| 1447 | * by our client gpu during PRIME blitImage conversion to a linear |
| 1448 | * wl_buffer, so add visual c as supported by the client renderer. |
| 1449 | */ |
| 1450 | dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i], |
| 1451 | count + 1, EGL_WINDOW_BIT, NULL, |
Kevin Strasser | 7b4ed2b | 2019-01-24 16:32:48 -0800 | [diff] [blame] | 1452 | dri2_wl_visuals[c].rgba_shifts, |
| 1453 | dri2_wl_visuals[c].rgba_sizes); |
Mario Kleiner | a34b0d6 | 2018-06-13 06:04:14 +0200 | [diff] [blame] | 1454 | if (dri2_conf) { |
| 1455 | if (dri2_conf->base.ConfigID == count + 1) |
| 1456 | count++; |
| 1457 | format_count[c]++; |
| 1458 | if (format_count[c] == 1) |
| 1459 | _eglLog(_EGL_DEBUG, "Client format %s to server format %s via " |
| 1460 | "PRIME blitImage.", dri2_wl_visuals[c].format_name, |
| 1461 | dri2_wl_visuals[s].format_name); |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1466 | for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) { |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1467 | if (!format_count[i]) { |
| 1468 | _eglLog(_EGL_DEBUG, "No DRI config supports native format %s", |
Daniel Stone | 19cbca3 | 2018-02-06 09:32:22 +0000 | [diff] [blame] | 1469 | dri2_wl_visuals[i].format_name); |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | return (count != 0); |
| 1474 | } |
| 1475 | |
| 1476 | static EGLBoolean |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1477 | dri2_initialize_wayland_drm(_EGLDisplay *disp) |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1478 | { |
Emil Velikov | 0099270 | 2018-09-04 11:20:03 +0100 | [diff] [blame] | 1479 | _EGLDevice *dev; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1480 | struct dri2_egl_display *dri2_dpy; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1481 | |
Matt Turner | 6bda027 | 2012-09-04 23:09:22 -0700 | [diff] [blame] | 1482 | dri2_dpy = calloc(1, sizeof *dri2_dpy); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1483 | if (!dri2_dpy) |
| 1484 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 1485 | |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1486 | dri2_dpy->fd = -1; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1487 | disp->DriverData = (void *) dri2_dpy; |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 1488 | if (disp->PlatformDisplay == NULL) { |
| 1489 | dri2_dpy->wl_dpy = wl_display_connect(NULL); |
| 1490 | if (dri2_dpy->wl_dpy == NULL) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1491 | goto cleanup; |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 1492 | dri2_dpy->own_device = true; |
Benjamin Franzke | b8325fd | 2011-06-11 22:07:02 +0200 | [diff] [blame] | 1493 | } else { |
| 1494 | dri2_dpy->wl_dpy = disp->PlatformDisplay; |
| 1495 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1496 | |
Daniel Stone | 4732094 | 2018-02-06 10:29:13 +0000 | [diff] [blame] | 1497 | dri2_dpy->wl_modifiers = |
| 1498 | calloc(ARRAY_SIZE(dri2_wl_visuals), sizeof(*dri2_dpy->wl_modifiers)); |
| 1499 | if (!dri2_dpy->wl_modifiers) |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1500 | goto cleanup; |
Daniel Stone | 4732094 | 2018-02-06 10:29:13 +0000 | [diff] [blame] | 1501 | for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) { |
| 1502 | if (!u_vector_init(&dri2_dpy->wl_modifiers[i], sizeof(uint64_t), 32)) |
| 1503 | goto cleanup; |
Daniel Stone | 02cc359 | 2017-06-16 18:01:23 +0100 | [diff] [blame] | 1504 | } |
| 1505 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1506 | 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] | 1507 | |
Jonas Ådahl | 36b9976 | 2017-01-13 23:05:10 +0800 | [diff] [blame] | 1508 | dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy); |
| 1509 | if (dri2_dpy->wl_dpy_wrapper == NULL) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1510 | goto cleanup; |
Jonas Ådahl | 36b9976 | 2017-01-13 23:05:10 +0800 | [diff] [blame] | 1511 | |
| 1512 | wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper, |
| 1513 | dri2_dpy->wl_queue); |
| 1514 | |
Kristian Høgsberg | 112ccfa | 2013-02-26 12:49:40 -0500 | [diff] [blame] | 1515 | if (dri2_dpy->own_device) |
| 1516 | wl_display_dispatch_pending(dri2_dpy->wl_dpy); |
| 1517 | |
Jonas Ådahl | 36b9976 | 2017-01-13 23:05:10 +0800 | [diff] [blame] | 1518 | dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1519 | wl_registry_add_listener(dri2_dpy->wl_registry, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1520 | ®istry_listener_drm, dri2_dpy); |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1521 | if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1522 | goto cleanup; |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1523 | |
| 1524 | if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1525 | goto cleanup; |
Kristian Høgsberg | 56758c8 | 2011-02-04 15:37:51 -0500 | [diff] [blame] | 1526 | |
Kristian Høgsberg | 0229e3a | 2012-10-10 22:10:42 -0400 | [diff] [blame] | 1527 | if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1528 | goto cleanup; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1529 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1530 | dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd, |
| 1531 | &dri2_dpy->is_different_gpu); |
Emil Velikov | 0099270 | 2018-09-04 11:20:03 +0100 | [diff] [blame] | 1532 | dev = _eglAddDevice(dri2_dpy->fd, false); |
| 1533 | if (!dev) { |
| 1534 | _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice"); |
| 1535 | goto cleanup; |
| 1536 | } |
| 1537 | |
| 1538 | disp->Device = dev; |
| 1539 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1540 | if (dri2_dpy->is_different_gpu) { |
| 1541 | free(dri2_dpy->device_name); |
| 1542 | dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd); |
| 1543 | if (!dri2_dpy->device_name) { |
| 1544 | _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name " |
| 1545 | "for requested GPU"); |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1546 | goto cleanup; |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | /* we have to do the check now, because loader_get_user_preferred_fd |
| 1551 | * will return a render-node when the requested gpu is different |
| 1552 | * to the server, but also if the client asks for the same gpu than |
| 1553 | * the server by requesting its pci-id */ |
Emil Velikov | 175d975 | 2015-07-10 12:27:06 +0100 | [diff] [blame] | 1554 | dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER; |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1555 | |
Emil Velikov | af7abc5 | 2016-09-12 17:48:18 +0100 | [diff] [blame] | 1556 | dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1557 | if (dri2_dpy->driver_name == NULL) { |
| 1558 | _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name"); |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1559 | goto cleanup; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
Axel Davy | fb0960a | 2015-05-01 01:30:10 +0200 | [diff] [blame] | 1562 | /* render nodes cannot use Gem names, and thus do not support |
| 1563 | * the __DRI_DRI2_LOADER extension */ |
Emil Velikov | 5cb16e0 | 2017-05-11 22:49:04 +0100 | [diff] [blame] | 1564 | if (!dri2_dpy->is_render_node) { |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1565 | dri2_dpy->loader_extensions = dri2_loader_extensions; |
Emil Velikov | 5cb16e0 | 2017-05-11 22:49:04 +0100 | [diff] [blame] | 1566 | if (!dri2_load_driver(disp)) { |
| 1567 | _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver"); |
| 1568 | goto cleanup; |
| 1569 | } |
| 1570 | } else { |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 1571 | dri2_dpy->loader_extensions = image_loader_extensions; |
Emil Velikov | 5cb16e0 | 2017-05-11 22:49:04 +0100 | [diff] [blame] | 1572 | if (!dri2_load_driver_dri3(disp)) { |
| 1573 | _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver"); |
| 1574 | goto cleanup; |
| 1575 | } |
| 1576 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1577 | |
| 1578 | if (!dri2_create_screen(disp)) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1579 | goto cleanup; |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1580 | |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 1581 | if (!dri2_setup_extensions(disp)) |
| 1582 | goto cleanup; |
| 1583 | |
| 1584 | dri2_setup_screen(disp); |
| 1585 | |
Emil Velikov | 47b06f5 | 2017-08-05 00:25:46 +0100 | [diff] [blame] | 1586 | dri2_wl_setup_swap_interval(disp); |
Neil Roberts | 992a2db | 2013-11-15 13:50:50 +0000 | [diff] [blame] | 1587 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1588 | /* To use Prime, we must have _DRI_IMAGE v7 at least. |
| 1589 | * createImageFromFds support indicates that Prime export/import |
| 1590 | * is supported by the driver. Fall back to |
| 1591 | * gem names if we don't have Prime support. */ |
Kristian Høgsberg | 1670737 | 2013-03-19 13:20:36 -0400 | [diff] [blame] | 1592 | |
| 1593 | if (dri2_dpy->image->base.version < 7 || |
| 1594 | dri2_dpy->image->createImageFromFds == NULL) |
Neil Roberts | 63d4661 | 2014-04-08 23:28:40 +0300 | [diff] [blame] | 1595 | dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME; |
Kristian Høgsberg | 1670737 | 2013-03-19 13:20:36 -0400 | [diff] [blame] | 1596 | |
Axel Davy | fb0960a | 2015-05-01 01:30:10 +0200 | [diff] [blame] | 1597 | /* We cannot use Gem names with render-nodes, only prime fds (dma-buf). |
| 1598 | * The server needs to accept them */ |
| 1599 | if (dri2_dpy->is_render_node && |
| 1600 | !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) { |
| 1601 | _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable"); |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1602 | goto cleanup; |
Axel Davy | fb0960a | 2015-05-01 01:30:10 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1605 | if (dri2_dpy->is_different_gpu && |
| 1606 | (dri2_dpy->image->base.version < 9 || |
| 1607 | dri2_dpy->image->blitImage == NULL)) { |
| 1608 | _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the " |
| 1609 | "Image extension in the driver is not " |
| 1610 | "compatible. Version 9 or later and blitImage() " |
| 1611 | "are required"); |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1612 | goto cleanup; |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1615 | if (!dri2_wl_add_configs_for_visuals(disp)) { |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 1616 | _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs"); |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1617 | goto cleanup; |
Kristian Høgsberg | 7b1d94e | 2011-08-31 16:45:04 -0400 | [diff] [blame] | 1618 | } |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1619 | |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1620 | dri2_set_WL_bind_wayland_display(disp); |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1621 | /* When cannot convert EGLImage to wl_buffer when on a different gpu, |
| 1622 | * because the buffer of the EGLImage has likely a tiling mode the server |
| 1623 | * gpu won't support. These is no way to check for now. Thus do not support the |
| 1624 | * extension */ |
Emil Velikov | 83a792c | 2017-05-15 16:14:16 +0100 | [diff] [blame] | 1625 | if (!dri2_dpy->is_different_gpu) |
Axel Davy | 4cd546d | 2015-05-01 01:16:24 +0200 | [diff] [blame] | 1626 | disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE; |
Emil Velikov | 83a792c | 2017-05-15 16:14:16 +0100 | [diff] [blame] | 1627 | |
Kristian Høgsberg | 6d4d4b0 | 2012-12-13 23:32:14 -0500 | [diff] [blame] | 1628 | disp->Extensions.EXT_buffer_age = EGL_TRUE; |
Benjamin Franzke | 6b369c4 | 2011-02-21 16:22:34 +0100 | [diff] [blame] | 1629 | |
Robert Bragg | f8c3242 | 2012-02-10 16:59:31 +0000 | [diff] [blame] | 1630 | disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE; |
| 1631 | |
Chad Versace | 90502b1 | 2014-01-28 11:41:46 -0800 | [diff] [blame] | 1632 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 1633 | * initialization. |
| 1634 | */ |
| 1635 | dri2_dpy->vtbl = &dri2_wl_display_vtbl; |
| 1636 | |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1637 | return EGL_TRUE; |
| 1638 | |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 1639 | cleanup: |
| 1640 | dri2_display_destroy(disp); |
Benjamin Franzke | 93aea84 | 2011-02-04 12:39:40 +0100 | [diff] [blame] | 1641 | return EGL_FALSE; |
| 1642 | } |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1643 | |
| 1644 | static int |
| 1645 | dri2_wl_swrast_get_stride_for_format(int format, int w) |
| 1646 | { |
Daniel Stone | d32b23f | 2018-02-06 11:58:45 +0000 | [diff] [blame] | 1647 | int visual_idx = dri2_wl_visual_idx_from_shm_format(format); |
| 1648 | |
Eric Engestrom | 65dda6c | 2018-02-15 11:10:22 +0000 | [diff] [blame] | 1649 | assume(visual_idx != -1); |
| 1650 | |
Daniel Stone | d32b23f | 2018-02-06 11:58:45 +0000 | [diff] [blame] | 1651 | return w * (dri2_wl_visuals[visual_idx].bpp / 8); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1654 | static EGLBoolean |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1655 | dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1656 | int format, int w, int h, |
| 1657 | void **data, int *size, |
| 1658 | struct wl_buffer **buffer) |
| 1659 | { |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1660 | struct dri2_egl_display *dri2_dpy = |
| 1661 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1662 | struct wl_shm_pool *pool; |
| 1663 | int fd, stride, size_map; |
| 1664 | void *data_map; |
| 1665 | |
| 1666 | stride = dri2_wl_swrast_get_stride_for_format(format, w); |
| 1667 | size_map = h * stride; |
| 1668 | |
Eric Engestrom | 882ed53 | 2018-08-10 12:37:17 +0100 | [diff] [blame] | 1669 | /* Create a shareable buffer */ |
Greg V | c0376a1 | 2018-01-18 23:29:14 +0300 | [diff] [blame] | 1670 | fd = os_create_anonymous_file(size_map, NULL); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1671 | if (fd < 0) |
| 1672 | return EGL_FALSE; |
| 1673 | |
| 1674 | data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 1675 | if (data_map == MAP_FAILED) { |
| 1676 | close(fd); |
| 1677 | return EGL_FALSE; |
| 1678 | } |
| 1679 | |
| 1680 | /* Share it in a wl_buffer */ |
| 1681 | pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map); |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1682 | wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1683 | *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format); |
| 1684 | wl_shm_pool_destroy(pool); |
| 1685 | close(fd); |
| 1686 | |
| 1687 | *data = data_map; |
| 1688 | *size = size_map; |
| 1689 | return EGL_TRUE; |
| 1690 | } |
| 1691 | |
| 1692 | static int |
| 1693 | swrast_update_buffers(struct dri2_egl_surface *dri2_surf) |
| 1694 | { |
| 1695 | struct dri2_egl_display *dri2_dpy = |
| 1696 | dri2_egl_display(dri2_surf->base.Resource.Display); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1697 | |
| 1698 | /* we need to do the following operations only once per frame */ |
| 1699 | if (dri2_surf->back) |
| 1700 | return 0; |
| 1701 | |
Olivier Fourdan | 55af17f | 2018-10-25 14:48:15 +0200 | [diff] [blame] | 1702 | if (dri2_surf->base.Width != dri2_surf->wl_win->width || |
| 1703 | dri2_surf->base.Height != dri2_surf->wl_win->height) { |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1704 | |
| 1705 | dri2_wl_release_buffers(dri2_surf); |
| 1706 | |
| 1707 | dri2_surf->base.Width = dri2_surf->wl_win->width; |
| 1708 | dri2_surf->base.Height = dri2_surf->wl_win->height; |
| 1709 | dri2_surf->dx = dri2_surf->wl_win->dx; |
| 1710 | dri2_surf->dy = dri2_surf->wl_win->dy; |
| 1711 | dri2_surf->current = NULL; |
| 1712 | } |
| 1713 | |
| 1714 | /* find back buffer */ |
| 1715 | |
Daniel Stone | 9ca6711 | 2016-06-01 09:59:06 +0100 | [diff] [blame] | 1716 | /* There might be a buffer release already queued that wasn't processed */ |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1717 | wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1718 | |
| 1719 | /* try get free buffer already created */ |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1720 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1721 | if (!dri2_surf->color_buffers[i].locked && |
| 1722 | dri2_surf->color_buffers[i].wl_buffer) { |
| 1723 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
| 1724 | break; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | /* else choose any another free location */ |
| 1729 | if (!dri2_surf->back) { |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1730 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1731 | if (!dri2_surf->color_buffers[i].locked) { |
| 1732 | dri2_surf->back = &dri2_surf->color_buffers[i]; |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1733 | if (!dri2_wl_swrast_allocate_buffer(dri2_surf, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1734 | dri2_surf->format, |
| 1735 | dri2_surf->base.Width, |
| 1736 | dri2_surf->base.Height, |
| 1737 | &dri2_surf->back->data, |
| 1738 | &dri2_surf->back->data_size, |
| 1739 | &dri2_surf->back->wl_buffer)) { |
| 1740 | _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer"); |
| 1741 | return -1; |
| 1742 | } |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1743 | wl_buffer_add_listener(dri2_surf->back->wl_buffer, |
| 1744 | &wl_buffer_listener, dri2_surf); |
| 1745 | break; |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | if (!dri2_surf->back) { |
| 1751 | _eglError(EGL_BAD_ALLOC, "failed to find free buffer"); |
| 1752 | return -1; |
| 1753 | } |
| 1754 | |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 1755 | dri2_surf->back->locked = true; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1756 | |
| 1757 | /* If we have an extra unlocked buffer at this point, we had to do triple |
| 1758 | * buffering for a while, but now can go back to just double buffering. |
| 1759 | * That means we can free any unlocked buffer now. */ |
Chad Versace | 98497df | 2017-06-22 11:00:40 -0700 | [diff] [blame] | 1760 | for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) { |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1761 | if (!dri2_surf->color_buffers[i].locked && |
| 1762 | dri2_surf->color_buffers[i].wl_buffer) { |
| 1763 | wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer); |
| 1764 | munmap(dri2_surf->color_buffers[i].data, |
| 1765 | dri2_surf->color_buffers[i].data_size); |
| 1766 | dri2_surf->color_buffers[i].wl_buffer = NULL; |
| 1767 | dri2_surf->color_buffers[i].data = NULL; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | static void* |
| 1775 | dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf) |
| 1776 | { |
| 1777 | /* if there has been a resize: */ |
| 1778 | if (!dri2_surf->current) |
| 1779 | return NULL; |
| 1780 | |
| 1781 | return dri2_surf->current->data; |
| 1782 | } |
| 1783 | |
| 1784 | static void* |
| 1785 | dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf) |
| 1786 | { |
| 1787 | assert(dri2_surf->back); |
| 1788 | return dri2_surf->back->data; |
| 1789 | } |
| 1790 | |
| 1791 | static void |
| 1792 | dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf) |
| 1793 | { |
| 1794 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display); |
| 1795 | |
Daniel Stone | 9ca6711 | 2016-06-01 09:59:06 +0100 | [diff] [blame] | 1796 | while (dri2_surf->throttle_callback != NULL) |
| 1797 | if (wl_display_dispatch_queue(dri2_dpy->wl_dpy, |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1798 | dri2_surf->wl_queue) == -1) |
Emil Velikov | 6ff948e | 2016-11-11 16:27:59 +0000 | [diff] [blame] | 1799 | return; |
Daniel Stone | 9ca6711 | 2016-06-01 09:59:06 +0100 | [diff] [blame] | 1800 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1801 | if (dri2_surf->base.SwapInterval > 0) { |
| 1802 | dri2_surf->throttle_callback = |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1803 | wl_surface_frame(dri2_surf->wl_surface_wrapper); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1804 | wl_callback_add_listener(dri2_surf->throttle_callback, |
| 1805 | &throttle_listener, dri2_surf); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | dri2_surf->current = dri2_surf->back; |
| 1809 | dri2_surf->back = NULL; |
| 1810 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1811 | wl_surface_attach(dri2_surf->wl_surface_wrapper, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1812 | dri2_surf->current->wl_buffer, |
| 1813 | dri2_surf->dx, dri2_surf->dy); |
| 1814 | |
| 1815 | dri2_surf->wl_win->attached_width = dri2_surf->base.Width; |
| 1816 | dri2_surf->wl_win->attached_height = dri2_surf->base.Height; |
| 1817 | /* reset resize growing parameters */ |
| 1818 | dri2_surf->dx = 0; |
| 1819 | dri2_surf->dy = 0; |
| 1820 | |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1821 | wl_surface_damage(dri2_surf->wl_surface_wrapper, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1822 | 0, 0, INT32_MAX, INT32_MAX); |
Daniel Stone | 03dd9a8 | 2017-05-05 14:49:09 +0100 | [diff] [blame] | 1823 | wl_surface_commit(dri2_surf->wl_surface_wrapper); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1824 | |
| 1825 | /* If we're not waiting for a frame callback then we'll at least throttle |
| 1826 | * to a sync callback so that we always give a chance for the compositor to |
| 1827 | * handle the commit and send a release event before checking for a free |
| 1828 | * buffer */ |
| 1829 | if (dri2_surf->throttle_callback == NULL) { |
Derek Foreman | aa18a63 | 2018-03-22 10:20:43 -0500 | [diff] [blame] | 1830 | dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1831 | wl_callback_add_listener(dri2_surf->throttle_callback, |
| 1832 | &throttle_listener, dri2_surf); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | wl_display_flush(dri2_dpy->wl_dpy); |
| 1836 | } |
| 1837 | |
| 1838 | static void |
| 1839 | dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw, |
| 1840 | int *x, int *y, int *w, int *h, |
| 1841 | void *loaderPrivate) |
| 1842 | { |
| 1843 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 1844 | |
| 1845 | (void) swrast_update_buffers(dri2_surf); |
| 1846 | *x = 0; |
| 1847 | *y = 0; |
| 1848 | *w = dri2_surf->base.Width; |
| 1849 | *h = dri2_surf->base.Height; |
| 1850 | } |
| 1851 | |
| 1852 | static void |
| 1853 | dri2_wl_swrast_get_image(__DRIdrawable * read, |
| 1854 | int x, int y, int w, int h, |
| 1855 | char *data, void *loaderPrivate) |
| 1856 | { |
| 1857 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 1858 | int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w); |
| 1859 | int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x); |
| 1860 | int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width); |
| 1861 | int dst_stride = copy_width; |
| 1862 | char *src, *dst; |
| 1863 | |
| 1864 | src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf); |
| 1865 | if (!src) { |
| 1866 | memset(data, 0, copy_width * h); |
| 1867 | return; |
| 1868 | } |
| 1869 | |
| 1870 | assert(data != src); |
| 1871 | assert(copy_width <= src_stride); |
| 1872 | |
| 1873 | src += x_offset; |
| 1874 | src += y * src_stride; |
| 1875 | dst = data; |
| 1876 | |
| 1877 | if (copy_width > src_stride-x_offset) |
| 1878 | copy_width = src_stride-x_offset; |
| 1879 | if (h > dri2_surf->base.Height-y) |
| 1880 | h = dri2_surf->base.Height-y; |
| 1881 | |
| 1882 | for (; h>0; h--) { |
| 1883 | memcpy(dst, src, copy_width); |
| 1884 | src += src_stride; |
| 1885 | dst += dst_stride; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | static void |
| 1890 | dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op, |
| 1891 | int x, int y, int w, int h, int stride, |
| 1892 | char *data, void *loaderPrivate) |
| 1893 | { |
| 1894 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 1895 | int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w); |
| 1896 | int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width); |
| 1897 | int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x); |
| 1898 | char *src, *dst; |
| 1899 | |
| 1900 | assert(copy_width <= stride); |
| 1901 | |
| 1902 | (void) swrast_update_buffers(dri2_surf); |
| 1903 | dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf); |
| 1904 | |
| 1905 | /* partial copy, copy old content */ |
| 1906 | if (copy_width < dst_stride) |
| 1907 | dri2_wl_swrast_get_image(draw, 0, 0, |
| 1908 | dri2_surf->base.Width, dri2_surf->base.Height, |
| 1909 | dst, loaderPrivate); |
| 1910 | |
| 1911 | dst += x_offset; |
| 1912 | dst += y * dst_stride; |
| 1913 | |
| 1914 | src = data; |
| 1915 | |
| 1916 | /* drivers expect we do these checks (and some rely on it) */ |
| 1917 | if (copy_width > dst_stride-x_offset) |
| 1918 | copy_width = dst_stride-x_offset; |
| 1919 | if (h > dri2_surf->base.Height-y) |
| 1920 | h = dri2_surf->base.Height-y; |
| 1921 | |
| 1922 | for (; h>0; h--) { |
| 1923 | memcpy(dst, src, copy_width); |
| 1924 | src += stride; |
| 1925 | dst += dst_stride; |
| 1926 | } |
| 1927 | dri2_wl_swrast_commit_backbuffer(dri2_surf); |
| 1928 | } |
| 1929 | |
| 1930 | static void |
| 1931 | dri2_wl_swrast_put_image(__DRIdrawable * draw, int op, |
| 1932 | int x, int y, int w, int h, |
| 1933 | char *data, void *loaderPrivate) |
| 1934 | { |
| 1935 | struct dri2_egl_surface *dri2_surf = loaderPrivate; |
| 1936 | int stride; |
| 1937 | |
| 1938 | stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w); |
| 1939 | dri2_wl_swrast_put_image2(draw, op, x, y, w, h, |
| 1940 | stride, data, loaderPrivate); |
| 1941 | } |
| 1942 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1943 | static EGLBoolean |
Eric Engestrom | 82035b2 | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 1944 | dri2_wl_swrast_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw) |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1945 | { |
| 1946 | struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp); |
| 1947 | struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw); |
| 1948 | |
| 1949 | dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable); |
| 1950 | return EGL_TRUE; |
| 1951 | } |
| 1952 | |
| 1953 | static void |
| 1954 | shm_handle_format(void *data, struct wl_shm *shm, uint32_t format) |
| 1955 | { |
| 1956 | struct dri2_egl_display *dri2_dpy = data; |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 1957 | int visual_idx = dri2_wl_visual_idx_from_shm_format(format); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1958 | |
Daniel Stone | 68a80c1 | 2018-02-06 10:20:39 +0000 | [diff] [blame] | 1959 | if (visual_idx == -1) |
| 1960 | return; |
| 1961 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 1962 | BITSET_SET(dri2_dpy->formats, visual_idx); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | static const struct wl_shm_listener shm_listener = { |
Emil Velikov | 5567494 | 2015-07-10 12:24:11 +0100 | [diff] [blame] | 1966 | .format = shm_handle_format |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1967 | }; |
| 1968 | |
| 1969 | static void |
Daniel Stone | 4b8ef27 | 2017-06-10 14:54:05 +0100 | [diff] [blame] | 1970 | registry_handle_global_swrast(void *data, struct wl_registry *registry, |
| 1971 | uint32_t name, const char *interface, |
| 1972 | uint32_t version) |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1973 | { |
| 1974 | struct dri2_egl_display *dri2_dpy = data; |
| 1975 | |
| 1976 | if (strcmp(interface, "wl_shm") == 0) { |
| 1977 | dri2_dpy->wl_shm = |
| 1978 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
| 1979 | wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | static const struct wl_registry_listener registry_listener_swrast = { |
Emil Velikov | 5567494 | 2015-07-10 12:24:11 +0100 | [diff] [blame] | 1984 | .global = registry_handle_global_swrast, |
| 1985 | .global_remove = registry_handle_global_remove |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1986 | }; |
| 1987 | |
Emil Velikov | 79d1fb9 | 2017-05-15 16:14:17 +0100 | [diff] [blame] | 1988 | static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = { |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1989 | .authenticate = NULL, |
Emil Velikov | cb5e799 | 2016-11-28 18:25:19 +0000 | [diff] [blame] | 1990 | .create_window_surface = dri2_wl_create_window_surface, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1991 | .create_pixmap_surface = dri2_wl_create_pixmap_surface, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1992 | .destroy_surface = dri2_wl_destroy_surface, |
Gurchetan Singh | 12181b5 | 2017-08-01 14:51:40 -0700 | [diff] [blame] | 1993 | .create_image = dri2_create_image_khr, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1994 | .swap_buffers = dri2_wl_swrast_swap_buffers, |
Boyan Ding | a25df54 | 2015-07-21 23:43:59 +0800 | [diff] [blame] | 1995 | .get_dri_drawable = dri2_surface_get_dri_drawable, |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 1996 | }; |
| 1997 | |
Emil Velikov | 2dbe14a | 2016-08-16 18:39:14 +0100 | [diff] [blame] | 1998 | static const __DRIswrastLoaderExtension swrast_loader_extension = { |
| 1999 | .base = { __DRI_SWRAST_LOADER, 2 }, |
| 2000 | |
| 2001 | .getDrawableInfo = dri2_wl_swrast_get_drawable_info, |
| 2002 | .putImage = dri2_wl_swrast_put_image, |
| 2003 | .getImage = dri2_wl_swrast_get_image, |
| 2004 | .putImage2 = dri2_wl_swrast_put_image2, |
| 2005 | }; |
| 2006 | |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 2007 | static const __DRIextension *swrast_loader_extensions[] = { |
| 2008 | &swrast_loader_extension.base, |
Gurchetan Singh | 12181b5 | 2017-08-01 14:51:40 -0700 | [diff] [blame] | 2009 | &image_lookup_extension.base, |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 2010 | NULL, |
| 2011 | }; |
| 2012 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2013 | static EGLBoolean |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 2014 | dri2_initialize_wayland_swrast(_EGLDisplay *disp) |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2015 | { |
Emil Velikov | 0099270 | 2018-09-04 11:20:03 +0100 | [diff] [blame] | 2016 | _EGLDevice *dev; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2017 | struct dri2_egl_display *dri2_dpy; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2018 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2019 | dri2_dpy = calloc(1, sizeof *dri2_dpy); |
| 2020 | if (!dri2_dpy) |
| 2021 | return _eglError(EGL_BAD_ALLOC, "eglInitialize"); |
| 2022 | |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2023 | dri2_dpy->fd = -1; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2024 | disp->DriverData = (void *) dri2_dpy; |
| 2025 | if (disp->PlatformDisplay == NULL) { |
| 2026 | dri2_dpy->wl_dpy = wl_display_connect(NULL); |
| 2027 | if (dri2_dpy->wl_dpy == NULL) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2028 | goto cleanup; |
Eric Engestrom | 60f9842 | 2017-06-19 00:16:21 +0100 | [diff] [blame] | 2029 | dri2_dpy->own_device = true; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2030 | } else { |
| 2031 | dri2_dpy->wl_dpy = disp->PlatformDisplay; |
| 2032 | } |
| 2033 | |
Emil Velikov | 0099270 | 2018-09-04 11:20:03 +0100 | [diff] [blame] | 2034 | dev = _eglAddDevice(dri2_dpy->fd, true); |
| 2035 | if (!dev) { |
| 2036 | _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice"); |
| 2037 | goto cleanup; |
| 2038 | } |
| 2039 | |
| 2040 | disp->Device = dev; |
| 2041 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2042 | dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy); |
| 2043 | |
Jonas Ådahl | 36b9976 | 2017-01-13 23:05:10 +0800 | [diff] [blame] | 2044 | dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy); |
| 2045 | if (dri2_dpy->wl_dpy_wrapper == NULL) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2046 | goto cleanup; |
Jonas Ådahl | 36b9976 | 2017-01-13 23:05:10 +0800 | [diff] [blame] | 2047 | |
| 2048 | wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper, |
| 2049 | dri2_dpy->wl_queue); |
| 2050 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2051 | if (dri2_dpy->own_device) |
| 2052 | wl_display_dispatch_pending(dri2_dpy->wl_dpy); |
| 2053 | |
Jonas Ådahl | 36b9976 | 2017-01-13 23:05:10 +0800 | [diff] [blame] | 2054 | dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2055 | wl_registry_add_listener(dri2_dpy->wl_registry, |
| 2056 | ®istry_listener_swrast, dri2_dpy); |
| 2057 | |
| 2058 | if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2059 | goto cleanup; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2060 | |
Eric Engestrom | fbf7c38 | 2018-11-27 12:27:45 +0000 | [diff] [blame] | 2061 | if (roundtrip(dri2_dpy) < 0 || !BITSET_TEST_RANGE(dri2_dpy->formats, |
| 2062 | 0, EGL_DRI2_MAX_FORMATS)) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2063 | goto cleanup; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2064 | |
| 2065 | dri2_dpy->driver_name = strdup("swrast"); |
| 2066 | if (!dri2_load_driver_swrast(disp)) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2067 | goto cleanup; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2068 | |
Emil Velikov | f871946 | 2016-08-24 23:32:27 +0100 | [diff] [blame] | 2069 | dri2_dpy->loader_extensions = swrast_loader_extensions; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2070 | |
| 2071 | if (!dri2_create_screen(disp)) |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2072 | goto cleanup; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2073 | |
Emil Velikov | 2c341f2 | 2017-05-11 16:20:04 +0100 | [diff] [blame] | 2074 | if (!dri2_setup_extensions(disp)) |
| 2075 | goto cleanup; |
| 2076 | |
| 2077 | dri2_setup_screen(disp); |
| 2078 | |
Emil Velikov | 47b06f5 | 2017-08-05 00:25:46 +0100 | [diff] [blame] | 2079 | dri2_wl_setup_swap_interval(disp); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2080 | |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 2081 | if (!dri2_wl_add_configs_for_visuals(disp)) { |
Emil Velikov | 0b2b719 | 2016-08-25 14:55:06 +0100 | [diff] [blame] | 2082 | _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs"); |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2083 | goto cleanup; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2086 | /* Fill vtbl last to prevent accidentally calling virtual function during |
| 2087 | * initialization. |
| 2088 | */ |
| 2089 | dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl; |
| 2090 | |
| 2091 | return EGL_TRUE; |
| 2092 | |
Emil Velikov | a0163f9 | 2017-05-11 17:26:48 +0100 | [diff] [blame] | 2093 | cleanup: |
| 2094 | dri2_display_destroy(disp); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2095 | return EGL_FALSE; |
| 2096 | } |
| 2097 | |
| 2098 | EGLBoolean |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 2099 | dri2_initialize_wayland(_EGLDisplay *disp) |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2100 | { |
Eric Engestrom | c3f5152 | 2017-10-02 11:34:52 +0100 | [diff] [blame] | 2101 | EGLBoolean initialized = EGL_FALSE; |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2102 | |
Eric Engestrom | 81cea66 | 2017-12-20 15:53:09 +0000 | [diff] [blame] | 2103 | if (!disp->Options.ForceSoftware) |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 2104 | initialized = dri2_initialize_wayland_drm(disp); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2105 | |
Eric Engestrom | c3f5152 | 2017-10-02 11:34:52 +0100 | [diff] [blame] | 2106 | if (!initialized) |
Eric Engestrom | ad61d4f | 2018-04-22 16:48:15 +0200 | [diff] [blame] | 2107 | initialized = dri2_initialize_wayland_swrast(disp); |
Axel Davy | cdcfe48 | 2015-05-01 11:11:20 +0200 | [diff] [blame] | 2108 | |
| 2109 | return initialized; |
| 2110 | |
| 2111 | } |
Emil Velikov | 8d745ab | 2017-11-09 19:13:09 +0000 | [diff] [blame] | 2112 | |
| 2113 | void |
| 2114 | dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy) |
| 2115 | { |
| 2116 | if (dri2_dpy->wl_drm) |
| 2117 | wl_drm_destroy(dri2_dpy->wl_drm); |
| 2118 | if (dri2_dpy->wl_dmabuf) |
| 2119 | zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf); |
| 2120 | if (dri2_dpy->wl_shm) |
| 2121 | wl_shm_destroy(dri2_dpy->wl_shm); |
| 2122 | if (dri2_dpy->wl_registry) |
| 2123 | wl_registry_destroy(dri2_dpy->wl_registry); |
| 2124 | if (dri2_dpy->wl_queue) |
| 2125 | wl_event_queue_destroy(dri2_dpy->wl_queue); |
| 2126 | if (dri2_dpy->wl_dpy_wrapper) |
| 2127 | wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper); |
Daniel Stone | 4732094 | 2018-02-06 10:29:13 +0000 | [diff] [blame] | 2128 | |
| 2129 | for (int i = 0; dri2_dpy->wl_modifiers && i < ARRAY_SIZE(dri2_wl_visuals); i++) |
| 2130 | u_vector_finish(&dri2_dpy->wl_modifiers[i]); |
| 2131 | free(dri2_dpy->wl_modifiers); |
| 2132 | |
Emil Velikov | 8d745ab | 2017-11-09 19:13:09 +0000 | [diff] [blame] | 2133 | if (dri2_dpy->own_device) |
| 2134 | wl_display_disconnect(dri2_dpy->wl_dpy); |
| 2135 | } |