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