Implementation of leveldb-backed PrefStore.
[chromium-blink-merge.git] / pdf / paint_manager.cc
blob093e0cd3e9972861cef225ad204bd8511802b0c6
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"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/cpp/instance.h"
12 #include "ppapi/cpp/module.h"
14 PaintManager::PaintManager(pp::Instance* instance,
15 Client* client,
16 bool is_always_opaque)
17 : instance_(instance),
18 client_(client),
19 is_always_opaque_(is_always_opaque),
20 callback_factory_(NULL),
21 manual_callback_pending_(false),
22 flush_pending_(false),
23 has_pending_resize_(false),
24 graphics_need_to_be_bound_(false),
25 pending_device_scale_(1.0),
26 device_scale_(1.0),
27 in_paint_(false),
28 first_paint_(true),
29 view_size_changed_waiting_for_paint_(false) {
30 // Set the callback object outside of the initializer list to avoid a
31 // compiler warning about using "this" in an initializer list.
32 callback_factory_.Initialize(this);
34 // You can not use a NULL client pointer.
35 DCHECK(client);
38 PaintManager::~PaintManager() {
41 // static
42 pp::Size PaintManager::GetNewContextSize(const pp::Size& current_context_size,
43 const pp::Size& plugin_size) {
44 // The number of additional space in pixels to allocate to the right/bottom
45 // of the context.
46 const int kBufferSize = 100;
48 // Default to returning the same size.
49 pp::Size result = current_context_size;
51 pp::Size min_size(std::max(current_context_size.width() - kBufferSize, 0),
52 std::max(current_context_size.height() - kBufferSize, 0));
53 // If the plugin size is bigger than the current context size, we need to
54 // resize the context. If the plugin size is smaller than the current
55 // context size by a given threshhold then resize the context so that we
56 // aren't wasting too much memory.
57 if (plugin_size.width() > current_context_size.width() ||
58 plugin_size.height() > current_context_size.height() ||
59 plugin_size.width() < min_size.width() ||
60 plugin_size.height() < min_size.height()) {
61 // Create a larger context than needed so that if we only resize by a
62 // small margin, we don't need a new context.
63 result = pp::Size(plugin_size.width() + kBufferSize,
64 plugin_size.height() + kBufferSize);
67 return result;
70 void PaintManager::Initialize(pp::Instance* instance,
71 Client* client,
72 bool is_always_opaque) {
73 DCHECK(!instance_ && !client_); // Can't initialize twice.
74 instance_ = instance;
75 client_ = client;
76 is_always_opaque_ = is_always_opaque;
79 void PaintManager::SetSize(const pp::Size& new_size, float device_scale) {
80 if (GetEffectiveSize() == new_size &&
81 GetEffectiveDeviceScale() == device_scale)
82 return;
84 has_pending_resize_ = true;
85 pending_size_ = new_size;
86 pending_device_scale_ = device_scale;
88 view_size_changed_waiting_for_paint_ = true;
90 Invalidate();
93 void PaintManager::Invalidate() {
94 // You must call SetSize before using.
95 DCHECK(!graphics_.is_null() || has_pending_resize_);
97 EnsureCallbackPending();
98 aggregator_.InvalidateRect(pp::Rect(GetEffectiveSize()));
101 void PaintManager::InvalidateRect(const pp::Rect& rect) {
102 DCHECK(!in_paint_);
104 // You must call SetSize before using.
105 DCHECK(!graphics_.is_null() || has_pending_resize_);
107 // Clip the rect to the device area.
108 pp::Rect clipped_rect = rect.Intersect(pp::Rect(GetEffectiveSize()));
109 if (clipped_rect.IsEmpty())
110 return; // Nothing to do.
112 EnsureCallbackPending();
113 aggregator_.InvalidateRect(clipped_rect);
116 void PaintManager::ScrollRect(const pp::Rect& clip_rect,
117 const pp::Point& amount) {
118 DCHECK(!in_paint_);
120 // You must call SetSize before using.
121 DCHECK(!graphics_.is_null() || has_pending_resize_);
123 EnsureCallbackPending();
125 aggregator_.ScrollRect(clip_rect, amount);
128 pp::Size PaintManager::GetEffectiveSize() const {
129 return has_pending_resize_ ? pending_size_ : plugin_size_;
132 float PaintManager::GetEffectiveDeviceScale() const {
133 return has_pending_resize_ ? pending_device_scale_ : device_scale_;
136 void PaintManager::EnsureCallbackPending() {
137 // The best way for us to do the next update is to get a notification that
138 // a previous one has completed. So if we're already waiting for one, we
139 // don't have to do anything differently now.
140 if (flush_pending_)
141 return;
143 // If no flush is pending, we need to do a manual call to get back to the
144 // main thread. We may have one already pending, or we may need to schedule.
145 if (manual_callback_pending_)
146 return;
148 pp::Module::Get()->core()->CallOnMainThread(
150 callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete),
152 manual_callback_pending_ = true;
155 void PaintManager::DoPaint() {
156 in_paint_ = true;
158 std::vector<ReadyRect> ready;
159 std::vector<pp::Rect> pending;
161 DCHECK(aggregator_.HasPendingUpdate());
163 // Apply any pending resize. Setting the graphics to this class must happen
164 // before asking the plugin to paint in case it requests invalides or resizes.
165 // However, the bind must not happen until afterward since we don't want to
166 // have an unpainted device bound. The needs_binding flag tells us whether to
167 // do this later.
168 if (has_pending_resize_) {
169 plugin_size_ = pending_size_;
170 // Only create a new graphics context if the current context isn't big
171 // enough or if it is far too big. This avoids creating a new context if
172 // we only resize by a small amount.
173 pp::Size new_size = GetNewContextSize(graphics_.size(), pending_size_);
174 if (graphics_.size() != new_size) {
175 graphics_ = pp::Graphics2D(instance_, new_size, is_always_opaque_);
176 graphics_need_to_be_bound_ = true;
178 // Since we're binding a new one, all of the callbacks have been canceled.
179 manual_callback_pending_ = false;
180 flush_pending_ = false;
181 callback_factory_.CancelAll();
184 if (pending_device_scale_ != 1.0)
185 graphics_.SetScale(1.0 / pending_device_scale_);
186 device_scale_ = pending_device_scale_;
188 // This must be cleared before calling into the plugin since it may do
189 // additional invalidation or sizing operations.
190 has_pending_resize_ = false;
191 pending_size_ = pp::Size();
194 PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate();
195 client_->OnPaint(update.paint_rects, &ready, &pending);
197 if (ready.empty() && pending.empty()) {
198 in_paint_ = false;
199 return; // Nothing was painted, don't schedule a flush.
202 std::vector<PaintAggregator::ReadyRect> ready_now;
203 if (pending.empty()) {
204 std::vector<PaintAggregator::ReadyRect> temp_ready;
205 for (size_t i = 0; i < ready.size(); ++i)
206 temp_ready.push_back(ready[i]);
207 aggregator_.SetIntermediateResults(temp_ready, pending);
208 ready_now = aggregator_.GetReadyRects();
209 aggregator_.ClearPendingUpdate();
211 // Apply any scroll first.
212 if (update.has_scroll)
213 graphics_.Scroll(update.scroll_rect, update.scroll_delta);
215 view_size_changed_waiting_for_paint_ = false;
216 } else {
217 std::vector<PaintAggregator::ReadyRect> ready_later;
218 for (size_t i = 0; i < ready.size(); ++i) {
219 // Don't flush any part (i.e. scrollbars) if we're resizing the browser,
220 // as that'll lead to flashes. Until we flush, the browser will use the
221 // previous image, but if we flush, it'll revert to using the blank image.
222 // We make an exception for the first paint since we want to show the
223 // default background color instead of the pepper default of black.
224 if (ready[i].flush_now &&
225 (!view_size_changed_waiting_for_paint_ || first_paint_)) {
226 ready_now.push_back(ready[i]);
227 } else {
228 ready_later.push_back(ready[i]);
231 // Take the rectangles, except the ones that need to be flushed right away,
232 // and save them so that everything is flushed at once.
233 aggregator_.SetIntermediateResults(ready_later, pending);
235 if (ready_now.empty()) {
236 in_paint_ = false;
237 EnsureCallbackPending();
238 return;
242 for (size_t i = 0; i < ready_now.size(); ++i) {
243 graphics_.PaintImageData(
244 ready_now[i].image_data, ready_now[i].offset, ready_now[i].rect);
247 int32_t result = graphics_.Flush(
248 callback_factory_.NewCallback(&PaintManager::OnFlushComplete));
250 // If you trigger this assertion, then your plugin has called Flush()
251 // manually. When using the PaintManager, you should not call Flush, it will
252 // handle that for you because it needs to know when it can do the next paint
253 // by implementing the flush callback.
255 // Another possible cause of this assertion is re-using devices. If you
256 // use one device, swap it with another, then swap it back, we won't know
257 // that we've already scheduled a Flush on the first device. It's best to not
258 // re-use devices in this way.
259 DCHECK(result != PP_ERROR_INPROGRESS);
261 if (result == PP_OK_COMPLETIONPENDING) {
262 flush_pending_ = true;
263 } else {
264 DCHECK(result == PP_OK); // Catch all other errors in debug mode.
267 in_paint_ = false;
268 first_paint_ = false;
270 if (graphics_need_to_be_bound_) {
271 instance_->BindGraphics(graphics_);
272 graphics_need_to_be_bound_ = false;
276 void PaintManager::OnFlushComplete(int32_t) {
277 DCHECK(flush_pending_);
278 flush_pending_ = false;
280 // If more paints were enqueued while we were waiting for the flush to
281 // complete, execute them now.
282 if (aggregator_.HasPendingUpdate())
283 DoPaint();
286 void PaintManager::OnManualCallbackComplete(int32_t) {
287 DCHECK(manual_callback_pending_);
288 manual_callback_pending_ = false;
290 // Just because we have a manual callback doesn't mean there are actually any
291 // invalid regions. Even though we only schedule this callback when something
292 // is pending, a Flush callback could have come in before this callback was
293 // executed and that could have cleared the queue.
294 if (aggregator_.HasPendingUpdate())
295 DoPaint();