blob: 4e91e60dd74f12e6f4f41c4a95c7fe9ecf18b21c [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
Daniel Stonee404b722019-06-22 18:40:31 +0100126static struct drm_plane_state *
Daniel Stone2cb926c2019-11-11 16:48:54 +0000127drm_output_prepare_overlay_view(struct drm_output_state *output_state,
Daniel Stonee404b722019-06-22 18:40:31 +0100128 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300129 enum drm_output_propose_state_mode mode,
130 uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100131{
132 struct drm_output *output = output_state->output;
133 struct weston_compositor *ec = output->base.compositor;
134 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000135 struct drm_plane *p;
Daniel Stonee404b722019-06-22 18:40:31 +0100136 struct drm_plane_state *state = NULL;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000137 struct drm_fb *fb;
138 unsigned int i;
Daniel Stonee404b722019-06-22 18:40:31 +0100139 int ret;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000140 enum {
141 NO_PLANES,
142 NO_PLANES_WITH_FORMAT,
143 NO_PLANES_ACCEPTED,
144 PLACED_ON_PLANE,
145 } availability = NO_PLANES;
Daniel Stonee404b722019-06-22 18:40:31 +0100146
147 assert(!b->sprites_are_broken);
148 assert(b->atomic_modeset);
149
Daniel Stone2cb926c2019-11-11 16:48:54 +0000150 fb = drm_fb_get_from_view(output_state, ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100151 if (!fb) {
152 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
153 " couldn't get fb\n", ev);
154 return NULL;
155 }
156
Daniel Stone2cb926c2019-11-11 16:48:54 +0000157 wl_list_for_each(p, &b->plane_list, link) {
158 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
159 continue;
Daniel Stonee404b722019-06-22 18:40:31 +0100160
Daniel Stone2cb926c2019-11-11 16:48:54 +0000161 if (!drm_plane_is_available(p, output))
162 continue;
Daniel Stonee404b722019-06-22 18:40:31 +0100163
Daniel Stone2cb926c2019-11-11 16:48:54 +0000164 state = drm_output_state_get_plane(output_state, p);
165 if (state->fb) {
166 state = NULL;
167 continue;
168 }
169
170 if (availability == NO_PLANES)
171 availability = NO_PLANES_WITH_FORMAT;
172
173 /* Check whether the format is supported */
174 for (i = 0; i < p->count_formats; i++) {
175 unsigned int j;
176
177 if (p->formats[i].format != fb->format->format)
178 continue;
179
180 if (fb->modifier == DRM_FORMAT_MOD_INVALID)
181 break;
182
183 for (j = 0; j < p->formats[i].count_modifiers; j++) {
184 if (p->formats[i].modifiers[j] == fb->modifier)
185 break;
186 }
187 if (j != p->formats[i].count_modifiers)
188 break;
189 }
190 if (i == p->count_formats) {
191 drm_plane_state_put_back(state);
192 state = NULL;
193 continue;
194 }
195
196 if (availability == NO_PLANES_WITH_FORMAT)
197 availability = NO_PLANES_ACCEPTED;
198
199 state->ev = ev;
200 state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300201 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000202 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
203 "unsuitable transform\n", ev);
204 drm_plane_state_put_back(state);
205 state = NULL;
206 continue;
207 }
208
209 /* If the surface buffer has an in-fence fd, but the plane
210 * doesn't support fences, we can't place the buffer on this
211 * plane. */
212 if (ev->surface->acquire_fence_fd >= 0 &&
213 p->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
214 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
215 "no in-fence support\n", ev);
216 drm_plane_state_put_back(state);
217 state = NULL;
218 continue;
219 }
220
221 /* We hold one reference for the lifetime of this function;
222 * from calling drm_fb_get_from_view, to the out label where
223 * we unconditionally drop the reference. So, we take another
224 * reference here to live within the state. */
225 state->fb = drm_fb_ref(fb);
226
227 state->in_fence_fd = ev->surface->acquire_fence_fd;
228
229 /* In planes-only mode, we don't have an incremental state to
230 * test against, so we just hope it'll work. */
231 if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
232 drm_debug(b, "\t\t\t\t[overlay] provisionally placing "
233 "view %p on overlay %lu in planes-only mode\n",
234 ev, (unsigned long) p->plane_id);
235 availability = PLACED_ON_PLANE;
236 goto out;
237 }
238
239 ret = drm_pending_state_test(output_state->pending_state);
240 if (ret == 0) {
241 drm_debug(b, "\t\t\t\t[overlay] provisionally placing "
242 "view %p on overlay %d in mixed mode\n",
243 ev, p->plane_id);
244 availability = PLACED_ON_PLANE;
245 goto out;
246 }
247
248 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay %lu "
249 "in mixed mode: kernel test failed\n",
250 ev, (unsigned long) p->plane_id);
251
Daniel Stonee404b722019-06-22 18:40:31 +0100252 drm_plane_state_put_back(state);
253 state = NULL;
254 }
255
Daniel Stone2cb926c2019-11-11 16:48:54 +0000256 switch (availability) {
257 case NO_PLANES:
Daniel Stonee404b722019-06-22 18:40:31 +0100258 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
Daniel Stone2cb926c2019-11-11 16:48:54 +0000259 "no free overlay planes\n", ev);
260 break;
261 case NO_PLANES_WITH_FORMAT:
262 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
263 "no free overlay planes matching format %s (0x%lx) "
264 "modifier 0x%llx\n",
265 ev, fb->format->drm_format_name,
266 (unsigned long) fb->format,
267 (unsigned long long) fb->modifier);
268 break;
269 case NO_PLANES_ACCEPTED:
270 case PLACED_ON_PLANE:
271 break;
Daniel Stonee404b722019-06-22 18:40:31 +0100272 }
273
274out:
Daniel Stone2cb926c2019-11-11 16:48:54 +0000275 drm_fb_unref(fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100276 return state;
277}
278
Stefan Agnerccf24072019-07-09 22:02:00 +0200279#ifdef BUILD_DRM_GBM
Daniel Stonee404b722019-06-22 18:40:31 +0100280/**
281 * Update the image for the current cursor surface
282 *
283 * @param plane_state DRM cursor plane state
284 * @param ev Source view for cursor
285 */
286static void
287cursor_bo_update(struct drm_plane_state *plane_state, struct weston_view *ev)
288{
289 struct drm_backend *b = plane_state->plane->backend;
290 struct gbm_bo *bo = plane_state->fb->bo;
291 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
292 uint32_t buf[b->cursor_width * b->cursor_height];
293 int32_t stride;
294 uint8_t *s;
295 int i;
296
297 assert(buffer && buffer->shm_buffer);
298 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
299 assert(buffer->width <= b->cursor_width);
300 assert(buffer->height <= b->cursor_height);
301
302 memset(buf, 0, sizeof buf);
303 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
304 s = wl_shm_buffer_get_data(buffer->shm_buffer);
305
306 wl_shm_buffer_begin_access(buffer->shm_buffer);
307 for (i = 0; i < buffer->height; i++)
308 memcpy(buf + i * b->cursor_width,
309 s + i * stride,
310 buffer->width * 4);
311 wl_shm_buffer_end_access(buffer->shm_buffer);
312
313 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
314 weston_log("failed update cursor: %s\n", strerror(errno));
315}
316
317static struct drm_plane_state *
318drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300319 struct weston_view *ev, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100320{
321 struct drm_output *output = output_state->output;
322 struct drm_backend *b = to_drm_backend(output->base.compositor);
323 struct drm_plane *plane = output->cursor_plane;
324 struct drm_plane_state *plane_state;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000325 struct wl_shm_buffer *shmbuf;
Daniel Stonee404b722019-06-22 18:40:31 +0100326 bool needs_update = false;
327
328 assert(!b->cursors_are_broken);
329
330 if (!plane)
331 return NULL;
332
333 if (!plane->state_cur->complete)
334 return NULL;
335
336 if (plane->state_cur->output && plane->state_cur->output != output)
337 return NULL;
338
339 /* We use GBM to import SHM buffers. */
340 if (b->gbm == NULL)
341 return NULL;
342
Daniel Stone2cb926c2019-11-11 16:48:54 +0000343 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
344 if (!shmbuf) {
345 drm_debug(b, "\t\t\t\t[cursor] not assigning view %p to cursor plane "
346 "(buffer isn't SHM)\n", ev);
347 return NULL;
348 }
349 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) {
350 drm_debug(b, "\t\t\t\t[cursor] not assigning view %p to cursor plane "
351 "(format 0x%lx unsuitable)\n",
352 ev, (unsigned long) wl_shm_buffer_get_format(shmbuf));
353 return NULL;
354 }
355
Daniel Stonee404b722019-06-22 18:40:31 +0100356 plane_state =
357 drm_output_state_get_plane(output_state, output->cursor_plane);
358
359 if (plane_state && plane_state->fb)
360 return NULL;
361
362 /* We can't scale with the legacy API, and we don't try to account for
363 * simple cropping/translation in cursor_bo_update. */
364 plane_state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300365 if (!drm_plane_state_coords_for_view(plane_state, ev, zpos))
Daniel Stonee404b722019-06-22 18:40:31 +0100366 goto err;
367
368 if (plane_state->src_x != 0 || plane_state->src_y != 0 ||
369 plane_state->src_w > (unsigned) b->cursor_width << 16 ||
370 plane_state->src_h > (unsigned) b->cursor_height << 16 ||
371 plane_state->src_w != plane_state->dest_w << 16 ||
372 plane_state->src_h != plane_state->dest_h << 16) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000373 drm_debug(b, "\t\t\t\t[cursor] not assigning view %p to cursor plane "
374 "(positioning requires cropping or scaling)\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100375 goto err;
376 }
377
378 /* Since we're setting plane state up front, we need to work out
379 * whether or not we need to upload a new cursor. We can't use the
380 * plane damage, since the planes haven't actually been calculated
381 * yet: instead try to figure it out directly. KMS cursor planes are
382 * pretty unique here, in that they lie partway between a Weston plane
383 * (direct scanout) and a renderer. */
384 if (ev != output->cursor_view ||
385 pixman_region32_not_empty(&ev->surface->damage)) {
386 output->current_cursor++;
387 output->current_cursor =
388 output->current_cursor %
389 ARRAY_LENGTH(output->gbm_cursor_fb);
390 needs_update = true;
391 }
392
393 output->cursor_view = ev;
394 plane_state->ev = ev;
395
396 plane_state->fb =
397 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
398
399 if (needs_update) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000400 drm_debug(b, "\t\t\t\t[cursor] copying new content to cursor BO\n");
Daniel Stonee404b722019-06-22 18:40:31 +0100401 cursor_bo_update(plane_state, ev);
402 }
403
404 /* The cursor API is somewhat special: in cursor_bo_update(), we upload
405 * a buffer which is always cursor_width x cursor_height, even if the
406 * surface we want to promote is actually smaller than this. Manually
407 * mangle the plane state to deal with this. */
408 plane_state->src_w = b->cursor_width << 16;
409 plane_state->src_h = b->cursor_height << 16;
410 plane_state->dest_w = b->cursor_width;
411 plane_state->dest_h = b->cursor_height;
412
Daniel Stone2cb926c2019-11-11 16:48:54 +0000413 drm_debug(b, "\t\t\t\t[cursor] provisionally assigned view %p to cursor\n",
414 ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100415
416 return plane_state;
417
418err:
419 drm_plane_state_put_back(plane_state);
420 return NULL;
421}
Stefan Agnerccf24072019-07-09 22:02:00 +0200422#else
423static struct drm_plane_state *
424drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300425 struct weston_view *ev, uint64_t zpos)
Stefan Agnerccf24072019-07-09 22:02:00 +0200426{
427 return NULL;
428}
429#endif
Daniel Stonee404b722019-06-22 18:40:31 +0100430
431static struct drm_plane_state *
432drm_output_prepare_scanout_view(struct drm_output_state *output_state,
433 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300434 enum drm_output_propose_state_mode mode,
435 uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100436{
437 struct drm_output *output = output_state->output;
438 struct drm_backend *b = to_drm_backend(output->base.compositor);
439 struct drm_plane *scanout_plane = output->scanout_plane;
440 struct drm_plane_state *state;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000441 struct drm_fb *fb;
Daniel Stonee404b722019-06-22 18:40:31 +0100442
443 assert(!b->sprites_are_broken);
444 assert(b->atomic_modeset);
445 assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
446
447 /* Check the view spans exactly the output size, calculated in the
448 * logical co-ordinate space. */
Marius Vlad47e3d1e2019-09-11 16:53:08 +0300449 if (!weston_view_matches_output_entirely(ev, &output->base))
Daniel Stonee404b722019-06-22 18:40:31 +0100450 return NULL;
451
452 /* If the surface buffer has an in-fence fd, but the plane doesn't
453 * support fences, we can't place the buffer on this plane. */
454 if (ev->surface->acquire_fence_fd >= 0 &&
Daniel Stone2cb926c2019-11-11 16:48:54 +0000455 scanout_plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0)
Daniel Stonee404b722019-06-22 18:40:31 +0100456 return NULL;
457
Daniel Stone2cb926c2019-11-11 16:48:54 +0000458 fb = drm_fb_get_from_view(output_state, ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100459 if (!fb) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000460 drm_debug(b, "\t\t\t\t[scanout] not placing view %p on scanout: "
461 " couldn't get fb\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100462 return NULL;
463 }
464
465 state = drm_output_state_get_plane(output_state, scanout_plane);
466
467 /* The only way we can already have a buffer in the scanout plane is
468 * if we are in mixed mode, or if a client buffer has already been
469 * placed into scanout. The former case will never call into here,
470 * and in the latter case, the view must have been marked as occluded,
471 * meaning we should never have ended up here. */
472 assert(!state->fb);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000473 state->fb = fb;
Daniel Stonee404b722019-06-22 18:40:31 +0100474 state->ev = ev;
475 state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300476 if (!drm_plane_state_coords_for_view(state, ev, zpos))
Daniel Stonee404b722019-06-22 18:40:31 +0100477 goto err;
478
479 if (state->dest_x != 0 || state->dest_y != 0 ||
480 state->dest_w != (unsigned) output->base.current_mode->width ||
Daniel Stone2cb926c2019-11-11 16:48:54 +0000481 state->dest_h != (unsigned) output->base.current_mode->height)
Daniel Stonee404b722019-06-22 18:40:31 +0100482 goto err;
483
484 state->in_fence_fd = ev->surface->acquire_fence_fd;
485
486 /* In plane-only mode, we don't need to test the state now, as we
487 * will only test it once at the end. */
488 return state;
489
490err:
491 drm_plane_state_put_back(state);
492 return NULL;
493}
494
Marius Vlad2538aac2019-10-14 11:05:30 +0300495static struct drm_plane_state *
496drm_output_try_view_on_plane(struct drm_plane *plane,
497 struct drm_output_state *state,
498 struct weston_view *ev,
499 enum drm_output_propose_state_mode mode,
500 uint64_t zpos)
501{
502 struct drm_backend *b = state->pending_state->backend;
503 struct weston_output *wet_output = &state->output->base;
504 bool view_matches_entire_output, scanout_has_view_assigned;
505 struct drm_plane *scanout_plane = state->output->scanout_plane;
506
507 /* sanity checks in case we over/underflow zpos or pass incorrect
508 * values */
509 assert(zpos <= plane->zpos_max ||
510 zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
511
512 switch (plane->type) {
513 case WDRM_PLANE_TYPE_CURSOR:
514 if (b->cursors_are_broken) {
515 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
516 "place view %p in cursor\n",
517 plane->plane_id, ev);
518 return NULL;
519 }
520 return drm_output_prepare_cursor_view(state, ev, zpos);
521 case WDRM_PLANE_TYPE_OVERLAY:
522 /* do not attempt to place it in the overlay if we don't have
523 * anything in the scanout/primary and the view doesn't cover
524 * the entire output */
525 view_matches_entire_output =
526 weston_view_matches_output_entirely(ev, wet_output);
527 scanout_has_view_assigned =
528 drm_output_check_plane_has_view_assigned(scanout_plane,
529 state);
530
531 if (view_matches_entire_output && !scanout_has_view_assigned) {
532 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
533 "place view %p in overlay\n",
534 plane->plane_id, ev);
535 return NULL;
536 }
537 return drm_output_prepare_overlay_view(state, ev, mode, zpos);
538 case WDRM_PLANE_TYPE_PRIMARY:
539 if (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
540 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
541 "place view %p in scanout\n",
542 plane->plane_id, ev);
543 return NULL;
544 }
545 return drm_output_prepare_scanout_view(state, ev, mode, zpos);
546 default:
547 assert(0);
548 break;
549 }
550}
551
552static int
553drm_output_check_zpos_plane_states(struct drm_output_state *state)
554{
555 struct drm_backend *b = state->pending_state->backend;
556 struct drm_plane_state *ps;
557 int ret = 0;
558
559 wl_list_for_each(ps, &state->plane_list, link) {
560 struct wl_list *next_node = ps->link.next;
561 bool found_dup = false;
562
563 /* find another plane with the same zpos value */
564 if (next_node == &state->plane_list)
565 break;
566
567 while (next_node && next_node != &state->plane_list) {
568 struct drm_plane_state *ps_next;
569
570 ps_next = container_of(next_node,
571 struct drm_plane_state,
572 link);
573
574 if (ps->zpos == ps_next->zpos) {
575 found_dup = true;
576 break;
577 }
578 next_node = next_node->next;
579 }
580
581 if (found_dup) {
582 ret = 1;
583 drm_debug(b, "\t\t\t[plane] found duplicate zpos values\n");
584 break;
585 }
586 }
587
588 return ret;
589}
590
591static struct drm_plane_state *
592drm_output_prepare_plane_view(struct drm_output_state *state,
593 struct weston_view *ev,
594 enum drm_output_propose_state_mode mode,
595 uint64_t current_lowest_zpos)
596{
597 struct drm_output *output = state->output;
598 struct drm_backend *b = to_drm_backend(output->base.compositor);
599
600 struct drm_plane_state *ps = NULL;
601 struct drm_plane *plane;
602 struct drm_plane_zpos *p_zpos, *p_zpos_next;
603 struct wl_list zpos_candidate_list;
604
605 wl_list_init(&zpos_candidate_list);
606
607 /* check view for valid buffer, doesn't make sense to even try */
608 if (!weston_view_has_valid_buffer(ev))
609 return ps;
610
611 /* assemble a list with possible candidates */
612 wl_list_for_each(plane, &b->plane_list, link) {
613 if (!drm_plane_is_available(plane, output))
614 continue;
615
616 if (drm_output_check_plane_has_view_assigned(plane, state)) {
617 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to"
618 " candidate list: view already assigned "
619 "to a plane\n", plane->plane_id);
620 continue;
621 }
622
623 if (plane->zpos_min >= current_lowest_zpos) {
624 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
625 "candidate list: minium zpos (%"PRIu64") "
626 "plane's above current lowest zpos "
627 "(%"PRIu64")\n", plane->plane_id,
628 plane->zpos_min, current_lowest_zpos);
629 continue;
630 }
631
632 drm_output_add_zpos_plane(plane, &zpos_candidate_list);
633 }
634
635 /* go over the potential candidate list and try to find a possible
636 * plane suitable for \c ev; start with the highest zpos value of a
637 * plane to maximize our chances, but do note we pass the zpos value
638 * based on current tracked value by \c current_lowest_zpos_in_use */
639 while (!wl_list_empty(&zpos_candidate_list)) {
640 struct drm_plane_zpos *head_p_zpos =
641 wl_container_of(zpos_candidate_list.next,
642 head_p_zpos, link);
643 struct drm_plane *plane = head_p_zpos->plane;
644 const char *p_name = drm_output_get_plane_type_name(plane);
645 uint64_t zpos;
646
647 if (current_lowest_zpos == DRM_PLANE_ZPOS_INVALID_PLANE)
648 zpos = plane->zpos_max;
649 else
650 zpos = MIN(current_lowest_zpos - 1, plane->zpos_max);
651
652 drm_debug(b, "\t\t\t\t[plane] plane %d picked "
653 "from candidate list, type: %s\n",
654 plane->plane_id, p_name);
655
656 ps = drm_output_try_view_on_plane(plane, state, ev, mode, zpos);
657 drm_output_destroy_zpos_plane(head_p_zpos);
658
659 if (ps) {
660 drm_debug(b, "\t\t\t\t[view] view %p has been placed to "
661 "%s plane with computed zpos %"PRIu64"\n",
662 ev, p_name, zpos);
663 break;
664 }
665 }
666
667 wl_list_for_each_safe(p_zpos, p_zpos_next, &zpos_candidate_list, link)
668 drm_output_destroy_zpos_plane(p_zpos);
669
670 return ps;
671}
672
Daniel Stonee404b722019-06-22 18:40:31 +0100673static struct drm_output_state *
674drm_output_propose_state(struct weston_output *output_base,
675 struct drm_pending_state *pending_state,
676 enum drm_output_propose_state_mode mode)
677{
678 struct drm_output *output = to_drm_output(output_base);
679 struct drm_backend *b = to_drm_backend(output->base.compositor);
680 struct drm_output_state *state;
681 struct drm_plane_state *scanout_state = NULL;
682 struct weston_view *ev;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000683 pixman_region32_t surface_overlap, renderer_region, occluded_region;
Daniel Stonee404b722019-06-22 18:40:31 +0100684 bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
685 int ret;
Marius Vlad2538aac2019-10-14 11:05:30 +0300686 uint64_t current_lowest_zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stonee404b722019-06-22 18:40:31 +0100687
688 assert(!output->state_last);
689 state = drm_output_state_duplicate(output->state_cur,
690 pending_state,
691 DRM_OUTPUT_STATE_CLEAR_PLANES);
692
693 /* We implement mixed mode by progressively creating and testing
694 * incremental states, of scanout + overlay + cursor. Since we
695 * walk our views top to bottom, the scanout plane is last, however
696 * we always need it in our scene for the test modeset to be
697 * meaningful. To do this, we steal a reference to the last
698 * renderer framebuffer we have, if we think it's basically
699 * compatible. If we don't have that, then we conservatively fall
700 * back to only using the renderer for this repaint. */
701 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
702 struct drm_plane *plane = output->scanout_plane;
703 struct drm_fb *scanout_fb = plane->state_cur->fb;
704
705 if (!scanout_fb ||
706 (scanout_fb->type != BUFFER_GBM_SURFACE &&
707 scanout_fb->type != BUFFER_PIXMAN_DUMB)) {
708 drm_debug(b, "\t\t[state] cannot propose mixed mode: "
709 "for output %s (%lu): no previous renderer "
710 "fb\n",
711 output->base.name,
712 (unsigned long) output->base.id);
713 drm_output_state_free(state);
714 return NULL;
715 }
716
717 if (scanout_fb->width != output_base->current_mode->width ||
718 scanout_fb->height != output_base->current_mode->height) {
719 drm_debug(b, "\t\t[state] cannot propose mixed mode "
720 "for output %s (%lu): previous fb has "
721 "different size\n",
722 output->base.name,
723 (unsigned long) output->base.id);
724 drm_output_state_free(state);
725 return NULL;
726 }
727
728 scanout_state = drm_plane_state_duplicate(state,
729 plane->state_cur);
730 drm_debug(b, "\t\t[state] using renderer FB ID %lu for mixed "
731 "mode for output %s (%lu)\n",
732 (unsigned long) scanout_fb->fb_id, output->base.name,
733 (unsigned long) output->base.id);
734 }
735
Daniel Stone2cb926c2019-11-11 16:48:54 +0000736 /*
737 * Find a surface for each sprite in the output using some heuristics:
738 * 1) size
739 * 2) frequency of update
740 * 3) opacity (though some hw might support alpha blending)
741 * 4) clipping (this can be fixed with color keys)
742 *
743 * The idea is to save on blitting since this should save power.
744 * If we can get a large video surface on the sprite for example,
745 * the main display surface may not need to update at all, and
746 * the client buffer can be used directly for the sprite surface
747 * as we do for flipping full screen surfaces.
Daniel Stonee404b722019-06-22 18:40:31 +0100748 */
749 pixman_region32_init(&renderer_region);
750 pixman_region32_init(&occluded_region);
751
752 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
753 struct drm_plane_state *ps = NULL;
754 bool force_renderer = false;
755 pixman_region32_t clipped_view;
756 bool totally_occluded = false;
Daniel Stonee404b722019-06-22 18:40:31 +0100757
758 drm_debug(b, "\t\t\t[view] evaluating view %p for "
759 "output %s (%lu)\n",
760 ev, output->base.name,
761 (unsigned long) output->base.id);
762
763 /* If this view doesn't touch our output at all, there's no
764 * reason to do anything with it. */
765 if (!(ev->output_mask & (1u << output->base.id))) {
766 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
767 "(not on our output)\n", ev);
768 continue;
769 }
770
771 /* We only assign planes to views which are exclusively present
772 * on our output. */
773 if (ev->output_mask != (1u << output->base.id)) {
774 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
775 "(on multiple outputs)\n", ev);
776 force_renderer = true;
777 }
778
Marius Vlad5f6bee42019-09-11 16:41:04 +0300779 if (!weston_view_has_valid_buffer(ev)) {
Daniel Stonee404b722019-06-22 18:40:31 +0100780 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
781 "(no buffer available)\n", ev);
782 force_renderer = true;
783 }
784
785 /* Ignore views we know to be totally occluded. */
786 pixman_region32_init(&clipped_view);
787 pixman_region32_intersect(&clipped_view,
788 &ev->transform.boundingbox,
789 &output->base.region);
790
791 pixman_region32_init(&surface_overlap);
792 pixman_region32_subtract(&surface_overlap, &clipped_view,
793 &occluded_region);
794 totally_occluded = !pixman_region32_not_empty(&surface_overlap);
795 if (totally_occluded) {
796 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
797 "(occluded on our output)\n", ev);
798 pixman_region32_fini(&surface_overlap);
799 pixman_region32_fini(&clipped_view);
800 continue;
801 }
802
803 /* Since we process views from top to bottom, we know that if
804 * the view intersects the calculated renderer region, it must
805 * be part of, or occluded by, it, and cannot go on a plane. */
806 pixman_region32_intersect(&surface_overlap, &renderer_region,
807 &clipped_view);
808 if (pixman_region32_not_empty(&surface_overlap)) {
809 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
810 "(occluded by renderer views)\n", ev);
811 force_renderer = true;
812 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300813 pixman_region32_fini(&surface_overlap);
Daniel Stonee404b722019-06-22 18:40:31 +0100814
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530815 /* In case of enforced mode of content-protection do not
816 * assign planes for a protected surface on an unsecured output.
817 */
818 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
819 ev->surface->desired_protection > output_base->current_protection) {
820 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
821 "(enforced protection mode on unsecured output)\n", ev);
822 force_renderer = true;
823 }
824
Marius Vlad2538aac2019-10-14 11:05:30 +0300825 if (!force_renderer) {
826 drm_debug(b, "\t\t\t[plane] started with zpos %"PRIu64"\n",
827 current_lowest_zpos);
828 ps = drm_output_prepare_plane_view(state, ev, mode,
829 current_lowest_zpos);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000830 }
Daniel Stone2cb926c2019-11-11 16:48:54 +0000831
832 if (ps) {
Marius Vlad2538aac2019-10-14 11:05:30 +0300833 current_lowest_zpos = ps->zpos;
834 drm_debug(b, "\t\t\t[plane] next zpos to use %"PRIu64"\n",
835 current_lowest_zpos);
836
Daniel Stonee404b722019-06-22 18:40:31 +0100837 /* If we have been assigned to an overlay or scanout
838 * plane, add this area to the occluded region, so
839 * other views are known to be behind it. The cursor
840 * plane, however, is special, in that it blends with
841 * the content underneath it: the area should neither
842 * be added to the renderer region nor the occluded
843 * region. */
844 if (ps->plane->type != WDRM_PLANE_TYPE_CURSOR) {
845 pixman_region32_union(&occluded_region,
846 &occluded_region,
847 &clipped_view);
848 pixman_region32_fini(&clipped_view);
849 }
850 continue;
851 }
852
853 /* We have been assigned to the primary (renderer) plane:
854 * check if this is OK, and add ourselves to the renderer
855 * region if so. */
856 if (!renderer_ok) {
857 drm_debug(b, "\t\t[view] failing state generation: "
858 "placing view %p to renderer not allowed\n",
859 ev);
860 pixman_region32_fini(&clipped_view);
861 goto err_region;
862 }
863
864 pixman_region32_union(&renderer_region,
865 &renderer_region,
866 &clipped_view);
867 pixman_region32_fini(&clipped_view);
868 }
869 pixman_region32_fini(&renderer_region);
870 pixman_region32_fini(&occluded_region);
871
872 /* In renderer-only mode, we can't test the state as we don't have a
873 * renderer buffer yet. */
874 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY)
875 return state;
876
Marius Vlad2538aac2019-10-14 11:05:30 +0300877 /* check if we have invalid zpos values, like duplicate(s) */
878 ret = drm_output_check_zpos_plane_states(state);
879 if (ret != 0) {
880 drm_debug(b, "\t\t[view] failing state generation: "
881 "zpos values are in-consistent\n");
882 goto err;
883 }
884
Daniel Stonee404b722019-06-22 18:40:31 +0100885 /* Check to see if this state will actually work. */
886 ret = drm_pending_state_test(state->pending_state);
887 if (ret != 0) {
888 drm_debug(b, "\t\t[view] failing state generation: "
889 "atomic test not OK\n");
890 goto err;
891 }
892
893 /* Counterpart to duplicating scanout state at the top of this
894 * function: if we have taken a renderer framebuffer and placed it in
895 * the pending state in order to incrementally test overlay planes,
896 * remove it now. */
897 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
898 assert(scanout_state->fb->type == BUFFER_GBM_SURFACE ||
899 scanout_state->fb->type == BUFFER_PIXMAN_DUMB);
900 drm_plane_state_put_back(scanout_state);
901 }
902 return state;
903
904err_region:
905 pixman_region32_fini(&renderer_region);
906 pixman_region32_fini(&occluded_region);
907err:
908 drm_output_state_free(state);
909 return NULL;
910}
911
912void
913drm_assign_planes(struct weston_output *output_base, void *repaint_data)
914{
915 struct drm_backend *b = to_drm_backend(output_base->compositor);
916 struct drm_pending_state *pending_state = repaint_data;
917 struct drm_output *output = to_drm_output(output_base);
918 struct drm_output_state *state = NULL;
919 struct drm_plane_state *plane_state;
920 struct weston_view *ev;
921 struct weston_plane *primary = &output_base->compositor->primary_plane;
922 enum drm_output_propose_state_mode mode = DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY;
923
924 drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n",
925 output_base->name, (unsigned long) output_base->id);
926
927 if (!b->sprites_are_broken && !output->virtual) {
928 drm_debug(b, "\t[repaint] trying planes-only build state\n");
929 state = drm_output_propose_state(output_base, pending_state, mode);
930 if (!state) {
931 drm_debug(b, "\t[repaint] could not build planes-only "
932 "state, trying mixed\n");
933 mode = DRM_OUTPUT_PROPOSE_STATE_MIXED;
934 state = drm_output_propose_state(output_base,
935 pending_state,
936 mode);
937 }
938 if (!state) {
939 drm_debug(b, "\t[repaint] could not build mixed-mode "
940 "state, trying renderer-only\n");
941 }
942 } else {
943 drm_debug(b, "\t[state] no overlay plane support\n");
944 }
945
946 if (!state) {
947 mode = DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY;
948 state = drm_output_propose_state(output_base, pending_state,
949 mode);
950 }
951
952 assert(state);
953 drm_debug(b, "\t[repaint] Using %s composition\n",
954 drm_propose_state_mode_to_string(mode));
955
956 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
957 struct drm_plane *target_plane = NULL;
958
959 /* If this view doesn't touch our output at all, there's no
960 * reason to do anything with it. */
961 if (!(ev->output_mask & (1u << output->base.id)))
962 continue;
963
964 /* Test whether this buffer can ever go into a plane:
965 * non-shm, or small enough to be a cursor.
966 *
967 * Also, keep a reference when using the pixman renderer.
968 * That makes it possible to do a seamless switch to the GL
969 * renderer and since the pixman renderer keeps a reference
970 * to the buffer anyway, there is no side effects.
971 */
972 if (b->use_pixman ||
Marius Vlad5f6bee42019-09-11 16:41:04 +0300973 (weston_view_has_valid_buffer(ev) &&
Daniel Stonee404b722019-06-22 18:40:31 +0100974 (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
975 (ev->surface->width <= b->cursor_width &&
976 ev->surface->height <= b->cursor_height))))
977 ev->surface->keep_buffer = true;
978 else
979 ev->surface->keep_buffer = false;
980
981 /* This is a bit unpleasant, but lacking a temporary place to
982 * hang a plane off the view, we have to do a nested walk.
983 * Our first-order iteration has to be planes rather than
984 * views, because otherwise we won't reset views which were
985 * previously on planes to being on the primary plane. */
986 wl_list_for_each(plane_state, &state->plane_list, link) {
987 if (plane_state->ev == ev) {
988 plane_state->ev = NULL;
989 target_plane = plane_state->plane;
990 break;
991 }
992 }
993
994 if (target_plane) {
995 drm_debug(b, "\t[repaint] view %p on %s plane %lu\n",
996 ev, plane_type_enums[target_plane->type].name,
997 (unsigned long) target_plane->plane_id);
998 weston_view_move_to_plane(ev, &target_plane->base);
999 } else {
1000 drm_debug(b, "\t[repaint] view %p using renderer "
1001 "composition\n", ev);
1002 weston_view_move_to_plane(ev, primary);
1003 }
1004
1005 if (!target_plane ||
1006 target_plane->type == WDRM_PLANE_TYPE_CURSOR) {
1007 /* cursor plane & renderer involve a copy */
1008 ev->psf_flags = 0;
1009 } else {
1010 /* All other planes are a direct scanout of a
1011 * single client buffer.
1012 */
1013 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1014 }
1015 }
1016
1017 /* We rely on output->cursor_view being both an accurate reflection of
1018 * the cursor plane's state, but also being maintained across repaints
1019 * to avoid unnecessary damage uploads, per the comment in
1020 * drm_output_prepare_cursor_view. In the event that we go from having
1021 * a cursor view to not having a cursor view, we need to clear it. */
1022 if (output->cursor_view) {
1023 plane_state =
1024 drm_output_state_get_existing_plane(state,
1025 output->cursor_plane);
1026 if (!plane_state || !plane_state->fb)
1027 output->cursor_view = NULL;
1028 }
1029}