blob: 6a1c4766f37c08c37a1adb790374b66affa97c01 [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 *
Marius Vlad677e4592019-10-25 00:07:37 +0300179drm_output_prepare_overlay_view(struct drm_plane *plane,
180 struct drm_output_state *output_state,
Daniel Stonee404b722019-06-22 18:40:31 +0100181 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300182 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200183 struct drm_fb *fb, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100184{
185 struct drm_output *output = output_state->output;
186 struct weston_compositor *ec = output->base.compositor;
187 struct drm_backend *b = to_drm_backend(ec);
Daniel Stonee404b722019-06-22 18:40:31 +0100188 struct drm_plane_state *state = NULL;
Daniel Stonee404b722019-06-22 18:40:31 +0100189 int ret;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000190 enum {
191 NO_PLANES,
Daniel Stone2cb926c2019-11-11 16:48:54 +0000192 NO_PLANES_ACCEPTED,
193 PLACED_ON_PLANE,
194 } availability = NO_PLANES;
Daniel Stonee404b722019-06-22 18:40:31 +0100195
196 assert(!b->sprites_are_broken);
197 assert(b->atomic_modeset);
198
Daniel Stonee404b722019-06-22 18:40:31 +0100199 if (!fb) {
200 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
201 " couldn't get fb\n", ev);
202 return NULL;
203 }
204
Marius Vlad677e4592019-10-25 00:07:37 +0300205 state = drm_output_state_get_plane(output_state, plane);
Marius Vladeef69452019-10-31 12:48:35 +0200206 /* we can't have a 'pending' framebuffer as never set one before reaching here */
207 assert(!state->fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100208
Marius Vlad677e4592019-10-25 00:07:37 +0300209 state->ev = ev;
210 state->output = output;
Marius Vladeef69452019-10-31 12:48:35 +0200211
Marius Vlad677e4592019-10-25 00:07:37 +0300212 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
213 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
214 "unsuitable transform\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100215 drm_plane_state_put_back(state);
216 state = NULL;
Marius Vlad677e4592019-10-25 00:07:37 +0300217 goto out;
Daniel Stonee404b722019-06-22 18:40:31 +0100218 }
219
Marius Vlad677e4592019-10-25 00:07:37 +0300220 /* If the surface buffer has an in-fence fd, but the plane
221 * doesn't support fences, we can't place the buffer on this
222 * plane. */
223 if (ev->surface->acquire_fence_fd >= 0 &&
224 plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
225 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
226 "no in-fence support\n", ev);
227 drm_plane_state_put_back(state);
228 state = NULL;
229 goto out;
230 }
231
Marius Vladeef69452019-10-31 12:48:35 +0200232 /* We hold one reference for the lifetime of this function; from
233 * calling drm_fb_get_from_view() in drm_output_prepare_plane_view(),
234 * so, we take another reference here to live within the state. */
Marius Vlad677e4592019-10-25 00:07:37 +0300235 state->fb = drm_fb_ref(fb);
236
237 state->in_fence_fd = ev->surface->acquire_fence_fd;
238
239 /* In planes-only mode, we don't have an incremental state to
240 * test against, so we just hope it'll work. */
241 if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
242 drm_debug(b, "\t\t\t[overlay] provisionally placing "
243 "view %p on overlay %lu in planes-only mode\n",
244 ev, (unsigned long) plane->plane_id);
245 availability = PLACED_ON_PLANE;
246 goto out;
247 }
248
249 ret = drm_pending_state_test(output_state->pending_state);
250 if (ret == 0) {
251 drm_debug(b, "\t\t\t[overlay] provisionally placing "
252 "view %p on overlay %d in mixed mode\n",
253 ev, plane->plane_id);
254 availability = PLACED_ON_PLANE;
255 goto out;
256 }
257
258 drm_debug(b, "\t\t\t[overlay] not placing view %p on overlay %lu "
259 "in mixed mode: kernel test failed\n",
260 ev, (unsigned long) plane->plane_id);
261
262 drm_plane_state_put_back(state);
263 state = NULL;
264
Daniel Stone2cb926c2019-11-11 16:48:54 +0000265 switch (availability) {
266 case NO_PLANES:
Daniel Stonee404b722019-06-22 18:40:31 +0100267 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
Daniel Stone2cb926c2019-11-11 16:48:54 +0000268 "no free overlay planes\n", ev);
269 break;
Daniel Stone2cb926c2019-11-11 16:48:54 +0000270 case NO_PLANES_ACCEPTED:
271 case PLACED_ON_PLANE:
272 break;
Daniel Stonee404b722019-06-22 18:40:31 +0100273 }
274
275out:
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 Stonee404b722019-06-22 18:40:31 +0100325 bool needs_update = false;
326
327 assert(!b->cursors_are_broken);
328
329 if (!plane)
330 return NULL;
331
332 if (!plane->state_cur->complete)
333 return NULL;
334
335 if (plane->state_cur->output && plane->state_cur->output != output)
336 return NULL;
337
338 /* We use GBM to import SHM buffers. */
339 if (b->gbm == NULL)
340 return NULL;
341
Daniel Stonee404b722019-06-22 18:40:31 +0100342 plane_state =
343 drm_output_state_get_plane(output_state, output->cursor_plane);
344
345 if (plane_state && plane_state->fb)
346 return NULL;
347
348 /* We can't scale with the legacy API, and we don't try to account for
349 * simple cropping/translation in cursor_bo_update. */
350 plane_state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300351 if (!drm_plane_state_coords_for_view(plane_state, ev, zpos))
Daniel Stonee404b722019-06-22 18:40:31 +0100352 goto err;
353
354 if (plane_state->src_x != 0 || plane_state->src_y != 0 ||
355 plane_state->src_w > (unsigned) b->cursor_width << 16 ||
356 plane_state->src_h > (unsigned) b->cursor_height << 16 ||
357 plane_state->src_w != plane_state->dest_w << 16 ||
358 plane_state->src_h != plane_state->dest_h << 16) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000359 drm_debug(b, "\t\t\t\t[cursor] not assigning view %p to cursor plane "
360 "(positioning requires cropping or scaling)\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100361 goto err;
362 }
363
364 /* Since we're setting plane state up front, we need to work out
365 * whether or not we need to upload a new cursor. We can't use the
366 * plane damage, since the planes haven't actually been calculated
367 * yet: instead try to figure it out directly. KMS cursor planes are
368 * pretty unique here, in that they lie partway between a Weston plane
369 * (direct scanout) and a renderer. */
370 if (ev != output->cursor_view ||
371 pixman_region32_not_empty(&ev->surface->damage)) {
372 output->current_cursor++;
373 output->current_cursor =
374 output->current_cursor %
375 ARRAY_LENGTH(output->gbm_cursor_fb);
376 needs_update = true;
377 }
378
379 output->cursor_view = ev;
380 plane_state->ev = ev;
381
382 plane_state->fb =
383 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
384
385 if (needs_update) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000386 drm_debug(b, "\t\t\t\t[cursor] copying new content to cursor BO\n");
Daniel Stonee404b722019-06-22 18:40:31 +0100387 cursor_bo_update(plane_state, ev);
388 }
389
390 /* The cursor API is somewhat special: in cursor_bo_update(), we upload
391 * a buffer which is always cursor_width x cursor_height, even if the
392 * surface we want to promote is actually smaller than this. Manually
393 * mangle the plane state to deal with this. */
394 plane_state->src_w = b->cursor_width << 16;
395 plane_state->src_h = b->cursor_height << 16;
396 plane_state->dest_w = b->cursor_width;
397 plane_state->dest_h = b->cursor_height;
398
Daniel Stone2cb926c2019-11-11 16:48:54 +0000399 drm_debug(b, "\t\t\t\t[cursor] provisionally assigned view %p to cursor\n",
400 ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100401
402 return plane_state;
403
404err:
405 drm_plane_state_put_back(plane_state);
406 return NULL;
407}
Stefan Agnerccf24072019-07-09 22:02:00 +0200408#else
409static struct drm_plane_state *
410drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300411 struct weston_view *ev, uint64_t zpos)
Stefan Agnerccf24072019-07-09 22:02:00 +0200412{
413 return NULL;
414}
415#endif
Daniel Stonee404b722019-06-22 18:40:31 +0100416
417static struct drm_plane_state *
418drm_output_prepare_scanout_view(struct drm_output_state *output_state,
419 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300420 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200421 struct drm_fb *fb, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100422{
423 struct drm_output *output = output_state->output;
424 struct drm_backend *b = to_drm_backend(output->base.compositor);
425 struct drm_plane *scanout_plane = output->scanout_plane;
426 struct drm_plane_state *state;
Daniel Stonee404b722019-06-22 18:40:31 +0100427
428 assert(!b->sprites_are_broken);
429 assert(b->atomic_modeset);
430 assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
431
432 /* Check the view spans exactly the output size, calculated in the
433 * logical co-ordinate space. */
Marius Vlad47e3d1e2019-09-11 16:53:08 +0300434 if (!weston_view_matches_output_entirely(ev, &output->base))
Daniel Stonee404b722019-06-22 18:40:31 +0100435 return NULL;
436
437 /* If the surface buffer has an in-fence fd, but the plane doesn't
438 * support fences, we can't place the buffer on this plane. */
439 if (ev->surface->acquire_fence_fd >= 0 &&
Daniel Stone2cb926c2019-11-11 16:48:54 +0000440 scanout_plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0)
Daniel Stonee404b722019-06-22 18:40:31 +0100441 return NULL;
442
Daniel Stonee404b722019-06-22 18:40:31 +0100443 if (!fb) {
Daniel Stone2cb926c2019-11-11 16:48:54 +0000444 drm_debug(b, "\t\t\t\t[scanout] not placing view %p on scanout: "
445 " couldn't get fb\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100446 return NULL;
447 }
448
449 state = drm_output_state_get_plane(output_state, scanout_plane);
450
451 /* The only way we can already have a buffer in the scanout plane is
452 * if we are in mixed mode, or if a client buffer has already been
453 * placed into scanout. The former case will never call into here,
454 * and in the latter case, the view must have been marked as occluded,
455 * meaning we should never have ended up here. */
456 assert(!state->fb);
Marius Vladeef69452019-10-31 12:48:35 +0200457
458 /* take another reference here to live within the state */
459 state->fb = drm_fb_ref(fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100460 state->ev = ev;
461 state->output = output;
Marius Vlad2538aac2019-10-14 11:05:30 +0300462 if (!drm_plane_state_coords_for_view(state, ev, zpos))
Daniel Stonee404b722019-06-22 18:40:31 +0100463 goto err;
464
465 if (state->dest_x != 0 || state->dest_y != 0 ||
466 state->dest_w != (unsigned) output->base.current_mode->width ||
Daniel Stone2cb926c2019-11-11 16:48:54 +0000467 state->dest_h != (unsigned) output->base.current_mode->height)
Daniel Stonee404b722019-06-22 18:40:31 +0100468 goto err;
469
470 state->in_fence_fd = ev->surface->acquire_fence_fd;
471
472 /* In plane-only mode, we don't need to test the state now, as we
473 * will only test it once at the end. */
474 return state;
475
476err:
477 drm_plane_state_put_back(state);
478 return NULL;
479}
480
Marius Vlad26dcce02019-10-24 14:49:33 +0300481static bool
482drm_output_plane_view_has_valid_format(struct drm_plane *plane,
483 struct drm_output_state *state,
484 struct weston_view *ev,
485 struct drm_fb *fb)
486{
487 /* depending on the type of the plane we have different requirements */
488 switch (plane->type) {
489 case WDRM_PLANE_TYPE_CURSOR:
490 return drm_output_plane_cursor_has_valid_format(ev);
491 case WDRM_PLANE_TYPE_OVERLAY:
492 return drm_output_plane_has_valid_format(plane, state, fb);
493 case WDRM_PLANE_TYPE_PRIMARY:
494 return drm_output_plane_has_valid_format(plane, state, fb);
495 default:
496 assert(0);
497 return false;
498 }
499
500 return false;
501}
502
Marius Vlad2538aac2019-10-14 11:05:30 +0300503static struct drm_plane_state *
504drm_output_try_view_on_plane(struct drm_plane *plane,
505 struct drm_output_state *state,
506 struct weston_view *ev,
507 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200508 struct drm_fb *fb, uint64_t zpos)
Marius Vlad2538aac2019-10-14 11:05:30 +0300509{
510 struct drm_backend *b = state->pending_state->backend;
511 struct weston_output *wet_output = &state->output->base;
512 bool view_matches_entire_output, scanout_has_view_assigned;
513 struct drm_plane *scanout_plane = state->output->scanout_plane;
514
515 /* sanity checks in case we over/underflow zpos or pass incorrect
516 * values */
517 assert(zpos <= plane->zpos_max ||
518 zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
519
520 switch (plane->type) {
521 case WDRM_PLANE_TYPE_CURSOR:
522 if (b->cursors_are_broken) {
523 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
524 "place view %p in cursor\n",
525 plane->plane_id, ev);
526 return NULL;
527 }
528 return drm_output_prepare_cursor_view(state, ev, zpos);
529 case WDRM_PLANE_TYPE_OVERLAY:
530 /* do not attempt to place it in the overlay if we don't have
531 * anything in the scanout/primary and the view doesn't cover
532 * the entire output */
533 view_matches_entire_output =
534 weston_view_matches_output_entirely(ev, wet_output);
535 scanout_has_view_assigned =
536 drm_output_check_plane_has_view_assigned(scanout_plane,
537 state);
538
539 if (view_matches_entire_output && !scanout_has_view_assigned) {
540 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
541 "place view %p in overlay\n",
542 plane->plane_id, ev);
543 return NULL;
544 }
Marius Vladeef69452019-10-31 12:48:35 +0200545 return drm_output_prepare_overlay_view(plane, state, ev,
546 mode, fb, zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300547 case WDRM_PLANE_TYPE_PRIMARY:
548 if (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
549 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
550 "place view %p in scanout\n",
551 plane->plane_id, ev);
552 return NULL;
553 }
Marius Vladeef69452019-10-31 12:48:35 +0200554 return drm_output_prepare_scanout_view(state, ev, mode,
555 fb, zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300556 default:
557 assert(0);
558 break;
559 }
560}
561
562static int
563drm_output_check_zpos_plane_states(struct drm_output_state *state)
564{
565 struct drm_backend *b = state->pending_state->backend;
566 struct drm_plane_state *ps;
567 int ret = 0;
568
569 wl_list_for_each(ps, &state->plane_list, link) {
570 struct wl_list *next_node = ps->link.next;
571 bool found_dup = false;
572
573 /* find another plane with the same zpos value */
574 if (next_node == &state->plane_list)
575 break;
576
577 while (next_node && next_node != &state->plane_list) {
578 struct drm_plane_state *ps_next;
579
580 ps_next = container_of(next_node,
581 struct drm_plane_state,
582 link);
583
584 if (ps->zpos == ps_next->zpos) {
585 found_dup = true;
586 break;
587 }
588 next_node = next_node->next;
589 }
590
591 if (found_dup) {
592 ret = 1;
593 drm_debug(b, "\t\t\t[plane] found duplicate zpos values\n");
594 break;
595 }
596 }
597
598 return ret;
599}
600
601static struct drm_plane_state *
602drm_output_prepare_plane_view(struct drm_output_state *state,
603 struct weston_view *ev,
604 enum drm_output_propose_state_mode mode,
605 uint64_t current_lowest_zpos)
606{
607 struct drm_output *output = state->output;
608 struct drm_backend *b = to_drm_backend(output->base.compositor);
609
610 struct drm_plane_state *ps = NULL;
611 struct drm_plane *plane;
612 struct drm_plane_zpos *p_zpos, *p_zpos_next;
613 struct wl_list zpos_candidate_list;
614
Marius Vlad26dcce02019-10-24 14:49:33 +0300615 struct drm_fb *fb;
616
Marius Vlad2538aac2019-10-14 11:05:30 +0300617 wl_list_init(&zpos_candidate_list);
618
619 /* check view for valid buffer, doesn't make sense to even try */
620 if (!weston_view_has_valid_buffer(ev))
621 return ps;
622
Marius Vlad26dcce02019-10-24 14:49:33 +0300623 fb = drm_fb_get_from_view(state, ev);
624
Marius Vlad2538aac2019-10-14 11:05:30 +0300625 /* assemble a list with possible candidates */
626 wl_list_for_each(plane, &b->plane_list, link) {
627 if (!drm_plane_is_available(plane, output))
628 continue;
629
630 if (drm_output_check_plane_has_view_assigned(plane, state)) {
631 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to"
632 " candidate list: view already assigned "
633 "to a plane\n", plane->plane_id);
634 continue;
635 }
636
637 if (plane->zpos_min >= current_lowest_zpos) {
638 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
639 "candidate list: minium zpos (%"PRIu64") "
640 "plane's above current lowest zpos "
641 "(%"PRIu64")\n", plane->plane_id,
642 plane->zpos_min, current_lowest_zpos);
643 continue;
644 }
645
Marius Vlad26dcce02019-10-24 14:49:33 +0300646 if (!drm_output_plane_view_has_valid_format(plane, state, ev, fb)) {
647 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
648 "candidate list: invalid pixel format\n",
649 plane->plane_id);
650 continue;
651 }
652
Marius Vlad2538aac2019-10-14 11:05:30 +0300653 drm_output_add_zpos_plane(plane, &zpos_candidate_list);
654 }
655
656 /* go over the potential candidate list and try to find a possible
657 * plane suitable for \c ev; start with the highest zpos value of a
658 * plane to maximize our chances, but do note we pass the zpos value
659 * based on current tracked value by \c current_lowest_zpos_in_use */
660 while (!wl_list_empty(&zpos_candidate_list)) {
661 struct drm_plane_zpos *head_p_zpos =
662 wl_container_of(zpos_candidate_list.next,
663 head_p_zpos, link);
664 struct drm_plane *plane = head_p_zpos->plane;
665 const char *p_name = drm_output_get_plane_type_name(plane);
666 uint64_t zpos;
667
668 if (current_lowest_zpos == DRM_PLANE_ZPOS_INVALID_PLANE)
669 zpos = plane->zpos_max;
670 else
671 zpos = MIN(current_lowest_zpos - 1, plane->zpos_max);
672
673 drm_debug(b, "\t\t\t\t[plane] plane %d picked "
674 "from candidate list, type: %s\n",
675 plane->plane_id, p_name);
676
Marius Vladeef69452019-10-31 12:48:35 +0200677 ps = drm_output_try_view_on_plane(plane, state, ev,
678 mode, fb, zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300679 drm_output_destroy_zpos_plane(head_p_zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300680 if (ps) {
681 drm_debug(b, "\t\t\t\t[view] view %p has been placed to "
682 "%s plane with computed zpos %"PRIu64"\n",
683 ev, p_name, zpos);
684 break;
685 }
686 }
687
688 wl_list_for_each_safe(p_zpos, p_zpos_next, &zpos_candidate_list, link)
689 drm_output_destroy_zpos_plane(p_zpos);
690
Marius Vlad26dcce02019-10-24 14:49:33 +0300691 drm_fb_unref(fb);
Marius Vlad2538aac2019-10-14 11:05:30 +0300692 return ps;
693}
694
Daniel Stonee404b722019-06-22 18:40:31 +0100695static struct drm_output_state *
696drm_output_propose_state(struct weston_output *output_base,
697 struct drm_pending_state *pending_state,
698 enum drm_output_propose_state_mode mode)
699{
700 struct drm_output *output = to_drm_output(output_base);
701 struct drm_backend *b = to_drm_backend(output->base.compositor);
702 struct drm_output_state *state;
703 struct drm_plane_state *scanout_state = NULL;
704 struct weston_view *ev;
Marius Vlad80a62e52019-09-11 18:21:59 +0300705
706 pixman_region32_t surface_overlap, renderer_region, planes_region;
707 pixman_region32_t occluded_region;
708
Daniel Stonee404b722019-06-22 18:40:31 +0100709 bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
710 int ret;
Marius Vlad2538aac2019-10-14 11:05:30 +0300711 uint64_t current_lowest_zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stonee404b722019-06-22 18:40:31 +0100712
713 assert(!output->state_last);
714 state = drm_output_state_duplicate(output->state_cur,
715 pending_state,
716 DRM_OUTPUT_STATE_CLEAR_PLANES);
717
718 /* We implement mixed mode by progressively creating and testing
719 * incremental states, of scanout + overlay + cursor. Since we
720 * walk our views top to bottom, the scanout plane is last, however
721 * we always need it in our scene for the test modeset to be
722 * meaningful. To do this, we steal a reference to the last
723 * renderer framebuffer we have, if we think it's basically
724 * compatible. If we don't have that, then we conservatively fall
725 * back to only using the renderer for this repaint. */
726 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
727 struct drm_plane *plane = output->scanout_plane;
728 struct drm_fb *scanout_fb = plane->state_cur->fb;
729
730 if (!scanout_fb ||
731 (scanout_fb->type != BUFFER_GBM_SURFACE &&
732 scanout_fb->type != BUFFER_PIXMAN_DUMB)) {
733 drm_debug(b, "\t\t[state] cannot propose mixed mode: "
734 "for output %s (%lu): no previous renderer "
735 "fb\n",
736 output->base.name,
737 (unsigned long) output->base.id);
738 drm_output_state_free(state);
739 return NULL;
740 }
741
742 if (scanout_fb->width != output_base->current_mode->width ||
743 scanout_fb->height != output_base->current_mode->height) {
744 drm_debug(b, "\t\t[state] cannot propose mixed mode "
745 "for output %s (%lu): previous fb has "
746 "different size\n",
747 output->base.name,
748 (unsigned long) output->base.id);
749 drm_output_state_free(state);
750 return NULL;
751 }
752
753 scanout_state = drm_plane_state_duplicate(state,
754 plane->state_cur);
755 drm_debug(b, "\t\t[state] using renderer FB ID %lu for mixed "
756 "mode for output %s (%lu)\n",
757 (unsigned long) scanout_fb->fb_id, output->base.name,
758 (unsigned long) output->base.id);
759 }
760
Marius Vlad80a62e52019-09-11 18:21:59 +0300761 /* - renderer_region contains the total region which which will be
762 * covered by the renderer
763 * - planes_region contains the total region which has been covered by
764 * hardware planes
765 * - occluded_region contains the total region which which will be
766 * covered by the renderer and hardware planes, where the view's
767 * visible-and-opaque region is added in both cases (the view's
768 * opaque region accumulates there for each view); it is being used
769 * to skip the view, if it is completely occluded; includes the
770 * situation where occluded_region covers entire output's region.
Daniel Stonee404b722019-06-22 18:40:31 +0100771 */
772 pixman_region32_init(&renderer_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300773 pixman_region32_init(&planes_region);
Daniel Stonee404b722019-06-22 18:40:31 +0100774 pixman_region32_init(&occluded_region);
775
776 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
777 struct drm_plane_state *ps = NULL;
778 bool force_renderer = false;
779 pixman_region32_t clipped_view;
780 bool totally_occluded = false;
Daniel Stonee404b722019-06-22 18:40:31 +0100781
782 drm_debug(b, "\t\t\t[view] evaluating view %p for "
783 "output %s (%lu)\n",
784 ev, output->base.name,
785 (unsigned long) output->base.id);
786
787 /* If this view doesn't touch our output at all, there's no
788 * reason to do anything with it. */
789 if (!(ev->output_mask & (1u << output->base.id))) {
790 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
791 "(not on our output)\n", ev);
792 continue;
793 }
794
795 /* We only assign planes to views which are exclusively present
796 * on our output. */
797 if (ev->output_mask != (1u << output->base.id)) {
798 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
799 "(on multiple outputs)\n", ev);
800 force_renderer = true;
801 }
802
Marius Vlad5f6bee42019-09-11 16:41:04 +0300803 if (!weston_view_has_valid_buffer(ev)) {
Daniel Stonee404b722019-06-22 18:40:31 +0100804 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
805 "(no buffer available)\n", ev);
806 force_renderer = true;
807 }
808
809 /* Ignore views we know to be totally occluded. */
810 pixman_region32_init(&clipped_view);
811 pixman_region32_intersect(&clipped_view,
812 &ev->transform.boundingbox,
813 &output->base.region);
814
815 pixman_region32_init(&surface_overlap);
816 pixman_region32_subtract(&surface_overlap, &clipped_view,
817 &occluded_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300818 /* if the view is completely occluded then ignore that
819 * view; includes the case where occluded_region covers
820 * the entire output */
Daniel Stonee404b722019-06-22 18:40:31 +0100821 totally_occluded = !pixman_region32_not_empty(&surface_overlap);
822 if (totally_occluded) {
823 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
824 "(occluded on our output)\n", ev);
825 pixman_region32_fini(&surface_overlap);
826 pixman_region32_fini(&clipped_view);
827 continue;
828 }
829
830 /* Since we process views from top to bottom, we know that if
831 * the view intersects the calculated renderer region, it must
832 * be part of, or occluded by, it, and cannot go on a plane. */
833 pixman_region32_intersect(&surface_overlap, &renderer_region,
834 &clipped_view);
835 if (pixman_region32_not_empty(&surface_overlap)) {
836 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
837 "(occluded by renderer views)\n", ev);
838 force_renderer = true;
839 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300840 pixman_region32_fini(&surface_overlap);
Daniel Stonee404b722019-06-22 18:40:31 +0100841
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530842 /* In case of enforced mode of content-protection do not
843 * assign planes for a protected surface on an unsecured output.
844 */
845 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
846 ev->surface->desired_protection > output_base->current_protection) {
847 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
848 "(enforced protection mode on unsecured output)\n", ev);
849 force_renderer = true;
850 }
851
Marius Vlad2538aac2019-10-14 11:05:30 +0300852 if (!force_renderer) {
853 drm_debug(b, "\t\t\t[plane] started with zpos %"PRIu64"\n",
854 current_lowest_zpos);
855 ps = drm_output_prepare_plane_view(state, ev, mode,
856 current_lowest_zpos);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000857 }
Daniel Stone2cb926c2019-11-11 16:48:54 +0000858
859 if (ps) {
Marius Vlad2538aac2019-10-14 11:05:30 +0300860 current_lowest_zpos = ps->zpos;
861 drm_debug(b, "\t\t\t[plane] next zpos to use %"PRIu64"\n",
862 current_lowest_zpos);
863
Daniel Stonee404b722019-06-22 18:40:31 +0100864 /* If we have been assigned to an overlay or scanout
865 * plane, add this area to the occluded region, so
866 * other views are known to be behind it. The cursor
867 * plane, however, is special, in that it blends with
868 * the content underneath it: the area should neither
869 * be added to the renderer region nor the occluded
870 * region. */
871 if (ps->plane->type != WDRM_PLANE_TYPE_CURSOR) {
Marius Vlad80a62e52019-09-11 18:21:59 +0300872 pixman_region32_union(&planes_region,
873 &planes_region,
874 &clipped_view);
875
876 if (!weston_view_is_opaque(ev, &clipped_view))
877 pixman_region32_intersect(&clipped_view,
878 &clipped_view,
879 &ev->transform.opaque);
880 /* the visible-and-opaque region of this view
881 * will occlude views underneath it */
Daniel Stonee404b722019-06-22 18:40:31 +0100882 pixman_region32_union(&occluded_region,
883 &occluded_region,
884 &clipped_view);
Marius Vlad80a62e52019-09-11 18:21:59 +0300885
Daniel Stonee404b722019-06-22 18:40:31 +0100886 pixman_region32_fini(&clipped_view);
Marius Vlad80a62e52019-09-11 18:21:59 +0300887
Daniel Stonee404b722019-06-22 18:40:31 +0100888 }
889 continue;
890 }
891
892 /* We have been assigned to the primary (renderer) plane:
893 * check if this is OK, and add ourselves to the renderer
894 * region if so. */
895 if (!renderer_ok) {
896 drm_debug(b, "\t\t[view] failing state generation: "
897 "placing view %p to renderer not allowed\n",
898 ev);
899 pixman_region32_fini(&clipped_view);
900 goto err_region;
901 }
902
903 pixman_region32_union(&renderer_region,
904 &renderer_region,
905 &clipped_view);
Marius Vlad80a62e52019-09-11 18:21:59 +0300906
907 if (!weston_view_is_opaque(ev, &clipped_view))
908 pixman_region32_intersect(&clipped_view,
909 &clipped_view,
910 &ev->transform.opaque);
911
912 pixman_region32_union(&occluded_region,
913 &occluded_region,
914 &clipped_view);
915
Daniel Stonee404b722019-06-22 18:40:31 +0100916 pixman_region32_fini(&clipped_view);
917 }
Marius Vlad80a62e52019-09-11 18:21:59 +0300918
Daniel Stonee404b722019-06-22 18:40:31 +0100919 pixman_region32_fini(&renderer_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300920 pixman_region32_fini(&planes_region);
Daniel Stonee404b722019-06-22 18:40:31 +0100921 pixman_region32_fini(&occluded_region);
922
923 /* In renderer-only mode, we can't test the state as we don't have a
924 * renderer buffer yet. */
925 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY)
926 return state;
927
Marius Vlad2538aac2019-10-14 11:05:30 +0300928 /* check if we have invalid zpos values, like duplicate(s) */
929 ret = drm_output_check_zpos_plane_states(state);
930 if (ret != 0) {
931 drm_debug(b, "\t\t[view] failing state generation: "
932 "zpos values are in-consistent\n");
933 goto err;
934 }
935
Daniel Stonee404b722019-06-22 18:40:31 +0100936 /* Check to see if this state will actually work. */
937 ret = drm_pending_state_test(state->pending_state);
938 if (ret != 0) {
939 drm_debug(b, "\t\t[view] failing state generation: "
940 "atomic test not OK\n");
941 goto err;
942 }
943
944 /* Counterpart to duplicating scanout state at the top of this
945 * function: if we have taken a renderer framebuffer and placed it in
946 * the pending state in order to incrementally test overlay planes,
947 * remove it now. */
948 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
949 assert(scanout_state->fb->type == BUFFER_GBM_SURFACE ||
950 scanout_state->fb->type == BUFFER_PIXMAN_DUMB);
951 drm_plane_state_put_back(scanout_state);
952 }
953 return state;
954
955err_region:
956 pixman_region32_fini(&renderer_region);
957 pixman_region32_fini(&occluded_region);
958err:
959 drm_output_state_free(state);
960 return NULL;
961}
962
963void
964drm_assign_planes(struct weston_output *output_base, void *repaint_data)
965{
966 struct drm_backend *b = to_drm_backend(output_base->compositor);
967 struct drm_pending_state *pending_state = repaint_data;
968 struct drm_output *output = to_drm_output(output_base);
969 struct drm_output_state *state = NULL;
970 struct drm_plane_state *plane_state;
971 struct weston_view *ev;
972 struct weston_plane *primary = &output_base->compositor->primary_plane;
973 enum drm_output_propose_state_mode mode = DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY;
974
975 drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n",
976 output_base->name, (unsigned long) output_base->id);
977
978 if (!b->sprites_are_broken && !output->virtual) {
979 drm_debug(b, "\t[repaint] trying planes-only build state\n");
980 state = drm_output_propose_state(output_base, pending_state, mode);
981 if (!state) {
982 drm_debug(b, "\t[repaint] could not build planes-only "
983 "state, trying mixed\n");
984 mode = DRM_OUTPUT_PROPOSE_STATE_MIXED;
985 state = drm_output_propose_state(output_base,
986 pending_state,
987 mode);
988 }
989 if (!state) {
990 drm_debug(b, "\t[repaint] could not build mixed-mode "
991 "state, trying renderer-only\n");
992 }
993 } else {
994 drm_debug(b, "\t[state] no overlay plane support\n");
995 }
996
997 if (!state) {
998 mode = DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY;
999 state = drm_output_propose_state(output_base, pending_state,
1000 mode);
1001 }
1002
1003 assert(state);
1004 drm_debug(b, "\t[repaint] Using %s composition\n",
1005 drm_propose_state_mode_to_string(mode));
1006
1007 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
1008 struct drm_plane *target_plane = NULL;
1009
1010 /* If this view doesn't touch our output at all, there's no
1011 * reason to do anything with it. */
1012 if (!(ev->output_mask & (1u << output->base.id)))
1013 continue;
1014
1015 /* Test whether this buffer can ever go into a plane:
1016 * non-shm, or small enough to be a cursor.
1017 *
1018 * Also, keep a reference when using the pixman renderer.
1019 * That makes it possible to do a seamless switch to the GL
1020 * renderer and since the pixman renderer keeps a reference
1021 * to the buffer anyway, there is no side effects.
1022 */
1023 if (b->use_pixman ||
Marius Vlad5f6bee42019-09-11 16:41:04 +03001024 (weston_view_has_valid_buffer(ev) &&
Daniel Stonee404b722019-06-22 18:40:31 +01001025 (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
1026 (ev->surface->width <= b->cursor_width &&
1027 ev->surface->height <= b->cursor_height))))
1028 ev->surface->keep_buffer = true;
1029 else
1030 ev->surface->keep_buffer = false;
1031
1032 /* This is a bit unpleasant, but lacking a temporary place to
1033 * hang a plane off the view, we have to do a nested walk.
1034 * Our first-order iteration has to be planes rather than
1035 * views, because otherwise we won't reset views which were
1036 * previously on planes to being on the primary plane. */
1037 wl_list_for_each(plane_state, &state->plane_list, link) {
1038 if (plane_state->ev == ev) {
1039 plane_state->ev = NULL;
1040 target_plane = plane_state->plane;
1041 break;
1042 }
1043 }
1044
1045 if (target_plane) {
1046 drm_debug(b, "\t[repaint] view %p on %s plane %lu\n",
1047 ev, plane_type_enums[target_plane->type].name,
1048 (unsigned long) target_plane->plane_id);
1049 weston_view_move_to_plane(ev, &target_plane->base);
1050 } else {
1051 drm_debug(b, "\t[repaint] view %p using renderer "
1052 "composition\n", ev);
1053 weston_view_move_to_plane(ev, primary);
1054 }
1055
1056 if (!target_plane ||
1057 target_plane->type == WDRM_PLANE_TYPE_CURSOR) {
1058 /* cursor plane & renderer involve a copy */
1059 ev->psf_flags = 0;
1060 } else {
1061 /* All other planes are a direct scanout of a
1062 * single client buffer.
1063 */
1064 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1065 }
1066 }
1067
1068 /* We rely on output->cursor_view being both an accurate reflection of
1069 * the cursor plane's state, but also being maintained across repaints
1070 * to avoid unnecessary damage uploads, per the comment in
1071 * drm_output_prepare_cursor_view. In the event that we go from having
1072 * a cursor view to not having a cursor view, we need to clear it. */
1073 if (output->cursor_view) {
1074 plane_state =
1075 drm_output_state_get_existing_plane(state,
1076 output->cursor_plane);
1077 if (!plane_state || !plane_state->fb)
1078 output->cursor_view = NULL;
1079 }
1080}