drm_damage_helper.c 13 KB

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