xref: /openbmc/linux/drivers/gpu/drm/drm_blend.c (revision fc2602b5)
144d1240dSMarek Szyprowski /*
244d1240dSMarek Szyprowski  * Copyright (C) 2016 Samsung Electronics Co.Ltd
344d1240dSMarek Szyprowski  * Authors:
444d1240dSMarek Szyprowski  *	Marek Szyprowski <m.szyprowski@samsung.com>
544d1240dSMarek Szyprowski  *
644d1240dSMarek Szyprowski  * DRM core plane blending related functions
744d1240dSMarek Szyprowski  *
844d1240dSMarek Szyprowski  * Permission to use, copy, modify, distribute, and sell this software and its
944d1240dSMarek Szyprowski  * documentation for any purpose is hereby granted without fee, provided that
1044d1240dSMarek Szyprowski  * the above copyright notice appear in all copies and that both that copyright
1144d1240dSMarek Szyprowski  * notice and this permission notice appear in supporting documentation, and
1244d1240dSMarek Szyprowski  * that the name of the copyright holders not be used in advertising or
1344d1240dSMarek Szyprowski  * publicity pertaining to distribution of the software without specific,
1444d1240dSMarek Szyprowski  * written prior permission.  The copyright holders make no representations
1544d1240dSMarek Szyprowski  * about the suitability of this software for any purpose.  It is provided "as
1644d1240dSMarek Szyprowski  * is" without express or implied warranty.
1744d1240dSMarek Szyprowski  *
1844d1240dSMarek Szyprowski  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1944d1240dSMarek Szyprowski  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
2044d1240dSMarek Szyprowski  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
2144d1240dSMarek Szyprowski  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
2244d1240dSMarek Szyprowski  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
2344d1240dSMarek Szyprowski  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2444d1240dSMarek Szyprowski  * OF THIS SOFTWARE.
2544d1240dSMarek Szyprowski  */
260500c04eSSam Ravnborg 
2744d1240dSMarek Szyprowski #include <linux/export.h>
2844d1240dSMarek Szyprowski #include <linux/slab.h>
2944d1240dSMarek Szyprowski #include <linux/sort.h>
3044d1240dSMarek Szyprowski 
310500c04eSSam Ravnborg #include <drm/drm_atomic.h>
320500c04eSSam Ravnborg #include <drm/drm_blend.h>
330500c04eSSam Ravnborg #include <drm/drm_device.h>
340500c04eSSam Ravnborg #include <drm/drm_print.h>
350500c04eSSam Ravnborg 
36c30b4400SVille Syrjälä #include "drm_crtc_internal.h"
3744d1240dSMarek Szyprowski 
381e4d84c6SDaniel Vetter /**
391e4d84c6SDaniel Vetter  * DOC: overview
401e4d84c6SDaniel Vetter  *
411e4d84c6SDaniel Vetter  * The basic plane composition model supported by standard plane properties only
421e4d84c6SDaniel Vetter  * has a source rectangle (in logical pixels within the &drm_framebuffer), with
431e4d84c6SDaniel Vetter  * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
441e4d84c6SDaniel Vetter  * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
451e4d84c6SDaniel Vetter  * defined by the horizontal and vertical visible pixels (stored in @hdisplay
46d574528aSDaniel Vetter  * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
47d574528aSDaniel Vetter  * two rectangles are both stored in the &drm_plane_state.
481e4d84c6SDaniel Vetter  *
491e4d84c6SDaniel Vetter  * For the atomic ioctl the following standard (atomic) properties on the plane object
501e4d84c6SDaniel Vetter  * encode the basic plane composition model:
511e4d84c6SDaniel Vetter  *
521e4d84c6SDaniel Vetter  * SRC_X:
531e4d84c6SDaniel Vetter  * 	X coordinate offset for the source rectangle within the
541e4d84c6SDaniel Vetter  * 	&drm_framebuffer, in 16.16 fixed point. Must be positive.
551e4d84c6SDaniel Vetter  * SRC_Y:
561e4d84c6SDaniel Vetter  * 	Y coordinate offset for the source rectangle within the
571e4d84c6SDaniel Vetter  * 	&drm_framebuffer, in 16.16 fixed point. Must be positive.
581e4d84c6SDaniel Vetter  * SRC_W:
591e4d84c6SDaniel Vetter  * 	Width for the source rectangle within the &drm_framebuffer, in 16.16
601e4d84c6SDaniel Vetter  * 	fixed point. SRC_X plus SRC_W must be within the width of the source
611e4d84c6SDaniel Vetter  * 	framebuffer. Must be positive.
621e4d84c6SDaniel Vetter  * SRC_H:
631e4d84c6SDaniel Vetter  * 	Height for the source rectangle within the &drm_framebuffer, in 16.16
641e4d84c6SDaniel Vetter  * 	fixed point. SRC_Y plus SRC_H must be within the height of the source
651e4d84c6SDaniel Vetter  * 	framebuffer. Must be positive.
661e4d84c6SDaniel Vetter  * CRTC_X:
671e4d84c6SDaniel Vetter  * 	X coordinate offset for the destination rectangle. Can be negative.
681e4d84c6SDaniel Vetter  * CRTC_Y:
691e4d84c6SDaniel Vetter  * 	Y coordinate offset for the destination rectangle. Can be negative.
701e4d84c6SDaniel Vetter  * CRTC_W:
711e4d84c6SDaniel Vetter  * 	Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
721e4d84c6SDaniel Vetter  * 	the currently visible horizontal area of the &drm_crtc.
731e4d84c6SDaniel Vetter  * CRTC_H:
741e4d84c6SDaniel Vetter  * 	Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
751e4d84c6SDaniel Vetter  * 	the currently visible vertical area of the &drm_crtc.
761e4d84c6SDaniel Vetter  * FB_ID:
771e4d84c6SDaniel Vetter  * 	Mode object ID of the &drm_framebuffer this plane should scan out.
781e4d84c6SDaniel Vetter  * CRTC_ID:
791e4d84c6SDaniel Vetter  * 	Mode object ID of the &drm_crtc this plane should be connected to.
801e4d84c6SDaniel Vetter  *
811e4d84c6SDaniel Vetter  * Note that the source rectangle must fully lie within the bounds of the
821e4d84c6SDaniel Vetter  * &drm_framebuffer. The destination rectangle can lie outside of the visible
83c8337569SMarek Vasut  * area of the current mode of the CRTC. It must be appropriately clipped by the
841e4d84c6SDaniel Vetter  * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
851e4d84c6SDaniel Vetter  * are also allowed to round the subpixel sampling positions appropriately, but
861e4d84c6SDaniel Vetter  * only to the next full pixel. No pixel outside of the source rectangle may
871e4d84c6SDaniel Vetter  * ever be sampled, which is important when applying more sophisticated
881e4d84c6SDaniel Vetter  * filtering than just a bilinear one when scaling. The filtering mode when
891e4d84c6SDaniel Vetter  * scaling is unspecified.
901e4d84c6SDaniel Vetter  *
911e4d84c6SDaniel Vetter  * On top of this basic transformation additional properties can be exposed by
921e4d84c6SDaniel Vetter  * the driver:
931e4d84c6SDaniel Vetter  *
94ae0e2826SMaxime Ripard  * alpha:
95ae0e2826SMaxime Ripard  * 	Alpha is setup with drm_plane_create_alpha_property(). It controls the
96ae0e2826SMaxime Ripard  * 	plane-wide opacity, from transparent (0) to opaque (0xffff). It can be
97ae0e2826SMaxime Ripard  * 	combined with pixel alpha.
98ae0e2826SMaxime Ripard  *	The pixel values in the framebuffers are expected to not be
99ae0e2826SMaxime Ripard  *	pre-multiplied by the global alpha associated to the plane.
100ae0e2826SMaxime Ripard  *
1014d10bd45SDaniel Vetter  * rotation:
1024d10bd45SDaniel Vetter  *	Rotation is set up with drm_plane_create_rotation_property(). It adds a
1031e4d84c6SDaniel Vetter  *	rotation and reflection step between the source and destination rectangles.
1041e4d84c6SDaniel Vetter  *	Without this property the rectangle is only scaled, but not rotated or
1051e4d84c6SDaniel Vetter  *	reflected.
1061e4d84c6SDaniel Vetter  *
1071f86fa15SAlexandru Gheorghe  *	Possbile values:
1081f86fa15SAlexandru Gheorghe  *
1091f86fa15SAlexandru Gheorghe  *	"rotate-<degrees>":
1101f86fa15SAlexandru Gheorghe  *		Signals that a drm plane is rotated <degrees> degrees in counter
1111f86fa15SAlexandru Gheorghe  *		clockwise direction.
1121f86fa15SAlexandru Gheorghe  *
1131f86fa15SAlexandru Gheorghe  *	"reflect-<axis>":
1141f86fa15SAlexandru Gheorghe  *		Signals that the contents of a drm plane is reflected along the
1151f86fa15SAlexandru Gheorghe  *		<axis> axis, in the same way as mirroring.
1161f86fa15SAlexandru Gheorghe  *
1171f86fa15SAlexandru Gheorghe  *	reflect-x::
1181f86fa15SAlexandru Gheorghe  *
1191f86fa15SAlexandru Gheorghe  *			|o |    | o|
1201f86fa15SAlexandru Gheorghe  *			|  | -> |  |
1211f86fa15SAlexandru Gheorghe  *			| v|    |v |
1221f86fa15SAlexandru Gheorghe  *
1231f86fa15SAlexandru Gheorghe  *	reflect-y::
1241f86fa15SAlexandru Gheorghe  *
1251f86fa15SAlexandru Gheorghe  *			|o |    | ^|
1261f86fa15SAlexandru Gheorghe  *			|  | -> |  |
1271f86fa15SAlexandru Gheorghe  *			| v|    |o |
1281f86fa15SAlexandru Gheorghe  *
1294d10bd45SDaniel Vetter  * zpos:
1304d10bd45SDaniel Vetter  *	Z position is set up with drm_plane_create_zpos_immutable_property() and
1311e4d84c6SDaniel Vetter  *	drm_plane_create_zpos_property(). It controls the visibility of overlapping
1321e4d84c6SDaniel Vetter  *	planes. Without this property the primary plane is always below the cursor
13342770cbfSDaniel Vetter  *	plane, and ordering between all other planes is undefined. The positive
13442770cbfSDaniel Vetter  *	Z axis points towards the user, i.e. planes with lower Z position values
1358f6ea27bSSimon Ser  *	are underneath planes with higher Z position values. Two planes with the
1368f6ea27bSSimon Ser  *	same Z position value have undefined ordering. Note that the Z position
1378f6ea27bSSimon Ser  *	value can also be immutable, to inform userspace about the hard-coded
1384dc55525SLaurent Pinchart  *	stacking of planes, see drm_plane_create_zpos_immutable_property(). If
1394dc55525SLaurent Pinchart  *	any plane has a zpos property (either mutable or immutable), then all
1404dc55525SLaurent Pinchart  *	planes shall have a zpos property.
1411e4d84c6SDaniel Vetter  *
142a5ec8332SLowry Li  * pixel blend mode:
143a5ec8332SLowry Li  *	Pixel blend mode is set up with drm_plane_create_blend_mode_property().
144a5ec8332SLowry Li  *	It adds a blend mode for alpha blending equation selection, describing
145a5ec8332SLowry Li  *	how the pixels from the current plane are composited with the
146a5ec8332SLowry Li  *	background.
147a5ec8332SLowry Li  *
148a5ec8332SLowry Li  *	 Three alpha blending equations are defined:
149a5ec8332SLowry Li  *
150a5ec8332SLowry Li  *	 "None":
151a5ec8332SLowry Li  *		 Blend formula that ignores the pixel alpha::
152a5ec8332SLowry Li  *
153a5ec8332SLowry Li  *			 out.rgb = plane_alpha * fg.rgb +
154a5ec8332SLowry Li  *				 (1 - plane_alpha) * bg.rgb
155a5ec8332SLowry Li  *
156a5ec8332SLowry Li  *	 "Pre-multiplied":
157a5ec8332SLowry Li  *		 Blend formula that assumes the pixel color values
158a5ec8332SLowry Li  *		 have been already pre-multiplied with the alpha
159a5ec8332SLowry Li  *		 channel values::
160a5ec8332SLowry Li  *
161a5ec8332SLowry Li  *			 out.rgb = plane_alpha * fg.rgb +
162a5ec8332SLowry Li  *				 (1 - (plane_alpha * fg.alpha)) * bg.rgb
163a5ec8332SLowry Li  *
164a5ec8332SLowry Li  *	 "Coverage":
165a5ec8332SLowry Li  *		 Blend formula that assumes the pixel color values have not
166a5ec8332SLowry Li  *		 been pre-multiplied and will do so when blending them to the
167a5ec8332SLowry Li  *		 background color values::
168a5ec8332SLowry Li  *
169a5ec8332SLowry Li  *			 out.rgb = plane_alpha * fg.alpha * fg.rgb +
170a5ec8332SLowry Li  *				 (1 - (plane_alpha * fg.alpha)) * bg.rgb
171a5ec8332SLowry Li  *
172a5ec8332SLowry Li  *	 Using the following symbols:
173a5ec8332SLowry Li  *
174a5ec8332SLowry Li  *	 "fg.rgb":
175a5ec8332SLowry Li  *		 Each of the RGB component values from the plane's pixel
176a5ec8332SLowry Li  *	 "fg.alpha":
177a5ec8332SLowry Li  *		 Alpha component value from the plane's pixel. If the plane's
178a5ec8332SLowry Li  *		 pixel format has no alpha component, then this is assumed to be
179a5ec8332SLowry Li  *		 1.0. In these cases, this property has no effect, as all three
180a5ec8332SLowry Li  *		 equations become equivalent.
181a5ec8332SLowry Li  *	 "bg.rgb":
182a5ec8332SLowry Li  *		 Each of the RGB component values from the background
183a5ec8332SLowry Li  *	 "plane_alpha":
184a5ec8332SLowry Li  *		 Plane alpha value set by the plane "alpha" property. If the
185a5ec8332SLowry Li  *		 plane does not expose the "alpha" property, then this is
186a5ec8332SLowry Li  *		 assumed to be 1.0
187a5ec8332SLowry Li  *
1881e4d84c6SDaniel Vetter  * Note that all the property extensions described here apply either to the
1891e4d84c6SDaniel Vetter  * plane or the CRTC (e.g. for the background color, which currently is not
1901e4d84c6SDaniel Vetter  * exposed and assumed to be black).
1915c759edaSPankaj Bharadiya  *
1925c759edaSPankaj Bharadiya  * SCALING_FILTER:
1935c759edaSPankaj Bharadiya  *     Indicates scaling filter to be used for plane scaler
1945c759edaSPankaj Bharadiya  *
1955c759edaSPankaj Bharadiya  *     The value of this property can be one of the following:
1961187ffc4SSimon Ser  *
1975c759edaSPankaj Bharadiya  *     Default:
1985c759edaSPankaj Bharadiya  *             Driver's default scaling filter
1995c759edaSPankaj Bharadiya  *     Nearest Neighbor:
2005c759edaSPankaj Bharadiya  *             Nearest Neighbor scaling filter
2015c759edaSPankaj Bharadiya  *
2025c759edaSPankaj Bharadiya  * Drivers can set up this property for a plane by calling
2035c759edaSPankaj Bharadiya  * drm_plane_create_scaling_filter_property
2041e4d84c6SDaniel Vetter  */
2051e4d84c6SDaniel Vetter 
2061e4d84c6SDaniel Vetter /**
207ae0e2826SMaxime Ripard  * drm_plane_create_alpha_property - create a new alpha property
208ae0e2826SMaxime Ripard  * @plane: drm plane
209ae0e2826SMaxime Ripard  *
210ae0e2826SMaxime Ripard  * This function creates a generic, mutable, alpha property and enables support
211ae0e2826SMaxime Ripard  * for it in the DRM core. It is attached to @plane.
212ae0e2826SMaxime Ripard  *
213ae0e2826SMaxime Ripard  * The alpha property will be allowed to be within the bounds of 0
214ae0e2826SMaxime Ripard  * (transparent) to 0xffff (opaque).
215ae0e2826SMaxime Ripard  *
216ae0e2826SMaxime Ripard  * Returns:
217ae0e2826SMaxime Ripard  * 0 on success, negative error code on failure.
218ae0e2826SMaxime Ripard  */
drm_plane_create_alpha_property(struct drm_plane * plane)219ae0e2826SMaxime Ripard int drm_plane_create_alpha_property(struct drm_plane *plane)
220ae0e2826SMaxime Ripard {
221ae0e2826SMaxime Ripard 	struct drm_property *prop;
222ae0e2826SMaxime Ripard 
223ae0e2826SMaxime Ripard 	prop = drm_property_create_range(plane->dev, 0, "alpha",
224ae0e2826SMaxime Ripard 					 0, DRM_BLEND_ALPHA_OPAQUE);
225ae0e2826SMaxime Ripard 	if (!prop)
226ae0e2826SMaxime Ripard 		return -ENOMEM;
227ae0e2826SMaxime Ripard 
228ae0e2826SMaxime Ripard 	drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
229ae0e2826SMaxime Ripard 	plane->alpha_property = prop;
230ae0e2826SMaxime Ripard 
231ae0e2826SMaxime Ripard 	if (plane->state)
232ae0e2826SMaxime Ripard 		plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
233ae0e2826SMaxime Ripard 
234ae0e2826SMaxime Ripard 	return 0;
235ae0e2826SMaxime Ripard }
236ae0e2826SMaxime Ripard EXPORT_SYMBOL(drm_plane_create_alpha_property);
237ae0e2826SMaxime Ripard 
238ae0e2826SMaxime Ripard /**
2396686df8cSVille Syrjälä  * drm_plane_create_rotation_property - create a new rotation property
2406686df8cSVille Syrjälä  * @plane: drm plane
2416686df8cSVille Syrjälä  * @rotation: initial value of the rotation property
2421e4d84c6SDaniel Vetter  * @supported_rotations: bitmask of supported rotations and reflections
2431e4d84c6SDaniel Vetter  *
2441e4d84c6SDaniel Vetter  * This creates a new property with the selected support for transformations.
2451e4d84c6SDaniel Vetter  *
2461e4d84c6SDaniel Vetter  * Since a rotation by 180° degress is the same as reflecting both along the x
2471e4d84c6SDaniel Vetter  * and the y axis the rotation property is somewhat redundant. Drivers can use
2481e4d84c6SDaniel Vetter  * drm_rotation_simplify() to normalize values of this property.
2491e4d84c6SDaniel Vetter  *
2501e4d84c6SDaniel Vetter  * The property exposed to userspace is a bitmask property (see
2511e4d84c6SDaniel Vetter  * drm_property_create_bitmask()) called "rotation" and has the following
2521e4d84c6SDaniel Vetter  * bitmask enumaration values:
2531e4d84c6SDaniel Vetter  *
254c2c446adSRobert Foss  * DRM_MODE_ROTATE_0:
2551e4d84c6SDaniel Vetter  * 	"rotate-0"
256c2c446adSRobert Foss  * DRM_MODE_ROTATE_90:
2571e4d84c6SDaniel Vetter  * 	"rotate-90"
258c2c446adSRobert Foss  * DRM_MODE_ROTATE_180:
2591e4d84c6SDaniel Vetter  * 	"rotate-180"
260c2c446adSRobert Foss  * DRM_MODE_ROTATE_270:
2611e4d84c6SDaniel Vetter  * 	"rotate-270"
262c2c446adSRobert Foss  * DRM_MODE_REFLECT_X:
2631e4d84c6SDaniel Vetter  * 	"reflect-x"
264c2c446adSRobert Foss  * DRM_MODE_REFLECT_Y:
2651e4d84c6SDaniel Vetter  * 	"reflect-y"
2661e4d84c6SDaniel Vetter  *
2671e4d84c6SDaniel Vetter  * Rotation is the specified amount in degrees in counter clockwise direction,
2681e4d84c6SDaniel Vetter  * the X and Y axis are within the source rectangle, i.e.  the X/Y axis before
2691e4d84c6SDaniel Vetter  * rotation. After reflection, the rotation is applied to the image sampled from
2701e4d84c6SDaniel Vetter  * the source rectangle, before scaling it to fit the destination rectangle.
2711e4d84c6SDaniel Vetter  */
drm_plane_create_rotation_property(struct drm_plane * plane,unsigned int rotation,unsigned int supported_rotations)272d138dd3cSVille Syrjälä int drm_plane_create_rotation_property(struct drm_plane *plane,
273d138dd3cSVille Syrjälä 				       unsigned int rotation,
274d138dd3cSVille Syrjälä 				       unsigned int supported_rotations)
275d138dd3cSVille Syrjälä {
276d138dd3cSVille Syrjälä 	static const struct drm_prop_enum_list props[] = {
277c2c446adSRobert Foss 		{ __builtin_ffs(DRM_MODE_ROTATE_0) - 1,   "rotate-0" },
278c2c446adSRobert Foss 		{ __builtin_ffs(DRM_MODE_ROTATE_90) - 1,  "rotate-90" },
279c2c446adSRobert Foss 		{ __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
280c2c446adSRobert Foss 		{ __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
281c2c446adSRobert Foss 		{ __builtin_ffs(DRM_MODE_REFLECT_X) - 1,  "reflect-x" },
282c2c446adSRobert Foss 		{ __builtin_ffs(DRM_MODE_REFLECT_Y) - 1,  "reflect-y" },
283d138dd3cSVille Syrjälä 	};
284d138dd3cSVille Syrjälä 	struct drm_property *prop;
285d138dd3cSVille Syrjälä 
286c2c446adSRobert Foss 	WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
287c2c446adSRobert Foss 	WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
288d138dd3cSVille Syrjälä 	WARN_ON(rotation & ~supported_rotations);
289d138dd3cSVille Syrjälä 
290d138dd3cSVille Syrjälä 	prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
291d138dd3cSVille Syrjälä 					   props, ARRAY_SIZE(props),
292d138dd3cSVille Syrjälä 					   supported_rotations);
293d138dd3cSVille Syrjälä 	if (!prop)
294d138dd3cSVille Syrjälä 		return -ENOMEM;
295d138dd3cSVille Syrjälä 
296d138dd3cSVille Syrjälä 	drm_object_attach_property(&plane->base, prop, rotation);
297d138dd3cSVille Syrjälä 
298d138dd3cSVille Syrjälä 	if (plane->state)
299d138dd3cSVille Syrjälä 		plane->state->rotation = rotation;
300d138dd3cSVille Syrjälä 
301d138dd3cSVille Syrjälä 	plane->rotation_property = prop;
302d138dd3cSVille Syrjälä 
303d138dd3cSVille Syrjälä 	return 0;
304d138dd3cSVille Syrjälä }
305d138dd3cSVille Syrjälä EXPORT_SYMBOL(drm_plane_create_rotation_property);
306d138dd3cSVille Syrjälä 
30718733802SDaniel Vetter /**
30818733802SDaniel Vetter  * drm_rotation_simplify() - Try to simplify the rotation
30918733802SDaniel Vetter  * @rotation: Rotation to be simplified
31018733802SDaniel Vetter  * @supported_rotations: Supported rotations
31118733802SDaniel Vetter  *
31218733802SDaniel Vetter  * Attempt to simplify the rotation to a form that is supported.
313c2c446adSRobert Foss  * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
31418733802SDaniel Vetter  * one could call this function like this:
31518733802SDaniel Vetter  *
316c2c446adSRobert Foss  * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
317c2c446adSRobert Foss  *                       DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
318c2c446adSRobert Foss  *                       DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
31918733802SDaniel Vetter  *
320a4f24adaSDmitry Baryshkov  * to eliminate the DRM_MODE_REFLECT_X flag. Depending on what kind of
32118733802SDaniel Vetter  * transforms the hardware supports, this function may not
32218733802SDaniel Vetter  * be able to produce a supported transform, so the caller should
32318733802SDaniel Vetter  * check the result afterwards.
32418733802SDaniel Vetter  */
drm_rotation_simplify(unsigned int rotation,unsigned int supported_rotations)32518733802SDaniel Vetter unsigned int drm_rotation_simplify(unsigned int rotation,
32618733802SDaniel Vetter 				   unsigned int supported_rotations)
32718733802SDaniel Vetter {
32818733802SDaniel Vetter 	if (rotation & ~supported_rotations) {
329c2c446adSRobert Foss 		rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
330c2c446adSRobert Foss 		rotation = (rotation & DRM_MODE_REFLECT_MASK) |
331c2c446adSRobert Foss 			    BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
332c2c446adSRobert Foss 			    % 4);
33318733802SDaniel Vetter 	}
33418733802SDaniel Vetter 
33518733802SDaniel Vetter 	return rotation;
33618733802SDaniel Vetter }
33718733802SDaniel Vetter EXPORT_SYMBOL(drm_rotation_simplify);
33818733802SDaniel Vetter 
33944d1240dSMarek Szyprowski /**
34044d1240dSMarek Szyprowski  * drm_plane_create_zpos_property - create mutable zpos property
34144d1240dSMarek Szyprowski  * @plane: drm plane
34244d1240dSMarek Szyprowski  * @zpos: initial value of zpos property
34344d1240dSMarek Szyprowski  * @min: minimal possible value of zpos property
34444d1240dSMarek Szyprowski  * @max: maximal possible value of zpos property
34544d1240dSMarek Szyprowski  *
34644d1240dSMarek Szyprowski  * This function initializes generic mutable zpos property and enables support
34744d1240dSMarek Szyprowski  * for it in drm core. Drivers can then attach this property to planes to enable
34844d1240dSMarek Szyprowski  * support for configurable planes arrangement during blending operation.
349ca40cfc8SThierry Reding  * Drivers that attach a mutable zpos property to any plane should call the
350ca40cfc8SThierry Reding  * drm_atomic_normalize_zpos() helper during their implementation of
351ca40cfc8SThierry Reding  * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
352ca40cfc8SThierry Reding  * values and store them in &drm_plane_state.normalized_zpos. Usually min
353ca40cfc8SThierry Reding  * should be set to 0 and max to maximal number of planes for given crtc - 1.
35444d1240dSMarek Szyprowski  *
35544d1240dSMarek Szyprowski  * If zpos of some planes cannot be changed (like fixed background or
3564dc55525SLaurent Pinchart  * cursor/topmost planes), drivers shall adjust the min/max values and assign
3574dc55525SLaurent Pinchart  * those planes immutable zpos properties with lower or higher values (for more
3581e4d84c6SDaniel Vetter  * information, see drm_plane_create_zpos_immutable_property() function). In such
3594dc55525SLaurent Pinchart  * case drivers shall also assign proper initial zpos values for all planes in
36044d1240dSMarek Szyprowski  * its plane_reset() callback, so the planes will be always sorted properly.
36144d1240dSMarek Szyprowski  *
3621e4d84c6SDaniel Vetter  * See also drm_atomic_normalize_zpos().
3631e4d84c6SDaniel Vetter  *
3641e4d84c6SDaniel Vetter  * The property exposed to userspace is called "zpos".
3651e4d84c6SDaniel Vetter  *
36644d1240dSMarek Szyprowski  * Returns:
36744d1240dSMarek Szyprowski  * Zero on success, negative errno on failure.
36844d1240dSMarek Szyprowski  */
drm_plane_create_zpos_property(struct drm_plane * plane,unsigned int zpos,unsigned int min,unsigned int max)36944d1240dSMarek Szyprowski int drm_plane_create_zpos_property(struct drm_plane *plane,
37044d1240dSMarek Szyprowski 				   unsigned int zpos,
37144d1240dSMarek Szyprowski 				   unsigned int min, unsigned int max)
37244d1240dSMarek Szyprowski {
37344d1240dSMarek Szyprowski 	struct drm_property *prop;
37444d1240dSMarek Szyprowski 
37544d1240dSMarek Szyprowski 	prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
37644d1240dSMarek Szyprowski 	if (!prop)
37744d1240dSMarek Szyprowski 		return -ENOMEM;
37844d1240dSMarek Szyprowski 
37944d1240dSMarek Szyprowski 	drm_object_attach_property(&plane->base, prop, zpos);
38044d1240dSMarek Szyprowski 
38144d1240dSMarek Szyprowski 	plane->zpos_property = prop;
38244d1240dSMarek Szyprowski 
38344d1240dSMarek Szyprowski 	if (plane->state) {
38444d1240dSMarek Szyprowski 		plane->state->zpos = zpos;
38544d1240dSMarek Szyprowski 		plane->state->normalized_zpos = zpos;
38644d1240dSMarek Szyprowski 	}
38744d1240dSMarek Szyprowski 
38844d1240dSMarek Szyprowski 	return 0;
38944d1240dSMarek Szyprowski }
39044d1240dSMarek Szyprowski EXPORT_SYMBOL(drm_plane_create_zpos_property);
39144d1240dSMarek Szyprowski 
39244d1240dSMarek Szyprowski /**
39344d1240dSMarek Szyprowski  * drm_plane_create_zpos_immutable_property - create immuttable zpos property
39444d1240dSMarek Szyprowski  * @plane: drm plane
39544d1240dSMarek Szyprowski  * @zpos: value of zpos property
39644d1240dSMarek Szyprowski  *
39744d1240dSMarek Szyprowski  * This function initializes generic immutable zpos property and enables
39844d1240dSMarek Szyprowski  * support for it in drm core. Using this property driver lets userspace
39944d1240dSMarek Szyprowski  * to get the arrangement of the planes for blending operation and notifies
40044d1240dSMarek Szyprowski  * it that the hardware (or driver) doesn't support changing of the planes'
4011e4d84c6SDaniel Vetter  * order. For mutable zpos see drm_plane_create_zpos_property().
4021e4d84c6SDaniel Vetter  *
4031e4d84c6SDaniel Vetter  * The property exposed to userspace is called "zpos".
40444d1240dSMarek Szyprowski  *
40544d1240dSMarek Szyprowski  * Returns:
40644d1240dSMarek Szyprowski  * Zero on success, negative errno on failure.
40744d1240dSMarek Szyprowski  */
drm_plane_create_zpos_immutable_property(struct drm_plane * plane,unsigned int zpos)40844d1240dSMarek Szyprowski int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
40944d1240dSMarek Szyprowski 					     unsigned int zpos)
41044d1240dSMarek Szyprowski {
41144d1240dSMarek Szyprowski 	struct drm_property *prop;
41244d1240dSMarek Szyprowski 
41344d1240dSMarek Szyprowski 	prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
41444d1240dSMarek Szyprowski 					 "zpos", zpos, zpos);
41544d1240dSMarek Szyprowski 	if (!prop)
41644d1240dSMarek Szyprowski 		return -ENOMEM;
41744d1240dSMarek Szyprowski 
41844d1240dSMarek Szyprowski 	drm_object_attach_property(&plane->base, prop, zpos);
41944d1240dSMarek Szyprowski 
42044d1240dSMarek Szyprowski 	plane->zpos_property = prop;
42144d1240dSMarek Szyprowski 
42244d1240dSMarek Szyprowski 	if (plane->state) {
42344d1240dSMarek Szyprowski 		plane->state->zpos = zpos;
42444d1240dSMarek Szyprowski 		plane->state->normalized_zpos = zpos;
42544d1240dSMarek Szyprowski 	}
42644d1240dSMarek Szyprowski 
42744d1240dSMarek Szyprowski 	return 0;
42844d1240dSMarek Szyprowski }
42944d1240dSMarek Szyprowski EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
43044d1240dSMarek Szyprowski 
drm_atomic_state_zpos_cmp(const void * a,const void * b)43144d1240dSMarek Szyprowski static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
43244d1240dSMarek Szyprowski {
43344d1240dSMarek Szyprowski 	const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
43444d1240dSMarek Szyprowski 	const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
43544d1240dSMarek Szyprowski 
43644d1240dSMarek Szyprowski 	if (sa->zpos != sb->zpos)
43744d1240dSMarek Szyprowski 		return sa->zpos - sb->zpos;
43844d1240dSMarek Szyprowski 	else
43944d1240dSMarek Szyprowski 		return sa->plane->base.id - sb->plane->base.id;
44044d1240dSMarek Szyprowski }
44144d1240dSMarek Szyprowski 
drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)44244d1240dSMarek Szyprowski static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
44344d1240dSMarek Szyprowski 					  struct drm_crtc_state *crtc_state)
44444d1240dSMarek Szyprowski {
44544d1240dSMarek Szyprowski 	struct drm_atomic_state *state = crtc_state->state;
44644d1240dSMarek Szyprowski 	struct drm_device *dev = crtc->dev;
44744d1240dSMarek Szyprowski 	int total_planes = dev->mode_config.num_total_plane;
44844d1240dSMarek Szyprowski 	struct drm_plane_state **states;
44944d1240dSMarek Szyprowski 	struct drm_plane *plane;
45044d1240dSMarek Szyprowski 	int i, n = 0;
45144d1240dSMarek Szyprowski 	int ret = 0;
45244d1240dSMarek Szyprowski 
453*fc2602b5SSiddh Raman Pant 	drm_dbg_atomic(dev, "[CRTC:%d:%s] calculating normalized zpos values\n",
45444d1240dSMarek Szyprowski 		       crtc->base.id, crtc->name);
45544d1240dSMarek Szyprowski 
4560ee931c4SMichal Hocko 	states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
45744d1240dSMarek Szyprowski 	if (!states)
45844d1240dSMarek Szyprowski 		return -ENOMEM;
45944d1240dSMarek Szyprowski 
46044d1240dSMarek Szyprowski 	/*
46144d1240dSMarek Szyprowski 	 * Normalization process might create new states for planes which
46244d1240dSMarek Szyprowski 	 * normalized_zpos has to be recalculated.
46344d1240dSMarek Szyprowski 	 */
46444d1240dSMarek Szyprowski 	drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
46544d1240dSMarek Szyprowski 		struct drm_plane_state *plane_state =
46644d1240dSMarek Szyprowski 			drm_atomic_get_plane_state(state, plane);
46744d1240dSMarek Szyprowski 		if (IS_ERR(plane_state)) {
46844d1240dSMarek Szyprowski 			ret = PTR_ERR(plane_state);
46944d1240dSMarek Szyprowski 			goto done;
47044d1240dSMarek Szyprowski 		}
47144d1240dSMarek Szyprowski 		states[n++] = plane_state;
472*fc2602b5SSiddh Raman Pant 		drm_dbg_atomic(dev, "[PLANE:%d:%s] processing zpos value %d\n",
473*fc2602b5SSiddh Raman Pant 			       plane->base.id, plane->name, plane_state->zpos);
47444d1240dSMarek Szyprowski 	}
47544d1240dSMarek Szyprowski 
47644d1240dSMarek Szyprowski 	sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
47744d1240dSMarek Szyprowski 
47844d1240dSMarek Szyprowski 	for (i = 0; i < n; i++) {
47944d1240dSMarek Szyprowski 		plane = states[i]->plane;
48044d1240dSMarek Szyprowski 
48144d1240dSMarek Szyprowski 		states[i]->normalized_zpos = i;
482*fc2602b5SSiddh Raman Pant 		drm_dbg_atomic(dev, "[PLANE:%d:%s] normalized zpos value %d\n",
48344d1240dSMarek Szyprowski 			       plane->base.id, plane->name, i);
48444d1240dSMarek Szyprowski 	}
48544d1240dSMarek Szyprowski 	crtc_state->zpos_changed = true;
48644d1240dSMarek Szyprowski 
48744d1240dSMarek Szyprowski done:
48844d1240dSMarek Szyprowski 	kfree(states);
48944d1240dSMarek Szyprowski 	return ret;
49044d1240dSMarek Szyprowski }
49144d1240dSMarek Szyprowski 
49244d1240dSMarek Szyprowski /**
49352a9fcdaSDaniel Vetter  * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
49444d1240dSMarek Szyprowski  * @dev: DRM device
49544d1240dSMarek Szyprowski  * @state: atomic state of DRM device
49644d1240dSMarek Szyprowski  *
49744d1240dSMarek Szyprowski  * This function calculates normalized zpos value for all modified planes in
4981e4d84c6SDaniel Vetter  * the provided atomic state of DRM device.
4991e4d84c6SDaniel Vetter  *
5001e4d84c6SDaniel Vetter  * For every CRTC this function checks new states of all planes assigned to
5011e4d84c6SDaniel Vetter  * it and calculates normalized zpos value for these planes. Planes are compared
5021e4d84c6SDaniel Vetter  * first by their zpos values, then by plane id (if zpos is equal). The plane
503d574528aSDaniel Vetter  * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
504d574528aSDaniel Vetter  * is then filled with unique values from 0 to number of active planes in crtc
5051e4d84c6SDaniel Vetter  * minus one.
50644d1240dSMarek Szyprowski  *
50744d1240dSMarek Szyprowski  * RETURNS
50844d1240dSMarek Szyprowski  * Zero for success or -errno
50944d1240dSMarek Szyprowski  */
drm_atomic_normalize_zpos(struct drm_device * dev,struct drm_atomic_state * state)51052a9fcdaSDaniel Vetter int drm_atomic_normalize_zpos(struct drm_device *dev,
51144d1240dSMarek Szyprowski 			      struct drm_atomic_state *state)
51244d1240dSMarek Szyprowski {
51344d1240dSMarek Szyprowski 	struct drm_crtc *crtc;
51462c7bdc0SMaarten Lankhorst 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
51544d1240dSMarek Szyprowski 	struct drm_plane *plane;
51662c7bdc0SMaarten Lankhorst 	struct drm_plane_state *old_plane_state, *new_plane_state;
51744d1240dSMarek Szyprowski 	int i, ret = 0;
51844d1240dSMarek Szyprowski 
51962c7bdc0SMaarten Lankhorst 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
52062c7bdc0SMaarten Lankhorst 		crtc = new_plane_state->crtc;
52144d1240dSMarek Szyprowski 		if (!crtc)
52244d1240dSMarek Szyprowski 			continue;
52362c7bdc0SMaarten Lankhorst 		if (old_plane_state->zpos != new_plane_state->zpos) {
52462c7bdc0SMaarten Lankhorst 			new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
52562c7bdc0SMaarten Lankhorst 			new_crtc_state->zpos_changed = true;
52644d1240dSMarek Szyprowski 		}
52744d1240dSMarek Szyprowski 	}
52844d1240dSMarek Szyprowski 
52962c7bdc0SMaarten Lankhorst 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
53062c7bdc0SMaarten Lankhorst 		if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
53162c7bdc0SMaarten Lankhorst 		    new_crtc_state->zpos_changed) {
53244d1240dSMarek Szyprowski 			ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
53362c7bdc0SMaarten Lankhorst 								    new_crtc_state);
53444d1240dSMarek Szyprowski 			if (ret)
53544d1240dSMarek Szyprowski 				return ret;
53644d1240dSMarek Szyprowski 		}
53744d1240dSMarek Szyprowski 	}
53844d1240dSMarek Szyprowski 	return 0;
53944d1240dSMarek Szyprowski }
54052a9fcdaSDaniel Vetter EXPORT_SYMBOL(drm_atomic_normalize_zpos);
541a5ec8332SLowry Li 
542a5ec8332SLowry Li /**
543a5ec8332SLowry Li  * drm_plane_create_blend_mode_property - create a new blend mode property
544a5ec8332SLowry Li  * @plane: drm plane
545a5ec8332SLowry Li  * @supported_modes: bitmask of supported modes, must include
546a5ec8332SLowry Li  *		     BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
547a5ec8332SLowry Li  *		     that alpha is premultiplied, and old userspace can break if
548a5ec8332SLowry Li  *		     the property defaults to anything else.
549a5ec8332SLowry Li  *
550a5ec8332SLowry Li  * This creates a new property describing the blend mode.
551a5ec8332SLowry Li  *
552a5ec8332SLowry Li  * The property exposed to userspace is an enumeration property (see
553a5ec8332SLowry Li  * drm_property_create_enum()) called "pixel blend mode" and has the
554a5ec8332SLowry Li  * following enumeration values:
555a5ec8332SLowry Li  *
556a5ec8332SLowry Li  * "None":
557a5ec8332SLowry Li  *	Blend formula that ignores the pixel alpha.
558a5ec8332SLowry Li  *
559a5ec8332SLowry Li  * "Pre-multiplied":
560a5ec8332SLowry Li  *	Blend formula that assumes the pixel color values have been already
561a5ec8332SLowry Li  *	pre-multiplied with the alpha channel values.
562a5ec8332SLowry Li  *
563a5ec8332SLowry Li  * "Coverage":
564a5ec8332SLowry Li  *	Blend formula that assumes the pixel color values have not been
565a5ec8332SLowry Li  *	pre-multiplied and will do so when blending them to the background color
566a5ec8332SLowry Li  *	values.
567a5ec8332SLowry Li  *
568a5ec8332SLowry Li  * RETURNS:
569a5ec8332SLowry Li  * Zero for success or -errno
570a5ec8332SLowry Li  */
drm_plane_create_blend_mode_property(struct drm_plane * plane,unsigned int supported_modes)571a5ec8332SLowry Li int drm_plane_create_blend_mode_property(struct drm_plane *plane,
572a5ec8332SLowry Li 					 unsigned int supported_modes)
573a5ec8332SLowry Li {
574a5ec8332SLowry Li 	struct drm_device *dev = plane->dev;
575a5ec8332SLowry Li 	struct drm_property *prop;
576a5ec8332SLowry Li 	static const struct drm_prop_enum_list props[] = {
577a5ec8332SLowry Li 		{ DRM_MODE_BLEND_PIXEL_NONE, "None" },
578a5ec8332SLowry Li 		{ DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
579a5ec8332SLowry Li 		{ DRM_MODE_BLEND_COVERAGE, "Coverage" },
580a5ec8332SLowry Li 	};
581a5ec8332SLowry Li 	unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
582a5ec8332SLowry Li 				       BIT(DRM_MODE_BLEND_PREMULTI)   |
583a5ec8332SLowry Li 				       BIT(DRM_MODE_BLEND_COVERAGE);
584a5ec8332SLowry Li 	int i;
585a5ec8332SLowry Li 
586a5ec8332SLowry Li 	if (WARN_ON((supported_modes & ~valid_mode_mask) ||
587a5ec8332SLowry Li 		    ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
588a5ec8332SLowry Li 		return -EINVAL;
589a5ec8332SLowry Li 
590a5ec8332SLowry Li 	prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
591a5ec8332SLowry Li 				   "pixel blend mode",
592a5ec8332SLowry Li 				   hweight32(supported_modes));
593a5ec8332SLowry Li 	if (!prop)
594a5ec8332SLowry Li 		return -ENOMEM;
595a5ec8332SLowry Li 
596a5ec8332SLowry Li 	for (i = 0; i < ARRAY_SIZE(props); i++) {
597a5ec8332SLowry Li 		int ret;
598a5ec8332SLowry Li 
599a5ec8332SLowry Li 		if (!(BIT(props[i].type) & supported_modes))
600a5ec8332SLowry Li 			continue;
601a5ec8332SLowry Li 
602a5ec8332SLowry Li 		ret = drm_property_add_enum(prop, props[i].type,
603a5ec8332SLowry Li 					    props[i].name);
604a5ec8332SLowry Li 
605a5ec8332SLowry Li 		if (ret) {
606a5ec8332SLowry Li 			drm_property_destroy(dev, prop);
607a5ec8332SLowry Li 
608a5ec8332SLowry Li 			return ret;
609a5ec8332SLowry Li 		}
610a5ec8332SLowry Li 	}
611a5ec8332SLowry Li 
612a5ec8332SLowry Li 	drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
613a5ec8332SLowry Li 	plane->blend_mode_property = prop;
614a5ec8332SLowry Li 
615a5ec8332SLowry Li 	return 0;
616a5ec8332SLowry Li }
617a5ec8332SLowry Li EXPORT_SYMBOL(drm_plane_create_blend_mode_property);
618