blob: e52aece309009be8f354a4b2368ba2d9804daf2b [file] [log] [blame]
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02001/*
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>
Daniel Vetter18733802016-09-21 10:59:26 +020028#include <drm/drm_blend.h>
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020029#include <linux/export.h>
30#include <linux/slab.h>
31#include <linux/sort.h>
32
Ville Syrjäläc30b4402016-09-19 16:33:43 +030033#include "drm_crtc_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020034
Daniel Vetter1e4d84c2016-09-21 10:59:27 +020035/**
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_mode_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_mode_create_rotation_property - create a new rotation property
109 * @dev: DRM device
110 * @supported_rotations: bitmask of supported rotations and reflections
111 *
112 * This creates a new property with the selected support for transformations.
113 * The resulting property should be stored in @rotation_property in
114 * &drm_mode_config. It then must be attached to each plane which supports
115 * rotations using drm_object_attach_property().
116 *
117 * FIXME: Probably better if the rotation property is created on each plane,
118 * like the zpos property. Otherwise it's not possible to allow different
119 * rotation modes on different planes.
120 *
121 * Since a rotation by 180° degress is the same as reflecting both along the x
122 * and the y axis the rotation property is somewhat redundant. Drivers can use
123 * drm_rotation_simplify() to normalize values of this property.
124 *
125 * The property exposed to userspace is a bitmask property (see
126 * drm_property_create_bitmask()) called "rotation" and has the following
127 * bitmask enumaration values:
128 *
129 * DRM_ROTATE_0:
130 * "rotate-0"
131 * DRM_ROTATE_90:
132 * "rotate-90"
133 * DRM_ROTATE_180:
134 * "rotate-180"
135 * DRM_ROTATE_270:
136 * "rotate-270"
137 * DRM_REFLECT_X:
138 * "reflect-x"
139 * DRM_REFELCT_Y:
140 * "reflect-y"
141 *
142 * Rotation is the specified amount in degrees in counter clockwise direction,
143 * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
144 * rotation. After reflection, the rotation is applied to the image sampled from
145 * the source rectangle, before scaling it to fit the destination rectangle.
146 */
Daniel Vetter18733802016-09-21 10:59:26 +0200147struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
148 unsigned int supported_rotations)
149{
150 static const struct drm_prop_enum_list props[] = {
151 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
152 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
153 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
154 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
155 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
156 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
157 };
158
159 return drm_property_create_bitmask(dev, 0, "rotation",
160 props, ARRAY_SIZE(props),
161 supported_rotations);
162}
163EXPORT_SYMBOL(drm_mode_create_rotation_property);
164
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300165int drm_plane_create_rotation_property(struct drm_plane *plane,
166 unsigned int rotation,
167 unsigned int supported_rotations)
168{
169 static const struct drm_prop_enum_list props[] = {
170 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
171 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
172 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
173 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
174 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
175 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
176 };
177 struct drm_property *prop;
178
179 WARN_ON((supported_rotations & DRM_ROTATE_MASK) == 0);
180 WARN_ON(!is_power_of_2(rotation & DRM_ROTATE_MASK));
181 WARN_ON(rotation & ~supported_rotations);
182
183 prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
184 props, ARRAY_SIZE(props),
185 supported_rotations);
186 if (!prop)
187 return -ENOMEM;
188
189 drm_object_attach_property(&plane->base, prop, rotation);
190
191 if (plane->state)
192 plane->state->rotation = rotation;
193
194 plane->rotation_property = prop;
195
196 return 0;
197}
198EXPORT_SYMBOL(drm_plane_create_rotation_property);
199
Daniel Vetter18733802016-09-21 10:59:26 +0200200/**
201 * drm_rotation_simplify() - Try to simplify the rotation
202 * @rotation: Rotation to be simplified
203 * @supported_rotations: Supported rotations
204 *
205 * Attempt to simplify the rotation to a form that is supported.
206 * Eg. if the hardware supports everything except DRM_REFLECT_X
207 * one could call this function like this:
208 *
209 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
210 * DRM_ROTATE_90 | DRM_ROTATE_180 |
211 * DRM_ROTATE_270 | DRM_REFLECT_Y);
212 *
213 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
214 * transforms the hardware supports, this function may not
215 * be able to produce a supported transform, so the caller should
216 * check the result afterwards.
217 */
218unsigned int drm_rotation_simplify(unsigned int rotation,
219 unsigned int supported_rotations)
220{
221 if (rotation & ~supported_rotations) {
222 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
223 rotation = (rotation & DRM_REFLECT_MASK) |
224 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
225 }
226
227 return rotation;
228}
229EXPORT_SYMBOL(drm_rotation_simplify);
230
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200231/**
232 * drm_plane_create_zpos_property - create mutable zpos property
233 * @plane: drm plane
234 * @zpos: initial value of zpos property
235 * @min: minimal possible value of zpos property
236 * @max: maximal possible value of zpos property
237 *
238 * This function initializes generic mutable zpos property and enables support
239 * for it in drm core. Drivers can then attach this property to planes to enable
240 * support for configurable planes arrangement during blending operation.
241 * Once mutable zpos property has been enabled, the DRM core will automatically
242 * calculate drm_plane_state->normalized_zpos values. Usually min should be set
243 * to 0 and max to maximal number of planes for given crtc - 1.
244 *
245 * If zpos of some planes cannot be changed (like fixed background or
246 * cursor/topmost planes), driver should adjust min/max values and assign those
247 * planes immutable zpos property with lower or higher values (for more
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200248 * information, see drm_plane_create_zpos_immutable_property() function). In such
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200249 * case driver should also assign proper initial zpos values for all planes in
250 * its plane_reset() callback, so the planes will be always sorted properly.
251 *
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200252 * See also drm_atomic_normalize_zpos().
253 *
254 * The property exposed to userspace is called "zpos".
255 *
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200256 * Returns:
257 * Zero on success, negative errno on failure.
258 */
259int drm_plane_create_zpos_property(struct drm_plane *plane,
260 unsigned int zpos,
261 unsigned int min, unsigned int max)
262{
263 struct drm_property *prop;
264
265 prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
266 if (!prop)
267 return -ENOMEM;
268
269 drm_object_attach_property(&plane->base, prop, zpos);
270
271 plane->zpos_property = prop;
272
273 if (plane->state) {
274 plane->state->zpos = zpos;
275 plane->state->normalized_zpos = zpos;
276 }
277
278 return 0;
279}
280EXPORT_SYMBOL(drm_plane_create_zpos_property);
281
282/**
283 * drm_plane_create_zpos_immutable_property - create immuttable zpos property
284 * @plane: drm plane
285 * @zpos: value of zpos property
286 *
287 * This function initializes generic immutable zpos property and enables
288 * support for it in drm core. Using this property driver lets userspace
289 * to get the arrangement of the planes for blending operation and notifies
290 * it that the hardware (or driver) doesn't support changing of the planes'
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200291 * order. For mutable zpos see drm_plane_create_zpos_property().
292 *
293 * The property exposed to userspace is called "zpos".
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200294 *
295 * Returns:
296 * Zero on success, negative errno on failure.
297 */
298int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
299 unsigned int zpos)
300{
301 struct drm_property *prop;
302
303 prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
304 "zpos", zpos, zpos);
305 if (!prop)
306 return -ENOMEM;
307
308 drm_object_attach_property(&plane->base, prop, zpos);
309
310 plane->zpos_property = prop;
311
312 if (plane->state) {
313 plane->state->zpos = zpos;
314 plane->state->normalized_zpos = zpos;
315 }
316
317 return 0;
318}
319EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
320
321static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
322{
323 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
324 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
325
326 if (sa->zpos != sb->zpos)
327 return sa->zpos - sb->zpos;
328 else
329 return sa->plane->base.id - sb->plane->base.id;
330}
331
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200332static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
333 struct drm_crtc_state *crtc_state)
334{
335 struct drm_atomic_state *state = crtc_state->state;
336 struct drm_device *dev = crtc->dev;
337 int total_planes = dev->mode_config.num_total_plane;
338 struct drm_plane_state **states;
339 struct drm_plane *plane;
340 int i, n = 0;
341 int ret = 0;
342
343 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
344 crtc->base.id, crtc->name);
345
346 states = kmalloc_array(total_planes, sizeof(*states), GFP_TEMPORARY);
347 if (!states)
348 return -ENOMEM;
349
350 /*
351 * Normalization process might create new states for planes which
352 * normalized_zpos has to be recalculated.
353 */
354 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
355 struct drm_plane_state *plane_state =
356 drm_atomic_get_plane_state(state, plane);
357 if (IS_ERR(plane_state)) {
358 ret = PTR_ERR(plane_state);
359 goto done;
360 }
361 states[n++] = plane_state;
362 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
363 plane->base.id, plane->name,
364 plane_state->zpos);
365 }
366
367 sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
368
369 for (i = 0; i < n; i++) {
370 plane = states[i]->plane;
371
372 states[i]->normalized_zpos = i;
373 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
374 plane->base.id, plane->name, i);
375 }
376 crtc_state->zpos_changed = true;
377
378done:
379 kfree(states);
380 return ret;
381}
382
383/**
Daniel Vetter52a9fcd2016-08-12 22:48:51 +0200384 * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200385 * @dev: DRM device
386 * @state: atomic state of DRM device
387 *
388 * This function calculates normalized zpos value for all modified planes in
Daniel Vetter1e4d84c2016-09-21 10:59:27 +0200389 * the provided atomic state of DRM device.
390 *
391 * For every CRTC this function checks new states of all planes assigned to
392 * it and calculates normalized zpos value for these planes. Planes are compared
393 * first by their zpos values, then by plane id (if zpos is equal). The plane
394 * with lowest zpos value is at the bottom. The plane_state->normalized_zpos is
395 * then filled with unique values from 0 to number of active planes in crtc
396 * minus one.
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200397 *
398 * RETURNS
399 * Zero for success or -errno
400 */
Daniel Vetter52a9fcd2016-08-12 22:48:51 +0200401int drm_atomic_normalize_zpos(struct drm_device *dev,
402 struct drm_atomic_state *state)
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200403{
404 struct drm_crtc *crtc;
405 struct drm_crtc_state *crtc_state;
406 struct drm_plane *plane;
407 struct drm_plane_state *plane_state;
408 int i, ret = 0;
409
410 for_each_plane_in_state(state, plane, plane_state, i) {
411 crtc = plane_state->crtc;
412 if (!crtc)
413 continue;
414 if (plane->state->zpos != plane_state->zpos) {
415 crtc_state =
416 drm_atomic_get_existing_crtc_state(state, crtc);
417 crtc_state->zpos_changed = true;
418 }
419 }
420
421 for_each_crtc_in_state(state, crtc, crtc_state, i) {
422 if (crtc_state->plane_mask != crtc->state->plane_mask ||
423 crtc_state->zpos_changed) {
424 ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
425 crtc_state);
426 if (ret)
427 return ret;
428 }
429 }
430 return 0;
431}
Daniel Vetter52a9fcd2016-08-12 22:48:51 +0200432EXPORT_SYMBOL(drm_atomic_normalize_zpos);