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