blob: 26cc3269e9db1a6b0bd8dc7fbe4d074e6b3ed6bf [file] [log] [blame]
Daniel Stonee404b722019-06-22 18:40:31 +01001/*
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
44enum 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
50static 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
56static const char *
57drm_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 Vlad2538aac2019-10-14 11:05:30 +030065static void
66drm_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
107static void
108drm_output_destroy_zpos_plane(struct drm_plane_zpos *plane_zpos)
109{
110 wl_list_remove(&plane_zpos->link);
111 free(plane_zpos);
112}
113
114static bool
115drm_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 Vlad4eeb4022019-10-14 00:27:05 +0300126static bool
127drm_output_plane_has_valid_format(struct drm_plane *plane,
128 struct drm_output_state *state,
129 struct drm_fb *fb)
130{
Marius Vlad26dcce02019-10-24 14:49:33 +0300131 struct drm_backend *b = plane->backend;
Scott Anderson74663092021-02-01 15:46:33 -0300132 struct weston_drm_format *fmt;
Marius Vlad4eeb4022019-10-14 00:27:05 +0300133
134 if (!fb)
135 return false;
136
137 /* Check whether the format is supported */
Scott Anderson74663092021-02-01 15:46:33 -0300138 fmt = weston_drm_format_array_find_format(&plane->formats,
139 fb->format->format);
140 if (fmt) {
Leandro Ribeiroa5560d62021-04-26 08:47:45 -0300141 /* We never try to promote a dmabuf with DRM_FORMAT_MOD_INVALID
142 * to a KMS plane (see drm_fb_get_from_dmabuf() for more details).
143 * So if fb->modifier == DRM_FORMAT_MOD_INVALID, we are sure
144 * that this is for the legacy GBM import path, in which a
145 * wl_drm is being used for scanout. Mesa is the only user we
146 * care in this case (even though recent versions are also using
147 * dmabufs), and it should know better what works or not. */
Marius Vlad4eeb4022019-10-14 00:27:05 +0300148 if (fb->modifier == DRM_FORMAT_MOD_INVALID)
149 return true;
Leandro Ribeiroa5560d62021-04-26 08:47:45 -0300150
Scott Anderson74663092021-02-01 15:46:33 -0300151 if (weston_drm_format_has_modifier(fmt, fb->modifier))
152 return true;
Marius Vlad4eeb4022019-10-14 00:27:05 +0300153 }
154
Marius Vlad26dcce02019-10-24 14:49:33 +0300155 drm_debug(b, "\t\t\t\t[%s] not placing view on %s: "
156 "no free %s planes matching format %s (0x%lx) "
157 "modifier 0x%llx\n",
158 drm_output_get_plane_type_name(plane),
159 drm_output_get_plane_type_name(plane),
160 drm_output_get_plane_type_name(plane),
161 fb->format->drm_format_name,
162 (unsigned long) fb->format,
163 (unsigned long long) fb->modifier);
164
Marius Vlad4eeb4022019-10-14 00:27:05 +0300165 return false;
166}
167
Marius Vlad3b13f562019-10-14 00:29:18 +0300168static bool
169drm_output_plane_cursor_has_valid_format(struct weston_view *ev)
170{
171 struct wl_shm_buffer *shmbuf =
172 wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
173
Leandro Ribeiro4b5df8b2021-03-23 16:30:09 -0300174 /* When we have cursor planes we've already checked for wl_shm buffer in
175 * the view before calling this function. */
176 assert(shmbuf);
177
178 if (wl_shm_buffer_get_format(shmbuf) == WL_SHM_FORMAT_ARGB8888)
Marius Vlad3b13f562019-10-14 00:29:18 +0300179 return true;
180
181 return false;
182}
183
Daniel Stonee404b722019-06-22 18:40:31 +0100184static struct drm_plane_state *
Marius Vlad677e4592019-10-25 00:07:37 +0300185drm_output_prepare_overlay_view(struct drm_plane *plane,
186 struct drm_output_state *output_state,
Daniel Stonee404b722019-06-22 18:40:31 +0100187 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300188 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200189 struct drm_fb *fb, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100190{
191 struct drm_output *output = output_state->output;
192 struct weston_compositor *ec = output->base.compositor;
193 struct drm_backend *b = to_drm_backend(ec);
Daniel Stonee404b722019-06-22 18:40:31 +0100194 struct drm_plane_state *state = NULL;
Daniel Stonee404b722019-06-22 18:40:31 +0100195 int ret;
Daniel Stonee404b722019-06-22 18:40:31 +0100196
197 assert(!b->sprites_are_broken);
198 assert(b->atomic_modeset);
199
Daniel Stonee404b722019-06-22 18:40:31 +0100200 if (!fb) {
201 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
202 " couldn't get fb\n", ev);
203 return NULL;
204 }
205
Marius Vlad677e4592019-10-25 00:07:37 +0300206 state = drm_output_state_get_plane(output_state, plane);
Marius Vladeef69452019-10-31 12:48:35 +0200207 /* we can't have a 'pending' framebuffer as never set one before reaching here */
208 assert(!state->fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100209
Marius Vlad677e4592019-10-25 00:07:37 +0300210 state->ev = ev;
211 state->output = output;
Marius Vladeef69452019-10-31 12:48:35 +0200212
Marius Vlad677e4592019-10-25 00:07:37 +0300213 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
214 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
215 "unsuitable transform\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100216 drm_plane_state_put_back(state);
217 state = NULL;
Marius Vlad677e4592019-10-25 00:07:37 +0300218 goto out;
Daniel Stonee404b722019-06-22 18:40:31 +0100219 }
220
Marius Vlad677e4592019-10-25 00:07:37 +0300221 /* If the surface buffer has an in-fence fd, but the plane
222 * doesn't support fences, we can't place the buffer on this
223 * plane. */
224 if (ev->surface->acquire_fence_fd >= 0 &&
225 plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
226 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
227 "no in-fence support\n", ev);
228 drm_plane_state_put_back(state);
229 state = NULL;
230 goto out;
231 }
232
Marius Vladeef69452019-10-31 12:48:35 +0200233 /* We hold one reference for the lifetime of this function; from
234 * calling drm_fb_get_from_view() in drm_output_prepare_plane_view(),
235 * so, we take another reference here to live within the state. */
Marius Vlad677e4592019-10-25 00:07:37 +0300236 state->fb = drm_fb_ref(fb);
237
238 state->in_fence_fd = ev->surface->acquire_fence_fd;
239
240 /* In planes-only mode, we don't have an incremental state to
241 * test against, so we just hope it'll work. */
242 if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
243 drm_debug(b, "\t\t\t[overlay] provisionally placing "
244 "view %p on overlay %lu in planes-only mode\n",
245 ev, (unsigned long) plane->plane_id);
Marius Vlad677e4592019-10-25 00:07:37 +0300246 goto out;
247 }
248
249 ret = drm_pending_state_test(output_state->pending_state);
250 if (ret == 0) {
251 drm_debug(b, "\t\t\t[overlay] provisionally placing "
252 "view %p on overlay %d in mixed mode\n",
253 ev, plane->plane_id);
Marius Vlad677e4592019-10-25 00:07:37 +0300254 goto out;
255 }
256
257 drm_debug(b, "\t\t\t[overlay] not placing view %p on overlay %lu "
258 "in mixed mode: kernel test failed\n",
259 ev, (unsigned long) plane->plane_id);
260
261 drm_plane_state_put_back(state);
262 state = NULL;
263
Daniel Stonee404b722019-06-22 18:40:31 +0100264out:
Daniel Stonee404b722019-06-22 18:40:31 +0100265 return state;
266}
267
Stefan Agnerccf24072019-07-09 22:02:00 +0200268#ifdef BUILD_DRM_GBM
Daniel Stonee404b722019-06-22 18:40:31 +0100269/**
270 * Update the image for the current cursor surface
271 *
272 * @param plane_state DRM cursor plane state
273 * @param ev Source view for cursor
274 */
275static void
276cursor_bo_update(struct drm_plane_state *plane_state, struct weston_view *ev)
277{
278 struct drm_backend *b = plane_state->plane->backend;
279 struct gbm_bo *bo = plane_state->fb->bo;
280 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
281 uint32_t buf[b->cursor_width * b->cursor_height];
282 int32_t stride;
283 uint8_t *s;
284 int i;
285
286 assert(buffer && buffer->shm_buffer);
287 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
288 assert(buffer->width <= b->cursor_width);
289 assert(buffer->height <= b->cursor_height);
290
291 memset(buf, 0, sizeof buf);
292 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
293 s = wl_shm_buffer_get_data(buffer->shm_buffer);
294
295 wl_shm_buffer_begin_access(buffer->shm_buffer);
296 for (i = 0; i < buffer->height; i++)
297 memcpy(buf + i * b->cursor_width,
298 s + i * stride,
299 buffer->width * 4);
300 wl_shm_buffer_end_access(buffer->shm_buffer);
301
302 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
303 weston_log("failed update cursor: %s\n", strerror(errno));
304}
305
306static struct drm_plane_state *
307drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300308 struct weston_view *ev, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100309{
310 struct drm_output *output = output_state->output;
311 struct drm_backend *b = to_drm_backend(output->base.compositor);
312 struct drm_plane *plane = output->cursor_plane;
313 struct drm_plane_state *plane_state;
Daniel Stonee404b722019-06-22 18:40:31 +0100314 bool needs_update = false;
Marius Vlad7a465d82021-04-03 19:54:05 +0300315 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Marius Vlad36f11a52019-11-01 23:49:21 +0200316 const char *p_name = drm_output_get_plane_type_name(plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100317
318 assert(!b->cursors_are_broken);
319
320 if (!plane)
321 return NULL;
322
323 if (!plane->state_cur->complete)
324 return NULL;
325
326 if (plane->state_cur->output && plane->state_cur->output != output)
327 return NULL;
328
329 /* We use GBM to import SHM buffers. */
330 if (b->gbm == NULL)
331 return NULL;
332
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300333 plane_state = drm_output_state_get_plane(output_state, plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100334
335 if (plane_state && plane_state->fb)
336 return NULL;
337
338 /* We can't scale with the legacy API, and we don't try to account for
339 * simple cropping/translation in cursor_bo_update. */
340 plane_state->output = output;
Marius Vlad36f11a52019-11-01 23:49:21 +0200341 if (!drm_plane_state_coords_for_view(plane_state, ev, zpos)) {
342 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
343 "unsuitable transform\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100344 goto err;
Marius Vlad36f11a52019-11-01 23:49:21 +0200345 }
Daniel Stonee404b722019-06-22 18:40:31 +0100346
Marius Vlad7a465d82021-04-03 19:54:05 +0300347 if (buffer->width > b->cursor_width ||
348 buffer->height > b->cursor_height) {
349 drm_debug(b, "\t\t\t\t[%s] not assigning view %p to %s plane "
350 "(surface buffer (%dx%d) larger than permitted"
351 " (%dx%d))\n", p_name, ev, p_name,
352 buffer->width, buffer->height,
353 b->cursor_width, b->cursor_height);
354 goto err;
355 }
356
Daniel Stonee404b722019-06-22 18:40:31 +0100357 if (plane_state->src_x != 0 || plane_state->src_y != 0 ||
358 plane_state->src_w > (unsigned) b->cursor_width << 16 ||
359 plane_state->src_h > (unsigned) b->cursor_height << 16 ||
360 plane_state->src_w != plane_state->dest_w << 16 ||
361 plane_state->src_h != plane_state->dest_h << 16) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200362 drm_debug(b, "\t\t\t\t[%s] not assigning view %p to %s plane "
363 "(positioning requires cropping or scaling)\n",
364 p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100365 goto err;
366 }
367
368 /* Since we're setting plane state up front, we need to work out
369 * whether or not we need to upload a new cursor. We can't use the
370 * plane damage, since the planes haven't actually been calculated
371 * yet: instead try to figure it out directly. KMS cursor planes are
372 * pretty unique here, in that they lie partway between a Weston plane
373 * (direct scanout) and a renderer. */
374 if (ev != output->cursor_view ||
375 pixman_region32_not_empty(&ev->surface->damage)) {
376 output->current_cursor++;
377 output->current_cursor =
378 output->current_cursor %
379 ARRAY_LENGTH(output->gbm_cursor_fb);
380 needs_update = true;
381 }
382
383 output->cursor_view = ev;
384 plane_state->ev = ev;
385
386 plane_state->fb =
387 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
388
389 if (needs_update) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200390 drm_debug(b, "\t\t\t\t[%s] copying new content to cursor BO\n", p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100391 cursor_bo_update(plane_state, ev);
392 }
393
394 /* The cursor API is somewhat special: in cursor_bo_update(), we upload
395 * a buffer which is always cursor_width x cursor_height, even if the
396 * surface we want to promote is actually smaller than this. Manually
397 * mangle the plane state to deal with this. */
398 plane_state->src_w = b->cursor_width << 16;
399 plane_state->src_h = b->cursor_height << 16;
400 plane_state->dest_w = b->cursor_width;
401 plane_state->dest_h = b->cursor_height;
402
Marius Vlad36f11a52019-11-01 23:49:21 +0200403 drm_debug(b, "\t\t\t\t[%s] provisionally assigned view %p to cursor\n",
404 p_name, ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100405
406 return plane_state;
407
408err:
409 drm_plane_state_put_back(plane_state);
410 return NULL;
411}
Stefan Agnerccf24072019-07-09 22:02:00 +0200412#else
413static struct drm_plane_state *
414drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300415 struct weston_view *ev, uint64_t zpos)
Stefan Agnerccf24072019-07-09 22:02:00 +0200416{
417 return NULL;
418}
419#endif
Daniel Stonee404b722019-06-22 18:40:31 +0100420
421static struct drm_plane_state *
422drm_output_prepare_scanout_view(struct drm_output_state *output_state,
423 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300424 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200425 struct drm_fb *fb, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100426{
427 struct drm_output *output = output_state->output;
428 struct drm_backend *b = to_drm_backend(output->base.compositor);
429 struct drm_plane *scanout_plane = output->scanout_plane;
430 struct drm_plane_state *state;
Marius Vlad36f11a52019-11-01 23:49:21 +0200431 const char *p_name = drm_output_get_plane_type_name(scanout_plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100432
433 assert(!b->sprites_are_broken);
434 assert(b->atomic_modeset);
435 assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
436
437 /* Check the view spans exactly the output size, calculated in the
438 * logical co-ordinate space. */
Marius Vlad28bb2da2019-10-19 18:18:38 +0300439 if (!weston_view_matches_output_entirely(ev, &output->base)) {
440 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
441 " view does not match output entirely\n",
442 p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100443 return NULL;
Marius Vlad28bb2da2019-10-19 18:18:38 +0300444 }
Daniel Stonee404b722019-06-22 18:40:31 +0100445
446 /* If the surface buffer has an in-fence fd, but the plane doesn't
447 * support fences, we can't place the buffer on this plane. */
448 if (ev->surface->acquire_fence_fd >= 0 &&
Marius Vlad36f11a52019-11-01 23:49:21 +0200449 scanout_plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
450 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
451 "no in-fence support\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100452 return NULL;
Marius Vlad36f11a52019-11-01 23:49:21 +0200453 }
Daniel Stonee404b722019-06-22 18:40:31 +0100454
Daniel Stonee404b722019-06-22 18:40:31 +0100455 if (!fb) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200456 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
457 " couldn't get fb\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100458 return NULL;
459 }
460
461 state = drm_output_state_get_plane(output_state, scanout_plane);
462
463 /* The only way we can already have a buffer in the scanout plane is
464 * if we are in mixed mode, or if a client buffer has already been
465 * placed into scanout. The former case will never call into here,
466 * and in the latter case, the view must have been marked as occluded,
467 * meaning we should never have ended up here. */
468 assert(!state->fb);
Marius Vladeef69452019-10-31 12:48:35 +0200469
470 /* take another reference here to live within the state */
471 state->fb = drm_fb_ref(fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100472 state->ev = ev;
473 state->output = output;
Marius Vlad36f11a52019-11-01 23:49:21 +0200474 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
475 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
476 "unsuitable transform\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100477 goto err;
Marius Vlad36f11a52019-11-01 23:49:21 +0200478 }
Daniel Stonee404b722019-06-22 18:40:31 +0100479
480 if (state->dest_x != 0 || state->dest_y != 0 ||
481 state->dest_w != (unsigned) output->base.current_mode->width ||
Marius Vlad36f11a52019-11-01 23:49:21 +0200482 state->dest_h != (unsigned) output->base.current_mode->height) {
483 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
484 " invalid plane state\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100485 goto err;
Marius Vlad36f11a52019-11-01 23:49:21 +0200486 }
Daniel Stonee404b722019-06-22 18:40:31 +0100487
488 state->in_fence_fd = ev->surface->acquire_fence_fd;
489
490 /* In plane-only mode, we don't need to test the state now, as we
491 * will only test it once at the end. */
492 return state;
493
494err:
495 drm_plane_state_put_back(state);
496 return NULL;
497}
498
Marius Vlad26dcce02019-10-24 14:49:33 +0300499static bool
500drm_output_plane_view_has_valid_format(struct drm_plane *plane,
501 struct drm_output_state *state,
502 struct weston_view *ev,
503 struct drm_fb *fb)
504{
505 /* depending on the type of the plane we have different requirements */
506 switch (plane->type) {
507 case WDRM_PLANE_TYPE_CURSOR:
508 return drm_output_plane_cursor_has_valid_format(ev);
509 case WDRM_PLANE_TYPE_OVERLAY:
510 return drm_output_plane_has_valid_format(plane, state, fb);
511 case WDRM_PLANE_TYPE_PRIMARY:
512 return drm_output_plane_has_valid_format(plane, state, fb);
513 default:
514 assert(0);
515 return false;
516 }
517
518 return false;
519}
520
Marius Vlad2538aac2019-10-14 11:05:30 +0300521static struct drm_plane_state *
522drm_output_try_view_on_plane(struct drm_plane *plane,
523 struct drm_output_state *state,
524 struct weston_view *ev,
525 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200526 struct drm_fb *fb, uint64_t zpos)
Marius Vlad2538aac2019-10-14 11:05:30 +0300527{
528 struct drm_backend *b = state->pending_state->backend;
529 struct weston_output *wet_output = &state->output->base;
530 bool view_matches_entire_output, scanout_has_view_assigned;
531 struct drm_plane *scanout_plane = state->output->scanout_plane;
Marius Vlad36f11a52019-11-01 23:49:21 +0200532 struct drm_plane_state *ps = NULL;
533 const char *p_name = drm_output_get_plane_type_name(plane);
534 enum {
535 NO_PLANES, /* generic err-handle */
536 NO_PLANES_ACCEPTED,
537 PLACED_ON_PLANE,
538 } availability = NO_PLANES;
Marius Vlad2538aac2019-10-14 11:05:30 +0300539
540 /* sanity checks in case we over/underflow zpos or pass incorrect
541 * values */
542 assert(zpos <= plane->zpos_max ||
543 zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
544
545 switch (plane->type) {
546 case WDRM_PLANE_TYPE_CURSOR:
547 if (b->cursors_are_broken) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200548 availability = NO_PLANES_ACCEPTED;
549 goto out;
Marius Vlad2538aac2019-10-14 11:05:30 +0300550 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200551
552 ps = drm_output_prepare_cursor_view(state, ev, zpos);
553 if (ps)
554 availability = PLACED_ON_PLANE;
555 break;
Marius Vlad2538aac2019-10-14 11:05:30 +0300556 case WDRM_PLANE_TYPE_OVERLAY:
557 /* do not attempt to place it in the overlay if we don't have
558 * anything in the scanout/primary and the view doesn't cover
559 * the entire output */
560 view_matches_entire_output =
561 weston_view_matches_output_entirely(ev, wet_output);
562 scanout_has_view_assigned =
563 drm_output_check_plane_has_view_assigned(scanout_plane,
564 state);
565
566 if (view_matches_entire_output && !scanout_has_view_assigned) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200567 availability = NO_PLANES_ACCEPTED;
568 goto out;
Marius Vlad2538aac2019-10-14 11:05:30 +0300569 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200570
571 ps = drm_output_prepare_overlay_view(plane, state, ev, mode,
572 fb, zpos);
573 if (ps)
574 availability = PLACED_ON_PLANE;
575 break;
Marius Vlad2538aac2019-10-14 11:05:30 +0300576 case WDRM_PLANE_TYPE_PRIMARY:
577 if (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200578 availability = NO_PLANES_ACCEPTED;
579 goto out;
Marius Vlad2538aac2019-10-14 11:05:30 +0300580 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200581
582 ps = drm_output_prepare_scanout_view(state, ev, mode,
583 fb, zpos);
584 if (ps)
585 availability = PLACED_ON_PLANE;
586 break;
Marius Vlad2538aac2019-10-14 11:05:30 +0300587 default:
588 assert(0);
589 break;
590 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200591
592out:
593 switch (availability) {
594 case NO_PLANES:
595 /* set initial to this catch-all case, such that
596 * prepare_cursor/overlay/scanout() should have/contain the
597 * reason for failling */
598 break;
599 case NO_PLANES_ACCEPTED:
600 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
601 "place view %p in %s\n",
602 plane->plane_id, ev, p_name);
603 break;
604 case PLACED_ON_PLANE:
605 break;
606 }
607
608
609 return ps;
Marius Vlad2538aac2019-10-14 11:05:30 +0300610}
611
Marius Vlad48bc5ef2019-11-15 14:32:08 +0200612static void
Marius Vlad2538aac2019-10-14 11:05:30 +0300613drm_output_check_zpos_plane_states(struct drm_output_state *state)
614{
Marius Vlad2538aac2019-10-14 11:05:30 +0300615 struct drm_plane_state *ps;
Marius Vlad2538aac2019-10-14 11:05:30 +0300616
617 wl_list_for_each(ps, &state->plane_list, link) {
618 struct wl_list *next_node = ps->link.next;
619 bool found_dup = false;
620
Marius Vlad788e80d2019-11-15 00:25:11 +0200621 /* skip any plane that is not enabled */
622 if (!ps->fb)
623 continue;
624
625 assert(ps->zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
626
Marius Vlad2538aac2019-10-14 11:05:30 +0300627 /* find another plane with the same zpos value */
628 if (next_node == &state->plane_list)
629 break;
630
631 while (next_node && next_node != &state->plane_list) {
632 struct drm_plane_state *ps_next;
633
634 ps_next = container_of(next_node,
635 struct drm_plane_state,
636 link);
637
638 if (ps->zpos == ps_next->zpos) {
639 found_dup = true;
640 break;
641 }
642 next_node = next_node->next;
643 }
644
Marius Vlad48bc5ef2019-11-15 14:32:08 +0200645 /* this should never happen so exit hard in case
646 * we screwed up that bad */
647 assert(!found_dup);
Marius Vlad2538aac2019-10-14 11:05:30 +0300648 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300649}
650
651static struct drm_plane_state *
652drm_output_prepare_plane_view(struct drm_output_state *state,
653 struct weston_view *ev,
654 enum drm_output_propose_state_mode mode,
Marius Vladbd002b92019-11-15 13:33:38 +0200655 struct drm_plane_state *scanout_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300656 uint64_t current_lowest_zpos)
657{
658 struct drm_output *output = state->output;
659 struct drm_backend *b = to_drm_backend(output->base.compositor);
660
661 struct drm_plane_state *ps = NULL;
662 struct drm_plane *plane;
663 struct drm_plane_zpos *p_zpos, *p_zpos_next;
664 struct wl_list zpos_candidate_list;
665
Leandro Ribeiro4b5df8b2021-03-23 16:30:09 -0300666 struct weston_buffer *buffer;
667 struct wl_shm_buffer *shmbuf;
Marius Vlad26dcce02019-10-24 14:49:33 +0300668 struct drm_fb *fb;
669
Marius Vlad2538aac2019-10-14 11:05:30 +0300670 wl_list_init(&zpos_candidate_list);
671
672 /* check view for valid buffer, doesn't make sense to even try */
673 if (!weston_view_has_valid_buffer(ev))
674 return ps;
675
Leandro Ribeiro4b5df8b2021-03-23 16:30:09 -0300676 buffer = ev->surface->buffer_ref.buffer;
677 shmbuf = wl_shm_buffer_get(buffer->resource);
Marius Vlad26dcce02019-10-24 14:49:33 +0300678 fb = drm_fb_get_from_view(state, ev);
679
Marius Vlad2538aac2019-10-14 11:05:30 +0300680 /* assemble a list with possible candidates */
681 wl_list_for_each(plane, &b->plane_list, link) {
682 if (!drm_plane_is_available(plane, output))
683 continue;
684
685 if (drm_output_check_plane_has_view_assigned(plane, state)) {
686 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to"
687 " candidate list: view already assigned "
688 "to a plane\n", plane->plane_id);
689 continue;
690 }
691
692 if (plane->zpos_min >= current_lowest_zpos) {
693 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
Maxime Roussin-Bélanger35e34502020-12-17 17:08:56 -0500694 "candidate list: minimum zpos (%"PRIu64") "
Marius Vlad2538aac2019-10-14 11:05:30 +0300695 "plane's above current lowest zpos "
696 "(%"PRIu64")\n", plane->plane_id,
697 plane->zpos_min, current_lowest_zpos);
698 continue;
699 }
700
Marius Vladbd002b92019-11-15 13:33:38 +0200701 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
702 assert(scanout_state != NULL);
703 if (scanout_state->zpos >= plane->zpos_max) {
704 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
705 "candidate list: primary's zpos "
706 "value (%"PRIu64") higher than "
707 "plane's maximum value (%"PRIu64")\n",
708 plane->plane_id, scanout_state->zpos,
709 plane->zpos_max);
710 continue;
711 }
712 }
713
Marius Vlad18462e82019-11-15 14:11:49 +0200714 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY &&
715 (plane->type == WDRM_PLANE_TYPE_OVERLAY ||
716 plane->type == WDRM_PLANE_TYPE_PRIMARY)) {
717 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
718 "candidate list: renderer-only mode\n",
719 plane->plane_id);
720 continue;
721 }
722
723 if (plane->type != WDRM_PLANE_TYPE_CURSOR &&
724 b->sprites_are_broken) {
725 drm_debug(b, "\t\t\t\t[plane] not adding plane %d, type %s to "
726 "candidate list: sprites are broken!\n",
727 plane->plane_id,
728 drm_output_get_plane_type_name(plane));
729 continue;
730 }
731
Leandro Ribeiro4b5df8b2021-03-23 16:30:09 -0300732 if (plane->type == WDRM_PLANE_TYPE_CURSOR && !shmbuf) {
733 drm_debug(b, "\t\t\t\t[plane] not adding plane %d, type cursor to "
734 "candidate list: cursor planes only support wl_shm "
735 "buffers and the view buffer is of another type\n",
736 plane->plane_id);
737 continue;
738 }
739
Marius Vlad26dcce02019-10-24 14:49:33 +0300740 if (!drm_output_plane_view_has_valid_format(plane, state, ev, fb)) {
741 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
742 "candidate list: invalid pixel format\n",
743 plane->plane_id);
744 continue;
745 }
746
Marius Vlad2538aac2019-10-14 11:05:30 +0300747 drm_output_add_zpos_plane(plane, &zpos_candidate_list);
748 }
749
750 /* go over the potential candidate list and try to find a possible
751 * plane suitable for \c ev; start with the highest zpos value of a
752 * plane to maximize our chances, but do note we pass the zpos value
753 * based on current tracked value by \c current_lowest_zpos_in_use */
754 while (!wl_list_empty(&zpos_candidate_list)) {
755 struct drm_plane_zpos *head_p_zpos =
756 wl_container_of(zpos_candidate_list.next,
757 head_p_zpos, link);
758 struct drm_plane *plane = head_p_zpos->plane;
759 const char *p_name = drm_output_get_plane_type_name(plane);
760 uint64_t zpos;
761
762 if (current_lowest_zpos == DRM_PLANE_ZPOS_INVALID_PLANE)
763 zpos = plane->zpos_max;
764 else
765 zpos = MIN(current_lowest_zpos - 1, plane->zpos_max);
766
767 drm_debug(b, "\t\t\t\t[plane] plane %d picked "
768 "from candidate list, type: %s\n",
769 plane->plane_id, p_name);
770
Marius Vladeef69452019-10-31 12:48:35 +0200771 ps = drm_output_try_view_on_plane(plane, state, ev,
772 mode, fb, zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300773 drm_output_destroy_zpos_plane(head_p_zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300774 if (ps) {
775 drm_debug(b, "\t\t\t\t[view] view %p has been placed to "
776 "%s plane with computed zpos %"PRIu64"\n",
777 ev, p_name, zpos);
778 break;
779 }
780 }
781
782 wl_list_for_each_safe(p_zpos, p_zpos_next, &zpos_candidate_list, link)
783 drm_output_destroy_zpos_plane(p_zpos);
784
Marius Vlad26dcce02019-10-24 14:49:33 +0300785 drm_fb_unref(fb);
Marius Vlad2538aac2019-10-14 11:05:30 +0300786 return ps;
787}
788
Daniel Stonee404b722019-06-22 18:40:31 +0100789static struct drm_output_state *
790drm_output_propose_state(struct weston_output *output_base,
791 struct drm_pending_state *pending_state,
792 enum drm_output_propose_state_mode mode)
793{
794 struct drm_output *output = to_drm_output(output_base);
795 struct drm_backend *b = to_drm_backend(output->base.compositor);
796 struct drm_output_state *state;
797 struct drm_plane_state *scanout_state = NULL;
798 struct weston_view *ev;
Marius Vlad80a62e52019-09-11 18:21:59 +0300799
Daniel Stone8ca5d732020-08-25 17:44:35 +0100800 pixman_region32_t renderer_region;
Marius Vlad80a62e52019-09-11 18:21:59 +0300801 pixman_region32_t occluded_region;
802
Daniel Stonee404b722019-06-22 18:40:31 +0100803 bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
804 int ret;
Marius Vlad2538aac2019-10-14 11:05:30 +0300805 uint64_t current_lowest_zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stonee404b722019-06-22 18:40:31 +0100806
807 assert(!output->state_last);
808 state = drm_output_state_duplicate(output->state_cur,
809 pending_state,
810 DRM_OUTPUT_STATE_CLEAR_PLANES);
811
812 /* We implement mixed mode by progressively creating and testing
813 * incremental states, of scanout + overlay + cursor. Since we
814 * walk our views top to bottom, the scanout plane is last, however
815 * we always need it in our scene for the test modeset to be
816 * meaningful. To do this, we steal a reference to the last
817 * renderer framebuffer we have, if we think it's basically
818 * compatible. If we don't have that, then we conservatively fall
819 * back to only using the renderer for this repaint. */
820 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
821 struct drm_plane *plane = output->scanout_plane;
822 struct drm_fb *scanout_fb = plane->state_cur->fb;
823
824 if (!scanout_fb ||
825 (scanout_fb->type != BUFFER_GBM_SURFACE &&
826 scanout_fb->type != BUFFER_PIXMAN_DUMB)) {
827 drm_debug(b, "\t\t[state] cannot propose mixed mode: "
828 "for output %s (%lu): no previous renderer "
829 "fb\n",
830 output->base.name,
831 (unsigned long) output->base.id);
832 drm_output_state_free(state);
833 return NULL;
834 }
835
836 if (scanout_fb->width != output_base->current_mode->width ||
837 scanout_fb->height != output_base->current_mode->height) {
838 drm_debug(b, "\t\t[state] cannot propose mixed mode "
839 "for output %s (%lu): previous fb has "
840 "different size\n",
841 output->base.name,
842 (unsigned long) output->base.id);
843 drm_output_state_free(state);
844 return NULL;
845 }
846
847 scanout_state = drm_plane_state_duplicate(state,
848 plane->state_cur);
Daniel Stone71e6ba52020-08-25 17:46:27 +0100849 /* assign the primary the lowest zpos value */
Marius Vladbd002b92019-11-15 13:33:38 +0200850 scanout_state->zpos = plane->zpos_min;
Daniel Stonee404b722019-06-22 18:40:31 +0100851 drm_debug(b, "\t\t[state] using renderer FB ID %lu for mixed "
852 "mode for output %s (%lu)\n",
853 (unsigned long) scanout_fb->fb_id, output->base.name,
854 (unsigned long) output->base.id);
Marius Vladbd002b92019-11-15 13:33:38 +0200855 drm_debug(b, "\t\t[state] scanout will use for zpos %"PRIu64"\n",
856 scanout_state->zpos);
Daniel Stonee404b722019-06-22 18:40:31 +0100857 }
858
Marius Vlad80a62e52019-09-11 18:21:59 +0300859 /* - renderer_region contains the total region which which will be
860 * covered by the renderer
Marius Vlad80a62e52019-09-11 18:21:59 +0300861 * - occluded_region contains the total region which which will be
862 * covered by the renderer and hardware planes, where the view's
863 * visible-and-opaque region is added in both cases (the view's
864 * opaque region accumulates there for each view); it is being used
865 * to skip the view, if it is completely occluded; includes the
866 * situation where occluded_region covers entire output's region.
Daniel Stonee404b722019-06-22 18:40:31 +0100867 */
868 pixman_region32_init(&renderer_region);
869 pixman_region32_init(&occluded_region);
870
871 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
872 struct drm_plane_state *ps = NULL;
873 bool force_renderer = false;
874 pixman_region32_t clipped_view;
Daniel Stone8ca5d732020-08-25 17:44:35 +0100875 pixman_region32_t surface_overlap;
Daniel Stonee404b722019-06-22 18:40:31 +0100876 bool totally_occluded = false;
Daniel Stonee404b722019-06-22 18:40:31 +0100877
878 drm_debug(b, "\t\t\t[view] evaluating view %p for "
879 "output %s (%lu)\n",
880 ev, output->base.name,
881 (unsigned long) output->base.id);
882
883 /* If this view doesn't touch our output at all, there's no
884 * reason to do anything with it. */
885 if (!(ev->output_mask & (1u << output->base.id))) {
886 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
887 "(not on our output)\n", ev);
888 continue;
889 }
890
Daniel Stonee404b722019-06-22 18:40:31 +0100891 /* Ignore views we know to be totally occluded. */
892 pixman_region32_init(&clipped_view);
893 pixman_region32_intersect(&clipped_view,
894 &ev->transform.boundingbox,
895 &output->base.region);
896
897 pixman_region32_init(&surface_overlap);
898 pixman_region32_subtract(&surface_overlap, &clipped_view,
899 &occluded_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300900 /* if the view is completely occluded then ignore that
901 * view; includes the case where occluded_region covers
902 * the entire output */
Daniel Stonee404b722019-06-22 18:40:31 +0100903 totally_occluded = !pixman_region32_not_empty(&surface_overlap);
904 if (totally_occluded) {
905 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
906 "(occluded on our output)\n", ev);
907 pixman_region32_fini(&surface_overlap);
908 pixman_region32_fini(&clipped_view);
909 continue;
910 }
911
Michael Olbrich3097acc2020-07-14 11:18:54 +0200912 /* We only assign planes to views which are exclusively present
913 * on our output. */
914 if (ev->output_mask != (1u << output->base.id)) {
915 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
916 "(on multiple outputs)\n", ev);
917 force_renderer = true;
918 }
919
920 if (!weston_view_has_valid_buffer(ev)) {
921 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
922 "(no buffer available)\n", ev);
923 force_renderer = true;
924 }
925
Daniel Stonee404b722019-06-22 18:40:31 +0100926 /* Since we process views from top to bottom, we know that if
927 * the view intersects the calculated renderer region, it must
928 * be part of, or occluded by, it, and cannot go on a plane. */
929 pixman_region32_intersect(&surface_overlap, &renderer_region,
930 &clipped_view);
931 if (pixman_region32_not_empty(&surface_overlap)) {
932 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
933 "(occluded by renderer views)\n", ev);
934 force_renderer = true;
935 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300936 pixman_region32_fini(&surface_overlap);
Daniel Stonee404b722019-06-22 18:40:31 +0100937
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530938 /* In case of enforced mode of content-protection do not
939 * assign planes for a protected surface on an unsecured output.
940 */
941 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
942 ev->surface->desired_protection > output_base->current_protection) {
943 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
944 "(enforced protection mode on unsecured output)\n", ev);
945 force_renderer = true;
946 }
947
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100948 /* Now try to place it on a plane if we can. */
Marius Vlad2538aac2019-10-14 11:05:30 +0300949 if (!force_renderer) {
950 drm_debug(b, "\t\t\t[plane] started with zpos %"PRIu64"\n",
951 current_lowest_zpos);
952 ps = drm_output_prepare_plane_view(state, ev, mode,
Marius Vladbd002b92019-11-15 13:33:38 +0200953 scanout_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300954 current_lowest_zpos);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000955 }
Daniel Stone2cb926c2019-11-11 16:48:54 +0000956
957 if (ps) {
Marius Vlad2538aac2019-10-14 11:05:30 +0300958 current_lowest_zpos = ps->zpos;
959 drm_debug(b, "\t\t\t[plane] next zpos to use %"PRIu64"\n",
960 current_lowest_zpos);
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100961 } else if (!ps && !renderer_ok) {
Daniel Stonee404b722019-06-22 18:40:31 +0100962 drm_debug(b, "\t\t[view] failing state generation: "
963 "placing view %p to renderer not allowed\n",
964 ev);
965 pixman_region32_fini(&clipped_view);
966 goto err_region;
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100967 } else if (!ps) {
968 /* clipped_view contains the area that's going to be
969 * visible on screen; add this to the renderer region */
970 pixman_region32_union(&renderer_region,
971 &renderer_region,
972 &clipped_view);
Daniel Stonee404b722019-06-22 18:40:31 +0100973
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100974 drm_debug(b, "\t\t\t\t[view] view %p will be placed "
975 "on the renderer\n", ev);
976 }
Marius Vlad80a62e52019-09-11 18:21:59 +0300977
Daniel Stoneb625cdc2020-08-25 17:37:34 +0100978 /* Opaque areas of our clipped view occlude areas behind it;
979 * however, anything not in the opaque region (which is the
980 * entire clipped area if the whole view is known to be
981 * opaque) does not necessarily occlude what's behind it, as
982 * it could be alpha-blended. */
Marius Vlad80a62e52019-09-11 18:21:59 +0300983 if (!weston_view_is_opaque(ev, &clipped_view))
984 pixman_region32_intersect(&clipped_view,
985 &clipped_view,
986 &ev->transform.opaque);
Marius Vlad80a62e52019-09-11 18:21:59 +0300987 pixman_region32_union(&occluded_region,
988 &occluded_region,
989 &clipped_view);
990
Daniel Stonee404b722019-06-22 18:40:31 +0100991 pixman_region32_fini(&clipped_view);
992 }
Marius Vlad80a62e52019-09-11 18:21:59 +0300993
Daniel Stonee404b722019-06-22 18:40:31 +0100994 pixman_region32_fini(&renderer_region);
995 pixman_region32_fini(&occluded_region);
996
997 /* In renderer-only mode, we can't test the state as we don't have a
998 * renderer buffer yet. */
999 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY)
1000 return state;
1001
Marius Vlad2538aac2019-10-14 11:05:30 +03001002 /* check if we have invalid zpos values, like duplicate(s) */
Marius Vlad48bc5ef2019-11-15 14:32:08 +02001003 drm_output_check_zpos_plane_states(state);
Marius Vlad2538aac2019-10-14 11:05:30 +03001004
Daniel Stonee404b722019-06-22 18:40:31 +01001005 /* Check to see if this state will actually work. */
1006 ret = drm_pending_state_test(state->pending_state);
1007 if (ret != 0) {
1008 drm_debug(b, "\t\t[view] failing state generation: "
1009 "atomic test not OK\n");
1010 goto err;
1011 }
1012
1013 /* Counterpart to duplicating scanout state at the top of this
1014 * function: if we have taken a renderer framebuffer and placed it in
1015 * the pending state in order to incrementally test overlay planes,
1016 * remove it now. */
1017 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
1018 assert(scanout_state->fb->type == BUFFER_GBM_SURFACE ||
1019 scanout_state->fb->type == BUFFER_PIXMAN_DUMB);
1020 drm_plane_state_put_back(scanout_state);
1021 }
1022 return state;
1023
1024err_region:
1025 pixman_region32_fini(&renderer_region);
1026 pixman_region32_fini(&occluded_region);
1027err:
1028 drm_output_state_free(state);
1029 return NULL;
1030}
1031
1032void
1033drm_assign_planes(struct weston_output *output_base, void *repaint_data)
1034{
1035 struct drm_backend *b = to_drm_backend(output_base->compositor);
1036 struct drm_pending_state *pending_state = repaint_data;
1037 struct drm_output *output = to_drm_output(output_base);
1038 struct drm_output_state *state = NULL;
1039 struct drm_plane_state *plane_state;
1040 struct weston_view *ev;
1041 struct weston_plane *primary = &output_base->compositor->primary_plane;
1042 enum drm_output_propose_state_mode mode = DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY;
1043
1044 drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n",
1045 output_base->name, (unsigned long) output_base->id);
1046
1047 if (!b->sprites_are_broken && !output->virtual) {
1048 drm_debug(b, "\t[repaint] trying planes-only build state\n");
1049 state = drm_output_propose_state(output_base, pending_state, mode);
1050 if (!state) {
1051 drm_debug(b, "\t[repaint] could not build planes-only "
1052 "state, trying mixed\n");
1053 mode = DRM_OUTPUT_PROPOSE_STATE_MIXED;
1054 state = drm_output_propose_state(output_base,
1055 pending_state,
1056 mode);
1057 }
1058 if (!state) {
1059 drm_debug(b, "\t[repaint] could not build mixed-mode "
1060 "state, trying renderer-only\n");
1061 }
1062 } else {
1063 drm_debug(b, "\t[state] no overlay plane support\n");
1064 }
1065
1066 if (!state) {
1067 mode = DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY;
1068 state = drm_output_propose_state(output_base, pending_state,
1069 mode);
1070 }
1071
1072 assert(state);
1073 drm_debug(b, "\t[repaint] Using %s composition\n",
1074 drm_propose_state_mode_to_string(mode));
1075
1076 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
1077 struct drm_plane *target_plane = NULL;
1078
1079 /* If this view doesn't touch our output at all, there's no
1080 * reason to do anything with it. */
1081 if (!(ev->output_mask & (1u << output->base.id)))
1082 continue;
1083
1084 /* Test whether this buffer can ever go into a plane:
1085 * non-shm, or small enough to be a cursor.
1086 *
1087 * Also, keep a reference when using the pixman renderer.
1088 * That makes it possible to do a seamless switch to the GL
1089 * renderer and since the pixman renderer keeps a reference
1090 * to the buffer anyway, there is no side effects.
1091 */
1092 if (b->use_pixman ||
Marius Vlad5f6bee42019-09-11 16:41:04 +03001093 (weston_view_has_valid_buffer(ev) &&
Daniel Stonee404b722019-06-22 18:40:31 +01001094 (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
1095 (ev->surface->width <= b->cursor_width &&
1096 ev->surface->height <= b->cursor_height))))
1097 ev->surface->keep_buffer = true;
1098 else
1099 ev->surface->keep_buffer = false;
1100
1101 /* This is a bit unpleasant, but lacking a temporary place to
1102 * hang a plane off the view, we have to do a nested walk.
1103 * Our first-order iteration has to be planes rather than
1104 * views, because otherwise we won't reset views which were
1105 * previously on planes to being on the primary plane. */
1106 wl_list_for_each(plane_state, &state->plane_list, link) {
1107 if (plane_state->ev == ev) {
1108 plane_state->ev = NULL;
1109 target_plane = plane_state->plane;
1110 break;
1111 }
1112 }
1113
1114 if (target_plane) {
1115 drm_debug(b, "\t[repaint] view %p on %s plane %lu\n",
1116 ev, plane_type_enums[target_plane->type].name,
1117 (unsigned long) target_plane->plane_id);
1118 weston_view_move_to_plane(ev, &target_plane->base);
1119 } else {
1120 drm_debug(b, "\t[repaint] view %p using renderer "
1121 "composition\n", ev);
1122 weston_view_move_to_plane(ev, primary);
1123 }
1124
1125 if (!target_plane ||
1126 target_plane->type == WDRM_PLANE_TYPE_CURSOR) {
1127 /* cursor plane & renderer involve a copy */
1128 ev->psf_flags = 0;
1129 } else {
1130 /* All other planes are a direct scanout of a
1131 * single client buffer.
1132 */
1133 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1134 }
1135 }
1136
1137 /* We rely on output->cursor_view being both an accurate reflection of
1138 * the cursor plane's state, but also being maintained across repaints
1139 * to avoid unnecessary damage uploads, per the comment in
1140 * drm_output_prepare_cursor_view. In the event that we go from having
1141 * a cursor view to not having a cursor view, we need to clear it. */
1142 if (output->cursor_view) {
1143 plane_state =
1144 drm_output_state_get_existing_plane(state,
1145 output->cursor_plane);
1146 if (!plane_state || !plane_state->fb)
1147 output->cursor_view = NULL;
1148 }
1149}