1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "pdf/paint_aggregator.h"
10 #include "base/check.h"
11 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/vector2d.h"
15 namespace chrome_pdf {
19 bool IsNegative(int32_t num) {
25 // ----------------------------------------------------------------------------
28 // We attempt to maintain a scroll rect in the presence of invalidations that
29 // are contained within the scroll rect. If an invalidation crosses a scroll
30 // rect, then we just treat the scroll rect as an invalidation rect.
32 // For invalidations performed prior to scrolling and contained within the
33 // scroll rect, we offset the invalidation rects to account for the fact that
34 // the consumer will perform scrolling before painting.
36 // We only support scrolling along one axis at a time. A diagonal scroll will
37 // therefore be treated as an invalidation.
38 // ----------------------------------------------------------------------------
40 PaintAggregator::PaintUpdate::PaintUpdate() = default;
42 PaintAggregator::PaintUpdate::PaintUpdate(const PaintUpdate& that) = default;
44 PaintAggregator::PaintUpdate::~PaintUpdate() = default;
46 PaintAggregator::InternalPaintUpdate::InternalPaintUpdate()
47 : synthesized_scroll_damage_rect_(false) {}
49 PaintAggregator::InternalPaintUpdate::~InternalPaintUpdate() = default;
51 gfx::Rect PaintAggregator::InternalPaintUpdate::GetScrollDamage() const {
52 // Should only be scrolling in one direction at a time.
53 DCHECK(!(scroll_delta.x() && scroll_delta.y()));
55 gfx::Rect damaged_rect;
57 // Compute the region we will expose by scrolling, and paint that into a
58 // shared memory section.
59 if (scroll_delta.x()) {
60 int32_t dx = scroll_delta.x();
61 damaged_rect.set_y(scroll_rect.y());
62 damaged_rect.set_height(scroll_rect.height());
64 damaged_rect.set_x(scroll_rect.x());
65 damaged_rect.set_width(dx);
67 damaged_rect.set_x(scroll_rect.right() + dx);
68 damaged_rect.set_width(-dx);
71 int32_t dy = scroll_delta.y();
72 damaged_rect.set_x(scroll_rect.x());
73 damaged_rect.set_width(scroll_rect.width());
75 damaged_rect.set_y(scroll_rect.y());
76 damaged_rect.set_height(dy);
78 damaged_rect.set_y(scroll_rect.bottom() + dy);
79 damaged_rect.set_height(-dy);
83 // In case the scroll offset exceeds the width/height of the scroll rect
84 return gfx::IntersectRects(scroll_rect, damaged_rect);
87 PaintAggregator::PaintAggregator() = default;
89 bool PaintAggregator::HasPendingUpdate() const {
90 return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
93 void PaintAggregator::ClearPendingUpdate() {
94 update_ = InternalPaintUpdate();
97 PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() {
98 // Convert the internal paint update to the external one, which includes a
99 // bit more precomputed info for the caller.
101 ret.scroll_delta = update_.scroll_delta;
102 ret.scroll_rect = update_.scroll_rect;
103 ret.has_scroll = ret.scroll_delta.x() != 0 || ret.scroll_delta.y() != 0;
105 // Include the scroll damage (if any) in the paint rects.
106 // Code invalidates damaged rect here, it pick it up from the list of paint
107 // rects in the next block.
108 if (ret.has_scroll && !update_.synthesized_scroll_damage_rect_) {
109 update_.synthesized_scroll_damage_rect_ = true;
110 gfx::Rect scroll_damage = update_.GetScrollDamage();
111 InvalidateRectInternal(scroll_damage, false);
114 ret.paint_rects.reserve(update_.paint_rects.size() + 1);
115 ret.paint_rects.insert(ret.paint_rects.end(), update_.paint_rects.begin(),
116 update_.paint_rects.end());
121 void PaintAggregator::SetIntermediateResults(
122 const std::vector<PaintReadyRect>& ready,
123 const std::vector<gfx::Rect>& pending) {
124 update_.ready_rects.insert(update_.ready_rects.end(), ready.begin(),
126 update_.paint_rects = pending;
129 std::vector<PaintReadyRect> PaintAggregator::GetReadyRects() const {
130 return update_.ready_rects;
133 void PaintAggregator::InvalidateRect(const gfx::Rect& rect) {
134 InvalidateRectInternal(rect, true);
137 void PaintAggregator::ScrollRect(const gfx::Rect& clip_rect,
138 const gfx::Vector2d& amount) {
139 // We only support scrolling along one axis at a time.
140 if (amount.x() != 0 && amount.y() != 0) {
141 InvalidateRect(clip_rect);
145 // We can only scroll one rect at a time.
146 if (!update_.scroll_rect.IsEmpty() && update_.scroll_rect != clip_rect) {
147 InvalidateRect(clip_rect);
151 // Again, we only support scrolling along one axis at a time. Make sure this
152 // update doesn't scroll on a different axis than any existing one.
153 if ((amount.x() && update_.scroll_delta.y()) ||
154 (amount.y() && update_.scroll_delta.x())) {
155 InvalidateRect(clip_rect);
159 // If we scroll in a reverse direction to the direction we originally scrolled
160 // and there were invalidations that happened in-between we may end up
161 // incorrectly clipping the invalidated rects (see crbug.com/488390). This bug
162 // doesn't exist in the original implementation
163 // (ppapi/utility/graphics/paint_aggregator.cc) which uses a different method
164 // of handling invalidations that occur after a scroll. The problem is that
165 // when we scroll the invalidated region, we clip it to the scroll rect. This
166 // can cause us to lose information about what the invalidated region was if
167 // it gets scrolled back into view. We either need to not do this clipping or
168 // disallow combining scrolls that occur in different directions with
169 // invalidations that happen in-between. This code really needs some tests...
170 if (!update_.paint_rects.empty()) {
171 if (IsNegative(amount.x()) != IsNegative(update_.scroll_delta.x()) ||
172 IsNegative(amount.y()) != IsNegative(update_.scroll_delta.y())) {
173 InvalidateRect(clip_rect);
178 // The scroll rect is new or isn't changing (though the scroll amount may
180 update_.scroll_rect = clip_rect;
181 update_.scroll_delta += amount;
183 // We might have just wiped out a pre-existing scroll.
184 if (update_.scroll_delta == gfx::Vector2d()) {
185 update_.scroll_rect = gfx::Rect();
189 // Adjust any paint rects that intersect the scroll. For the portion of the
190 // paint that is inside the scroll area, move it by the scroll amount and
191 // replace the existing paint with it. For the portion (if any) that is
192 // outside the scroll, just invalidate it.
193 std::vector<gfx::Rect> leftover_rects;
194 for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
195 if (!update_.scroll_rect.Intersects(update_.paint_rects[i]))
198 gfx::Rect intersection =
199 gfx::IntersectRects(update_.paint_rects[i], update_.scroll_rect);
200 gfx::Rect rect = update_.paint_rects[i];
201 while (!rect.IsEmpty()) {
202 gfx::Rect leftover = gfx::SubtractRects(rect, intersection);
203 if (leftover.IsEmpty())
205 // Don't want to call InvalidateRectInternal now since it'll modify
206 // update_.paint_rects, so keep track of this and do it below.
207 leftover_rects.push_back(leftover);
208 rect.Subtract(leftover);
211 update_.paint_rects[i] = ScrollPaintRect(intersection, amount);
213 // The rect may have been scrolled out of view.
214 if (update_.paint_rects[i].IsEmpty()) {
215 update_.paint_rects.erase(update_.paint_rects.begin() + i);
220 for (const auto& leftover_rect : leftover_rects)
221 InvalidateRectInternal(leftover_rect, false);
223 for (auto& update_rect : update_.ready_rects) {
224 if (update_.scroll_rect.Contains(update_rect.rect()))
225 update_rect.set_rect(ScrollPaintRect(update_rect.rect(), amount));
228 if (update_.synthesized_scroll_damage_rect_) {
229 InvalidateRect(update_.GetScrollDamage());
233 gfx::Rect PaintAggregator::ScrollPaintRect(const gfx::Rect& paint_rect,
234 const gfx::Vector2d& amount) const {
235 gfx::Rect result = paint_rect + amount;
236 result.Intersect(update_.scroll_rect);
240 void PaintAggregator::InvalidateScrollRect() {
241 gfx::Rect scroll_rect = update_.scroll_rect;
242 update_.scroll_rect = gfx::Rect();
243 update_.scroll_delta = gfx::Vector2d();
244 InvalidateRect(scroll_rect);
247 void PaintAggregator::InvalidateRectInternal(const gfx::Rect& rect_old,
249 gfx::Rect rect = rect_old;
250 // Check if any rects that are ready to be painted overlap.
251 for (size_t i = 0; i < update_.ready_rects.size(); ++i) {
252 const gfx::Rect& existing_rect = update_.ready_rects[i].rect();
253 if (rect.Intersects(existing_rect)) {
254 // Re-invalidate in case the union intersects other paint rects.
255 rect.Union(existing_rect);
256 update_.ready_rects.erase(update_.ready_rects.begin() + i);
261 bool add_paint = true;
263 // Combine overlapping paints using smallest bounding box.
264 for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
265 const gfx::Rect& existing_rect = update_.paint_rects[i];
266 if (existing_rect.Contains(rect)) // Optimize out redundancy.
268 if (rect.Intersects(existing_rect) || rect.SharesEdgeWith(existing_rect)) {
269 // Re-invalidate in case the union intersects other paint rects.
270 gfx::Rect combined_rect = gfx::UnionRects(rect, existing_rect);
271 update_.paint_rects.erase(update_.paint_rects.begin() + i);
272 InvalidateRectInternal(combined_rect, check_scroll);
278 // Add a non-overlapping paint.
279 update_.paint_rects.push_back(rect);
282 // If the new paint overlaps with a scroll, then also invalidate the rect in
284 if (check_scroll && !update_.scroll_rect.IsEmpty() &&
285 update_.scroll_rect.Intersects(rect)) {
286 InvalidateRectInternal(ScrollPaintRect(rect, update_.scroll_delta), false);
290 } // namespace chrome_pdf