1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright (c) 2018 VMware, Inc., Palo Alto, CA., USA
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
28 * Deepak Rawat <drawat@vmware.com>
29 * Rob Clark <robdclark@gmail.com>
31 **************************************************************************/
33 #include <drm/drm_atomic.h>
34 #include <drm/drm_damage_helper.h>
35 #include <drm/drm_device.h>
36 #include <drm/drm_framebuffer.h>
38 static void convert_clip_rect_to_rect(const struct drm_clip_rect *src,
39 struct drm_mode_rect *dest,
40 uint32_t num_clips, uint32_t src_inc)
42 while (num_clips > 0) {
54 * drm_atomic_helper_check_plane_damage - Verify plane damage on atomic_check.
55 * @state: The driver state object.
56 * @plane_state: Plane state for which to verify damage.
58 * This helper function makes sure that damage from plane state is discarded
59 * for full modeset. If there are more reasons a driver would want to do a full
60 * plane update rather than processing individual damage regions, then those
61 * cases should be taken care of here.
63 * Note that &drm_plane_state.fb_damage_clips == NULL in plane state means that
64 * full plane update should happen. It also ensure helper iterator will return
65 * &drm_plane_state.src as damage.
67 void drm_atomic_helper_check_plane_damage(struct drm_atomic_state *state,
68 struct drm_plane_state *plane_state)
70 struct drm_crtc_state *crtc_state;
72 if (plane_state->crtc) {
73 crtc_state = drm_atomic_get_new_crtc_state(state,
76 if (WARN_ON(!crtc_state))
79 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
80 drm_property_blob_put(plane_state->fb_damage_clips);
81 plane_state->fb_damage_clips = NULL;
85 EXPORT_SYMBOL(drm_atomic_helper_check_plane_damage);
88 * drm_atomic_helper_dirtyfb - Helper for dirtyfb.
89 * @fb: DRM framebuffer.
90 * @file_priv: Drm file for the ioctl call.
91 * @flags: Dirty fb annotate flags.
92 * @color: Color for annotate fill.
93 * @clips: Dirty region.
94 * @num_clips: Count of clip in clips.
96 * A helper to implement &drm_framebuffer_funcs.dirty using damage interface
97 * during plane update. If num_clips is 0 then this helper will do a full plane
98 * update. This is the same behaviour expected by DIRTFB IOCTL.
100 * Note that this helper is blocking implementation. This is what current
101 * drivers and userspace expect in their DIRTYFB IOCTL implementation, as a way
102 * to rate-limit userspace and make sure its rendering doesn't get ahead of
103 * uploading new data too much.
105 * Return: Zero on success, negative errno on failure.
107 int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
108 struct drm_file *file_priv, unsigned int flags,
109 unsigned int color, struct drm_clip_rect *clips,
110 unsigned int num_clips)
112 struct drm_modeset_acquire_ctx ctx;
113 struct drm_property_blob *damage = NULL;
114 struct drm_mode_rect *rects = NULL;
115 struct drm_atomic_state *state;
116 struct drm_plane *plane;
120 * When called from ioctl, we are interruptible, but not when called
121 * internally (ie. defio worker)
123 drm_modeset_acquire_init(&ctx,
124 file_priv ? DRM_MODESET_ACQUIRE_INTERRUPTIBLE : 0);
126 state = drm_atomic_state_alloc(fb->dev);
131 state->acquire_ctx = &ctx;
136 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
141 rects = kcalloc(num_clips, sizeof(*rects), GFP_KERNEL);
147 convert_clip_rect_to_rect(clips, rects, num_clips, inc);
148 damage = drm_property_create_blob(fb->dev,
149 num_clips * sizeof(*rects),
151 if (IS_ERR(damage)) {
152 ret = PTR_ERR(damage);
159 drm_for_each_plane(plane, fb->dev) {
160 struct drm_plane_state *plane_state;
162 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
166 if (plane->state->fb != fb) {
167 drm_modeset_unlock(&plane->mutex);
171 plane_state = drm_atomic_get_plane_state(state, plane);
172 if (IS_ERR(plane_state)) {
173 ret = PTR_ERR(plane_state);
177 drm_property_replace_blob(&plane_state->fb_damage_clips,
181 ret = drm_atomic_commit(state);
184 if (ret == -EDEADLK) {
185 drm_atomic_state_clear(state);
186 ret = drm_modeset_backoff(&ctx);
191 drm_property_blob_put(damage);
193 drm_atomic_state_put(state);
196 drm_modeset_drop_locks(&ctx);
197 drm_modeset_acquire_fini(&ctx);
202 EXPORT_SYMBOL(drm_atomic_helper_dirtyfb);
205 * drm_atomic_helper_damage_iter_init - Initialize the damage iterator.
206 * @iter: The iterator to initialize.
207 * @old_state: Old plane state for validation.
208 * @state: Plane state from which to iterate the damage clips.
210 * Initialize an iterator, which clips plane damage
211 * &drm_plane_state.fb_damage_clips to plane &drm_plane_state.src. This iterator
212 * returns full plane src in case damage is not present because either
213 * user-space didn't sent or driver discarded it (it want to do full plane
214 * update). Currently this iterator returns full plane src in case plane src
215 * changed but that can be changed in future to return damage.
217 * For the case when plane is not visible or plane update should not happen the
218 * first call to iter_next will return false. Note that this helper use clipped
219 * &drm_plane_state.src, so driver calling this helper should have called
220 * drm_atomic_helper_check_plane_state() earlier.
223 drm_atomic_helper_damage_iter_init(struct drm_atomic_helper_damage_iter *iter,
224 const struct drm_plane_state *old_state,
225 const struct drm_plane_state *state)
228 memset(iter, 0, sizeof(*iter));
230 if (!state || !state->crtc || !state->fb || !state->visible)
233 iter->clips = (struct drm_rect *)drm_plane_get_damage_clips(state);
234 iter->num_clips = drm_plane_get_damage_clips_count(state);
236 /* Round down for x1/y1 and round up for x2/y2 to catch all pixels */
237 src = drm_plane_state_src(state);
239 iter->plane_src.x1 = src.x1 >> 16;
240 iter->plane_src.y1 = src.y1 >> 16;
241 iter->plane_src.x2 = (src.x2 >> 16) + !!(src.x2 & 0xFFFF);
242 iter->plane_src.y2 = (src.y2 >> 16) + !!(src.y2 & 0xFFFF);
244 if (!iter->clips || !drm_rect_equals(&state->src, &old_state->src)) {
247 iter->full_update = true;
250 EXPORT_SYMBOL(drm_atomic_helper_damage_iter_init);
253 * drm_atomic_helper_damage_iter_next - Advance the damage iterator.
254 * @iter: The iterator to advance.
255 * @rect: Return a rectangle in fb coordinate clipped to plane src.
257 * Since plane src is in 16.16 fixed point and damage clips are whole number,
258 * this iterator round off clips that intersect with plane src. Round down for
259 * x1/y1 and round up for x2/y2 for the intersected coordinate. Similar rounding
260 * off for full plane src, in case it's returned as damage. This iterator will
261 * skip damage clips outside of plane src.
263 * Return: True if the output is valid, false if reached the end.
265 * If the first call to iterator next returns false then it means no need to
269 drm_atomic_helper_damage_iter_next(struct drm_atomic_helper_damage_iter *iter,
270 struct drm_rect *rect)
274 if (iter->full_update) {
275 *rect = iter->plane_src;
276 iter->full_update = false;
280 while (iter->curr_clip < iter->num_clips) {
281 *rect = iter->clips[iter->curr_clip];
284 if (drm_rect_intersect(rect, &iter->plane_src)) {
292 EXPORT_SYMBOL(drm_atomic_helper_damage_iter_next);
295 * drm_atomic_helper_damage_merged - Merged plane damage
296 * @old_state: Old plane state for validation.
297 * @state: Plane state from which to iterate the damage clips.
298 * @rect: Returns the merged damage rectangle
300 * This function merges any valid plane damage clips into one rectangle and
301 * returns it in @rect.
303 * For details see: drm_atomic_helper_damage_iter_init() and
304 * drm_atomic_helper_damage_iter_next().
307 * True if there is valid plane damage otherwise false.
309 bool drm_atomic_helper_damage_merged(const struct drm_plane_state *old_state,
310 struct drm_plane_state *state,
311 struct drm_rect *rect)
313 struct drm_atomic_helper_damage_iter iter;
314 struct drm_rect clip;
322 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
323 drm_atomic_for_each_plane_damage(&iter, &clip) {
324 rect->x1 = min(rect->x1, clip.x1);
325 rect->y1 = min(rect->y1, clip.y1);
326 rect->x2 = max(rect->x2, clip.x2);
327 rect->y2 = max(rect->y2, clip.y2);
333 EXPORT_SYMBOL(drm_atomic_helper_damage_merged);