xref: /dragonfly/sys/dev/drm/drm_blend.c (revision 3f2dd94a)
11dedbd3bSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot  * Copyright (C) 2016 Samsung Electronics Co.Ltd
31dedbd3bSFrançois Tigeot  * Authors:
41dedbd3bSFrançois Tigeot  *	Marek Szyprowski <m.szyprowski@samsung.com>
51dedbd3bSFrançois Tigeot  *
61dedbd3bSFrançois Tigeot  * DRM core plane blending related functions
71dedbd3bSFrançois Tigeot  *
81dedbd3bSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
91dedbd3bSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
101dedbd3bSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
111dedbd3bSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
121dedbd3bSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
131dedbd3bSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
141dedbd3bSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
151dedbd3bSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
161dedbd3bSFrançois Tigeot  * is" without express or implied warranty.
171dedbd3bSFrançois Tigeot  *
181dedbd3bSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
191dedbd3bSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
201dedbd3bSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
211dedbd3bSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
221dedbd3bSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
231dedbd3bSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
241dedbd3bSFrançois Tigeot  * OF THIS SOFTWARE.
251dedbd3bSFrançois Tigeot  */
261dedbd3bSFrançois Tigeot #include <drm/drmP.h>
271dedbd3bSFrançois Tigeot #include <drm/drm_atomic.h>
281dedbd3bSFrançois Tigeot #include <drm/drm_blend.h>
291dedbd3bSFrançois Tigeot #include <linux/export.h>
301dedbd3bSFrançois Tigeot #include <linux/slab.h>
311dedbd3bSFrançois Tigeot #include <linux/sort.h>
321dedbd3bSFrançois Tigeot 
331dedbd3bSFrançois Tigeot #include "drm_crtc_internal.h"
341dedbd3bSFrançois Tigeot 
351dedbd3bSFrançois Tigeot /**
361dedbd3bSFrançois Tigeot  * DOC: overview
371dedbd3bSFrançois Tigeot  *
381dedbd3bSFrançois Tigeot  * The basic plane composition model supported by standard plane properties only
391dedbd3bSFrançois Tigeot  * has a source rectangle (in logical pixels within the &drm_framebuffer), with
401dedbd3bSFrançois Tigeot  * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
411dedbd3bSFrançois Tigeot  * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
421dedbd3bSFrançois Tigeot  * defined by the horizontal and vertical visible pixels (stored in @hdisplay
43a85cb24fSFrançois Tigeot  * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
44a85cb24fSFrançois Tigeot  * two rectangles are both stored in the &drm_plane_state.
451dedbd3bSFrançois Tigeot  *
461dedbd3bSFrançois Tigeot  * For the atomic ioctl the following standard (atomic) properties on the plane object
471dedbd3bSFrançois Tigeot  * encode the basic plane composition model:
481dedbd3bSFrançois Tigeot  *
491dedbd3bSFrançois Tigeot  * SRC_X:
501dedbd3bSFrançois Tigeot  * 	X coordinate offset for the source rectangle within the
511dedbd3bSFrançois Tigeot  * 	&drm_framebuffer, in 16.16 fixed point. Must be positive.
521dedbd3bSFrançois Tigeot  * SRC_Y:
531dedbd3bSFrançois Tigeot  * 	Y coordinate offset for the source rectangle within the
541dedbd3bSFrançois Tigeot  * 	&drm_framebuffer, in 16.16 fixed point. Must be positive.
551dedbd3bSFrançois Tigeot  * SRC_W:
561dedbd3bSFrançois Tigeot  * 	Width for the source rectangle within the &drm_framebuffer, in 16.16
571dedbd3bSFrançois Tigeot  * 	fixed point. SRC_X plus SRC_W must be within the width of the source
581dedbd3bSFrançois Tigeot  * 	framebuffer. Must be positive.
591dedbd3bSFrançois Tigeot  * SRC_H:
601dedbd3bSFrançois Tigeot  * 	Height for the source rectangle within the &drm_framebuffer, in 16.16
611dedbd3bSFrançois Tigeot  * 	fixed point. SRC_Y plus SRC_H must be within the height of the source
621dedbd3bSFrançois Tigeot  * 	framebuffer. Must be positive.
631dedbd3bSFrançois Tigeot  * CRTC_X:
641dedbd3bSFrançois Tigeot  * 	X coordinate offset for the destination rectangle. Can be negative.
651dedbd3bSFrançois Tigeot  * CRTC_Y:
661dedbd3bSFrançois Tigeot  * 	Y coordinate offset for the destination rectangle. Can be negative.
671dedbd3bSFrançois Tigeot  * CRTC_W:
681dedbd3bSFrançois Tigeot  * 	Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
691dedbd3bSFrançois Tigeot  * 	the currently visible horizontal area of the &drm_crtc.
701dedbd3bSFrançois Tigeot  * CRTC_H:
711dedbd3bSFrançois Tigeot  * 	Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
721dedbd3bSFrançois Tigeot  * 	the currently visible vertical area of the &drm_crtc.
731dedbd3bSFrançois Tigeot  * FB_ID:
741dedbd3bSFrançois Tigeot  * 	Mode object ID of the &drm_framebuffer this plane should scan out.
751dedbd3bSFrançois Tigeot  * CRTC_ID:
761dedbd3bSFrançois Tigeot  * 	Mode object ID of the &drm_crtc this plane should be connected to.
771dedbd3bSFrançois Tigeot  *
781dedbd3bSFrançois Tigeot  * Note that the source rectangle must fully lie within the bounds of the
791dedbd3bSFrançois Tigeot  * &drm_framebuffer. The destination rectangle can lie outside of the visible
801dedbd3bSFrançois Tigeot  * area of the current mode of the CRTC. It must be apprpriately clipped by the
811dedbd3bSFrançois Tigeot  * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
821dedbd3bSFrançois Tigeot  * are also allowed to round the subpixel sampling positions appropriately, but
831dedbd3bSFrançois Tigeot  * only to the next full pixel. No pixel outside of the source rectangle may
841dedbd3bSFrançois Tigeot  * ever be sampled, which is important when applying more sophisticated
851dedbd3bSFrançois Tigeot  * filtering than just a bilinear one when scaling. The filtering mode when
861dedbd3bSFrançois Tigeot  * scaling is unspecified.
871dedbd3bSFrançois Tigeot  *
881dedbd3bSFrançois Tigeot  * On top of this basic transformation additional properties can be exposed by
891dedbd3bSFrançois Tigeot  * the driver:
901dedbd3bSFrançois Tigeot  *
914be47400SFrançois Tigeot  * - Rotation is set up with drm_plane_create_rotation_property(). It adds a
921dedbd3bSFrançois Tigeot  *   rotation and reflection step between the source and destination rectangles.
931dedbd3bSFrançois Tigeot  *   Without this property the rectangle is only scaled, but not rotated or
941dedbd3bSFrançois Tigeot  *   reflected.
951dedbd3bSFrançois Tigeot  *
961dedbd3bSFrançois Tigeot  * - Z position is set up with drm_plane_create_zpos_immutable_property() and
971dedbd3bSFrançois Tigeot  *   drm_plane_create_zpos_property(). It controls the visibility of overlapping
981dedbd3bSFrançois Tigeot  *   planes. Without this property the primary plane is always below the cursor
991dedbd3bSFrançois Tigeot  *   plane, and ordering between all other planes is undefined.
1001dedbd3bSFrançois Tigeot  *
1011dedbd3bSFrançois Tigeot  * Note that all the property extensions described here apply either to the
1021dedbd3bSFrançois Tigeot  * plane or the CRTC (e.g. for the background color, which currently is not
1031dedbd3bSFrançois Tigeot  * exposed and assumed to be black).
1041dedbd3bSFrançois Tigeot  */
1051dedbd3bSFrançois Tigeot 
1061dedbd3bSFrançois Tigeot /**
1074be47400SFrançois Tigeot  * drm_plane_create_rotation_property - create a new rotation property
1084be47400SFrançois Tigeot  * @plane: drm plane
1094be47400SFrançois Tigeot  * @rotation: initial value of the rotation property
1101dedbd3bSFrançois Tigeot  * @supported_rotations: bitmask of supported rotations and reflections
1111dedbd3bSFrançois Tigeot  *
1121dedbd3bSFrançois Tigeot  * This creates a new property with the selected support for transformations.
1131dedbd3bSFrançois Tigeot  *
1141dedbd3bSFrançois Tigeot  * Since a rotation by 180° degress is the same as reflecting both along the x
1151dedbd3bSFrançois Tigeot  * and the y axis the rotation property is somewhat redundant. Drivers can use
1161dedbd3bSFrançois Tigeot  * drm_rotation_simplify() to normalize values of this property.
1171dedbd3bSFrançois Tigeot  *
1181dedbd3bSFrançois Tigeot  * The property exposed to userspace is a bitmask property (see
1191dedbd3bSFrançois Tigeot  * drm_property_create_bitmask()) called "rotation" and has the following
1201dedbd3bSFrançois Tigeot  * bitmask enumaration values:
1211dedbd3bSFrançois Tigeot  *
122*3f2dd94aSFrançois Tigeot  * DRM_MODE_ROTATE_0:
1231dedbd3bSFrançois Tigeot  * 	"rotate-0"
124*3f2dd94aSFrançois Tigeot  * DRM_MODE_ROTATE_90:
1251dedbd3bSFrançois Tigeot  * 	"rotate-90"
126*3f2dd94aSFrançois Tigeot  * DRM_MODE_ROTATE_180:
1271dedbd3bSFrançois Tigeot  * 	"rotate-180"
128*3f2dd94aSFrançois Tigeot  * DRM_MODE_ROTATE_270:
1291dedbd3bSFrançois Tigeot  * 	"rotate-270"
130*3f2dd94aSFrançois Tigeot  * DRM_MODE_REFLECT_X:
1311dedbd3bSFrançois Tigeot  * 	"reflect-x"
132*3f2dd94aSFrançois Tigeot  * DRM_MODE_REFLECT_Y:
1331dedbd3bSFrançois Tigeot  * 	"reflect-y"
1341dedbd3bSFrançois Tigeot  *
1351dedbd3bSFrançois Tigeot  * Rotation is the specified amount in degrees in counter clockwise direction,
1361dedbd3bSFrançois Tigeot  * the X and Y axis are within the source rectangle, i.e.  the X/Y axis before
1371dedbd3bSFrançois Tigeot  * rotation. After reflection, the rotation is applied to the image sampled from
1381dedbd3bSFrançois Tigeot  * the source rectangle, before scaling it to fit the destination rectangle.
1391dedbd3bSFrançois Tigeot  */
drm_plane_create_rotation_property(struct drm_plane * plane,unsigned int rotation,unsigned int supported_rotations)1404be47400SFrançois Tigeot int drm_plane_create_rotation_property(struct drm_plane *plane,
1414be47400SFrançois Tigeot 				       unsigned int rotation,
1421dedbd3bSFrançois Tigeot 				       unsigned int supported_rotations)
1431dedbd3bSFrançois Tigeot {
1441dedbd3bSFrançois Tigeot 	static const struct drm_prop_enum_list props[] = {
145*3f2dd94aSFrançois Tigeot 		{ __builtin_ffs(DRM_MODE_ROTATE_0) - 1,   "rotate-0" },
146*3f2dd94aSFrançois Tigeot 		{ __builtin_ffs(DRM_MODE_ROTATE_90) - 1,  "rotate-90" },
147*3f2dd94aSFrançois Tigeot 		{ __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
148*3f2dd94aSFrançois Tigeot 		{ __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
149*3f2dd94aSFrançois Tigeot 		{ __builtin_ffs(DRM_MODE_REFLECT_X) - 1,  "reflect-x" },
150*3f2dd94aSFrançois Tigeot 		{ __builtin_ffs(DRM_MODE_REFLECT_Y) - 1,  "reflect-y" },
1511dedbd3bSFrançois Tigeot 	};
1524be47400SFrançois Tigeot 	struct drm_property *prop;
1531dedbd3bSFrançois Tigeot 
154*3f2dd94aSFrançois Tigeot 	WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
155*3f2dd94aSFrançois Tigeot 	WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
1564be47400SFrançois Tigeot 	WARN_ON(rotation & ~supported_rotations);
1574be47400SFrançois Tigeot 
1584be47400SFrançois Tigeot 	prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
1591dedbd3bSFrançois Tigeot 					   props, ARRAY_SIZE(props),
1601dedbd3bSFrançois Tigeot 					   supported_rotations);
1614be47400SFrançois Tigeot 	if (!prop)
1624be47400SFrançois Tigeot 		return -ENOMEM;
1634be47400SFrançois Tigeot 
1644be47400SFrançois Tigeot 	drm_object_attach_property(&plane->base, prop, rotation);
1654be47400SFrançois Tigeot 
1664be47400SFrançois Tigeot 	if (plane->state)
1674be47400SFrançois Tigeot 		plane->state->rotation = rotation;
1684be47400SFrançois Tigeot 
1694be47400SFrançois Tigeot 	plane->rotation_property = prop;
1704be47400SFrançois Tigeot 
1714be47400SFrançois Tigeot 	return 0;
1721dedbd3bSFrançois Tigeot }
1734be47400SFrançois Tigeot EXPORT_SYMBOL(drm_plane_create_rotation_property);
1741dedbd3bSFrançois Tigeot 
1751dedbd3bSFrançois Tigeot /**
1761dedbd3bSFrançois Tigeot  * drm_rotation_simplify() - Try to simplify the rotation
1771dedbd3bSFrançois Tigeot  * @rotation: Rotation to be simplified
1781dedbd3bSFrançois Tigeot  * @supported_rotations: Supported rotations
1791dedbd3bSFrançois Tigeot  *
1801dedbd3bSFrançois Tigeot  * Attempt to simplify the rotation to a form that is supported.
181*3f2dd94aSFrançois Tigeot  * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
1821dedbd3bSFrançois Tigeot  * one could call this function like this:
1831dedbd3bSFrançois Tigeot  *
184*3f2dd94aSFrançois Tigeot  * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
185*3f2dd94aSFrançois Tigeot  *                       DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
186*3f2dd94aSFrançois Tigeot  *                       DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
1871dedbd3bSFrançois Tigeot  *
188*3f2dd94aSFrançois Tigeot  * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
1891dedbd3bSFrançois Tigeot  * transforms the hardware supports, this function may not
1901dedbd3bSFrançois Tigeot  * be able to produce a supported transform, so the caller should
1911dedbd3bSFrançois Tigeot  * check the result afterwards.
1921dedbd3bSFrançois Tigeot  */
drm_rotation_simplify(unsigned int rotation,unsigned int supported_rotations)1931dedbd3bSFrançois Tigeot unsigned int drm_rotation_simplify(unsigned int rotation,
1941dedbd3bSFrançois Tigeot 				   unsigned int supported_rotations)
1951dedbd3bSFrançois Tigeot {
1961dedbd3bSFrançois Tigeot 	if (rotation & ~supported_rotations) {
197*3f2dd94aSFrançois Tigeot 		rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
198*3f2dd94aSFrançois Tigeot 		rotation = (rotation & DRM_MODE_REFLECT_MASK) |
199*3f2dd94aSFrançois Tigeot 		           BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
200*3f2dd94aSFrançois Tigeot 		           % 4);
2011dedbd3bSFrançois Tigeot 	}
2021dedbd3bSFrançois Tigeot 
2031dedbd3bSFrançois Tigeot 	return rotation;
2041dedbd3bSFrançois Tigeot }
2051dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_rotation_simplify);
2061dedbd3bSFrançois Tigeot 
2071dedbd3bSFrançois Tigeot /**
2081dedbd3bSFrançois Tigeot  * drm_plane_create_zpos_property - create mutable zpos property
2091dedbd3bSFrançois Tigeot  * @plane: drm plane
2101dedbd3bSFrançois Tigeot  * @zpos: initial value of zpos property
2111dedbd3bSFrançois Tigeot  * @min: minimal possible value of zpos property
2121dedbd3bSFrançois Tigeot  * @max: maximal possible value of zpos property
2131dedbd3bSFrançois Tigeot  *
2141dedbd3bSFrançois Tigeot  * This function initializes generic mutable zpos property and enables support
2151dedbd3bSFrançois Tigeot  * for it in drm core. Drivers can then attach this property to planes to enable
2161dedbd3bSFrançois Tigeot  * support for configurable planes arrangement during blending operation.
2171dedbd3bSFrançois Tigeot  * Once mutable zpos property has been enabled, the DRM core will automatically
218a85cb24fSFrançois Tigeot  * calculate &drm_plane_state.normalized_zpos values. Usually min should be set
2191dedbd3bSFrançois Tigeot  * to 0 and max to maximal number of planes for given crtc - 1.
2201dedbd3bSFrançois Tigeot  *
2211dedbd3bSFrançois Tigeot  * If zpos of some planes cannot be changed (like fixed background or
2221dedbd3bSFrançois Tigeot  * cursor/topmost planes), driver should adjust min/max values and assign those
2231dedbd3bSFrançois Tigeot  * planes immutable zpos property with lower or higher values (for more
2241dedbd3bSFrançois Tigeot  * information, see drm_plane_create_zpos_immutable_property() function). In such
2251dedbd3bSFrançois Tigeot  * case driver should also assign proper initial zpos values for all planes in
2261dedbd3bSFrançois Tigeot  * its plane_reset() callback, so the planes will be always sorted properly.
2271dedbd3bSFrançois Tigeot  *
2281dedbd3bSFrançois Tigeot  * See also drm_atomic_normalize_zpos().
2291dedbd3bSFrançois Tigeot  *
2301dedbd3bSFrançois Tigeot  * The property exposed to userspace is called "zpos".
2311dedbd3bSFrançois Tigeot  *
2321dedbd3bSFrançois Tigeot  * Returns:
2331dedbd3bSFrançois Tigeot  * Zero on success, negative errno on failure.
2341dedbd3bSFrançois Tigeot  */
drm_plane_create_zpos_property(struct drm_plane * plane,unsigned int zpos,unsigned int min,unsigned int max)2351dedbd3bSFrançois Tigeot int drm_plane_create_zpos_property(struct drm_plane *plane,
2361dedbd3bSFrançois Tigeot 				   unsigned int zpos,
2371dedbd3bSFrançois Tigeot 				   unsigned int min, unsigned int max)
2381dedbd3bSFrançois Tigeot {
2391dedbd3bSFrançois Tigeot 	struct drm_property *prop;
2401dedbd3bSFrançois Tigeot 
2411dedbd3bSFrançois Tigeot 	prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
2421dedbd3bSFrançois Tigeot 	if (!prop)
2431dedbd3bSFrançois Tigeot 		return -ENOMEM;
2441dedbd3bSFrançois Tigeot 
2451dedbd3bSFrançois Tigeot 	drm_object_attach_property(&plane->base, prop, zpos);
2461dedbd3bSFrançois Tigeot 
2471dedbd3bSFrançois Tigeot 	plane->zpos_property = prop;
2481dedbd3bSFrançois Tigeot 
2491dedbd3bSFrançois Tigeot 	if (plane->state) {
2501dedbd3bSFrançois Tigeot 		plane->state->zpos = zpos;
2511dedbd3bSFrançois Tigeot 		plane->state->normalized_zpos = zpos;
2521dedbd3bSFrançois Tigeot 	}
2531dedbd3bSFrançois Tigeot 
2541dedbd3bSFrançois Tigeot 	return 0;
2551dedbd3bSFrançois Tigeot }
2561dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_plane_create_zpos_property);
2571dedbd3bSFrançois Tigeot 
2581dedbd3bSFrançois Tigeot /**
2591dedbd3bSFrançois Tigeot  * drm_plane_create_zpos_immutable_property - create immuttable zpos property
2601dedbd3bSFrançois Tigeot  * @plane: drm plane
2611dedbd3bSFrançois Tigeot  * @zpos: value of zpos property
2621dedbd3bSFrançois Tigeot  *
2631dedbd3bSFrançois Tigeot  * This function initializes generic immutable zpos property and enables
2641dedbd3bSFrançois Tigeot  * support for it in drm core. Using this property driver lets userspace
2651dedbd3bSFrançois Tigeot  * to get the arrangement of the planes for blending operation and notifies
2661dedbd3bSFrançois Tigeot  * it that the hardware (or driver) doesn't support changing of the planes'
2671dedbd3bSFrançois Tigeot  * order. For mutable zpos see drm_plane_create_zpos_property().
2681dedbd3bSFrançois Tigeot  *
2691dedbd3bSFrançois Tigeot  * The property exposed to userspace is called "zpos".
2701dedbd3bSFrançois Tigeot  *
2711dedbd3bSFrançois Tigeot  * Returns:
2721dedbd3bSFrançois Tigeot  * Zero on success, negative errno on failure.
2731dedbd3bSFrançois Tigeot  */
drm_plane_create_zpos_immutable_property(struct drm_plane * plane,unsigned int zpos)2741dedbd3bSFrançois Tigeot int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
2751dedbd3bSFrançois Tigeot 					     unsigned int zpos)
2761dedbd3bSFrançois Tigeot {
2771dedbd3bSFrançois Tigeot 	struct drm_property *prop;
2781dedbd3bSFrançois Tigeot 
2791dedbd3bSFrançois Tigeot 	prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
2801dedbd3bSFrançois Tigeot 					 "zpos", zpos, zpos);
2811dedbd3bSFrançois Tigeot 	if (!prop)
2821dedbd3bSFrançois Tigeot 		return -ENOMEM;
2831dedbd3bSFrançois Tigeot 
2841dedbd3bSFrançois Tigeot 	drm_object_attach_property(&plane->base, prop, zpos);
2851dedbd3bSFrançois Tigeot 
2861dedbd3bSFrançois Tigeot 	plane->zpos_property = prop;
2871dedbd3bSFrançois Tigeot 
2881dedbd3bSFrançois Tigeot 	if (plane->state) {
2891dedbd3bSFrançois Tigeot 		plane->state->zpos = zpos;
2901dedbd3bSFrançois Tigeot 		plane->state->normalized_zpos = zpos;
2911dedbd3bSFrançois Tigeot 	}
2921dedbd3bSFrançois Tigeot 
2931dedbd3bSFrançois Tigeot 	return 0;
2941dedbd3bSFrançois Tigeot }
2951dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
2961dedbd3bSFrançois Tigeot 
drm_atomic_state_zpos_cmp(const void * a,const void * b)2971dedbd3bSFrançois Tigeot static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
2981dedbd3bSFrançois Tigeot {
2991dedbd3bSFrançois Tigeot 	const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
3001dedbd3bSFrançois Tigeot 	const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
3011dedbd3bSFrançois Tigeot 
3021dedbd3bSFrançois Tigeot 	if (sa->zpos != sb->zpos)
3031dedbd3bSFrançois Tigeot 		return sa->zpos - sb->zpos;
3041dedbd3bSFrançois Tigeot 	else
3051dedbd3bSFrançois Tigeot 		return sa->plane->base.id - sb->plane->base.id;
3061dedbd3bSFrançois Tigeot }
3071dedbd3bSFrançois Tigeot 
drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)3081dedbd3bSFrançois Tigeot static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
3091dedbd3bSFrançois Tigeot 					  struct drm_crtc_state *crtc_state)
3101dedbd3bSFrançois Tigeot {
3111dedbd3bSFrançois Tigeot 	struct drm_atomic_state *state = crtc_state->state;
3121dedbd3bSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
3131dedbd3bSFrançois Tigeot 	int total_planes = dev->mode_config.num_total_plane;
3141dedbd3bSFrançois Tigeot 	struct drm_plane_state **states;
3151dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
3161dedbd3bSFrançois Tigeot 	int i, n = 0;
3171dedbd3bSFrançois Tigeot 	int ret = 0;
3181dedbd3bSFrançois Tigeot 
3191dedbd3bSFrançois Tigeot 	DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
3201dedbd3bSFrançois Tigeot 			 crtc->base.id, crtc->name);
3211dedbd3bSFrançois Tigeot 
322*3f2dd94aSFrançois Tigeot 	states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
3231dedbd3bSFrançois Tigeot 	if (!states)
3241dedbd3bSFrançois Tigeot 		return -ENOMEM;
3251dedbd3bSFrançois Tigeot 
3261dedbd3bSFrançois Tigeot 	/*
3271dedbd3bSFrançois Tigeot 	 * Normalization process might create new states for planes which
3281dedbd3bSFrançois Tigeot 	 * normalized_zpos has to be recalculated.
3291dedbd3bSFrançois Tigeot 	 */
3301dedbd3bSFrançois Tigeot 	drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
3311dedbd3bSFrançois Tigeot 		struct drm_plane_state *plane_state =
3321dedbd3bSFrançois Tigeot 			drm_atomic_get_plane_state(state, plane);
3331dedbd3bSFrançois Tigeot 		if (IS_ERR(plane_state)) {
3341dedbd3bSFrançois Tigeot 			ret = PTR_ERR(plane_state);
3351dedbd3bSFrançois Tigeot 			goto done;
3361dedbd3bSFrançois Tigeot 		}
3371dedbd3bSFrançois Tigeot 		states[n++] = plane_state;
3381dedbd3bSFrançois Tigeot 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
3391dedbd3bSFrançois Tigeot 				 plane->base.id, plane->name,
3401dedbd3bSFrançois Tigeot 				 plane_state->zpos);
3411dedbd3bSFrançois Tigeot 	}
3421dedbd3bSFrançois Tigeot 
3431dedbd3bSFrançois Tigeot 	sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
3441dedbd3bSFrançois Tigeot 
3451dedbd3bSFrançois Tigeot 	for (i = 0; i < n; i++) {
3461dedbd3bSFrançois Tigeot 		plane = states[i]->plane;
3471dedbd3bSFrançois Tigeot 
3481dedbd3bSFrançois Tigeot 		states[i]->normalized_zpos = i;
3491dedbd3bSFrançois Tigeot 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
3501dedbd3bSFrançois Tigeot 				 plane->base.id, plane->name, i);
3511dedbd3bSFrançois Tigeot 	}
3521dedbd3bSFrançois Tigeot 	crtc_state->zpos_changed = true;
3531dedbd3bSFrançois Tigeot 
3541dedbd3bSFrançois Tigeot done:
3551dedbd3bSFrançois Tigeot 	kfree(states);
3561dedbd3bSFrançois Tigeot 	return ret;
3571dedbd3bSFrançois Tigeot }
3581dedbd3bSFrançois Tigeot 
3591dedbd3bSFrançois Tigeot /**
3601dedbd3bSFrançois Tigeot  * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
3611dedbd3bSFrançois Tigeot  * @dev: DRM device
3621dedbd3bSFrançois Tigeot  * @state: atomic state of DRM device
3631dedbd3bSFrançois Tigeot  *
3641dedbd3bSFrançois Tigeot  * This function calculates normalized zpos value for all modified planes in
3651dedbd3bSFrançois Tigeot  * the provided atomic state of DRM device.
3661dedbd3bSFrançois Tigeot  *
3671dedbd3bSFrançois Tigeot  * For every CRTC this function checks new states of all planes assigned to
3681dedbd3bSFrançois Tigeot  * it and calculates normalized zpos value for these planes. Planes are compared
3691dedbd3bSFrançois Tigeot  * first by their zpos values, then by plane id (if zpos is equal). The plane
370a85cb24fSFrançois Tigeot  * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
371a85cb24fSFrançois Tigeot  * is then filled with unique values from 0 to number of active planes in crtc
3721dedbd3bSFrançois Tigeot  * minus one.
3731dedbd3bSFrançois Tigeot  *
3741dedbd3bSFrançois Tigeot  * RETURNS
3751dedbd3bSFrançois Tigeot  * Zero for success or -errno
3761dedbd3bSFrançois Tigeot  */
drm_atomic_normalize_zpos(struct drm_device * dev,struct drm_atomic_state * state)3771dedbd3bSFrançois Tigeot int drm_atomic_normalize_zpos(struct drm_device *dev,
3781dedbd3bSFrançois Tigeot 			      struct drm_atomic_state *state)
3791dedbd3bSFrançois Tigeot {
3801dedbd3bSFrançois Tigeot 	struct drm_crtc *crtc;
381a85cb24fSFrançois Tigeot 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
3821dedbd3bSFrançois Tigeot 	struct drm_plane *plane;
383a85cb24fSFrançois Tigeot 	struct drm_plane_state *old_plane_state, *new_plane_state;
3841dedbd3bSFrançois Tigeot 	int i, ret = 0;
3851dedbd3bSFrançois Tigeot 
386a85cb24fSFrançois Tigeot 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
387a85cb24fSFrançois Tigeot 		crtc = new_plane_state->crtc;
3881dedbd3bSFrançois Tigeot 		if (!crtc)
3891dedbd3bSFrançois Tigeot 			continue;
390a85cb24fSFrançois Tigeot 		if (old_plane_state->zpos != new_plane_state->zpos) {
391a85cb24fSFrançois Tigeot 			new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
392a85cb24fSFrançois Tigeot 			new_crtc_state->zpos_changed = true;
3931dedbd3bSFrançois Tigeot 		}
3941dedbd3bSFrançois Tigeot 	}
3951dedbd3bSFrançois Tigeot 
396a85cb24fSFrançois Tigeot 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
397a85cb24fSFrançois Tigeot 		if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
398a85cb24fSFrançois Tigeot 		    new_crtc_state->zpos_changed) {
3991dedbd3bSFrançois Tigeot 			ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
400a85cb24fSFrançois Tigeot 								    new_crtc_state);
4011dedbd3bSFrançois Tigeot 			if (ret)
4021dedbd3bSFrançois Tigeot 				return ret;
4031dedbd3bSFrançois Tigeot 		}
4041dedbd3bSFrançois Tigeot 	}
4051dedbd3bSFrançois Tigeot 	return 0;
4061dedbd3bSFrançois Tigeot }
4071dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_normalize_zpos);
408