xref: /netbsd/sys/external/bsd/drm2/dist/drm/drm_blend.c (revision 8248f75a)
1*8248f75aSriastradh /*	$NetBSD: drm_blend.c,v 1.4 2021/12/19 00:50:01 riastradh Exp $	*/
21571a7a1Sriastradh 
31571a7a1Sriastradh /*
41571a7a1Sriastradh  * Copyright (C) 2016 Samsung Electronics Co.Ltd
51571a7a1Sriastradh  * Authors:
61571a7a1Sriastradh  *	Marek Szyprowski <m.szyprowski@samsung.com>
71571a7a1Sriastradh  *
81571a7a1Sriastradh  * DRM core plane blending related functions
91571a7a1Sriastradh  *
101571a7a1Sriastradh  * Permission to use, copy, modify, distribute, and sell this software and its
111571a7a1Sriastradh  * documentation for any purpose is hereby granted without fee, provided that
121571a7a1Sriastradh  * the above copyright notice appear in all copies and that both that copyright
131571a7a1Sriastradh  * notice and this permission notice appear in supporting documentation, and
141571a7a1Sriastradh  * that the name of the copyright holders not be used in advertising or
151571a7a1Sriastradh  * publicity pertaining to distribution of the software without specific,
161571a7a1Sriastradh  * written prior permission.  The copyright holders make no representations
171571a7a1Sriastradh  * about the suitability of this software for any purpose.  It is provided "as
181571a7a1Sriastradh  * is" without express or implied warranty.
191571a7a1Sriastradh  *
201571a7a1Sriastradh  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
211571a7a1Sriastradh  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
221571a7a1Sriastradh  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
231571a7a1Sriastradh  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
241571a7a1Sriastradh  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
251571a7a1Sriastradh  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
261571a7a1Sriastradh  * OF THIS SOFTWARE.
271571a7a1Sriastradh  */
281571a7a1Sriastradh 
291571a7a1Sriastradh #include <sys/cdefs.h>
30*8248f75aSriastradh __KERNEL_RCSID(0, "$NetBSD: drm_blend.c,v 1.4 2021/12/19 00:50:01 riastradh Exp $");
311571a7a1Sriastradh 
321571a7a1Sriastradh #include <linux/export.h>
331571a7a1Sriastradh #include <linux/slab.h>
341571a7a1Sriastradh #include <linux/sort.h>
351571a7a1Sriastradh 
361571a7a1Sriastradh #include <drm/drm_atomic.h>
371571a7a1Sriastradh #include <drm/drm_blend.h>
381571a7a1Sriastradh #include <drm/drm_device.h>
391571a7a1Sriastradh #include <drm/drm_print.h>
401571a7a1Sriastradh 
411571a7a1Sriastradh #include "drm_crtc_internal.h"
421571a7a1Sriastradh 
431571a7a1Sriastradh /**
441571a7a1Sriastradh  * DOC: overview
451571a7a1Sriastradh  *
461571a7a1Sriastradh  * The basic plane composition model supported by standard plane properties only
471571a7a1Sriastradh  * has a source rectangle (in logical pixels within the &drm_framebuffer), with
481571a7a1Sriastradh  * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
491571a7a1Sriastradh  * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
501571a7a1Sriastradh  * defined by the horizontal and vertical visible pixels (stored in @hdisplay
511571a7a1Sriastradh  * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
521571a7a1Sriastradh  * two rectangles are both stored in the &drm_plane_state.
531571a7a1Sriastradh  *
541571a7a1Sriastradh  * For the atomic ioctl the following standard (atomic) properties on the plane object
551571a7a1Sriastradh  * encode the basic plane composition model:
561571a7a1Sriastradh  *
571571a7a1Sriastradh  * SRC_X:
581571a7a1Sriastradh  * 	X coordinate offset for the source rectangle within the
591571a7a1Sriastradh  * 	&drm_framebuffer, in 16.16 fixed point. Must be positive.
601571a7a1Sriastradh  * SRC_Y:
611571a7a1Sriastradh  * 	Y coordinate offset for the source rectangle within the
621571a7a1Sriastradh  * 	&drm_framebuffer, in 16.16 fixed point. Must be positive.
631571a7a1Sriastradh  * SRC_W:
641571a7a1Sriastradh  * 	Width for the source rectangle within the &drm_framebuffer, in 16.16
651571a7a1Sriastradh  * 	fixed point. SRC_X plus SRC_W must be within the width of the source
661571a7a1Sriastradh  * 	framebuffer. Must be positive.
671571a7a1Sriastradh  * SRC_H:
681571a7a1Sriastradh  * 	Height for the source rectangle within the &drm_framebuffer, in 16.16
691571a7a1Sriastradh  * 	fixed point. SRC_Y plus SRC_H must be within the height of the source
701571a7a1Sriastradh  * 	framebuffer. Must be positive.
711571a7a1Sriastradh  * CRTC_X:
721571a7a1Sriastradh  * 	X coordinate offset for the destination rectangle. Can be negative.
731571a7a1Sriastradh  * CRTC_Y:
741571a7a1Sriastradh  * 	Y coordinate offset for the destination rectangle. Can be negative.
751571a7a1Sriastradh  * CRTC_W:
761571a7a1Sriastradh  * 	Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
771571a7a1Sriastradh  * 	the currently visible horizontal area of the &drm_crtc.
781571a7a1Sriastradh  * CRTC_H:
791571a7a1Sriastradh  * 	Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
801571a7a1Sriastradh  * 	the currently visible vertical area of the &drm_crtc.
811571a7a1Sriastradh  * FB_ID:
821571a7a1Sriastradh  * 	Mode object ID of the &drm_framebuffer this plane should scan out.
831571a7a1Sriastradh  * CRTC_ID:
841571a7a1Sriastradh  * 	Mode object ID of the &drm_crtc this plane should be connected to.
851571a7a1Sriastradh  *
861571a7a1Sriastradh  * Note that the source rectangle must fully lie within the bounds of the
871571a7a1Sriastradh  * &drm_framebuffer. The destination rectangle can lie outside of the visible
881571a7a1Sriastradh  * area of the current mode of the CRTC. It must be apprpriately clipped by the
891571a7a1Sriastradh  * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
901571a7a1Sriastradh  * are also allowed to round the subpixel sampling positions appropriately, but
911571a7a1Sriastradh  * only to the next full pixel. No pixel outside of the source rectangle may
921571a7a1Sriastradh  * ever be sampled, which is important when applying more sophisticated
931571a7a1Sriastradh  * filtering than just a bilinear one when scaling. The filtering mode when
941571a7a1Sriastradh  * scaling is unspecified.
951571a7a1Sriastradh  *
961571a7a1Sriastradh  * On top of this basic transformation additional properties can be exposed by
971571a7a1Sriastradh  * the driver:
981571a7a1Sriastradh  *
991571a7a1Sriastradh  * alpha:
1001571a7a1Sriastradh  * 	Alpha is setup with drm_plane_create_alpha_property(). It controls the
1011571a7a1Sriastradh  * 	plane-wide opacity, from transparent (0) to opaque (0xffff). It can be
1021571a7a1Sriastradh  * 	combined with pixel alpha.
1031571a7a1Sriastradh  *	The pixel values in the framebuffers are expected to not be
1041571a7a1Sriastradh  *	pre-multiplied by the global alpha associated to the plane.
1051571a7a1Sriastradh  *
1061571a7a1Sriastradh  * rotation:
1071571a7a1Sriastradh  *	Rotation is set up with drm_plane_create_rotation_property(). It adds a
1081571a7a1Sriastradh  *	rotation and reflection step between the source and destination rectangles.
1091571a7a1Sriastradh  *	Without this property the rectangle is only scaled, but not rotated or
1101571a7a1Sriastradh  *	reflected.
1111571a7a1Sriastradh  *
1121571a7a1Sriastradh  *	Possbile values:
1131571a7a1Sriastradh  *
1141571a7a1Sriastradh  *	"rotate-<degrees>":
1151571a7a1Sriastradh  *		Signals that a drm plane is rotated <degrees> degrees in counter
1161571a7a1Sriastradh  *		clockwise direction.
1171571a7a1Sriastradh  *
1181571a7a1Sriastradh  *	"reflect-<axis>":
1191571a7a1Sriastradh  *		Signals that the contents of a drm plane is reflected along the
1201571a7a1Sriastradh  *		<axis> axis, in the same way as mirroring.
1211571a7a1Sriastradh  *
1221571a7a1Sriastradh  *	reflect-x::
1231571a7a1Sriastradh  *
1241571a7a1Sriastradh  *			|o |    | o|
1251571a7a1Sriastradh  *			|  | -> |  |
1261571a7a1Sriastradh  *			| v|    |v |
1271571a7a1Sriastradh  *
1281571a7a1Sriastradh  *	reflect-y::
1291571a7a1Sriastradh  *
1301571a7a1Sriastradh  *			|o |    | ^|
1311571a7a1Sriastradh  *			|  | -> |  |
1321571a7a1Sriastradh  *			| v|    |o |
1331571a7a1Sriastradh  *
1341571a7a1Sriastradh  * zpos:
1351571a7a1Sriastradh  *	Z position is set up with drm_plane_create_zpos_immutable_property() and
1361571a7a1Sriastradh  *	drm_plane_create_zpos_property(). It controls the visibility of overlapping
1371571a7a1Sriastradh  *	planes. Without this property the primary plane is always below the cursor
1381571a7a1Sriastradh  *	plane, and ordering between all other planes is undefined. The positive
1391571a7a1Sriastradh  *	Z axis points towards the user, i.e. planes with lower Z position values
1401571a7a1Sriastradh  *	are underneath planes with higher Z position values. Two planes with the
1411571a7a1Sriastradh  *	same Z position value have undefined ordering. Note that the Z position
1421571a7a1Sriastradh  *	value can also be immutable, to inform userspace about the hard-coded
1431571a7a1Sriastradh  *	stacking of planes, see drm_plane_create_zpos_immutable_property().
1441571a7a1Sriastradh  *
1451571a7a1Sriastradh  * pixel blend mode:
1461571a7a1Sriastradh  *	Pixel blend mode is set up with drm_plane_create_blend_mode_property().
1471571a7a1Sriastradh  *	It adds a blend mode for alpha blending equation selection, describing
1481571a7a1Sriastradh  *	how the pixels from the current plane are composited with the
1491571a7a1Sriastradh  *	background.
1501571a7a1Sriastradh  *
1511571a7a1Sriastradh  *	 Three alpha blending equations are defined:
1521571a7a1Sriastradh  *
1531571a7a1Sriastradh  *	 "None":
1541571a7a1Sriastradh  *		 Blend formula that ignores the pixel alpha::
1551571a7a1Sriastradh  *
1561571a7a1Sriastradh  *			 out.rgb = plane_alpha * fg.rgb +
1571571a7a1Sriastradh  *				 (1 - plane_alpha) * bg.rgb
1581571a7a1Sriastradh  *
1591571a7a1Sriastradh  *	 "Pre-multiplied":
1601571a7a1Sriastradh  *		 Blend formula that assumes the pixel color values
1611571a7a1Sriastradh  *		 have been already pre-multiplied with the alpha
1621571a7a1Sriastradh  *		 channel values::
1631571a7a1Sriastradh  *
1641571a7a1Sriastradh  *			 out.rgb = plane_alpha * fg.rgb +
1651571a7a1Sriastradh  *				 (1 - (plane_alpha * fg.alpha)) * bg.rgb
1661571a7a1Sriastradh  *
1671571a7a1Sriastradh  *	 "Coverage":
1681571a7a1Sriastradh  *		 Blend formula that assumes the pixel color values have not
1691571a7a1Sriastradh  *		 been pre-multiplied and will do so when blending them to the
1701571a7a1Sriastradh  *		 background color values::
1711571a7a1Sriastradh  *
1721571a7a1Sriastradh  *			 out.rgb = plane_alpha * fg.alpha * fg.rgb +
1731571a7a1Sriastradh  *				 (1 - (plane_alpha * fg.alpha)) * bg.rgb
1741571a7a1Sriastradh  *
1751571a7a1Sriastradh  *	 Using the following symbols:
1761571a7a1Sriastradh  *
1771571a7a1Sriastradh  *	 "fg.rgb":
1781571a7a1Sriastradh  *		 Each of the RGB component values from the plane's pixel
1791571a7a1Sriastradh  *	 "fg.alpha":
1801571a7a1Sriastradh  *		 Alpha component value from the plane's pixel. If the plane's
1811571a7a1Sriastradh  *		 pixel format has no alpha component, then this is assumed to be
1821571a7a1Sriastradh  *		 1.0. In these cases, this property has no effect, as all three
1831571a7a1Sriastradh  *		 equations become equivalent.
1841571a7a1Sriastradh  *	 "bg.rgb":
1851571a7a1Sriastradh  *		 Each of the RGB component values from the background
1861571a7a1Sriastradh  *	 "plane_alpha":
1871571a7a1Sriastradh  *		 Plane alpha value set by the plane "alpha" property. If the
1881571a7a1Sriastradh  *		 plane does not expose the "alpha" property, then this is
1891571a7a1Sriastradh  *		 assumed to be 1.0
1901571a7a1Sriastradh  *
1911571a7a1Sriastradh  * Note that all the property extensions described here apply either to the
1921571a7a1Sriastradh  * plane or the CRTC (e.g. for the background color, which currently is not
1931571a7a1Sriastradh  * exposed and assumed to be black).
1941571a7a1Sriastradh  */
1951571a7a1Sriastradh 
1961571a7a1Sriastradh /**
1971571a7a1Sriastradh  * drm_plane_create_alpha_property - create a new alpha property
1981571a7a1Sriastradh  * @plane: drm plane
1991571a7a1Sriastradh  *
2001571a7a1Sriastradh  * This function creates a generic, mutable, alpha property and enables support
2011571a7a1Sriastradh  * for it in the DRM core. It is attached to @plane.
2021571a7a1Sriastradh  *
2031571a7a1Sriastradh  * The alpha property will be allowed to be within the bounds of 0
2041571a7a1Sriastradh  * (transparent) to 0xffff (opaque).
2051571a7a1Sriastradh  *
2061571a7a1Sriastradh  * Returns:
2071571a7a1Sriastradh  * 0 on success, negative error code on failure.
2081571a7a1Sriastradh  */
drm_plane_create_alpha_property(struct drm_plane * plane)2091571a7a1Sriastradh int drm_plane_create_alpha_property(struct drm_plane *plane)
2101571a7a1Sriastradh {
2111571a7a1Sriastradh 	struct drm_property *prop;
2121571a7a1Sriastradh 
2131571a7a1Sriastradh 	prop = drm_property_create_range(plane->dev, 0, "alpha",
2141571a7a1Sriastradh 					 0, DRM_BLEND_ALPHA_OPAQUE);
2151571a7a1Sriastradh 	if (!prop)
2161571a7a1Sriastradh 		return -ENOMEM;
2171571a7a1Sriastradh 
2181571a7a1Sriastradh 	drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
2191571a7a1Sriastradh 	plane->alpha_property = prop;
2201571a7a1Sriastradh 
2211571a7a1Sriastradh 	if (plane->state)
2221571a7a1Sriastradh 		plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
2231571a7a1Sriastradh 
2241571a7a1Sriastradh 	return 0;
2251571a7a1Sriastradh }
2261571a7a1Sriastradh EXPORT_SYMBOL(drm_plane_create_alpha_property);
2271571a7a1Sriastradh 
2281571a7a1Sriastradh /**
2291571a7a1Sriastradh  * drm_plane_create_rotation_property - create a new rotation property
2301571a7a1Sriastradh  * @plane: drm plane
2311571a7a1Sriastradh  * @rotation: initial value of the rotation property
2321571a7a1Sriastradh  * @supported_rotations: bitmask of supported rotations and reflections
2331571a7a1Sriastradh  *
2341571a7a1Sriastradh  * This creates a new property with the selected support for transformations.
2351571a7a1Sriastradh  *
2361571a7a1Sriastradh  * Since a rotation by 180° degress is the same as reflecting both along the x
2371571a7a1Sriastradh  * and the y axis the rotation property is somewhat redundant. Drivers can use
2381571a7a1Sriastradh  * drm_rotation_simplify() to normalize values of this property.
2391571a7a1Sriastradh  *
2401571a7a1Sriastradh  * The property exposed to userspace is a bitmask property (see
2411571a7a1Sriastradh  * drm_property_create_bitmask()) called "rotation" and has the following
2421571a7a1Sriastradh  * bitmask enumaration values:
2431571a7a1Sriastradh  *
2441571a7a1Sriastradh  * DRM_MODE_ROTATE_0:
2451571a7a1Sriastradh  * 	"rotate-0"
2461571a7a1Sriastradh  * DRM_MODE_ROTATE_90:
2471571a7a1Sriastradh  * 	"rotate-90"
2481571a7a1Sriastradh  * DRM_MODE_ROTATE_180:
2491571a7a1Sriastradh  * 	"rotate-180"
2501571a7a1Sriastradh  * DRM_MODE_ROTATE_270:
2511571a7a1Sriastradh  * 	"rotate-270"
2521571a7a1Sriastradh  * DRM_MODE_REFLECT_X:
2531571a7a1Sriastradh  * 	"reflect-x"
2541571a7a1Sriastradh  * DRM_MODE_REFLECT_Y:
2551571a7a1Sriastradh  * 	"reflect-y"
2561571a7a1Sriastradh  *
2571571a7a1Sriastradh  * Rotation is the specified amount in degrees in counter clockwise direction,
2581571a7a1Sriastradh  * the X and Y axis are within the source rectangle, i.e.  the X/Y axis before
2591571a7a1Sriastradh  * rotation. After reflection, the rotation is applied to the image sampled from
2601571a7a1Sriastradh  * the source rectangle, before scaling it to fit the destination rectangle.
2611571a7a1Sriastradh  */
drm_plane_create_rotation_property(struct drm_plane * plane,unsigned int rotation,unsigned int supported_rotations)2621571a7a1Sriastradh int drm_plane_create_rotation_property(struct drm_plane *plane,
2631571a7a1Sriastradh 				       unsigned int rotation,
2641571a7a1Sriastradh 				       unsigned int supported_rotations)
2651571a7a1Sriastradh {
2661571a7a1Sriastradh 	static const struct drm_prop_enum_list props[] = {
2671571a7a1Sriastradh 		{ __builtin_ffs(DRM_MODE_ROTATE_0) - 1,   "rotate-0" },
2681571a7a1Sriastradh 		{ __builtin_ffs(DRM_MODE_ROTATE_90) - 1,  "rotate-90" },
2691571a7a1Sriastradh 		{ __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
2701571a7a1Sriastradh 		{ __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
2711571a7a1Sriastradh 		{ __builtin_ffs(DRM_MODE_REFLECT_X) - 1,  "reflect-x" },
2721571a7a1Sriastradh 		{ __builtin_ffs(DRM_MODE_REFLECT_Y) - 1,  "reflect-y" },
2731571a7a1Sriastradh 	};
2741571a7a1Sriastradh 	struct drm_property *prop;
2751571a7a1Sriastradh 
2761571a7a1Sriastradh 	WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
2771571a7a1Sriastradh 	WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
2781571a7a1Sriastradh 	WARN_ON(rotation & ~supported_rotations);
2791571a7a1Sriastradh 
2801571a7a1Sriastradh 	prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
2811571a7a1Sriastradh 					   props, ARRAY_SIZE(props),
2821571a7a1Sriastradh 					   supported_rotations);
2831571a7a1Sriastradh 	if (!prop)
2841571a7a1Sriastradh 		return -ENOMEM;
2851571a7a1Sriastradh 
2861571a7a1Sriastradh 	drm_object_attach_property(&plane->base, prop, rotation);
2871571a7a1Sriastradh 
2881571a7a1Sriastradh 	if (plane->state)
2891571a7a1Sriastradh 		plane->state->rotation = rotation;
2901571a7a1Sriastradh 
2911571a7a1Sriastradh 	plane->rotation_property = prop;
2921571a7a1Sriastradh 
2931571a7a1Sriastradh 	return 0;
2941571a7a1Sriastradh }
2951571a7a1Sriastradh EXPORT_SYMBOL(drm_plane_create_rotation_property);
2961571a7a1Sriastradh 
2971571a7a1Sriastradh /**
2981571a7a1Sriastradh  * drm_rotation_simplify() - Try to simplify the rotation
2991571a7a1Sriastradh  * @rotation: Rotation to be simplified
3001571a7a1Sriastradh  * @supported_rotations: Supported rotations
3011571a7a1Sriastradh  *
3021571a7a1Sriastradh  * Attempt to simplify the rotation to a form that is supported.
3031571a7a1Sriastradh  * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
3041571a7a1Sriastradh  * one could call this function like this:
3051571a7a1Sriastradh  *
3061571a7a1Sriastradh  * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
3071571a7a1Sriastradh  *                       DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
3081571a7a1Sriastradh  *                       DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
3091571a7a1Sriastradh  *
3101571a7a1Sriastradh  * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
3111571a7a1Sriastradh  * transforms the hardware supports, this function may not
3121571a7a1Sriastradh  * be able to produce a supported transform, so the caller should
3131571a7a1Sriastradh  * check the result afterwards.
3141571a7a1Sriastradh  */
drm_rotation_simplify(unsigned int rotation,unsigned int supported_rotations)3151571a7a1Sriastradh unsigned int drm_rotation_simplify(unsigned int rotation,
3161571a7a1Sriastradh 				   unsigned int supported_rotations)
3171571a7a1Sriastradh {
3181571a7a1Sriastradh 	if (rotation & ~supported_rotations) {
3191571a7a1Sriastradh 		rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
3201571a7a1Sriastradh 		rotation = (rotation & DRM_MODE_REFLECT_MASK) |
3211571a7a1Sriastradh 		           BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
3221571a7a1Sriastradh 		           % 4);
3231571a7a1Sriastradh 	}
3241571a7a1Sriastradh 
3251571a7a1Sriastradh 	return rotation;
3261571a7a1Sriastradh }
3271571a7a1Sriastradh EXPORT_SYMBOL(drm_rotation_simplify);
3281571a7a1Sriastradh 
3291571a7a1Sriastradh /**
3301571a7a1Sriastradh  * drm_plane_create_zpos_property - create mutable zpos property
3311571a7a1Sriastradh  * @plane: drm plane
3321571a7a1Sriastradh  * @zpos: initial value of zpos property
3331571a7a1Sriastradh  * @min: minimal possible value of zpos property
3341571a7a1Sriastradh  * @max: maximal possible value of zpos property
3351571a7a1Sriastradh  *
3361571a7a1Sriastradh  * This function initializes generic mutable zpos property and enables support
3371571a7a1Sriastradh  * for it in drm core. Drivers can then attach this property to planes to enable
3381571a7a1Sriastradh  * support for configurable planes arrangement during blending operation.
3391571a7a1Sriastradh  * Drivers that attach a mutable zpos property to any plane should call the
3401571a7a1Sriastradh  * drm_atomic_normalize_zpos() helper during their implementation of
3411571a7a1Sriastradh  * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
3421571a7a1Sriastradh  * values and store them in &drm_plane_state.normalized_zpos. Usually min
3431571a7a1Sriastradh  * should be set to 0 and max to maximal number of planes for given crtc - 1.
3441571a7a1Sriastradh  *
3451571a7a1Sriastradh  * If zpos of some planes cannot be changed (like fixed background or
3461571a7a1Sriastradh  * cursor/topmost planes), driver should adjust min/max values and assign those
3471571a7a1Sriastradh  * planes immutable zpos property with lower or higher values (for more
3481571a7a1Sriastradh  * information, see drm_plane_create_zpos_immutable_property() function). In such
3491571a7a1Sriastradh  * case driver should also assign proper initial zpos values for all planes in
3501571a7a1Sriastradh  * its plane_reset() callback, so the planes will be always sorted properly.
3511571a7a1Sriastradh  *
3521571a7a1Sriastradh  * See also drm_atomic_normalize_zpos().
3531571a7a1Sriastradh  *
3541571a7a1Sriastradh  * The property exposed to userspace is called "zpos".
3551571a7a1Sriastradh  *
3561571a7a1Sriastradh  * Returns:
3571571a7a1Sriastradh  * Zero on success, negative errno on failure.
3581571a7a1Sriastradh  */
drm_plane_create_zpos_property(struct drm_plane * plane,unsigned int zpos,unsigned int min,unsigned int max)3591571a7a1Sriastradh int drm_plane_create_zpos_property(struct drm_plane *plane,
3601571a7a1Sriastradh 				   unsigned int zpos,
3611571a7a1Sriastradh 				   unsigned int min, unsigned int max)
3621571a7a1Sriastradh {
3631571a7a1Sriastradh 	struct drm_property *prop;
3641571a7a1Sriastradh 
3651571a7a1Sriastradh 	prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
3661571a7a1Sriastradh 	if (!prop)
3671571a7a1Sriastradh 		return -ENOMEM;
3681571a7a1Sriastradh 
3691571a7a1Sriastradh 	drm_object_attach_property(&plane->base, prop, zpos);
3701571a7a1Sriastradh 
3711571a7a1Sriastradh 	plane->zpos_property = prop;
3721571a7a1Sriastradh 
3731571a7a1Sriastradh 	if (plane->state) {
3741571a7a1Sriastradh 		plane->state->zpos = zpos;
3751571a7a1Sriastradh 		plane->state->normalized_zpos = zpos;
3761571a7a1Sriastradh 	}
3771571a7a1Sriastradh 
3781571a7a1Sriastradh 	return 0;
3791571a7a1Sriastradh }
3801571a7a1Sriastradh EXPORT_SYMBOL(drm_plane_create_zpos_property);
3811571a7a1Sriastradh 
3821571a7a1Sriastradh /**
3831571a7a1Sriastradh  * drm_plane_create_zpos_immutable_property - create immuttable zpos property
3841571a7a1Sriastradh  * @plane: drm plane
3851571a7a1Sriastradh  * @zpos: value of zpos property
3861571a7a1Sriastradh  *
3871571a7a1Sriastradh  * This function initializes generic immutable zpos property and enables
3881571a7a1Sriastradh  * support for it in drm core. Using this property driver lets userspace
3891571a7a1Sriastradh  * to get the arrangement of the planes for blending operation and notifies
3901571a7a1Sriastradh  * it that the hardware (or driver) doesn't support changing of the planes'
3911571a7a1Sriastradh  * order. For mutable zpos see drm_plane_create_zpos_property().
3921571a7a1Sriastradh  *
3931571a7a1Sriastradh  * The property exposed to userspace is called "zpos".
3941571a7a1Sriastradh  *
3951571a7a1Sriastradh  * Returns:
3961571a7a1Sriastradh  * Zero on success, negative errno on failure.
3971571a7a1Sriastradh  */
drm_plane_create_zpos_immutable_property(struct drm_plane * plane,unsigned int zpos)3981571a7a1Sriastradh int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
3991571a7a1Sriastradh 					     unsigned int zpos)
4001571a7a1Sriastradh {
4011571a7a1Sriastradh 	struct drm_property *prop;
4021571a7a1Sriastradh 
4031571a7a1Sriastradh 	prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
4041571a7a1Sriastradh 					 "zpos", zpos, zpos);
4051571a7a1Sriastradh 	if (!prop)
4061571a7a1Sriastradh 		return -ENOMEM;
4071571a7a1Sriastradh 
4081571a7a1Sriastradh 	drm_object_attach_property(&plane->base, prop, zpos);
4091571a7a1Sriastradh 
4101571a7a1Sriastradh 	plane->zpos_property = prop;
4111571a7a1Sriastradh 
4121571a7a1Sriastradh 	if (plane->state) {
4131571a7a1Sriastradh 		plane->state->zpos = zpos;
4141571a7a1Sriastradh 		plane->state->normalized_zpos = zpos;
4151571a7a1Sriastradh 	}
4161571a7a1Sriastradh 
4171571a7a1Sriastradh 	return 0;
4181571a7a1Sriastradh }
4191571a7a1Sriastradh EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
4201571a7a1Sriastradh 
drm_atomic_state_zpos_cmp(const void * a,const void * b)4211571a7a1Sriastradh static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
4221571a7a1Sriastradh {
42310142403Sriastradh 	const struct drm_plane_state *sa = *(struct drm_plane_state *const *)a;
42410142403Sriastradh 	const struct drm_plane_state *sb = *(struct drm_plane_state *const *)b;
4251571a7a1Sriastradh 
4261571a7a1Sriastradh 	if (sa->zpos != sb->zpos)
4271571a7a1Sriastradh 		return sa->zpos - sb->zpos;
4281571a7a1Sriastradh 	else
4291571a7a1Sriastradh 		return sa->plane->base.id - sb->plane->base.id;
4301571a7a1Sriastradh }
4311571a7a1Sriastradh 
drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)4321571a7a1Sriastradh static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
4331571a7a1Sriastradh 					  struct drm_crtc_state *crtc_state)
4341571a7a1Sriastradh {
4351571a7a1Sriastradh 	struct drm_atomic_state *state = crtc_state->state;
4361571a7a1Sriastradh 	struct drm_device *dev = crtc->dev;
4371571a7a1Sriastradh 	int total_planes = dev->mode_config.num_total_plane;
4381571a7a1Sriastradh 	struct drm_plane_state **states;
4391571a7a1Sriastradh 	struct drm_plane *plane;
4401571a7a1Sriastradh 	int i, n = 0;
4411571a7a1Sriastradh 	int ret = 0;
4421571a7a1Sriastradh 
4431571a7a1Sriastradh 	DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
4441571a7a1Sriastradh 			 crtc->base.id, crtc->name);
4451571a7a1Sriastradh 
4461571a7a1Sriastradh 	states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
4471571a7a1Sriastradh 	if (!states)
4481571a7a1Sriastradh 		return -ENOMEM;
4491571a7a1Sriastradh 
4501571a7a1Sriastradh 	/*
4511571a7a1Sriastradh 	 * Normalization process might create new states for planes which
4521571a7a1Sriastradh 	 * normalized_zpos has to be recalculated.
4531571a7a1Sriastradh 	 */
4541571a7a1Sriastradh 	drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
4551571a7a1Sriastradh 		struct drm_plane_state *plane_state =
4561571a7a1Sriastradh 			drm_atomic_get_plane_state(state, plane);
4571571a7a1Sriastradh 		if (IS_ERR(plane_state)) {
4581571a7a1Sriastradh 			ret = PTR_ERR(plane_state);
4591571a7a1Sriastradh 			goto done;
4601571a7a1Sriastradh 		}
4611571a7a1Sriastradh 		states[n++] = plane_state;
4621571a7a1Sriastradh 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
4631571a7a1Sriastradh 				 plane->base.id, plane->name,
4641571a7a1Sriastradh 				 plane_state->zpos);
4651571a7a1Sriastradh 	}
4661571a7a1Sriastradh 
4671571a7a1Sriastradh 	sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
4681571a7a1Sriastradh 
4691571a7a1Sriastradh 	for (i = 0; i < n; i++) {
4701571a7a1Sriastradh 		plane = states[i]->plane;
4711571a7a1Sriastradh 
4721571a7a1Sriastradh 		states[i]->normalized_zpos = i;
4731571a7a1Sriastradh 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
4741571a7a1Sriastradh 				 plane->base.id, plane->name, i);
4751571a7a1Sriastradh 	}
4761571a7a1Sriastradh 	crtc_state->zpos_changed = true;
4771571a7a1Sriastradh 
4781571a7a1Sriastradh done:
4791571a7a1Sriastradh 	kfree(states);
4801571a7a1Sriastradh 	return ret;
4811571a7a1Sriastradh }
4821571a7a1Sriastradh 
4831571a7a1Sriastradh /**
4841571a7a1Sriastradh  * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
4851571a7a1Sriastradh  * @dev: DRM device
4861571a7a1Sriastradh  * @state: atomic state of DRM device
4871571a7a1Sriastradh  *
4881571a7a1Sriastradh  * This function calculates normalized zpos value for all modified planes in
4891571a7a1Sriastradh  * the provided atomic state of DRM device.
4901571a7a1Sriastradh  *
4911571a7a1Sriastradh  * For every CRTC this function checks new states of all planes assigned to
4921571a7a1Sriastradh  * it and calculates normalized zpos value for these planes. Planes are compared
4931571a7a1Sriastradh  * first by their zpos values, then by plane id (if zpos is equal). The plane
4941571a7a1Sriastradh  * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
4951571a7a1Sriastradh  * is then filled with unique values from 0 to number of active planes in crtc
4961571a7a1Sriastradh  * minus one.
4971571a7a1Sriastradh  *
4981571a7a1Sriastradh  * RETURNS
4991571a7a1Sriastradh  * Zero for success or -errno
5001571a7a1Sriastradh  */
drm_atomic_normalize_zpos(struct drm_device * dev,struct drm_atomic_state * state)5011571a7a1Sriastradh int drm_atomic_normalize_zpos(struct drm_device *dev,
5021571a7a1Sriastradh 			      struct drm_atomic_state *state)
5031571a7a1Sriastradh {
5041571a7a1Sriastradh 	struct drm_crtc *crtc;
5051571a7a1Sriastradh 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
506*8248f75aSriastradh 	struct drm_plane *plane __unused;
5071571a7a1Sriastradh 	struct drm_plane_state *old_plane_state, *new_plane_state;
5081571a7a1Sriastradh 	int i, ret = 0;
5091571a7a1Sriastradh 
5101571a7a1Sriastradh 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
5111571a7a1Sriastradh 		crtc = new_plane_state->crtc;
5121571a7a1Sriastradh 		if (!crtc)
5131571a7a1Sriastradh 			continue;
5141571a7a1Sriastradh 		if (old_plane_state->zpos != new_plane_state->zpos) {
5151571a7a1Sriastradh 			new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
5161571a7a1Sriastradh 			new_crtc_state->zpos_changed = true;
5171571a7a1Sriastradh 		}
5181571a7a1Sriastradh 	}
5191571a7a1Sriastradh 
5201571a7a1Sriastradh 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5211571a7a1Sriastradh 		if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
5221571a7a1Sriastradh 		    new_crtc_state->zpos_changed) {
5231571a7a1Sriastradh 			ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
5241571a7a1Sriastradh 								    new_crtc_state);
5251571a7a1Sriastradh 			if (ret)
5261571a7a1Sriastradh 				return ret;
5271571a7a1Sriastradh 		}
5281571a7a1Sriastradh 	}
5291571a7a1Sriastradh 	return 0;
5301571a7a1Sriastradh }
5311571a7a1Sriastradh EXPORT_SYMBOL(drm_atomic_normalize_zpos);
5321571a7a1Sriastradh 
5331571a7a1Sriastradh /**
5341571a7a1Sriastradh  * drm_plane_create_blend_mode_property - create a new blend mode property
5351571a7a1Sriastradh  * @plane: drm plane
5361571a7a1Sriastradh  * @supported_modes: bitmask of supported modes, must include
5371571a7a1Sriastradh  *		     BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
5381571a7a1Sriastradh  *		     that alpha is premultiplied, and old userspace can break if
5391571a7a1Sriastradh  *		     the property defaults to anything else.
5401571a7a1Sriastradh  *
5411571a7a1Sriastradh  * This creates a new property describing the blend mode.
5421571a7a1Sriastradh  *
5431571a7a1Sriastradh  * The property exposed to userspace is an enumeration property (see
5441571a7a1Sriastradh  * drm_property_create_enum()) called "pixel blend mode" and has the
5451571a7a1Sriastradh  * following enumeration values:
5461571a7a1Sriastradh  *
5471571a7a1Sriastradh  * "None":
5481571a7a1Sriastradh  *	Blend formula that ignores the pixel alpha.
5491571a7a1Sriastradh  *
5501571a7a1Sriastradh  * "Pre-multiplied":
5511571a7a1Sriastradh  *	Blend formula that assumes the pixel color values have been already
5521571a7a1Sriastradh  *	pre-multiplied with the alpha channel values.
5531571a7a1Sriastradh  *
5541571a7a1Sriastradh  * "Coverage":
5551571a7a1Sriastradh  *	Blend formula that assumes the pixel color values have not been
5561571a7a1Sriastradh  *	pre-multiplied and will do so when blending them to the background color
5571571a7a1Sriastradh  *	values.
5581571a7a1Sriastradh  *
5591571a7a1Sriastradh  * RETURNS:
5601571a7a1Sriastradh  * Zero for success or -errno
5611571a7a1Sriastradh  */
drm_plane_create_blend_mode_property(struct drm_plane * plane,unsigned int supported_modes)5621571a7a1Sriastradh int drm_plane_create_blend_mode_property(struct drm_plane *plane,
5631571a7a1Sriastradh 					 unsigned int supported_modes)
5641571a7a1Sriastradh {
5651571a7a1Sriastradh 	struct drm_device *dev = plane->dev;
5661571a7a1Sriastradh 	struct drm_property *prop;
5671571a7a1Sriastradh 	static const struct drm_prop_enum_list props[] = {
5681571a7a1Sriastradh 		{ DRM_MODE_BLEND_PIXEL_NONE, "None" },
5691571a7a1Sriastradh 		{ DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
5701571a7a1Sriastradh 		{ DRM_MODE_BLEND_COVERAGE, "Coverage" },
5711571a7a1Sriastradh 	};
5721571a7a1Sriastradh 	unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
5731571a7a1Sriastradh 				       BIT(DRM_MODE_BLEND_PREMULTI)   |
5741571a7a1Sriastradh 				       BIT(DRM_MODE_BLEND_COVERAGE);
5751571a7a1Sriastradh 	int i;
5761571a7a1Sriastradh 
5771571a7a1Sriastradh 	if (WARN_ON((supported_modes & ~valid_mode_mask) ||
5781571a7a1Sriastradh 		    ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
5791571a7a1Sriastradh 		return -EINVAL;
5801571a7a1Sriastradh 
5811571a7a1Sriastradh 	prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
5821571a7a1Sriastradh 				   "pixel blend mode",
5831571a7a1Sriastradh 				   hweight32(supported_modes));
5841571a7a1Sriastradh 	if (!prop)
5851571a7a1Sriastradh 		return -ENOMEM;
5861571a7a1Sriastradh 
5871571a7a1Sriastradh 	for (i = 0; i < ARRAY_SIZE(props); i++) {
5881571a7a1Sriastradh 		int ret;
5891571a7a1Sriastradh 
5901571a7a1Sriastradh 		if (!(BIT(props[i].type) & supported_modes))
5911571a7a1Sriastradh 			continue;
5921571a7a1Sriastradh 
5931571a7a1Sriastradh 		ret = drm_property_add_enum(prop, props[i].type,
5941571a7a1Sriastradh 					    props[i].name);
5951571a7a1Sriastradh 
5961571a7a1Sriastradh 		if (ret) {
5971571a7a1Sriastradh 			drm_property_destroy(dev, prop);
5981571a7a1Sriastradh 
5991571a7a1Sriastradh 			return ret;
6001571a7a1Sriastradh 		}
6011571a7a1Sriastradh 	}
6021571a7a1Sriastradh 
6031571a7a1Sriastradh 	drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
6041571a7a1Sriastradh 	plane->blend_mode_property = prop;
6051571a7a1Sriastradh 
6061571a7a1Sriastradh 	return 0;
6071571a7a1Sriastradh }
6081571a7a1Sriastradh EXPORT_SYMBOL(drm_plane_create_blend_mode_property);
609