Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "pdf/paint_manager.h" | 5 #include "pdf/paint_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
| 14 #include "ppapi/cpp/instance.h" | 14 #include "ppapi/cpp/instance.h" |
| 15 #include "ppapi/cpp/module.h" | 15 #include "ppapi/cpp/module.h" |
| 16 | 16 |
| 17 PaintManager::PaintManager(pp::Instance* instance, | 17 PaintManager::PaintManager(pp::Instance* instance, |
| 18 Client* client, | 18 Client* client, |
| 19 bool is_always_opaque) | 19 bool is_always_opaque) |
| 20 : instance_(instance), | 20 : instance_(instance), |
| 21 client_(client), | 21 client_(client), |
| 22 is_always_opaque_(is_always_opaque), | 22 is_always_opaque_(is_always_opaque), |
| 23 callback_factory_(NULL), | 23 callback_factory_(NULL), |
| 24 manual_callback_pending_(false), | 24 manual_callback_pending_(false), |
| 25 flush_pending_(false), | 25 flush_pending_(false), |
| 26 flush_requested_(false), | |
| 26 has_pending_resize_(false), | 27 has_pending_resize_(false), |
| 27 graphics_need_to_be_bound_(false), | 28 graphics_need_to_be_bound_(false), |
| 28 pending_device_scale_(1.0), | 29 pending_device_scale_(1.0), |
| 29 device_scale_(1.0), | 30 device_scale_(1.0), |
| 30 in_paint_(false), | 31 in_paint_(false), |
| 31 first_paint_(true), | 32 first_paint_(true), |
| 32 view_size_changed_waiting_for_paint_(false) { | 33 view_size_changed_waiting_for_paint_(false) { |
| 33 // Set the callback object outside of the initializer list to avoid a | 34 // Set the callback object outside of the initializer list to avoid a |
| 34 // compiler warning about using "this" in an initializer list. | 35 // compiler warning about using "this" in an initializer list. |
| 35 callback_factory_.Initialize(this); | 36 callback_factory_.Initialize(this); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 DCHECK(!instance_ && !client_); // Can't initialize twice. | 83 DCHECK(!instance_ && !client_); // Can't initialize twice. |
| 83 instance_ = instance; | 84 instance_ = instance; |
| 84 client_ = client; | 85 client_ = client; |
| 85 is_always_opaque_ = is_always_opaque; | 86 is_always_opaque_ = is_always_opaque; |
| 86 } | 87 } |
| 87 | 88 |
| 88 void PaintManager::SetSize(const pp::Size& new_size, float device_scale) { | 89 void PaintManager::SetSize(const pp::Size& new_size, float device_scale) { |
| 89 if (GetEffectiveSize() == new_size && | 90 if (GetEffectiveSize() == new_size && |
| 90 GetEffectiveDeviceScale() == device_scale) | 91 GetEffectiveDeviceScale() == device_scale) |
| 91 return; | 92 return; |
| 92 | |
| 93 has_pending_resize_ = true; | 93 has_pending_resize_ = true; |
| 94 pending_size_ = new_size; | 94 pending_size_ = new_size; |
| 95 pending_device_scale_ = device_scale; | 95 pending_device_scale_ = device_scale; |
| 96 | 96 |
| 97 view_size_changed_waiting_for_paint_ = true; | 97 view_size_changed_waiting_for_paint_ = true; |
| 98 Invalidate(); | |
| 99 } | |
| 98 | 100 |
| 99 Invalidate(); | 101 void PaintManager::SetTransform(float scale, |
| 102 pp::Point origin, | |
| 103 pp::Point translate) { | |
| 104 if (!flush_pending_) { | |
| 105 graphics_.SetLayerTransform(scale, origin, translate); | |
| 106 flush_pending_ = true; | |
| 107 flush_requested_ = false; | |
| 108 graphics_.Flush( | |
| 109 callback_factory_.NewCallback(&PaintManager::OnFlushComplete)); | |
| 110 scale_ = 1.f; | |
| 111 origin_ = pp::Point(); | |
| 112 transform_ = pp::Point(); | |
| 113 } else { | |
| 114 flush_requested_ = true; | |
| 115 graphics_.SetLayerTransform(scale, origin, translate); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void PaintManager::SetTransform(float scale) { | |
| 120 graphics_.SetLayerTransform(scale, pp::Point(), pp::Point()); | |
| 121 | |
| 122 EnsureCallbackPending(); | |
| 100 } | 123 } |
| 101 | 124 |
| 102 void PaintManager::Invalidate() { | 125 void PaintManager::Invalidate() { |
| 103 if (graphics_.is_null() && !has_pending_resize_) | 126 if (graphics_.is_null() && !has_pending_resize_) |
| 104 return; | 127 return; |
| 105 | 128 |
| 106 EnsureCallbackPending(); | 129 EnsureCallbackPending(); |
| 107 aggregator_.InvalidateRect(pp::Rect(GetEffectiveSize())); | 130 aggregator_.InvalidateRect(pp::Rect(GetEffectiveSize())); |
| 108 } | 131 } |
| 109 | 132 |
| 110 void PaintManager::InvalidateRect(const pp::Rect& rect) { | 133 void PaintManager::InvalidateRect(const pp::Rect& rect) { |
| 111 DCHECK(!in_paint_); | 134 DCHECK(!in_paint_); |
| 112 | 135 |
| 113 if (graphics_.is_null() && !has_pending_resize_) | 136 if (graphics_.is_null() && !has_pending_resize_) |
| 114 return; | 137 return; |
| 115 | 138 |
| 116 // Clip the rect to the device area. | 139 // Clip the rect to the device area. |
| 117 pp::Rect clipped_rect = rect.Intersect(pp::Rect(GetEffectiveSize())); | 140 pp::Rect clipped_rect = rect.Intersect(pp::Rect(GetEffectiveSize())); |
| 118 if (clipped_rect.IsEmpty()) | 141 if (clipped_rect.IsEmpty()) |
| 119 return; // Nothing to do. | 142 return; // Nothing to do. |
| 120 | 143 |
| 121 EnsureCallbackPending(); | 144 EnsureCallbackPending(); |
| 122 aggregator_.InvalidateRect(clipped_rect); | 145 aggregator_.InvalidateRect(clipped_rect); |
| 123 } | 146 } |
| 124 | 147 |
| 125 void PaintManager::ScrollRect(const pp::Rect& clip_rect, | 148 void PaintManager::ScrollRect(const pp::Rect& clip_rect, |
| 126 const pp::Point& amount) { | 149 const pp::Point& amount) { |
| 127 DCHECK(!in_paint_); | 150 DCHECK(!in_paint_); |
| 128 | |
|
wjmaclean
2016/04/21 14:11:02
Undo white space changes.
alessandroa
2016/04/22 14:33:05
Done.
| |
| 129 if (graphics_.is_null() && !has_pending_resize_) | 151 if (graphics_.is_null() && !has_pending_resize_) |
| 130 return; | 152 return; |
| 131 | 153 |
| 132 EnsureCallbackPending(); | 154 EnsureCallbackPending(); |
| 133 | 155 |
| 134 aggregator_.ScrollRect(clip_rect, amount); | 156 aggregator_.ScrollRect(clip_rect, amount); |
| 135 } | 157 } |
| 136 | 158 |
| 137 pp::Size PaintManager::GetEffectiveSize() const { | 159 pp::Size PaintManager::GetEffectiveSize() const { |
| 138 return has_pending_resize_ ? pending_size_ : plugin_size_; | 160 return has_pending_resize_ ? pending_size_ : plugin_size_; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 156 | 178 |
| 157 pp::Module::Get()->core()->CallOnMainThread( | 179 pp::Module::Get()->core()->CallOnMainThread( |
| 158 0, | 180 0, |
| 159 callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete), | 181 callback_factory_.NewCallback(&PaintManager::OnManualCallbackComplete), |
| 160 0); | 182 0); |
| 161 manual_callback_pending_ = true; | 183 manual_callback_pending_ = true; |
| 162 } | 184 } |
| 163 | 185 |
| 164 void PaintManager::DoPaint() { | 186 void PaintManager::DoPaint() { |
| 165 in_paint_ = true; | 187 in_paint_ = true; |
| 166 | |
|
wjmaclean
2016/04/21 14:11:02
Ditto.
alessandroa
2016/04/22 14:33:05
Done.
| |
| 167 std::vector<ReadyRect> ready; | 188 std::vector<ReadyRect> ready; |
| 168 std::vector<pp::Rect> pending; | 189 std::vector<pp::Rect> pending; |
| 169 | 190 |
| 170 DCHECK(aggregator_.HasPendingUpdate()); | 191 DCHECK(aggregator_.HasPendingUpdate()); |
| 171 | 192 |
| 172 // Apply any pending resize. Setting the graphics to this class must happen | 193 // Apply any pending resize. Setting the graphics to this class must happen |
| 173 // before asking the plugin to paint in case it requests invalides or resizes. | 194 // before asking the plugin to paint in case it requests invalides or resizes. |
| 174 // However, the bind must not happen until afterward since we don't want to | 195 // However, the bind must not happen until afterward since we don't want to |
| 175 // have an unpainted device bound. The needs_binding flag tells us whether to | 196 // have an unpainted device bound. The needs_binding flag tells us whether to |
| 176 // do this later. | 197 // do this later. |
| 177 if (has_pending_resize_) { | 198 if (has_pending_resize_) { |
| 178 plugin_size_ = pending_size_; | 199 plugin_size_ = pending_size_; |
| 179 // Only create a new graphics context if the current context isn't big | 200 // Only create a new graphics context if the current context isn't big |
| 180 // enough or if it is far too big. This avoids creating a new context if | 201 // enough or if it is far too big. This avoids creating a new context if |
| 181 // we only resize by a small amount. | 202 // we only resize by a small amount. |
| 182 pp::Size new_size = GetNewContextSize(graphics_.size(), pending_size_); | 203 pp::Size new_size = GetNewContextSize(graphics_.size(), pending_size_); |
| 183 if (graphics_.size() != new_size) { | 204 if (graphics_.size() != new_size) { |
| 184 graphics_ = pp::Graphics2D(instance_, new_size, is_always_opaque_); | 205 graphics_ = pp::Graphics2D(instance_, new_size, is_always_opaque_); |
| 185 graphics_need_to_be_bound_ = true; | 206 graphics_need_to_be_bound_ = true; |
| 186 | 207 |
| 187 // Since we're binding a new one, all of the callbacks have been canceled. | 208 // Since we're binding a new one, all of the callbacks have been canceled. |
| 188 manual_callback_pending_ = false; | 209 manual_callback_pending_ = false; |
| 189 flush_pending_ = false; | 210 flush_pending_ = false; |
| 190 callback_factory_.CancelAll(); | 211 callback_factory_.CancelAll(); |
| 191 } | 212 } |
| 192 | 213 |
| 193 if (pending_device_scale_ != 1.0) | 214 if (pending_device_scale_ != device_scale_) |
|
bokan
2016/04/21 18:20:17
Is this related to the pinch zoom functionality or
alessandroa
2016/04/22 14:33:05
Reverted :)
| |
| 194 graphics_.SetScale(1.0 / pending_device_scale_); | 215 graphics_.SetScale(1.0 / pending_device_scale_); |
| 216 | |
|
wjmaclean
2016/04/21 14:11:02
Remove blank line.
alessandroa
2016/04/22 14:33:05
Done.
| |
| 195 device_scale_ = pending_device_scale_; | 217 device_scale_ = pending_device_scale_; |
| 196 | 218 |
| 197 // This must be cleared before calling into the plugin since it may do | 219 // This must be cleared before calling into the plugin since it may do |
| 198 // additional invalidation or sizing operations. | 220 // additional invalidation or sizing operations. |
| 199 has_pending_resize_ = false; | 221 has_pending_resize_ = false; |
| 200 pending_size_ = pp::Size(); | 222 pending_size_ = pp::Size(); |
| 201 } | 223 } |
|
wjmaclean
2016/04/21 14:11:02
Undo this change ...
alessandroa
2016/04/22 14:33:05
Done.
| |
| 202 | 224 |
| 203 PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate(); | 225 PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate(); |
| 204 client_->OnPaint(update.paint_rects, &ready, &pending); | 226 client_->OnPaint(update.paint_rects, &ready, &pending); |
| 205 | 227 |
| 206 if (ready.empty() && pending.empty()) { | 228 if (ready.empty() && pending.empty()) { |
| 207 in_paint_ = false; | 229 in_paint_ = false; |
| 208 return; // Nothing was painted, don't schedule a flush. | 230 return; // Nothing was painted, don't schedule a flush. |
| 209 } | 231 } |
| 210 | 232 |
| 211 std::vector<PaintAggregator::ReadyRect> ready_now; | 233 std::vector<PaintAggregator::ReadyRect> ready_now; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 in_paint_ = false; | 266 in_paint_ = false; |
| 245 EnsureCallbackPending(); | 267 EnsureCallbackPending(); |
| 246 return; | 268 return; |
| 247 } | 269 } |
| 248 } | 270 } |
| 249 | 271 |
| 250 for (const auto& ready_rect : ready_now) { | 272 for (const auto& ready_rect : ready_now) { |
| 251 graphics_.PaintImageData( | 273 graphics_.PaintImageData( |
| 252 ready_rect.image_data, ready_rect.offset, ready_rect.rect); | 274 ready_rect.image_data, ready_rect.offset, ready_rect.rect); |
| 253 } | 275 } |
| 254 | |
| 255 int32_t result = graphics_.Flush( | 276 int32_t result = graphics_.Flush( |
| 256 callback_factory_.NewCallback(&PaintManager::OnFlushComplete)); | 277 callback_factory_.NewCallback(&PaintManager::OnFlushComplete)); |
| 257 | |
| 258 // If you trigger this assertion, then your plugin has called Flush() | 278 // If you trigger this assertion, then your plugin has called Flush() |
| 259 // manually. When using the PaintManager, you should not call Flush, it will | 279 // manually. When using the PaintManager, you should not call Flush, it will |
| 260 // handle that for you because it needs to know when it can do the next paint | 280 // handle that for you because it needs to know when it can do the next paint |
| 261 // by implementing the flush callback. | 281 // by implementing the flush callback. |
| 262 // | 282 // |
| 263 // Another possible cause of this assertion is re-using devices. If you | 283 // Another possible cause of this assertion is re-using devices. If you |
| 264 // use one device, swap it with another, then swap it back, we won't know | 284 // use one device, swap it with another, then swap it back, we won't know |
| 265 // that we've already scheduled a Flush on the first device. It's best to not | 285 // that we've already scheduled a Flush on the first device. It's best to not |
| 266 // re-use devices in this way. | 286 // re-use devices in this way. |
| 267 DCHECK(result != PP_ERROR_INPROGRESS); | 287 DCHECK(result != PP_ERROR_INPROGRESS); |
| 268 | 288 |
| 269 if (result == PP_OK_COMPLETIONPENDING) { | 289 if (result == PP_OK_COMPLETIONPENDING) { |
| 270 flush_pending_ = true; | 290 flush_pending_ = true; |
| 271 } else { | 291 } else { |
| 272 DCHECK(result == PP_OK); // Catch all other errors in debug mode. | 292 DCHECK(result == PP_OK); // Catch all other errors in debug mode. |
| 273 } | 293 } |
| 274 | 294 |
| 275 in_paint_ = false; | 295 in_paint_ = false; |
| 276 first_paint_ = false; | 296 first_paint_ = false; |
| 277 | 297 |
| 278 if (graphics_need_to_be_bound_) { | 298 if (graphics_need_to_be_bound_) { |
| 279 instance_->BindGraphics(graphics_); | 299 instance_->BindGraphics(graphics_); |
| 280 graphics_need_to_be_bound_ = false; | 300 graphics_need_to_be_bound_ = false; |
| 281 } | 301 } |
| 282 } | 302 } |
| 283 | 303 |
| 284 void PaintManager::OnFlushComplete(int32_t) { | 304 void PaintManager::OnFlushComplete(int32_t) { |
| 285 DCHECK(flush_pending_); | 305 DCHECK(flush_pending_); |
| 286 flush_pending_ = false; | 306 flush_pending_ = false; |
| 287 | 307 // If there was another flush request while flushing we flush again. |
| 308 if (flush_requested_) { | |
| 309 flush_requested_ = false; | |
| 310 graphics_.Flush( | |
| 311 callback_factory_.NewCallback(&PaintManager::OnFlushComplete)); | |
| 312 } | |
| 288 // If more paints were enqueued while we were waiting for the flush to | 313 // If more paints were enqueued while we were waiting for the flush to |
| 289 // complete, execute them now. | 314 // complete, execute them now. |
| 290 if (aggregator_.HasPendingUpdate()) | 315 if (aggregator_.HasPendingUpdate()) |
| 291 DoPaint(); | 316 DoPaint(); |
| 292 } | 317 } |
| 293 | 318 |
| 294 void PaintManager::OnManualCallbackComplete(int32_t) { | 319 void PaintManager::OnManualCallbackComplete(int32_t) { |
| 295 DCHECK(manual_callback_pending_); | 320 DCHECK(manual_callback_pending_); |
| 296 manual_callback_pending_ = false; | 321 manual_callback_pending_ = false; |
| 297 | |
| 298 // Just because we have a manual callback doesn't mean there are actually any | 322 // Just because we have a manual callback doesn't mean there are actually any |
| 299 // invalid regions. Even though we only schedule this callback when something | 323 // invalid regions. Even though we only schedule this callback when something |
| 300 // is pending, a Flush callback could have come in before this callback was | 324 // is pending, a Flush callback could have come in before this callback was |
| 301 // executed and that could have cleared the queue. | 325 // executed and that could have cleared the queue. |
| 302 if (aggregator_.HasPendingUpdate()) | 326 if (aggregator_.HasPendingUpdate()) |
| 303 DoPaint(); | 327 DoPaint(); |
| 304 } | 328 } |
| OLD | NEW |