Commit | Line | Data |
---|---|---|
44d1240d MS |
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> | |
18733802 | 28 | #include <drm/drm_blend.h> |
44d1240d MS |
29 | #include <linux/export.h> |
30 | #include <linux/slab.h> | |
31 | #include <linux/sort.h> | |
32 | ||
c30b4400 | 33 | #include "drm_crtc_internal.h" |
44d1240d | 34 | |
1e4d84c6 DV |
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 | |
d574528a DV |
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. | |
1e4d84c6 DV |
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 | * | |
ae0e2826 MR |
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 | * | |
4d10bd45 DV |
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. | |
1e4d84c6 | 103 | * |
4d10bd45 DV |
104 | * zpos: |
105 | * Z position is set up with drm_plane_create_zpos_immutable_property() and | |
106 | * drm_plane_create_zpos_property(). It controls the visibility of overlapping | |
107 | * planes. Without this property the primary plane is always below the cursor | |
108 | * plane, and ordering between all other planes is undefined. | |
1e4d84c6 DV |
109 | * |
110 | * Note that all the property extensions described here apply either to the | |
111 | * plane or the CRTC (e.g. for the background color, which currently is not | |
112 | * exposed and assumed to be black). | |
113 | */ | |
114 | ||
ae0e2826 MR |
115 | /** |
116 | * drm_plane_create_alpha_property - create a new alpha property | |
117 | * @plane: drm plane | |
118 | * | |
119 | * This function creates a generic, mutable, alpha property and enables support | |
120 | * for it in the DRM core. It is attached to @plane. | |
121 | * | |
122 | * The alpha property will be allowed to be within the bounds of 0 | |
123 | * (transparent) to 0xffff (opaque). | |
124 | * | |
125 | * Returns: | |
126 | * 0 on success, negative error code on failure. | |
127 | */ | |
128 | int drm_plane_create_alpha_property(struct drm_plane *plane) | |
129 | { | |
130 | struct drm_property *prop; | |
131 | ||
132 | prop = drm_property_create_range(plane->dev, 0, "alpha", | |
133 | 0, DRM_BLEND_ALPHA_OPAQUE); | |
134 | if (!prop) | |
135 | return -ENOMEM; | |
136 | ||
137 | drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE); | |
138 | plane->alpha_property = prop; | |
139 | ||
140 | if (plane->state) | |
141 | plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | EXPORT_SYMBOL(drm_plane_create_alpha_property); | |
146 | ||
1e4d84c6 | 147 | /** |
6686df8c VS |
148 | * drm_plane_create_rotation_property - create a new rotation property |
149 | * @plane: drm plane | |
150 | * @rotation: initial value of the rotation property | |
1e4d84c6 DV |
151 | * @supported_rotations: bitmask of supported rotations and reflections |
152 | * | |
153 | * This creates a new property with the selected support for transformations. | |
1e4d84c6 DV |
154 | * |
155 | * Since a rotation by 180° degress is the same as reflecting both along the x | |
156 | * and the y axis the rotation property is somewhat redundant. Drivers can use | |
157 | * drm_rotation_simplify() to normalize values of this property. | |
158 | * | |
159 | * The property exposed to userspace is a bitmask property (see | |
160 | * drm_property_create_bitmask()) called "rotation" and has the following | |
161 | * bitmask enumaration values: | |
162 | * | |
c2c446ad | 163 | * DRM_MODE_ROTATE_0: |
1e4d84c6 | 164 | * "rotate-0" |
c2c446ad | 165 | * DRM_MODE_ROTATE_90: |
1e4d84c6 | 166 | * "rotate-90" |
c2c446ad | 167 | * DRM_MODE_ROTATE_180: |
1e4d84c6 | 168 | * "rotate-180" |
c2c446ad | 169 | * DRM_MODE_ROTATE_270: |
1e4d84c6 | 170 | * "rotate-270" |
c2c446ad | 171 | * DRM_MODE_REFLECT_X: |
1e4d84c6 | 172 | * "reflect-x" |
c2c446ad | 173 | * DRM_MODE_REFLECT_Y: |
1e4d84c6 DV |
174 | * "reflect-y" |
175 | * | |
176 | * Rotation is the specified amount in degrees in counter clockwise direction, | |
177 | * the X and Y axis are within the source rectangle, i.e. the X/Y axis before | |
178 | * rotation. After reflection, the rotation is applied to the image sampled from | |
179 | * the source rectangle, before scaling it to fit the destination rectangle. | |
180 | */ | |
d138dd3c VS |
181 | int drm_plane_create_rotation_property(struct drm_plane *plane, |
182 | unsigned int rotation, | |
183 | unsigned int supported_rotations) | |
184 | { | |
185 | static const struct drm_prop_enum_list props[] = { | |
c2c446ad RF |
186 | { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" }, |
187 | { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" }, | |
188 | { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" }, | |
189 | { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" }, | |
190 | { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" }, | |
191 | { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" }, | |
d138dd3c VS |
192 | }; |
193 | struct drm_property *prop; | |
194 | ||
c2c446ad RF |
195 | WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0); |
196 | WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK)); | |
d138dd3c VS |
197 | WARN_ON(rotation & ~supported_rotations); |
198 | ||
199 | prop = drm_property_create_bitmask(plane->dev, 0, "rotation", | |
200 | props, ARRAY_SIZE(props), | |
201 | supported_rotations); | |
202 | if (!prop) | |
203 | return -ENOMEM; | |
204 | ||
205 | drm_object_attach_property(&plane->base, prop, rotation); | |
206 | ||
207 | if (plane->state) | |
208 | plane->state->rotation = rotation; | |
209 | ||
210 | plane->rotation_property = prop; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | EXPORT_SYMBOL(drm_plane_create_rotation_property); | |
215 | ||
18733802 DV |
216 | /** |
217 | * drm_rotation_simplify() - Try to simplify the rotation | |
218 | * @rotation: Rotation to be simplified | |
219 | * @supported_rotations: Supported rotations | |
220 | * | |
221 | * Attempt to simplify the rotation to a form that is supported. | |
c2c446ad | 222 | * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X |
18733802 DV |
223 | * one could call this function like this: |
224 | * | |
c2c446ad RF |
225 | * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 | |
226 | * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 | | |
227 | * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y); | |
18733802 | 228 | * |
c2c446ad | 229 | * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of |
18733802 DV |
230 | * transforms the hardware supports, this function may not |
231 | * be able to produce a supported transform, so the caller should | |
232 | * check the result afterwards. | |
233 | */ | |
234 | unsigned int drm_rotation_simplify(unsigned int rotation, | |
235 | unsigned int supported_rotations) | |
236 | { | |
237 | if (rotation & ~supported_rotations) { | |
c2c446ad RF |
238 | rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y; |
239 | rotation = (rotation & DRM_MODE_REFLECT_MASK) | | |
240 | BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1) | |
241 | % 4); | |
18733802 DV |
242 | } |
243 | ||
244 | return rotation; | |
245 | } | |
246 | EXPORT_SYMBOL(drm_rotation_simplify); | |
247 | ||
44d1240d MS |
248 | /** |
249 | * drm_plane_create_zpos_property - create mutable zpos property | |
250 | * @plane: drm plane | |
251 | * @zpos: initial value of zpos property | |
252 | * @min: minimal possible value of zpos property | |
253 | * @max: maximal possible value of zpos property | |
254 | * | |
255 | * This function initializes generic mutable zpos property and enables support | |
256 | * for it in drm core. Drivers can then attach this property to planes to enable | |
257 | * support for configurable planes arrangement during blending operation. | |
ca40cfc8 TR |
258 | * Drivers that attach a mutable zpos property to any plane should call the |
259 | * drm_atomic_normalize_zpos() helper during their implementation of | |
260 | * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos | |
261 | * values and store them in &drm_plane_state.normalized_zpos. Usually min | |
262 | * should be set to 0 and max to maximal number of planes for given crtc - 1. | |
44d1240d MS |
263 | * |
264 | * If zpos of some planes cannot be changed (like fixed background or | |
265 | * cursor/topmost planes), driver should adjust min/max values and assign those | |
266 | * planes immutable zpos property with lower or higher values (for more | |
1e4d84c6 | 267 | * information, see drm_plane_create_zpos_immutable_property() function). In such |
44d1240d MS |
268 | * case driver should also assign proper initial zpos values for all planes in |
269 | * its plane_reset() callback, so the planes will be always sorted properly. | |
270 | * | |
1e4d84c6 DV |
271 | * See also drm_atomic_normalize_zpos(). |
272 | * | |
273 | * The property exposed to userspace is called "zpos". | |
274 | * | |
44d1240d MS |
275 | * Returns: |
276 | * Zero on success, negative errno on failure. | |
277 | */ | |
278 | int drm_plane_create_zpos_property(struct drm_plane *plane, | |
279 | unsigned int zpos, | |
280 | unsigned int min, unsigned int max) | |
281 | { | |
282 | struct drm_property *prop; | |
283 | ||
284 | prop = drm_property_create_range(plane->dev, 0, "zpos", min, max); | |
285 | if (!prop) | |
286 | return -ENOMEM; | |
287 | ||
288 | drm_object_attach_property(&plane->base, prop, zpos); | |
289 | ||
290 | plane->zpos_property = prop; | |
291 | ||
292 | if (plane->state) { | |
293 | plane->state->zpos = zpos; | |
294 | plane->state->normalized_zpos = zpos; | |
295 | } | |
296 | ||
297 | return 0; | |
298 | } | |
299 | EXPORT_SYMBOL(drm_plane_create_zpos_property); | |
300 | ||
301 | /** | |
302 | * drm_plane_create_zpos_immutable_property - create immuttable zpos property | |
303 | * @plane: drm plane | |
304 | * @zpos: value of zpos property | |
305 | * | |
306 | * This function initializes generic immutable zpos property and enables | |
307 | * support for it in drm core. Using this property driver lets userspace | |
308 | * to get the arrangement of the planes for blending operation and notifies | |
309 | * it that the hardware (or driver) doesn't support changing of the planes' | |
1e4d84c6 DV |
310 | * order. For mutable zpos see drm_plane_create_zpos_property(). |
311 | * | |
312 | * The property exposed to userspace is called "zpos". | |
44d1240d MS |
313 | * |
314 | * Returns: | |
315 | * Zero on success, negative errno on failure. | |
316 | */ | |
317 | int drm_plane_create_zpos_immutable_property(struct drm_plane *plane, | |
318 | unsigned int zpos) | |
319 | { | |
320 | struct drm_property *prop; | |
321 | ||
322 | prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE, | |
323 | "zpos", zpos, zpos); | |
324 | if (!prop) | |
325 | return -ENOMEM; | |
326 | ||
327 | drm_object_attach_property(&plane->base, prop, zpos); | |
328 | ||
329 | plane->zpos_property = prop; | |
330 | ||
331 | if (plane->state) { | |
332 | plane->state->zpos = zpos; | |
333 | plane->state->normalized_zpos = zpos; | |
334 | } | |
335 | ||
336 | return 0; | |
337 | } | |
338 | EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property); | |
339 | ||
340 | static int drm_atomic_state_zpos_cmp(const void *a, const void *b) | |
341 | { | |
342 | const struct drm_plane_state *sa = *(struct drm_plane_state **)a; | |
343 | const struct drm_plane_state *sb = *(struct drm_plane_state **)b; | |
344 | ||
345 | if (sa->zpos != sb->zpos) | |
346 | return sa->zpos - sb->zpos; | |
347 | else | |
348 | return sa->plane->base.id - sb->plane->base.id; | |
349 | } | |
350 | ||
44d1240d MS |
351 | static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc, |
352 | struct drm_crtc_state *crtc_state) | |
353 | { | |
354 | struct drm_atomic_state *state = crtc_state->state; | |
355 | struct drm_device *dev = crtc->dev; | |
356 | int total_planes = dev->mode_config.num_total_plane; | |
357 | struct drm_plane_state **states; | |
358 | struct drm_plane *plane; | |
359 | int i, n = 0; | |
360 | int ret = 0; | |
361 | ||
362 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n", | |
363 | crtc->base.id, crtc->name); | |
364 | ||
0ee931c4 | 365 | states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL); |
44d1240d MS |
366 | if (!states) |
367 | return -ENOMEM; | |
368 | ||
369 | /* | |
370 | * Normalization process might create new states for planes which | |
371 | * normalized_zpos has to be recalculated. | |
372 | */ | |
373 | drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) { | |
374 | struct drm_plane_state *plane_state = | |
375 | drm_atomic_get_plane_state(state, plane); | |
376 | if (IS_ERR(plane_state)) { | |
377 | ret = PTR_ERR(plane_state); | |
378 | goto done; | |
379 | } | |
380 | states[n++] = plane_state; | |
381 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n", | |
382 | plane->base.id, plane->name, | |
383 | plane_state->zpos); | |
384 | } | |
385 | ||
386 | sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL); | |
387 | ||
388 | for (i = 0; i < n; i++) { | |
389 | plane = states[i]->plane; | |
390 | ||
391 | states[i]->normalized_zpos = i; | |
392 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n", | |
393 | plane->base.id, plane->name, i); | |
394 | } | |
395 | crtc_state->zpos_changed = true; | |
396 | ||
397 | done: | |
398 | kfree(states); | |
399 | return ret; | |
400 | } | |
401 | ||
402 | /** | |
52a9fcda | 403 | * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs |
44d1240d MS |
404 | * @dev: DRM device |
405 | * @state: atomic state of DRM device | |
406 | * | |
407 | * This function calculates normalized zpos value for all modified planes in | |
1e4d84c6 DV |
408 | * the provided atomic state of DRM device. |
409 | * | |
410 | * For every CRTC this function checks new states of all planes assigned to | |
411 | * it and calculates normalized zpos value for these planes. Planes are compared | |
412 | * first by their zpos values, then by plane id (if zpos is equal). The plane | |
d574528a DV |
413 | * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos |
414 | * is then filled with unique values from 0 to number of active planes in crtc | |
1e4d84c6 | 415 | * minus one. |
44d1240d MS |
416 | * |
417 | * RETURNS | |
418 | * Zero for success or -errno | |
419 | */ | |
52a9fcda DV |
420 | int drm_atomic_normalize_zpos(struct drm_device *dev, |
421 | struct drm_atomic_state *state) | |
44d1240d MS |
422 | { |
423 | struct drm_crtc *crtc; | |
62c7bdc0 | 424 | struct drm_crtc_state *old_crtc_state, *new_crtc_state; |
44d1240d | 425 | struct drm_plane *plane; |
62c7bdc0 | 426 | struct drm_plane_state *old_plane_state, *new_plane_state; |
44d1240d MS |
427 | int i, ret = 0; |
428 | ||
62c7bdc0 ML |
429 | for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { |
430 | crtc = new_plane_state->crtc; | |
44d1240d MS |
431 | if (!crtc) |
432 | continue; | |
62c7bdc0 ML |
433 | if (old_plane_state->zpos != new_plane_state->zpos) { |
434 | new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); | |
435 | new_crtc_state->zpos_changed = true; | |
44d1240d MS |
436 | } |
437 | } | |
438 | ||
62c7bdc0 ML |
439 | for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { |
440 | if (old_crtc_state->plane_mask != new_crtc_state->plane_mask || | |
441 | new_crtc_state->zpos_changed) { | |
44d1240d | 442 | ret = drm_atomic_helper_crtc_normalize_zpos(crtc, |
62c7bdc0 | 443 | new_crtc_state); |
44d1240d MS |
444 | if (ret) |
445 | return ret; | |
446 | } | |
447 | } | |
448 | return 0; | |
449 | } | |
52a9fcda | 450 | EXPORT_SYMBOL(drm_atomic_normalize_zpos); |