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