Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm: Add a new plane property to send damage during plane update

FB_DAMAGE_CLIPS is an optional plane property to mark damaged regions
on the plane in framebuffer coordinates of the framebuffer attached to
the plane.

The layout of blob data is simply an array of "struct drm_mode_rect".
Unlike plane src coordinates, damage clips are not in 16.16 fixed point.
As plane src in framebuffer cannot be negative so are damage clips. In
damage clip, x1/y1 are inclusive and x2/y2 are exclusive.

This patch also exports the kernel internal drm_rect to userspace as
drm_mode_rect. This is because "struct drm_clip_rect" is not sufficient
to represent damage for current plane size.

Driver which are interested in enabling FB_DAMAGE_CLIPS property for a
plane should enable this property using drm_plane_enable_damage_clips.

v2:
- Input validation on damage clips against framebuffer size.
- Doc update, other minor changes.

Signed-off-by: Lukasz Spintzyk <lukasz.spintzyk@displaylink.com>
Signed-off-by: Deepak Rawat <drawat@vmware.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Reviewed-by: Thomas Hellstrom <thellstrom@vmware.com>
Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>

authored by

Lukasz Spintzyk and committed by
Thomas Hellstrom
d3b21767 818182dd

+244 -1
+12
Documentation/gpu/drm-kms.rst
··· 554 554 .. kernel-doc:: drivers/gpu/drm/drm_blend.c 555 555 :export: 556 556 557 + FB_DAMAGE_CLIPS 558 + ~~~~~~~~~~~~~~~ 559 + 560 + .. kernel-doc:: drivers/gpu/drm/drm_damage_helper.c 561 + :doc: overview 562 + 563 + .. kernel-doc:: drivers/gpu/drm/drm_damage_helper.c 564 + :export: 565 + 566 + .. kernel-doc:: include/drm/drm_damage_helper.h 567 + :internal: 568 + 557 569 Color Management Properties 558 570 --------------------------- 559 571
+1 -1
drivers/gpu/drm/Makefile
··· 37 37 drm_kms_helper_common.o drm_dp_dual_mode_helper.o \ 38 38 drm_simple_kms_helper.o drm_modeset_helper.o \ 39 39 drm_scdc_helper.o drm_gem_framebuffer_helper.o \ 40 - drm_atomic_state_helper.o 40 + drm_atomic_state_helper.o drm_damage_helper.o 41 41 42 42 drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o 43 43 drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
+22
drivers/gpu/drm/drm_atomic.c
··· 531 531 struct drm_crtc *crtc = new_plane_state->crtc; 532 532 const struct drm_framebuffer *fb = new_plane_state->fb; 533 533 unsigned int fb_width, fb_height; 534 + struct drm_mode_rect *clips; 535 + uint32_t num_clips; 534 536 int ret; 535 537 536 538 /* either *both* CRTC and FB must be set, or neither */ ··· 604 602 ((new_plane_state->src_y & 0xffff) * 15625) >> 10, 605 603 fb->width, fb->height); 606 604 return -ENOSPC; 605 + } 606 + 607 + clips = drm_plane_get_damage_clips(new_plane_state); 608 + num_clips = drm_plane_get_damage_clips_count(new_plane_state); 609 + 610 + /* Make sure damage clips are valid and inside the fb. */ 611 + while (num_clips > 0) { 612 + if (clips->x1 >= clips->x2 || 613 + clips->y1 >= clips->y2 || 614 + clips->x1 < 0 || 615 + clips->y1 < 0 || 616 + clips->x2 > fb_width || 617 + clips->y2 > fb_height) { 618 + DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid damage clip %d %d %d %d\n", 619 + plane->base.id, plane->name, clips->x1, 620 + clips->y1, clips->x2, clips->y2); 621 + return -EINVAL; 622 + } 623 + clips++; 624 + num_clips--; 607 625 } 608 626 609 627 if (plane_switching_crtc(old_plane_state, new_plane_state)) {
+13
drivers/gpu/drm/drm_atomic_uapi.c
··· 513 513 { 514 514 struct drm_device *dev = plane->dev; 515 515 struct drm_mode_config *config = &dev->mode_config; 516 + bool replaced = false; 517 + int ret; 516 518 517 519 if (property == config->prop_fb_id) { 518 520 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val); ··· 568 566 state->color_encoding = val; 569 567 } else if (property == plane->color_range_property) { 570 568 state->color_range = val; 569 + } else if (property == config->prop_fb_damage_clips) { 570 + ret = drm_atomic_replace_property_blob_from_id(dev, 571 + &state->fb_damage_clips, 572 + val, 573 + -1, 574 + sizeof(struct drm_rect), 575 + &replaced); 576 + return ret; 571 577 } else if (plane->funcs->atomic_set_property) { 572 578 return plane->funcs->atomic_set_property(plane, state, 573 579 property, val); ··· 631 621 *val = state->color_encoding; 632 622 } else if (property == plane->color_range_property) { 633 623 *val = state->color_range; 624 + } else if (property == config->prop_fb_damage_clips) { 625 + *val = (state->fb_damage_clips) ? 626 + state->fb_damage_clips->base.id : 0; 634 627 } else if (plane->funcs->atomic_get_property) { 635 628 return plane->funcs->atomic_get_property(plane, state, property, val); 636 629 } else {
+83
drivers/gpu/drm/drm_damage_helper.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 OR MIT 2 + /************************************************************************** 3 + * 4 + * Copyright (c) 2018 VMware, Inc., Palo Alto, CA., USA 5 + * All Rights Reserved. 6 + * 7 + * Permission is hereby granted, free of charge, to any person obtaining a 8 + * copy of this software and associated documentation files (the 9 + * "Software"), to deal in the Software without restriction, including 10 + * without limitation the rights to use, copy, modify, merge, publish, 11 + * distribute, sub license, and/or sell copies of the Software, and to 12 + * permit persons to whom the Software is furnished to do so, subject to 13 + * the following conditions: 14 + * 15 + * The above copyright notice and this permission notice (including the 16 + * next paragraph) shall be included in all copies or substantial portions 17 + * of the Software. 18 + * 19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 + * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 + * 27 + * Authors: 28 + * Deepak Rawat <drawat@vmware.com> 29 + * 30 + **************************************************************************/ 31 + 32 + #include <drm/drm_damage_helper.h> 33 + 34 + /** 35 + * DOC: overview 36 + * 37 + * FB_DAMAGE_CLIPS is an optional plane property which provides a means to 38 + * specify a list of damage rectangles on a plane in framebuffer coordinates of 39 + * the framebuffer attached to the plane. In current context damage is the area 40 + * of plane framebuffer that has changed since last plane update (also called 41 + * page-flip), irrespective of whether currently attached framebuffer is same as 42 + * framebuffer attached during last plane update or not. 43 + * 44 + * FB_DAMAGE_CLIPS is a hint to kernel which could be helpful for some drivers 45 + * to optimize internally especially for virtual devices where each framebuffer 46 + * change needs to be transmitted over network, usb, etc. 47 + * 48 + * Since FB_DAMAGE_CLIPS is a hint so it is an optional property. User-space can 49 + * ignore damage clips property and in that case driver will do a full plane 50 + * update. In case damage clips are provided then it is guaranteed that the area 51 + * inside damage clips will be updated to plane. For efficiency driver can do 52 + * full update or can update more than specified in damage clips. Since driver 53 + * is free to read more, user-space must always render the entire visible 54 + * framebuffer. Otherwise there can be corruptions. Also, if a user-space 55 + * provides damage clips which doesn't encompass the actual damage to 56 + * framebuffer (since last plane update) can result in incorrect rendering. 57 + * 58 + * FB_DAMAGE_CLIPS is a blob property with the layout of blob data is simply an 59 + * array of &drm_mode_rect. Unlike plane &drm_plane_state.src coordinates, 60 + * damage clips are not in 16.16 fixed point. Similar to plane src in 61 + * framebuffer, damage clips cannot be negative. In damage clip, x1/y1 are 62 + * inclusive and x2/y2 are exclusive. While kernel does not error for overlapped 63 + * damage clips, it is strongly discouraged. 64 + * 65 + * Drivers that are interested in damage interface for plane should enable 66 + * FB_DAMAGE_CLIPS property by calling drm_plane_enable_fb_damage_clips(). 67 + */ 68 + 69 + /** 70 + * drm_plane_enable_fb_damage_clips - Enables plane fb damage clips property. 71 + * @plane: Plane on which to enable damage clips property. 72 + * 73 + * This function lets driver to enable the damage clips property on a plane. 74 + */ 75 + void drm_plane_enable_fb_damage_clips(struct drm_plane *plane) 76 + { 77 + struct drm_device *dev = plane->dev; 78 + struct drm_mode_config *config = &dev->mode_config; 79 + 80 + drm_object_attach_property(&plane->base, config->prop_fb_damage_clips, 81 + 0); 82 + } 83 + EXPORT_SYMBOL(drm_plane_enable_fb_damage_clips);
+6
drivers/gpu/drm/drm_mode_config.c
··· 297 297 return -ENOMEM; 298 298 dev->mode_config.prop_crtc_id = prop; 299 299 300 + prop = drm_property_create(dev, DRM_MODE_PROP_BLOB, "FB_DAMAGE_CLIPS", 301 + 0); 302 + if (!prop) 303 + return -ENOMEM; 304 + dev->mode_config.prop_fb_damage_clips = prop; 305 + 300 306 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC, 301 307 "ACTIVE"); 302 308 if (!prop)
+39
include/drm/drm_damage_helper.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 + /************************************************************************** 3 + * 4 + * Copyright (c) 2018 VMware, Inc., Palo Alto, CA., USA 5 + * All Rights Reserved. 6 + * 7 + * Permission is hereby granted, free of charge, to any person obtaining a 8 + * copy of this software and associated documentation files (the 9 + * "Software"), to deal in the Software without restriction, including 10 + * without limitation the rights to use, copy, modify, merge, publish, 11 + * distribute, sub license, and/or sell copies of the Software, and to 12 + * permit persons to whom the Software is furnished to do so, subject to 13 + * the following conditions: 14 + * 15 + * The above copyright notice and this permission notice (including the 16 + * next paragraph) shall be included in all copies or substantial portions 17 + * of the Software. 18 + * 19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 + * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 + * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 + * 27 + * Authors: 28 + * Deepak Rawat <drawat@vmware.com> 29 + * 30 + **************************************************************************/ 31 + 32 + #ifndef DRM_DAMAGE_HELPER_H_ 33 + #define DRM_DAMAGE_HELPER_H_ 34 + 35 + #include <drm/drm_atomic_helper.h> 36 + 37 + void drm_plane_enable_fb_damage_clips(struct drm_plane *plane); 38 + 39 + #endif
+9
include/drm/drm_mode_config.h
··· 634 634 */ 635 635 struct drm_property *prop_crtc_id; 636 636 /** 637 + * @prop_fb_damage_clips: Optional plane property to mark damaged 638 + * regions on the plane in framebuffer coordinates of the framebuffer 639 + * attached to the plane. 640 + * 641 + * The layout of blob data is simply an array of &drm_mode_rect. Unlike 642 + * plane src coordinates, damage clips are not in 16.16 fixed point. 643 + */ 644 + struct drm_property *prop_fb_damage_clips; 645 + /** 637 646 * @prop_active: Default atomic CRTC property to control the active 638 647 * state, which is the simplified implementation for DPMS in atomic 639 648 * drivers.
+40
include/drm/drm_plane.h
··· 173 173 */ 174 174 enum drm_color_range color_range; 175 175 176 + /** 177 + * @fb_damage_clips: 178 + * 179 + * Blob representing damage (area in plane framebuffer that changed 180 + * since last plane update) as an array of &drm_mode_rect in framebuffer 181 + * coodinates of the attached framebuffer. Note that unlike plane src, 182 + * damage clips are not in 16.16 fixed point. 183 + */ 184 + struct drm_property_blob *fb_damage_clips; 185 + 176 186 /** @src: clipped source coordinates of the plane (in 16.16) */ 177 187 /** @dst: clipped destination coordinates of the plane */ 178 188 struct drm_rect src, dst; ··· 810 800 811 801 bool drm_any_plane_has_format(struct drm_device *dev, 812 802 u32 format, u64 modifier); 803 + /** 804 + * drm_plane_get_damage_clips_count - Returns damage clips count. 805 + * @state: Plane state. 806 + * 807 + * Simple helper to get the number of &drm_mode_rect clips set by user-space 808 + * during plane update. 809 + * 810 + * Return: Number of clips in plane fb_damage_clips blob property. 811 + */ 812 + static inline unsigned int 813 + drm_plane_get_damage_clips_count(const struct drm_plane_state *state) 814 + { 815 + return (state && state->fb_damage_clips) ? 816 + state->fb_damage_clips->length/sizeof(struct drm_mode_rect) : 0; 817 + } 818 + 819 + /** 820 + * drm_plane_get_damage_clips - Returns damage clips. 821 + * @state: Plane state. 822 + * 823 + * Note that this function returns uapi type &drm_mode_rect. 824 + * 825 + * Return: Damage clips in plane fb_damage_clips blob property. 826 + */ 827 + static inline struct drm_mode_rect * 828 + drm_plane_get_damage_clips(const struct drm_plane_state *state) 829 + { 830 + return (struct drm_mode_rect *)((state && state->fb_damage_clips) ? 831 + state->fb_damage_clips->data : NULL); 832 + } 813 833 814 834 #endif
+19
include/uapi/drm/drm_mode.h
··· 888 888 __u32 lessee_id; 889 889 }; 890 890 891 + /** 892 + * struct drm_mode_rect - Two dimensional rectangle. 893 + * @x1: Horizontal starting coordinate (inclusive). 894 + * @y1: Vertical starting coordinate (inclusive). 895 + * @x2: Horizontal ending coordinate (exclusive). 896 + * @y2: Vertical ending coordinate (exclusive). 897 + * 898 + * With drm subsystem using struct drm_rect to manage rectangular area this 899 + * export it to user-space. 900 + * 901 + * Currently used by drm_mode_atomic blob property FB_DAMAGE_CLIPS. 902 + */ 903 + struct drm_mode_rect { 904 + __s32 x1; 905 + __s32 y1; 906 + __s32 x2; 907 + __s32 y2; 908 + }; 909 + 891 910 #if defined(__cplusplus) 892 911 } 893 912 #endif