1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2016 Samsung Electronics Co.Ltd
3*4882a593Smuzhiyun * Authors:
4*4882a593Smuzhiyun * Marek Szyprowski <m.szyprowski@samsung.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * DRM core plane blending related functions
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
9*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
10*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that copyright
11*4882a593Smuzhiyun * notice and this permission notice appear in supporting documentation, and
12*4882a593Smuzhiyun * that the name of the copyright holders not be used in advertising or
13*4882a593Smuzhiyun * publicity pertaining to distribution of the software without specific,
14*4882a593Smuzhiyun * written prior permission. The copyright holders make no representations
15*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided "as
16*4882a593Smuzhiyun * is" without express or implied warranty.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20*4882a593Smuzhiyun * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24*4882a593Smuzhiyun * OF THIS SOFTWARE.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/export.h>
28*4882a593Smuzhiyun #include <linux/slab.h>
29*4882a593Smuzhiyun #include <linux/sort.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <drm/drm_atomic.h>
32*4882a593Smuzhiyun #include <drm/drm_blend.h>
33*4882a593Smuzhiyun #include <drm/drm_device.h>
34*4882a593Smuzhiyun #include <drm/drm_print.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include "drm_crtc_internal.h"
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /**
39*4882a593Smuzhiyun * DOC: overview
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * The basic plane composition model supported by standard plane properties only
42*4882a593Smuzhiyun * has a source rectangle (in logical pixels within the &drm_framebuffer), with
43*4882a593Smuzhiyun * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
44*4882a593Smuzhiyun * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
45*4882a593Smuzhiyun * defined by the horizontal and vertical visible pixels (stored in @hdisplay
46*4882a593Smuzhiyun * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
47*4882a593Smuzhiyun * two rectangles are both stored in the &drm_plane_state.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * For the atomic ioctl the following standard (atomic) properties on the plane object
50*4882a593Smuzhiyun * encode the basic plane composition model:
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * SRC_X:
53*4882a593Smuzhiyun * X coordinate offset for the source rectangle within the
54*4882a593Smuzhiyun * &drm_framebuffer, in 16.16 fixed point. Must be positive.
55*4882a593Smuzhiyun * SRC_Y:
56*4882a593Smuzhiyun * Y coordinate offset for the source rectangle within the
57*4882a593Smuzhiyun * &drm_framebuffer, in 16.16 fixed point. Must be positive.
58*4882a593Smuzhiyun * SRC_W:
59*4882a593Smuzhiyun * Width for the source rectangle within the &drm_framebuffer, in 16.16
60*4882a593Smuzhiyun * fixed point. SRC_X plus SRC_W must be within the width of the source
61*4882a593Smuzhiyun * framebuffer. Must be positive.
62*4882a593Smuzhiyun * SRC_H:
63*4882a593Smuzhiyun * Height for the source rectangle within the &drm_framebuffer, in 16.16
64*4882a593Smuzhiyun * fixed point. SRC_Y plus SRC_H must be within the height of the source
65*4882a593Smuzhiyun * framebuffer. Must be positive.
66*4882a593Smuzhiyun * CRTC_X:
67*4882a593Smuzhiyun * X coordinate offset for the destination rectangle. Can be negative.
68*4882a593Smuzhiyun * CRTC_Y:
69*4882a593Smuzhiyun * Y coordinate offset for the destination rectangle. Can be negative.
70*4882a593Smuzhiyun * CRTC_W:
71*4882a593Smuzhiyun * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
72*4882a593Smuzhiyun * the currently visible horizontal area of the &drm_crtc.
73*4882a593Smuzhiyun * CRTC_H:
74*4882a593Smuzhiyun * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
75*4882a593Smuzhiyun * the currently visible vertical area of the &drm_crtc.
76*4882a593Smuzhiyun * FB_ID:
77*4882a593Smuzhiyun * Mode object ID of the &drm_framebuffer this plane should scan out.
78*4882a593Smuzhiyun * CRTC_ID:
79*4882a593Smuzhiyun * Mode object ID of the &drm_crtc this plane should be connected to.
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * Note that the source rectangle must fully lie within the bounds of the
82*4882a593Smuzhiyun * &drm_framebuffer. The destination rectangle can lie outside of the visible
83*4882a593Smuzhiyun * area of the current mode of the CRTC. It must be apprpriately clipped by the
84*4882a593Smuzhiyun * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
85*4882a593Smuzhiyun * are also allowed to round the subpixel sampling positions appropriately, but
86*4882a593Smuzhiyun * only to the next full pixel. No pixel outside of the source rectangle may
87*4882a593Smuzhiyun * ever be sampled, which is important when applying more sophisticated
88*4882a593Smuzhiyun * filtering than just a bilinear one when scaling. The filtering mode when
89*4882a593Smuzhiyun * scaling is unspecified.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * On top of this basic transformation additional properties can be exposed by
92*4882a593Smuzhiyun * the driver:
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * alpha:
95*4882a593Smuzhiyun * Alpha is setup with drm_plane_create_alpha_property(). It controls the
96*4882a593Smuzhiyun * plane-wide opacity, from transparent (0) to opaque (0xffff). It can be
97*4882a593Smuzhiyun * combined with pixel alpha.
98*4882a593Smuzhiyun * The pixel values in the framebuffers are expected to not be
99*4882a593Smuzhiyun * pre-multiplied by the global alpha associated to the plane.
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * rotation:
102*4882a593Smuzhiyun * Rotation is set up with drm_plane_create_rotation_property(). It adds a
103*4882a593Smuzhiyun * rotation and reflection step between the source and destination rectangles.
104*4882a593Smuzhiyun * Without this property the rectangle is only scaled, but not rotated or
105*4882a593Smuzhiyun * reflected.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Possbile values:
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * "rotate-<degrees>":
110*4882a593Smuzhiyun * Signals that a drm plane is rotated <degrees> degrees in counter
111*4882a593Smuzhiyun * clockwise direction.
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * "reflect-<axis>":
114*4882a593Smuzhiyun * Signals that the contents of a drm plane is reflected along the
115*4882a593Smuzhiyun * <axis> axis, in the same way as mirroring.
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * reflect-x::
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * |o | | o|
120*4882a593Smuzhiyun * | | -> | |
121*4882a593Smuzhiyun * | v| |v |
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * reflect-y::
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun * |o | | ^|
126*4882a593Smuzhiyun * | | -> | |
127*4882a593Smuzhiyun * | v| |o |
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * zpos:
130*4882a593Smuzhiyun * Z position is set up with drm_plane_create_zpos_immutable_property() and
131*4882a593Smuzhiyun * drm_plane_create_zpos_property(). It controls the visibility of overlapping
132*4882a593Smuzhiyun * planes. Without this property the primary plane is always below the cursor
133*4882a593Smuzhiyun * plane, and ordering between all other planes is undefined. The positive
134*4882a593Smuzhiyun * Z axis points towards the user, i.e. planes with lower Z position values
135*4882a593Smuzhiyun * are underneath planes with higher Z position values. Two planes with the
136*4882a593Smuzhiyun * same Z position value have undefined ordering. Note that the Z position
137*4882a593Smuzhiyun * value can also be immutable, to inform userspace about the hard-coded
138*4882a593Smuzhiyun * stacking of planes, see drm_plane_create_zpos_immutable_property(). If
139*4882a593Smuzhiyun * any plane has a zpos property (either mutable or immutable), then all
140*4882a593Smuzhiyun * planes shall have a zpos property.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * pixel blend mode:
143*4882a593Smuzhiyun * Pixel blend mode is set up with drm_plane_create_blend_mode_property().
144*4882a593Smuzhiyun * It adds a blend mode for alpha blending equation selection, describing
145*4882a593Smuzhiyun * how the pixels from the current plane are composited with the
146*4882a593Smuzhiyun * background.
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * Three alpha blending equations are defined:
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * "None":
151*4882a593Smuzhiyun * Blend formula that ignores the pixel alpha::
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * out.rgb = plane_alpha * fg.rgb +
154*4882a593Smuzhiyun * (1 - plane_alpha) * bg.rgb
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * "Pre-multiplied":
157*4882a593Smuzhiyun * Blend formula that assumes the pixel color values
158*4882a593Smuzhiyun * have been already pre-multiplied with the alpha
159*4882a593Smuzhiyun * channel values::
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * out.rgb = plane_alpha * fg.rgb +
162*4882a593Smuzhiyun * (1 - (plane_alpha * fg.alpha)) * bg.rgb
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * "Coverage":
165*4882a593Smuzhiyun * Blend formula that assumes the pixel color values have not
166*4882a593Smuzhiyun * been pre-multiplied and will do so when blending them to the
167*4882a593Smuzhiyun * background color values::
168*4882a593Smuzhiyun *
169*4882a593Smuzhiyun * out.rgb = plane_alpha * fg.alpha * fg.rgb +
170*4882a593Smuzhiyun * (1 - (plane_alpha * fg.alpha)) * bg.rgb
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * Using the following symbols:
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * "fg.rgb":
175*4882a593Smuzhiyun * Each of the RGB component values from the plane's pixel
176*4882a593Smuzhiyun * "fg.alpha":
177*4882a593Smuzhiyun * Alpha component value from the plane's pixel. If the plane's
178*4882a593Smuzhiyun * pixel format has no alpha component, then this is assumed to be
179*4882a593Smuzhiyun * 1.0. In these cases, this property has no effect, as all three
180*4882a593Smuzhiyun * equations become equivalent.
181*4882a593Smuzhiyun * "bg.rgb":
182*4882a593Smuzhiyun * Each of the RGB component values from the background
183*4882a593Smuzhiyun * "plane_alpha":
184*4882a593Smuzhiyun * Plane alpha value set by the plane "alpha" property. If the
185*4882a593Smuzhiyun * plane does not expose the "alpha" property, then this is
186*4882a593Smuzhiyun * assumed to be 1.0
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * IN_FORMATS:
189*4882a593Smuzhiyun * Blob property which contains the set of buffer format and modifier
190*4882a593Smuzhiyun * pairs supported by this plane. The blob is a drm_format_modifier_blob
191*4882a593Smuzhiyun * struct. Without this property the plane doesn't support buffers with
192*4882a593Smuzhiyun * modifiers. Userspace cannot change this property.
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * Note that all the property extensions described here apply either to the
195*4882a593Smuzhiyun * plane or the CRTC (e.g. for the background color, which currently is not
196*4882a593Smuzhiyun * exposed and assumed to be black).
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /**
200*4882a593Smuzhiyun * drm_plane_create_alpha_property - create a new alpha property
201*4882a593Smuzhiyun * @plane: drm plane
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * This function creates a generic, mutable, alpha property and enables support
204*4882a593Smuzhiyun * for it in the DRM core. It is attached to @plane.
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * The alpha property will be allowed to be within the bounds of 0
207*4882a593Smuzhiyun * (transparent) to 0xffff (opaque).
208*4882a593Smuzhiyun *
209*4882a593Smuzhiyun * Returns:
210*4882a593Smuzhiyun * 0 on success, negative error code on failure.
211*4882a593Smuzhiyun */
drm_plane_create_alpha_property(struct drm_plane * plane)212*4882a593Smuzhiyun int drm_plane_create_alpha_property(struct drm_plane *plane)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun struct drm_property *prop;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun prop = drm_property_create_range(plane->dev, 0, "alpha",
217*4882a593Smuzhiyun 0, DRM_BLEND_ALPHA_OPAQUE);
218*4882a593Smuzhiyun if (!prop)
219*4882a593Smuzhiyun return -ENOMEM;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
222*4882a593Smuzhiyun plane->alpha_property = prop;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun if (plane->state)
225*4882a593Smuzhiyun plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun return 0;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun EXPORT_SYMBOL(drm_plane_create_alpha_property);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * drm_plane_create_rotation_property - create a new rotation property
233*4882a593Smuzhiyun * @plane: drm plane
234*4882a593Smuzhiyun * @rotation: initial value of the rotation property
235*4882a593Smuzhiyun * @supported_rotations: bitmask of supported rotations and reflections
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * This creates a new property with the selected support for transformations.
238*4882a593Smuzhiyun *
239*4882a593Smuzhiyun * Since a rotation by 180° degress is the same as reflecting both along the x
240*4882a593Smuzhiyun * and the y axis the rotation property is somewhat redundant. Drivers can use
241*4882a593Smuzhiyun * drm_rotation_simplify() to normalize values of this property.
242*4882a593Smuzhiyun *
243*4882a593Smuzhiyun * The property exposed to userspace is a bitmask property (see
244*4882a593Smuzhiyun * drm_property_create_bitmask()) called "rotation" and has the following
245*4882a593Smuzhiyun * bitmask enumaration values:
246*4882a593Smuzhiyun *
247*4882a593Smuzhiyun * DRM_MODE_ROTATE_0:
248*4882a593Smuzhiyun * "rotate-0"
249*4882a593Smuzhiyun * DRM_MODE_ROTATE_90:
250*4882a593Smuzhiyun * "rotate-90"
251*4882a593Smuzhiyun * DRM_MODE_ROTATE_180:
252*4882a593Smuzhiyun * "rotate-180"
253*4882a593Smuzhiyun * DRM_MODE_ROTATE_270:
254*4882a593Smuzhiyun * "rotate-270"
255*4882a593Smuzhiyun * DRM_MODE_REFLECT_X:
256*4882a593Smuzhiyun * "reflect-x"
257*4882a593Smuzhiyun * DRM_MODE_REFLECT_Y:
258*4882a593Smuzhiyun * "reflect-y"
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * Rotation is the specified amount in degrees in counter clockwise direction,
261*4882a593Smuzhiyun * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
262*4882a593Smuzhiyun * rotation. After reflection, the rotation is applied to the image sampled from
263*4882a593Smuzhiyun * the source rectangle, before scaling it to fit the destination rectangle.
264*4882a593Smuzhiyun */
drm_plane_create_rotation_property(struct drm_plane * plane,unsigned int rotation,unsigned int supported_rotations)265*4882a593Smuzhiyun int drm_plane_create_rotation_property(struct drm_plane *plane,
266*4882a593Smuzhiyun unsigned int rotation,
267*4882a593Smuzhiyun unsigned int supported_rotations)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun static const struct drm_prop_enum_list props[] = {
270*4882a593Smuzhiyun { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
271*4882a593Smuzhiyun { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
272*4882a593Smuzhiyun { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
273*4882a593Smuzhiyun { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
274*4882a593Smuzhiyun { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
275*4882a593Smuzhiyun { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
276*4882a593Smuzhiyun };
277*4882a593Smuzhiyun struct drm_property *prop;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
280*4882a593Smuzhiyun WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
281*4882a593Smuzhiyun WARN_ON(rotation & ~supported_rotations);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
284*4882a593Smuzhiyun props, ARRAY_SIZE(props),
285*4882a593Smuzhiyun supported_rotations);
286*4882a593Smuzhiyun if (!prop)
287*4882a593Smuzhiyun return -ENOMEM;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun drm_object_attach_property(&plane->base, prop, rotation);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun if (plane->state)
292*4882a593Smuzhiyun plane->state->rotation = rotation;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun plane->rotation_property = prop;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun return 0;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun EXPORT_SYMBOL(drm_plane_create_rotation_property);
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /**
301*4882a593Smuzhiyun * drm_rotation_simplify() - Try to simplify the rotation
302*4882a593Smuzhiyun * @rotation: Rotation to be simplified
303*4882a593Smuzhiyun * @supported_rotations: Supported rotations
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * Attempt to simplify the rotation to a form that is supported.
306*4882a593Smuzhiyun * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
307*4882a593Smuzhiyun * one could call this function like this:
308*4882a593Smuzhiyun *
309*4882a593Smuzhiyun * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
310*4882a593Smuzhiyun * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
311*4882a593Smuzhiyun * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
314*4882a593Smuzhiyun * transforms the hardware supports, this function may not
315*4882a593Smuzhiyun * be able to produce a supported transform, so the caller should
316*4882a593Smuzhiyun * check the result afterwards.
317*4882a593Smuzhiyun */
drm_rotation_simplify(unsigned int rotation,unsigned int supported_rotations)318*4882a593Smuzhiyun unsigned int drm_rotation_simplify(unsigned int rotation,
319*4882a593Smuzhiyun unsigned int supported_rotations)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun if (rotation & ~supported_rotations) {
322*4882a593Smuzhiyun rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
323*4882a593Smuzhiyun rotation = (rotation & DRM_MODE_REFLECT_MASK) |
324*4882a593Smuzhiyun BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
325*4882a593Smuzhiyun % 4);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun return rotation;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun EXPORT_SYMBOL(drm_rotation_simplify);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /**
333*4882a593Smuzhiyun * drm_plane_create_zpos_property - create mutable zpos property
334*4882a593Smuzhiyun * @plane: drm plane
335*4882a593Smuzhiyun * @zpos: initial value of zpos property
336*4882a593Smuzhiyun * @min: minimal possible value of zpos property
337*4882a593Smuzhiyun * @max: maximal possible value of zpos property
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * This function initializes generic mutable zpos property and enables support
340*4882a593Smuzhiyun * for it in drm core. Drivers can then attach this property to planes to enable
341*4882a593Smuzhiyun * support for configurable planes arrangement during blending operation.
342*4882a593Smuzhiyun * Drivers that attach a mutable zpos property to any plane should call the
343*4882a593Smuzhiyun * drm_atomic_normalize_zpos() helper during their implementation of
344*4882a593Smuzhiyun * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
345*4882a593Smuzhiyun * values and store them in &drm_plane_state.normalized_zpos. Usually min
346*4882a593Smuzhiyun * should be set to 0 and max to maximal number of planes for given crtc - 1.
347*4882a593Smuzhiyun *
348*4882a593Smuzhiyun * If zpos of some planes cannot be changed (like fixed background or
349*4882a593Smuzhiyun * cursor/topmost planes), drivers shall adjust the min/max values and assign
350*4882a593Smuzhiyun * those planes immutable zpos properties with lower or higher values (for more
351*4882a593Smuzhiyun * information, see drm_plane_create_zpos_immutable_property() function). In such
352*4882a593Smuzhiyun * case drivers shall also assign proper initial zpos values for all planes in
353*4882a593Smuzhiyun * its plane_reset() callback, so the planes will be always sorted properly.
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * See also drm_atomic_normalize_zpos().
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * The property exposed to userspace is called "zpos".
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * Returns:
360*4882a593Smuzhiyun * Zero on success, negative errno on failure.
361*4882a593Smuzhiyun */
drm_plane_create_zpos_property(struct drm_plane * plane,unsigned int zpos,unsigned int min,unsigned int max)362*4882a593Smuzhiyun int drm_plane_create_zpos_property(struct drm_plane *plane,
363*4882a593Smuzhiyun unsigned int zpos,
364*4882a593Smuzhiyun unsigned int min, unsigned int max)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun struct drm_property *prop;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
369*4882a593Smuzhiyun if (!prop)
370*4882a593Smuzhiyun return -ENOMEM;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun drm_object_attach_property(&plane->base, prop, zpos);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun plane->zpos_property = prop;
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun if (plane->state) {
377*4882a593Smuzhiyun plane->state->zpos = zpos;
378*4882a593Smuzhiyun plane->state->normalized_zpos = zpos;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun return 0;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun EXPORT_SYMBOL(drm_plane_create_zpos_property);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /**
386*4882a593Smuzhiyun * drm_plane_create_zpos_immutable_property - create immuttable zpos property
387*4882a593Smuzhiyun * @plane: drm plane
388*4882a593Smuzhiyun * @zpos: value of zpos property
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * This function initializes generic immutable zpos property and enables
391*4882a593Smuzhiyun * support for it in drm core. Using this property driver lets userspace
392*4882a593Smuzhiyun * to get the arrangement of the planes for blending operation and notifies
393*4882a593Smuzhiyun * it that the hardware (or driver) doesn't support changing of the planes'
394*4882a593Smuzhiyun * order. For mutable zpos see drm_plane_create_zpos_property().
395*4882a593Smuzhiyun *
396*4882a593Smuzhiyun * The property exposed to userspace is called "zpos".
397*4882a593Smuzhiyun *
398*4882a593Smuzhiyun * Returns:
399*4882a593Smuzhiyun * Zero on success, negative errno on failure.
400*4882a593Smuzhiyun */
drm_plane_create_zpos_immutable_property(struct drm_plane * plane,unsigned int zpos)401*4882a593Smuzhiyun int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
402*4882a593Smuzhiyun unsigned int zpos)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun struct drm_property *prop;
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
407*4882a593Smuzhiyun "zpos", zpos, zpos);
408*4882a593Smuzhiyun if (!prop)
409*4882a593Smuzhiyun return -ENOMEM;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun drm_object_attach_property(&plane->base, prop, zpos);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun plane->zpos_property = prop;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (plane->state) {
416*4882a593Smuzhiyun plane->state->zpos = zpos;
417*4882a593Smuzhiyun plane->state->normalized_zpos = zpos;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun return 0;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
423*4882a593Smuzhiyun
drm_atomic_state_zpos_cmp(const void * a,const void * b)424*4882a593Smuzhiyun static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
427*4882a593Smuzhiyun const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun if (sa->zpos != sb->zpos)
430*4882a593Smuzhiyun return sa->zpos - sb->zpos;
431*4882a593Smuzhiyun else
432*4882a593Smuzhiyun return sa->plane->base.id - sb->plane->base.id;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)435*4882a593Smuzhiyun static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
436*4882a593Smuzhiyun struct drm_crtc_state *crtc_state)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun struct drm_atomic_state *state = crtc_state->state;
439*4882a593Smuzhiyun struct drm_device *dev = crtc->dev;
440*4882a593Smuzhiyun int total_planes = dev->mode_config.num_total_plane;
441*4882a593Smuzhiyun struct drm_plane_state **states;
442*4882a593Smuzhiyun struct drm_plane *plane;
443*4882a593Smuzhiyun int i, n = 0;
444*4882a593Smuzhiyun int ret = 0;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
447*4882a593Smuzhiyun crtc->base.id, crtc->name);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
450*4882a593Smuzhiyun if (!states)
451*4882a593Smuzhiyun return -ENOMEM;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /*
454*4882a593Smuzhiyun * Normalization process might create new states for planes which
455*4882a593Smuzhiyun * normalized_zpos has to be recalculated.
456*4882a593Smuzhiyun */
457*4882a593Smuzhiyun drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
458*4882a593Smuzhiyun struct drm_plane_state *plane_state =
459*4882a593Smuzhiyun drm_atomic_get_plane_state(state, plane);
460*4882a593Smuzhiyun if (IS_ERR(plane_state)) {
461*4882a593Smuzhiyun ret = PTR_ERR(plane_state);
462*4882a593Smuzhiyun goto done;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun states[n++] = plane_state;
465*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
466*4882a593Smuzhiyun plane->base.id, plane->name,
467*4882a593Smuzhiyun plane_state->zpos);
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun for (i = 0; i < n; i++) {
473*4882a593Smuzhiyun plane = states[i]->plane;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun states[i]->normalized_zpos = i;
476*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
477*4882a593Smuzhiyun plane->base.id, plane->name, i);
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun crtc_state->zpos_changed = true;
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun done:
482*4882a593Smuzhiyun kfree(states);
483*4882a593Smuzhiyun return ret;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /**
487*4882a593Smuzhiyun * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
488*4882a593Smuzhiyun * @dev: DRM device
489*4882a593Smuzhiyun * @state: atomic state of DRM device
490*4882a593Smuzhiyun *
491*4882a593Smuzhiyun * This function calculates normalized zpos value for all modified planes in
492*4882a593Smuzhiyun * the provided atomic state of DRM device.
493*4882a593Smuzhiyun *
494*4882a593Smuzhiyun * For every CRTC this function checks new states of all planes assigned to
495*4882a593Smuzhiyun * it and calculates normalized zpos value for these planes. Planes are compared
496*4882a593Smuzhiyun * first by their zpos values, then by plane id (if zpos is equal). The plane
497*4882a593Smuzhiyun * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
498*4882a593Smuzhiyun * is then filled with unique values from 0 to number of active planes in crtc
499*4882a593Smuzhiyun * minus one.
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * RETURNS
502*4882a593Smuzhiyun * Zero for success or -errno
503*4882a593Smuzhiyun */
drm_atomic_normalize_zpos(struct drm_device * dev,struct drm_atomic_state * state)504*4882a593Smuzhiyun int drm_atomic_normalize_zpos(struct drm_device *dev,
505*4882a593Smuzhiyun struct drm_atomic_state *state)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun struct drm_crtc *crtc;
508*4882a593Smuzhiyun struct drm_crtc_state *old_crtc_state, *new_crtc_state;
509*4882a593Smuzhiyun struct drm_plane *plane;
510*4882a593Smuzhiyun struct drm_plane_state *old_plane_state, *new_plane_state;
511*4882a593Smuzhiyun int i, ret = 0;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
514*4882a593Smuzhiyun crtc = new_plane_state->crtc;
515*4882a593Smuzhiyun if (!crtc)
516*4882a593Smuzhiyun continue;
517*4882a593Smuzhiyun if (old_plane_state->zpos != new_plane_state->zpos) {
518*4882a593Smuzhiyun new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
519*4882a593Smuzhiyun new_crtc_state->zpos_changed = true;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
524*4882a593Smuzhiyun if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
525*4882a593Smuzhiyun new_crtc_state->zpos_changed) {
526*4882a593Smuzhiyun ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
527*4882a593Smuzhiyun new_crtc_state);
528*4882a593Smuzhiyun if (ret)
529*4882a593Smuzhiyun return ret;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun return 0;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_normalize_zpos);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun /**
537*4882a593Smuzhiyun * drm_plane_create_blend_mode_property - create a new blend mode property
538*4882a593Smuzhiyun * @plane: drm plane
539*4882a593Smuzhiyun * @supported_modes: bitmask of supported modes, must include
540*4882a593Smuzhiyun * BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
541*4882a593Smuzhiyun * that alpha is premultiplied, and old userspace can break if
542*4882a593Smuzhiyun * the property defaults to anything else.
543*4882a593Smuzhiyun *
544*4882a593Smuzhiyun * This creates a new property describing the blend mode.
545*4882a593Smuzhiyun *
546*4882a593Smuzhiyun * The property exposed to userspace is an enumeration property (see
547*4882a593Smuzhiyun * drm_property_create_enum()) called "pixel blend mode" and has the
548*4882a593Smuzhiyun * following enumeration values:
549*4882a593Smuzhiyun *
550*4882a593Smuzhiyun * "None":
551*4882a593Smuzhiyun * Blend formula that ignores the pixel alpha.
552*4882a593Smuzhiyun *
553*4882a593Smuzhiyun * "Pre-multiplied":
554*4882a593Smuzhiyun * Blend formula that assumes the pixel color values have been already
555*4882a593Smuzhiyun * pre-multiplied with the alpha channel values.
556*4882a593Smuzhiyun *
557*4882a593Smuzhiyun * "Coverage":
558*4882a593Smuzhiyun * Blend formula that assumes the pixel color values have not been
559*4882a593Smuzhiyun * pre-multiplied and will do so when blending them to the background color
560*4882a593Smuzhiyun * values.
561*4882a593Smuzhiyun *
562*4882a593Smuzhiyun * RETURNS:
563*4882a593Smuzhiyun * Zero for success or -errno
564*4882a593Smuzhiyun */
drm_plane_create_blend_mode_property(struct drm_plane * plane,unsigned int supported_modes)565*4882a593Smuzhiyun int drm_plane_create_blend_mode_property(struct drm_plane *plane,
566*4882a593Smuzhiyun unsigned int supported_modes)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun struct drm_device *dev = plane->dev;
569*4882a593Smuzhiyun struct drm_property *prop;
570*4882a593Smuzhiyun static const struct drm_prop_enum_list props[] = {
571*4882a593Smuzhiyun { DRM_MODE_BLEND_PIXEL_NONE, "None" },
572*4882a593Smuzhiyun { DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
573*4882a593Smuzhiyun { DRM_MODE_BLEND_COVERAGE, "Coverage" },
574*4882a593Smuzhiyun };
575*4882a593Smuzhiyun unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
576*4882a593Smuzhiyun BIT(DRM_MODE_BLEND_PREMULTI) |
577*4882a593Smuzhiyun BIT(DRM_MODE_BLEND_COVERAGE);
578*4882a593Smuzhiyun int i;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun if (WARN_ON((supported_modes & ~valid_mode_mask) ||
581*4882a593Smuzhiyun ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
582*4882a593Smuzhiyun return -EINVAL;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
585*4882a593Smuzhiyun "pixel blend mode",
586*4882a593Smuzhiyun hweight32(supported_modes));
587*4882a593Smuzhiyun if (!prop)
588*4882a593Smuzhiyun return -ENOMEM;
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(props); i++) {
591*4882a593Smuzhiyun int ret;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun if (!(BIT(props[i].type) & supported_modes))
594*4882a593Smuzhiyun continue;
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun ret = drm_property_add_enum(prop, props[i].type,
597*4882a593Smuzhiyun props[i].name);
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun if (ret) {
600*4882a593Smuzhiyun drm_property_destroy(dev, prop);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun return ret;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
607*4882a593Smuzhiyun plane->blend_mode_property = prop;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun return 0;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun EXPORT_SYMBOL(drm_plane_create_blend_mode_property);
612