Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
| 4 | * Copyright © 2017, 2018 Collabora, Ltd. |
| 5 | * Copyright © 2017, 2018 General Electric Company |
| 6 | * Copyright (c) 2018 DisplayLink (UK) Ltd. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining |
| 9 | * a copy of this software and associated documentation files (the |
| 10 | * "Software"), to deal in the Software without restriction, including |
| 11 | * without limitation the rights to use, copy, modify, merge, publish, |
| 12 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | * permit persons to whom the Software is furnished to do so, subject to |
| 14 | * the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice (including the |
| 17 | * next paragraph) shall be included in all copies or substantial |
| 18 | * portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 24 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 25 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 26 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 32 | #include <xf86drm.h> |
| 33 | #include <xf86drmMode.h> |
| 34 | |
| 35 | #include <libweston/libweston.h> |
| 36 | #include <libweston/backend-drm.h> |
| 37 | #include <libweston/pixel-formats.h> |
| 38 | |
| 39 | #include "drm-internal.h" |
| 40 | |
| 41 | #include "linux-dmabuf.h" |
| 42 | #include "presentation-time-server-protocol.h" |
| 43 | |
| 44 | enum drm_output_propose_state_mode { |
| 45 | DRM_OUTPUT_PROPOSE_STATE_MIXED, /**< mix renderer & planes */ |
| 46 | DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY, /**< only assign to renderer & cursor */ |
| 47 | DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY, /**< no renderer use, only planes */ |
| 48 | }; |
| 49 | |
| 50 | static const char *const drm_output_propose_state_mode_as_string[] = { |
| 51 | [DRM_OUTPUT_PROPOSE_STATE_MIXED] = "mixed state", |
| 52 | [DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY] = "render-only state", |
| 53 | [DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY] = "plane-only state" |
| 54 | }; |
| 55 | |
| 56 | static const char * |
| 57 | drm_propose_state_mode_to_string(enum drm_output_propose_state_mode mode) |
| 58 | { |
| 59 | if (mode < 0 || mode >= ARRAY_LENGTH(drm_output_propose_state_mode_as_string)) |
| 60 | return " unknown compositing mode"; |
| 61 | |
| 62 | return drm_output_propose_state_mode_as_string[mode]; |
| 63 | } |
| 64 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 65 | static void |
| 66 | drm_output_add_zpos_plane(struct drm_plane *plane, struct wl_list *planes) |
| 67 | { |
| 68 | struct drm_backend *b = plane->backend; |
| 69 | struct drm_plane_zpos *h_plane; |
| 70 | struct drm_plane_zpos *plane_zpos; |
| 71 | |
| 72 | plane_zpos = zalloc(sizeof(*plane_zpos)); |
| 73 | if (!plane_zpos) |
| 74 | return; |
| 75 | |
| 76 | plane_zpos->plane = plane; |
| 77 | |
| 78 | drm_debug(b, "\t\t\t\t[plane] plane %d added to candidate list\n", |
| 79 | plane->plane_id); |
| 80 | |
| 81 | if (wl_list_empty(planes)) { |
| 82 | wl_list_insert(planes, &plane_zpos->link); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | h_plane = wl_container_of(planes->next, h_plane, link); |
| 87 | if (h_plane->plane->zpos_max >= plane->zpos_max) { |
| 88 | wl_list_insert(planes->prev, &plane_zpos->link); |
| 89 | } else { |
| 90 | struct drm_plane_zpos *p_zpos = NULL; |
| 91 | |
| 92 | if (wl_list_length(planes) == 1) { |
| 93 | wl_list_insert(planes->prev, &plane_zpos->link); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | wl_list_for_each(p_zpos, planes, link) { |
| 98 | if (p_zpos->plane->zpos_max > |
| 99 | plane_zpos->plane->zpos_max) |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | wl_list_insert(p_zpos->link.prev, &plane_zpos->link); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | drm_output_destroy_zpos_plane(struct drm_plane_zpos *plane_zpos) |
| 109 | { |
| 110 | wl_list_remove(&plane_zpos->link); |
| 111 | free(plane_zpos); |
| 112 | } |
| 113 | |
| 114 | static bool |
| 115 | drm_output_check_plane_has_view_assigned(struct drm_plane *plane, |
| 116 | struct drm_output_state *output_state) |
| 117 | { |
| 118 | struct drm_plane_state *ps; |
| 119 | wl_list_for_each(ps, &output_state->plane_list, link) { |
| 120 | if (ps->plane == plane && ps->fb) |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
Marius Vlad | 4eeb402 | 2019-10-14 00:27:05 +0300 | [diff] [blame] | 126 | static bool |
| 127 | drm_output_plane_has_valid_format(struct drm_plane *plane, |
| 128 | struct drm_output_state *state, |
| 129 | struct drm_fb *fb) |
| 130 | { |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 131 | struct drm_backend *b = plane->backend; |
Marius Vlad | 4eeb402 | 2019-10-14 00:27:05 +0300 | [diff] [blame] | 132 | unsigned int i; |
| 133 | |
| 134 | if (!fb) |
| 135 | return false; |
| 136 | |
| 137 | /* Check whether the format is supported */ |
| 138 | for (i = 0; i < plane->count_formats; i++) { |
| 139 | unsigned int j; |
| 140 | |
| 141 | if (plane->formats[i].format != fb->format->format) |
| 142 | continue; |
| 143 | |
| 144 | if (fb->modifier == DRM_FORMAT_MOD_INVALID) |
| 145 | return true; |
| 146 | |
| 147 | for (j = 0; j < plane->formats[i].count_modifiers; j++) { |
| 148 | if (plane->formats[i].modifiers[j] == fb->modifier) |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 153 | drm_debug(b, "\t\t\t\t[%s] not placing view on %s: " |
| 154 | "no free %s planes matching format %s (0x%lx) " |
| 155 | "modifier 0x%llx\n", |
| 156 | drm_output_get_plane_type_name(plane), |
| 157 | drm_output_get_plane_type_name(plane), |
| 158 | drm_output_get_plane_type_name(plane), |
| 159 | fb->format->drm_format_name, |
| 160 | (unsigned long) fb->format, |
| 161 | (unsigned long long) fb->modifier); |
| 162 | |
Marius Vlad | 4eeb402 | 2019-10-14 00:27:05 +0300 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
Marius Vlad | 3b13f56 | 2019-10-14 00:29:18 +0300 | [diff] [blame] | 166 | static bool |
| 167 | drm_output_plane_cursor_has_valid_format(struct weston_view *ev) |
| 168 | { |
| 169 | struct wl_shm_buffer *shmbuf = |
| 170 | wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 171 | |
| 172 | if (shmbuf && wl_shm_buffer_get_format(shmbuf) == WL_SHM_FORMAT_ARGB8888) |
| 173 | return true; |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 178 | static struct drm_plane_state * |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 179 | drm_output_prepare_overlay_view(struct drm_plane *plane, |
| 180 | struct drm_output_state *output_state, |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 181 | struct weston_view *ev, |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 182 | enum drm_output_propose_state_mode mode, |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 183 | struct drm_fb *fb, uint64_t zpos) |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 184 | { |
| 185 | struct drm_output *output = output_state->output; |
| 186 | struct weston_compositor *ec = output->base.compositor; |
| 187 | struct drm_backend *b = to_drm_backend(ec); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 188 | struct drm_plane_state *state = NULL; |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 189 | int ret; |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 190 | |
| 191 | assert(!b->sprites_are_broken); |
| 192 | assert(b->atomic_modeset); |
| 193 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 194 | if (!fb) { |
| 195 | drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: " |
| 196 | " couldn't get fb\n", ev); |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 200 | state = drm_output_state_get_plane(output_state, plane); |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 201 | /* we can't have a 'pending' framebuffer as never set one before reaching here */ |
| 202 | assert(!state->fb); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 203 | |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 204 | state->ev = ev; |
| 205 | state->output = output; |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 206 | |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 207 | if (!drm_plane_state_coords_for_view(state, ev, zpos)) { |
| 208 | drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: " |
| 209 | "unsuitable transform\n", ev); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 210 | drm_plane_state_put_back(state); |
| 211 | state = NULL; |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 212 | goto out; |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 215 | /* If the surface buffer has an in-fence fd, but the plane |
| 216 | * doesn't support fences, we can't place the buffer on this |
| 217 | * plane. */ |
| 218 | if (ev->surface->acquire_fence_fd >= 0 && |
| 219 | plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) { |
| 220 | drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: " |
| 221 | "no in-fence support\n", ev); |
| 222 | drm_plane_state_put_back(state); |
| 223 | state = NULL; |
| 224 | goto out; |
| 225 | } |
| 226 | |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 227 | /* We hold one reference for the lifetime of this function; from |
| 228 | * calling drm_fb_get_from_view() in drm_output_prepare_plane_view(), |
| 229 | * so, we take another reference here to live within the state. */ |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 230 | state->fb = drm_fb_ref(fb); |
| 231 | |
| 232 | state->in_fence_fd = ev->surface->acquire_fence_fd; |
| 233 | |
| 234 | /* In planes-only mode, we don't have an incremental state to |
| 235 | * test against, so we just hope it'll work. */ |
| 236 | if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) { |
| 237 | drm_debug(b, "\t\t\t[overlay] provisionally placing " |
| 238 | "view %p on overlay %lu in planes-only mode\n", |
| 239 | ev, (unsigned long) plane->plane_id); |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 240 | goto out; |
| 241 | } |
| 242 | |
| 243 | ret = drm_pending_state_test(output_state->pending_state); |
| 244 | if (ret == 0) { |
| 245 | drm_debug(b, "\t\t\t[overlay] provisionally placing " |
| 246 | "view %p on overlay %d in mixed mode\n", |
| 247 | ev, plane->plane_id); |
Marius Vlad | 677e459 | 2019-10-25 00:07:37 +0300 | [diff] [blame] | 248 | goto out; |
| 249 | } |
| 250 | |
| 251 | drm_debug(b, "\t\t\t[overlay] not placing view %p on overlay %lu " |
| 252 | "in mixed mode: kernel test failed\n", |
| 253 | ev, (unsigned long) plane->plane_id); |
| 254 | |
| 255 | drm_plane_state_put_back(state); |
| 256 | state = NULL; |
| 257 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 258 | out: |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 259 | return state; |
| 260 | } |
| 261 | |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 262 | #ifdef BUILD_DRM_GBM |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 263 | /** |
| 264 | * Update the image for the current cursor surface |
| 265 | * |
| 266 | * @param plane_state DRM cursor plane state |
| 267 | * @param ev Source view for cursor |
| 268 | */ |
| 269 | static void |
| 270 | cursor_bo_update(struct drm_plane_state *plane_state, struct weston_view *ev) |
| 271 | { |
| 272 | struct drm_backend *b = plane_state->plane->backend; |
| 273 | struct gbm_bo *bo = plane_state->fb->bo; |
| 274 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 275 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 276 | int32_t stride; |
| 277 | uint8_t *s; |
| 278 | int i; |
| 279 | |
| 280 | assert(buffer && buffer->shm_buffer); |
| 281 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 282 | assert(buffer->width <= b->cursor_width); |
| 283 | assert(buffer->height <= b->cursor_height); |
| 284 | |
| 285 | memset(buf, 0, sizeof buf); |
| 286 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 287 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 288 | |
| 289 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 290 | for (i = 0; i < buffer->height; i++) |
| 291 | memcpy(buf + i * b->cursor_width, |
| 292 | s + i * stride, |
| 293 | buffer->width * 4); |
| 294 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 295 | |
| 296 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 297 | weston_log("failed update cursor: %s\n", strerror(errno)); |
| 298 | } |
| 299 | |
| 300 | static struct drm_plane_state * |
| 301 | drm_output_prepare_cursor_view(struct drm_output_state *output_state, |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 302 | struct weston_view *ev, uint64_t zpos) |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 303 | { |
| 304 | struct drm_output *output = output_state->output; |
| 305 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 306 | struct drm_plane *plane = output->cursor_plane; |
| 307 | struct drm_plane_state *plane_state; |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 308 | bool needs_update = false; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 309 | const char *p_name = drm_output_get_plane_type_name(plane); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 310 | |
| 311 | assert(!b->cursors_are_broken); |
| 312 | |
| 313 | if (!plane) |
| 314 | return NULL; |
| 315 | |
| 316 | if (!plane->state_cur->complete) |
| 317 | return NULL; |
| 318 | |
| 319 | if (plane->state_cur->output && plane->state_cur->output != output) |
| 320 | return NULL; |
| 321 | |
| 322 | /* We use GBM to import SHM buffers. */ |
| 323 | if (b->gbm == NULL) |
| 324 | return NULL; |
| 325 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 326 | plane_state = |
| 327 | drm_output_state_get_plane(output_state, output->cursor_plane); |
| 328 | |
| 329 | if (plane_state && plane_state->fb) |
| 330 | return NULL; |
| 331 | |
| 332 | /* We can't scale with the legacy API, and we don't try to account for |
| 333 | * simple cropping/translation in cursor_bo_update. */ |
| 334 | plane_state->output = output; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 335 | if (!drm_plane_state_coords_for_view(plane_state, ev, zpos)) { |
| 336 | drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: " |
| 337 | "unsuitable transform\n", p_name, ev, p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 338 | goto err; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 339 | } |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 340 | |
| 341 | if (plane_state->src_x != 0 || plane_state->src_y != 0 || |
| 342 | plane_state->src_w > (unsigned) b->cursor_width << 16 || |
| 343 | plane_state->src_h > (unsigned) b->cursor_height << 16 || |
| 344 | plane_state->src_w != plane_state->dest_w << 16 || |
| 345 | plane_state->src_h != plane_state->dest_h << 16) { |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 346 | drm_debug(b, "\t\t\t\t[%s] not assigning view %p to %s plane " |
| 347 | "(positioning requires cropping or scaling)\n", |
| 348 | p_name, ev, p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 349 | goto err; |
| 350 | } |
| 351 | |
| 352 | /* Since we're setting plane state up front, we need to work out |
| 353 | * whether or not we need to upload a new cursor. We can't use the |
| 354 | * plane damage, since the planes haven't actually been calculated |
| 355 | * yet: instead try to figure it out directly. KMS cursor planes are |
| 356 | * pretty unique here, in that they lie partway between a Weston plane |
| 357 | * (direct scanout) and a renderer. */ |
| 358 | if (ev != output->cursor_view || |
| 359 | pixman_region32_not_empty(&ev->surface->damage)) { |
| 360 | output->current_cursor++; |
| 361 | output->current_cursor = |
| 362 | output->current_cursor % |
| 363 | ARRAY_LENGTH(output->gbm_cursor_fb); |
| 364 | needs_update = true; |
| 365 | } |
| 366 | |
| 367 | output->cursor_view = ev; |
| 368 | plane_state->ev = ev; |
| 369 | |
| 370 | plane_state->fb = |
| 371 | drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]); |
| 372 | |
| 373 | if (needs_update) { |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 374 | drm_debug(b, "\t\t\t\t[%s] copying new content to cursor BO\n", p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 375 | cursor_bo_update(plane_state, ev); |
| 376 | } |
| 377 | |
| 378 | /* The cursor API is somewhat special: in cursor_bo_update(), we upload |
| 379 | * a buffer which is always cursor_width x cursor_height, even if the |
| 380 | * surface we want to promote is actually smaller than this. Manually |
| 381 | * mangle the plane state to deal with this. */ |
| 382 | plane_state->src_w = b->cursor_width << 16; |
| 383 | plane_state->src_h = b->cursor_height << 16; |
| 384 | plane_state->dest_w = b->cursor_width; |
| 385 | plane_state->dest_h = b->cursor_height; |
| 386 | |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 387 | drm_debug(b, "\t\t\t\t[%s] provisionally assigned view %p to cursor\n", |
| 388 | p_name, ev); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 389 | |
| 390 | return plane_state; |
| 391 | |
| 392 | err: |
| 393 | drm_plane_state_put_back(plane_state); |
| 394 | return NULL; |
| 395 | } |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 396 | #else |
| 397 | static struct drm_plane_state * |
| 398 | drm_output_prepare_cursor_view(struct drm_output_state *output_state, |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 399 | struct weston_view *ev, uint64_t zpos) |
Stefan Agner | ccf2407 | 2019-07-09 22:02:00 +0200 | [diff] [blame] | 400 | { |
| 401 | return NULL; |
| 402 | } |
| 403 | #endif |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 404 | |
| 405 | static struct drm_plane_state * |
| 406 | drm_output_prepare_scanout_view(struct drm_output_state *output_state, |
| 407 | struct weston_view *ev, |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 408 | enum drm_output_propose_state_mode mode, |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 409 | struct drm_fb *fb, uint64_t zpos) |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 410 | { |
| 411 | struct drm_output *output = output_state->output; |
| 412 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 413 | struct drm_plane *scanout_plane = output->scanout_plane; |
| 414 | struct drm_plane_state *state; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 415 | const char *p_name = drm_output_get_plane_type_name(scanout_plane); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 416 | |
| 417 | assert(!b->sprites_are_broken); |
| 418 | assert(b->atomic_modeset); |
| 419 | assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY); |
| 420 | |
| 421 | /* Check the view spans exactly the output size, calculated in the |
| 422 | * logical co-ordinate space. */ |
Marius Vlad | 47e3d1e | 2019-09-11 16:53:08 +0300 | [diff] [blame] | 423 | if (!weston_view_matches_output_entirely(ev, &output->base)) |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 424 | return NULL; |
| 425 | |
| 426 | /* If the surface buffer has an in-fence fd, but the plane doesn't |
| 427 | * support fences, we can't place the buffer on this plane. */ |
| 428 | if (ev->surface->acquire_fence_fd >= 0 && |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 429 | scanout_plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) { |
| 430 | drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: " |
| 431 | "no in-fence support\n", p_name, ev, p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 432 | return NULL; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 433 | } |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 434 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 435 | if (!fb) { |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 436 | drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: " |
| 437 | " couldn't get fb\n", p_name, ev, p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | state = drm_output_state_get_plane(output_state, scanout_plane); |
| 442 | |
| 443 | /* The only way we can already have a buffer in the scanout plane is |
| 444 | * if we are in mixed mode, or if a client buffer has already been |
| 445 | * placed into scanout. The former case will never call into here, |
| 446 | * and in the latter case, the view must have been marked as occluded, |
| 447 | * meaning we should never have ended up here. */ |
| 448 | assert(!state->fb); |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 449 | |
| 450 | /* take another reference here to live within the state */ |
| 451 | state->fb = drm_fb_ref(fb); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 452 | state->ev = ev; |
| 453 | state->output = output; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 454 | if (!drm_plane_state_coords_for_view(state, ev, zpos)) { |
| 455 | drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: " |
| 456 | "unsuitable transform\n", p_name, ev, p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 457 | goto err; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 458 | } |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 459 | |
| 460 | if (state->dest_x != 0 || state->dest_y != 0 || |
| 461 | state->dest_w != (unsigned) output->base.current_mode->width || |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 462 | state->dest_h != (unsigned) output->base.current_mode->height) { |
| 463 | drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: " |
| 464 | " invalid plane state\n", p_name, ev, p_name); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 465 | goto err; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 466 | } |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 467 | |
| 468 | state->in_fence_fd = ev->surface->acquire_fence_fd; |
| 469 | |
| 470 | /* In plane-only mode, we don't need to test the state now, as we |
| 471 | * will only test it once at the end. */ |
| 472 | return state; |
| 473 | |
| 474 | err: |
| 475 | drm_plane_state_put_back(state); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 479 | static bool |
| 480 | drm_output_plane_view_has_valid_format(struct drm_plane *plane, |
| 481 | struct drm_output_state *state, |
| 482 | struct weston_view *ev, |
| 483 | struct drm_fb *fb) |
| 484 | { |
| 485 | /* depending on the type of the plane we have different requirements */ |
| 486 | switch (plane->type) { |
| 487 | case WDRM_PLANE_TYPE_CURSOR: |
| 488 | return drm_output_plane_cursor_has_valid_format(ev); |
| 489 | case WDRM_PLANE_TYPE_OVERLAY: |
| 490 | return drm_output_plane_has_valid_format(plane, state, fb); |
| 491 | case WDRM_PLANE_TYPE_PRIMARY: |
| 492 | return drm_output_plane_has_valid_format(plane, state, fb); |
| 493 | default: |
| 494 | assert(0); |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 501 | static struct drm_plane_state * |
| 502 | drm_output_try_view_on_plane(struct drm_plane *plane, |
| 503 | struct drm_output_state *state, |
| 504 | struct weston_view *ev, |
| 505 | enum drm_output_propose_state_mode mode, |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 506 | struct drm_fb *fb, uint64_t zpos) |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 507 | { |
| 508 | struct drm_backend *b = state->pending_state->backend; |
| 509 | struct weston_output *wet_output = &state->output->base; |
| 510 | bool view_matches_entire_output, scanout_has_view_assigned; |
| 511 | struct drm_plane *scanout_plane = state->output->scanout_plane; |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 512 | struct drm_plane_state *ps = NULL; |
| 513 | const char *p_name = drm_output_get_plane_type_name(plane); |
| 514 | enum { |
| 515 | NO_PLANES, /* generic err-handle */ |
| 516 | NO_PLANES_ACCEPTED, |
| 517 | PLACED_ON_PLANE, |
| 518 | } availability = NO_PLANES; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 519 | |
| 520 | /* sanity checks in case we over/underflow zpos or pass incorrect |
| 521 | * values */ |
| 522 | assert(zpos <= plane->zpos_max || |
| 523 | zpos != DRM_PLANE_ZPOS_INVALID_PLANE); |
| 524 | |
| 525 | switch (plane->type) { |
| 526 | case WDRM_PLANE_TYPE_CURSOR: |
| 527 | if (b->cursors_are_broken) { |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 528 | availability = NO_PLANES_ACCEPTED; |
| 529 | goto out; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 530 | } |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 531 | |
| 532 | ps = drm_output_prepare_cursor_view(state, ev, zpos); |
| 533 | if (ps) |
| 534 | availability = PLACED_ON_PLANE; |
| 535 | break; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 536 | case WDRM_PLANE_TYPE_OVERLAY: |
| 537 | /* do not attempt to place it in the overlay if we don't have |
| 538 | * anything in the scanout/primary and the view doesn't cover |
| 539 | * the entire output */ |
| 540 | view_matches_entire_output = |
| 541 | weston_view_matches_output_entirely(ev, wet_output); |
| 542 | scanout_has_view_assigned = |
| 543 | drm_output_check_plane_has_view_assigned(scanout_plane, |
| 544 | state); |
| 545 | |
| 546 | if (view_matches_entire_output && !scanout_has_view_assigned) { |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 547 | availability = NO_PLANES_ACCEPTED; |
| 548 | goto out; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 549 | } |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 550 | |
| 551 | ps = drm_output_prepare_overlay_view(plane, state, ev, mode, |
| 552 | fb, zpos); |
| 553 | if (ps) |
| 554 | availability = PLACED_ON_PLANE; |
| 555 | break; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 556 | case WDRM_PLANE_TYPE_PRIMARY: |
| 557 | if (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) { |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 558 | availability = NO_PLANES_ACCEPTED; |
| 559 | goto out; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 560 | } |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 561 | |
| 562 | ps = drm_output_prepare_scanout_view(state, ev, mode, |
| 563 | fb, zpos); |
| 564 | if (ps) |
| 565 | availability = PLACED_ON_PLANE; |
| 566 | break; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 567 | default: |
| 568 | assert(0); |
| 569 | break; |
| 570 | } |
Marius Vlad | 36f11a5 | 2019-11-01 23:49:21 +0200 | [diff] [blame] | 571 | |
| 572 | out: |
| 573 | switch (availability) { |
| 574 | case NO_PLANES: |
| 575 | /* set initial to this catch-all case, such that |
| 576 | * prepare_cursor/overlay/scanout() should have/contain the |
| 577 | * reason for failling */ |
| 578 | break; |
| 579 | case NO_PLANES_ACCEPTED: |
| 580 | drm_debug(b, "\t\t\t\t[plane] plane %d refusing to " |
| 581 | "place view %p in %s\n", |
| 582 | plane->plane_id, ev, p_name); |
| 583 | break; |
| 584 | case PLACED_ON_PLANE: |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | return ps; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static int |
| 593 | drm_output_check_zpos_plane_states(struct drm_output_state *state) |
| 594 | { |
| 595 | struct drm_backend *b = state->pending_state->backend; |
| 596 | struct drm_plane_state *ps; |
| 597 | int ret = 0; |
| 598 | |
| 599 | wl_list_for_each(ps, &state->plane_list, link) { |
| 600 | struct wl_list *next_node = ps->link.next; |
| 601 | bool found_dup = false; |
| 602 | |
| 603 | /* find another plane with the same zpos value */ |
| 604 | if (next_node == &state->plane_list) |
| 605 | break; |
| 606 | |
| 607 | while (next_node && next_node != &state->plane_list) { |
| 608 | struct drm_plane_state *ps_next; |
| 609 | |
| 610 | ps_next = container_of(next_node, |
| 611 | struct drm_plane_state, |
| 612 | link); |
| 613 | |
| 614 | if (ps->zpos == ps_next->zpos) { |
| 615 | found_dup = true; |
| 616 | break; |
| 617 | } |
| 618 | next_node = next_node->next; |
| 619 | } |
| 620 | |
| 621 | if (found_dup) { |
| 622 | ret = 1; |
| 623 | drm_debug(b, "\t\t\t[plane] found duplicate zpos values\n"); |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static struct drm_plane_state * |
| 632 | drm_output_prepare_plane_view(struct drm_output_state *state, |
| 633 | struct weston_view *ev, |
| 634 | enum drm_output_propose_state_mode mode, |
| 635 | uint64_t current_lowest_zpos) |
| 636 | { |
| 637 | struct drm_output *output = state->output; |
| 638 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 639 | |
| 640 | struct drm_plane_state *ps = NULL; |
| 641 | struct drm_plane *plane; |
| 642 | struct drm_plane_zpos *p_zpos, *p_zpos_next; |
| 643 | struct wl_list zpos_candidate_list; |
| 644 | |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 645 | struct drm_fb *fb; |
| 646 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 647 | wl_list_init(&zpos_candidate_list); |
| 648 | |
| 649 | /* check view for valid buffer, doesn't make sense to even try */ |
| 650 | if (!weston_view_has_valid_buffer(ev)) |
| 651 | return ps; |
| 652 | |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 653 | fb = drm_fb_get_from_view(state, ev); |
| 654 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 655 | /* assemble a list with possible candidates */ |
| 656 | wl_list_for_each(plane, &b->plane_list, link) { |
| 657 | if (!drm_plane_is_available(plane, output)) |
| 658 | continue; |
| 659 | |
| 660 | if (drm_output_check_plane_has_view_assigned(plane, state)) { |
| 661 | drm_debug(b, "\t\t\t\t[plane] not adding plane %d to" |
| 662 | " candidate list: view already assigned " |
| 663 | "to a plane\n", plane->plane_id); |
| 664 | continue; |
| 665 | } |
| 666 | |
| 667 | if (plane->zpos_min >= current_lowest_zpos) { |
| 668 | drm_debug(b, "\t\t\t\t[plane] not adding plane %d to " |
| 669 | "candidate list: minium zpos (%"PRIu64") " |
| 670 | "plane's above current lowest zpos " |
| 671 | "(%"PRIu64")\n", plane->plane_id, |
| 672 | plane->zpos_min, current_lowest_zpos); |
| 673 | continue; |
| 674 | } |
| 675 | |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 676 | if (!drm_output_plane_view_has_valid_format(plane, state, ev, fb)) { |
| 677 | drm_debug(b, "\t\t\t\t[plane] not adding plane %d to " |
| 678 | "candidate list: invalid pixel format\n", |
| 679 | plane->plane_id); |
| 680 | continue; |
| 681 | } |
| 682 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 683 | drm_output_add_zpos_plane(plane, &zpos_candidate_list); |
| 684 | } |
| 685 | |
| 686 | /* go over the potential candidate list and try to find a possible |
| 687 | * plane suitable for \c ev; start with the highest zpos value of a |
| 688 | * plane to maximize our chances, but do note we pass the zpos value |
| 689 | * based on current tracked value by \c current_lowest_zpos_in_use */ |
| 690 | while (!wl_list_empty(&zpos_candidate_list)) { |
| 691 | struct drm_plane_zpos *head_p_zpos = |
| 692 | wl_container_of(zpos_candidate_list.next, |
| 693 | head_p_zpos, link); |
| 694 | struct drm_plane *plane = head_p_zpos->plane; |
| 695 | const char *p_name = drm_output_get_plane_type_name(plane); |
| 696 | uint64_t zpos; |
| 697 | |
| 698 | if (current_lowest_zpos == DRM_PLANE_ZPOS_INVALID_PLANE) |
| 699 | zpos = plane->zpos_max; |
| 700 | else |
| 701 | zpos = MIN(current_lowest_zpos - 1, plane->zpos_max); |
| 702 | |
| 703 | drm_debug(b, "\t\t\t\t[plane] plane %d picked " |
| 704 | "from candidate list, type: %s\n", |
| 705 | plane->plane_id, p_name); |
| 706 | |
Marius Vlad | eef6945 | 2019-10-31 12:48:35 +0200 | [diff] [blame] | 707 | ps = drm_output_try_view_on_plane(plane, state, ev, |
| 708 | mode, fb, zpos); |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 709 | drm_output_destroy_zpos_plane(head_p_zpos); |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 710 | if (ps) { |
| 711 | drm_debug(b, "\t\t\t\t[view] view %p has been placed to " |
| 712 | "%s plane with computed zpos %"PRIu64"\n", |
| 713 | ev, p_name, zpos); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | wl_list_for_each_safe(p_zpos, p_zpos_next, &zpos_candidate_list, link) |
| 719 | drm_output_destroy_zpos_plane(p_zpos); |
| 720 | |
Marius Vlad | 26dcce0 | 2019-10-24 14:49:33 +0300 | [diff] [blame] | 721 | drm_fb_unref(fb); |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 722 | return ps; |
| 723 | } |
| 724 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 725 | static struct drm_output_state * |
| 726 | drm_output_propose_state(struct weston_output *output_base, |
| 727 | struct drm_pending_state *pending_state, |
| 728 | enum drm_output_propose_state_mode mode) |
| 729 | { |
| 730 | struct drm_output *output = to_drm_output(output_base); |
| 731 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
| 732 | struct drm_output_state *state; |
| 733 | struct drm_plane_state *scanout_state = NULL; |
| 734 | struct weston_view *ev; |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 735 | |
| 736 | pixman_region32_t surface_overlap, renderer_region, planes_region; |
| 737 | pixman_region32_t occluded_region; |
| 738 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 739 | bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY); |
| 740 | int ret; |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 741 | uint64_t current_lowest_zpos = DRM_PLANE_ZPOS_INVALID_PLANE; |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 742 | |
| 743 | assert(!output->state_last); |
| 744 | state = drm_output_state_duplicate(output->state_cur, |
| 745 | pending_state, |
| 746 | DRM_OUTPUT_STATE_CLEAR_PLANES); |
| 747 | |
| 748 | /* We implement mixed mode by progressively creating and testing |
| 749 | * incremental states, of scanout + overlay + cursor. Since we |
| 750 | * walk our views top to bottom, the scanout plane is last, however |
| 751 | * we always need it in our scene for the test modeset to be |
| 752 | * meaningful. To do this, we steal a reference to the last |
| 753 | * renderer framebuffer we have, if we think it's basically |
| 754 | * compatible. If we don't have that, then we conservatively fall |
| 755 | * back to only using the renderer for this repaint. */ |
| 756 | if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) { |
| 757 | struct drm_plane *plane = output->scanout_plane; |
| 758 | struct drm_fb *scanout_fb = plane->state_cur->fb; |
| 759 | |
| 760 | if (!scanout_fb || |
| 761 | (scanout_fb->type != BUFFER_GBM_SURFACE && |
| 762 | scanout_fb->type != BUFFER_PIXMAN_DUMB)) { |
| 763 | drm_debug(b, "\t\t[state] cannot propose mixed mode: " |
| 764 | "for output %s (%lu): no previous renderer " |
| 765 | "fb\n", |
| 766 | output->base.name, |
| 767 | (unsigned long) output->base.id); |
| 768 | drm_output_state_free(state); |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | if (scanout_fb->width != output_base->current_mode->width || |
| 773 | scanout_fb->height != output_base->current_mode->height) { |
| 774 | drm_debug(b, "\t\t[state] cannot propose mixed mode " |
| 775 | "for output %s (%lu): previous fb has " |
| 776 | "different size\n", |
| 777 | output->base.name, |
| 778 | (unsigned long) output->base.id); |
| 779 | drm_output_state_free(state); |
| 780 | return NULL; |
| 781 | } |
| 782 | |
| 783 | scanout_state = drm_plane_state_duplicate(state, |
| 784 | plane->state_cur); |
| 785 | drm_debug(b, "\t\t[state] using renderer FB ID %lu for mixed " |
| 786 | "mode for output %s (%lu)\n", |
| 787 | (unsigned long) scanout_fb->fb_id, output->base.name, |
| 788 | (unsigned long) output->base.id); |
| 789 | } |
| 790 | |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 791 | /* - renderer_region contains the total region which which will be |
| 792 | * covered by the renderer |
| 793 | * - planes_region contains the total region which has been covered by |
| 794 | * hardware planes |
| 795 | * - occluded_region contains the total region which which will be |
| 796 | * covered by the renderer and hardware planes, where the view's |
| 797 | * visible-and-opaque region is added in both cases (the view's |
| 798 | * opaque region accumulates there for each view); it is being used |
| 799 | * to skip the view, if it is completely occluded; includes the |
| 800 | * situation where occluded_region covers entire output's region. |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 801 | */ |
| 802 | pixman_region32_init(&renderer_region); |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 803 | pixman_region32_init(&planes_region); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 804 | pixman_region32_init(&occluded_region); |
| 805 | |
| 806 | wl_list_for_each(ev, &output_base->compositor->view_list, link) { |
| 807 | struct drm_plane_state *ps = NULL; |
| 808 | bool force_renderer = false; |
| 809 | pixman_region32_t clipped_view; |
| 810 | bool totally_occluded = false; |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 811 | |
| 812 | drm_debug(b, "\t\t\t[view] evaluating view %p for " |
| 813 | "output %s (%lu)\n", |
| 814 | ev, output->base.name, |
| 815 | (unsigned long) output->base.id); |
| 816 | |
| 817 | /* If this view doesn't touch our output at all, there's no |
| 818 | * reason to do anything with it. */ |
| 819 | if (!(ev->output_mask & (1u << output->base.id))) { |
| 820 | drm_debug(b, "\t\t\t\t[view] ignoring view %p " |
| 821 | "(not on our output)\n", ev); |
| 822 | continue; |
| 823 | } |
| 824 | |
| 825 | /* We only assign planes to views which are exclusively present |
| 826 | * on our output. */ |
| 827 | if (ev->output_mask != (1u << output->base.id)) { |
| 828 | drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane " |
| 829 | "(on multiple outputs)\n", ev); |
| 830 | force_renderer = true; |
| 831 | } |
| 832 | |
Marius Vlad | 5f6bee4 | 2019-09-11 16:41:04 +0300 | [diff] [blame] | 833 | if (!weston_view_has_valid_buffer(ev)) { |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 834 | drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane " |
| 835 | "(no buffer available)\n", ev); |
| 836 | force_renderer = true; |
| 837 | } |
| 838 | |
| 839 | /* Ignore views we know to be totally occluded. */ |
| 840 | pixman_region32_init(&clipped_view); |
| 841 | pixman_region32_intersect(&clipped_view, |
| 842 | &ev->transform.boundingbox, |
| 843 | &output->base.region); |
| 844 | |
| 845 | pixman_region32_init(&surface_overlap); |
| 846 | pixman_region32_subtract(&surface_overlap, &clipped_view, |
| 847 | &occluded_region); |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 848 | /* if the view is completely occluded then ignore that |
| 849 | * view; includes the case where occluded_region covers |
| 850 | * the entire output */ |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 851 | totally_occluded = !pixman_region32_not_empty(&surface_overlap); |
| 852 | if (totally_occluded) { |
| 853 | drm_debug(b, "\t\t\t\t[view] ignoring view %p " |
| 854 | "(occluded on our output)\n", ev); |
| 855 | pixman_region32_fini(&surface_overlap); |
| 856 | pixman_region32_fini(&clipped_view); |
| 857 | continue; |
| 858 | } |
| 859 | |
| 860 | /* Since we process views from top to bottom, we know that if |
| 861 | * the view intersects the calculated renderer region, it must |
| 862 | * be part of, or occluded by, it, and cannot go on a plane. */ |
| 863 | pixman_region32_intersect(&surface_overlap, &renderer_region, |
| 864 | &clipped_view); |
| 865 | if (pixman_region32_not_empty(&surface_overlap)) { |
| 866 | drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane " |
| 867 | "(occluded by renderer views)\n", ev); |
| 868 | force_renderer = true; |
| 869 | } |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 870 | pixman_region32_fini(&surface_overlap); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 871 | |
Ankit Nautiyal | a344fe3 | 2019-05-14 18:36:08 +0530 | [diff] [blame] | 872 | /* In case of enforced mode of content-protection do not |
| 873 | * assign planes for a protected surface on an unsecured output. |
| 874 | */ |
| 875 | if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED && |
| 876 | ev->surface->desired_protection > output_base->current_protection) { |
| 877 | drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane " |
| 878 | "(enforced protection mode on unsecured output)\n", ev); |
| 879 | force_renderer = true; |
| 880 | } |
| 881 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 882 | if (!force_renderer) { |
| 883 | drm_debug(b, "\t\t\t[plane] started with zpos %"PRIu64"\n", |
| 884 | current_lowest_zpos); |
| 885 | ps = drm_output_prepare_plane_view(state, ev, mode, |
| 886 | current_lowest_zpos); |
Daniel Stone | 2cb926c | 2019-11-11 16:48:54 +0000 | [diff] [blame] | 887 | } |
Daniel Stone | 2cb926c | 2019-11-11 16:48:54 +0000 | [diff] [blame] | 888 | |
| 889 | if (ps) { |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 890 | current_lowest_zpos = ps->zpos; |
| 891 | drm_debug(b, "\t\t\t[plane] next zpos to use %"PRIu64"\n", |
| 892 | current_lowest_zpos); |
| 893 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 894 | /* If we have been assigned to an overlay or scanout |
| 895 | * plane, add this area to the occluded region, so |
| 896 | * other views are known to be behind it. The cursor |
| 897 | * plane, however, is special, in that it blends with |
| 898 | * the content underneath it: the area should neither |
| 899 | * be added to the renderer region nor the occluded |
| 900 | * region. */ |
| 901 | if (ps->plane->type != WDRM_PLANE_TYPE_CURSOR) { |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 902 | pixman_region32_union(&planes_region, |
| 903 | &planes_region, |
| 904 | &clipped_view); |
| 905 | |
| 906 | if (!weston_view_is_opaque(ev, &clipped_view)) |
| 907 | pixman_region32_intersect(&clipped_view, |
| 908 | &clipped_view, |
| 909 | &ev->transform.opaque); |
| 910 | /* the visible-and-opaque region of this view |
| 911 | * will occlude views underneath it */ |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 912 | pixman_region32_union(&occluded_region, |
| 913 | &occluded_region, |
| 914 | &clipped_view); |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 915 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 916 | pixman_region32_fini(&clipped_view); |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 917 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 918 | } |
| 919 | continue; |
| 920 | } |
| 921 | |
| 922 | /* We have been assigned to the primary (renderer) plane: |
| 923 | * check if this is OK, and add ourselves to the renderer |
| 924 | * region if so. */ |
| 925 | if (!renderer_ok) { |
| 926 | drm_debug(b, "\t\t[view] failing state generation: " |
| 927 | "placing view %p to renderer not allowed\n", |
| 928 | ev); |
| 929 | pixman_region32_fini(&clipped_view); |
| 930 | goto err_region; |
| 931 | } |
| 932 | |
| 933 | pixman_region32_union(&renderer_region, |
| 934 | &renderer_region, |
| 935 | &clipped_view); |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 936 | |
| 937 | if (!weston_view_is_opaque(ev, &clipped_view)) |
| 938 | pixman_region32_intersect(&clipped_view, |
| 939 | &clipped_view, |
| 940 | &ev->transform.opaque); |
| 941 | |
| 942 | pixman_region32_union(&occluded_region, |
| 943 | &occluded_region, |
| 944 | &clipped_view); |
| 945 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 946 | pixman_region32_fini(&clipped_view); |
Marius Vlad | 555bfaf | 2019-10-14 23:15:49 +0300 | [diff] [blame^] | 947 | |
| 948 | drm_debug(b, "\t\t\t\t[view] view %p will be placed " |
| 949 | "on the renderer\n", ev); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 950 | } |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 951 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 952 | pixman_region32_fini(&renderer_region); |
Marius Vlad | 80a62e5 | 2019-09-11 18:21:59 +0300 | [diff] [blame] | 953 | pixman_region32_fini(&planes_region); |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 954 | pixman_region32_fini(&occluded_region); |
| 955 | |
| 956 | /* In renderer-only mode, we can't test the state as we don't have a |
| 957 | * renderer buffer yet. */ |
| 958 | if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY) |
| 959 | return state; |
| 960 | |
Marius Vlad | 2538aac | 2019-10-14 11:05:30 +0300 | [diff] [blame] | 961 | /* check if we have invalid zpos values, like duplicate(s) */ |
| 962 | ret = drm_output_check_zpos_plane_states(state); |
| 963 | if (ret != 0) { |
| 964 | drm_debug(b, "\t\t[view] failing state generation: " |
| 965 | "zpos values are in-consistent\n"); |
| 966 | goto err; |
| 967 | } |
| 968 | |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 969 | /* Check to see if this state will actually work. */ |
| 970 | ret = drm_pending_state_test(state->pending_state); |
| 971 | if (ret != 0) { |
| 972 | drm_debug(b, "\t\t[view] failing state generation: " |
| 973 | "atomic test not OK\n"); |
| 974 | goto err; |
| 975 | } |
| 976 | |
| 977 | /* Counterpart to duplicating scanout state at the top of this |
| 978 | * function: if we have taken a renderer framebuffer and placed it in |
| 979 | * the pending state in order to incrementally test overlay planes, |
| 980 | * remove it now. */ |
| 981 | if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) { |
| 982 | assert(scanout_state->fb->type == BUFFER_GBM_SURFACE || |
| 983 | scanout_state->fb->type == BUFFER_PIXMAN_DUMB); |
| 984 | drm_plane_state_put_back(scanout_state); |
| 985 | } |
| 986 | return state; |
| 987 | |
| 988 | err_region: |
| 989 | pixman_region32_fini(&renderer_region); |
| 990 | pixman_region32_fini(&occluded_region); |
| 991 | err: |
| 992 | drm_output_state_free(state); |
| 993 | return NULL; |
| 994 | } |
| 995 | |
| 996 | void |
| 997 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
| 998 | { |
| 999 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1000 | struct drm_pending_state *pending_state = repaint_data; |
| 1001 | struct drm_output *output = to_drm_output(output_base); |
| 1002 | struct drm_output_state *state = NULL; |
| 1003 | struct drm_plane_state *plane_state; |
| 1004 | struct weston_view *ev; |
| 1005 | struct weston_plane *primary = &output_base->compositor->primary_plane; |
| 1006 | enum drm_output_propose_state_mode mode = DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY; |
| 1007 | |
| 1008 | drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n", |
| 1009 | output_base->name, (unsigned long) output_base->id); |
| 1010 | |
| 1011 | if (!b->sprites_are_broken && !output->virtual) { |
| 1012 | drm_debug(b, "\t[repaint] trying planes-only build state\n"); |
| 1013 | state = drm_output_propose_state(output_base, pending_state, mode); |
| 1014 | if (!state) { |
| 1015 | drm_debug(b, "\t[repaint] could not build planes-only " |
| 1016 | "state, trying mixed\n"); |
| 1017 | mode = DRM_OUTPUT_PROPOSE_STATE_MIXED; |
| 1018 | state = drm_output_propose_state(output_base, |
| 1019 | pending_state, |
| 1020 | mode); |
| 1021 | } |
| 1022 | if (!state) { |
| 1023 | drm_debug(b, "\t[repaint] could not build mixed-mode " |
| 1024 | "state, trying renderer-only\n"); |
| 1025 | } |
| 1026 | } else { |
| 1027 | drm_debug(b, "\t[state] no overlay plane support\n"); |
| 1028 | } |
| 1029 | |
| 1030 | if (!state) { |
| 1031 | mode = DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY; |
| 1032 | state = drm_output_propose_state(output_base, pending_state, |
| 1033 | mode); |
| 1034 | } |
| 1035 | |
| 1036 | assert(state); |
| 1037 | drm_debug(b, "\t[repaint] Using %s composition\n", |
| 1038 | drm_propose_state_mode_to_string(mode)); |
| 1039 | |
| 1040 | wl_list_for_each(ev, &output_base->compositor->view_list, link) { |
| 1041 | struct drm_plane *target_plane = NULL; |
| 1042 | |
| 1043 | /* If this view doesn't touch our output at all, there's no |
| 1044 | * reason to do anything with it. */ |
| 1045 | if (!(ev->output_mask & (1u << output->base.id))) |
| 1046 | continue; |
| 1047 | |
| 1048 | /* Test whether this buffer can ever go into a plane: |
| 1049 | * non-shm, or small enough to be a cursor. |
| 1050 | * |
| 1051 | * Also, keep a reference when using the pixman renderer. |
| 1052 | * That makes it possible to do a seamless switch to the GL |
| 1053 | * renderer and since the pixman renderer keeps a reference |
| 1054 | * to the buffer anyway, there is no side effects. |
| 1055 | */ |
| 1056 | if (b->use_pixman || |
Marius Vlad | 5f6bee4 | 2019-09-11 16:41:04 +0300 | [diff] [blame] | 1057 | (weston_view_has_valid_buffer(ev) && |
Daniel Stone | e404b72 | 2019-06-22 18:40:31 +0100 | [diff] [blame] | 1058 | (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) || |
| 1059 | (ev->surface->width <= b->cursor_width && |
| 1060 | ev->surface->height <= b->cursor_height)))) |
| 1061 | ev->surface->keep_buffer = true; |
| 1062 | else |
| 1063 | ev->surface->keep_buffer = false; |
| 1064 | |
| 1065 | /* This is a bit unpleasant, but lacking a temporary place to |
| 1066 | * hang a plane off the view, we have to do a nested walk. |
| 1067 | * Our first-order iteration has to be planes rather than |
| 1068 | * views, because otherwise we won't reset views which were |
| 1069 | * previously on planes to being on the primary plane. */ |
| 1070 | wl_list_for_each(plane_state, &state->plane_list, link) { |
| 1071 | if (plane_state->ev == ev) { |
| 1072 | plane_state->ev = NULL; |
| 1073 | target_plane = plane_state->plane; |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (target_plane) { |
| 1079 | drm_debug(b, "\t[repaint] view %p on %s plane %lu\n", |
| 1080 | ev, plane_type_enums[target_plane->type].name, |
| 1081 | (unsigned long) target_plane->plane_id); |
| 1082 | weston_view_move_to_plane(ev, &target_plane->base); |
| 1083 | } else { |
| 1084 | drm_debug(b, "\t[repaint] view %p using renderer " |
| 1085 | "composition\n", ev); |
| 1086 | weston_view_move_to_plane(ev, primary); |
| 1087 | } |
| 1088 | |
| 1089 | if (!target_plane || |
| 1090 | target_plane->type == WDRM_PLANE_TYPE_CURSOR) { |
| 1091 | /* cursor plane & renderer involve a copy */ |
| 1092 | ev->psf_flags = 0; |
| 1093 | } else { |
| 1094 | /* All other planes are a direct scanout of a |
| 1095 | * single client buffer. |
| 1096 | */ |
| 1097 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /* We rely on output->cursor_view being both an accurate reflection of |
| 1102 | * the cursor plane's state, but also being maintained across repaints |
| 1103 | * to avoid unnecessary damage uploads, per the comment in |
| 1104 | * drm_output_prepare_cursor_view. In the event that we go from having |
| 1105 | * a cursor view to not having a cursor view, we need to clear it. */ |
| 1106 | if (output->cursor_view) { |
| 1107 | plane_state = |
| 1108 | drm_output_state_get_existing_plane(state, |
| 1109 | output->cursor_plane); |
| 1110 | if (!plane_state || !plane_state->fb) |
| 1111 | output->cursor_view = NULL; |
| 1112 | } |
| 1113 | } |