paint_aggregator.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright (c) 2010 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "pdf/paint_aggregator.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/check.h"
  8. #include "ui/gfx/geometry/point.h"
  9. #include "ui/gfx/geometry/rect.h"
  10. #include "ui/gfx/geometry/vector2d.h"
  11. namespace chrome_pdf {
  12. namespace {
  13. bool IsNegative(int32_t num) {
  14. return num < 0;
  15. }
  16. } // namespace
  17. // ----------------------------------------------------------------------------
  18. // ALGORITHM NOTES
  19. //
  20. // We attempt to maintain a scroll rect in the presence of invalidations that
  21. // are contained within the scroll rect. If an invalidation crosses a scroll
  22. // rect, then we just treat the scroll rect as an invalidation rect.
  23. //
  24. // For invalidations performed prior to scrolling and contained within the
  25. // scroll rect, we offset the invalidation rects to account for the fact that
  26. // the consumer will perform scrolling before painting.
  27. //
  28. // We only support scrolling along one axis at a time. A diagonal scroll will
  29. // therefore be treated as an invalidation.
  30. // ----------------------------------------------------------------------------
  31. PaintAggregator::PaintUpdate::PaintUpdate() = default;
  32. PaintAggregator::PaintUpdate::PaintUpdate(const PaintUpdate& that) = default;
  33. PaintAggregator::PaintUpdate::~PaintUpdate() = default;
  34. PaintAggregator::InternalPaintUpdate::InternalPaintUpdate()
  35. : synthesized_scroll_damage_rect_(false) {}
  36. PaintAggregator::InternalPaintUpdate::~InternalPaintUpdate() = default;
  37. gfx::Rect PaintAggregator::InternalPaintUpdate::GetScrollDamage() const {
  38. // Should only be scrolling in one direction at a time.
  39. DCHECK(!(scroll_delta.x() && scroll_delta.y()));
  40. gfx::Rect damaged_rect;
  41. // Compute the region we will expose by scrolling, and paint that into a
  42. // shared memory section.
  43. if (scroll_delta.x()) {
  44. int32_t dx = scroll_delta.x();
  45. damaged_rect.set_y(scroll_rect.y());
  46. damaged_rect.set_height(scroll_rect.height());
  47. if (dx > 0) {
  48. damaged_rect.set_x(scroll_rect.x());
  49. damaged_rect.set_width(dx);
  50. } else {
  51. damaged_rect.set_x(scroll_rect.right() + dx);
  52. damaged_rect.set_width(-dx);
  53. }
  54. } else {
  55. int32_t dy = scroll_delta.y();
  56. damaged_rect.set_x(scroll_rect.x());
  57. damaged_rect.set_width(scroll_rect.width());
  58. if (dy > 0) {
  59. damaged_rect.set_y(scroll_rect.y());
  60. damaged_rect.set_height(dy);
  61. } else {
  62. damaged_rect.set_y(scroll_rect.bottom() + dy);
  63. damaged_rect.set_height(-dy);
  64. }
  65. }
  66. // In case the scroll offset exceeds the width/height of the scroll rect
  67. return gfx::IntersectRects(scroll_rect, damaged_rect);
  68. }
  69. PaintAggregator::PaintAggregator() = default;
  70. bool PaintAggregator::HasPendingUpdate() const {
  71. return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
  72. }
  73. void PaintAggregator::ClearPendingUpdate() {
  74. update_ = InternalPaintUpdate();
  75. }
  76. PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() {
  77. // Convert the internal paint update to the external one, which includes a
  78. // bit more precomputed info for the caller.
  79. PaintUpdate ret;
  80. ret.scroll_delta = update_.scroll_delta;
  81. ret.scroll_rect = update_.scroll_rect;
  82. ret.has_scroll = ret.scroll_delta.x() != 0 || ret.scroll_delta.y() != 0;
  83. // Include the scroll damage (if any) in the paint rects.
  84. // Code invalidates damaged rect here, it pick it up from the list of paint
  85. // rects in the next block.
  86. if (ret.has_scroll && !update_.synthesized_scroll_damage_rect_) {
  87. update_.synthesized_scroll_damage_rect_ = true;
  88. gfx::Rect scroll_damage = update_.GetScrollDamage();
  89. InvalidateRectInternal(scroll_damage, false);
  90. }
  91. ret.paint_rects.reserve(update_.paint_rects.size() + 1);
  92. ret.paint_rects.insert(ret.paint_rects.end(), update_.paint_rects.begin(),
  93. update_.paint_rects.end());
  94. return ret;
  95. }
  96. void PaintAggregator::SetIntermediateResults(
  97. const std::vector<PaintReadyRect>& ready,
  98. const std::vector<gfx::Rect>& pending) {
  99. update_.ready_rects.insert(update_.ready_rects.end(), ready.begin(),
  100. ready.end());
  101. update_.paint_rects = pending;
  102. }
  103. std::vector<PaintReadyRect> PaintAggregator::GetReadyRects() const {
  104. return update_.ready_rects;
  105. }
  106. void PaintAggregator::InvalidateRect(const gfx::Rect& rect) {
  107. InvalidateRectInternal(rect, true);
  108. }
  109. void PaintAggregator::ScrollRect(const gfx::Rect& clip_rect,
  110. const gfx::Vector2d& amount) {
  111. // We only support scrolling along one axis at a time.
  112. if (amount.x() != 0 && amount.y() != 0) {
  113. InvalidateRect(clip_rect);
  114. return;
  115. }
  116. // We can only scroll one rect at a time.
  117. if (!update_.scroll_rect.IsEmpty() && update_.scroll_rect != clip_rect) {
  118. InvalidateRect(clip_rect);
  119. return;
  120. }
  121. // Again, we only support scrolling along one axis at a time. Make sure this
  122. // update doesn't scroll on a different axis than any existing one.
  123. if ((amount.x() && update_.scroll_delta.y()) ||
  124. (amount.y() && update_.scroll_delta.x())) {
  125. InvalidateRect(clip_rect);
  126. return;
  127. }
  128. // If we scroll in a reverse direction to the direction we originally scrolled
  129. // and there were invalidations that happened in-between we may end up
  130. // incorrectly clipping the invalidated rects (see crbug.com/488390). This bug
  131. // doesn't exist in the original implementation
  132. // (ppapi/utility/graphics/paint_aggregator.cc) which uses a different method
  133. // of handling invalidations that occur after a scroll. The problem is that
  134. // when we scroll the invalidated region, we clip it to the scroll rect. This
  135. // can cause us to lose information about what the invalidated region was if
  136. // it gets scrolled back into view. We either need to not do this clipping or
  137. // disallow combining scrolls that occur in different directions with
  138. // invalidations that happen in-between. This code really needs some tests...
  139. if (!update_.paint_rects.empty()) {
  140. if (IsNegative(amount.x()) != IsNegative(update_.scroll_delta.x()) ||
  141. IsNegative(amount.y()) != IsNegative(update_.scroll_delta.y())) {
  142. InvalidateRect(clip_rect);
  143. return;
  144. }
  145. }
  146. // The scroll rect is new or isn't changing (though the scroll amount may
  147. // be changing).
  148. update_.scroll_rect = clip_rect;
  149. update_.scroll_delta += amount;
  150. // We might have just wiped out a pre-existing scroll.
  151. if (update_.scroll_delta == gfx::Vector2d()) {
  152. update_.scroll_rect = gfx::Rect();
  153. return;
  154. }
  155. // Adjust any paint rects that intersect the scroll. For the portion of the
  156. // paint that is inside the scroll area, move it by the scroll amount and
  157. // replace the existing paint with it. For the portion (if any) that is
  158. // outside the scroll, just invalidate it.
  159. std::vector<gfx::Rect> leftover_rects;
  160. for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
  161. if (!update_.scroll_rect.Intersects(update_.paint_rects[i]))
  162. continue;
  163. gfx::Rect intersection =
  164. gfx::IntersectRects(update_.paint_rects[i], update_.scroll_rect);
  165. gfx::Rect rect = update_.paint_rects[i];
  166. while (!rect.IsEmpty()) {
  167. gfx::Rect leftover = gfx::SubtractRects(rect, intersection);
  168. if (leftover.IsEmpty())
  169. break;
  170. // Don't want to call InvalidateRectInternal now since it'll modify
  171. // update_.paint_rects, so keep track of this and do it below.
  172. leftover_rects.push_back(leftover);
  173. rect.Subtract(leftover);
  174. }
  175. update_.paint_rects[i] = ScrollPaintRect(intersection, amount);
  176. // The rect may have been scrolled out of view.
  177. if (update_.paint_rects[i].IsEmpty()) {
  178. update_.paint_rects.erase(update_.paint_rects.begin() + i);
  179. i--;
  180. }
  181. }
  182. for (const auto& leftover_rect : leftover_rects)
  183. InvalidateRectInternal(leftover_rect, false);
  184. for (auto& update_rect : update_.ready_rects) {
  185. if (update_.scroll_rect.Contains(update_rect.rect()))
  186. update_rect.set_rect(ScrollPaintRect(update_rect.rect(), amount));
  187. }
  188. if (update_.synthesized_scroll_damage_rect_) {
  189. InvalidateRect(update_.GetScrollDamage());
  190. }
  191. }
  192. gfx::Rect PaintAggregator::ScrollPaintRect(const gfx::Rect& paint_rect,
  193. const gfx::Vector2d& amount) const {
  194. gfx::Rect result = paint_rect + amount;
  195. result.Intersect(update_.scroll_rect);
  196. return result;
  197. }
  198. void PaintAggregator::InvalidateScrollRect() {
  199. gfx::Rect scroll_rect = update_.scroll_rect;
  200. update_.scroll_rect = gfx::Rect();
  201. update_.scroll_delta = gfx::Vector2d();
  202. InvalidateRect(scroll_rect);
  203. }
  204. void PaintAggregator::InvalidateRectInternal(const gfx::Rect& rect_old,
  205. bool check_scroll) {
  206. gfx::Rect rect = rect_old;
  207. // Check if any rects that are ready to be painted overlap.
  208. for (size_t i = 0; i < update_.ready_rects.size(); ++i) {
  209. const gfx::Rect& existing_rect = update_.ready_rects[i].rect();
  210. if (rect.Intersects(existing_rect)) {
  211. // Re-invalidate in case the union intersects other paint rects.
  212. rect.Union(existing_rect);
  213. update_.ready_rects.erase(update_.ready_rects.begin() + i);
  214. break;
  215. }
  216. }
  217. bool add_paint = true;
  218. // Combine overlapping paints using smallest bounding box.
  219. for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
  220. const gfx::Rect& existing_rect = update_.paint_rects[i];
  221. if (existing_rect.Contains(rect)) // Optimize out redundancy.
  222. add_paint = false;
  223. if (rect.Intersects(existing_rect) || rect.SharesEdgeWith(existing_rect)) {
  224. // Re-invalidate in case the union intersects other paint rects.
  225. gfx::Rect combined_rect = gfx::UnionRects(rect, existing_rect);
  226. update_.paint_rects.erase(update_.paint_rects.begin() + i);
  227. InvalidateRectInternal(combined_rect, check_scroll);
  228. add_paint = false;
  229. }
  230. }
  231. if (add_paint) {
  232. // Add a non-overlapping paint.
  233. update_.paint_rects.push_back(rect);
  234. }
  235. // If the new paint overlaps with a scroll, then also invalidate the rect in
  236. // its new position.
  237. if (check_scroll && !update_.scroll_rect.IsEmpty() &&
  238. update_.scroll_rect.Intersects(rect)) {
  239. InvalidateRectInternal(ScrollPaintRect(rect, update_.scroll_delta), false);
  240. }
  241. }
  242. } // namespace chrome_pdf