| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/compositor/compositor.h" | 5 #include "ui/compositor/compositor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <deque> | 8 #include <deque> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 17 #include "cc/base/latency_info_swap_promise.h" | 17 #include "cc/base/latency_info_swap_promise.h" |
| 18 #include "cc/base/switches.h" | 18 #include "cc/base/switches.h" |
| 19 #include "cc/input/input_handler.h" | 19 #include "cc/input/input_handler.h" |
| 20 #include "cc/layers/layer.h" | 20 #include "cc/layers/layer.h" |
| 21 #include "cc/output/begin_frame_args.h" | 21 #include "cc/output/begin_frame_args.h" |
| 22 #include "cc/output/context_provider.h" | 22 #include "cc/output/context_provider.h" |
| 23 #include "cc/scheduler/begin_frame_source.h" | 23 #include "cc/scheduler/begin_frame_source.h" |
| 24 #include "cc/surfaces/surface_id_allocator.h" | 24 #include "cc/surfaces/surface_id_allocator.h" |
| 25 #include "cc/trees/layer_tree_host.h" | 25 #include "cc/trees/layer_tree_host.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
| 27 #include "ui/compositor/compositor_observer.h" | 27 #include "ui/compositor/compositor_observer.h" |
| 28 #include "ui/compositor/compositor_switches.h" | 28 #include "ui/compositor/compositor_switches.h" |
| 29 #include "ui/compositor/compositor_vsync_manager.h" | |
| 30 #include "ui/compositor/dip_util.h" | 29 #include "ui/compositor/dip_util.h" |
| 31 #include "ui/compositor/layer.h" | 30 #include "ui/compositor/layer.h" |
| 32 #include "ui/compositor/layer_animator_collection.h" | 31 #include "ui/compositor/layer_animator_collection.h" |
| 33 #include "ui/gfx/frame_time.h" | 32 #include "ui/gfx/frame_time.h" |
| 34 #include "ui/gl/gl_context.h" | 33 #include "ui/gl/gl_context.h" |
| 35 #include "ui/gl/gl_switches.h" | 34 #include "ui/gl/gl_switches.h" |
| 36 | 35 |
| 37 namespace { | 36 namespace { |
| 38 | 37 |
| 39 const double kDefaultRefreshRate = 60.0; | 38 const double kDefaultRefreshRate = 60.0; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 65 } | 64 } |
| 66 | 65 |
| 67 Compositor::Compositor(gfx::AcceleratedWidget widget, | 66 Compositor::Compositor(gfx::AcceleratedWidget widget, |
| 68 ui::ContextFactory* context_factory, | 67 ui::ContextFactory* context_factory, |
| 69 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 68 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 70 : context_factory_(context_factory), | 69 : context_factory_(context_factory), |
| 71 root_layer_(NULL), | 70 root_layer_(NULL), |
| 72 widget_(widget), | 71 widget_(widget), |
| 73 surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()), | 72 surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()), |
| 74 task_runner_(task_runner), | 73 task_runner_(task_runner), |
| 75 vsync_manager_(new CompositorVSyncManager()), | |
| 76 device_scale_factor_(0.0f), | 74 device_scale_factor_(0.0f), |
| 77 last_started_frame_(0), | 75 last_started_frame_(0), |
| 78 last_ended_frame_(0), | 76 last_ended_frame_(0), |
| 79 disable_schedule_composite_(false), | 77 disable_schedule_composite_(false), |
| 80 compositor_lock_(NULL), | 78 compositor_lock_(NULL), |
| 81 layer_animator_collection_(this), | 79 layer_animator_collection_(this), |
| 82 weak_ptr_factory_(this) { | 80 weak_ptr_factory_(this) { |
| 83 root_web_layer_ = cc::Layer::Create(); | 81 root_web_layer_ = cc::Layer::Create(); |
| 84 | 82 |
| 85 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 83 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 144 |
| 147 Compositor::~Compositor() { | 145 Compositor::~Compositor() { |
| 148 TRACE_EVENT0("shutdown", "Compositor::destructor"); | 146 TRACE_EVENT0("shutdown", "Compositor::destructor"); |
| 149 | 147 |
| 150 CancelCompositorLock(); | 148 CancelCompositorLock(); |
| 151 DCHECK(!compositor_lock_); | 149 DCHECK(!compositor_lock_); |
| 152 | 150 |
| 153 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, | 151 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 154 OnCompositingShuttingDown(this)); | 152 OnCompositingShuttingDown(this)); |
| 155 | 153 |
| 154 DCHECK(!begin_frame_observer_list_.might_have_observers()); |
| 155 |
| 156 if (root_layer_) | 156 if (root_layer_) |
| 157 root_layer_->SetCompositor(NULL); | 157 root_layer_->SetCompositor(NULL); |
| 158 | 158 |
| 159 // Stop all outstanding draws before telling the ContextFactory to tear | 159 // Stop all outstanding draws before telling the ContextFactory to tear |
| 160 // down any contexts that the |host_| may rely upon. | 160 // down any contexts that the |host_| may rely upon. |
| 161 host_.reset(); | 161 host_.reset(); |
| 162 | 162 |
| 163 context_factory_->RemoveCompositor(this); | 163 context_factory_->RemoveCompositor(this); |
| 164 } | 164 } |
| 165 | 165 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 242 } |
| 243 | 243 |
| 244 void Compositor::SetVisible(bool visible) { | 244 void Compositor::SetVisible(bool visible) { |
| 245 host_->SetVisible(visible); | 245 host_->SetVisible(visible); |
| 246 } | 246 } |
| 247 | 247 |
| 248 bool Compositor::IsVisible() { | 248 bool Compositor::IsVisible() { |
| 249 return host_->visible(); | 249 return host_->visible(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const { | 252 void Compositor::SetAuthoritativeVSyncInterval( |
| 253 return vsync_manager_; | 253 const base::TimeDelta& interval) { |
| 254 host_->SetAuthoritativeVSyncInterval(interval); |
| 254 } | 255 } |
| 255 | 256 |
| 256 void Compositor::AddObserver(CompositorObserver* observer) { | 257 void Compositor::AddObserver(CompositorObserver* observer) { |
| 257 observer_list_.AddObserver(observer); | 258 observer_list_.AddObserver(observer); |
| 258 } | 259 } |
| 259 | 260 |
| 260 void Compositor::RemoveObserver(CompositorObserver* observer) { | 261 void Compositor::RemoveObserver(CompositorObserver* observer) { |
| 261 observer_list_.RemoveObserver(observer); | 262 observer_list_.RemoveObserver(observer); |
| 262 } | 263 } |
| 263 | 264 |
| 264 bool Compositor::HasObserver(const CompositorObserver* observer) const { | 265 bool Compositor::HasObserver(const CompositorObserver* observer) const { |
| 265 return observer_list_.HasObserver(observer); | 266 return observer_list_.HasObserver(observer); |
| 266 } | 267 } |
| 267 | 268 |
| 268 void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) { | 269 void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) { |
| 269 animation_observer_list_.AddObserver(observer); | 270 animation_observer_list_.AddObserver(observer); |
| 270 host_->SetNeedsAnimate(); | 271 host_->SetNeedsAnimate(); |
| 271 } | 272 } |
| 272 | 273 |
| 273 void Compositor::RemoveAnimationObserver( | 274 void Compositor::RemoveAnimationObserver( |
| 274 CompositorAnimationObserver* observer) { | 275 CompositorAnimationObserver* observer) { |
| 275 animation_observer_list_.RemoveObserver(observer); | 276 animation_observer_list_.RemoveObserver(observer); |
| 276 } | 277 } |
| 277 | 278 |
| 278 bool Compositor::HasAnimationObserver( | 279 bool Compositor::HasAnimationObserver( |
| 279 const CompositorAnimationObserver* observer) const { | 280 const CompositorAnimationObserver* observer) const { |
| 280 return animation_observer_list_.HasObserver(observer); | 281 return animation_observer_list_.HasObserver(observer); |
| 281 } | 282 } |
| 282 | 283 |
| 284 void Compositor::AddBeginFrameObserver( |
| 285 CompositorBeginFrameObserver* observer, |
| 286 const cc::BeginFrameArgs& last_begin_frame_args_sent_to_observer) { |
| 287 // If |last_begin_frame_args_| is still effective, send it to the new |
| 288 // |observer| immediately. |
| 289 if (last_begin_frame_args_sent_to_observer.frame_time != |
| 290 missed_begin_frame_args_.frame_time) { |
| 291 observer->OnSendBeginFrame(missed_begin_frame_args_); |
| 292 } |
| 293 |
| 294 if (!begin_frame_observer_list_.might_have_observers()) |
| 295 SetChildrenNeedBeginFrames(true); |
| 296 begin_frame_observer_list_.AddObserver(observer); |
| 297 } |
| 298 |
| 299 void Compositor::RemoveBeginFrameObserver( |
| 300 CompositorBeginFrameObserver* observer) { |
| 301 DCHECK(begin_frame_observer_list_.HasObserver(observer)); |
| 302 begin_frame_observer_list_.RemoveObserver(observer); |
| 303 |
| 304 if (!begin_frame_observer_list_.might_have_observers()) |
| 305 SetChildrenNeedBeginFrames(false); |
| 306 } |
| 307 |
| 283 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) { | 308 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) { |
| 284 FOR_EACH_OBSERVER(CompositorAnimationObserver, | 309 FOR_EACH_OBSERVER(CompositorAnimationObserver, |
| 285 animation_observer_list_, | 310 animation_observer_list_, |
| 286 OnAnimationStep(args.frame_time)); | 311 OnAnimationStep(args.frame_time)); |
| 287 if (animation_observer_list_.might_have_observers()) | 312 if (animation_observer_list_.might_have_observers()) |
| 288 host_->SetNeedsAnimate(); | 313 host_->SetNeedsAnimate(); |
| 289 } | 314 } |
| 290 | 315 |
| 291 void Compositor::BeginMainFrameNotExpectedSoon() { | 316 void Compositor::BeginMainFrameNotExpectedSoon() { |
| 292 } | 317 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, | 358 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 334 OnCompositingStarted(this, start_time)); | 359 OnCompositingStarted(this, start_time)); |
| 335 } | 360 } |
| 336 | 361 |
| 337 void Compositor::DidAbortSwapBuffers() { | 362 void Compositor::DidAbortSwapBuffers() { |
| 338 FOR_EACH_OBSERVER(CompositorObserver, | 363 FOR_EACH_OBSERVER(CompositorObserver, |
| 339 observer_list_, | 364 observer_list_, |
| 340 OnCompositingAborted(this)); | 365 OnCompositingAborted(this)); |
| 341 } | 366 } |
| 342 | 367 |
| 368 void Compositor::SendBeginFramesToChildren(const cc::BeginFrameArgs& args) { |
| 369 FOR_EACH_OBSERVER(CompositorBeginFrameObserver, |
| 370 begin_frame_observer_list_, |
| 371 OnSendBeginFrame(args)); |
| 372 missed_begin_frame_args_ = args; |
| 373 missed_begin_frame_args_.type = cc::BeginFrameArgs::MISSED; |
| 374 } |
| 375 |
| 343 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 376 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 344 return host_->debug_state(); | 377 return host_->debug_state(); |
| 345 } | 378 } |
| 346 | 379 |
| 347 void Compositor::SetLayerTreeDebugState( | 380 void Compositor::SetLayerTreeDebugState( |
| 348 const cc::LayerTreeDebugState& debug_state) { | 381 const cc::LayerTreeDebugState& debug_state) { |
| 349 host_->SetDebugState(debug_state); | 382 host_->SetDebugState(debug_state); |
| 350 } | 383 } |
| 351 | 384 |
| 352 const cc::RendererSettings& Compositor::GetRendererSettings() const { | 385 const cc::RendererSettings& Compositor::GetRendererSettings() const { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 371 FOR_EACH_OBSERVER(CompositorObserver, | 404 FOR_EACH_OBSERVER(CompositorObserver, |
| 372 observer_list_, | 405 observer_list_, |
| 373 OnCompositingLockStateChanged(this)); | 406 OnCompositingLockStateChanged(this)); |
| 374 } | 407 } |
| 375 | 408 |
| 376 void Compositor::CancelCompositorLock() { | 409 void Compositor::CancelCompositorLock() { |
| 377 if (compositor_lock_) | 410 if (compositor_lock_) |
| 378 compositor_lock_->CancelLock(); | 411 compositor_lock_->CancelLock(); |
| 379 } | 412 } |
| 380 | 413 |
| 414 void Compositor::SetChildrenNeedBeginFrames(bool need_begin_frames) { |
| 415 host_->SetChildrenNeedBeginFrames(need_begin_frames); |
| 416 } |
| 417 |
| 381 } // namespace ui | 418 } // namespace ui |
| OLD | NEW |