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