OLD | NEW |
(Empty) | |
| 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 <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/cpp/instance.h" |
| 12 #include "ppapi/cpp/module.h" |
| 13 |
| 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); |
| 33 |
| 34 // You can not use a NULL client pointer. |
| 35 DCHECK(client); |
| 36 } |
| 37 |
| 38 PaintManager::~PaintManager() { |
| 39 } |
| 40 |
| 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; |
| 47 |
| 48 // Default to returning the same size. |
| 49 pp::Size result = current_context_size; |
| 50 |
| 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); |
| 65 } |
| 66 |
| 67 return result; |
| 68 } |
| 69 |
| 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; |
| 77 } |
| 78 |
| 79 void PaintManager::SetSize(const pp::Size& new_size, float device_scale) { |
| 80 if (GetEffectiveSize() == new_size && |
| 81 GetEffectiveDeviceScale() == device_scale) |
| 82 return; |
| 83 |
| 84 has_pending_resize_ = true; |
| 85 pending_size_ = new_size; |
| 86 pending_device_scale_ = device_scale; |
| 87 |
| 88 view_size_changed_waiting_for_paint_ = true; |
| 89 |
| 90 Invalidate(); |
| 91 } |
| 92 |
| 93 void PaintManager::Invalidate() { |
| 94 // You must call SetSize before using. |
| 95 DCHECK(!graphics_.is_null() || has_pending_resize_); |
| 96 |
| 97 EnsureCallbackPending(); |
| 98 aggregator_.InvalidateRect(pp::Rect(GetEffectiveSize())); |
| 99 } |
| 100 |
| 101 void PaintManager::InvalidateRect(const pp::Rect& rect) { |
| 102 DCHECK(!in_paint_); |
| 103 |
| 104 // You must call SetSize before using. |
| 105 DCHECK(!graphics_.is_null() || has_pending_resize_); |
| 106 |
| 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. |
| 111 |
| 112 EnsureCallbackPending(); |
| 113 aggregator_.InvalidateRect(clipped_rect); |
| 114 } |
| 115 |
| 116 void PaintManager::ScrollRect(const pp::Rect& clip_rect, |
| 117 const pp::Point& amount) { |
| 118 DCHECK(!in_paint_); |
| 119 |
| 120 // You must call SetSize before using. |
| 121 DCHECK(!graphics_.is_null() || has_pending_resize_); |
| 122 |
| 123 EnsureCallbackPending(); |
| 124 |
| 125 aggregator_.ScrollRect(clip_rect, amount); |
| 126 } |
| 127 |
| 128 pp::Size PaintManager::GetEffectiveSize() const { |
| 129 return has_pending_resize_ ? pending_size_ : plugin_size_; |
| 130 } |
| 131 |
| 132 float PaintManager::GetEffectiveDeviceScale() const { |
| 133 return has_pending_resize_ ? pending_device_scale_ : device_scale_; |
| 134 } |
| 135 |
| 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; |
| 142 |
| 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; |
| 147 |
| 148 pp::Module::Get()->core()->CallOnMainThread( |
| 149 0, |
| 150 callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete), |
| 151 0); |
| 152 manual_callback_pending_ = true; |
| 153 } |
| 154 |
| 155 void PaintManager::DoPaint() { |
| 156 in_paint_ = true; |
| 157 |
| 158 std::vector<ReadyRect> ready; |
| 159 std::vector<pp::Rect> pending; |
| 160 |
| 161 DCHECK(aggregator_.HasPendingUpdate()); |
| 162 |
| 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; |
| 177 |
| 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(); |
| 182 } |
| 183 |
| 184 if (pending_device_scale_ != 1.0) |
| 185 graphics_.SetScale(1.0 / pending_device_scale_); |
| 186 device_scale_ = pending_device_scale_; |
| 187 |
| 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(); |
| 192 } |
| 193 |
| 194 PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate(); |
| 195 client_->OnPaint(update.paint_rects, &ready, &pending); |
| 196 |
| 197 if (ready.empty() && pending.empty()) { |
| 198 in_paint_ = false; |
| 199 return; // Nothing was painted, don't schedule a flush. |
| 200 } |
| 201 |
| 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(); |
| 210 |
| 211 // Apply any scroll first. |
| 212 if (update.has_scroll) |
| 213 graphics_.Scroll(update.scroll_rect, update.scroll_delta); |
| 214 |
| 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]); |
| 229 } |
| 230 } |
| 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); |
| 234 |
| 235 if (ready_now.empty()) { |
| 236 in_paint_ = false; |
| 237 EnsureCallbackPending(); |
| 238 return; |
| 239 } |
| 240 } |
| 241 |
| 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); |
| 245 } |
| 246 |
| 247 int32_t result = graphics_.Flush( |
| 248 callback_factory_.NewCallback(&PaintManager::OnFlushComplete)); |
| 249 |
| 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. |
| 254 // |
| 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); |
| 260 |
| 261 if (result == PP_OK_COMPLETIONPENDING) { |
| 262 flush_pending_ = true; |
| 263 } else { |
| 264 DCHECK(result == PP_OK); // Catch all other errors in debug mode. |
| 265 } |
| 266 |
| 267 in_paint_ = false; |
| 268 first_paint_ = false; |
| 269 |
| 270 if (graphics_need_to_be_bound_) { |
| 271 instance_->BindGraphics(graphics_); |
| 272 graphics_need_to_be_bound_ = false; |
| 273 } |
| 274 } |
| 275 |
| 276 void PaintManager::OnFlushComplete(int32_t) { |
| 277 DCHECK(flush_pending_); |
| 278 flush_pending_ = false; |
| 279 |
| 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(); |
| 284 } |
| 285 |
| 286 void PaintManager::OnManualCallbackComplete(int32_t) { |
| 287 DCHECK(manual_callback_pending_); |
| 288 manual_callback_pending_ = false; |
| 289 |
| 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(); |
| 296 } |
OLD | NEW |