Marek Szyprowski | 44d1240d | 2016-06-13 11:11:26 +0200 | [diff] [blame] | 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> |
Daniel Vetter | 1873380 | 2016-09-21 10:59:26 +0200 | [diff] [blame^] | 28 | #include <drm/drm_blend.h> |
Marek Szyprowski | 44d1240d | 2016-06-13 11:11:26 +0200 | [diff] [blame] | 29 | #include <linux/export.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/sort.h> |
| 32 | |
Ville Syrjälä | c30b440 | 2016-09-19 16:33:43 +0300 | [diff] [blame] | 33 | #include "drm_crtc_internal.h" |
Marek Szyprowski | 44d1240d | 2016-06-13 11:11:26 +0200 | [diff] [blame] | 34 | |
Daniel Vetter | 1873380 | 2016-09-21 10:59:26 +0200 | [diff] [blame^] | 35 | struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev, |
| 36 | unsigned int supported_rotations) |
| 37 | { |
| 38 | static const struct drm_prop_enum_list props[] = { |
| 39 | { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" }, |
| 40 | { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" }, |
| 41 | { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" }, |
| 42 | { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" }, |
| 43 | { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" }, |
| 44 | { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" }, |
| 45 | }; |
| 46 | |
| 47 | return drm_property_create_bitmask(dev, 0, "rotation", |
| 48 | props, ARRAY_SIZE(props), |
| 49 | supported_rotations); |
| 50 | } |
| 51 | EXPORT_SYMBOL(drm_mode_create_rotation_property); |
| 52 | |
| 53 | /** |
| 54 | * drm_rotation_simplify() - Try to simplify the rotation |
| 55 | * @rotation: Rotation to be simplified |
| 56 | * @supported_rotations: Supported rotations |
| 57 | * |
| 58 | * Attempt to simplify the rotation to a form that is supported. |
| 59 | * Eg. if the hardware supports everything except DRM_REFLECT_X |
| 60 | * one could call this function like this: |
| 61 | * |
| 62 | * drm_rotation_simplify(rotation, DRM_ROTATE_0 | |
| 63 | * DRM_ROTATE_90 | DRM_ROTATE_180 | |
| 64 | * DRM_ROTATE_270 | DRM_REFLECT_Y); |
| 65 | * |
| 66 | * to eliminate the DRM_ROTATE_X flag. Depending on what kind of |
| 67 | * transforms the hardware supports, this function may not |
| 68 | * be able to produce a supported transform, so the caller should |
| 69 | * check the result afterwards. |
| 70 | */ |
| 71 | unsigned int drm_rotation_simplify(unsigned int rotation, |
| 72 | unsigned int supported_rotations) |
| 73 | { |
| 74 | if (rotation & ~supported_rotations) { |
| 75 | rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y; |
| 76 | rotation = (rotation & DRM_REFLECT_MASK) | |
| 77 | BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4); |
| 78 | } |
| 79 | |
| 80 | return rotation; |
| 81 | } |
| 82 | EXPORT_SYMBOL(drm_rotation_simplify); |
| 83 | |
Marek Szyprowski | 44d1240d | 2016-06-13 11:11:26 +0200 | [diff] [blame] | 84 | /** |
| 85 | * drm_plane_create_zpos_property - create mutable zpos property |
| 86 | * @plane: drm plane |
| 87 | * @zpos: initial value of zpos property |
| 88 | * @min: minimal possible value of zpos property |
| 89 | * @max: maximal possible value of zpos property |
| 90 | * |
| 91 | * This function initializes generic mutable zpos property and enables support |
| 92 | * for it in drm core. Drivers can then attach this property to planes to enable |
| 93 | * support for configurable planes arrangement during blending operation. |
| 94 | * Once mutable zpos property has been enabled, the DRM core will automatically |
| 95 | * calculate drm_plane_state->normalized_zpos values. Usually min should be set |
| 96 | * to 0 and max to maximal number of planes for given crtc - 1. |
| 97 | * |
| 98 | * If zpos of some planes cannot be changed (like fixed background or |
| 99 | * cursor/topmost planes), driver should adjust min/max values and assign those |
| 100 | * planes immutable zpos property with lower or higher values (for more |
| 101 | * information, see drm_mode_create_zpos_immutable_property() function). In such |
| 102 | * case driver should also assign proper initial zpos values for all planes in |
| 103 | * its plane_reset() callback, so the planes will be always sorted properly. |
| 104 | * |
| 105 | * Returns: |
| 106 | * Zero on success, negative errno on failure. |
| 107 | */ |
| 108 | int drm_plane_create_zpos_property(struct drm_plane *plane, |
| 109 | unsigned int zpos, |
| 110 | unsigned int min, unsigned int max) |
| 111 | { |
| 112 | struct drm_property *prop; |
| 113 | |
| 114 | prop = drm_property_create_range(plane->dev, 0, "zpos", min, max); |
| 115 | if (!prop) |
| 116 | return -ENOMEM; |
| 117 | |
| 118 | drm_object_attach_property(&plane->base, prop, zpos); |
| 119 | |
| 120 | plane->zpos_property = prop; |
| 121 | |
| 122 | if (plane->state) { |
| 123 | plane->state->zpos = zpos; |
| 124 | plane->state->normalized_zpos = zpos; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | EXPORT_SYMBOL(drm_plane_create_zpos_property); |
| 130 | |
| 131 | /** |
| 132 | * drm_plane_create_zpos_immutable_property - create immuttable zpos property |
| 133 | * @plane: drm plane |
| 134 | * @zpos: value of zpos property |
| 135 | * |
| 136 | * This function initializes generic immutable zpos property and enables |
| 137 | * support for it in drm core. Using this property driver lets userspace |
| 138 | * to get the arrangement of the planes for blending operation and notifies |
| 139 | * it that the hardware (or driver) doesn't support changing of the planes' |
| 140 | * order. |
| 141 | * |
| 142 | * Returns: |
| 143 | * Zero on success, negative errno on failure. |
| 144 | */ |
| 145 | int drm_plane_create_zpos_immutable_property(struct drm_plane *plane, |
| 146 | unsigned int zpos) |
| 147 | { |
| 148 | struct drm_property *prop; |
| 149 | |
| 150 | prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE, |
| 151 | "zpos", zpos, zpos); |
| 152 | if (!prop) |
| 153 | return -ENOMEM; |
| 154 | |
| 155 | drm_object_attach_property(&plane->base, prop, zpos); |
| 156 | |
| 157 | plane->zpos_property = prop; |
| 158 | |
| 159 | if (plane->state) { |
| 160 | plane->state->zpos = zpos; |
| 161 | plane->state->normalized_zpos = zpos; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property); |
| 167 | |
| 168 | static int drm_atomic_state_zpos_cmp(const void *a, const void *b) |
| 169 | { |
| 170 | const struct drm_plane_state *sa = *(struct drm_plane_state **)a; |
| 171 | const struct drm_plane_state *sb = *(struct drm_plane_state **)b; |
| 172 | |
| 173 | if (sa->zpos != sb->zpos) |
| 174 | return sa->zpos - sb->zpos; |
| 175 | else |
| 176 | return sa->plane->base.id - sb->plane->base.id; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * drm_atomic_helper_crtc_normalize_zpos - calculate normalized zpos values |
| 181 | * @crtc: crtc with planes, which have to be considered for normalization |
| 182 | * @crtc_state: new atomic state to apply |
| 183 | * |
| 184 | * This function checks new states of all planes assigned to given crtc and |
| 185 | * calculates normalized zpos value for them. Planes are compared first by their |
| 186 | * zpos values, then by plane id (if zpos equals). Plane with lowest zpos value |
| 187 | * is at the bottom. The plane_state->normalized_zpos is then filled with unique |
| 188 | * values from 0 to number of active planes in crtc minus one. |
| 189 | * |
| 190 | * RETURNS |
| 191 | * Zero for success or -errno |
| 192 | */ |
| 193 | static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc, |
| 194 | struct drm_crtc_state *crtc_state) |
| 195 | { |
| 196 | struct drm_atomic_state *state = crtc_state->state; |
| 197 | struct drm_device *dev = crtc->dev; |
| 198 | int total_planes = dev->mode_config.num_total_plane; |
| 199 | struct drm_plane_state **states; |
| 200 | struct drm_plane *plane; |
| 201 | int i, n = 0; |
| 202 | int ret = 0; |
| 203 | |
| 204 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n", |
| 205 | crtc->base.id, crtc->name); |
| 206 | |
| 207 | states = kmalloc_array(total_planes, sizeof(*states), GFP_TEMPORARY); |
| 208 | if (!states) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | /* |
| 212 | * Normalization process might create new states for planes which |
| 213 | * normalized_zpos has to be recalculated. |
| 214 | */ |
| 215 | drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) { |
| 216 | struct drm_plane_state *plane_state = |
| 217 | drm_atomic_get_plane_state(state, plane); |
| 218 | if (IS_ERR(plane_state)) { |
| 219 | ret = PTR_ERR(plane_state); |
| 220 | goto done; |
| 221 | } |
| 222 | states[n++] = plane_state; |
| 223 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n", |
| 224 | plane->base.id, plane->name, |
| 225 | plane_state->zpos); |
| 226 | } |
| 227 | |
| 228 | sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL); |
| 229 | |
| 230 | for (i = 0; i < n; i++) { |
| 231 | plane = states[i]->plane; |
| 232 | |
| 233 | states[i]->normalized_zpos = i; |
| 234 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n", |
| 235 | plane->base.id, plane->name, i); |
| 236 | } |
| 237 | crtc_state->zpos_changed = true; |
| 238 | |
| 239 | done: |
| 240 | kfree(states); |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | /** |
Daniel Vetter | 52a9fcd | 2016-08-12 22:48:51 +0200 | [diff] [blame] | 245 | * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs |
Marek Szyprowski | 44d1240d | 2016-06-13 11:11:26 +0200 | [diff] [blame] | 246 | * @dev: DRM device |
| 247 | * @state: atomic state of DRM device |
| 248 | * |
| 249 | * This function calculates normalized zpos value for all modified planes in |
| 250 | * the provided atomic state of DRM device. For more information, see |
| 251 | * drm_atomic_helper_crtc_normalize_zpos() function. |
| 252 | * |
| 253 | * RETURNS |
| 254 | * Zero for success or -errno |
| 255 | */ |
Daniel Vetter | 52a9fcd | 2016-08-12 22:48:51 +0200 | [diff] [blame] | 256 | int drm_atomic_normalize_zpos(struct drm_device *dev, |
| 257 | struct drm_atomic_state *state) |
Marek Szyprowski | 44d1240d | 2016-06-13 11:11:26 +0200 | [diff] [blame] | 258 | { |
| 259 | struct drm_crtc *crtc; |
| 260 | struct drm_crtc_state *crtc_state; |
| 261 | struct drm_plane *plane; |
| 262 | struct drm_plane_state *plane_state; |
| 263 | int i, ret = 0; |
| 264 | |
| 265 | for_each_plane_in_state(state, plane, plane_state, i) { |
| 266 | crtc = plane_state->crtc; |
| 267 | if (!crtc) |
| 268 | continue; |
| 269 | if (plane->state->zpos != plane_state->zpos) { |
| 270 | crtc_state = |
| 271 | drm_atomic_get_existing_crtc_state(state, crtc); |
| 272 | crtc_state->zpos_changed = true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
| 277 | if (crtc_state->plane_mask != crtc->state->plane_mask || |
| 278 | crtc_state->zpos_changed) { |
| 279 | ret = drm_atomic_helper_crtc_normalize_zpos(crtc, |
| 280 | crtc_state); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | } |
| 284 | } |
| 285 | return 0; |
| 286 | } |
Daniel Vetter | 52a9fcd | 2016-08-12 22:48:51 +0200 | [diff] [blame] | 287 | EXPORT_SYMBOL(drm_atomic_normalize_zpos); |