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