Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / pdf / paint_manager.cc
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
5 #include "pdf/paint_manager.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <algorithm>
11
12 #include "base/logging.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
16
17 PaintManager::ReadyRect::ReadyRect() = default;
18
19 PaintManager::ReadyRect::ReadyRect(const pp::Rect& r,
20                                    const pp::ImageData& i,
21                                    bool f)
22     : rect(r), image_data(i), flush_now(f) {}
23
24 PaintManager::ReadyRect::ReadyRect(const ReadyRect& that) = default;
25
26 PaintManager::PaintManager(pp::Instance* instance,
27                            Client* client,
28                            bool is_always_opaque)
29     : instance_(instance),
30       client_(client),
31       is_always_opaque_(is_always_opaque),
32       callback_factory_(nullptr),
33       manual_callback_pending_(false),
34       flush_pending_(false),
35       flush_requested_(false),
36       has_pending_resize_(false),
37       graphics_need_to_be_bound_(false),
38       pending_device_scale_(1.0),
39       device_scale_(1.0),
40       in_paint_(false),
41       first_paint_(true),
42       view_size_changed_waiting_for_paint_(false) {
43   // Set the callback object outside of the initializer list to avoid a
44   // compiler warning about using "this" in an initializer list.
45   callback_factory_.Initialize(this);
46
47   // You can not use a NULL client pointer.
48   DCHECK(client);
49 }
50
51 PaintManager::~PaintManager() = default;
52
53 // static
54 pp::Size PaintManager::GetNewContextSize(const pp::Size& current_context_size,
55                                          const pp::Size& plugin_size) {
56   // The amount of additional space in pixels to allocate to the right/bottom of
57   // the context.
58   constexpr int kBufferSize = 50;
59
60   // Default to returning the same size.
61   pp::Size result = current_context_size;
62
63   // The minimum size of the plugin before resizing the context to ensure we
64   // aren't wasting too much memory. We deduct twice the kBufferSize from the
65   // current context size which gives a threshhold that is kBufferSize below
66   // the plugin size when the context size was last computed.
67   pp::Size min_size(
68       std::max(current_context_size.width() - 2 * kBufferSize, 0),
69       std::max(current_context_size.height() - 2 * kBufferSize, 0));
70
71   // If the plugin size is bigger than the current context size, we need to
72   // resize the context. If the plugin size is smaller than the current
73   // context size by a given threshhold then resize the context so that we
74   // aren't wasting too much memory.
75   if (plugin_size.width() > current_context_size.width() ||
76       plugin_size.height() > current_context_size.height() ||
77       plugin_size.width() < min_size.width() ||
78       plugin_size.height() < min_size.height()) {
79     // Create a larger context than needed so that if we only resize by a
80     // small margin, we don't need a new context.
81     result = pp::Size(plugin_size.width() + kBufferSize,
82                       plugin_size.height() + kBufferSize);
83   }
84
85   return result;
86 }
87
88 void PaintManager::Initialize(pp::Instance* instance,
89                               Client* client,
90                               bool is_always_opaque) {
91   DCHECK(!instance_ && !client_);  // Can't initialize twice.
92   instance_ = instance;
93   client_ = client;
94   is_always_opaque_ = is_always_opaque;
95 }
96
97 void PaintManager::SetSize(const pp::Size& new_size, float device_scale) {
98   if (GetEffectiveSize() == new_size &&
99       GetEffectiveDeviceScale() == device_scale)
100     return;
101
102   has_pending_resize_ = true;
103   pending_size_ = new_size;
104   pending_device_scale_ = device_scale;
105
106   view_size_changed_waiting_for_paint_ = true;
107
108   Invalidate();
109 }
110
111 void PaintManager::SetTransform(float scale,
112                                 const pp::Point& origin,
113                                 const pp::Point& translate,
114                                 bool schedule_flush) {
115   if (graphics_.is_null())
116     return;
117
118   graphics_.SetLayerTransform(scale, origin, translate);
119
120   if (!schedule_flush)
121     return;
122
123   if (flush_pending_) {
124     flush_requested_ = true;
125     return;
126   }
127   Flush();
128 }
129
130 void PaintManager::ClearTransform() {
131   SetTransform(1.f, pp::Point(), pp::Point(), false);
132 }
133
134 void PaintManager::Invalidate() {
135   if (graphics_.is_null() && !has_pending_resize_)
136     return;
137
138   EnsureCallbackPending();
139   aggregator_.InvalidateRect(pp::Rect(GetEffectiveSize()));
140 }
141
142 void PaintManager::InvalidateRect(const pp::Rect& rect) {
143   DCHECK(!in_paint_);
144
145   if (graphics_.is_null() && !has_pending_resize_)
146     return;
147
148   // Clip the rect to the device area.
149   pp::Rect clipped_rect = rect.Intersect(pp::Rect(GetEffectiveSize()));
150   if (clipped_rect.IsEmpty())
151     return;  // Nothing to do.
152
153   EnsureCallbackPending();
154   aggregator_.InvalidateRect(clipped_rect);
155 }
156
157 void PaintManager::ScrollRect(const pp::Rect& clip_rect,
158                               const pp::Point& amount) {
159   DCHECK(!in_paint_);
160
161   if (graphics_.is_null() && !has_pending_resize_)
162     return;
163
164   EnsureCallbackPending();
165
166   aggregator_.ScrollRect(clip_rect, amount);
167 }
168
169 pp::Size PaintManager::GetEffectiveSize() const {
170   return has_pending_resize_ ? pending_size_ : plugin_size_;
171 }
172
173 float PaintManager::GetEffectiveDeviceScale() const {
174   return has_pending_resize_ ? pending_device_scale_ : device_scale_;
175 }
176
177 void PaintManager::EnsureCallbackPending() {
178   // The best way for us to do the next update is to get a notification that
179   // a previous one has completed. So if we're already waiting for one, we
180   // don't have to do anything differently now.
181   if (flush_pending_)
182     return;
183
184   // If no flush is pending, we need to do a manual call to get back to the
185   // main thread. We may have one already pending, or we may need to schedule.
186   if (manual_callback_pending_)
187     return;
188
189   pp::Module::Get()->core()->CallOnMainThread(
190       0, callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete),
191       0);
192   manual_callback_pending_ = true;
193 }
194
195 void PaintManager::DoPaint() {
196   in_paint_ = true;
197
198   std::vector<ReadyRect> ready_rects;
199   std::vector<pp::Rect> pending_rects;
200
201   DCHECK(aggregator_.HasPendingUpdate());
202
203   // Apply any pending resize. Setting the graphics to this class must happen
204   // before asking the plugin to paint in case it requests invalides or resizes.
205   // However, the bind must not happen until afterward since we don't want to
206   // have an unpainted device bound. The needs_binding flag tells us whether to
207   // do this later.
208   if (has_pending_resize_) {
209     plugin_size_ = pending_size_;
210     // Only create a new graphics context if the current context isn't big
211     // enough or if it is far too big. This avoids creating a new context if
212     // we only resize by a small amount.
213     pp::Size new_size = GetNewContextSize(graphics_.size(), pending_size_);
214     if (graphics_.size() != new_size) {
215       graphics_ = pp::Graphics2D(instance_, new_size, is_always_opaque_);
216       graphics_need_to_be_bound_ = true;
217
218       // Since we're binding a new one, all of the callbacks have been canceled.
219       manual_callback_pending_ = false;
220       flush_pending_ = false;
221       callback_factory_.CancelAll();
222     }
223
224     if (pending_device_scale_ != 1.0)
225       graphics_.SetScale(1.0 / pending_device_scale_);
226     device_scale_ = pending_device_scale_;
227
228     // This must be cleared before calling into the plugin since it may do
229     // additional invalidation or sizing operations.
230     has_pending_resize_ = false;
231     pending_size_ = pp::Size();
232   }
233
234   PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate();
235   client_->OnPaint(update.paint_rects, &ready_rects, &pending_rects);
236
237   if (ready_rects.empty() && pending_rects.empty()) {
238     in_paint_ = false;
239     return;  // Nothing was painted, don't schedule a flush.
240   }
241
242   std::vector<PaintAggregator::ReadyRect> ready_now;
243   if (pending_rects.empty()) {
244     std::vector<PaintAggregator::ReadyRect> temp_ready;
245     temp_ready.insert(temp_ready.end(), ready_rects.begin(), ready_rects.end());
246     aggregator_.SetIntermediateResults(temp_ready, pending_rects);
247     ready_now = aggregator_.GetReadyRects();
248     aggregator_.ClearPendingUpdate();
249
250     // Apply any scroll first.
251     if (update.has_scroll)
252       graphics_.Scroll(update.scroll_rect, update.scroll_delta);
253
254     view_size_changed_waiting_for_paint_ = false;
255   } else {
256     std::vector<PaintAggregator::ReadyRect> ready_later;
257     for (const auto& ready_rect : ready_rects) {
258       // Don't flush any part (i.e. scrollbars) if we're resizing the browser,
259       // as that'll lead to flashes.  Until we flush, the browser will use the
260       // previous image, but if we flush, it'll revert to using the blank image.
261       // We make an exception for the first paint since we want to show the
262       // default background color instead of the pepper default of black.
263       if (ready_rect.flush_now &&
264           (!view_size_changed_waiting_for_paint_ || first_paint_)) {
265         ready_now.push_back(ready_rect);
266       } else {
267         ready_later.push_back(ready_rect);
268       }
269     }
270     // Take the rectangles, except the ones that need to be flushed right away,
271     // and save them so that everything is flushed at once.
272     aggregator_.SetIntermediateResults(ready_later, pending_rects);
273
274     if (ready_now.empty()) {
275       in_paint_ = false;
276       EnsureCallbackPending();
277       return;
278     }
279   }
280
281   for (const auto& ready_rect : ready_now) {
282     graphics_.PaintImageData(ready_rect.image_data, ready_rect.offset,
283                              ready_rect.rect);
284   }
285
286   Flush();
287
288   in_paint_ = false;
289   first_paint_ = false;
290
291   if (graphics_need_to_be_bound_) {
292     instance_->BindGraphics(graphics_);
293     graphics_need_to_be_bound_ = false;
294   }
295 }
296
297 void PaintManager::Flush() {
298   flush_requested_ = false;
299
300   int32_t result = graphics_.Flush(
301       callback_factory_.NewCallback(&PaintManager::OnFlushComplete));
302
303   // If you trigger this assertion, then your plugin has called Flush()
304   // manually. When using the PaintManager, you should not call Flush, it will
305   // handle that for you because it needs to know when it can do the next paint
306   // by implementing the flush callback.
307   //
308   // Another possible cause of this assertion is re-using devices. If you
309   // use one device, swap it with another, then swap it back, we won't know
310   // that we've already scheduled a Flush on the first device. It's best to not
311   // re-use devices in this way.
312   DCHECK(result != PP_ERROR_INPROGRESS);
313
314   if (result == PP_OK_COMPLETIONPENDING) {
315     flush_pending_ = true;
316   } else {
317     DCHECK(result == PP_OK);  // Catch all other errors in debug mode.
318   }
319 }
320
321 void PaintManager::OnFlushComplete(int32_t) {
322   DCHECK(flush_pending_);
323   flush_pending_ = false;
324
325   // If more paints were enqueued while we were waiting for the flush to
326   // complete, execute them now.
327   if (aggregator_.HasPendingUpdate())
328     DoPaint();
329
330   // If there was another flush request while flushing we flush again.
331   if (flush_requested_) {
332     Flush();
333   }
334 }
335
336 void PaintManager::OnManualCallbackComplete(int32_t) {
337   DCHECK(manual_callback_pending_);
338   manual_callback_pending_ = false;
339
340   // Just because we have a manual callback doesn't mean there are actually any
341   // invalid regions. Even though we only schedule this callback when something
342   // is pending, a Flush callback could have come in before this callback was
343   // executed and that could have cleared the queue.
344   if (aggregator_.HasPendingUpdate())
345     DoPaint();
346 }