fixup! [M120 Migration][NaCl][PPFWK] Upgradable pepper plugin requirement
[platform/framework/web/chromium-efl.git] / pdf / paint_aggregator.cc
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.
4
5 #include "pdf/paint_aggregator.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
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"
14
15 namespace chrome_pdf {
16
17 namespace {
18
19 bool IsNegative(int32_t num) {
20   return num < 0;
21 }
22
23 }  // namespace
24
25 // ----------------------------------------------------------------------------
26 // ALGORITHM NOTES
27 //
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.
31 //
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.
35 //
36 // We only support scrolling along one axis at a time.  A diagonal scroll will
37 // therefore be treated as an invalidation.
38 // ----------------------------------------------------------------------------
39
40 PaintAggregator::PaintUpdate::PaintUpdate() = default;
41
42 PaintAggregator::PaintUpdate::PaintUpdate(const PaintUpdate& that) = default;
43
44 PaintAggregator::PaintUpdate::~PaintUpdate() = default;
45
46 PaintAggregator::InternalPaintUpdate::InternalPaintUpdate()
47     : synthesized_scroll_damage_rect_(false) {}
48
49 PaintAggregator::InternalPaintUpdate::~InternalPaintUpdate() = default;
50
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()));
54
55   gfx::Rect damaged_rect;
56
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());
63     if (dx > 0) {
64       damaged_rect.set_x(scroll_rect.x());
65       damaged_rect.set_width(dx);
66     } else {
67       damaged_rect.set_x(scroll_rect.right() + dx);
68       damaged_rect.set_width(-dx);
69     }
70   } else {
71     int32_t dy = scroll_delta.y();
72     damaged_rect.set_x(scroll_rect.x());
73     damaged_rect.set_width(scroll_rect.width());
74     if (dy > 0) {
75       damaged_rect.set_y(scroll_rect.y());
76       damaged_rect.set_height(dy);
77     } else {
78       damaged_rect.set_y(scroll_rect.bottom() + dy);
79       damaged_rect.set_height(-dy);
80     }
81   }
82
83   // In case the scroll offset exceeds the width/height of the scroll rect
84   return gfx::IntersectRects(scroll_rect, damaged_rect);
85 }
86
87 PaintAggregator::PaintAggregator() = default;
88
89 bool PaintAggregator::HasPendingUpdate() const {
90   return !update_.scroll_rect.IsEmpty() || !update_.paint_rects.empty();
91 }
92
93 void PaintAggregator::ClearPendingUpdate() {
94   update_ = InternalPaintUpdate();
95 }
96
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.
100   PaintUpdate ret;
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;
104
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);
112   }
113
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());
117
118   return ret;
119 }
120
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(),
125                              ready.end());
126   update_.paint_rects = pending;
127 }
128
129 std::vector<PaintReadyRect> PaintAggregator::GetReadyRects() const {
130   return update_.ready_rects;
131 }
132
133 void PaintAggregator::InvalidateRect(const gfx::Rect& rect) {
134   InvalidateRectInternal(rect, true);
135 }
136
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);
142     return;
143   }
144
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);
148     return;
149   }
150
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);
156     return;
157   }
158
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);
174       return;
175     }
176   }
177
178   // The scroll rect is new or isn't changing (though the scroll amount may
179   // be changing).
180   update_.scroll_rect = clip_rect;
181   update_.scroll_delta += amount;
182
183   // We might have just wiped out a pre-existing scroll.
184   if (update_.scroll_delta == gfx::Vector2d()) {
185     update_.scroll_rect = gfx::Rect();
186     return;
187   }
188
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]))
196       continue;
197
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())
204         break;
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);
209     }
210
211     update_.paint_rects[i] = ScrollPaintRect(intersection, amount);
212
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);
216       i--;
217     }
218   }
219
220   for (const auto& leftover_rect : leftover_rects)
221     InvalidateRectInternal(leftover_rect, false);
222
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));
226   }
227
228   if (update_.synthesized_scroll_damage_rect_) {
229     InvalidateRect(update_.GetScrollDamage());
230   }
231 }
232
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);
237   return result;
238 }
239
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);
245 }
246
247 void PaintAggregator::InvalidateRectInternal(const gfx::Rect& rect_old,
248                                              bool check_scroll) {
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);
257       break;
258     }
259   }
260
261   bool add_paint = true;
262
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.
267       add_paint = false;
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);
273       add_paint = false;
274     }
275   }
276
277   if (add_paint) {
278     // Add a non-overlapping paint.
279     update_.paint_rects.push_back(rect);
280   }
281
282   // If the new paint overlaps with a scroll, then also invalidate the rect in
283   // its new position.
284   if (check_scroll && !update_.scroll_rect.IsEmpty() &&
285       update_.scroll_rect.Intersects(rect)) {
286     InvalidateRectInternal(ScrollPaintRect(rect, update_.scroll_delta), false);
287   }
288 }
289
290 }  // namespace chrome_pdf