| 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" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()), | 73 surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()), |
| 74 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), | 74 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), |
| 75 task_runner_(task_runner), | 75 task_runner_(task_runner), |
| 76 vsync_manager_(new CompositorVSyncManager()), | 76 vsync_manager_(new CompositorVSyncManager()), |
| 77 device_scale_factor_(0.0f), | 77 device_scale_factor_(0.0f), |
| 78 last_started_frame_(0), | 78 last_started_frame_(0), |
| 79 last_ended_frame_(0), | 79 last_ended_frame_(0), |
| 80 num_failed_recreate_attempts_(0), | 80 num_failed_recreate_attempts_(0), |
| 81 disable_schedule_composite_(false), | 81 disable_schedule_composite_(false), |
| 82 compositor_lock_(NULL), | 82 compositor_lock_(NULL), |
| 83 defer_draw_scheduling_(false), | |
| 84 waiting_on_compositing_end_(false), | |
| 85 draw_on_compositing_end_(false), | |
| 86 swap_state_(SWAP_NONE), | |
| 87 layer_animator_collection_(this), | 83 layer_animator_collection_(this), |
| 88 weak_ptr_factory_(this) { | 84 weak_ptr_factory_(this) { |
| 89 root_web_layer_ = cc::Layer::Create(); | 85 root_web_layer_ = cc::Layer::Create(); |
| 90 | 86 |
| 91 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 87 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 92 | 88 |
| 93 cc::LayerTreeSettings settings; | 89 cc::LayerTreeSettings settings; |
| 94 // When impl-side painting is enabled, this will ensure PictureLayers always | 90 // When impl-side painting is enabled, this will ensure PictureLayers always |
| 95 // can have LCD text, to match the previous behaviour with ContentLayers, | 91 // can have LCD text, to match the previous behaviour with ContentLayers, |
| 96 // where LCD-not-allowed notifications were ignored. | 92 // where LCD-not-allowed notifications were ignored. |
| 97 settings.layers_always_allowed_lcd_text = true; | 93 settings.layers_always_allowed_lcd_text = true; |
| 98 settings.renderer_settings.refresh_rate = | 94 settings.renderer_settings.refresh_rate = |
| 99 context_factory_->DoesCreateTestContexts() ? kTestRefreshRate | 95 context_factory_->DoesCreateTestContexts() ? kTestRefreshRate |
| 100 : kDefaultRefreshRate; | 96 : kDefaultRefreshRate; |
| 101 settings.main_frame_before_activation_enabled = false; | 97 settings.main_frame_before_activation_enabled = false; |
| 102 settings.throttle_frame_production = | 98 settings.throttle_frame_production = |
| 103 !command_line->HasSwitch(switches::kDisableGpuVsync); | 99 !command_line->HasSwitch(switches::kDisableGpuVsync); |
| 104 #if !defined(OS_MACOSX) | 100 #if !defined(OS_MACOSX) |
| 105 settings.renderer_settings.partial_swap_enabled = | 101 settings.renderer_settings.partial_swap_enabled = |
| 106 !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap); | 102 !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap); |
| 107 #endif | 103 #endif |
| 108 #if defined(OS_CHROMEOS) | 104 #if defined(OS_CHROMEOS) |
| 109 settings.per_tile_painting_enabled = true; | 105 settings.per_tile_painting_enabled = true; |
| 110 #endif | 106 #endif |
| 111 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
| 112 settings.disable_hi_res_timer_tasks_on_battery = true; | 108 settings.disable_hi_res_timer_tasks_on_battery = |
| 109 !context_factory_->DoesCreateTestContexts(); |
| 113 settings.renderer_settings.finish_rendering_on_resize = true; | 110 settings.renderer_settings.finish_rendering_on_resize = true; |
| 114 #endif | 111 #endif |
| 115 | 112 |
| 116 // These flags should be mirrored by renderer versions in content/renderer/. | 113 // These flags should be mirrored by renderer versions in content/renderer/. |
| 117 settings.initial_debug_state.show_debug_borders = | 114 settings.initial_debug_state.show_debug_borders = |
| 118 command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders); | 115 command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders); |
| 119 settings.initial_debug_state.show_fps_counter = | 116 settings.initial_debug_state.show_fps_counter = |
| 120 command_line->HasSwitch(cc::switches::kUIShowFPSCounter); | 117 command_line->HasSwitch(cc::switches::kUIShowFPSCounter); |
| 121 settings.initial_debug_state.show_layer_animation_bounds_rects = | 118 settings.initial_debug_state.show_layer_animation_bounds_rects = |
| 122 command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds); | 119 command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 133 settings.initial_debug_state.show_occluding_rects = | 130 settings.initial_debug_state.show_occluding_rects = |
| 134 command_line->HasSwitch(cc::switches::kUIShowOccludingRects); | 131 command_line->HasSwitch(cc::switches::kUIShowOccludingRects); |
| 135 settings.initial_debug_state.show_non_occluding_rects = | 132 settings.initial_debug_state.show_non_occluding_rects = |
| 136 command_line->HasSwitch(cc::switches::kUIShowNonOccludingRects); | 133 command_line->HasSwitch(cc::switches::kUIShowNonOccludingRects); |
| 137 | 134 |
| 138 settings.initial_debug_state.SetRecordRenderingStats( | 135 settings.initial_debug_state.SetRecordRenderingStats( |
| 139 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); | 136 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); |
| 140 | 137 |
| 141 settings.impl_side_painting = IsUIImplSidePaintingEnabled(); | 138 settings.impl_side_painting = IsUIImplSidePaintingEnabled(); |
| 142 settings.use_zero_copy = IsUIZeroCopyEnabled(); | 139 settings.use_zero_copy = IsUIZeroCopyEnabled(); |
| 143 settings.single_thread_proxy_scheduler = false; | |
| 144 | 140 |
| 145 base::TimeTicks before_create = base::TimeTicks::Now(); | 141 base::TimeTicks before_create = base::TimeTicks::Now(); |
| 146 if (compositor_thread_loop_.get()) { | 142 if (compositor_thread_loop_.get()) { |
| 147 host_ = cc::LayerTreeHost::CreateThreaded( | 143 host_ = cc::LayerTreeHost::CreateThreaded( |
| 148 this, | 144 this, |
| 149 context_factory_->GetSharedBitmapManager(), | 145 context_factory_->GetSharedBitmapManager(), |
| 150 context_factory_->GetGpuMemoryBufferManager(), | 146 context_factory_->GetGpuMemoryBufferManager(), |
| 151 settings, | 147 settings, |
| 152 task_runner_, | 148 task_runner_, |
| 153 compositor_thread_loop_, | 149 compositor_thread_loop_, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 183 |
| 188 context_factory_->RemoveCompositor(this); | 184 context_factory_->RemoveCompositor(this); |
| 189 } | 185 } |
| 190 | 186 |
| 191 void Compositor::SetOutputSurface( | 187 void Compositor::SetOutputSurface( |
| 192 scoped_ptr<cc::OutputSurface> output_surface) { | 188 scoped_ptr<cc::OutputSurface> output_surface) { |
| 193 host_->SetOutputSurface(output_surface.Pass()); | 189 host_->SetOutputSurface(output_surface.Pass()); |
| 194 } | 190 } |
| 195 | 191 |
| 196 void Compositor::ScheduleDraw() { | 192 void Compositor::ScheduleDraw() { |
| 197 if (compositor_thread_loop_.get()) { | 193 host_->SetNeedsCommit(); |
| 198 host_->SetNeedsCommit(); | |
| 199 } else if (!defer_draw_scheduling_) { | |
| 200 defer_draw_scheduling_ = true; | |
| 201 task_runner_->PostTask( | |
| 202 FROM_HERE, | |
| 203 base::Bind(&Compositor::Draw, weak_ptr_factory_.GetWeakPtr())); | |
| 204 } | |
| 205 } | 194 } |
| 206 | 195 |
| 207 void Compositor::SetRootLayer(Layer* root_layer) { | 196 void Compositor::SetRootLayer(Layer* root_layer) { |
| 208 if (root_layer_ == root_layer) | 197 if (root_layer_ == root_layer) |
| 209 return; | 198 return; |
| 210 if (root_layer_) | 199 if (root_layer_) |
| 211 root_layer_->SetCompositor(NULL); | 200 root_layer_->SetCompositor(NULL); |
| 212 root_layer_ = root_layer; | 201 root_layer_ = root_layer; |
| 213 if (root_layer_ && !root_layer_->GetCompositor()) | 202 if (root_layer_ && !root_layer_->GetCompositor()) |
| 214 root_layer_->SetCompositor(this); | 203 root_layer_->SetCompositor(this); |
| 215 root_web_layer_->RemoveAllChildren(); | 204 root_web_layer_->RemoveAllChildren(); |
| 216 if (root_layer_) | 205 if (root_layer_) |
| 217 root_web_layer_->AddChild(root_layer_->cc_layer()); | 206 root_web_layer_->AddChild(root_layer_->cc_layer()); |
| 218 } | 207 } |
| 219 | 208 |
| 220 void Compositor::SetHostHasTransparentBackground( | 209 void Compositor::SetHostHasTransparentBackground( |
| 221 bool host_has_transparent_background) { | 210 bool host_has_transparent_background) { |
| 222 host_->set_has_transparent_background(host_has_transparent_background); | 211 host_->set_has_transparent_background(host_has_transparent_background); |
| 223 } | 212 } |
| 224 | 213 |
| 225 void Compositor::Draw() { | |
| 226 DCHECK(!compositor_thread_loop_.get()); | |
| 227 | |
| 228 defer_draw_scheduling_ = false; | |
| 229 if (waiting_on_compositing_end_) { | |
| 230 draw_on_compositing_end_ = true; | |
| 231 return; | |
| 232 } | |
| 233 if (!root_layer_) | |
| 234 return; | |
| 235 | |
| 236 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1); | |
| 237 | |
| 238 DCHECK_NE(swap_state_, SWAP_POSTED); | |
| 239 swap_state_ = SWAP_NONE; | |
| 240 | |
| 241 waiting_on_compositing_end_ = true; | |
| 242 last_started_frame_++; | |
| 243 if (!IsLocked()) { | |
| 244 // TODO(nduca): Temporary while compositor calls | |
| 245 // compositeImmediately() directly. | |
| 246 cc::BeginFrameArgs args = cc::BeginFrameArgs::Create( | |
| 247 BEGINFRAME_FROM_HERE, gfx::FrameTime::Now(), base::TimeTicks(), | |
| 248 cc::BeginFrameArgs::DefaultInterval(), cc::BeginFrameArgs::SYNCHRONOUS); | |
| 249 BeginMainFrame(args); | |
| 250 host_->Composite(args.frame_time); | |
| 251 } | |
| 252 if (swap_state_ == SWAP_NONE) | |
| 253 NotifyEnd(); | |
| 254 } | |
| 255 | |
| 256 void Compositor::ScheduleFullRedraw() { | 214 void Compositor::ScheduleFullRedraw() { |
| 215 // TODO(enne): Some callers (mac) call this function expecting that it |
| 216 // will also commit. This should probably just redraw the screen |
| 217 // from damage and not commit. ScheduleDraw/ScheduleRedraw need |
| 218 // better names. |
| 257 host_->SetNeedsRedraw(); | 219 host_->SetNeedsRedraw(); |
| 220 host_->SetNeedsCommit(); |
| 258 } | 221 } |
| 259 | 222 |
| 260 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 223 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 224 // TODO(enne): Make this not commit. See ScheduleFullRedraw. |
| 261 host_->SetNeedsRedrawRect(damage_rect); | 225 host_->SetNeedsRedrawRect(damage_rect); |
| 226 host_->SetNeedsCommit(); |
| 262 } | 227 } |
| 263 | 228 |
| 264 void Compositor::DisableSwapUntilResize() { | 229 void Compositor::DisableSwapUntilResize() { |
| 265 host_->FinishAllRendering(); | 230 host_->FinishAllRendering(); |
| 266 context_factory_->ResizeDisplay(this, gfx::Size()); | 231 context_factory_->ResizeDisplay(this, gfx::Size()); |
| 267 } | 232 } |
| 268 | 233 |
| 269 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { | 234 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { |
| 270 scoped_ptr<cc::SwapPromise> swap_promise( | 235 scoped_ptr<cc::SwapPromise> swap_promise( |
| 271 new cc::LatencyInfoSwapPromise(latency_info)); | 236 new cc::LatencyInfoSwapPromise(latency_info)); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 } | 335 } |
| 371 | 336 |
| 372 void Compositor::DidCommit() { | 337 void Compositor::DidCommit() { |
| 373 DCHECK(!IsLocked()); | 338 DCHECK(!IsLocked()); |
| 374 FOR_EACH_OBSERVER(CompositorObserver, | 339 FOR_EACH_OBSERVER(CompositorObserver, |
| 375 observer_list_, | 340 observer_list_, |
| 376 OnCompositingDidCommit(this)); | 341 OnCompositingDidCommit(this)); |
| 377 } | 342 } |
| 378 | 343 |
| 379 void Compositor::DidCommitAndDrawFrame() { | 344 void Compositor::DidCommitAndDrawFrame() { |
| 345 } |
| 346 |
| 347 void Compositor::DidCompleteSwapBuffers() { |
| 348 // DidPostSwapBuffers is a SingleThreadProxy-only feature. Synthetically |
| 349 // generate OnCompositingStarted messages for the threaded case so that |
| 350 // OnCompositingStarted/OnCompositingEnded messages match. |
| 351 if (compositor_thread_loop_.get()) { |
| 352 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 353 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 354 OnCompositingStarted(this, start_time)); |
| 355 } |
| 356 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 357 OnCompositingEnded(this)); |
| 358 } |
| 359 |
| 360 void Compositor::DidPostSwapBuffers() { |
| 380 base::TimeTicks start_time = gfx::FrameTime::Now(); | 361 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 381 FOR_EACH_OBSERVER(CompositorObserver, | 362 FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 382 observer_list_, | |
| 383 OnCompositingStarted(this, start_time)); | 363 OnCompositingStarted(this, start_time)); |
| 384 } | 364 } |
| 385 | 365 |
| 386 void Compositor::DidCompleteSwapBuffers() { | |
| 387 if (compositor_thread_loop_.get()) { | |
| 388 NotifyEnd(); | |
| 389 } else { | |
| 390 DCHECK_EQ(swap_state_, SWAP_POSTED); | |
| 391 NotifyEnd(); | |
| 392 swap_state_ = SWAP_COMPLETED; | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 void Compositor::ScheduleComposite() { | |
| 397 if (!disable_schedule_composite_) | |
| 398 ScheduleDraw(); | |
| 399 } | |
| 400 | |
| 401 void Compositor::ScheduleAnimation() { | |
| 402 ScheduleComposite(); | |
| 403 } | |
| 404 | |
| 405 void Compositor::DidPostSwapBuffers() { | |
| 406 DCHECK(!compositor_thread_loop_.get()); | |
| 407 DCHECK_EQ(swap_state_, SWAP_NONE); | |
| 408 swap_state_ = SWAP_POSTED; | |
| 409 } | |
| 410 | |
| 411 void Compositor::DidAbortSwapBuffers() { | 366 void Compositor::DidAbortSwapBuffers() { |
| 412 if (!compositor_thread_loop_.get()) { | |
| 413 if (swap_state_ == SWAP_POSTED) { | |
| 414 NotifyEnd(); | |
| 415 swap_state_ = SWAP_COMPLETED; | |
| 416 } | |
| 417 } | |
| 418 | |
| 419 FOR_EACH_OBSERVER(CompositorObserver, | 367 FOR_EACH_OBSERVER(CompositorObserver, |
| 420 observer_list_, | 368 observer_list_, |
| 421 OnCompositingAborted(this)); | 369 OnCompositingAborted(this)); |
| 422 } | 370 } |
| 423 | 371 |
| 424 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 372 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 425 return host_->debug_state(); | 373 return host_->debug_state(); |
| 426 } | 374 } |
| 427 | 375 |
| 428 void Compositor::SetLayerTreeDebugState( | 376 void Compositor::SetLayerTreeDebugState( |
| 429 const cc::LayerTreeDebugState& debug_state) { | 377 const cc::LayerTreeDebugState& debug_state) { |
| 430 host_->SetDebugState(debug_state); | 378 host_->SetDebugState(debug_state); |
| 431 } | 379 } |
| 432 | 380 |
| 433 const cc::RendererSettings& Compositor::GetRendererSettings() const { | 381 const cc::RendererSettings& Compositor::GetRendererSettings() const { |
| 434 return host_->settings().renderer_settings; | 382 return host_->settings().renderer_settings; |
| 435 } | 383 } |
| 436 | 384 |
| 437 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 385 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 438 if (!compositor_lock_) { | 386 if (!compositor_lock_) { |
| 439 compositor_lock_ = new CompositorLock(this); | 387 compositor_lock_ = new CompositorLock(this); |
| 440 if (compositor_thread_loop_.get()) | 388 host_->SetDeferCommits(true); |
| 441 host_->SetDeferCommits(true); | |
| 442 FOR_EACH_OBSERVER(CompositorObserver, | 389 FOR_EACH_OBSERVER(CompositorObserver, |
| 443 observer_list_, | 390 observer_list_, |
| 444 OnCompositingLockStateChanged(this)); | 391 OnCompositingLockStateChanged(this)); |
| 445 } | 392 } |
| 446 return compositor_lock_; | 393 return compositor_lock_; |
| 447 } | 394 } |
| 448 | 395 |
| 449 void Compositor::UnlockCompositor() { | 396 void Compositor::UnlockCompositor() { |
| 450 DCHECK(compositor_lock_); | 397 DCHECK(compositor_lock_); |
| 451 compositor_lock_ = NULL; | 398 compositor_lock_ = NULL; |
| 452 if (compositor_thread_loop_.get()) | 399 host_->SetDeferCommits(false); |
| 453 host_->SetDeferCommits(false); | |
| 454 FOR_EACH_OBSERVER(CompositorObserver, | 400 FOR_EACH_OBSERVER(CompositorObserver, |
| 455 observer_list_, | 401 observer_list_, |
| 456 OnCompositingLockStateChanged(this)); | 402 OnCompositingLockStateChanged(this)); |
| 457 } | 403 } |
| 458 | 404 |
| 459 void Compositor::CancelCompositorLock() { | 405 void Compositor::CancelCompositorLock() { |
| 460 if (compositor_lock_) | 406 if (compositor_lock_) |
| 461 compositor_lock_->CancelLock(); | 407 compositor_lock_->CancelLock(); |
| 462 } | 408 } |
| 463 | 409 |
| 464 void Compositor::NotifyEnd() { | |
| 465 last_ended_frame_++; | |
| 466 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); | |
| 467 waiting_on_compositing_end_ = false; | |
| 468 if (draw_on_compositing_end_) { | |
| 469 draw_on_compositing_end_ = false; | |
| 470 | |
| 471 // Call ScheduleDraw() instead of Draw() in order to allow other | |
| 472 // CompositorObservers to be notified before starting another | |
| 473 // draw cycle. | |
| 474 ScheduleDraw(); | |
| 475 } | |
| 476 FOR_EACH_OBSERVER( | |
| 477 CompositorObserver, observer_list_, OnCompositingEnded(this)); | |
| 478 } | |
| 479 | |
| 480 } // namespace ui | 410 } // namespace ui |
| OLD | NEW |