blob: 4c62dff14893646f7e818d1fb87947da52a3ccef [file] [log] [blame]
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02001/*
2 * Copyright (C) 2016 Samsung Electronics Co.Ltd
3 * Authors:
4 * Marek Szyprowski <m.szyprowski@samsung.com>
5 *
6 * DRM core plane blending related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26#include <drm/drmP.h>
27#include <drm/drm_atomic.h>
Daniel Vetter18733802016-09-21 10:59:26 +020028#include <drm/drm_blend.h>
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020029#include <linux/export.h>
30#include <linux/slab.h>
31#include <linux/sort.h>
32
Ville Syrjäläc30b4402016-09-19 16:33:43 +030033#include "drm_crtc_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020034
Daniel Vetter1e4d84c2016-09-21 10:59:27 +020035/**
36 * DOC: overview
37 *
38 * The basic plane composition model supported by standard plane properties only
39 * has a source rectangle (in logical pixels within the &drm_framebuffer), with
40 * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
41 * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
42 * defined by the horizontal and vertical visible pixels (stored in @hdisplay
Daniel Vetterd5745282017-01-25 07:26:45 +010043 * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
44 * two rectangles are both stored in the &drm_plane_state.
Daniel Vetter1e4d84c2016-09-21 10:59:27 +020045 *
46 * For the atomic ioctl the following standard (atomic) properties on the plane object
47 * encode the basic plane composition model:
48 *
49 * SRC_X:
50 * X coordinate offset for the source rectangle within the
51 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
52 * SRC_Y:
53 * Y coordinate offset for the source rectangle within the
54 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
55 * SRC_W:
56 * Width for the source rectangle within the &drm_framebuffer, in 16.16
57 * fixed point. SRC_X plus SRC_W must be within the width of the source
58 * framebuffer. Must be positive.
59 * SRC_H:
60 * Height for the source rectangle within the &drm_framebuffer, in 16.16
61 * fixed point. SRC_Y plus SRC_H must be within the height of the source
62 * framebuffer. Must be positive.
63 * CRTC_X:
64 * X coordinate offset for the destination rectangle. Can be negative.
65 * CRTC_Y:
66 * Y coordinate offset for the destination rectangle. Can be negative.
67 * CRTC_W:
68 * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
69 * the currently visible horizontal area of the &drm_crtc.
70 * CRTC_H:
71 * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
72 * the currently visible vertical area of the &drm_crtc.
73 * FB_ID:
74 * Mode object ID of the &drm_framebuffer this plane should scan out.
75 * CRTC_ID:
76 * Mode object ID of the &drm_crtc this plane should be connected to.
77 *
78 * Note that the source rectangle must fully lie within the bounds of the
79 * &drm_framebuffer. The destination rectangle can lie outside of the visible
80 * area of the current mode of the CRTC. It must be apprpriately clipped by the
81 * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
82 * are also allowed to round the subpixel sampling positions appropriately, but
83 * only to the next full pixel. No pixel outside of the source rectangle may
84 * ever be sampled, which is important when applying more sophisticated
85 * filtering than just a bilinear one when scaling. The filtering mode when
86 * scaling is unspecified.
87 *
88 * On top of this basic transformation additional properties can be exposed by
89 * the driver:
90 *
Ville Syrjälä6686df82016-10-21 22:22:45 +030091 * - Rotation is set up with drm_plane_create_rotation_property(). It adds a
Daniel Vetter1e4d84c2016-09-21 10:59:27 +020092 * rotation and reflection step between the source and destination rectangles.
93 * Without this property the rectangle is only scaled, but not rotated or
94 * reflected.
95 *
96 * - Z position is set up with drm_plane_create_zpos_immutable_property() and
97 * drm_plane_create_zpos_property(). It controls the visibility of overlapping
98 * planes. Without this property the primary plane is always below the cursor
99 * plane, and ordering between all other planes is undefined.
100 *
101 * Note that all the property extensions described here apply either to the
102 * plane or the CRTC (e.g. for the background color, which currently is not
103 * exposed and assumed to be black).
104 */
105
106/**
Ville Syrjälä6686df82016-10-21 22:22:45 +0300107 * drm_plane_create_rotation_property - create a new rotation property
108 * @plane: drm plane
109 * @rotation: initial value of the rotation property
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200110 * @supported_rotations: bitmask of supported rotations and reflections
111 *
112 * This creates a new property with the selected support for transformations.
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200113 *
114 * Since a rotation by 180° degress is the same as reflecting both along the x
115 * and the y axis the rotation property is somewhat redundant. Drivers can use
116 * drm_rotation_simplify() to normalize values of this property.
117 *
118 * The property exposed to userspace is a bitmask property (see
119 * drm_property_create_bitmask()) called "rotation" and has the following
120 * bitmask enumaration values:
121 *
Robert Fossc2c446a2017-05-19 16:50:17 -0400122 * DRM_MODE_ROTATE_0:
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200123 * "rotate-0"
Robert Fossc2c446a2017-05-19 16:50:17 -0400124 * DRM_MODE_ROTATE_90:
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200125 * "rotate-90"
Robert Fossc2c446a2017-05-19 16:50:17 -0400126 * DRM_MODE_ROTATE_180:
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200127 * "rotate-180"
Robert Fossc2c446a2017-05-19 16:50:17 -0400128 * DRM_MODE_ROTATE_270:
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200129 * "rotate-270"
Robert Fossc2c446a2017-05-19 16:50:17 -0400130 * DRM_MODE_REFLECT_X:
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200131 * "reflect-x"
Robert Fossc2c446a2017-05-19 16:50:17 -0400132 * DRM_MODE_REFLECT_Y:
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200133 * "reflect-y"
134 *
135 * Rotation is the specified amount in degrees in counter clockwise direction,
136 * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
137 * rotation. After reflection, the rotation is applied to the image sampled from
138 * the source rectangle, before scaling it to fit the destination rectangle.
139 */
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300140int drm_plane_create_rotation_property(struct drm_plane *plane,
141 unsigned int rotation,
142 unsigned int supported_rotations)
143{
144 static const struct drm_prop_enum_list props[] = {
Robert Fossc2c446a2017-05-19 16:50:17 -0400145 { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
146 { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
147 { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
148 { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
149 { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
150 { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300151 };
152 struct drm_property *prop;
153
Robert Fossc2c446a2017-05-19 16:50:17 -0400154 WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
155 WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300156 WARN_ON(rotation & ~supported_rotations);
157
158 prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
159 props, ARRAY_SIZE(props),
160 supported_rotations);
161 if (!prop)
162 return -ENOMEM;
163
164 drm_object_attach_property(&plane->base, prop, rotation);
165
166 if (plane->state)
167 plane->state->rotation = rotation;
168
169 plane->rotation_property = prop;
170
171 return 0;
172}
173EXPORT_SYMBOL(drm_plane_create_rotation_property);
174
Daniel Vetter18733802016-09-21 10:59:26 +0200175/**
176 * drm_rotation_simplify() - Try to simplify the rotation
177 * @rotation: Rotation to be simplified
178 * @supported_rotations: Supported rotations
179 *
180 * Attempt to simplify the rotation to a form that is supported.
Robert Fossc2c446a2017-05-19 16:50:17 -0400181 * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
Daniel Vetter18733802016-09-21 10:59:26 +0200182 * one could call this function like this:
183 *
Robert Fossc2c446a2017-05-19 16:50:17 -0400184 * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
185 * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
186 * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
Daniel Vetter18733802016-09-21 10:59:26 +0200187 *
Robert Fossc2c446a2017-05-19 16:50:17 -0400188 * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
Daniel Vetter18733802016-09-21 10:59:26 +0200189 * transforms the hardware supports, this function may not
190 * be able to produce a supported transform, so the caller should
191 * check the result afterwards.
192 */
193unsigned int drm_rotation_simplify(unsigned int rotation,
194 unsigned int supported_rotations)
195{
196 if (rotation & ~supported_rotations) {
Robert Fossc2c446a2017-05-19 16:50:17 -0400197 rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
198 rotation = (rotation & DRM_MODE_REFLECT_MASK) |
199 BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
200 % 4);
Daniel Vetter18733802016-09-21 10:59:26 +0200201 }
202
203 return rotation;
204}
205EXPORT_SYMBOL(drm_rotation_simplify);
206
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200207/**
208 * drm_plane_create_zpos_property - create mutable zpos property
209 * @plane: drm plane
210 * @zpos: initial value of zpos property
211 * @min: minimal possible value of zpos property
212 * @max: maximal possible value of zpos property
213 *
214 * This function initializes generic mutable zpos property and enables support
215 * for it in drm core. Drivers can then attach this property to planes to enable
216 * support for configurable planes arrangement during blending operation.
Thierry Redingca40cfc2017-11-20 16:09:59 +0100217 * Drivers that attach a mutable zpos property to any plane should call the
218 * drm_atomic_normalize_zpos() helper during their implementation of
219 * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
220 * values and store them in &drm_plane_state.normalized_zpos. Usually min
221 * should be set to 0 and max to maximal number of planes for given crtc - 1.
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200222 *
223 * If zpos of some planes cannot be changed (like fixed background or
224 * cursor/topmost planes), driver should adjust min/max values and assign those
225 * planes immutable zpos property with lower or higher values (for more
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200226 * information, see drm_plane_create_zpos_immutable_property() function). In such
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200227 * case driver should also assign proper initial zpos values for all planes in
228 * its plane_reset() callback, so the planes will be always sorted properly.
229 *
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200230 * See also drm_atomic_normalize_zpos().
231 *
232 * The property exposed to userspace is called "zpos".
233 *
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200234 * Returns:
235 * Zero on success, negative errno on failure.
236 */
237int drm_plane_create_zpos_property(struct drm_plane *plane,
238 unsigned int zpos,
239 unsigned int min, unsigned int max)
240{
241 struct drm_property *prop;
242
243 prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
244 if (!prop)
245 return -ENOMEM;
246
247 drm_object_attach_property(&plane->base, prop, zpos);
248
249 plane->zpos_property = prop;
250
251 if (plane->state) {
252 plane->state->zpos = zpos;
253 plane->state->normalized_zpos = zpos;
254 }
255
256 return 0;
257}
258EXPORT_SYMBOL(drm_plane_create_zpos_property);
259
260/**
261 * drm_plane_create_zpos_immutable_property - create immuttable zpos property
262 * @plane: drm plane
263 * @zpos: value of zpos property
264 *
265 * This function initializes generic immutable zpos property and enables
266 * support for it in drm core. Using this property driver lets userspace
267 * to get the arrangement of the planes for blending operation and notifies
268 * it that the hardware (or driver) doesn't support changing of the planes'
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200269 * order. For mutable zpos see drm_plane_create_zpos_property().
270 *
271 * The property exposed to userspace is called "zpos".
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200272 *
273 * Returns:
274 * Zero on success, negative errno on failure.
275 */
276int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
277 unsigned int zpos)
278{
279 struct drm_property *prop;
280
281 prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
282 "zpos", zpos, zpos);
283 if (!prop)
284 return -ENOMEM;
285
286 drm_object_attach_property(&plane->base, prop, zpos);
287
288 plane->zpos_property = prop;
289
290 if (plane->state) {
291 plane->state->zpos = zpos;
292 plane->state->normalized_zpos = zpos;
293 }
294
295 return 0;
296}
297EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
298
299static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
300{
301 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
302 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
303
304 if (sa->zpos != sb->zpos)
305 return sa->zpos - sb->zpos;
306 else
307 return sa->plane->base.id - sb->plane->base.id;
308}
309
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200310static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
311 struct drm_crtc_state *crtc_state)
312{
313 struct drm_atomic_state *state = crtc_state->state;
314 struct drm_device *dev = crtc->dev;
315 int total_planes = dev->mode_config.num_total_plane;
316 struct drm_plane_state **states;
317 struct drm_plane *plane;
318 int i, n = 0;
319 int ret = 0;
320
321 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
322 crtc->base.id, crtc->name);
323
Michal Hocko0ee931c2017-09-13 16:28:29 -0700324 states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200325 if (!states)
326 return -ENOMEM;
327
328 /*
329 * Normalization process might create new states for planes which
330 * normalized_zpos has to be recalculated.
331 */
332 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
333 struct drm_plane_state *plane_state =
334 drm_atomic_get_plane_state(state, plane);
335 if (IS_ERR(plane_state)) {
336 ret = PTR_ERR(plane_state);
337 goto done;
338 }
339 states[n++] = plane_state;
340 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
341 plane->base.id, plane->name,
342 plane_state->zpos);
343 }
344
345 sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
346
347 for (i = 0; i < n; i++) {
348 plane = states[i]->plane;
349
350 states[i]->normalized_zpos = i;
351 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
352 plane->base.id, plane->name, i);
353 }
354 crtc_state->zpos_changed = true;
355
356done:
357 kfree(states);
358 return ret;
359}
360
361/**
Daniel Vetter52a9fcd2016-08-12 22:48:51 +0200362 * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200363 * @dev: DRM device
364 * @state: atomic state of DRM device
365 *
366 * This function calculates normalized zpos value for all modified planes in
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200367 * the provided atomic state of DRM device.
368 *
369 * For every CRTC this function checks new states of all planes assigned to
370 * it and calculates normalized zpos value for these planes. Planes are compared
371 * first by their zpos values, then by plane id (if zpos is equal). The plane
Daniel Vetterd5745282017-01-25 07:26:45 +0100372 * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
373 * is then filled with unique values from 0 to number of active planes in crtc
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200374 * minus one.
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200375 *
376 * RETURNS
377 * Zero for success or -errno
378 */
Daniel Vetter52a9fcd2016-08-12 22:48:51 +0200379int drm_atomic_normalize_zpos(struct drm_device *dev,
380 struct drm_atomic_state *state)
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200381{
382 struct drm_crtc *crtc;
Maarten Lankhorst62c7bdc2017-02-16 15:47:10 +0100383 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200384 struct drm_plane *plane;
Maarten Lankhorst62c7bdc2017-02-16 15:47:10 +0100385 struct drm_plane_state *old_plane_state, *new_plane_state;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200386 int i, ret = 0;
387
Maarten Lankhorst62c7bdc2017-02-16 15:47:10 +0100388 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
389 crtc = new_plane_state->crtc;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200390 if (!crtc)
391 continue;
Maarten Lankhorst62c7bdc2017-02-16 15:47:10 +0100392 if (old_plane_state->zpos != new_plane_state->zpos) {
393 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
394 new_crtc_state->zpos_changed = true;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200395 }
396 }
397
Maarten Lankhorst62c7bdc2017-02-16 15:47:10 +0100398 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
399 if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
400 new_crtc_state->zpos_changed) {
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200401 ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
Maarten Lankhorst62c7bdc2017-02-16 15:47:10 +0100402 new_crtc_state);
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200403 if (ret)
404 return ret;
405 }
406 }
407 return 0;
408}
Daniel Vetter52a9fcd2016-08-12 22:48:51 +0200409EXPORT_SYMBOL(drm_atomic_normalize_zpos);