blob: 75a2a18c9b6c0f9012ce9457963ef42cc5819e97 [file] [log] [blame]
Daniel Stone6b466f22019-06-18 11:30:54 +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>
Daniel Stone6b466f22019-06-18 11:30:54 +010034
35#include "drm-internal.h"
Pekka Paalanen4b301fe2021-02-04 17:39:45 +020036#include "shared/weston-drm-fourcc.h"
Daniel Stone6b466f22019-06-18 11:30:54 +010037
38/**
39 * Allocate a new, empty, plane state.
40 */
41struct drm_plane_state *
42drm_plane_state_alloc(struct drm_output_state *state_output,
43 struct drm_plane *plane)
44{
45 struct drm_plane_state *state = zalloc(sizeof(*state));
46
47 assert(state);
48 state->output_state = state_output;
49 state->plane = plane;
50 state->in_fence_fd = -1;
Marius Vladcdd6fa22019-08-29 20:42:00 +030051 state->zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Daniel Stone6b466f22019-06-18 11:30:54 +010052
53 /* Here we only add the plane state to the desired link, and not
54 * set the member. Having an output pointer set means that the
55 * plane will be displayed on the output; this won't be the case
56 * when we go to disable a plane. In this case, it must be part of
57 * the commit (and thus the output state), but the member must be
58 * NULL, as it will not be on any output when the state takes
59 * effect.
60 */
61 if (state_output)
62 wl_list_insert(&state_output->plane_list, &state->link);
63 else
64 wl_list_init(&state->link);
65
66 return state;
67}
68
69/**
70 * Free an existing plane state. As a special case, the state will not
71 * normally be freed if it is the current state; see drm_plane_set_state.
72 */
73void
74drm_plane_state_free(struct drm_plane_state *state, bool force)
75{
76 if (!state)
77 return;
78
79 wl_list_remove(&state->link);
80 wl_list_init(&state->link);
81 state->output_state = NULL;
82 state->in_fence_fd = -1;
Marius Vladcdd6fa22019-08-29 20:42:00 +030083 state->zpos = DRM_PLANE_ZPOS_INVALID_PLANE;
Scott Anderson15c603c2020-06-02 17:39:43 +120084
85 /* Once the damage blob has been submitted, it is refcounted internally
86 * by the kernel, which means we can safely discard it.
87 */
88 if (state->damage_blob_id != 0) {
89 drmModeDestroyPropertyBlob(state->plane->backend->drm.fd,
90 state->damage_blob_id);
91 state->damage_blob_id = 0;
92 }
Daniel Stone6b466f22019-06-18 11:30:54 +010093
94 if (force || state != state->plane->state_cur) {
95 drm_fb_unref(state->fb);
Daniel Stone2ecc38b2021-11-18 15:33:17 +000096 weston_buffer_reference(&state->fb_ref.buffer, NULL);
97 weston_buffer_release_reference(&state->fb_ref.release, NULL);
Daniel Stone6b466f22019-06-18 11:30:54 +010098 free(state);
99 }
100}
101
102/**
103 * Duplicate an existing plane state into a new plane state, storing it within
104 * the given output state. If the output state already contains a plane state
105 * for the drm_plane referenced by 'src', that plane state is freed first.
106 */
107struct drm_plane_state *
108drm_plane_state_duplicate(struct drm_output_state *state_output,
109 struct drm_plane_state *src)
110{
Scott Anderson15c603c2020-06-02 17:39:43 +1200111 struct drm_plane_state *dst = zalloc(sizeof(*dst));
Daniel Stone6b466f22019-06-18 11:30:54 +0100112 struct drm_plane_state *old, *tmp;
113
114 assert(src);
115 assert(dst);
116 *dst = *src;
Scott Anderson15c603c2020-06-02 17:39:43 +1200117 /* We don't want to copy this, because damage is transient, and only
118 * lasts for the duration of a single repaint.
119 */
120 dst->damage_blob_id = 0;
Daniel Stone6b466f22019-06-18 11:30:54 +0100121 wl_list_init(&dst->link);
122
123 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
124 /* Duplicating a plane state into the same output state, so
125 * it can replace itself with an identical copy of itself,
126 * makes no sense. */
127 assert(old != src);
128 if (old->plane == dst->plane)
129 drm_plane_state_free(old, false);
130 }
131
132 wl_list_insert(&state_output->plane_list, &dst->link);
Daniel Stone2ecc38b2021-11-18 15:33:17 +0000133
134 /* Take a reference on the src framebuffer; if it wraps a client
135 * buffer, then we must also transfer the reference on the client
136 * buffer. */
137 if (src->fb) {
Daniel Stone6b466f22019-06-18 11:30:54 +0100138 dst->fb = drm_fb_ref(src->fb);
Daniel Stone2ecc38b2021-11-18 15:33:17 +0000139 memset(&dst->fb_ref, 0, sizeof(dst->fb_ref));
140 weston_buffer_reference(&dst->fb_ref.buffer,
141 src->fb_ref.buffer.buffer);
142 weston_buffer_release_reference(&dst->fb_ref.release,
143 src->fb_ref.release.buffer_release);
144 } else {
145 assert(!src->fb_ref.buffer.buffer);
146 assert(!src->fb_ref.release.buffer_release);
147 }
Daniel Stone6b466f22019-06-18 11:30:54 +0100148 dst->output_state = state_output;
Daniel Stone6b466f22019-06-18 11:30:54 +0100149 dst->complete = false;
150
151 return dst;
152}
153
154/**
155 * Remove a plane state from an output state; if the plane was previously
156 * enabled, then replace it with a disabling state. This ensures that the
157 * output state was untouched from it was before the plane state was
158 * modified by the caller of this function.
159 *
160 * This is required as drm_output_state_get_plane may either allocate a
161 * new plane state, in which case this function will just perform a matching
162 * drm_plane_state_free, or it may instead repurpose an existing disabling
163 * state (if the plane was previously active), in which case this function
164 * will reset it.
165 */
166void
167drm_plane_state_put_back(struct drm_plane_state *state)
168{
169 struct drm_output_state *state_output;
170 struct drm_plane *plane;
171
172 if (!state)
173 return;
174
175 state_output = state->output_state;
176 plane = state->plane;
177 drm_plane_state_free(state, false);
178
179 /* Plane was previously disabled; no need to keep this temporary
180 * state around. */
181 if (!plane->state_cur->fb)
182 return;
183
184 (void) drm_plane_state_alloc(state_output, plane);
185}
186
187/**
188 * Given a weston_view, fill the drm_plane_state's co-ordinates to display on
189 * a given plane.
190 */
191bool
192drm_plane_state_coords_for_view(struct drm_plane_state *state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300193 struct weston_view *ev, uint64_t zpos)
Daniel Stone6b466f22019-06-18 11:30:54 +0100194{
195 struct drm_output *output = state->output;
196 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
197 pixman_region32_t dest_rect, src_rect;
198 pixman_box32_t *box, tbox;
199 float sxf1, syf1, sxf2, syf2;
200
201 if (!drm_view_transform_supported(ev, &output->base))
202 return false;
203
204 /* Update the base weston_plane co-ordinates. */
205 box = pixman_region32_extents(&ev->transform.boundingbox);
206 state->plane->base.x = box->x1;
207 state->plane->base.y = box->y1;
208
209 /* First calculate the destination co-ordinates by taking the
210 * area of the view which is visible on this output, performing any
211 * transforms to account for output rotation and scale as necessary. */
212 pixman_region32_init(&dest_rect);
213 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
214 &output->base.region);
215 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
216 box = pixman_region32_extents(&dest_rect);
217 tbox = weston_transformed_rect(output->base.width,
218 output->base.height,
219 output->base.transform,
220 output->base.current_scale,
221 *box);
222 state->dest_x = tbox.x1;
223 state->dest_y = tbox.y1;
224 state->dest_w = tbox.x2 - tbox.x1;
225 state->dest_h = tbox.y2 - tbox.y1;
226 pixman_region32_fini(&dest_rect);
227
228 /* Now calculate the source rectangle, by finding the extents of the
229 * view, and working backwards to source co-ordinates. */
230 pixman_region32_init(&src_rect);
231 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
232 &output->base.region);
233 box = pixman_region32_extents(&src_rect);
234 weston_view_from_global_float(ev, box->x1, box->y1, &sxf1, &syf1);
235 weston_surface_to_buffer_float(ev->surface, sxf1, syf1, &sxf1, &syf1);
236 weston_view_from_global_float(ev, box->x2, box->y2, &sxf2, &syf2);
237 weston_surface_to_buffer_float(ev->surface, sxf2, syf2, &sxf2, &syf2);
238 pixman_region32_fini(&src_rect);
239
240 /* Buffer transforms may mean that x2 is to the left of x1, and/or that
241 * y2 is above y1. */
242 if (sxf2 < sxf1) {
243 double tmp = sxf1;
244 sxf1 = sxf2;
245 sxf2 = tmp;
246 }
247 if (syf2 < syf1) {
248 double tmp = syf1;
249 syf1 = syf2;
250 syf2 = tmp;
251 }
252
253 /* Shift from S23.8 wl_fixed to U16.16 KMS fixed-point encoding. */
254 state->src_x = wl_fixed_from_double(sxf1) << 8;
255 state->src_y = wl_fixed_from_double(syf1) << 8;
256 state->src_w = wl_fixed_from_double(sxf2 - sxf1) << 8;
257 state->src_h = wl_fixed_from_double(syf2 - syf1) << 8;
258
259 /* Clamp our source co-ordinates to surface bounds; it's possible
260 * for intermediate translations to give us slightly incorrect
261 * co-ordinates if we have, for example, multiple zooming
262 * transformations. View bounding boxes are also explicitly rounded
263 * greedily. */
264 if (state->src_x < 0)
265 state->src_x = 0;
266 if (state->src_y < 0)
267 state->src_y = 0;
268 if (state->src_w > (uint32_t) ((buffer->width << 16) - state->src_x))
269 state->src_w = (buffer->width << 16) - state->src_x;
270 if (state->src_h > (uint32_t) ((buffer->height << 16) - state->src_y))
271 state->src_h = (buffer->height << 16) - state->src_y;
272
Marius Vlad2538aac2019-10-14 11:05:30 +0300273 /* apply zpos if available */
274 state->zpos = zpos;
275
Daniel Stone6b466f22019-06-18 11:30:54 +0100276 return true;
277}
278
279/**
Alexandros Frantzis99751342020-05-18 15:22:49 +0300280 * Reset the current state of a DRM plane
281 *
282 * The current state will be freed and replaced by a pristine state.
283 *
284 * @param plane The plane to reset the current state of
285 */
286void
287drm_plane_reset_state(struct drm_plane *plane)
288{
289 drm_plane_state_free(plane->state_cur, true);
290 plane->state_cur = drm_plane_state_alloc(NULL, plane);
291 plane->state_cur->complete = true;
292}
293
294/**
Daniel Stone6b466f22019-06-18 11:30:54 +0100295 * Return a plane state from a drm_output_state.
296 */
297struct drm_plane_state *
298drm_output_state_get_existing_plane(struct drm_output_state *state_output,
299 struct drm_plane *plane)
300{
301 struct drm_plane_state *ps;
302
303 wl_list_for_each(ps, &state_output->plane_list, link) {
304 if (ps->plane == plane)
305 return ps;
306 }
307
308 return NULL;
309}
310
311/**
312 * Return a plane state from a drm_output_state, either existing or
313 * freshly allocated.
314 */
315struct drm_plane_state *
316drm_output_state_get_plane(struct drm_output_state *state_output,
317 struct drm_plane *plane)
318{
319 struct drm_plane_state *ps;
320
321 ps = drm_output_state_get_existing_plane(state_output, plane);
322 if (ps)
323 return ps;
324
325 return drm_plane_state_alloc(state_output, plane);
326}
327
328/**
329 * Allocate a new, empty drm_output_state. This should not generally be used
330 * in the repaint cycle; see drm_output_state_duplicate.
331 */
332struct drm_output_state *
333drm_output_state_alloc(struct drm_output *output,
334 struct drm_pending_state *pending_state)
335{
336 struct drm_output_state *state = zalloc(sizeof(*state));
337
338 assert(state);
339 state->output = output;
340 state->dpms = WESTON_DPMS_OFF;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530341 state->protection = WESTON_HDCP_DISABLE;
Daniel Stone6b466f22019-06-18 11:30:54 +0100342 state->pending_state = pending_state;
343 if (pending_state)
344 wl_list_insert(&pending_state->output_list, &state->link);
345 else
346 wl_list_init(&state->link);
347
348 wl_list_init(&state->plane_list);
349
350 return state;
351}
352
353/**
354 * Duplicate an existing drm_output_state into a new one. This is generally
355 * used during the repaint cycle, to capture the existing state of an output
356 * and modify it to create a new state to be used.
357 *
358 * The mode determines whether the output will be reset to an a blank state,
359 * or an exact mirror of the current state.
360 */
361struct drm_output_state *
362drm_output_state_duplicate(struct drm_output_state *src,
363 struct drm_pending_state *pending_state,
364 enum drm_output_state_duplicate_mode plane_mode)
365{
366 struct drm_output_state *dst = malloc(sizeof(*dst));
367 struct drm_plane_state *ps;
368
369 assert(dst);
370
371 /* Copy the whole structure, then individually modify the
372 * pending_state, as well as the list link into our pending
373 * state. */
374 *dst = *src;
375
376 dst->pending_state = pending_state;
377 if (pending_state)
378 wl_list_insert(&pending_state->output_list, &dst->link);
379 else
380 wl_list_init(&dst->link);
381
382 wl_list_init(&dst->plane_list);
383
384 wl_list_for_each(ps, &src->plane_list, link) {
385 /* Don't carry planes which are now disabled; these should be
386 * free for other outputs to reuse. */
387 if (!ps->output)
388 continue;
389
390 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
391 (void) drm_plane_state_alloc(dst, ps->plane);
392 else
393 (void) drm_plane_state_duplicate(dst, ps);
394 }
395
396 return dst;
397}
398
399/**
400 * Free an unused drm_output_state.
401 */
402void
403drm_output_state_free(struct drm_output_state *state)
404{
405 struct drm_plane_state *ps, *next;
406
407 if (!state)
408 return;
409
410 wl_list_for_each_safe(ps, next, &state->plane_list, link)
411 drm_plane_state_free(ps, false);
412
413 wl_list_remove(&state->link);
414
415 free(state);
416}
417
418/**
419 * Allocate a new drm_pending_state
420 *
421 * Allocate a new, empty, 'pending state' structure to be used across a
422 * repaint cycle or similar.
423 *
424 * @param backend DRM backend
425 * @returns Newly-allocated pending state structure
426 */
427struct drm_pending_state *
428drm_pending_state_alloc(struct drm_backend *backend)
429{
430 struct drm_pending_state *ret;
431
432 ret = calloc(1, sizeof(*ret));
433 if (!ret)
434 return NULL;
435
436 ret->backend = backend;
437 wl_list_init(&ret->output_list);
438
439 return ret;
440}
441
442/**
443 * Free a drm_pending_state structure
444 *
445 * Frees a pending_state structure, as well as any output_states connected
446 * to this pending state.
447 *
448 * @param pending_state Pending state structure to free
449 */
450void
451drm_pending_state_free(struct drm_pending_state *pending_state)
452{
453 struct drm_output_state *output_state, *tmp;
454
455 if (!pending_state)
456 return;
457
458 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
459 link) {
460 drm_output_state_free(output_state);
461 }
462
463 free(pending_state);
464}
465
466/**
467 * Find an output state in a pending state
468 *
469 * Given a pending_state structure, find the output_state for a particular
470 * output.
471 *
472 * @param pending_state Pending state structure to search
473 * @param output Output to find state for
474 * @returns Output state if present, or NULL if not
475 */
476struct drm_output_state *
477drm_pending_state_get_output(struct drm_pending_state *pending_state,
478 struct drm_output *output)
479{
480 struct drm_output_state *output_state;
481
482 wl_list_for_each(output_state, &pending_state->output_list, link) {
483 if (output_state->output == output)
484 return output_state;
485 }
486
487 return NULL;
488}