blob: 8fc2e03aab02e74c66165a1adc3b4df83b66475b [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);
96 free(state);
97 }
98}
99
100/**
101 * Duplicate an existing plane state into a new plane state, storing it within
102 * the given output state. If the output state already contains a plane state
103 * for the drm_plane referenced by 'src', that plane state is freed first.
104 */
105struct drm_plane_state *
106drm_plane_state_duplicate(struct drm_output_state *state_output,
107 struct drm_plane_state *src)
108{
Scott Anderson15c603c2020-06-02 17:39:43 +1200109 struct drm_plane_state *dst = zalloc(sizeof(*dst));
Daniel Stone6b466f22019-06-18 11:30:54 +0100110 struct drm_plane_state *old, *tmp;
111
112 assert(src);
113 assert(dst);
114 *dst = *src;
Scott Anderson15c603c2020-06-02 17:39:43 +1200115 /* We don't want to copy this, because damage is transient, and only
116 * lasts for the duration of a single repaint.
117 */
118 dst->damage_blob_id = 0;
Daniel Stone6b466f22019-06-18 11:30:54 +0100119 wl_list_init(&dst->link);
120
121 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
122 /* Duplicating a plane state into the same output state, so
123 * it can replace itself with an identical copy of itself,
124 * makes no sense. */
125 assert(old != src);
126 if (old->plane == dst->plane)
127 drm_plane_state_free(old, false);
128 }
129
130 wl_list_insert(&state_output->plane_list, &dst->link);
131 if (src->fb)
132 dst->fb = drm_fb_ref(src->fb);
133 dst->output_state = state_output;
Daniel Stone6b466f22019-06-18 11:30:54 +0100134 dst->complete = false;
135
136 return dst;
137}
138
139/**
140 * Remove a plane state from an output state; if the plane was previously
141 * enabled, then replace it with a disabling state. This ensures that the
142 * output state was untouched from it was before the plane state was
143 * modified by the caller of this function.
144 *
145 * This is required as drm_output_state_get_plane may either allocate a
146 * new plane state, in which case this function will just perform a matching
147 * drm_plane_state_free, or it may instead repurpose an existing disabling
148 * state (if the plane was previously active), in which case this function
149 * will reset it.
150 */
151void
152drm_plane_state_put_back(struct drm_plane_state *state)
153{
154 struct drm_output_state *state_output;
155 struct drm_plane *plane;
156
157 if (!state)
158 return;
159
160 state_output = state->output_state;
161 plane = state->plane;
162 drm_plane_state_free(state, false);
163
164 /* Plane was previously disabled; no need to keep this temporary
165 * state around. */
166 if (!plane->state_cur->fb)
167 return;
168
169 (void) drm_plane_state_alloc(state_output, plane);
170}
171
172/**
173 * Given a weston_view, fill the drm_plane_state's co-ordinates to display on
174 * a given plane.
175 */
176bool
177drm_plane_state_coords_for_view(struct drm_plane_state *state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300178 struct weston_view *ev, uint64_t zpos)
Daniel Stone6b466f22019-06-18 11:30:54 +0100179{
180 struct drm_output *output = state->output;
181 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
182 pixman_region32_t dest_rect, src_rect;
183 pixman_box32_t *box, tbox;
184 float sxf1, syf1, sxf2, syf2;
185
186 if (!drm_view_transform_supported(ev, &output->base))
187 return false;
188
189 /* Update the base weston_plane co-ordinates. */
190 box = pixman_region32_extents(&ev->transform.boundingbox);
191 state->plane->base.x = box->x1;
192 state->plane->base.y = box->y1;
193
194 /* First calculate the destination co-ordinates by taking the
195 * area of the view which is visible on this output, performing any
196 * transforms to account for output rotation and scale as necessary. */
197 pixman_region32_init(&dest_rect);
198 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
199 &output->base.region);
200 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
201 box = pixman_region32_extents(&dest_rect);
202 tbox = weston_transformed_rect(output->base.width,
203 output->base.height,
204 output->base.transform,
205 output->base.current_scale,
206 *box);
207 state->dest_x = tbox.x1;
208 state->dest_y = tbox.y1;
209 state->dest_w = tbox.x2 - tbox.x1;
210 state->dest_h = tbox.y2 - tbox.y1;
211 pixman_region32_fini(&dest_rect);
212
213 /* Now calculate the source rectangle, by finding the extents of the
214 * view, and working backwards to source co-ordinates. */
215 pixman_region32_init(&src_rect);
216 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
217 &output->base.region);
218 box = pixman_region32_extents(&src_rect);
219 weston_view_from_global_float(ev, box->x1, box->y1, &sxf1, &syf1);
220 weston_surface_to_buffer_float(ev->surface, sxf1, syf1, &sxf1, &syf1);
221 weston_view_from_global_float(ev, box->x2, box->y2, &sxf2, &syf2);
222 weston_surface_to_buffer_float(ev->surface, sxf2, syf2, &sxf2, &syf2);
223 pixman_region32_fini(&src_rect);
224
225 /* Buffer transforms may mean that x2 is to the left of x1, and/or that
226 * y2 is above y1. */
227 if (sxf2 < sxf1) {
228 double tmp = sxf1;
229 sxf1 = sxf2;
230 sxf2 = tmp;
231 }
232 if (syf2 < syf1) {
233 double tmp = syf1;
234 syf1 = syf2;
235 syf2 = tmp;
236 }
237
238 /* Shift from S23.8 wl_fixed to U16.16 KMS fixed-point encoding. */
239 state->src_x = wl_fixed_from_double(sxf1) << 8;
240 state->src_y = wl_fixed_from_double(syf1) << 8;
241 state->src_w = wl_fixed_from_double(sxf2 - sxf1) << 8;
242 state->src_h = wl_fixed_from_double(syf2 - syf1) << 8;
243
244 /* Clamp our source co-ordinates to surface bounds; it's possible
245 * for intermediate translations to give us slightly incorrect
246 * co-ordinates if we have, for example, multiple zooming
247 * transformations. View bounding boxes are also explicitly rounded
248 * greedily. */
249 if (state->src_x < 0)
250 state->src_x = 0;
251 if (state->src_y < 0)
252 state->src_y = 0;
253 if (state->src_w > (uint32_t) ((buffer->width << 16) - state->src_x))
254 state->src_w = (buffer->width << 16) - state->src_x;
255 if (state->src_h > (uint32_t) ((buffer->height << 16) - state->src_y))
256 state->src_h = (buffer->height << 16) - state->src_y;
257
Marius Vlad2538aac2019-10-14 11:05:30 +0300258 /* apply zpos if available */
259 state->zpos = zpos;
260
Daniel Stone6b466f22019-06-18 11:30:54 +0100261 return true;
262}
263
264/**
Alexandros Frantzis99751342020-05-18 15:22:49 +0300265 * Reset the current state of a DRM plane
266 *
267 * The current state will be freed and replaced by a pristine state.
268 *
269 * @param plane The plane to reset the current state of
270 */
271void
272drm_plane_reset_state(struct drm_plane *plane)
273{
274 drm_plane_state_free(plane->state_cur, true);
275 plane->state_cur = drm_plane_state_alloc(NULL, plane);
276 plane->state_cur->complete = true;
277}
278
279/**
Daniel Stone6b466f22019-06-18 11:30:54 +0100280 * Return a plane state from a drm_output_state.
281 */
282struct drm_plane_state *
283drm_output_state_get_existing_plane(struct drm_output_state *state_output,
284 struct drm_plane *plane)
285{
286 struct drm_plane_state *ps;
287
288 wl_list_for_each(ps, &state_output->plane_list, link) {
289 if (ps->plane == plane)
290 return ps;
291 }
292
293 return NULL;
294}
295
296/**
297 * Return a plane state from a drm_output_state, either existing or
298 * freshly allocated.
299 */
300struct drm_plane_state *
301drm_output_state_get_plane(struct drm_output_state *state_output,
302 struct drm_plane *plane)
303{
304 struct drm_plane_state *ps;
305
306 ps = drm_output_state_get_existing_plane(state_output, plane);
307 if (ps)
308 return ps;
309
310 return drm_plane_state_alloc(state_output, plane);
311}
312
313/**
314 * Allocate a new, empty drm_output_state. This should not generally be used
315 * in the repaint cycle; see drm_output_state_duplicate.
316 */
317struct drm_output_state *
318drm_output_state_alloc(struct drm_output *output,
319 struct drm_pending_state *pending_state)
320{
321 struct drm_output_state *state = zalloc(sizeof(*state));
322
323 assert(state);
324 state->output = output;
325 state->dpms = WESTON_DPMS_OFF;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530326 state->protection = WESTON_HDCP_DISABLE;
Daniel Stone6b466f22019-06-18 11:30:54 +0100327 state->pending_state = pending_state;
328 if (pending_state)
329 wl_list_insert(&pending_state->output_list, &state->link);
330 else
331 wl_list_init(&state->link);
332
333 wl_list_init(&state->plane_list);
334
335 return state;
336}
337
338/**
339 * Duplicate an existing drm_output_state into a new one. This is generally
340 * used during the repaint cycle, to capture the existing state of an output
341 * and modify it to create a new state to be used.
342 *
343 * The mode determines whether the output will be reset to an a blank state,
344 * or an exact mirror of the current state.
345 */
346struct drm_output_state *
347drm_output_state_duplicate(struct drm_output_state *src,
348 struct drm_pending_state *pending_state,
349 enum drm_output_state_duplicate_mode plane_mode)
350{
351 struct drm_output_state *dst = malloc(sizeof(*dst));
352 struct drm_plane_state *ps;
353
354 assert(dst);
355
356 /* Copy the whole structure, then individually modify the
357 * pending_state, as well as the list link into our pending
358 * state. */
359 *dst = *src;
360
361 dst->pending_state = pending_state;
362 if (pending_state)
363 wl_list_insert(&pending_state->output_list, &dst->link);
364 else
365 wl_list_init(&dst->link);
366
367 wl_list_init(&dst->plane_list);
368
369 wl_list_for_each(ps, &src->plane_list, link) {
370 /* Don't carry planes which are now disabled; these should be
371 * free for other outputs to reuse. */
372 if (!ps->output)
373 continue;
374
375 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
376 (void) drm_plane_state_alloc(dst, ps->plane);
377 else
378 (void) drm_plane_state_duplicate(dst, ps);
379 }
380
381 return dst;
382}
383
384/**
385 * Free an unused drm_output_state.
386 */
387void
388drm_output_state_free(struct drm_output_state *state)
389{
390 struct drm_plane_state *ps, *next;
391
392 if (!state)
393 return;
394
395 wl_list_for_each_safe(ps, next, &state->plane_list, link)
396 drm_plane_state_free(ps, false);
397
398 wl_list_remove(&state->link);
399
400 free(state);
401}
402
403/**
404 * Allocate a new drm_pending_state
405 *
406 * Allocate a new, empty, 'pending state' structure to be used across a
407 * repaint cycle or similar.
408 *
409 * @param backend DRM backend
410 * @returns Newly-allocated pending state structure
411 */
412struct drm_pending_state *
413drm_pending_state_alloc(struct drm_backend *backend)
414{
415 struct drm_pending_state *ret;
416
417 ret = calloc(1, sizeof(*ret));
418 if (!ret)
419 return NULL;
420
421 ret->backend = backend;
422 wl_list_init(&ret->output_list);
423
424 return ret;
425}
426
427/**
428 * Free a drm_pending_state structure
429 *
430 * Frees a pending_state structure, as well as any output_states connected
431 * to this pending state.
432 *
433 * @param pending_state Pending state structure to free
434 */
435void
436drm_pending_state_free(struct drm_pending_state *pending_state)
437{
438 struct drm_output_state *output_state, *tmp;
439
440 if (!pending_state)
441 return;
442
443 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
444 link) {
445 drm_output_state_free(output_state);
446 }
447
448 free(pending_state);
449}
450
451/**
452 * Find an output state in a pending state
453 *
454 * Given a pending_state structure, find the output_state for a particular
455 * output.
456 *
457 * @param pending_state Pending state structure to search
458 * @param output Output to find state for
459 * @returns Output state if present, or NULL if not
460 */
461struct drm_output_state *
462drm_pending_state_get_output(struct drm_pending_state *pending_state,
463 struct drm_output *output)
464{
465 struct drm_output_state *output_state;
466
467 wl_list_for_each(output_state, &pending_state->output_list, link) {
468 if (output_state->output == output)
469 return output_state;
470 }
471
472 return NULL;
473}