2 * Copyright (C) 2016 Samsung Electronics Co.Ltd
4 * Marek Szyprowski <m.szyprowski@samsung.com>
6 * DRM core plane blending related functions
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.
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
27 #include <drm/drm_atomic.h>
28 #include <drm/drm_blend.h>
29 #include <linux/export.h>
30 #include <linux/slab.h>
31 #include <linux/sort.h>
33 #include "drm_crtc_internal.h"
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
43 * and @vdisplay) of the requested mode (stored in @mode in the
44 * &drm_crtc_state). These two rectangles are both stored in the
47 * For the atomic ioctl the following standard (atomic) properties on the plane object
48 * encode the basic plane composition model:
51 * X coordinate offset for the source rectangle within the
52 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
54 * Y coordinate offset for the source rectangle within the
55 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
57 * Width for the source rectangle within the &drm_framebuffer, in 16.16
58 * fixed point. SRC_X plus SRC_W must be within the width of the source
59 * framebuffer. Must be positive.
61 * Height for the source rectangle within the &drm_framebuffer, in 16.16
62 * fixed point. SRC_Y plus SRC_H must be within the height of the source
63 * framebuffer. Must be positive.
65 * X coordinate offset for the destination rectangle. Can be negative.
67 * Y coordinate offset for the destination rectangle. Can be negative.
69 * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
70 * the currently visible horizontal area of the &drm_crtc.
72 * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
73 * the currently visible vertical area of the &drm_crtc.
75 * Mode object ID of the &drm_framebuffer this plane should scan out.
77 * Mode object ID of the &drm_crtc this plane should be connected to.
79 * Note that the source rectangle must fully lie within the bounds of the
80 * &drm_framebuffer. The destination rectangle can lie outside of the visible
81 * area of the current mode of the CRTC. It must be apprpriately clipped by the
82 * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
83 * are also allowed to round the subpixel sampling positions appropriately, but
84 * only to the next full pixel. No pixel outside of the source rectangle may
85 * ever be sampled, which is important when applying more sophisticated
86 * filtering than just a bilinear one when scaling. The filtering mode when
87 * scaling is unspecified.
89 * On top of this basic transformation additional properties can be exposed by
92 * - Rotation is set up with drm_plane_create_rotation_property(). It adds a
93 * rotation and reflection step between the source and destination rectangles.
94 * Without this property the rectangle is only scaled, but not rotated or
97 * - Z position is set up with drm_plane_create_zpos_immutable_property() and
98 * drm_plane_create_zpos_property(). It controls the visibility of overlapping
99 * planes. Without this property the primary plane is always below the cursor
100 * plane, and ordering between all other planes is undefined.
102 * Note that all the property extensions described here apply either to the
103 * plane or the CRTC (e.g. for the background color, which currently is not
104 * exposed and assumed to be black).
108 * drm_plane_create_rotation_property - create a new rotation property
110 * @rotation: initial value of the rotation property
111 * @supported_rotations: bitmask of supported rotations and reflections
113 * This creates a new property with the selected support for transformations.
115 * Since a rotation by 180° degress is the same as reflecting both along the x
116 * and the y axis the rotation property is somewhat redundant. Drivers can use
117 * drm_rotation_simplify() to normalize values of this property.
119 * The property exposed to userspace is a bitmask property (see
120 * drm_property_create_bitmask()) called "rotation" and has the following
121 * bitmask enumaration values:
136 * Rotation is the specified amount in degrees in counter clockwise direction,
137 * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
138 * rotation. After reflection, the rotation is applied to the image sampled from
139 * the source rectangle, before scaling it to fit the destination rectangle.
141 int drm_plane_create_rotation_property(struct drm_plane
*plane
,
142 unsigned int rotation
,
143 unsigned int supported_rotations
)
145 static const struct drm_prop_enum_list props
[] = {
146 { __builtin_ffs(DRM_ROTATE_0
) - 1, "rotate-0" },
147 { __builtin_ffs(DRM_ROTATE_90
) - 1, "rotate-90" },
148 { __builtin_ffs(DRM_ROTATE_180
) - 1, "rotate-180" },
149 { __builtin_ffs(DRM_ROTATE_270
) - 1, "rotate-270" },
150 { __builtin_ffs(DRM_REFLECT_X
) - 1, "reflect-x" },
151 { __builtin_ffs(DRM_REFLECT_Y
) - 1, "reflect-y" },
153 struct drm_property
*prop
;
155 WARN_ON((supported_rotations
& DRM_ROTATE_MASK
) == 0);
156 WARN_ON(!is_power_of_2(rotation
& DRM_ROTATE_MASK
));
157 WARN_ON(rotation
& ~supported_rotations
);
159 prop
= drm_property_create_bitmask(plane
->dev
, 0, "rotation",
160 props
, ARRAY_SIZE(props
),
161 supported_rotations
);
165 drm_object_attach_property(&plane
->base
, prop
, rotation
);
168 plane
->state
->rotation
= rotation
;
170 plane
->rotation_property
= prop
;
174 EXPORT_SYMBOL(drm_plane_create_rotation_property
);
177 * drm_rotation_simplify() - Try to simplify the rotation
178 * @rotation: Rotation to be simplified
179 * @supported_rotations: Supported rotations
181 * Attempt to simplify the rotation to a form that is supported.
182 * Eg. if the hardware supports everything except DRM_REFLECT_X
183 * one could call this function like this:
185 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
186 * DRM_ROTATE_90 | DRM_ROTATE_180 |
187 * DRM_ROTATE_270 | DRM_REFLECT_Y);
189 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
190 * transforms the hardware supports, this function may not
191 * be able to produce a supported transform, so the caller should
192 * check the result afterwards.
194 unsigned int drm_rotation_simplify(unsigned int rotation
,
195 unsigned int supported_rotations
)
197 if (rotation
& ~supported_rotations
) {
198 rotation
^= DRM_REFLECT_X
| DRM_REFLECT_Y
;
199 rotation
= (rotation
& DRM_REFLECT_MASK
) |
200 BIT((ffs(rotation
& DRM_ROTATE_MASK
) + 1) % 4);
205 EXPORT_SYMBOL(drm_rotation_simplify
);
208 * drm_plane_create_zpos_property - create mutable zpos property
210 * @zpos: initial value of zpos property
211 * @min: minimal possible value of zpos property
212 * @max: maximal possible value of zpos property
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.
217 * Once mutable zpos property has been enabled, the DRM core will automatically
218 * calculate drm_plane_state->normalized_zpos values. Usually min should be set
219 * to 0 and max to maximal number of planes for given crtc - 1.
221 * If zpos of some planes cannot be changed (like fixed background or
222 * cursor/topmost planes), driver should adjust min/max values and assign those
223 * planes immutable zpos property with lower or higher values (for more
224 * information, see drm_plane_create_zpos_immutable_property() function). In such
225 * case driver should also assign proper initial zpos values for all planes in
226 * its plane_reset() callback, so the planes will be always sorted properly.
228 * See also drm_atomic_normalize_zpos().
230 * The property exposed to userspace is called "zpos".
233 * Zero on success, negative errno on failure.
235 int drm_plane_create_zpos_property(struct drm_plane
*plane
,
237 unsigned int min
, unsigned int max
)
239 struct drm_property
*prop
;
241 prop
= drm_property_create_range(plane
->dev
, 0, "zpos", min
, max
);
245 drm_object_attach_property(&plane
->base
, prop
, zpos
);
247 plane
->zpos_property
= prop
;
250 plane
->state
->zpos
= zpos
;
251 plane
->state
->normalized_zpos
= zpos
;
256 EXPORT_SYMBOL(drm_plane_create_zpos_property
);
259 * drm_plane_create_zpos_immutable_property - create immuttable zpos property
261 * @zpos: value of zpos property
263 * This function initializes generic immutable zpos property and enables
264 * support for it in drm core. Using this property driver lets userspace
265 * to get the arrangement of the planes for blending operation and notifies
266 * it that the hardware (or driver) doesn't support changing of the planes'
267 * order. For mutable zpos see drm_plane_create_zpos_property().
269 * The property exposed to userspace is called "zpos".
272 * Zero on success, negative errno on failure.
274 int drm_plane_create_zpos_immutable_property(struct drm_plane
*plane
,
277 struct drm_property
*prop
;
279 prop
= drm_property_create_range(plane
->dev
, DRM_MODE_PROP_IMMUTABLE
,
284 drm_object_attach_property(&plane
->base
, prop
, zpos
);
286 plane
->zpos_property
= prop
;
289 plane
->state
->zpos
= zpos
;
290 plane
->state
->normalized_zpos
= zpos
;
295 EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property
);
297 static int drm_atomic_state_zpos_cmp(const void *a
, const void *b
)
299 const struct drm_plane_state
*sa
= *(struct drm_plane_state
**)a
;
300 const struct drm_plane_state
*sb
= *(struct drm_plane_state
**)b
;
302 if (sa
->zpos
!= sb
->zpos
)
303 return sa
->zpos
- sb
->zpos
;
305 return sa
->plane
->base
.id
- sb
->plane
->base
.id
;
308 static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc
*crtc
,
309 struct drm_crtc_state
*crtc_state
)
311 struct drm_atomic_state
*state
= crtc_state
->state
;
312 struct drm_device
*dev
= crtc
->dev
;
313 int total_planes
= dev
->mode_config
.num_total_plane
;
314 struct drm_plane_state
**states
;
315 struct drm_plane
*plane
;
319 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
320 crtc
->base
.id
, crtc
->name
);
322 states
= kmalloc_array(total_planes
, sizeof(*states
), GFP_TEMPORARY
);
327 * Normalization process might create new states for planes which
328 * normalized_zpos has to be recalculated.
330 drm_for_each_plane_mask(plane
, dev
, crtc_state
->plane_mask
) {
331 struct drm_plane_state
*plane_state
=
332 drm_atomic_get_plane_state(state
, plane
);
333 if (IS_ERR(plane_state
)) {
334 ret
= PTR_ERR(plane_state
);
337 states
[n
++] = plane_state
;
338 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
339 plane
->base
.id
, plane
->name
,
343 sort(states
, n
, sizeof(*states
), drm_atomic_state_zpos_cmp
, NULL
);
345 for (i
= 0; i
< n
; i
++) {
346 plane
= states
[i
]->plane
;
348 states
[i
]->normalized_zpos
= i
;
349 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
350 plane
->base
.id
, plane
->name
, i
);
352 crtc_state
->zpos_changed
= true;
360 * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
362 * @state: atomic state of DRM device
364 * This function calculates normalized zpos value for all modified planes in
365 * the provided atomic state of DRM device.
367 * For every CRTC this function checks new states of all planes assigned to
368 * it and calculates normalized zpos value for these planes. Planes are compared
369 * first by their zpos values, then by plane id (if zpos is equal). The plane
370 * with lowest zpos value is at the bottom. The plane_state->normalized_zpos is
371 * then filled with unique values from 0 to number of active planes in crtc
375 * Zero for success or -errno
377 int drm_atomic_normalize_zpos(struct drm_device
*dev
,
378 struct drm_atomic_state
*state
)
380 struct drm_crtc
*crtc
;
381 struct drm_crtc_state
*crtc_state
;
382 struct drm_plane
*plane
;
383 struct drm_plane_state
*plane_state
;
386 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
387 crtc
= plane_state
->crtc
;
390 if (plane
->state
->zpos
!= plane_state
->zpos
) {
392 drm_atomic_get_existing_crtc_state(state
, crtc
);
393 crtc_state
->zpos_changed
= true;
397 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
398 if (crtc_state
->plane_mask
!= crtc
->state
->plane_mask
||
399 crtc_state
->zpos_changed
) {
400 ret
= drm_atomic_helper_crtc_normalize_zpos(crtc
,
408 EXPORT_SYMBOL(drm_atomic_normalize_zpos
);