| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 CancelLock(); | 54 CancelLock(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void CompositorLock::CancelLock() { | 57 void CompositorLock::CancelLock() { |
| 58 if (!compositor_) | 58 if (!compositor_) |
| 59 return; | 59 return; |
| 60 compositor_->UnlockCompositor(); | 60 compositor_->UnlockCompositor(); |
| 61 compositor_ = NULL; | 61 compositor_ = NULL; |
| 62 } | 62 } |
| 63 | 63 |
| 64 } // namespace ui | |
| 65 | |
| 66 namespace { | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 namespace ui { | |
| 71 | |
| 72 Compositor::Compositor(gfx::AcceleratedWidget widget, | 64 Compositor::Compositor(gfx::AcceleratedWidget widget, |
| 73 ui::ContextFactory* context_factory, | 65 ui::ContextFactory* context_factory, |
| 74 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 66 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 75 : context_factory_(context_factory), | 67 : context_factory_(context_factory), |
| 76 root_layer_(NULL), | 68 root_layer_(NULL), |
| 77 widget_(widget), | 69 widget_(widget), |
| 78 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), | 70 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), |
| 79 task_runner_(task_runner), | 71 task_runner_(task_runner), |
| 80 vsync_manager_(new CompositorVSyncManager()), | 72 vsync_manager_(new CompositorVSyncManager()), |
| 81 device_scale_factor_(0.0f), | 73 device_scale_factor_(0.0f), |
| 82 last_started_frame_(0), | |
| 83 last_ended_frame_(0), | |
| 84 disable_schedule_composite_(false), | 74 disable_schedule_composite_(false), |
| 85 compositor_lock_(NULL), | 75 compositor_lock_(NULL), |
| 86 defer_draw_scheduling_(false), | 76 layer_animator_collection_(this) { |
| 87 waiting_on_compositing_end_(false), | |
| 88 draw_on_compositing_end_(false), | |
| 89 swap_state_(SWAP_NONE), | |
| 90 layer_animator_collection_(this), | |
| 91 schedule_draw_factory_(this) { | |
| 92 root_web_layer_ = cc::Layer::Create(); | 77 root_web_layer_ = cc::Layer::Create(); |
| 93 | 78 |
| 94 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 79 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 95 | 80 |
| 96 cc::LayerTreeSettings settings; | 81 cc::LayerTreeSettings settings; |
| 97 settings.refresh_rate = | 82 settings.refresh_rate = |
| 98 context_factory_->DoesCreateTestContexts() | 83 context_factory_->DoesCreateTestContexts() |
| 99 ? kTestRefreshRate | 84 ? kTestRefreshRate |
| 100 : kDefaultRefreshRate; | 85 : kDefaultRefreshRate; |
| 101 settings.main_frame_before_draw_enabled = false; | 86 settings.main_frame_before_draw_enabled = false; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 root_layer_->SetCompositor(NULL); | 155 root_layer_->SetCompositor(NULL); |
| 171 | 156 |
| 172 // Stop all outstanding draws before telling the ContextFactory to tear | 157 // Stop all outstanding draws before telling the ContextFactory to tear |
| 173 // down any contexts that the |host_| may rely upon. | 158 // down any contexts that the |host_| may rely upon. |
| 174 host_.reset(); | 159 host_.reset(); |
| 175 | 160 |
| 176 context_factory_->RemoveCompositor(this); | 161 context_factory_->RemoveCompositor(this); |
| 177 } | 162 } |
| 178 | 163 |
| 179 void Compositor::ScheduleDraw() { | 164 void Compositor::ScheduleDraw() { |
| 180 if (compositor_thread_loop_) { | 165 host_->SetNeedsCommit(); |
| 181 host_->SetNeedsCommit(); | |
| 182 } else if (!defer_draw_scheduling_) { | |
| 183 defer_draw_scheduling_ = true; | |
| 184 task_runner_->PostTask( | |
| 185 FROM_HERE, | |
| 186 base::Bind(&Compositor::Draw, schedule_draw_factory_.GetWeakPtr())); | |
| 187 } | |
| 188 } | 166 } |
| 189 | 167 |
| 190 void Compositor::SetRootLayer(Layer* root_layer) { | 168 void Compositor::SetRootLayer(Layer* root_layer) { |
| 191 if (root_layer_ == root_layer) | 169 if (root_layer_ == root_layer) |
| 192 return; | 170 return; |
| 193 if (root_layer_) | 171 if (root_layer_) |
| 194 root_layer_->SetCompositor(NULL); | 172 root_layer_->SetCompositor(NULL); |
| 195 root_layer_ = root_layer; | 173 root_layer_ = root_layer; |
| 196 if (root_layer_ && !root_layer_->GetCompositor()) | 174 if (root_layer_ && !root_layer_->GetCompositor()) |
| 197 root_layer_->SetCompositor(this); | 175 root_layer_->SetCompositor(this); |
| 198 root_web_layer_->RemoveAllChildren(); | 176 root_web_layer_->RemoveAllChildren(); |
| 199 if (root_layer_) | 177 if (root_layer_) |
| 200 root_web_layer_->AddChild(root_layer_->cc_layer()); | 178 root_web_layer_->AddChild(root_layer_->cc_layer()); |
| 201 } | 179 } |
| 202 | 180 |
| 203 void Compositor::SetHostHasTransparentBackground( | 181 void Compositor::SetHostHasTransparentBackground( |
| 204 bool host_has_transparent_background) { | 182 bool host_has_transparent_background) { |
| 205 host_->set_has_transparent_background(host_has_transparent_background); | 183 host_->set_has_transparent_background(host_has_transparent_background); |
| 206 } | 184 } |
| 207 | 185 |
| 208 void Compositor::Draw() { | |
| 209 DCHECK(!compositor_thread_loop_); | |
| 210 | |
| 211 defer_draw_scheduling_ = false; | |
| 212 if (waiting_on_compositing_end_) { | |
| 213 draw_on_compositing_end_ = true; | |
| 214 return; | |
| 215 } | |
| 216 if (!root_layer_) | |
| 217 return; | |
| 218 | |
| 219 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1); | |
| 220 | |
| 221 DCHECK_NE(swap_state_, SWAP_POSTED); | |
| 222 swap_state_ = SWAP_NONE; | |
| 223 | |
| 224 waiting_on_compositing_end_ = true; | |
| 225 last_started_frame_++; | |
| 226 if (!IsLocked()) { | |
| 227 // TODO(nduca): Temporary while compositor calls | |
| 228 // compositeImmediately() directly. | |
| 229 base::TimeTicks now = gfx::FrameTime::Now(); | |
| 230 Animate(now); | |
| 231 Layout(); | |
| 232 host_->Composite(now); | |
| 233 } | |
| 234 if (swap_state_ == SWAP_NONE) | |
| 235 NotifyEnd(); | |
| 236 } | |
| 237 | |
| 238 void Compositor::ScheduleFullRedraw() { | 186 void Compositor::ScheduleFullRedraw() { |
| 187 // TODO(enne): Some callers (mac) call this function expecting that it |
| 188 // will also commit. This should probably just redraw the screen |
| 189 // from damage and not commit. ScheduleDraw/ScheduleRedraw need |
| 190 // better names. |
| 239 host_->SetNeedsRedraw(); | 191 host_->SetNeedsRedraw(); |
| 192 host_->SetNeedsCommit(); |
| 240 } | 193 } |
| 241 | 194 |
| 242 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 195 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 196 // TODO(enne): Make this not commit. See ScheduleFullRedraw. |
| 243 host_->SetNeedsRedrawRect(damage_rect); | 197 host_->SetNeedsRedrawRect(damage_rect); |
| 198 host_->SetNeedsCommit(); |
| 244 } | 199 } |
| 245 | 200 |
| 246 void Compositor::FinishAllRendering() { | 201 void Compositor::FinishAllRendering() { |
| 247 host_->FinishAllRendering(); | 202 host_->FinishAllRendering(); |
| 248 } | 203 } |
| 249 | 204 |
| 250 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { | 205 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { |
| 251 scoped_ptr<cc::SwapPromise> swap_promise( | 206 scoped_ptr<cc::SwapPromise> swap_promise( |
| 252 new cc::LatencyInfoSwapPromise(latency_info)); | 207 new cc::LatencyInfoSwapPromise(latency_info)); |
| 253 host_->QueueSwapPromise(swap_promise.Pass()); | 208 host_->QueueSwapPromise(swap_promise.Pass()); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 } | 295 } |
| 341 | 296 |
| 342 void Compositor::DidCommit() { | 297 void Compositor::DidCommit() { |
| 343 DCHECK(!IsLocked()); | 298 DCHECK(!IsLocked()); |
| 344 FOR_EACH_OBSERVER(CompositorObserver, | 299 FOR_EACH_OBSERVER(CompositorObserver, |
| 345 observer_list_, | 300 observer_list_, |
| 346 OnCompositingDidCommit(this)); | 301 OnCompositingDidCommit(this)); |
| 347 } | 302 } |
| 348 | 303 |
| 349 void Compositor::DidCommitAndDrawFrame() { | 304 void Compositor::DidCommitAndDrawFrame() { |
| 305 } |
| 306 |
| 307 void Compositor::DidCompleteSwapBuffers() { |
| 308 // DidPostSwapBuffers is a SingleThreadProxy-only feature. Synthetically |
| 309 // generate OnCompositingStarted messages for the threaded case so that |
| 310 // OnCompositingStarted/OnCompositingEnded messages match. |
| 311 if (compositor_thread_loop_) { |
| 312 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 313 FOR_EACH_OBSERVER(CompositorObserver, |
| 314 observer_list_, |
| 315 OnCompositingStarted(this, start_time)); |
| 316 } |
| 317 FOR_EACH_OBSERVER( |
| 318 CompositorObserver, observer_list_, OnCompositingEnded(this)); |
| 319 } |
| 320 |
| 321 void Compositor::DidPostSwapBuffers() { |
| 350 base::TimeTicks start_time = gfx::FrameTime::Now(); | 322 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 351 FOR_EACH_OBSERVER(CompositorObserver, | 323 FOR_EACH_OBSERVER(CompositorObserver, |
| 352 observer_list_, | 324 observer_list_, |
| 353 OnCompositingStarted(this, start_time)); | 325 OnCompositingStarted(this, start_time)); |
| 354 } | 326 } |
| 355 | 327 |
| 356 void Compositor::DidCompleteSwapBuffers() { | |
| 357 if (compositor_thread_loop_) { | |
| 358 NotifyEnd(); | |
| 359 } else { | |
| 360 DCHECK_EQ(swap_state_, SWAP_POSTED); | |
| 361 NotifyEnd(); | |
| 362 swap_state_ = SWAP_COMPLETED; | |
| 363 } | |
| 364 } | |
| 365 | |
| 366 void Compositor::ScheduleComposite() { | |
| 367 if (!disable_schedule_composite_) | |
| 368 ScheduleDraw(); | |
| 369 } | |
| 370 | |
| 371 void Compositor::ScheduleAnimation() { | |
| 372 ScheduleComposite(); | |
| 373 } | |
| 374 | |
| 375 void Compositor::DidPostSwapBuffers() { | |
| 376 DCHECK(!compositor_thread_loop_); | |
| 377 DCHECK_EQ(swap_state_, SWAP_NONE); | |
| 378 swap_state_ = SWAP_POSTED; | |
| 379 } | |
| 380 | |
| 381 void Compositor::DidAbortSwapBuffers() { | 328 void Compositor::DidAbortSwapBuffers() { |
| 382 if (!compositor_thread_loop_) { | |
| 383 if (swap_state_ == SWAP_POSTED) { | |
| 384 NotifyEnd(); | |
| 385 swap_state_ = SWAP_COMPLETED; | |
| 386 } | |
| 387 } | |
| 388 | |
| 389 FOR_EACH_OBSERVER(CompositorObserver, | 329 FOR_EACH_OBSERVER(CompositorObserver, |
| 390 observer_list_, | 330 observer_list_, |
| 391 OnCompositingAborted(this)); | 331 OnCompositingAborted(this)); |
| 392 } | 332 } |
| 393 | 333 |
| 394 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 334 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 395 return host_->debug_state(); | 335 return host_->debug_state(); |
| 396 } | 336 } |
| 397 | 337 |
| 398 void Compositor::SetLayerTreeDebugState( | 338 void Compositor::SetLayerTreeDebugState( |
| 399 const cc::LayerTreeDebugState& debug_state) { | 339 const cc::LayerTreeDebugState& debug_state) { |
| 400 host_->SetDebugState(debug_state); | 340 host_->SetDebugState(debug_state); |
| 401 } | 341 } |
| 402 | 342 |
| 403 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 343 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 404 if (!compositor_lock_) { | 344 if (!compositor_lock_) { |
| 405 compositor_lock_ = new CompositorLock(this); | 345 compositor_lock_ = new CompositorLock(this); |
| 406 if (compositor_thread_loop_) | 346 host_->SetDeferCommits(true); |
| 407 host_->SetDeferCommits(true); | |
| 408 FOR_EACH_OBSERVER(CompositorObserver, | 347 FOR_EACH_OBSERVER(CompositorObserver, |
| 409 observer_list_, | 348 observer_list_, |
| 410 OnCompositingLockStateChanged(this)); | 349 OnCompositingLockStateChanged(this)); |
| 411 } | 350 } |
| 412 return compositor_lock_; | 351 return compositor_lock_; |
| 413 } | 352 } |
| 414 | 353 |
| 415 void Compositor::UnlockCompositor() { | 354 void Compositor::UnlockCompositor() { |
| 416 DCHECK(compositor_lock_); | 355 DCHECK(compositor_lock_); |
| 417 compositor_lock_ = NULL; | 356 compositor_lock_ = NULL; |
| 418 if (compositor_thread_loop_) | 357 host_->SetDeferCommits(false); |
| 419 host_->SetDeferCommits(false); | |
| 420 FOR_EACH_OBSERVER(CompositorObserver, | 358 FOR_EACH_OBSERVER(CompositorObserver, |
| 421 observer_list_, | 359 observer_list_, |
| 422 OnCompositingLockStateChanged(this)); | 360 OnCompositingLockStateChanged(this)); |
| 423 } | 361 } |
| 424 | 362 |
| 425 void Compositor::CancelCompositorLock() { | 363 void Compositor::CancelCompositorLock() { |
| 426 if (compositor_lock_) | 364 if (compositor_lock_) |
| 427 compositor_lock_->CancelLock(); | 365 compositor_lock_->CancelLock(); |
| 428 } | 366 } |
| 429 | 367 |
| 430 void Compositor::NotifyEnd() { | |
| 431 last_ended_frame_++; | |
| 432 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); | |
| 433 waiting_on_compositing_end_ = false; | |
| 434 if (draw_on_compositing_end_) { | |
| 435 draw_on_compositing_end_ = false; | |
| 436 | |
| 437 // Call ScheduleDraw() instead of Draw() in order to allow other | |
| 438 // CompositorObservers to be notified before starting another | |
| 439 // draw cycle. | |
| 440 ScheduleDraw(); | |
| 441 } | |
| 442 FOR_EACH_OBSERVER(CompositorObserver, | |
| 443 observer_list_, | |
| 444 OnCompositingEnded(this)); | |
| 445 } | |
| 446 | |
| 447 } // namespace ui | 368 } // namespace ui |
| OLD | NEW |