blob: 87bc9db19e07d45b4e0f6f3fa136cd7a70d88215 [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 Stonee404b722019-06-22 18:40:31 +0100190
191 assert(!b->sprites_are_broken);
192 assert(b->atomic_modeset);
193
Daniel Stonee404b722019-06-22 18:40:31 +0100194 if (!fb) {
195 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
196 " couldn't get fb\n", ev);
197 return NULL;
198 }
199
Marius Vlad677e4592019-10-25 00:07:37 +0300200 state = drm_output_state_get_plane(output_state, plane);
Marius Vladeef69452019-10-31 12:48:35 +0200201 /* we can't have a 'pending' framebuffer as never set one before reaching here */
202 assert(!state->fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100203
Marius Vlad677e4592019-10-25 00:07:37 +0300204 state->ev = ev;
205 state->output = output;
Marius Vladeef69452019-10-31 12:48:35 +0200206
Marius Vlad677e4592019-10-25 00:07:37 +0300207 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
208 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
209 "unsuitable transform\n", ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100210 drm_plane_state_put_back(state);
211 state = NULL;
Marius Vlad677e4592019-10-25 00:07:37 +0300212 goto out;
Daniel Stonee404b722019-06-22 18:40:31 +0100213 }
214
Marius Vlad677e4592019-10-25 00:07:37 +0300215 /* If the surface buffer has an in-fence fd, but the plane
216 * doesn't support fences, we can't place the buffer on this
217 * plane. */
218 if (ev->surface->acquire_fence_fd >= 0 &&
219 plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
220 drm_debug(b, "\t\t\t\t[overlay] not placing view %p on overlay: "
221 "no in-fence support\n", ev);
222 drm_plane_state_put_back(state);
223 state = NULL;
224 goto out;
225 }
226
Marius Vladeef69452019-10-31 12:48:35 +0200227 /* We hold one reference for the lifetime of this function; from
228 * calling drm_fb_get_from_view() in drm_output_prepare_plane_view(),
229 * so, we take another reference here to live within the state. */
Marius Vlad677e4592019-10-25 00:07:37 +0300230 state->fb = drm_fb_ref(fb);
231
232 state->in_fence_fd = ev->surface->acquire_fence_fd;
233
234 /* In planes-only mode, we don't have an incremental state to
235 * test against, so we just hope it'll work. */
236 if (mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
237 drm_debug(b, "\t\t\t[overlay] provisionally placing "
238 "view %p on overlay %lu in planes-only mode\n",
239 ev, (unsigned long) plane->plane_id);
Marius Vlad677e4592019-10-25 00:07:37 +0300240 goto out;
241 }
242
243 ret = drm_pending_state_test(output_state->pending_state);
244 if (ret == 0) {
245 drm_debug(b, "\t\t\t[overlay] provisionally placing "
246 "view %p on overlay %d in mixed mode\n",
247 ev, plane->plane_id);
Marius Vlad677e4592019-10-25 00:07:37 +0300248 goto out;
249 }
250
251 drm_debug(b, "\t\t\t[overlay] not placing view %p on overlay %lu "
252 "in mixed mode: kernel test failed\n",
253 ev, (unsigned long) plane->plane_id);
254
255 drm_plane_state_put_back(state);
256 state = NULL;
257
Daniel Stonee404b722019-06-22 18:40:31 +0100258out:
Daniel Stonee404b722019-06-22 18:40:31 +0100259 return state;
260}
261
Stefan Agnerccf24072019-07-09 22:02:00 +0200262#ifdef BUILD_DRM_GBM
Daniel Stonee404b722019-06-22 18:40:31 +0100263/**
264 * Update the image for the current cursor surface
265 *
266 * @param plane_state DRM cursor plane state
267 * @param ev Source view for cursor
268 */
269static void
270cursor_bo_update(struct drm_plane_state *plane_state, struct weston_view *ev)
271{
272 struct drm_backend *b = plane_state->plane->backend;
273 struct gbm_bo *bo = plane_state->fb->bo;
274 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
275 uint32_t buf[b->cursor_width * b->cursor_height];
276 int32_t stride;
277 uint8_t *s;
278 int i;
279
280 assert(buffer && buffer->shm_buffer);
281 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
282 assert(buffer->width <= b->cursor_width);
283 assert(buffer->height <= b->cursor_height);
284
285 memset(buf, 0, sizeof buf);
286 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
287 s = wl_shm_buffer_get_data(buffer->shm_buffer);
288
289 wl_shm_buffer_begin_access(buffer->shm_buffer);
290 for (i = 0; i < buffer->height; i++)
291 memcpy(buf + i * b->cursor_width,
292 s + i * stride,
293 buffer->width * 4);
294 wl_shm_buffer_end_access(buffer->shm_buffer);
295
296 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
297 weston_log("failed update cursor: %s\n", strerror(errno));
298}
299
300static struct drm_plane_state *
301drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300302 struct weston_view *ev, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100303{
304 struct drm_output *output = output_state->output;
305 struct drm_backend *b = to_drm_backend(output->base.compositor);
306 struct drm_plane *plane = output->cursor_plane;
307 struct drm_plane_state *plane_state;
Daniel Stonee404b722019-06-22 18:40:31 +0100308 bool needs_update = false;
Marius Vlad36f11a52019-11-01 23:49:21 +0200309 const char *p_name = drm_output_get_plane_type_name(plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100310
311 assert(!b->cursors_are_broken);
312
313 if (!plane)
314 return NULL;
315
316 if (!plane->state_cur->complete)
317 return NULL;
318
319 if (plane->state_cur->output && plane->state_cur->output != output)
320 return NULL;
321
322 /* We use GBM to import SHM buffers. */
323 if (b->gbm == NULL)
324 return NULL;
325
Leandro Ribeiro05cecc82020-08-13 17:34:58 -0300326 plane_state = drm_output_state_get_plane(output_state, plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100327
328 if (plane_state && plane_state->fb)
329 return NULL;
330
331 /* We can't scale with the legacy API, and we don't try to account for
332 * simple cropping/translation in cursor_bo_update. */
333 plane_state->output = output;
Marius Vlad36f11a52019-11-01 23:49:21 +0200334 if (!drm_plane_state_coords_for_view(plane_state, ev, zpos)) {
335 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
336 "unsuitable transform\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100337 goto err;
Marius Vlad36f11a52019-11-01 23:49:21 +0200338 }
Daniel Stonee404b722019-06-22 18:40:31 +0100339
340 if (plane_state->src_x != 0 || plane_state->src_y != 0 ||
341 plane_state->src_w > (unsigned) b->cursor_width << 16 ||
342 plane_state->src_h > (unsigned) b->cursor_height << 16 ||
343 plane_state->src_w != plane_state->dest_w << 16 ||
344 plane_state->src_h != plane_state->dest_h << 16) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200345 drm_debug(b, "\t\t\t\t[%s] not assigning view %p to %s plane "
346 "(positioning requires cropping or scaling)\n",
347 p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100348 goto err;
349 }
350
351 /* Since we're setting plane state up front, we need to work out
352 * whether or not we need to upload a new cursor. We can't use the
353 * plane damage, since the planes haven't actually been calculated
354 * yet: instead try to figure it out directly. KMS cursor planes are
355 * pretty unique here, in that they lie partway between a Weston plane
356 * (direct scanout) and a renderer. */
357 if (ev != output->cursor_view ||
358 pixman_region32_not_empty(&ev->surface->damage)) {
359 output->current_cursor++;
360 output->current_cursor =
361 output->current_cursor %
362 ARRAY_LENGTH(output->gbm_cursor_fb);
363 needs_update = true;
364 }
365
366 output->cursor_view = ev;
367 plane_state->ev = ev;
368
369 plane_state->fb =
370 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
371
372 if (needs_update) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200373 drm_debug(b, "\t\t\t\t[%s] copying new content to cursor BO\n", p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100374 cursor_bo_update(plane_state, ev);
375 }
376
377 /* The cursor API is somewhat special: in cursor_bo_update(), we upload
378 * a buffer which is always cursor_width x cursor_height, even if the
379 * surface we want to promote is actually smaller than this. Manually
380 * mangle the plane state to deal with this. */
381 plane_state->src_w = b->cursor_width << 16;
382 plane_state->src_h = b->cursor_height << 16;
383 plane_state->dest_w = b->cursor_width;
384 plane_state->dest_h = b->cursor_height;
385
Marius Vlad36f11a52019-11-01 23:49:21 +0200386 drm_debug(b, "\t\t\t\t[%s] provisionally assigned view %p to cursor\n",
387 p_name, ev);
Daniel Stonee404b722019-06-22 18:40:31 +0100388
389 return plane_state;
390
391err:
392 drm_plane_state_put_back(plane_state);
393 return NULL;
394}
Stefan Agnerccf24072019-07-09 22:02:00 +0200395#else
396static struct drm_plane_state *
397drm_output_prepare_cursor_view(struct drm_output_state *output_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300398 struct weston_view *ev, uint64_t zpos)
Stefan Agnerccf24072019-07-09 22:02:00 +0200399{
400 return NULL;
401}
402#endif
Daniel Stonee404b722019-06-22 18:40:31 +0100403
404static struct drm_plane_state *
405drm_output_prepare_scanout_view(struct drm_output_state *output_state,
406 struct weston_view *ev,
Marius Vlad2538aac2019-10-14 11:05:30 +0300407 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200408 struct drm_fb *fb, uint64_t zpos)
Daniel Stonee404b722019-06-22 18:40:31 +0100409{
410 struct drm_output *output = output_state->output;
411 struct drm_backend *b = to_drm_backend(output->base.compositor);
412 struct drm_plane *scanout_plane = output->scanout_plane;
413 struct drm_plane_state *state;
Marius Vlad36f11a52019-11-01 23:49:21 +0200414 const char *p_name = drm_output_get_plane_type_name(scanout_plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100415
416 assert(!b->sprites_are_broken);
417 assert(b->atomic_modeset);
418 assert(mode == DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
419
420 /* Check the view spans exactly the output size, calculated in the
421 * logical co-ordinate space. */
Marius Vlad28bb2da2019-10-19 18:18:38 +0300422 if (!weston_view_matches_output_entirely(ev, &output->base)) {
423 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
424 " view does not match output entirely\n",
425 p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100426 return NULL;
Marius Vlad28bb2da2019-10-19 18:18:38 +0300427 }
Daniel Stonee404b722019-06-22 18:40:31 +0100428
429 /* If the surface buffer has an in-fence fd, but the plane doesn't
430 * support fences, we can't place the buffer on this plane. */
431 if (ev->surface->acquire_fence_fd >= 0 &&
Marius Vlad36f11a52019-11-01 23:49:21 +0200432 scanout_plane->props[WDRM_PLANE_IN_FENCE_FD].prop_id == 0) {
433 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
434 "no in-fence support\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100435 return NULL;
Marius Vlad36f11a52019-11-01 23:49:21 +0200436 }
Daniel Stonee404b722019-06-22 18:40:31 +0100437
Daniel Stonee404b722019-06-22 18:40:31 +0100438 if (!fb) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200439 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
440 " couldn't get fb\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100441 return NULL;
442 }
443
444 state = drm_output_state_get_plane(output_state, scanout_plane);
445
446 /* The only way we can already have a buffer in the scanout plane is
447 * if we are in mixed mode, or if a client buffer has already been
448 * placed into scanout. The former case will never call into here,
449 * and in the latter case, the view must have been marked as occluded,
450 * meaning we should never have ended up here. */
451 assert(!state->fb);
Marius Vladeef69452019-10-31 12:48:35 +0200452
453 /* take another reference here to live within the state */
454 state->fb = drm_fb_ref(fb);
Daniel Stonee404b722019-06-22 18:40:31 +0100455 state->ev = ev;
456 state->output = output;
Marius Vlad36f11a52019-11-01 23:49:21 +0200457 if (!drm_plane_state_coords_for_view(state, ev, zpos)) {
458 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
459 "unsuitable transform\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100460 goto err;
Marius Vlad36f11a52019-11-01 23:49:21 +0200461 }
Daniel Stonee404b722019-06-22 18:40:31 +0100462
463 if (state->dest_x != 0 || state->dest_y != 0 ||
464 state->dest_w != (unsigned) output->base.current_mode->width ||
Marius Vlad36f11a52019-11-01 23:49:21 +0200465 state->dest_h != (unsigned) output->base.current_mode->height) {
466 drm_debug(b, "\t\t\t\t[%s] not placing view %p on %s: "
467 " invalid plane state\n", p_name, ev, p_name);
Daniel Stonee404b722019-06-22 18:40:31 +0100468 goto err;
Marius Vlad36f11a52019-11-01 23:49:21 +0200469 }
Daniel Stonee404b722019-06-22 18:40:31 +0100470
471 state->in_fence_fd = ev->surface->acquire_fence_fd;
472
473 /* In plane-only mode, we don't need to test the state now, as we
474 * will only test it once at the end. */
475 return state;
476
477err:
478 drm_plane_state_put_back(state);
479 return NULL;
480}
481
Marius Vlad26dcce02019-10-24 14:49:33 +0300482static bool
483drm_output_plane_view_has_valid_format(struct drm_plane *plane,
484 struct drm_output_state *state,
485 struct weston_view *ev,
486 struct drm_fb *fb)
487{
488 /* depending on the type of the plane we have different requirements */
489 switch (plane->type) {
490 case WDRM_PLANE_TYPE_CURSOR:
491 return drm_output_plane_cursor_has_valid_format(ev);
492 case WDRM_PLANE_TYPE_OVERLAY:
493 return drm_output_plane_has_valid_format(plane, state, fb);
494 case WDRM_PLANE_TYPE_PRIMARY:
495 return drm_output_plane_has_valid_format(plane, state, fb);
496 default:
497 assert(0);
498 return false;
499 }
500
501 return false;
502}
503
Marius Vlad2538aac2019-10-14 11:05:30 +0300504static struct drm_plane_state *
505drm_output_try_view_on_plane(struct drm_plane *plane,
506 struct drm_output_state *state,
507 struct weston_view *ev,
508 enum drm_output_propose_state_mode mode,
Marius Vladeef69452019-10-31 12:48:35 +0200509 struct drm_fb *fb, uint64_t zpos)
Marius Vlad2538aac2019-10-14 11:05:30 +0300510{
511 struct drm_backend *b = state->pending_state->backend;
512 struct weston_output *wet_output = &state->output->base;
513 bool view_matches_entire_output, scanout_has_view_assigned;
514 struct drm_plane *scanout_plane = state->output->scanout_plane;
Marius Vlad36f11a52019-11-01 23:49:21 +0200515 struct drm_plane_state *ps = NULL;
516 const char *p_name = drm_output_get_plane_type_name(plane);
517 enum {
518 NO_PLANES, /* generic err-handle */
519 NO_PLANES_ACCEPTED,
520 PLACED_ON_PLANE,
521 } availability = NO_PLANES;
Marius Vlad2538aac2019-10-14 11:05:30 +0300522
523 /* sanity checks in case we over/underflow zpos or pass incorrect
524 * values */
525 assert(zpos <= plane->zpos_max ||
526 zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
527
528 switch (plane->type) {
529 case WDRM_PLANE_TYPE_CURSOR:
530 if (b->cursors_are_broken) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200531 availability = NO_PLANES_ACCEPTED;
532 goto out;
Marius Vlad2538aac2019-10-14 11:05:30 +0300533 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200534
535 ps = drm_output_prepare_cursor_view(state, ev, zpos);
536 if (ps)
537 availability = PLACED_ON_PLANE;
538 break;
Marius Vlad2538aac2019-10-14 11:05:30 +0300539 case WDRM_PLANE_TYPE_OVERLAY:
540 /* do not attempt to place it in the overlay if we don't have
541 * anything in the scanout/primary and the view doesn't cover
542 * the entire output */
543 view_matches_entire_output =
544 weston_view_matches_output_entirely(ev, wet_output);
545 scanout_has_view_assigned =
546 drm_output_check_plane_has_view_assigned(scanout_plane,
547 state);
548
549 if (view_matches_entire_output && !scanout_has_view_assigned) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200550 availability = NO_PLANES_ACCEPTED;
551 goto out;
Marius Vlad2538aac2019-10-14 11:05:30 +0300552 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200553
554 ps = drm_output_prepare_overlay_view(plane, state, ev, mode,
555 fb, zpos);
556 if (ps)
557 availability = PLACED_ON_PLANE;
558 break;
Marius Vlad2538aac2019-10-14 11:05:30 +0300559 case WDRM_PLANE_TYPE_PRIMARY:
560 if (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY) {
Marius Vlad36f11a52019-11-01 23:49:21 +0200561 availability = NO_PLANES_ACCEPTED;
562 goto out;
Marius Vlad2538aac2019-10-14 11:05:30 +0300563 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200564
565 ps = drm_output_prepare_scanout_view(state, ev, mode,
566 fb, zpos);
567 if (ps)
568 availability = PLACED_ON_PLANE;
569 break;
Marius Vlad2538aac2019-10-14 11:05:30 +0300570 default:
571 assert(0);
572 break;
573 }
Marius Vlad36f11a52019-11-01 23:49:21 +0200574
575out:
576 switch (availability) {
577 case NO_PLANES:
578 /* set initial to this catch-all case, such that
579 * prepare_cursor/overlay/scanout() should have/contain the
580 * reason for failling */
581 break;
582 case NO_PLANES_ACCEPTED:
583 drm_debug(b, "\t\t\t\t[plane] plane %d refusing to "
584 "place view %p in %s\n",
585 plane->plane_id, ev, p_name);
586 break;
587 case PLACED_ON_PLANE:
588 break;
589 }
590
591
592 return ps;
Marius Vlad2538aac2019-10-14 11:05:30 +0300593}
594
Marius Vlad48bc5ef2019-11-15 14:32:08 +0200595static void
Marius Vlad2538aac2019-10-14 11:05:30 +0300596drm_output_check_zpos_plane_states(struct drm_output_state *state)
597{
Marius Vlad2538aac2019-10-14 11:05:30 +0300598 struct drm_plane_state *ps;
Marius Vlad2538aac2019-10-14 11:05:30 +0300599
600 wl_list_for_each(ps, &state->plane_list, link) {
601 struct wl_list *next_node = ps->link.next;
602 bool found_dup = false;
603
Marius Vlad788e80d2019-11-15 00:25:11 +0200604 /* skip any plane that is not enabled */
605 if (!ps->fb)
606 continue;
607
608 assert(ps->zpos != DRM_PLANE_ZPOS_INVALID_PLANE);
609
Marius Vlad2538aac2019-10-14 11:05:30 +0300610 /* find another plane with the same zpos value */
611 if (next_node == &state->plane_list)
612 break;
613
614 while (next_node && next_node != &state->plane_list) {
615 struct drm_plane_state *ps_next;
616
617 ps_next = container_of(next_node,
618 struct drm_plane_state,
619 link);
620
621 if (ps->zpos == ps_next->zpos) {
622 found_dup = true;
623 break;
624 }
625 next_node = next_node->next;
626 }
627
Marius Vlad48bc5ef2019-11-15 14:32:08 +0200628 /* this should never happen so exit hard in case
629 * we screwed up that bad */
630 assert(!found_dup);
Marius Vlad2538aac2019-10-14 11:05:30 +0300631 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300632}
633
634static struct drm_plane_state *
635drm_output_prepare_plane_view(struct drm_output_state *state,
636 struct weston_view *ev,
637 enum drm_output_propose_state_mode mode,
Marius Vladbd002b92019-11-15 13:33:38 +0200638 struct drm_plane_state *scanout_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300639 uint64_t current_lowest_zpos)
640{
641 struct drm_output *output = state->output;
642 struct drm_backend *b = to_drm_backend(output->base.compositor);
643
644 struct drm_plane_state *ps = NULL;
645 struct drm_plane *plane;
646 struct drm_plane_zpos *p_zpos, *p_zpos_next;
647 struct wl_list zpos_candidate_list;
648
Marius Vlad26dcce02019-10-24 14:49:33 +0300649 struct drm_fb *fb;
650
Marius Vlad2538aac2019-10-14 11:05:30 +0300651 wl_list_init(&zpos_candidate_list);
652
653 /* check view for valid buffer, doesn't make sense to even try */
654 if (!weston_view_has_valid_buffer(ev))
655 return ps;
656
Marius Vlad26dcce02019-10-24 14:49:33 +0300657 fb = drm_fb_get_from_view(state, ev);
658
Marius Vlad2538aac2019-10-14 11:05:30 +0300659 /* assemble a list with possible candidates */
660 wl_list_for_each(plane, &b->plane_list, link) {
661 if (!drm_plane_is_available(plane, output))
662 continue;
663
664 if (drm_output_check_plane_has_view_assigned(plane, state)) {
665 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to"
666 " candidate list: view already assigned "
667 "to a plane\n", plane->plane_id);
668 continue;
669 }
670
671 if (plane->zpos_min >= current_lowest_zpos) {
672 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
Maxime Roussin-Bélanger35e34502020-12-17 17:08:56 -0500673 "candidate list: minimum zpos (%"PRIu64") "
Marius Vlad2538aac2019-10-14 11:05:30 +0300674 "plane's above current lowest zpos "
675 "(%"PRIu64")\n", plane->plane_id,
676 plane->zpos_min, current_lowest_zpos);
677 continue;
678 }
679
Marius Vladbd002b92019-11-15 13:33:38 +0200680 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
681 assert(scanout_state != NULL);
682 if (scanout_state->zpos >= plane->zpos_max) {
683 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
684 "candidate list: primary's zpos "
685 "value (%"PRIu64") higher than "
686 "plane's maximum value (%"PRIu64")\n",
687 plane->plane_id, scanout_state->zpos,
688 plane->zpos_max);
689 continue;
690 }
691 }
692
Marius Vlad18462e82019-11-15 14:11:49 +0200693 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY &&
694 (plane->type == WDRM_PLANE_TYPE_OVERLAY ||
695 plane->type == WDRM_PLANE_TYPE_PRIMARY)) {
696 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
697 "candidate list: renderer-only mode\n",
698 plane->plane_id);
699 continue;
700 }
701
702 if (plane->type != WDRM_PLANE_TYPE_CURSOR &&
703 b->sprites_are_broken) {
704 drm_debug(b, "\t\t\t\t[plane] not adding plane %d, type %s to "
705 "candidate list: sprites are broken!\n",
706 plane->plane_id,
707 drm_output_get_plane_type_name(plane));
708 continue;
709 }
710
Marius Vlad26dcce02019-10-24 14:49:33 +0300711 if (!drm_output_plane_view_has_valid_format(plane, state, ev, fb)) {
712 drm_debug(b, "\t\t\t\t[plane] not adding plane %d to "
713 "candidate list: invalid pixel format\n",
714 plane->plane_id);
715 continue;
716 }
717
Marius Vlad2538aac2019-10-14 11:05:30 +0300718 drm_output_add_zpos_plane(plane, &zpos_candidate_list);
719 }
720
721 /* go over the potential candidate list and try to find a possible
722 * plane suitable for \c ev; start with the highest zpos value of a
723 * plane to maximize our chances, but do note we pass the zpos value
724 * based on current tracked value by \c current_lowest_zpos_in_use */
725 while (!wl_list_empty(&zpos_candidate_list)) {
726 struct drm_plane_zpos *head_p_zpos =
727 wl_container_of(zpos_candidate_list.next,
728 head_p_zpos, link);
729 struct drm_plane *plane = head_p_zpos->plane;
730 const char *p_name = drm_output_get_plane_type_name(plane);
731 uint64_t zpos;
732
733 if (current_lowest_zpos == DRM_PLANE_ZPOS_INVALID_PLANE)
734 zpos = plane->zpos_max;
735 else
736 zpos = MIN(current_lowest_zpos - 1, plane->zpos_max);
737
738 drm_debug(b, "\t\t\t\t[plane] plane %d picked "
739 "from candidate list, type: %s\n",
740 plane->plane_id, p_name);
741
Marius Vladeef69452019-10-31 12:48:35 +0200742 ps = drm_output_try_view_on_plane(plane, state, ev,
743 mode, fb, zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300744 drm_output_destroy_zpos_plane(head_p_zpos);
Marius Vlad2538aac2019-10-14 11:05:30 +0300745 if (ps) {
746 drm_debug(b, "\t\t\t\t[view] view %p has been placed to "
747 "%s plane with computed zpos %"PRIu64"\n",
748 ev, p_name, zpos);
749 break;
750 }
751 }
752
753 wl_list_for_each_safe(p_zpos, p_zpos_next, &zpos_candidate_list, link)
754 drm_output_destroy_zpos_plane(p_zpos);
755
Marius Vlad26dcce02019-10-24 14:49:33 +0300756 drm_fb_unref(fb);
Marius Vlad2538aac2019-10-14 11:05:30 +0300757 return ps;
758}
759
Daniel Stonee404b722019-06-22 18:40:31 +0100760static struct drm_output_state *
761drm_output_propose_state(struct weston_output *output_base,
762 struct drm_pending_state *pending_state,
763 enum drm_output_propose_state_mode mode)
764{
765 struct drm_output *output = to_drm_output(output_base);
766 struct drm_backend *b = to_drm_backend(output->base.compositor);
767 struct drm_output_state *state;
768 struct drm_plane_state *scanout_state = NULL;
769 struct weston_view *ev;
Marius Vlad80a62e52019-09-11 18:21:59 +0300770
Daniel Stone8ca5d732020-08-25 17:44:35 +0100771 pixman_region32_t renderer_region;
Marius Vlad80a62e52019-09-11 18:21:59 +0300772 pixman_region32_t occluded_region;
773
Daniel Stonee404b722019-06-22 18:40:31 +0100774 bool renderer_ok = (mode != DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY);
775 int ret;
Marius Vlad2538aac2019-10-14 11:05:30 +0300776 uint64_t current_lowest_zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stonee404b722019-06-22 18:40:31 +0100777
778 assert(!output->state_last);
779 state = drm_output_state_duplicate(output->state_cur,
780 pending_state,
781 DRM_OUTPUT_STATE_CLEAR_PLANES);
782
783 /* We implement mixed mode by progressively creating and testing
784 * incremental states, of scanout + overlay + cursor. Since we
785 * walk our views top to bottom, the scanout plane is last, however
786 * we always need it in our scene for the test modeset to be
787 * meaningful. To do this, we steal a reference to the last
788 * renderer framebuffer we have, if we think it's basically
789 * compatible. If we don't have that, then we conservatively fall
790 * back to only using the renderer for this repaint. */
791 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
792 struct drm_plane *plane = output->scanout_plane;
793 struct drm_fb *scanout_fb = plane->state_cur->fb;
794
795 if (!scanout_fb ||
796 (scanout_fb->type != BUFFER_GBM_SURFACE &&
797 scanout_fb->type != BUFFER_PIXMAN_DUMB)) {
798 drm_debug(b, "\t\t[state] cannot propose mixed mode: "
799 "for output %s (%lu): no previous renderer "
800 "fb\n",
801 output->base.name,
802 (unsigned long) output->base.id);
803 drm_output_state_free(state);
804 return NULL;
805 }
806
807 if (scanout_fb->width != output_base->current_mode->width ||
808 scanout_fb->height != output_base->current_mode->height) {
809 drm_debug(b, "\t\t[state] cannot propose mixed mode "
810 "for output %s (%lu): previous fb has "
811 "different size\n",
812 output->base.name,
813 (unsigned long) output->base.id);
814 drm_output_state_free(state);
815 return NULL;
816 }
817
818 scanout_state = drm_plane_state_duplicate(state,
819 plane->state_cur);
Daniel Stone71e6ba52020-08-25 17:46:27 +0100820 /* assign the primary the lowest zpos value */
Marius Vladbd002b92019-11-15 13:33:38 +0200821 scanout_state->zpos = plane->zpos_min;
Daniel Stonee404b722019-06-22 18:40:31 +0100822 drm_debug(b, "\t\t[state] using renderer FB ID %lu for mixed "
823 "mode for output %s (%lu)\n",
824 (unsigned long) scanout_fb->fb_id, output->base.name,
825 (unsigned long) output->base.id);
Marius Vladbd002b92019-11-15 13:33:38 +0200826 drm_debug(b, "\t\t[state] scanout will use for zpos %"PRIu64"\n",
827 scanout_state->zpos);
Daniel Stonee404b722019-06-22 18:40:31 +0100828 }
829
Marius Vlad80a62e52019-09-11 18:21:59 +0300830 /* - renderer_region contains the total region which which will be
831 * covered by the renderer
Marius Vlad80a62e52019-09-11 18:21:59 +0300832 * - occluded_region contains the total region which which will be
833 * covered by the renderer and hardware planes, where the view's
834 * visible-and-opaque region is added in both cases (the view's
835 * opaque region accumulates there for each view); it is being used
836 * to skip the view, if it is completely occluded; includes the
837 * situation where occluded_region covers entire output's region.
Daniel Stonee404b722019-06-22 18:40:31 +0100838 */
839 pixman_region32_init(&renderer_region);
840 pixman_region32_init(&occluded_region);
841
842 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
843 struct drm_plane_state *ps = NULL;
844 bool force_renderer = false;
845 pixman_region32_t clipped_view;
Daniel Stone8ca5d732020-08-25 17:44:35 +0100846 pixman_region32_t surface_overlap;
Daniel Stonee404b722019-06-22 18:40:31 +0100847 bool totally_occluded = false;
Daniel Stonee404b722019-06-22 18:40:31 +0100848
849 drm_debug(b, "\t\t\t[view] evaluating view %p for "
850 "output %s (%lu)\n",
851 ev, output->base.name,
852 (unsigned long) output->base.id);
853
854 /* If this view doesn't touch our output at all, there's no
855 * reason to do anything with it. */
856 if (!(ev->output_mask & (1u << output->base.id))) {
857 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
858 "(not on our output)\n", ev);
859 continue;
860 }
861
Daniel Stonee404b722019-06-22 18:40:31 +0100862 /* Ignore views we know to be totally occluded. */
863 pixman_region32_init(&clipped_view);
864 pixman_region32_intersect(&clipped_view,
865 &ev->transform.boundingbox,
866 &output->base.region);
867
868 pixman_region32_init(&surface_overlap);
869 pixman_region32_subtract(&surface_overlap, &clipped_view,
870 &occluded_region);
Marius Vlad80a62e52019-09-11 18:21:59 +0300871 /* if the view is completely occluded then ignore that
872 * view; includes the case where occluded_region covers
873 * the entire output */
Daniel Stonee404b722019-06-22 18:40:31 +0100874 totally_occluded = !pixman_region32_not_empty(&surface_overlap);
875 if (totally_occluded) {
876 drm_debug(b, "\t\t\t\t[view] ignoring view %p "
877 "(occluded on our output)\n", ev);
878 pixman_region32_fini(&surface_overlap);
879 pixman_region32_fini(&clipped_view);
880 continue;
881 }
882
Michael Olbrich3097acc2020-07-14 11:18:54 +0200883 /* We only assign planes to views which are exclusively present
884 * on our output. */
885 if (ev->output_mask != (1u << output->base.id)) {
886 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
887 "(on multiple outputs)\n", ev);
888 force_renderer = true;
889 }
890
891 if (!weston_view_has_valid_buffer(ev)) {
892 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
893 "(no buffer available)\n", ev);
894 force_renderer = true;
895 }
896
Daniel Stonee404b722019-06-22 18:40:31 +0100897 /* Since we process views from top to bottom, we know that if
898 * the view intersects the calculated renderer region, it must
899 * be part of, or occluded by, it, and cannot go on a plane. */
900 pixman_region32_intersect(&surface_overlap, &renderer_region,
901 &clipped_view);
902 if (pixman_region32_not_empty(&surface_overlap)) {
903 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
904 "(occluded by renderer views)\n", ev);
905 force_renderer = true;
906 }
Marius Vlad2538aac2019-10-14 11:05:30 +0300907 pixman_region32_fini(&surface_overlap);
Daniel Stonee404b722019-06-22 18:40:31 +0100908
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530909 /* In case of enforced mode of content-protection do not
910 * assign planes for a protected surface on an unsecured output.
911 */
912 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
913 ev->surface->desired_protection > output_base->current_protection) {
914 drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
915 "(enforced protection mode on unsecured output)\n", ev);
916 force_renderer = true;
917 }
918
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100919 /* Now try to place it on a plane if we can. */
Marius Vlad2538aac2019-10-14 11:05:30 +0300920 if (!force_renderer) {
921 drm_debug(b, "\t\t\t[plane] started with zpos %"PRIu64"\n",
922 current_lowest_zpos);
923 ps = drm_output_prepare_plane_view(state, ev, mode,
Marius Vladbd002b92019-11-15 13:33:38 +0200924 scanout_state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300925 current_lowest_zpos);
Daniel Stone2cb926c2019-11-11 16:48:54 +0000926 }
Daniel Stone2cb926c2019-11-11 16:48:54 +0000927
928 if (ps) {
Marius Vlad2538aac2019-10-14 11:05:30 +0300929 current_lowest_zpos = ps->zpos;
930 drm_debug(b, "\t\t\t[plane] next zpos to use %"PRIu64"\n",
931 current_lowest_zpos);
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100932 } else if (!ps && !renderer_ok) {
Daniel Stonee404b722019-06-22 18:40:31 +0100933 drm_debug(b, "\t\t[view] failing state generation: "
934 "placing view %p to renderer not allowed\n",
935 ev);
936 pixman_region32_fini(&clipped_view);
937 goto err_region;
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100938 } else if (!ps) {
939 /* clipped_view contains the area that's going to be
940 * visible on screen; add this to the renderer region */
941 pixman_region32_union(&renderer_region,
942 &renderer_region,
943 &clipped_view);
Daniel Stonee404b722019-06-22 18:40:31 +0100944
Daniel Stoneebcf4f32020-08-25 17:46:55 +0100945 drm_debug(b, "\t\t\t\t[view] view %p will be placed "
946 "on the renderer\n", ev);
947 }
Marius Vlad80a62e52019-09-11 18:21:59 +0300948
Daniel Stoneb625cdc2020-08-25 17:37:34 +0100949 /* Opaque areas of our clipped view occlude areas behind it;
950 * however, anything not in the opaque region (which is the
951 * entire clipped area if the whole view is known to be
952 * opaque) does not necessarily occlude what's behind it, as
953 * it could be alpha-blended. */
Marius Vlad80a62e52019-09-11 18:21:59 +0300954 if (!weston_view_is_opaque(ev, &clipped_view))
955 pixman_region32_intersect(&clipped_view,
956 &clipped_view,
957 &ev->transform.opaque);
Marius Vlad80a62e52019-09-11 18:21:59 +0300958 pixman_region32_union(&occluded_region,
959 &occluded_region,
960 &clipped_view);
961
Daniel Stonee404b722019-06-22 18:40:31 +0100962 pixman_region32_fini(&clipped_view);
963 }
Marius Vlad80a62e52019-09-11 18:21:59 +0300964
Daniel Stonee404b722019-06-22 18:40:31 +0100965 pixman_region32_fini(&renderer_region);
966 pixman_region32_fini(&occluded_region);
967
968 /* In renderer-only mode, we can't test the state as we don't have a
969 * renderer buffer yet. */
970 if (mode == DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY)
971 return state;
972
Marius Vlad2538aac2019-10-14 11:05:30 +0300973 /* check if we have invalid zpos values, like duplicate(s) */
Marius Vlad48bc5ef2019-11-15 14:32:08 +0200974 drm_output_check_zpos_plane_states(state);
Marius Vlad2538aac2019-10-14 11:05:30 +0300975
Daniel Stonee404b722019-06-22 18:40:31 +0100976 /* Check to see if this state will actually work. */
977 ret = drm_pending_state_test(state->pending_state);
978 if (ret != 0) {
979 drm_debug(b, "\t\t[view] failing state generation: "
980 "atomic test not OK\n");
981 goto err;
982 }
983
984 /* Counterpart to duplicating scanout state at the top of this
985 * function: if we have taken a renderer framebuffer and placed it in
986 * the pending state in order to incrementally test overlay planes,
987 * remove it now. */
988 if (mode == DRM_OUTPUT_PROPOSE_STATE_MIXED) {
989 assert(scanout_state->fb->type == BUFFER_GBM_SURFACE ||
990 scanout_state->fb->type == BUFFER_PIXMAN_DUMB);
991 drm_plane_state_put_back(scanout_state);
992 }
993 return state;
994
995err_region:
996 pixman_region32_fini(&renderer_region);
997 pixman_region32_fini(&occluded_region);
998err:
999 drm_output_state_free(state);
1000 return NULL;
1001}
1002
1003void
1004drm_assign_planes(struct weston_output *output_base, void *repaint_data)
1005{
1006 struct drm_backend *b = to_drm_backend(output_base->compositor);
1007 struct drm_pending_state *pending_state = repaint_data;
1008 struct drm_output *output = to_drm_output(output_base);
1009 struct drm_output_state *state = NULL;
1010 struct drm_plane_state *plane_state;
1011 struct weston_view *ev;
1012 struct weston_plane *primary = &output_base->compositor->primary_plane;
1013 enum drm_output_propose_state_mode mode = DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY;
1014
1015 drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n",
1016 output_base->name, (unsigned long) output_base->id);
1017
1018 if (!b->sprites_are_broken && !output->virtual) {
1019 drm_debug(b, "\t[repaint] trying planes-only build state\n");
1020 state = drm_output_propose_state(output_base, pending_state, mode);
1021 if (!state) {
1022 drm_debug(b, "\t[repaint] could not build planes-only "
1023 "state, trying mixed\n");
1024 mode = DRM_OUTPUT_PROPOSE_STATE_MIXED;
1025 state = drm_output_propose_state(output_base,
1026 pending_state,
1027 mode);
1028 }
1029 if (!state) {
1030 drm_debug(b, "\t[repaint] could not build mixed-mode "
1031 "state, trying renderer-only\n");
1032 }
1033 } else {
1034 drm_debug(b, "\t[state] no overlay plane support\n");
1035 }
1036
1037 if (!state) {
1038 mode = DRM_OUTPUT_PROPOSE_STATE_RENDERER_ONLY;
1039 state = drm_output_propose_state(output_base, pending_state,
1040 mode);
1041 }
1042
1043 assert(state);
1044 drm_debug(b, "\t[repaint] Using %s composition\n",
1045 drm_propose_state_mode_to_string(mode));
1046
1047 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
1048 struct drm_plane *target_plane = NULL;
1049
1050 /* If this view doesn't touch our output at all, there's no
1051 * reason to do anything with it. */
1052 if (!(ev->output_mask & (1u << output->base.id)))
1053 continue;
1054
1055 /* Test whether this buffer can ever go into a plane:
1056 * non-shm, or small enough to be a cursor.
1057 *
1058 * Also, keep a reference when using the pixman renderer.
1059 * That makes it possible to do a seamless switch to the GL
1060 * renderer and since the pixman renderer keeps a reference
1061 * to the buffer anyway, there is no side effects.
1062 */
1063 if (b->use_pixman ||
Marius Vlad5f6bee42019-09-11 16:41:04 +03001064 (weston_view_has_valid_buffer(ev) &&
Daniel Stonee404b722019-06-22 18:40:31 +01001065 (!wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
1066 (ev->surface->width <= b->cursor_width &&
1067 ev->surface->height <= b->cursor_height))))
1068 ev->surface->keep_buffer = true;
1069 else
1070 ev->surface->keep_buffer = false;
1071
1072 /* This is a bit unpleasant, but lacking a temporary place to
1073 * hang a plane off the view, we have to do a nested walk.
1074 * Our first-order iteration has to be planes rather than
1075 * views, because otherwise we won't reset views which were
1076 * previously on planes to being on the primary plane. */
1077 wl_list_for_each(plane_state, &state->plane_list, link) {
1078 if (plane_state->ev == ev) {
1079 plane_state->ev = NULL;
1080 target_plane = plane_state->plane;
1081 break;
1082 }
1083 }
1084
1085 if (target_plane) {
1086 drm_debug(b, "\t[repaint] view %p on %s plane %lu\n",
1087 ev, plane_type_enums[target_plane->type].name,
1088 (unsigned long) target_plane->plane_id);
1089 weston_view_move_to_plane(ev, &target_plane->base);
1090 } else {
1091 drm_debug(b, "\t[repaint] view %p using renderer "
1092 "composition\n", ev);
1093 weston_view_move_to_plane(ev, primary);
1094 }
1095
1096 if (!target_plane ||
1097 target_plane->type == WDRM_PLANE_TYPE_CURSOR) {
1098 /* cursor plane & renderer involve a copy */
1099 ev->psf_flags = 0;
1100 } else {
1101 /* All other planes are a direct scanout of a
1102 * single client buffer.
1103 */
1104 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1105 }
1106 }
1107
1108 /* We rely on output->cursor_view being both an accurate reflection of
1109 * the cursor plane's state, but also being maintained across repaints
1110 * to avoid unnecessary damage uploads, per the comment in
1111 * drm_output_prepare_cursor_view. In the event that we go from having
1112 * a cursor view to not having a cursor view, we need to clear it. */
1113 if (output->cursor_view) {
1114 plane_state =
1115 drm_output_state_get_existing_plane(state,
1116 output->cursor_plane);
1117 if (!plane_state || !plane_state->fb)
1118 output->cursor_view = NULL;
1119 }
1120}