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