| 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 : context_factory_(context_factory), | 66 : context_factory_(context_factory), |
| 75 root_layer_(NULL), | 67 root_layer_(NULL), |
| 76 widget_(widget), | 68 widget_(widget), |
| 77 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), | 69 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), |
| 78 vsync_manager_(new CompositorVSyncManager()), | 70 vsync_manager_(new CompositorVSyncManager()), |
| 79 device_scale_factor_(0.0f), | 71 device_scale_factor_(0.0f), |
| 80 last_started_frame_(0), | 72 last_started_frame_(0), |
| 81 last_ended_frame_(0), | 73 last_ended_frame_(0), |
| 82 disable_schedule_composite_(false), | 74 disable_schedule_composite_(false), |
| 83 compositor_lock_(NULL), | 75 compositor_lock_(NULL), |
| 84 defer_draw_scheduling_(false), | |
| 85 waiting_on_compositing_end_(false), | |
| 86 draw_on_compositing_end_(false), | |
| 87 swap_state_(SWAP_NONE), | |
| 88 layer_animator_collection_(this), | 76 layer_animator_collection_(this), |
| 89 schedule_draw_factory_(this) { | 77 schedule_draw_factory_(this) { |
| 90 root_web_layer_ = cc::Layer::Create(); | 78 root_web_layer_ = cc::Layer::Create(); |
| 91 | 79 |
| 92 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 80 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 93 | 81 |
| 94 cc::LayerTreeSettings settings; | 82 cc::LayerTreeSettings settings; |
| 95 settings.refresh_rate = | 83 settings.refresh_rate = |
| 96 context_factory_->DoesCreateTestContexts() | 84 context_factory_->DoesCreateTestContexts() |
| 97 ? kTestRefreshRate | 85 ? kTestRefreshRate |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 root_layer_->SetCompositor(NULL); | 151 root_layer_->SetCompositor(NULL); |
| 164 | 152 |
| 165 // Stop all outstanding draws before telling the ContextFactory to tear | 153 // Stop all outstanding draws before telling the ContextFactory to tear |
| 166 // down any contexts that the |host_| may rely upon. | 154 // down any contexts that the |host_| may rely upon. |
| 167 host_.reset(); | 155 host_.reset(); |
| 168 | 156 |
| 169 context_factory_->RemoveCompositor(this); | 157 context_factory_->RemoveCompositor(this); |
| 170 } | 158 } |
| 171 | 159 |
| 172 void Compositor::ScheduleDraw() { | 160 void Compositor::ScheduleDraw() { |
| 173 if (compositor_thread_loop_) { | 161 host_->SetNeedsCommit(); |
| 174 host_->SetNeedsCommit(); | |
| 175 } else if (!defer_draw_scheduling_) { | |
| 176 defer_draw_scheduling_ = true; | |
| 177 base::MessageLoop::current()->PostTask( | |
| 178 FROM_HERE, | |
| 179 base::Bind(&Compositor::Draw, schedule_draw_factory_.GetWeakPtr())); | |
| 180 } | |
| 181 } | 162 } |
| 182 | 163 |
| 183 void Compositor::SetRootLayer(Layer* root_layer) { | 164 void Compositor::SetRootLayer(Layer* root_layer) { |
| 184 if (root_layer_ == root_layer) | 165 if (root_layer_ == root_layer) |
| 185 return; | 166 return; |
| 186 if (root_layer_) | 167 if (root_layer_) |
| 187 root_layer_->SetCompositor(NULL); | 168 root_layer_->SetCompositor(NULL); |
| 188 root_layer_ = root_layer; | 169 root_layer_ = root_layer; |
| 189 if (root_layer_ && !root_layer_->GetCompositor()) | 170 if (root_layer_ && !root_layer_->GetCompositor()) |
| 190 root_layer_->SetCompositor(this); | 171 root_layer_->SetCompositor(this); |
| 191 root_web_layer_->RemoveAllChildren(); | 172 root_web_layer_->RemoveAllChildren(); |
| 192 if (root_layer_) | 173 if (root_layer_) |
| 193 root_web_layer_->AddChild(root_layer_->cc_layer()); | 174 root_web_layer_->AddChild(root_layer_->cc_layer()); |
| 194 } | 175 } |
| 195 | 176 |
| 196 void Compositor::SetHostHasTransparentBackground( | 177 void Compositor::SetHostHasTransparentBackground( |
| 197 bool host_has_transparent_background) { | 178 bool host_has_transparent_background) { |
| 198 host_->set_has_transparent_background(host_has_transparent_background); | 179 host_->set_has_transparent_background(host_has_transparent_background); |
| 199 } | 180 } |
| 200 | 181 |
| 201 void Compositor::Draw() { | |
| 202 DCHECK(!compositor_thread_loop_); | |
| 203 | |
| 204 defer_draw_scheduling_ = false; | |
| 205 if (waiting_on_compositing_end_) { | |
| 206 draw_on_compositing_end_ = true; | |
| 207 return; | |
| 208 } | |
| 209 waiting_on_compositing_end_ = true; | |
| 210 | |
| 211 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1); | |
| 212 | |
| 213 if (!root_layer_) | |
| 214 return; | |
| 215 | |
| 216 DCHECK_NE(swap_state_, SWAP_POSTED); | |
| 217 swap_state_ = SWAP_NONE; | |
| 218 | |
| 219 last_started_frame_++; | |
| 220 if (!IsLocked()) { | |
| 221 // TODO(nduca): Temporary while compositor calls | |
| 222 // compositeImmediately() directly. | |
| 223 base::TimeTicks now = gfx::FrameTime::Now(); | |
| 224 Animate(now); | |
| 225 Layout(); | |
| 226 host_->Composite(now); | |
| 227 } | |
| 228 if (swap_state_ == SWAP_NONE) | |
| 229 NotifyEnd(); | |
| 230 } | |
| 231 | |
| 232 void Compositor::ScheduleFullRedraw() { | 182 void Compositor::ScheduleFullRedraw() { |
| 233 host_->SetNeedsRedraw(); | 183 host_->SetNeedsRedraw(); |
| 234 } | 184 } |
| 235 | 185 |
| 236 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 186 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 237 host_->SetNeedsRedrawRect(damage_rect); | 187 host_->SetNeedsRedrawRect(damage_rect); |
| 238 } | 188 } |
| 239 | 189 |
| 240 void Compositor::FinishAllRendering() { | 190 void Compositor::FinishAllRendering() { |
| 241 host_->FinishAllRendering(); | 191 host_->FinishAllRendering(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 260 } |
| 311 | 261 |
| 312 void Compositor::DidCommitAndDrawFrame() { | 262 void Compositor::DidCommitAndDrawFrame() { |
| 313 base::TimeTicks start_time = gfx::FrameTime::Now(); | 263 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 314 FOR_EACH_OBSERVER(CompositorObserver, | 264 FOR_EACH_OBSERVER(CompositorObserver, |
| 315 observer_list_, | 265 observer_list_, |
| 316 OnCompositingStarted(this, start_time)); | 266 OnCompositingStarted(this, start_time)); |
| 317 } | 267 } |
| 318 | 268 |
| 319 void Compositor::DidCompleteSwapBuffers() { | 269 void Compositor::DidCompleteSwapBuffers() { |
| 320 if (compositor_thread_loop_) { | 270 last_ended_frame_++; |
| 321 NotifyEnd(); | 271 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); |
| 322 } else { | 272 FOR_EACH_OBSERVER( |
| 323 DCHECK_EQ(swap_state_, SWAP_POSTED); | 273 CompositorObserver, observer_list_, OnCompositingEnded(this)); |
| 324 NotifyEnd(); | |
| 325 swap_state_ = SWAP_COMPLETED; | |
| 326 } | |
| 327 } | 274 } |
| 328 | 275 |
| 329 void Compositor::ScheduleComposite() { | 276 void Compositor::ScheduleComposite() { |
| 330 if (!disable_schedule_composite_) | 277 if (!disable_schedule_composite_) |
| 331 ScheduleDraw(); | 278 ScheduleDraw(); |
| 332 } | 279 } |
| 333 | 280 |
| 334 void Compositor::ScheduleAnimation() { | 281 void Compositor::ScheduleAnimation() { |
| 335 ScheduleComposite(); | 282 ScheduleComposite(); |
| 336 } | 283 } |
| 337 | 284 |
| 338 void Compositor::DidPostSwapBuffers() { | 285 void Compositor::DidPostSwapBuffers() { |
| 339 DCHECK(!compositor_thread_loop_); | |
| 340 DCHECK_EQ(swap_state_, SWAP_NONE); | |
| 341 swap_state_ = SWAP_POSTED; | |
| 342 } | 286 } |
| 343 | 287 |
| 344 void Compositor::DidAbortSwapBuffers() { | 288 void Compositor::DidAbortSwapBuffers() { |
| 345 if (!compositor_thread_loop_) { | |
| 346 if (swap_state_ == SWAP_POSTED) { | |
| 347 NotifyEnd(); | |
| 348 swap_state_ = SWAP_COMPLETED; | |
| 349 } | |
| 350 } | |
| 351 | |
| 352 FOR_EACH_OBSERVER(CompositorObserver, | 289 FOR_EACH_OBSERVER(CompositorObserver, |
| 353 observer_list_, | 290 observer_list_, |
| 354 OnCompositingAborted(this)); | 291 OnCompositingAborted(this)); |
| 355 } | 292 } |
| 356 | 293 |
| 357 void Compositor::ScheduleAnimationForLayerCollection() { | 294 void Compositor::ScheduleAnimationForLayerCollection() { |
| 358 host_->SetNeedsAnimate(); | 295 host_->SetNeedsAnimate(); |
| 359 } | 296 } |
| 360 | 297 |
| 361 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 298 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 362 return host_->debug_state(); | 299 return host_->debug_state(); |
| 363 } | 300 } |
| 364 | 301 |
| 365 void Compositor::SetLayerTreeDebugState( | 302 void Compositor::SetLayerTreeDebugState( |
| 366 const cc::LayerTreeDebugState& debug_state) { | 303 const cc::LayerTreeDebugState& debug_state) { |
| 367 host_->SetDebugState(debug_state); | 304 host_->SetDebugState(debug_state); |
| 368 } | 305 } |
| 369 | 306 |
| 370 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 307 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 371 if (!compositor_lock_) { | 308 if (!compositor_lock_) { |
| 372 compositor_lock_ = new CompositorLock(this); | 309 compositor_lock_ = new CompositorLock(this); |
| 373 if (compositor_thread_loop_) | 310 host_->SetDeferCommits(true); |
| 374 host_->SetDeferCommits(true); | |
| 375 FOR_EACH_OBSERVER(CompositorObserver, | 311 FOR_EACH_OBSERVER(CompositorObserver, |
| 376 observer_list_, | 312 observer_list_, |
| 377 OnCompositingLockStateChanged(this)); | 313 OnCompositingLockStateChanged(this)); |
| 378 } | 314 } |
| 379 return compositor_lock_; | 315 return compositor_lock_; |
| 380 } | 316 } |
| 381 | 317 |
| 382 void Compositor::UnlockCompositor() { | 318 void Compositor::UnlockCompositor() { |
| 383 DCHECK(compositor_lock_); | 319 DCHECK(compositor_lock_); |
| 384 compositor_lock_ = NULL; | 320 compositor_lock_ = NULL; |
| 385 if (compositor_thread_loop_) | 321 host_->SetDeferCommits(false); |
| 386 host_->SetDeferCommits(false); | |
| 387 FOR_EACH_OBSERVER(CompositorObserver, | 322 FOR_EACH_OBSERVER(CompositorObserver, |
| 388 observer_list_, | 323 observer_list_, |
| 389 OnCompositingLockStateChanged(this)); | 324 OnCompositingLockStateChanged(this)); |
| 390 } | 325 } |
| 391 | 326 |
| 392 void Compositor::CancelCompositorLock() { | 327 void Compositor::CancelCompositorLock() { |
| 393 if (compositor_lock_) | 328 if (compositor_lock_) |
| 394 compositor_lock_->CancelLock(); | 329 compositor_lock_->CancelLock(); |
| 395 } | 330 } |
| 396 | 331 |
| 397 void Compositor::NotifyEnd() { | |
| 398 last_ended_frame_++; | |
| 399 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); | |
| 400 waiting_on_compositing_end_ = false; | |
| 401 if (draw_on_compositing_end_) { | |
| 402 draw_on_compositing_end_ = false; | |
| 403 | |
| 404 // Call ScheduleDraw() instead of Draw() in order to allow other | |
| 405 // CompositorObservers to be notified before starting another | |
| 406 // draw cycle. | |
| 407 ScheduleDraw(); | |
| 408 } | |
| 409 FOR_EACH_OBSERVER(CompositorObserver, | |
| 410 observer_list_, | |
| 411 OnCompositingEnded(this)); | |
| 412 } | |
| 413 | |
| 414 } // namespace ui | 332 } // namespace ui |
| OLD | NEW |