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