/*1* Copyright (C) 2016 Samsung Electronics Co.Ltd2* Authors:3* Marek Szyprowski <[email protected]>4*5* DRM core plane blending related functions6*7* Permission to use, copy, modify, distribute, and sell this software and its8* documentation for any purpose is hereby granted without fee, provided that9* the above copyright notice appear in all copies and that both that copyright10* notice and this permission notice appear in supporting documentation, and11* that the name of the copyright holders not be used in advertising or12* publicity pertaining to distribution of the software without specific,13* written prior permission. The copyright holders make no representations14* about the suitability of this software for any purpose. It is provided "as15* is" without express or implied warranty.16*17* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,18* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO19* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR20* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,21* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER22* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE23* OF THIS SOFTWARE.24*/2526#include <linux/export.h>27#include <linux/slab.h>28#include <linux/sort.h>2930#include <drm/drm_atomic.h>31#include <drm/drm_blend.h>32#include <drm/drm_device.h>33#include <drm/drm_print.h>3435#include "drm_crtc_internal.h"3637/**38* DOC: overview39*40* The basic plane composition model supported by standard plane properties only41* has a source rectangle (in logical pixels within the &drm_framebuffer), with42* sub-pixel accuracy, which is scaled up to a pixel-aligned destination43* rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is44* defined by the horizontal and vertical visible pixels (stored in @hdisplay45* and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These46* two rectangles are both stored in the &drm_plane_state.47*48* For the atomic ioctl the following standard (atomic) properties on the plane object49* encode the basic plane composition model:50*51* SRC_X:52* X coordinate offset for the source rectangle within the53* &drm_framebuffer, in 16.16 fixed point. Must be positive.54* SRC_Y:55* Y coordinate offset for the source rectangle within the56* &drm_framebuffer, in 16.16 fixed point. Must be positive.57* SRC_W:58* Width for the source rectangle within the &drm_framebuffer, in 16.1659* fixed point. SRC_X plus SRC_W must be within the width of the source60* framebuffer. Must be positive.61* SRC_H:62* Height for the source rectangle within the &drm_framebuffer, in 16.1663* fixed point. SRC_Y plus SRC_H must be within the height of the source64* framebuffer. Must be positive.65* CRTC_X:66* X coordinate offset for the destination rectangle. Can be negative.67* CRTC_Y:68* Y coordinate offset for the destination rectangle. Can be negative.69* CRTC_W:70* Width for the destination rectangle. CRTC_X plus CRTC_W can extend past71* the currently visible horizontal area of the &drm_crtc.72* CRTC_H:73* Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past74* the currently visible vertical area of the &drm_crtc.75* FB_ID:76* Mode object ID of the &drm_framebuffer this plane should scan out.77*78* When a KMS client is performing front-buffer rendering, it should set79* FB_ID to the same front-buffer FB on each atomic commit. This implies80* to the driver that it needs to re-read the same FB again. Otherwise81* drivers which do not employ continuously repeated scanout cycles might82* not update the screen.83* CRTC_ID:84* Mode object ID of the &drm_crtc this plane should be connected to.85*86* Note that the source rectangle must fully lie within the bounds of the87* &drm_framebuffer. The destination rectangle can lie outside of the visible88* area of the current mode of the CRTC. It must be appropriately clipped by the89* driver, which can be done by calling drm_plane_helper_check_update(). Drivers90* are also allowed to round the subpixel sampling positions appropriately, but91* only to the next full pixel. No pixel outside of the source rectangle may92* ever be sampled, which is important when applying more sophisticated93* filtering than just a bilinear one when scaling. The filtering mode when94* scaling is unspecified.95*96* On top of this basic transformation additional properties can be exposed by97* the driver:98*99* alpha:100* Alpha is setup with drm_plane_create_alpha_property(). It controls the101* plane-wide opacity, from transparent (0) to opaque (0xffff). It can be102* combined with pixel alpha.103* The pixel values in the framebuffers are expected to not be104* pre-multiplied by the global alpha associated to the plane.105*106* rotation:107* Rotation is set up with drm_plane_create_rotation_property(). It adds a108* rotation and reflection step between the source and destination rectangles.109* Without this property the rectangle is only scaled, but not rotated or110* reflected.111*112* Possbile values:113*114* "rotate-<degrees>":115* Signals that a drm plane is rotated <degrees> degrees in counter116* clockwise direction.117*118* "reflect-<axis>":119* Signals that the contents of a drm plane is reflected along the120* <axis> axis, in the same way as mirroring.121*122* reflect-x::123*124* |o | | o|125* | | -> | |126* | v| |v |127*128* reflect-y::129*130* |o | | ^|131* | | -> | |132* | v| |o |133*134* zpos:135* Z position is set up with drm_plane_create_zpos_immutable_property() and136* drm_plane_create_zpos_property(). It controls the visibility of overlapping137* planes. Without this property the primary plane is always below the cursor138* plane, and ordering between all other planes is undefined. The positive139* Z axis points towards the user, i.e. planes with lower Z position values140* are underneath planes with higher Z position values. Two planes with the141* same Z position value have undefined ordering. Note that the Z position142* value can also be immutable, to inform userspace about the hard-coded143* stacking of planes, see drm_plane_create_zpos_immutable_property(). If144* any plane has a zpos property (either mutable or immutable), then all145* planes shall have a zpos property.146*147* pixel blend mode:148* Pixel blend mode is set up with drm_plane_create_blend_mode_property().149* It adds a blend mode for alpha blending equation selection, describing150* how the pixels from the current plane are composited with the151* background.152*153* Three alpha blending equations are defined:154*155* "None":156* Blend formula that ignores the pixel alpha::157*158* out.rgb = plane_alpha * fg.rgb +159* (1 - plane_alpha) * bg.rgb160*161* "Pre-multiplied":162* Blend formula that assumes the pixel color values163* have been already pre-multiplied with the alpha164* channel values::165*166* out.rgb = plane_alpha * fg.rgb +167* (1 - (plane_alpha * fg.alpha)) * bg.rgb168*169* "Coverage":170* Blend formula that assumes the pixel color values have not171* been pre-multiplied and will do so when blending them to the172* background color values::173*174* out.rgb = plane_alpha * fg.alpha * fg.rgb +175* (1 - (plane_alpha * fg.alpha)) * bg.rgb176*177* Using the following symbols:178*179* "fg.rgb":180* Each of the RGB component values from the plane's pixel181* "fg.alpha":182* Alpha component value from the plane's pixel. If the plane's183* pixel format has no alpha component, then this is assumed to be184* 1.0. In these cases, this property has no effect, as all three185* equations become equivalent.186* "bg.rgb":187* Each of the RGB component values from the background188* "plane_alpha":189* Plane alpha value set by the plane "alpha" property. If the190* plane does not expose the "alpha" property, then this is191* assumed to be 1.0192*193* Note that all the property extensions described here apply either to the194* plane or the CRTC (e.g. for the background color, which currently is not195* exposed and assumed to be black).196*197* SCALING_FILTER:198* Indicates scaling filter to be used for plane scaler199*200* The value of this property can be one of the following:201*202* Default:203* Driver's default scaling filter204* Nearest Neighbor:205* Nearest Neighbor scaling filter206*207* Drivers can set up this property for a plane by calling208* drm_plane_create_scaling_filter_property209*/210211/**212* drm_plane_create_alpha_property - create a new alpha property213* @plane: drm plane214*215* This function creates a generic, mutable, alpha property and enables support216* for it in the DRM core. It is attached to @plane.217*218* The alpha property will be allowed to be within the bounds of 0219* (transparent) to 0xffff (opaque).220*221* Returns:222* 0 on success, negative error code on failure.223*/224int drm_plane_create_alpha_property(struct drm_plane *plane)225{226struct drm_property *prop;227228prop = drm_property_create_range(plane->dev, 0, "alpha",2290, DRM_BLEND_ALPHA_OPAQUE);230if (!prop)231return -ENOMEM;232233drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);234plane->alpha_property = prop;235236if (plane->state)237plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;238239return 0;240}241EXPORT_SYMBOL(drm_plane_create_alpha_property);242243/**244* drm_plane_create_rotation_property - create a new rotation property245* @plane: drm plane246* @rotation: initial value of the rotation property247* @supported_rotations: bitmask of supported rotations and reflections248*249* This creates a new property with the selected support for transformations.250*251* Since a rotation by 180° degress is the same as reflecting both along the x252* and the y axis the rotation property is somewhat redundant. Drivers can use253* drm_rotation_simplify() to normalize values of this property.254*255* The property exposed to userspace is a bitmask property (see256* drm_property_create_bitmask()) called "rotation" and has the following257* bitmask enumaration values:258*259* DRM_MODE_ROTATE_0:260* "rotate-0"261* DRM_MODE_ROTATE_90:262* "rotate-90"263* DRM_MODE_ROTATE_180:264* "rotate-180"265* DRM_MODE_ROTATE_270:266* "rotate-270"267* DRM_MODE_REFLECT_X:268* "reflect-x"269* DRM_MODE_REFLECT_Y:270* "reflect-y"271*272* Rotation is the specified amount in degrees in counter clockwise direction,273* the X and Y axis are within the source rectangle, i.e. the X/Y axis before274* rotation. After reflection, the rotation is applied to the image sampled from275* the source rectangle, before scaling it to fit the destination rectangle.276*/277int drm_plane_create_rotation_property(struct drm_plane *plane,278unsigned int rotation,279unsigned int supported_rotations)280{281static const struct drm_prop_enum_list props[] = {282{ __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },283{ __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },284{ __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },285{ __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },286{ __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },287{ __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },288};289struct drm_property *prop;290291WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);292WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));293WARN_ON(rotation & ~supported_rotations);294295prop = drm_property_create_bitmask(plane->dev, 0, "rotation",296props, ARRAY_SIZE(props),297supported_rotations);298if (!prop)299return -ENOMEM;300301drm_object_attach_property(&plane->base, prop, rotation);302303if (plane->state)304plane->state->rotation = rotation;305306plane->rotation_property = prop;307308return 0;309}310EXPORT_SYMBOL(drm_plane_create_rotation_property);311312/**313* drm_rotation_simplify() - Try to simplify the rotation314* @rotation: Rotation to be simplified315* @supported_rotations: Supported rotations316*317* Attempt to simplify the rotation to a form that is supported.318* Eg. if the hardware supports everything except DRM_MODE_REFLECT_X319* one could call this function like this:320*321* drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |322* DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |323* DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);324*325* to eliminate the DRM_MODE_REFLECT_X flag. Depending on what kind of326* transforms the hardware supports, this function may not327* be able to produce a supported transform, so the caller should328* check the result afterwards.329*/330unsigned int drm_rotation_simplify(unsigned int rotation,331unsigned int supported_rotations)332{333if (rotation & ~supported_rotations) {334rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;335rotation = (rotation & DRM_MODE_REFLECT_MASK) |336BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)337% 4);338}339340return rotation;341}342EXPORT_SYMBOL(drm_rotation_simplify);343344/**345* drm_plane_create_zpos_property - create mutable zpos property346* @plane: drm plane347* @zpos: initial value of zpos property348* @min: minimal possible value of zpos property349* @max: maximal possible value of zpos property350*351* This function initializes generic mutable zpos property and enables support352* for it in drm core. Drivers can then attach this property to planes to enable353* support for configurable planes arrangement during blending operation.354* Drivers that attach a mutable zpos property to any plane should call the355* drm_atomic_normalize_zpos() helper during their implementation of356* &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos357* values and store them in &drm_plane_state.normalized_zpos. Usually min358* should be set to 0 and max to maximal number of planes for given crtc - 1.359*360* If zpos of some planes cannot be changed (like fixed background or361* cursor/topmost planes), drivers shall adjust the min/max values and assign362* those planes immutable zpos properties with lower or higher values (for more363* information, see drm_plane_create_zpos_immutable_property() function). In such364* case drivers shall also assign proper initial zpos values for all planes in365* its plane_reset() callback, so the planes will be always sorted properly.366*367* See also drm_atomic_normalize_zpos().368*369* The property exposed to userspace is called "zpos".370*371* Returns:372* Zero on success, negative errno on failure.373*/374int drm_plane_create_zpos_property(struct drm_plane *plane,375unsigned int zpos,376unsigned int min, unsigned int max)377{378struct drm_property *prop;379380prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);381if (!prop)382return -ENOMEM;383384drm_object_attach_property(&plane->base, prop, zpos);385386plane->zpos_property = prop;387388if (plane->state) {389plane->state->zpos = zpos;390plane->state->normalized_zpos = zpos;391}392393return 0;394}395EXPORT_SYMBOL(drm_plane_create_zpos_property);396397/**398* drm_plane_create_zpos_immutable_property - create immuttable zpos property399* @plane: drm plane400* @zpos: value of zpos property401*402* This function initializes generic immutable zpos property and enables403* support for it in drm core. Using this property driver lets userspace404* to get the arrangement of the planes for blending operation and notifies405* it that the hardware (or driver) doesn't support changing of the planes'406* order. For mutable zpos see drm_plane_create_zpos_property().407*408* The property exposed to userspace is called "zpos".409*410* Returns:411* Zero on success, negative errno on failure.412*/413int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,414unsigned int zpos)415{416struct drm_property *prop;417418prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,419"zpos", zpos, zpos);420if (!prop)421return -ENOMEM;422423drm_object_attach_property(&plane->base, prop, zpos);424425plane->zpos_property = prop;426427if (plane->state) {428plane->state->zpos = zpos;429plane->state->normalized_zpos = zpos;430}431432return 0;433}434EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);435436static int drm_atomic_state_zpos_cmp(const void *a, const void *b)437{438const struct drm_plane_state *sa = *(struct drm_plane_state **)a;439const struct drm_plane_state *sb = *(struct drm_plane_state **)b;440441if (sa->zpos != sb->zpos)442return sa->zpos - sb->zpos;443else444return sa->plane->base.id - sb->plane->base.id;445}446447static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,448struct drm_crtc_state *crtc_state)449{450struct drm_atomic_state *state = crtc_state->state;451struct drm_device *dev = crtc->dev;452int total_planes = dev->mode_config.num_total_plane;453struct drm_plane_state **states;454struct drm_plane *plane;455int i, n = 0;456int ret = 0;457458drm_dbg_atomic(dev, "[CRTC:%d:%s] calculating normalized zpos values\n",459crtc->base.id, crtc->name);460461states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);462if (!states)463return -ENOMEM;464465/*466* Normalization process might create new states for planes which467* normalized_zpos has to be recalculated.468*/469drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {470struct drm_plane_state *plane_state =471drm_atomic_get_plane_state(state, plane);472if (IS_ERR(plane_state)) {473ret = PTR_ERR(plane_state);474goto done;475}476states[n++] = plane_state;477drm_dbg_atomic(dev, "[PLANE:%d:%s] processing zpos value %d\n",478plane->base.id, plane->name, plane_state->zpos);479}480481sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);482483for (i = 0; i < n; i++) {484plane = states[i]->plane;485486states[i]->normalized_zpos = i;487drm_dbg_atomic(dev, "[PLANE:%d:%s] normalized zpos value %d\n",488plane->base.id, plane->name, i);489}490crtc_state->zpos_changed = true;491492done:493kfree(states);494return ret;495}496497/**498* drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs499* @dev: DRM device500* @state: atomic state of DRM device501*502* This function calculates normalized zpos value for all modified planes in503* the provided atomic state of DRM device.504*505* For every CRTC this function checks new states of all planes assigned to506* it and calculates normalized zpos value for these planes. Planes are compared507* first by their zpos values, then by plane id (if zpos is equal). The plane508* with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos509* is then filled with unique values from 0 to number of active planes in crtc510* minus one.511*512* RETURNS513* Zero for success or -errno514*/515int drm_atomic_normalize_zpos(struct drm_device *dev,516struct drm_atomic_state *state)517{518struct drm_crtc *crtc;519struct drm_crtc_state *old_crtc_state, *new_crtc_state;520struct drm_plane *plane;521struct drm_plane_state *old_plane_state, *new_plane_state;522int i, ret = 0;523524for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {525crtc = new_plane_state->crtc;526if (!crtc)527continue;528if (old_plane_state->zpos != new_plane_state->zpos) {529new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);530new_crtc_state->zpos_changed = true;531}532}533534for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {535if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||536new_crtc_state->zpos_changed) {537ret = drm_atomic_helper_crtc_normalize_zpos(crtc,538new_crtc_state);539if (ret)540return ret;541}542}543return 0;544}545EXPORT_SYMBOL(drm_atomic_normalize_zpos);546547/**548* drm_plane_create_blend_mode_property - create a new blend mode property549* @plane: drm plane550* @supported_modes: bitmask of supported modes, must include551* BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is552* that alpha is premultiplied, and old userspace can break if553* the property defaults to anything else.554*555* This creates a new property describing the blend mode.556*557* The property exposed to userspace is an enumeration property (see558* drm_property_create_enum()) called "pixel blend mode" and has the559* following enumeration values:560*561* "None":562* Blend formula that ignores the pixel alpha.563*564* "Pre-multiplied":565* Blend formula that assumes the pixel color values have been already566* pre-multiplied with the alpha channel values.567*568* "Coverage":569* Blend formula that assumes the pixel color values have not been570* pre-multiplied and will do so when blending them to the background color571* values.572*573* RETURNS:574* Zero for success or -errno575*/576int drm_plane_create_blend_mode_property(struct drm_plane *plane,577unsigned int supported_modes)578{579struct drm_device *dev = plane->dev;580struct drm_property *prop;581static const struct drm_prop_enum_list props[] = {582{ DRM_MODE_BLEND_PIXEL_NONE, "None" },583{ DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },584{ DRM_MODE_BLEND_COVERAGE, "Coverage" },585};586unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |587BIT(DRM_MODE_BLEND_PREMULTI) |588BIT(DRM_MODE_BLEND_COVERAGE);589int i;590591if (WARN_ON((supported_modes & ~valid_mode_mask) ||592((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))593return -EINVAL;594595prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,596"pixel blend mode",597hweight32(supported_modes));598if (!prop)599return -ENOMEM;600601for (i = 0; i < ARRAY_SIZE(props); i++) {602int ret;603604if (!(BIT(props[i].type) & supported_modes))605continue;606607ret = drm_property_add_enum(prop, props[i].type,608props[i].name);609610if (ret) {611drm_property_destroy(dev, prop);612613return ret;614}615}616617drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);618plane->blend_mode_property = prop;619620return 0;621}622EXPORT_SYMBOL(drm_plane_create_blend_mode_property);623624625