drm_damage_helper.c 10.0 KB

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