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  * Rob Clark <robdclark@gmail.com>
30  *
31  **************************************************************************/
32 
33 #include <drm/drm_atomic.h>
34 #include <drm/drm_damage_helper.h>
35 #include <drm/drm_device.h>
36 
37 /**
38  * DOC: overview
39  *
40  * FB_DAMAGE_CLIPS is an optional plane property which provides a means to
41  * specify a list of damage rectangles on a plane in framebuffer coordinates of
42  * the framebuffer attached to the plane. In current context damage is the area
43  * of plane framebuffer that has changed since last plane update (also called
44  * page-flip), irrespective of whether currently attached framebuffer is same as
45  * framebuffer attached during last plane update or not.
46  *
47  * FB_DAMAGE_CLIPS is a hint to kernel which could be helpful for some drivers
48  * to optimize internally especially for virtual devices where each framebuffer
49  * change needs to be transmitted over network, usb, etc.
50  *
51  * Since FB_DAMAGE_CLIPS is a hint so it is an optional property. User-space can
52  * ignore damage clips property and in that case driver will do a full plane
53  * update. In case damage clips are provided then it is guaranteed that the area
54  * inside damage clips will be updated to plane. For efficiency driver can do
55  * full update or can update more than specified in damage clips. Since driver
56  * is free to read more, user-space must always render the entire visible
57  * framebuffer. Otherwise there can be corruptions. Also, if a user-space
58  * provides damage clips which doesn't encompass the actual damage to
59  * framebuffer (since last plane update) can result in incorrect rendering.
60  *
61  * FB_DAMAGE_CLIPS is a blob property with the layout of blob data is simply an
62  * array of &drm_mode_rect. Unlike plane &drm_plane_state.src coordinates,
63  * damage clips are not in 16.16 fixed point. Similar to plane src in
64  * framebuffer, damage clips cannot be negative. In damage clip, x1/y1 are
65  * inclusive and x2/y2 are exclusive. While kernel does not error for overlapped
66  * damage clips, it is strongly discouraged.
67  *
68  * Drivers that are interested in damage interface for plane should enable
69  * FB_DAMAGE_CLIPS property by calling drm_plane_enable_fb_damage_clips().
70  * Drivers implementing damage can use drm_atomic_helper_damage_iter_init() and
71  * drm_atomic_helper_damage_iter_next() helper iterator function to get damage
72  * rectangles clipped to &drm_plane_state.src.
73  */
74 
75 static void convert_clip_rect_to_rect(const struct drm_clip_rect *src,
76 				      struct drm_mode_rect *dest,
77 				      uint32_t num_clips, uint32_t src_inc)
78 {
79 	while (num_clips > 0) {
80 		dest->x1 = src->x1;
81 		dest->y1 = src->y1;
82 		dest->x2 = src->x2;
83 		dest->y2 = src->y2;
84 		src += src_inc;
85 		dest++;
86 		num_clips--;
87 	}
88 }
89 
90 /**
91  * drm_plane_enable_fb_damage_clips - Enables plane fb damage clips property.
92  * @plane: Plane on which to enable damage clips property.
93  *
94  * This function lets driver to enable the damage clips property on a plane.
95  */
96 void drm_plane_enable_fb_damage_clips(struct drm_plane *plane)
97 {
98 	struct drm_device *dev = plane->dev;
99 	struct drm_mode_config *config = &dev->mode_config;
100 
101 	drm_object_attach_property(&plane->base, config->prop_fb_damage_clips,
102 				   0);
103 }
104 EXPORT_SYMBOL(drm_plane_enable_fb_damage_clips);
105 
106 /**
107  * drm_atomic_helper_check_plane_damage - Verify plane damage on atomic_check.
108  * @state: The driver state object.
109  * @plane_state: Plane state for which to verify damage.
110  *
111  * This helper function makes sure that damage from plane state is discarded
112  * for full modeset. If there are more reasons a driver would want to do a full
113  * plane update rather than processing individual damage regions, then those
114  * cases should be taken care of here.
115  *
116  * Note that &drm_plane_state.fb_damage_clips == NULL in plane state means that
117  * full plane update should happen. It also ensure helper iterator will return
118  * &drm_plane_state.src as damage.
119  */
120 void drm_atomic_helper_check_plane_damage(struct drm_atomic_state *state,
121 					  struct drm_plane_state *plane_state)
122 {
123 	struct drm_crtc_state *crtc_state;
124 
125 	if (plane_state->crtc) {
126 		crtc_state = drm_atomic_get_new_crtc_state(state,
127 							   plane_state->crtc);
128 
129 		if (WARN_ON(!crtc_state))
130 			return;
131 
132 		if (drm_atomic_crtc_needs_modeset(crtc_state)) {
133 			drm_property_blob_put(plane_state->fb_damage_clips);
134 			plane_state->fb_damage_clips = NULL;
135 		}
136 	}
137 }
138 EXPORT_SYMBOL(drm_atomic_helper_check_plane_damage);
139 
140 /**
141  * drm_atomic_helper_dirtyfb - Helper for dirtyfb.
142  * @fb: DRM framebuffer.
143  * @file_priv: Drm file for the ioctl call.
144  * @flags: Dirty fb annotate flags.
145  * @color: Color for annotate fill.
146  * @clips: Dirty region.
147  * @num_clips: Count of clip in clips.
148  *
149  * A helper to implement &drm_framebuffer_funcs.dirty using damage interface
150  * during plane update. If num_clips is 0 then this helper will do a full plane
151  * update. This is the same behaviour expected by DIRTFB IOCTL.
152  *
153  * Note that this helper is blocking implementation. This is what current
154  * drivers and userspace expect in their DIRTYFB IOCTL implementation, as a way
155  * to rate-limit userspace and make sure its rendering doesn't get ahead of
156  * uploading new data too much.
157  *
158  * Return: Zero on success, negative errno on failure.
159  */
160 int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
161 			      struct drm_file *file_priv, unsigned int flags,
162 			      unsigned int color, struct drm_clip_rect *clips,
163 			      unsigned int num_clips)
164 {
165 	struct drm_modeset_acquire_ctx ctx;
166 	struct drm_property_blob *damage = NULL;
167 	struct drm_mode_rect *rects = NULL;
168 	struct drm_atomic_state *state;
169 	struct drm_plane *plane;
170 	int ret = 0;
171 
172 	/*
173 	 * When called from ioctl, we are interruptable, but not when called
174 	 * internally (ie. defio worker)
175 	 */
176 	drm_modeset_acquire_init(&ctx,
177 		file_priv ? DRM_MODESET_ACQUIRE_INTERRUPTIBLE : 0);
178 
179 	state = drm_atomic_state_alloc(fb->dev);
180 	if (!state) {
181 		ret = -ENOMEM;
182 		goto out_drop_locks;
183 	}
184 	state->acquire_ctx = &ctx;
185 
186 	if (clips) {
187 		uint32_t inc = 1;
188 
189 		if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
190 			inc = 2;
191 			num_clips /= 2;
192 		}
193 
194 		rects = kcalloc(num_clips, sizeof(*rects), GFP_KERNEL);
195 		if (!rects) {
196 			ret = -ENOMEM;
197 			goto out;
198 		}
199 
200 		convert_clip_rect_to_rect(clips, rects, num_clips, inc);
201 		damage = drm_property_create_blob(fb->dev,
202 						  num_clips * sizeof(*rects),
203 						  rects);
204 		if (IS_ERR(damage)) {
205 			ret = PTR_ERR(damage);
206 			damage = NULL;
207 			goto out;
208 		}
209 	}
210 
211 retry:
212 	drm_for_each_plane(plane, fb->dev) {
213 		struct drm_plane_state *plane_state;
214 
215 		if (plane->state->fb != fb)
216 			continue;
217 
218 		plane_state = drm_atomic_get_plane_state(state, plane);
219 		if (IS_ERR(plane_state)) {
220 			ret = PTR_ERR(plane_state);
221 			goto out;
222 		}
223 
224 		drm_property_replace_blob(&plane_state->fb_damage_clips,
225 					  damage);
226 	}
227 
228 	ret = drm_atomic_commit(state);
229 
230 out:
231 	if (ret == -EDEADLK) {
232 		drm_atomic_state_clear(state);
233 		ret = drm_modeset_backoff(&ctx);
234 		if (!ret)
235 			goto retry;
236 	}
237 
238 	drm_property_blob_put(damage);
239 	kfree(rects);
240 	drm_atomic_state_put(state);
241 
242 out_drop_locks:
243 	drm_modeset_drop_locks(&ctx);
244 	drm_modeset_acquire_fini(&ctx);
245 
246 	return ret;
247 
248 }
249 EXPORT_SYMBOL(drm_atomic_helper_dirtyfb);
250 
251 /**
252  * drm_atomic_helper_damage_iter_init - Initialize the damage iterator.
253  * @iter: The iterator to initialize.
254  * @old_state: Old plane state for validation.
255  * @state: Plane state from which to iterate the damage clips.
256  *
257  * Initialize an iterator, which clips plane damage
258  * &drm_plane_state.fb_damage_clips to plane &drm_plane_state.src. This iterator
259  * returns full plane src in case damage is not present because either
260  * user-space didn't sent or driver discarded it (it want to do full plane
261  * update). Currently this iterator returns full plane src in case plane src
262  * changed but that can be changed in future to return damage.
263  *
264  * For the case when plane is not visible or plane update should not happen the
265  * first call to iter_next will return false. Note that this helper use clipped
266  * &drm_plane_state.src, so driver calling this helper should have called
267  * drm_atomic_helper_check_plane_state() earlier.
268  */
269 void
270 drm_atomic_helper_damage_iter_init(struct drm_atomic_helper_damage_iter *iter,
271 				   const struct drm_plane_state *old_state,
272 				   const struct drm_plane_state *state)
273 {
274 	memset(iter, 0, sizeof(*iter));
275 
276 	if (!state || !state->crtc || !state->fb || !state->visible)
277 		return;
278 
279 	iter->clips = drm_helper_get_plane_damage_clips(state);
280 	iter->num_clips = drm_plane_get_damage_clips_count(state);
281 
282 	/* Round down for x1/y1 and round up for x2/y2 to catch all pixels */
283 	iter->plane_src.x1 = state->src.x1 >> 16;
284 	iter->plane_src.y1 = state->src.y1 >> 16;
285 	iter->plane_src.x2 = (state->src.x2 >> 16) + !!(state->src.x2 & 0xFFFF);
286 	iter->plane_src.y2 = (state->src.y2 >> 16) + !!(state->src.y2 & 0xFFFF);
287 
288 	if (!iter->clips || !drm_rect_equals(&state->src, &old_state->src)) {
289 		iter->clips = 0;
290 		iter->num_clips = 0;
291 		iter->full_update = true;
292 	}
293 }
294 EXPORT_SYMBOL(drm_atomic_helper_damage_iter_init);
295 
296 /**
297  * drm_atomic_helper_damage_iter_next - Advance the damage iterator.
298  * @iter: The iterator to advance.
299  * @rect: Return a rectangle in fb coordinate clipped to plane src.
300  *
301  * Since plane src is in 16.16 fixed point and damage clips are whole number,
302  * this iterator round off clips that intersect with plane src. Round down for
303  * x1/y1 and round up for x2/y2 for the intersected coordinate. Similar rounding
304  * off for full plane src, in case it's returned as damage. This iterator will
305  * skip damage clips outside of plane src.
306  *
307  * Return: True if the output is valid, false if reached the end.
308  *
309  * If the first call to iterator next returns false then it means no need to
310  * update the plane.
311  */
312 bool
313 drm_atomic_helper_damage_iter_next(struct drm_atomic_helper_damage_iter *iter,
314 				   struct drm_rect *rect)
315 {
316 	bool ret = false;
317 
318 	if (iter->full_update) {
319 		*rect = iter->plane_src;
320 		iter->full_update = false;
321 		return true;
322 	}
323 
324 	while (iter->curr_clip < iter->num_clips) {
325 		*rect = iter->clips[iter->curr_clip];
326 		iter->curr_clip++;
327 
328 		if (drm_rect_intersect(rect, &iter->plane_src)) {
329 			ret = true;
330 			break;
331 		}
332 	}
333 
334 	return ret;
335 }
336 EXPORT_SYMBOL(drm_atomic_helper_damage_iter_next);
337 
338 /**
339  * drm_atomic_helper_damage_merged - Merged plane damage
340  * @old_state: Old plane state for validation.
341  * @state: Plane state from which to iterate the damage clips.
342  * @rect: Returns the merged damage rectangle
343  *
344  * This function merges any valid plane damage clips into one rectangle and
345  * returns it in @rect.
346  *
347  * For details see: drm_atomic_helper_damage_iter_init() and
348  * drm_atomic_helper_damage_iter_next().
349  *
350  * Returns:
351  * True if there is valid plane damage otherwise false.
352  */
353 bool drm_atomic_helper_damage_merged(const struct drm_plane_state *old_state,
354 				     struct drm_plane_state *state,
355 				     struct drm_rect *rect)
356 {
357 	struct drm_atomic_helper_damage_iter iter;
358 	struct drm_rect clip;
359 	bool valid = false;
360 
361 	rect->x1 = INT_MAX;
362 	rect->y1 = INT_MAX;
363 	rect->x2 = 0;
364 	rect->y2 = 0;
365 
366 	drm_atomic_helper_damage_iter_init(&iter, old_state, state);
367 	drm_atomic_for_each_plane_damage(&iter, &clip) {
368 		rect->x1 = min(rect->x1, clip.x1);
369 		rect->y1 = min(rect->y1, clip.y1);
370 		rect->x2 = max(rect->x2, clip.x2);
371 		rect->y2 = max(rect->y2, clip.y2);
372 		valid = true;
373 	}
374 
375 	return valid;
376 }
377 EXPORT_SYMBOL(drm_atomic_helper_damage_merged);
378