Removal of UpdateMultivaluedField() as we now don't have multiple values for name...
[chromium-blink-merge.git] / pdf / paint_aggregator.cc
blob09073ec30fa76d5e5c208e36ed7b23f2ab17f1d5
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.
5 #include "pdf/paint_aggregator.h"
7 #include <algorithm>
9 #include "base/logging.h"
11 namespace {
13 bool IsNegative(int32_t num) {
14 return num < 0;
17 } // namespace
19 // ----------------------------------------------------------------------------
20 // ALGORITHM NOTES
22 // We attempt to maintain a scroll rect in the presence of invalidations that
23 // are contained within the scroll rect. If an invalidation crosses a scroll
24 // rect, then we just treat the scroll rect as an invalidation rect.
26 // For invalidations performed prior to scrolling and contained within the
27 // scroll rect, we offset the invalidation rects to account for the fact that
28 // the consumer will perform scrolling before painting.
30 // We only support scrolling along one axis at a time. A diagonal scroll will
31 // therefore be treated as an invalidation.
32 // ----------------------------------------------------------------------------
34 PaintAggregator::PaintUpdate::PaintUpdate() {
37 PaintAggregator::PaintUpdate::~PaintUpdate() {
40 PaintAggregator::InternalPaintUpdate::InternalPaintUpdate() :
41 synthesized_scroll_damage_rect_(false) {
44 PaintAggregator::InternalPaintUpdate::~InternalPaintUpdate() {
47 pp::Rect PaintAggregator::InternalPaintUpdate::GetScrollDamage() const {
48 // Should only be scrolling in one direction at a time.
49 DCHECK(!(scroll_delta.x() && scroll_delta.y()));
51 pp::Rect damaged_rect;
53 // Compute the region we will expose by scrolling, and paint that into a
54 // shared memory section.
55 if (scroll_delta.x()) {
56 int32_t dx = scroll_delta.x();
57 damaged_rect.set_y(scroll_rect.y());
58 damaged_rect.set_height(scroll_rect.height());
59 if (dx > 0) {
60 damaged_rect.set_x(scroll_rect.x());
61 damaged_rect.set_width(dx);
62 } else {
63 damaged_rect.set_x(scroll_rect.right() + dx);
64 damaged_rect.set_width(-dx);
66 } else {
67 int32_t dy = scroll_delta.y();
68 damaged_rect.set_x(scroll_rect.x());
69 damaged_rect.set_width(scroll_rect.width());
70 if (dy > 0) {
71 damaged_rect.set_y(scroll_rect.y());
72 damaged_rect.set_height(dy);
73 } else {
74 damaged_rect.set_y(scroll_rect.bottom() + dy);
75 damaged_rect.set_height(-dy);
79 // In case the scroll offset exceeds the width/height of the scroll rect
80 return scroll_rect.Intersect(damaged_rect);
83 PaintAggregator::PaintAggregator() {
86 bool PaintAggregator::HasPendingUpdate() const {
87 return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
90 void PaintAggregator::ClearPendingUpdate() {
91 update_ = InternalPaintUpdate();
94 PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() {
95 // Convert the internal paint update to the external one, which includes a
96 // bit more precomputed info for the caller.
97 PaintUpdate ret;
98 ret.scroll_delta = update_.scroll_delta;
99 ret.scroll_rect = update_.scroll_rect;
100 ret.has_scroll = ret.scroll_delta.x() != 0 || ret.scroll_delta.y() != 0;
102 // Include the scroll damage (if any) in the paint rects.
103 // Code invalidates damaged rect here, it pick it up from the list of paint
104 // rects in the next block.
105 if (ret.has_scroll && !update_.synthesized_scroll_damage_rect_) {
106 update_.synthesized_scroll_damage_rect_ = true;
107 pp::Rect scroll_damage = update_.GetScrollDamage();
108 InvalidateRectInternal(scroll_damage, false);
111 ret.paint_rects.reserve(update_.paint_rects.size() + 1);
112 for (size_t i = 0; i < update_.paint_rects.size(); i++)
113 ret.paint_rects.push_back(update_.paint_rects[i]);
115 return ret;
118 void PaintAggregator::SetIntermediateResults(
119 const std::vector<ReadyRect>& ready,
120 const std::vector<pp::Rect>& pending) {
121 update_.ready_rects.insert(
122 update_.ready_rects.end(), ready.begin(), ready.end());
123 update_.paint_rects = pending;
126 std::vector<PaintAggregator::ReadyRect> PaintAggregator::GetReadyRects() const {
127 return update_.ready_rects;
130 void PaintAggregator::InvalidateRect(const pp::Rect& rect) {
131 InvalidateRectInternal(rect, true);
134 void PaintAggregator::ScrollRect(const pp::Rect& clip_rect,
135 const pp::Point& amount) {
136 // We only support scrolling along one axis at a time.
137 if (amount.x() != 0 && amount.y() != 0) {
138 InvalidateRect(clip_rect);
139 return;
142 // We can only scroll one rect at a time.
143 if (!update_.scroll_rect.IsEmpty() && update_.scroll_rect != clip_rect) {
144 InvalidateRect(clip_rect);
145 return;
148 // Again, we only support scrolling along one axis at a time. Make sure this
149 // update doesn't scroll on a different axis than any existing one.
150 if ((amount.x() && update_.scroll_delta.y()) ||
151 (amount.y() && update_.scroll_delta.x())) {
152 InvalidateRect(clip_rect);
153 return;
156 // If we scroll in a reverse direction to the direction we originally scrolled
157 // and there were invalidations that happened in-between we may end up
158 // incorrectly clipping the invalidated rects (see crbug.com/488390). This bug
159 // doesn't exist in the original implementation
160 // (ppapi/utility/graphics/paint_aggregator.cc) which uses a different method
161 // of handling invalidations that occur after a scroll. The problem is that
162 // when we scroll the invalidated region, we clip it to the scroll rect. This
163 // can cause us to lose information about what the invalidated region was if
164 // it gets scrolled back into view. We either need to not do this clipping or
165 // disallow combining scrolls that occur in different directions with
166 // invalidations that happen in-between. This code really needs some tests...
167 if (!update_.paint_rects.empty()) {
168 if (IsNegative(amount.x()) != IsNegative(update_.scroll_delta.x()) ||
169 IsNegative(amount.y()) != IsNegative(update_.scroll_delta.y())) {
170 InvalidateRect(clip_rect);
171 return;
175 // The scroll rect is new or isn't changing (though the scroll amount may
176 // be changing).
177 update_.scroll_rect = clip_rect;
178 update_.scroll_delta += amount;
180 // We might have just wiped out a pre-existing scroll.
181 if (update_.scroll_delta == pp::Point()) {
182 update_.scroll_rect = pp::Rect();
183 return;
186 // Adjust any paint rects that intersect the scroll. For the portion of the
187 // paint that is inside the scroll area, move it by the scroll amount and
188 // replace the existing paint with it. For the portion (if any) that is
189 // outside the scroll, just invalidate it.
190 std::vector<pp::Rect> leftover_rects;
191 for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
192 if (!update_.scroll_rect.Intersects(update_.paint_rects[i]))
193 continue;
195 pp::Rect intersection =
196 update_.paint_rects[i].Intersect(update_.scroll_rect);
197 pp::Rect rect = update_.paint_rects[i];
198 while (!rect.IsEmpty()) {
199 pp::Rect leftover = rect.Subtract(intersection);
200 if (leftover.IsEmpty())
201 break;
202 // Don't want to call InvalidateRectInternal now since it'll modify
203 // update_.paint_rects, so keep track of this and do it below.
204 leftover_rects.push_back(leftover);
205 rect = rect.Subtract(leftover);
208 update_.paint_rects[i] = ScrollPaintRect(intersection, amount);
210 // The rect may have been scrolled out of view.
211 if (update_.paint_rects[i].IsEmpty()) {
212 update_.paint_rects.erase(update_.paint_rects.begin() + i);
213 i--;
217 for (size_t i = 0; i < leftover_rects.size(); ++i)
218 InvalidateRectInternal(leftover_rects[i], false);
220 for (size_t i = 0; i < update_.ready_rects.size(); ++i) {
221 if (update_.scroll_rect.Contains(update_.ready_rects[i].rect)) {
222 update_.ready_rects[i].rect =
223 ScrollPaintRect(update_.ready_rects[i].rect, amount);
227 if (update_.synthesized_scroll_damage_rect_) {
228 pp::Rect damage = update_.GetScrollDamage();
229 InvalidateRect(damage);
233 pp::Rect PaintAggregator::ScrollPaintRect(const pp::Rect& paint_rect,
234 const pp::Point& amount) const {
235 pp::Rect result = paint_rect;
236 result.Offset(amount);
237 result = update_.scroll_rect.Intersect(result);
238 return result;
241 void PaintAggregator::InvalidateScrollRect() {
242 pp::Rect scroll_rect = update_.scroll_rect;
243 update_.scroll_rect = pp::Rect();
244 update_.scroll_delta = pp::Point();
245 InvalidateRect(scroll_rect);
248 void PaintAggregator::InvalidateRectInternal(const pp::Rect& rect_old,
249 bool check_scroll) {
250 pp::Rect rect = rect_old;
251 // Check if any rects that are ready to be painted overlap.
252 for (size_t i = 0; i < update_.ready_rects.size(); ++i) {
253 const pp::Rect& existing_rect = update_.ready_rects[i].rect;
254 if (rect.Intersects(existing_rect)) {
255 // Re-invalidate in case the union intersects other paint rects.
256 rect = existing_rect.Union(rect);
257 update_.ready_rects.erase(update_.ready_rects.begin() + i);
258 break;
262 bool add_paint = true;
264 // Combine overlapping paints using smallest bounding box.
265 for (size_t i = 0; i < update_.paint_rects.size(); ++i) {
266 const pp::Rect& existing_rect = update_.paint_rects[i];
267 if (existing_rect.Contains(rect)) // Optimize out redundancy.
268 add_paint = false;
269 if (rect.Intersects(existing_rect) || rect.SharesEdgeWith(existing_rect)) {
270 // Re-invalidate in case the union intersects other paint rects.
271 pp::Rect combined_rect = existing_rect.Union(rect);
272 update_.paint_rects.erase(update_.paint_rects.begin() + i);
273 InvalidateRectInternal(combined_rect, check_scroll);
274 add_paint = false;
278 if (add_paint) {
279 // Add a non-overlapping paint.
280 update_.paint_rects.push_back(rect);
283 // If the new paint overlaps with a scroll, then also invalidate the rect in
284 // its new position.
285 if (check_scroll &&
286 !update_.scroll_rect.IsEmpty() &&
287 update_.scroll_rect.Intersects(rect)) {
288 InvalidateRectInternal(ScrollPaintRect(rect, update_.scroll_delta), false);