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 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.
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.
134 *
135 * pixel blend mode:
136 * Pixel blend mode is set up with drm_plane_create_blend_mode_property().
137 * It adds a blend mode for alpha blending equation selection, describing
138 * how the pixels from the current plane are composited with the
139 * background.
140 *
141 * Three alpha blending equations are defined:
142 *
143 * "None":
144 * Blend formula that ignores the pixel alpha::
145 *
146 * out.rgb = plane_alpha * fg.rgb +
147 * (1 - plane_alpha) * bg.rgb
148 *
149 * "Pre-multiplied":
150 * Blend formula that assumes the pixel color values
151 * have been already pre-multiplied with the alpha
152 * channel values::
153 *
154 * out.rgb = plane_alpha * fg.rgb +
155 * (1 - (plane_alpha * fg.alpha)) * bg.rgb
156 *
157 * "Coverage":
158 * Blend formula that assumes the pixel color values have not
159 * been pre-multiplied and will do so when blending them to the
160 * background color values::
161 *
162 * out.rgb = plane_alpha * fg.alpha * fg.rgb +
163 * (1 - (plane_alpha * fg.alpha)) * bg.rgb
164 *
165 * Using the following symbols:
166 *
167 * "fg.rgb":
168 * Each of the RGB component values from the plane's pixel
169 * "fg.alpha":
170 * Alpha component value from the plane's pixel. If the plane's
171 * pixel format has no alpha component, then this is assumed to be
172 * 1.0. In these cases, this property has no effect, as all three
173 * equations become equivalent.
174 * "bg.rgb":
175 * Each of the RGB component values from the background
176 * "plane_alpha":
177 * Plane alpha value set by the plane "alpha" property. If the
178 * plane does not expose the "alpha" property, then this is
179 * assumed to be 1.0
180 *
181 * Note that all the property extensions described here apply either to the
182 * plane or the CRTC (e.g. for the background color, which currently is not
183 * exposed and assumed to be black).
184 */
185
186 /**
187 * drm_plane_create_alpha_property - create a new alpha property
188 * @plane: drm plane
189 *
190 * This function creates a generic, mutable, alpha property and enables support
191 * for it in the DRM core. It is attached to @plane.
192 *
193 * The alpha property will be allowed to be within the bounds of 0
194 * (transparent) to 0xffff (opaque).
195 *
196 * Returns:
197 * 0 on success, negative error code on failure.
198 */
drm_plane_create_alpha_property(struct drm_plane * plane)199 int drm_plane_create_alpha_property(struct drm_plane *plane)
200 {
201 struct drm_property *prop;
202
203 prop = drm_property_create_range(plane->dev, 0, "alpha",
204 0, DRM_BLEND_ALPHA_OPAQUE);
205 if (!prop)
206 return -ENOMEM;
207
208 drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
209 plane->alpha_property = prop;
210
211 if (plane->state)
212 plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
213
214 return 0;
215 }
216 EXPORT_SYMBOL(drm_plane_create_alpha_property);
217
218 /**
219 * drm_plane_create_rotation_property - create a new rotation property
220 * @plane: drm plane
221 * @rotation: initial value of the rotation property
222 * @supported_rotations: bitmask of supported rotations and reflections
223 *
224 * This creates a new property with the selected support for transformations.
225 *
226 * Since a rotation by 180° degress is the same as reflecting both along the x
227 * and the y axis the rotation property is somewhat redundant. Drivers can use
228 * drm_rotation_simplify() to normalize values of this property.
229 *
230 * The property exposed to userspace is a bitmask property (see
231 * drm_property_create_bitmask()) called "rotation" and has the following
232 * bitmask enumaration values:
233 *
234 * DRM_MODE_ROTATE_0:
235 * "rotate-0"
236 * DRM_MODE_ROTATE_90:
237 * "rotate-90"
238 * DRM_MODE_ROTATE_180:
239 * "rotate-180"
240 * DRM_MODE_ROTATE_270:
241 * "rotate-270"
242 * DRM_MODE_REFLECT_X:
243 * "reflect-x"
244 * DRM_MODE_REFLECT_Y:
245 * "reflect-y"
246 *
247 * Rotation is the specified amount in degrees in counter clockwise direction,
248 * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
249 * rotation. After reflection, the rotation is applied to the image sampled from
250 * the source rectangle, before scaling it to fit the destination rectangle.
251 */
drm_plane_create_rotation_property(struct drm_plane * plane,unsigned int rotation,unsigned int supported_rotations)252 int drm_plane_create_rotation_property(struct drm_plane *plane,
253 unsigned int rotation,
254 unsigned int supported_rotations)
255 {
256 static const struct drm_prop_enum_list props[] = {
257 { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
258 { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
259 { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
260 { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
261 { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
262 { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
263 };
264 struct drm_property *prop;
265
266 WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
267 WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
268 WARN_ON(rotation & ~supported_rotations);
269
270 prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
271 props, ARRAY_SIZE(props),
272 supported_rotations);
273 if (!prop)
274 return -ENOMEM;
275
276 drm_object_attach_property(&plane->base, prop, rotation);
277
278 if (plane->state)
279 plane->state->rotation = rotation;
280
281 plane->rotation_property = prop;
282
283 return 0;
284 }
285 EXPORT_SYMBOL(drm_plane_create_rotation_property);
286
287 /**
288 * drm_rotation_simplify() - Try to simplify the rotation
289 * @rotation: Rotation to be simplified
290 * @supported_rotations: Supported rotations
291 *
292 * Attempt to simplify the rotation to a form that is supported.
293 * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
294 * one could call this function like this:
295 *
296 * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
297 * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
298 * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
299 *
300 * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
301 * transforms the hardware supports, this function may not
302 * be able to produce a supported transform, so the caller should
303 * check the result afterwards.
304 */
drm_rotation_simplify(unsigned int rotation,unsigned int supported_rotations)305 unsigned int drm_rotation_simplify(unsigned int rotation,
306 unsigned int supported_rotations)
307 {
308 if (rotation & ~supported_rotations) {
309 rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
310 rotation = (rotation & DRM_MODE_REFLECT_MASK) |
311 BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
312 % 4);
313 }
314
315 return rotation;
316 }
317 EXPORT_SYMBOL(drm_rotation_simplify);
318
319 /**
320 * drm_plane_create_zpos_property - create mutable zpos property
321 * @plane: drm plane
322 * @zpos: initial value of zpos property
323 * @min: minimal possible value of zpos property
324 * @max: maximal possible value of zpos property
325 *
326 * This function initializes generic mutable zpos property and enables support
327 * for it in drm core. Drivers can then attach this property to planes to enable
328 * support for configurable planes arrangement during blending operation.
329 * Drivers that attach a mutable zpos property to any plane should call the
330 * drm_atomic_normalize_zpos() helper during their implementation of
331 * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
332 * values and store them in &drm_plane_state.normalized_zpos. Usually min
333 * should be set to 0 and max to maximal number of planes for given crtc - 1.
334 *
335 * If zpos of some planes cannot be changed (like fixed background or
336 * cursor/topmost planes), driver should adjust min/max values and assign those
337 * planes immutable zpos property with lower or higher values (for more
338 * information, see drm_plane_create_zpos_immutable_property() function). In such
339 * case driver should also assign proper initial zpos values for all planes in
340 * its plane_reset() callback, so the planes will be always sorted properly.
341 *
342 * See also drm_atomic_normalize_zpos().
343 *
344 * The property exposed to userspace is called "zpos".
345 *
346 * Returns:
347 * Zero on success, negative errno on failure.
348 */
drm_plane_create_zpos_property(struct drm_plane * plane,unsigned int zpos,unsigned int min,unsigned int max)349 int drm_plane_create_zpos_property(struct drm_plane *plane,
350 unsigned int zpos,
351 unsigned int min, unsigned int max)
352 {
353 struct drm_property *prop;
354
355 prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
356 if (!prop)
357 return -ENOMEM;
358
359 drm_object_attach_property(&plane->base, prop, zpos);
360
361 plane->zpos_property = prop;
362
363 if (plane->state) {
364 plane->state->zpos = zpos;
365 plane->state->normalized_zpos = zpos;
366 }
367
368 return 0;
369 }
370 EXPORT_SYMBOL(drm_plane_create_zpos_property);
371
372 /**
373 * drm_plane_create_zpos_immutable_property - create immuttable zpos property
374 * @plane: drm plane
375 * @zpos: value of zpos property
376 *
377 * This function initializes generic immutable zpos property and enables
378 * support for it in drm core. Using this property driver lets userspace
379 * to get the arrangement of the planes for blending operation and notifies
380 * it that the hardware (or driver) doesn't support changing of the planes'
381 * order. For mutable zpos see drm_plane_create_zpos_property().
382 *
383 * The property exposed to userspace is called "zpos".
384 *
385 * Returns:
386 * Zero on success, negative errno on failure.
387 */
drm_plane_create_zpos_immutable_property(struct drm_plane * plane,unsigned int zpos)388 int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
389 unsigned int zpos)
390 {
391 struct drm_property *prop;
392
393 prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
394 "zpos", zpos, zpos);
395 if (!prop)
396 return -ENOMEM;
397
398 drm_object_attach_property(&plane->base, prop, zpos);
399
400 plane->zpos_property = prop;
401
402 if (plane->state) {
403 plane->state->zpos = zpos;
404 plane->state->normalized_zpos = zpos;
405 }
406
407 return 0;
408 }
409 EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
410
drm_atomic_state_zpos_cmp(const void * a,const void * b)411 static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
412 {
413 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
414 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
415
416 if (sa->zpos != sb->zpos)
417 return sa->zpos - sb->zpos;
418 else
419 return sa->plane->base.id - sb->plane->base.id;
420 }
421
drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)422 static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
423 struct drm_crtc_state *crtc_state)
424 {
425 struct drm_atomic_state *state = crtc_state->state;
426 struct drm_device *dev = crtc->dev;
427 int total_planes = dev->mode_config.num_total_plane;
428 struct drm_plane_state **states;
429 struct drm_plane *plane;
430 int i, n = 0;
431 int ret = 0;
432
433 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
434 crtc->base.id, crtc->name);
435
436 states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
437 if (!states)
438 return -ENOMEM;
439
440 /*
441 * Normalization process might create new states for planes which
442 * normalized_zpos has to be recalculated.
443 */
444 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
445 struct drm_plane_state *plane_state =
446 drm_atomic_get_plane_state(state, plane);
447 if (IS_ERR(plane_state)) {
448 ret = PTR_ERR(plane_state);
449 goto done;
450 }
451 states[n++] = plane_state;
452 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
453 plane->base.id, plane->name,
454 plane_state->zpos);
455 }
456
457 sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
458
459 for (i = 0; i < n; i++) {
460 plane = states[i]->plane;
461
462 states[i]->normalized_zpos = i;
463 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
464 plane->base.id, plane->name, i);
465 }
466 crtc_state->zpos_changed = true;
467
468 done:
469 kfree(states);
470 return ret;
471 }
472
473 /**
474 * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
475 * @dev: DRM device
476 * @state: atomic state of DRM device
477 *
478 * This function calculates normalized zpos value for all modified planes in
479 * the provided atomic state of DRM device.
480 *
481 * For every CRTC this function checks new states of all planes assigned to
482 * it and calculates normalized zpos value for these planes. Planes are compared
483 * first by their zpos values, then by plane id (if zpos is equal). The plane
484 * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
485 * is then filled with unique values from 0 to number of active planes in crtc
486 * minus one.
487 *
488 * RETURNS
489 * Zero for success or -errno
490 */
drm_atomic_normalize_zpos(struct drm_device * dev,struct drm_atomic_state * state)491 int drm_atomic_normalize_zpos(struct drm_device *dev,
492 struct drm_atomic_state *state)
493 {
494 struct drm_crtc *crtc;
495 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
496 struct drm_plane *plane;
497 struct drm_plane_state *old_plane_state, *new_plane_state;
498 int i, ret = 0;
499
500 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
501 crtc = new_plane_state->crtc;
502 if (!crtc)
503 continue;
504 if (old_plane_state->zpos != new_plane_state->zpos) {
505 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
506 new_crtc_state->zpos_changed = true;
507 }
508 }
509
510 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
511 if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
512 new_crtc_state->zpos_changed) {
513 ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
514 new_crtc_state);
515 if (ret)
516 return ret;
517 }
518 }
519 return 0;
520 }
521 EXPORT_SYMBOL(drm_atomic_normalize_zpos);
522
523 /**
524 * drm_plane_create_blend_mode_property - create a new blend mode property
525 * @plane: drm plane
526 * @supported_modes: bitmask of supported modes, must include
527 * BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
528 * that alpha is premultiplied, and old userspace can break if
529 * the property defaults to anything else.
530 *
531 * This creates a new property describing the blend mode.
532 *
533 * The property exposed to userspace is an enumeration property (see
534 * drm_property_create_enum()) called "pixel blend mode" and has the
535 * following enumeration values:
536 *
537 * "None":
538 * Blend formula that ignores the pixel alpha.
539 *
540 * "Pre-multiplied":
541 * Blend formula that assumes the pixel color values have been already
542 * pre-multiplied with the alpha channel values.
543 *
544 * "Coverage":
545 * Blend formula that assumes the pixel color values have not been
546 * pre-multiplied and will do so when blending them to the background color
547 * values.
548 *
549 * RETURNS:
550 * Zero for success or -errno
551 */
drm_plane_create_blend_mode_property(struct drm_plane * plane,unsigned int supported_modes)552 int drm_plane_create_blend_mode_property(struct drm_plane *plane,
553 unsigned int supported_modes)
554 {
555 struct drm_device *dev = plane->dev;
556 struct drm_property *prop;
557 static const struct drm_prop_enum_list props[] = {
558 { DRM_MODE_BLEND_PIXEL_NONE, "None" },
559 { DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
560 { DRM_MODE_BLEND_COVERAGE, "Coverage" },
561 };
562 unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
563 BIT(DRM_MODE_BLEND_PREMULTI) |
564 BIT(DRM_MODE_BLEND_COVERAGE);
565 int i;
566
567 if (WARN_ON((supported_modes & ~valid_mode_mask) ||
568 ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
569 return -EINVAL;
570
571 prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
572 "pixel blend mode",
573 hweight32(supported_modes));
574 if (!prop)
575 return -ENOMEM;
576
577 for (i = 0; i < ARRAY_SIZE(props); i++) {
578 int ret;
579
580 if (!(BIT(props[i].type) & supported_modes))
581 continue;
582
583 ret = drm_property_add_enum(prop, props[i].type,
584 props[i].name);
585
586 if (ret) {
587 drm_property_destroy(dev, prop);
588
589 return ret;
590 }
591 }
592
593 drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
594 plane->blend_mode_property = prop;
595
596 return 0;
597 }
598 EXPORT_SYMBOL(drm_plane_create_blend_mode_property);
599