2 * Copyright (C) 2016 Samsung Electronics Co.Ltd
4 * Marek Szyprowski <m.szyprowski@samsung.com>
6 * DRM core plane blending related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 #include <linux/export.h>
28 #include <linux/slab.h>
29 #include <linux/sort.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_blend.h>
33 #include <drm/drm_device.h>
34 #include <drm/drm_print.h>
36 #include "drm_crtc_internal.h"
41 * The basic plane composition model supported by standard plane properties only
42 * has a source rectangle (in logical pixels within the &drm_framebuffer), with
43 * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
44 * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
45 * defined by the horizontal and vertical visible pixels (stored in @hdisplay
46 * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
47 * two rectangles are both stored in the &drm_plane_state.
49 * For the atomic ioctl the following standard (atomic) properties on the plane object
50 * encode the basic plane composition model:
53 * X coordinate offset for the source rectangle within the
54 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
56 * Y coordinate offset for the source rectangle within the
57 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
59 * Width for the source rectangle within the &drm_framebuffer, in 16.16
60 * fixed point. SRC_X plus SRC_W must be within the width of the source
61 * framebuffer. Must be positive.
63 * Height for the source rectangle within the &drm_framebuffer, in 16.16
64 * fixed point. SRC_Y plus SRC_H must be within the height of the source
65 * framebuffer. Must be positive.
67 * X coordinate offset for the destination rectangle. Can be negative.
69 * Y coordinate offset for the destination rectangle. Can be negative.
71 * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
72 * the currently visible horizontal area of the &drm_crtc.
74 * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
75 * the currently visible vertical area of the &drm_crtc.
77 * Mode object ID of the &drm_framebuffer this plane should scan out.
79 * Mode object ID of the &drm_crtc this plane should be connected to.
81 * Note that the source rectangle must fully lie within the bounds of the
82 * &drm_framebuffer. The destination rectangle can lie outside of the visible
83 * area of the current mode of the CRTC. It must be apprpriately clipped by the
84 * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
85 * are also allowed to round the subpixel sampling positions appropriately, but
86 * only to the next full pixel. No pixel outside of the source rectangle may
87 * ever be sampled, which is important when applying more sophisticated
88 * filtering than just a bilinear one when scaling. The filtering mode when
89 * scaling is unspecified.
91 * On top of this basic transformation additional properties can be exposed by
95 * Alpha is setup with drm_plane_create_alpha_property(). It controls the
96 * plane-wide opacity, from transparent (0) to opaque (0xffff). It can be
97 * combined with pixel alpha.
98 * The pixel values in the framebuffers are expected to not be
99 * pre-multiplied by the global alpha associated to the plane.
102 * Rotation is set up with drm_plane_create_rotation_property(). It adds a
103 * rotation and reflection step between the source and destination rectangles.
104 * Without this property the rectangle is only scaled, but not rotated or
109 * "rotate-<degrees>":
110 * Signals that a drm plane is rotated <degrees> degrees in counter
111 * clockwise direction.
114 * Signals that the contents of a drm plane is reflected along the
115 * <axis> axis, in the same way as mirroring.
130 * Z position is set up with drm_plane_create_zpos_immutable_property() and
131 * drm_plane_create_zpos_property(). It controls the visibility of overlapping
132 * planes. Without this property the primary plane is always below the cursor
133 * plane, and ordering between all other planes is undefined. The positive
134 * Z axis points towards the user, i.e. planes with lower Z position values
135 * are underneath planes with higher Z position values. Two planes with the
136 * same Z position value have undefined ordering. Note that the Z position
137 * value can also be immutable, to inform userspace about the hard-coded
138 * stacking of planes, see drm_plane_create_zpos_immutable_property().
141 * Pixel blend mode is set up with drm_plane_create_blend_mode_property().
142 * It adds a blend mode for alpha blending equation selection, describing
143 * how the pixels from the current plane are composited with the
146 * Three alpha blending equations are defined:
149 * Blend formula that ignores the pixel alpha::
151 * out.rgb = plane_alpha * fg.rgb +
152 * (1 - plane_alpha) * bg.rgb
155 * Blend formula that assumes the pixel color values
156 * have been already pre-multiplied with the alpha
159 * out.rgb = plane_alpha * fg.rgb +
160 * (1 - (plane_alpha * fg.alpha)) * bg.rgb
163 * Blend formula that assumes the pixel color values have not
164 * been pre-multiplied and will do so when blending them to the
165 * background color values::
167 * out.rgb = plane_alpha * fg.alpha * fg.rgb +
168 * (1 - (plane_alpha * fg.alpha)) * bg.rgb
170 * Using the following symbols:
173 * Each of the RGB component values from the plane's pixel
175 * Alpha component value from the plane's pixel. If the plane's
176 * pixel format has no alpha component, then this is assumed to be
177 * 1.0. In these cases, this property has no effect, as all three
178 * equations become equivalent.
180 * Each of the RGB component values from the background
182 * Plane alpha value set by the plane "alpha" property. If the
183 * plane does not expose the "alpha" property, then this is
187 * Blob property which contains the set of buffer format and modifier
188 * pairs supported by this plane. The blob is a drm_format_modifier_blob
189 * struct. Without this property the plane doesn't support buffers with
190 * modifiers. Userspace cannot change this property.
192 * Note that all the property extensions described here apply either to the
193 * plane or the CRTC (e.g. for the background color, which currently is not
194 * exposed and assumed to be black).
198 * drm_plane_create_alpha_property - create a new alpha property
201 * This function creates a generic, mutable, alpha property and enables support
202 * for it in the DRM core. It is attached to @plane.
204 * The alpha property will be allowed to be within the bounds of 0
205 * (transparent) to 0xffff (opaque).
208 * 0 on success, negative error code on failure.
210 int drm_plane_create_alpha_property(struct drm_plane *plane)
212 struct drm_property *prop;
214 prop = drm_property_create_range(plane->dev, 0, "alpha",
215 0, DRM_BLEND_ALPHA_OPAQUE);
219 drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
220 plane->alpha_property = prop;
223 plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
227 EXPORT_SYMBOL(drm_plane_create_alpha_property);
230 * drm_plane_create_rotation_property - create a new rotation property
232 * @rotation: initial value of the rotation property
233 * @supported_rotations: bitmask of supported rotations and reflections
235 * This creates a new property with the selected support for transformations.
237 * Since a rotation by 180° degress is the same as reflecting both along the x
238 * and the y axis the rotation property is somewhat redundant. Drivers can use
239 * drm_rotation_simplify() to normalize values of this property.
241 * The property exposed to userspace is a bitmask property (see
242 * drm_property_create_bitmask()) called "rotation" and has the following
243 * bitmask enumaration values:
247 * DRM_MODE_ROTATE_90:
249 * DRM_MODE_ROTATE_180:
251 * DRM_MODE_ROTATE_270:
253 * DRM_MODE_REFLECT_X:
255 * DRM_MODE_REFLECT_Y:
258 * Rotation is the specified amount in degrees in counter clockwise direction,
259 * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
260 * rotation. After reflection, the rotation is applied to the image sampled from
261 * the source rectangle, before scaling it to fit the destination rectangle.
263 int drm_plane_create_rotation_property(struct drm_plane *plane,
264 unsigned int rotation,
265 unsigned int supported_rotations)
267 static const struct drm_prop_enum_list props[] = {
268 { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
269 { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
270 { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
271 { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
272 { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
273 { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
275 struct drm_property *prop;
277 WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
278 WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
279 WARN_ON(rotation & ~supported_rotations);
281 prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
282 props, ARRAY_SIZE(props),
283 supported_rotations);
287 drm_object_attach_property(&plane->base, prop, rotation);
290 plane->state->rotation = rotation;
292 plane->rotation_property = prop;
296 EXPORT_SYMBOL(drm_plane_create_rotation_property);
299 * drm_rotation_simplify() - Try to simplify the rotation
300 * @rotation: Rotation to be simplified
301 * @supported_rotations: Supported rotations
303 * Attempt to simplify the rotation to a form that is supported.
304 * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
305 * one could call this function like this:
307 * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
308 * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
309 * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
311 * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
312 * transforms the hardware supports, this function may not
313 * be able to produce a supported transform, so the caller should
314 * check the result afterwards.
316 unsigned int drm_rotation_simplify(unsigned int rotation,
317 unsigned int supported_rotations)
319 if (rotation & ~supported_rotations) {
320 rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
321 rotation = (rotation & DRM_MODE_REFLECT_MASK) |
322 BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
328 EXPORT_SYMBOL(drm_rotation_simplify);
331 * drm_plane_create_zpos_property - create mutable zpos property
333 * @zpos: initial value of zpos property
334 * @min: minimal possible value of zpos property
335 * @max: maximal possible value of zpos property
337 * This function initializes generic mutable zpos property and enables support
338 * for it in drm core. Drivers can then attach this property to planes to enable
339 * support for configurable planes arrangement during blending operation.
340 * Drivers that attach a mutable zpos property to any plane should call the
341 * drm_atomic_normalize_zpos() helper during their implementation of
342 * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
343 * values and store them in &drm_plane_state.normalized_zpos. Usually min
344 * should be set to 0 and max to maximal number of planes for given crtc - 1.
346 * If zpos of some planes cannot be changed (like fixed background or
347 * cursor/topmost planes), driver should adjust min/max values and assign those
348 * planes immutable zpos property with lower or higher values (for more
349 * information, see drm_plane_create_zpos_immutable_property() function). In such
350 * case driver should also assign proper initial zpos values for all planes in
351 * its plane_reset() callback, so the planes will be always sorted properly.
353 * See also drm_atomic_normalize_zpos().
355 * The property exposed to userspace is called "zpos".
358 * Zero on success, negative errno on failure.
360 int drm_plane_create_zpos_property(struct drm_plane *plane,
362 unsigned int min, unsigned int max)
364 struct drm_property *prop;
366 prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
370 drm_object_attach_property(&plane->base, prop, zpos);
372 plane->zpos_property = prop;
375 plane->state->zpos = zpos;
376 plane->state->normalized_zpos = zpos;
381 EXPORT_SYMBOL(drm_plane_create_zpos_property);
384 * drm_plane_create_zpos_immutable_property - create immuttable zpos property
386 * @zpos: value of zpos property
388 * This function initializes generic immutable zpos property and enables
389 * support for it in drm core. Using this property driver lets userspace
390 * to get the arrangement of the planes for blending operation and notifies
391 * it that the hardware (or driver) doesn't support changing of the planes'
392 * order. For mutable zpos see drm_plane_create_zpos_property().
394 * The property exposed to userspace is called "zpos".
397 * Zero on success, negative errno on failure.
399 int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
402 struct drm_property *prop;
404 prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
409 drm_object_attach_property(&plane->base, prop, zpos);
411 plane->zpos_property = prop;
414 plane->state->zpos = zpos;
415 plane->state->normalized_zpos = zpos;
420 EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
422 static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
424 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
425 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
427 if (sa->zpos != sb->zpos)
428 return sa->zpos - sb->zpos;
430 return sa->plane->base.id - sb->plane->base.id;
433 static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
434 struct drm_crtc_state *crtc_state)
436 struct drm_atomic_state *state = crtc_state->state;
437 struct drm_device *dev = crtc->dev;
438 int total_planes = dev->mode_config.num_total_plane;
439 struct drm_plane_state **states;
440 struct drm_plane *plane;
444 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
445 crtc->base.id, crtc->name);
447 states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
452 * Normalization process might create new states for planes which
453 * normalized_zpos has to be recalculated.
455 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
456 struct drm_plane_state *plane_state =
457 drm_atomic_get_plane_state(state, plane);
458 if (IS_ERR(plane_state)) {
459 ret = PTR_ERR(plane_state);
462 states[n++] = plane_state;
463 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
464 plane->base.id, plane->name,
468 sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
470 for (i = 0; i < n; i++) {
471 plane = states[i]->plane;
473 states[i]->normalized_zpos = i;
474 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
475 plane->base.id, plane->name, i);
477 crtc_state->zpos_changed = true;
485 * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
487 * @state: atomic state of DRM device
489 * This function calculates normalized zpos value for all modified planes in
490 * the provided atomic state of DRM device.
492 * For every CRTC this function checks new states of all planes assigned to
493 * it and calculates normalized zpos value for these planes. Planes are compared
494 * first by their zpos values, then by plane id (if zpos is equal). The plane
495 * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
496 * is then filled with unique values from 0 to number of active planes in crtc
500 * Zero for success or -errno
502 int drm_atomic_normalize_zpos(struct drm_device *dev,
503 struct drm_atomic_state *state)
505 struct drm_crtc *crtc;
506 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
507 struct drm_plane *plane;
508 struct drm_plane_state *old_plane_state, *new_plane_state;
511 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
512 crtc = new_plane_state->crtc;
515 if (old_plane_state->zpos != new_plane_state->zpos) {
516 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
517 new_crtc_state->zpos_changed = true;
521 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
522 if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
523 new_crtc_state->zpos_changed) {
524 ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
532 EXPORT_SYMBOL(drm_atomic_normalize_zpos);
535 * drm_plane_create_blend_mode_property - create a new blend mode property
537 * @supported_modes: bitmask of supported modes, must include
538 * BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
539 * that alpha is premultiplied, and old userspace can break if
540 * the property defaults to anything else.
542 * This creates a new property describing the blend mode.
544 * The property exposed to userspace is an enumeration property (see
545 * drm_property_create_enum()) called "pixel blend mode" and has the
546 * following enumeration values:
549 * Blend formula that ignores the pixel alpha.
552 * Blend formula that assumes the pixel color values have been already
553 * pre-multiplied with the alpha channel values.
556 * Blend formula that assumes the pixel color values have not been
557 * pre-multiplied and will do so when blending them to the background color
561 * Zero for success or -errno
563 int drm_plane_create_blend_mode_property(struct drm_plane *plane,
564 unsigned int supported_modes)
566 struct drm_device *dev = plane->dev;
567 struct drm_property *prop;
568 static const struct drm_prop_enum_list props[] = {
569 { DRM_MODE_BLEND_PIXEL_NONE, "None" },
570 { DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
571 { DRM_MODE_BLEND_COVERAGE, "Coverage" },
573 unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
574 BIT(DRM_MODE_BLEND_PREMULTI) |
575 BIT(DRM_MODE_BLEND_COVERAGE);
578 if (WARN_ON((supported_modes & ~valid_mode_mask) ||
579 ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
582 prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
584 hweight32(supported_modes));
588 for (i = 0; i < ARRAY_SIZE(props); i++) {
591 if (!(BIT(props[i].type) & supported_modes))
594 ret = drm_property_add_enum(prop, props[i].type,
598 drm_property_destroy(dev, prop);
604 drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
605 plane->blend_mode_property = prop;
609 EXPORT_SYMBOL(drm_plane_create_blend_mode_property);