blob: 7d180f421f33ac71e04f046f88589db0725db749 [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;
Marius Vlad4eeb4022019-10-14 00:27:05 +0300132 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 Vlad26dcce02019-10-24 14:49:33 +0300153 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 Vlad4eeb4022019-10-14 00:27:05 +0300163 return false;
164}
165
Marius Vlad3b13f562019-10-14 00:29:18 +0300166static bool
167drm_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 Stonee404b722019-06-22 18:40:31 +0100178static struct drm_plane_state *
Daniel Stone2cb926c2019-11-11 16:48:54 +0000179drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Daniel Stonee404b722019-06-22 18:40:31 +0100180 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300181 enum drm_output_propose_state_mode mode,
182 uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100183{
184 struct drm_output *output = output_state->output;
185 struct weston_compositor *ec = output->base.compositor;
186 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000187 struct drm_plane *p;
Daniel Stonee404b722019-06-22 18:40:31 +0100188 struct drm_plane_state *state = NULL;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000189 struct drm_fb *fb;
Daniel Stonee404b722019-06-22 18:40:31 +0100190 int ret;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000191 enum {
192 NO_PLANES,
Daniel Stone2cb926c2019-11-11 16:48:54 +0000193 NO_PLANES_ACCEPTED,
194 PLACED_ON_PLANE,
195 } availability = NO_PLANES;
Daniel Stonee404b722019-06-22 18:40:31 +0100196
197 assert(!b->sprites_are_broken);
198 assert(b->atomic_modeset);
199
Daniel Stone2cb926c2019-11-11 16:48:54 +0000200 fb = drm_fb_get_from_view(output_state, ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100201 if (!fb) {
202 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
203 " couldn't get fb\n", ev);
204 return NULL;
205 }
206
Daniel Stone2cb926c2019-11-11 16:48:54 +0000207 wl_list_for_each(p, &b->plane_list, link) {
208 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
209 continue;
Daniel Stonee404b722019-06-22 18:40:31 +0100210
Daniel Stone2cb926c2019-11-11 16:48:54 +0000211 if (!drm_plane_is_available(p, output))
212 continue;
Daniel Stonee404b722019-06-22 18:40:31 +0100213
Daniel Stone2cb926c2019-11-11 16:48:54 +0000214 state = drm_output_state_get_plane(output_state, p);
215 if (state->fb) {
216 state = NULL;
217 continue;
218 }
219
Daniel Stone2cb926c2019-11-11 16:48:54 +0000220 state->ev = ev;
221 state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300222 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000223 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
224 "unsuitable transform\n", ev);
225 drm_plane_state_put_back(state);
226 state = NULL;
227 continue;
228 }
229
230 /* If the surface buffer has an in-fence fd, but the plane
231 * doesn't support fences, we can't place the buffer on this
232 * plane. */
233 if (ev->surface->acquire_fence_fd >= 0 &&
234 p->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
235 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
236 "no in-fence support\n", ev);
237 drm_plane_state_put_back(state);
238 state = NULL;
239 continue;
240 }
241
242 /* We hold one reference for the lifetime of this function;
243 * from calling drm_fb_get_from_view, to the out label where
244 * we unconditionally drop the reference. So, we take another
245 * reference here to live within the state. */
246 state->fb = drm_fb_ref(fb);
247
248 state->in_fence_fd = ev->surface->acquire_fence_fd;
249
250 /* In planes-only mode, we don't have an incremental state to
251 * test against, so we just hope it'll work. */
252 if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
253 drm_debug(b, "\t\t\t\t[overlay] provisionally placing "
254 "view %p on overlay %lu in planes-only mode\n",
255 ev, (unsigned long) p->plane_id);
256 availability = PLACED_ON_PLANE;
257 goto out;
258 }
259
260 ret = drm_pending_state_test(output_state->pending_state);
261 if (ret == 0) {
262 drm_debug(b, "\t\t\t\t[overlay] provisionally placing "
263 "view %p on overlay %d in mixed mode\n",
264 ev, p->plane_id);
265 availability = PLACED_ON_PLANE;
266 goto out;
267 }
268
269 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay %lu "
270 "in mixed mode: kernel test failed\n",
271 ev, (unsigned long) p->plane_id);
272
Daniel Stonee404b722019-06-22 18:40:31 +0100273 drm_plane_state_put_back(state);
274 state = NULL;
275 }
276
Daniel Stone2cb926c2019-11-11 16:48:54 +0000277 switch (availability) {
278 case NO_PLANES:
Daniel Stonee404b722019-06-22 18:40:31 +0100279 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
Daniel Stone2cb926c2019-11-11 16:48:54 +0000280 "no free overlay planes\n", ev);
281 break;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000282 case NO_PLANES_ACCEPTED:
283 case PLACED_ON_PLANE:
284 break;
Daniel Stonee404b722019-06-22 18:40:31 +0100285 }
286
287out:
Daniel Stone2cb926c2019-11-11 16:48:54 +0000288 drm_fb_unref(fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100289 return state;
290}
291
Stefan Agnerccf24072019-07-09 22:02:00 +0200292#ifdef BUILD_DRM_GBM
Daniel Stonee404b722019-06-22 18:40:31 +0100293/**
294 * Update the image for the current cursor surface
295 *
296 * @param plane_state DRM cursor plane state
297 * @param ev Source view for cursor
298 */
299static void
300cursor_bo_update(struct drm_plane_state *plane_state, struct weston_view *ev)
301{
302 struct drm_backend *b = plane_state->plane->backend;
303 struct gbm_bo *bo = plane_state->fb->bo;
304 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
305 uint32_t buf[b->cursor_width * b->cursor_height];
306 int32_t stride;
307 uint8_t *s;
308 int i;
309
310 assert(buffer && buffer->shm_buffer);
311 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
312 assert(buffer->width <= b->cursor_width);
313 assert(buffer->height <= b->cursor_height);
314
315 memset(buf, 0, sizeof buf);
316 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
317 s = wl_shm_buffer_get_data(buffer->shm_buffer);
318
319 wl_shm_buffer_begin_access(buffer->shm_buffer);
320 for (i = 0; i < buffer->height; i++)
321 memcpy(buf + i * b->cursor_width,
322 s + i * stride,
323 buffer->width * 4);
324 wl_shm_buffer_end_access(buffer->shm_buffer);
325
326 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
327 weston_log("failed update cursor: %s\n", strerror(errno));
328}
329
330static struct drm_plane_state *
331drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300332 struct weston_view *ev, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100333{
334 struct drm_output *output = output_state->output;
335 struct drm_backend *b = to_drm_backend(output->base.compositor);
336 struct drm_plane *plane = output->cursor_plane;
337 struct drm_plane_state *plane_state;
Daniel Stonee404b722019-06-22 18:40:31 +0100338 bool needs_update = false;
339
340 assert(!b->cursors_are_broken);
341
342 if (!plane)
343 return NULL;
344
345 if (!plane->state_cur->complete)
346 return NULL;
347
348 if (plane->state_cur->output && plane->state_cur->output != output)
349 return NULL;
350
351 /* We use GBM to import SHM buffers. */
352 if (b->gbm == NULL)
353 return NULL;
354
Daniel Stonee404b722019-06-22 18:40:31 +0100355 plane_state =
356 drm_output_state_get_plane(output_state, output->cursor_plane);
357
358 if (plane_state && plane_state->fb)
359 return NULL;
360
361 /* We can't scale with the legacy API, and we don't try to account for
362 * simple cropping/translation in cursor_bo_update. */
363 plane_state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300364 if (!drm_plane_state_coords_for_view(plane_state, ev, zpos))
Daniel Stonee404b722019-06-22 18:40:31 +0100365 goto err;
366
367 if (plane_state->src_x != 0 || plane_state->src_y != 0 ||
368 plane_state->src_w > (unsigned) b->cursor_width << 16 ||
369 plane_state->src_h > (unsigned) b->cursor_height << 16 ||
370 plane_state->src_w != plane_state->dest_w << 16 ||
371 plane_state->src_h != plane_state->dest_h << 16) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000372 drm_debug(b, "\t\t\t\t[cursor] not assigning view %p to cursor plane "
373 "(positioning requires cropping or scaling)\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100374 goto err;
375 }
376
377 /* Since we're setting plane state up front, we need to work out
378 * whether or not we need to upload a new cursor. We can't use the
379 * plane damage, since the planes haven't actually been calculated
380 * yet: instead try to figure it out directly. KMS cursor planes are
381 * pretty unique here, in that they lie partway between a Weston plane
382 * (direct scanout) and a renderer. */
383 if (ev != output->cursor_view ||
384 pixman_region32_not_empty(&ev->surface->damage)) {
385 output->current_cursor++;
386 output->current_cursor =
387 output->current_cursor %
388 ARRAY_LENGTH(output->gbm_cursor_fb);
389 needs_update = true;
390 }
391
392 output->cursor_view = ev;
393 plane_state->ev = ev;
394
395 plane_state->fb =
396 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
397
398 if (needs_update) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000399 drm_debug(b, "\t\t\t\t[cursor] copying new content to cursor BO\n");
Daniel Stonee404b722019-06-22 18:40:31 +0100400 cursor_bo_update(plane_state, ev);
401 }
402
403 /* The cursor API is somewhat special: in cursor_bo_update(), we upload
404 * a buffer which is always cursor_width x cursor_height, even if the
405 * surface we want to promote is actually smaller than this. Manually
406 * mangle the plane state to deal with this. */
407 plane_state->src_w = b->cursor_width << 16;
408 plane_state->src_h = b->cursor_height << 16;
409 plane_state->dest_w = b->cursor_width;
410 plane_state->dest_h = b->cursor_height;
411
Daniel Stone2cb926c2019-11-11 16:48:54 +0000412 drm_debug(b, "\t\t\t\t[cursor] provisionally assigned view %p to cursor\n",
413 ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100414
415 return plane_state;
416
417err:
418 drm_plane_state_put_back(plane_state);
419 return NULL;
420}
Stefan Agnerccf24072019-07-09 22:02:00 +0200421#else
422static struct drm_plane_state *
423drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300424 struct weston_view *ev, uint64_t zpos)
Stefan Agnerccf24072019-07-09 22:02:00 +0200425{
426 return NULL;
427}
428#endif
Daniel Stonee404b722019-06-22 18:40:31 +0100429
430static struct drm_plane_state *
431drm_output_prepare_scanout_view(struct drm_output_state *output_state,
432 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300433 enum drm_output_propose_state_mode mode,
434 uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100435{
436 struct drm_output *output = output_state->output;
437 struct drm_backend *b = to_drm_backend(output->base.compositor);
438 struct drm_plane *scanout_plane = output->scanout_plane;
439 struct drm_plane_state *state;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000440 struct drm_fb *fb;
Daniel Stonee404b722019-06-22 18:40:31 +0100441
442 assert(!b->sprites_are_broken);
443 assert(b->atomic_modeset);
444 assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
445
446 /* Check the view spans exactly the output size, calculated in the
447 * logical co-ordinate space. */
Marius Vlad47e3d1e2019-09-11 16:53:08 +0300448 if (!weston_view_matches_output_entirely(ev, &output->base))
Daniel Stonee404b722019-06-22 18:40:31 +0100449 return NULL;
450
451 /* If the surface buffer has an in-fence fd, but the plane doesn't
452 * support fences, we can't place the buffer on this plane. */
453 if (ev->surface->acquire_fence_fd >= 0 &&
Daniel Stone2cb926c2019-11-11 16:48:54 +0000454 scanout_plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0)
Daniel Stonee404b722019-06-22 18:40:31 +0100455 return NULL;
456
Daniel Stone2cb926c2019-11-11 16:48:54 +0000457 fb = drm_fb_get_from_view(output_state, ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100458 if (!fb) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000459 drm_debug(b, "\t\t\t\t[scanout] not placing view %p on scanout: "
460 " couldn't get fb\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100461 return NULL;
462 }
463
464 state = drm_output_state_get_plane(output_state, scanout_plane);
465
466 /* The only way we can already have a buffer in the scanout plane is
467 * if we are in mixed mode, or if a client buffer has already been
468 * placed into scanout. The former case will never call into here,
469 * and in the latter case, the view must have been marked as occluded,
470 * meaning we should never have ended up here. */
471 assert(!state->fb);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000472 state->fb = fb;
Daniel Stonee404b722019-06-22 18:40:31 +0100473 state->ev = ev;
474 state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300475 if (!drm_plane_state_coords_for_view(state, ev, zpos))
Daniel Stonee404b722019-06-22 18:40:31 +0100476 goto err;
477
478 if (state->dest_x != 0 || state->dest_y != 0 ||
479 state->dest_w != (unsigned) output->base.current_mode->width ||
Daniel Stone2cb926c2019-11-11 16:48:54 +0000480 state->dest_h != (unsigned) output->base.current_mode->height)
Daniel Stonee404b722019-06-22 18:40:31 +0100481 goto err;
482
483 state->in_fence_fd = ev->surface->acquire_fence_fd;
484
485 /* In plane-only mode, we don't need to test the state now, as we
486 * will only test it once at the end. */
487 return state;
488
489err:
490 drm_plane_state_put_back(state);
491 return NULL;
492}
493
Marius Vlad26dcce02019-10-24 14:49:33 +0300494static bool
495drm_output_plane_view_has_valid_format(struct drm_plane *plane,
496 struct drm_output_state *state,
497 struct weston_view *ev,
498 struct drm_fb *fb)
499{
500 /* depending on the type of the plane we have different requirements */
501 switch (plane->type) {
502 case WDRM_PLANE_TYPE_CURSOR:
503 return drm_output_plane_cursor_has_valid_format(ev);
504 case WDRM_PLANE_TYPE_OVERLAY:
505 return drm_output_plane_has_valid_format(plane, state, fb);
506 case WDRM_PLANE_TYPE_PRIMARY:
507 return drm_output_plane_has_valid_format(plane, state, fb);
508 default:
509 assert(0);
510 return false;
511 }
512
513 return false;
514}
515
Marius Vlad2538aac2019-10-14 11:05:30 +0300516static struct drm_plane_state *
517drm_output_try_view_on_plane(struct drm_plane *plane,
518 struct drm_output_state *state,
519 struct weston_view *ev,
520 enum drm_output_propose_state_mode mode,
521 uint64_t zpos)
522{
523 struct drm_backend *b = state->pending_state->backend;
524 struct weston_output *wet_output = &state->output->base;
525 bool view_matches_entire_output, scanout_has_view_assigned;
526 struct drm_plane *scanout_plane = state->output->scanout_plane;
527
528 /* sanity checks in case we over/underflow zpos or pass incorrect
529 * values */
530 assert(zpos <= plane->zpos_max ||
531 zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
532
533 switch (plane->type) {
534 case WDRM_PLANE_TYPE_CURSOR:
535 if (b->cursors_are_broken) {
536 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
537 "place view %p in cursor\n",
538 plane->plane_id, ev);
539 return NULL;
540 }
541 return drm_output_prepare_cursor_view(state, ev, zpos);
542 case WDRM_PLANE_TYPE_OVERLAY:
543 /* do not attempt to place it in the overlay if we don't have
544 * anything in the scanout/primary and the view doesn't cover
545 * the entire output */
546 view_matches_entire_output =
547 weston_view_matches_output_entirely(ev, wet_output);
548 scanout_has_view_assigned =
549 drm_output_check_plane_has_view_assigned(scanout_plane,
550 state);
551
552 if (view_matches_entire_output && !scanout_has_view_assigned) {
553 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
554 "place view %p in overlay\n",
555 plane->plane_id, ev);
556 return NULL;
557 }
558 return drm_output_prepare_overlay_view(state, ev, mode, zpos);
559 case WDRM_PLANE_TYPE_PRIMARY:
560 if (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
561 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
562 "place view %p in scanout\n",
563 plane->plane_id, ev);
564 return NULL;
565 }
566 return drm_output_prepare_scanout_view(state, ev, mode, zpos);
567 default:
568 assert(0);
569 break;
570 }
571}
572
573static int
574drm_output_check_zpos_plane_states(struct drm_output_state *state)
575{
576 struct drm_backend *b = state->pending_state->backend;
577 struct drm_plane_state *ps;
578 int ret = 0;
579
580 wl_list_for_each(ps, &state->plane_list, link) {
581 struct wl_list *next_node = ps->link.next;
582 bool found_dup = false;
583
584 /* find another plane with the same zpos value */
585 if (next_node == &state->plane_list)
586 break;
587
588 while (next_node && next_node != &state->plane_list) {
589 struct drm_plane_state *ps_next;
590
591 ps_next = container_of(next_node,
592 struct drm_plane_state,
593 link);
594
595 if (ps->zpos == ps_next->zpos) {
596 found_dup = true;
597 break;
598 }
599 next_node = next_node->next;
600 }
601
602 if (found_dup) {
603 ret = 1;
604 drm_debug(b, "\t\t\t[plane] found duplicate zpos values\n");
605 break;
606 }
607 }
608
609 return ret;
610}
611
612static struct drm_plane_state *
613drm_output_prepare_plane_view(struct drm_output_state *state,
614 struct weston_view *ev,
615 enum drm_output_propose_state_mode mode,
616 uint64_t current_lowest_zpos)
617{
618 struct drm_output *output = state->output;
619 struct drm_backend *b = to_drm_backend(output->base.compositor);
620
621 struct drm_plane_state *ps = NULL;
622 struct drm_plane *plane;
623 struct drm_plane_zpos *p_zpos, *p_zpos_next;
624 struct wl_list zpos_candidate_list;
625
Marius Vlad26dcce02019-10-24 14:49:33 +0300626 struct drm_fb *fb;
627
Marius Vlad2538aac2019-10-14 11:05:30 +0300628 wl_list_init(&zpos_candidate_list);
629
630 /* check view for valid buffer, doesn't make sense to even try */
631 if (!weston_view_has_valid_buffer(ev))
632 return ps;
633
Marius Vlad26dcce02019-10-24 14:49:33 +0300634 fb = drm_fb_get_from_view(state, ev);
635
Marius Vlad2538aac2019-10-14 11:05:30 +0300636 /* assemble a list with possible candidates */
637 wl_list_for_each(plane, &b->plane_list, link) {
638 if (!drm_plane_is_available(plane, output))
639 continue;
640
641 if (drm_output_check_plane_has_view_assigned(plane, state)) {
642 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to"
643 " candidate list: view already assigned "
644 "to a plane\n", plane->plane_id);
645 continue;
646 }
647
648 if (plane->zpos_min >= current_lowest_zpos) {
649 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
650 "candidate list: minium zpos (%"PRIu64") "
651 "plane's above current lowest zpos "
652 "(%"PRIu64")\n", plane->plane_id,
653 plane->zpos_min, current_lowest_zpos);
654 continue;
655 }
656
Marius Vlad26dcce02019-10-24 14:49:33 +0300657 if (!drm_output_plane_view_has_valid_format(plane, state, ev, fb)) {
658 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
659 "candidate list: invalid pixel format\n",
660 plane->plane_id);
661 continue;
662 }
663
Marius Vlad2538aac2019-10-14 11:05:30 +0300664 drm_output_add_zpos_plane(plane, &zpos_candidate_list);
665 }
666
667 /* go over the potential candidate list and try to find a possible
668 * plane suitable for \c ev; start with the highest zpos value of a
669 * plane to maximize our chances, but do note we pass the zpos value
670 * based on current tracked value by \c current_lowest_zpos_in_use */
671 while (!wl_list_empty(&zpos_candidate_list)) {
672 struct drm_plane_zpos *head_p_zpos =
673 wl_container_of(zpos_candidate_list.next,
674 head_p_zpos, link);
675 struct drm_plane *plane = head_p_zpos->plane;
676 const char *p_name = drm_output_get_plane_type_name(plane);
677 uint64_t zpos;
678
679 if (current_lowest_zpos == DRM_PLANE_ZPOS_INVALID_PLANE)
680 zpos = plane->zpos_max;
681 else
682 zpos = MIN(current_lowest_zpos - 1, plane->zpos_max);
683
684 drm_debug(b, "\t\t\t\t[plane] plane %d picked "
685 "from candidate list, type: %s\n",
686 plane->plane_id, p_name);
687
688 ps = drm_output_try_view_on_plane(plane, state, ev, mode, zpos);
689 drm_output_destroy_zpos_plane(head_p_zpos);
690
691 if (ps) {
692 drm_debug(b, "\t\t\t\t[view] view %p has been placed to "
693 "%s plane with computed zpos %"PRIu64"\n",
694 ev, p_name, zpos);
695 break;
696 }
697 }
698
699 wl_list_for_each_safe(p_zpos, p_zpos_next, &zpos_candidate_list, link)
700 drm_output_destroy_zpos_plane(p_zpos);
701
Marius Vlad26dcce02019-10-24 14:49:33 +0300702 drm_fb_unref(fb);
Marius Vlad2538aac2019-10-14 11:05:30 +0300703 return ps;
704}
705
Daniel Stonee404b722019-06-22 18:40:31 +0100706static struct drm_output_state *
707drm_output_propose_state(struct weston_output *output_base,
708 struct drm_pending_state *pending_state,
709 enum drm_output_propose_state_mode mode)
710{
711 struct drm_output *output = to_drm_output(output_base);
712 struct drm_backend *b = to_drm_backend(output->base.compositor);
713 struct drm_output_state *state;
714 struct drm_plane_state *scanout_state = NULL;
715 struct weston_view *ev;
Marius Vlad80a62e52019-09-11 18:21:59 +0300716
717 pixman_region32_t surface_overlap, renderer_region, planes_region;
718 pixman_region32_t occluded_region;
719
Daniel Stonee404b722019-06-22 18:40:31 +0100720 bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
721 int ret;
Marius Vlad2538aac2019-10-14 11:05:30 +0300722 uint64_t current_lowest_zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stonee404b722019-06-22 18:40:31 +0100723
724 assert(!output->state_last);
725 state = drm_output_state_duplicate(output->state_cur,
726 pending_state,
727 DRM_OUTPUT_STATE_CLEAR_PLANES);
728
729 /* We implement mixed mode by progressively creating and testing
730 * incremental states, of scanout + overlay + cursor. Since we
731 * walk our views top to bottom, the scanout plane is last, however
732 * we always need it in our scene for the test modeset to be
733 * meaningful. To do this, we steal a reference to the last
734 * renderer framebuffer we have, if we think it's basically
735 * compatible. If we don't have that, then we conservatively fall
736 * back to only using the renderer for this repaint. */
737 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
738 struct drm_plane *plane = output->scanout_plane;
739 struct drm_fb *scanout_fb = plane->state_cur->fb;
740
741 if (!scanout_fb ||
742 (scanout_fb->type != BUFFER_GBM_SURFACE &&
743 scanout_fb->type != BUFFER_PIXMAN_DUMB)) {
744 drm_debug(b, "\t\t[state] cannot propose mixed mode: "
745 "for output %s (%lu): no previous renderer "
746 "fb\n",
747 output->base.name,
748 (unsigned long) output->base.id);
749 drm_output_state_free(state);
750 return NULL;
751 }
752
753 if (scanout_fb->width != output_base->current_mode->width ||
754 scanout_fb->height != output_base->current_mode->height) {
755 drm_debug(b, "\t\t[state] cannot propose mixed mode "
756 "for output %s (%lu): previous fb has "
757 "different size\n",
758 output->base.name,
759 (unsigned long) output->base.id);
760 drm_output_state_free(state);
761 return NULL;
762 }
763
764 scanout_state = drm_plane_state_duplicate(state,
765 plane->state_cur);
766 drm_debug(b, "\t\t[state] using renderer FB ID %lu for mixed "
767 "mode for output %s (%lu)\n",
768 (unsigned long) scanout_fb->fb_id, output->base.name,
769 (unsigned long) output->base.id);
770 }
771
Marius Vlad80a62e52019-09-11 18:21:59 +0300772 /* - renderer_region contains the total region which which will be
773 * covered by the renderer
774 * - planes_region contains the total region which has been covered by
775 * hardware planes
776 * - occluded_region contains the total region which which will be
777 * covered by the renderer and hardware planes, where the view's
778 * visible-and-opaque region is added in both cases (the view's
779 * opaque region accumulates there for each view); it is being used
780 * to skip the view, if it is completely occluded; includes the
781 * situation where occluded_region covers entire output's region.
Daniel Stonee404b722019-06-22 18:40:31 +0100782 */
783 pixman_region32_init(&renderer_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300784 pixman_region32_init(&planes_region);
Daniel Stonee404b722019-06-22 18:40:31 +0100785 pixman_region32_init(&occluded_region);
786
787 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
788 struct drm_plane_state *ps = NULL;
789 bool force_renderer = false;
790 pixman_region32_t clipped_view;
791 bool totally_occluded = false;
Daniel Stonee404b722019-06-22 18:40:31 +0100792
793 drm_debug(b, "\t\t\t[view] evaluating view %p for "
794 "output %s (%lu)\n",
795 ev, output->base.name,
796 (unsigned long) output->base.id);
797
798 /* If this view doesn't touch our output at all, there's no
799 * reason to do anything with it. */
800 if (!(ev->output_mask & (1u << output->base.id))) {
801 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
802 "(not on our output)\n", ev);
803 continue;
804 }
805
806 /* We only assign planes to views which are exclusively present
807 * on our output. */
808 if (ev->output_mask != (1u << output->base.id)) {
809 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
810 "(on multiple outputs)\n", ev);
811 force_renderer = true;
812 }
813
Marius Vlad5f6bee42019-09-11 16:41:04 +0300814 if (!weston_view_has_valid_buffer(ev)) {
Daniel Stonee404b722019-06-22 18:40:31 +0100815 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
816 "(no buffer available)\n", ev);
817 force_renderer = true;
818 }
819
820 /* Ignore views we know to be totally occluded. */
821 pixman_region32_init(&clipped_view);
822 pixman_region32_intersect(&clipped_view,
823 &ev->transform.boundingbox,
824 &output->base.region);
825
826 pixman_region32_init(&surface_overlap);
827 pixman_region32_subtract(&surface_overlap, &clipped_view,
828 &occluded_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300829 /* if the view is completely occluded then ignore that
830 * view; includes the case where occluded_region covers
831 * the entire output */
Daniel Stonee404b722019-06-22 18:40:31 +0100832 totally_occluded = !pixman_region32_not_empty(&surface_overlap);
833 if (totally_occluded) {
834 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
835 "(occluded on our output)\n", ev);
836 pixman_region32_fini(&surface_overlap);
837 pixman_region32_fini(&clipped_view);
838 continue;
839 }
840
841 /* Since we process views from top to bottom, we know that if
842 * the view intersects the calculated renderer region, it must
843 * be part of, or occluded by, it, and cannot go on a plane. */
844 pixman_region32_intersect(&surface_overlap, &renderer_region,
845 &clipped_view);
846 if (pixman_region32_not_empty(&surface_overlap)) {
847 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
848 "(occluded by renderer views)\n", ev);
849 force_renderer = true;
850 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300851 pixman_region32_fini(&surface_overlap);
Daniel Stonee404b722019-06-22 18:40:31 +0100852
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530853 /* In case of enforced mode of content-protection do not
854 * assign planes for a protected surface on an unsecured output.
855 */
856 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
857 ev->surface->desired_protection > output_base->current_protection) {
858 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
859 "(enforced protection mode on unsecured output)\n", ev);
860 force_renderer = true;
861 }
862
Marius Vlad2538aac2019-10-14 11:05:30 +0300863 if (!force_renderer) {
864 drm_debug(b, "\t\t\t[plane] started with zpos %"PRIu64"\n",
865 current_lowest_zpos);
866 ps = drm_output_prepare_plane_view(state, ev, mode,
867 current_lowest_zpos);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000868 }
Daniel Stone2cb926c2019-11-11 16:48:54 +0000869
870 if (ps) {
Marius Vlad2538aac2019-10-14 11:05:30 +0300871 current_lowest_zpos = ps->zpos;
872 drm_debug(b, "\t\t\t[plane] next zpos to use %"PRIu64"\n",
873 current_lowest_zpos);
874
Daniel Stonee404b722019-06-22 18:40:31 +0100875 /* If we have been assigned to an overlay or scanout
876 * plane, add this area to the occluded region, so
877 * other views are known to be behind it. The cursor
878 * plane, however, is special, in that it blends with
879 * the content underneath it: the area should neither
880 * be added to the renderer region nor the occluded
881 * region. */
882 if (ps->plane->type != WDRM_PLANE_TYPE_CURSOR) {
Marius Vlad80a62e52019-09-11 18:21:59 +0300883 pixman_region32_union(&planes_region,
884 &planes_region,
885 &clipped_view);
886
887 if (!weston_view_is_opaque(ev, &clipped_view))
888 pixman_region32_intersect(&clipped_view,
889 &clipped_view,
890 &ev->transform.opaque);
891 /* the visible-and-opaque region of this view
892 * will occlude views underneath it */
Daniel Stonee404b722019-06-22 18:40:31 +0100893 pixman_region32_union(&occluded_region,
894 &occluded_region,
895 &clipped_view);
Marius Vlad80a62e52019-09-11 18:21:59 +0300896
Daniel Stonee404b722019-06-22 18:40:31 +0100897 pixman_region32_fini(&clipped_view);
Marius Vlad80a62e52019-09-11 18:21:59 +0300898
Daniel Stonee404b722019-06-22 18:40:31 +0100899 }
900 continue;
901 }
902
903 /* We have been assigned to the primary (renderer) plane:
904 * check if this is OK, and add ourselves to the renderer
905 * region if so. */
906 if (!renderer_ok) {
907 drm_debug(b, "\t\t[view] failing state generation: "
908 "placing view %p to renderer not allowed\n",
909 ev);
910 pixman_region32_fini(&clipped_view);
911 goto err_region;
912 }
913
914 pixman_region32_union(&renderer_region,
915 &renderer_region,
916 &clipped_view);
Marius Vlad80a62e52019-09-11 18:21:59 +0300917
918 if (!weston_view_is_opaque(ev, &clipped_view))
919 pixman_region32_intersect(&clipped_view,
920 &clipped_view,
921 &ev->transform.opaque);
922
923 pixman_region32_union(&occluded_region,
924 &occluded_region,
925 &clipped_view);
926
Daniel Stonee404b722019-06-22 18:40:31 +0100927 pixman_region32_fini(&clipped_view);
928 }
Marius Vlad80a62e52019-09-11 18:21:59 +0300929
Daniel Stonee404b722019-06-22 18:40:31 +0100930 pixman_region32_fini(&renderer_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300931 pixman_region32_fini(&planes_region);
Daniel Stonee404b722019-06-22 18:40:31 +0100932 pixman_region32_fini(&occluded_region);
933
934 /* In renderer-only mode, we can't test the state as we don't have a
935 * renderer buffer yet. */
936 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY)
937 return state;
938
Marius Vlad2538aac2019-10-14 11:05:30 +0300939 /* check if we have invalid zpos values, like duplicate(s) */
940 ret = drm_output_check_zpos_plane_states(state);
941 if (ret != 0) {
942 drm_debug(b, "\t\t[view] failing state generation: "
943 "zpos values are in-consistent\n");
944 goto err;
945 }
946
Daniel Stonee404b722019-06-22 18:40:31 +0100947 /* Check to see if this state will actually work. */
948 ret = drm_pending_state_test(state->pending_state);
949 if (ret != 0) {
950 drm_debug(b, "\t\t[view] failing state generation: "
951 "atomic test not OK\n");
952 goto err;
953 }
954
955 /* Counterpart to duplicating scanout state at the top of this
956 * function: if we have taken a renderer framebuffer and placed it in
957 * the pending state in order to incrementally test overlay planes,
958 * remove it now. */
959 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
960 assert(scanout_state->fb->type == BUFFER_GBM_SURFACE ||
961 scanout_state->fb->type == BUFFER_PIXMAN_DUMB);
962 drm_plane_state_put_back(scanout_state);
963 }
964 return state;
965
966err_region:
967 pixman_region32_fini(&renderer_region);
968 pixman_region32_fini(&occluded_region);
969err:
970 drm_output_state_free(state);
971 return NULL;
972}
973
974void
975drm_assign_planes(struct weston_output *output_base, void *repaint_data)
976{
977 struct drm_backend *b = to_drm_backend(output_base->compositor);
978 struct drm_pending_state *pending_state = repaint_data;
979 struct drm_output *output = to_drm_output(output_base);
980 struct drm_output_state *state = NULL;
981 struct drm_plane_state *plane_state;
982 struct weston_view *ev;
983 struct weston_plane *primary = &output_base->compositor->primary_plane;
984 enum drm_output_propose_state_mode mode = DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY;
985
986 drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n",
987 output_base->name, (unsigned long) output_base->id);
988
989 if (!b->sprites_are_broken && !output->virtual) {
990 drm_debug(b, "\t[repaint] trying planes-only build state\n");
991 state = drm_output_propose_state(output_base, pending_state, mode);
992 if (!state) {
993 drm_debug(b, "\t[repaint] could not build planes-only "
994 "state, trying mixed\n");
995 mode = DRM_OUTPUT_PROPOSE_STATE_MIXED;
996 state = drm_output_propose_state(output_base,
997 pending_state,
998 mode);
999 }
1000 if (!state) {
1001 drm_debug(b, "\t[repaint] could not build mixed-mode "
1002 "state, trying renderer-only\n");
1003 }
1004 } else {
1005 drm_debug(b, "\t[state] no overlay plane support\n");
1006 }
1007
1008 if (!state) {
1009 mode = DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY;
1010 state = drm_output_propose_state(output_base, pending_state,
1011 mode);
1012 }
1013
1014 assert(state);
1015 drm_debug(b, "\t[repaint] Using %s composition\n",
1016 drm_propose_state_mode_to_string(mode));
1017
1018 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
1019 struct drm_plane *target_plane = NULL;
1020
1021 /* If this view doesn't touch our output at all, there's no
1022 * reason to do anything with it. */
1023 if (!(ev->output_mask & (1u << output->base.id)))
1024 continue;
1025
1026 /* Test whether this buffer can ever go into a plane:
1027 * non-shm, or small enough to be a cursor.
1028 *
1029 * Also, keep a reference when using the pixman renderer.
1030 * That makes it possible to do a seamless switch to the GL
1031 * renderer and since the pixman renderer keeps a reference
1032 * to the buffer anyway, there is no side effects.
1033 */
1034 if (b->use_pixman ||
Marius Vlad5f6bee42019-09-11 16:41:04 +03001035 (weston_view_has_valid_buffer(ev) &&
Daniel Stonee404b722019-06-22 18:40:31 +01001036 (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
1037 (ev->surface->width <= b->cursor_width &&
1038 ev->surface->height <= b->cursor_height))))
1039 ev->surface->keep_buffer = true;
1040 else
1041 ev->surface->keep_buffer = false;
1042
1043 /* This is a bit unpleasant, but lacking a temporary place to
1044 * hang a plane off the view, we have to do a nested walk.
1045 * Our first-order iteration has to be planes rather than
1046 * views, because otherwise we won't reset views which were
1047 * previously on planes to being on the primary plane. */
1048 wl_list_for_each(plane_state, &state->plane_list, link) {
1049 if (plane_state->ev == ev) {
1050 plane_state->ev = NULL;
1051 target_plane = plane_state->plane;
1052 break;
1053 }
1054 }
1055
1056 if (target_plane) {
1057 drm_debug(b, "\t[repaint] view %p on %s plane %lu\n",
1058 ev, plane_type_enums[target_plane->type].name,
1059 (unsigned long) target_plane->plane_id);
1060 weston_view_move_to_plane(ev, &target_plane->base);
1061 } else {
1062 drm_debug(b, "\t[repaint] view %p using renderer "
1063 "composition\n", ev);
1064 weston_view_move_to_plane(ev, primary);
1065 }
1066
1067 if (!target_plane ||
1068 target_plane->type == WDRM_PLANE_TYPE_CURSOR) {
1069 /* cursor plane & renderer involve a copy */
1070 ev->psf_flags = 0;
1071 } else {
1072 /* All other planes are a direct scanout of a
1073 * single client buffer.
1074 */
1075 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1076 }
1077 }
1078
1079 /* We rely on output->cursor_view being both an accurate reflection of
1080 * the cursor plane's state, but also being maintained across repaints
1081 * to avoid unnecessary damage uploads, per the comment in
1082 * drm_output_prepare_cursor_view. In the event that we go from having
1083 * a cursor view to not having a cursor view, we need to clear it. */
1084 if (output->cursor_view) {
1085 plane_state =
1086 drm_output_state_get_existing_plane(state,
1087 output->cursor_plane);
1088 if (!plane_state || !plane_state->fb)
1089 output->cursor_view = NULL;
1090 }
1091}