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_manager.h"
12 #include "base/logging.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
17 PaintManager::ReadyRect::ReadyRect() = default;
19 PaintManager::ReadyRect::ReadyRect(const pp::Rect& r,
20 const pp::ImageData& i,
22 : rect(r), image_data(i), flush_now(f) {}
24 PaintManager::ReadyRect::ReadyRect(const ReadyRect& that) = default;
26 PaintManager::PaintManager(pp::Instance* instance,
28 bool is_always_opaque)
29 : instance_(instance),
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),
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);
47 // You can not use a NULL client pointer.
51 PaintManager::~PaintManager() = default;
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
58 const int kBufferSize = 50;
60 // Default to returning the same size.
61 pp::Size result = current_context_size;
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.
68 std::max(current_context_size.width() - 2 * kBufferSize, 0),
69 std::max(current_context_size.height() - 2 * kBufferSize, 0));
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);
88 void PaintManager::Initialize(pp::Instance* instance,
90 bool is_always_opaque) {
91 DCHECK(!instance_ && !client_); // Can't initialize twice.
94 is_always_opaque_ = is_always_opaque;
97 void PaintManager::SetSize(const pp::Size& new_size, float device_scale) {
98 if (GetEffectiveSize() == new_size &&
99 GetEffectiveDeviceScale() == device_scale)
102 has_pending_resize_ = true;
103 pending_size_ = new_size;
104 pending_device_scale_ = device_scale;
106 view_size_changed_waiting_for_paint_ = true;
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())
118 graphics_.SetLayerTransform(scale, origin, translate);
123 if (flush_pending_) {
124 flush_requested_ = true;
130 void PaintManager::ClearTransform() {
131 SetTransform(1.f, pp::Point(), pp::Point(), false);
134 void PaintManager::Invalidate() {
135 if (graphics_.is_null() && !has_pending_resize_)
138 EnsureCallbackPending();
139 aggregator_.InvalidateRect(pp::Rect(GetEffectiveSize()));
142 void PaintManager::InvalidateRect(const pp::Rect& rect) {
145 if (graphics_.is_null() && !has_pending_resize_)
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.
153 EnsureCallbackPending();
154 aggregator_.InvalidateRect(clipped_rect);
157 void PaintManager::ScrollRect(const pp::Rect& clip_rect,
158 const pp::Point& amount) {
161 if (graphics_.is_null() && !has_pending_resize_)
164 EnsureCallbackPending();
166 aggregator_.ScrollRect(clip_rect, amount);
169 pp::Size PaintManager::GetEffectiveSize() const {
170 return has_pending_resize_ ? pending_size_ : plugin_size_;
173 float PaintManager::GetEffectiveDeviceScale() const {
174 return has_pending_resize_ ? pending_device_scale_ : device_scale_;
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.
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_)
189 pp::Module::Get()->core()->CallOnMainThread(
190 0, callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete),
192 manual_callback_pending_ = true;
195 void PaintManager::DoPaint() {
198 std::vector<ReadyRect> ready_rects;
199 std::vector<pp::Rect> pending_rects;
201 DCHECK(aggregator_.HasPendingUpdate());
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
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;
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();
224 if (pending_device_scale_ != 1.0)
225 graphics_.SetScale(1.0 / pending_device_scale_);
226 device_scale_ = pending_device_scale_;
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();
234 PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate();
235 client_->OnPaint(update.paint_rects, &ready_rects, &pending_rects);
237 if (ready_rects.empty() && pending_rects.empty()) {
239 return; // Nothing was painted, don't schedule a flush.
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();
250 // Apply any scroll first.
251 if (update.has_scroll)
252 graphics_.Scroll(update.scroll_rect, update.scroll_delta);
254 view_size_changed_waiting_for_paint_ = false;
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);
267 ready_later.push_back(ready_rect);
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);
274 if (ready_now.empty()) {
276 EnsureCallbackPending();
281 for (const auto& ready_rect : ready_now) {
282 graphics_.PaintImageData(ready_rect.image_data, ready_rect.offset,
289 first_paint_ = false;
291 if (graphics_need_to_be_bound_) {
292 instance_->BindGraphics(graphics_);
293 graphics_need_to_be_bound_ = false;
297 void PaintManager::Flush() {
298 flush_requested_ = false;
300 int32_t result = graphics_.Flush(
301 callback_factory_.NewCallback(&PaintManager::OnFlushComplete));
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.
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);
314 if (result == PP_OK_COMPLETIONPENDING) {
315 flush_pending_ = true;
317 DCHECK(result == PP_OK); // Catch all other errors in debug mode.
321 void PaintManager::OnFlushComplete(int32_t) {
322 DCHECK(flush_pending_);
323 flush_pending_ = false;
325 // If more paints were enqueued while we were waiting for the flush to
326 // complete, execute them now.
327 if (aggregator_.HasPendingUpdate())
330 // If there was another flush request while flushing we flush again.
331 if (flush_requested_) {
336 void PaintManager::OnManualCallbackComplete(int32_t) {
337 DCHECK(manual_callback_pending_);
338 manual_callback_pending_ = false;
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())