| 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), | |
| 81 last_ended_frame_(0), | |
| 82 disable_schedule_composite_(false), | 72 disable_schedule_composite_(false), |
| 83 compositor_lock_(NULL), | 73 compositor_lock_(NULL), |
| 84 defer_draw_scheduling_(false), | 74 layer_animator_collection_(this) { |
| 85 waiting_on_compositing_end_(false), | |
| 86 draw_on_compositing_end_(false), | |
| 87 swap_state_(SWAP_NONE), | |
| 88 layer_animator_collection_(this), | |
| 89 schedule_draw_factory_(this) { | |
| 90 root_web_layer_ = cc::Layer::Create(); | 75 root_web_layer_ = cc::Layer::Create(); |
| 91 | 76 |
| 92 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 77 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 93 | 78 |
| 94 cc::LayerTreeSettings settings; | 79 cc::LayerTreeSettings settings; |
| 95 settings.refresh_rate = | 80 settings.refresh_rate = |
| 96 context_factory_->DoesCreateTestContexts() | 81 context_factory_->DoesCreateTestContexts() |
| 97 ? kTestRefreshRate | 82 ? kTestRefreshRate |
| 98 : kDefaultRefreshRate; | 83 : kDefaultRefreshRate; |
| 99 settings.main_frame_before_draw_enabled = false; | 84 settings.main_frame_before_draw_enabled = false; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 root_layer_->SetCompositor(NULL); | 148 root_layer_->SetCompositor(NULL); |
| 164 | 149 |
| 165 // Stop all outstanding draws before telling the ContextFactory to tear | 150 // Stop all outstanding draws before telling the ContextFactory to tear |
| 166 // down any contexts that the |host_| may rely upon. | 151 // down any contexts that the |host_| may rely upon. |
| 167 host_.reset(); | 152 host_.reset(); |
| 168 | 153 |
| 169 context_factory_->RemoveCompositor(this); | 154 context_factory_->RemoveCompositor(this); |
| 170 } | 155 } |
| 171 | 156 |
| 172 void Compositor::ScheduleDraw() { | 157 void Compositor::ScheduleDraw() { |
| 173 if (compositor_thread_loop_) { | 158 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 } | 159 } |
| 182 | 160 |
| 183 void Compositor::SetRootLayer(Layer* root_layer) { | 161 void Compositor::SetRootLayer(Layer* root_layer) { |
| 184 if (root_layer_ == root_layer) | 162 if (root_layer_ == root_layer) |
| 185 return; | 163 return; |
| 186 if (root_layer_) | 164 if (root_layer_) |
| 187 root_layer_->SetCompositor(NULL); | 165 root_layer_->SetCompositor(NULL); |
| 188 root_layer_ = root_layer; | 166 root_layer_ = root_layer; |
| 189 if (root_layer_ && !root_layer_->GetCompositor()) | 167 if (root_layer_ && !root_layer_->GetCompositor()) |
| 190 root_layer_->SetCompositor(this); | 168 root_layer_->SetCompositor(this); |
| 191 root_web_layer_->RemoveAllChildren(); | 169 root_web_layer_->RemoveAllChildren(); |
| 192 if (root_layer_) | 170 if (root_layer_) |
| 193 root_web_layer_->AddChild(root_layer_->cc_layer()); | 171 root_web_layer_->AddChild(root_layer_->cc_layer()); |
| 194 } | 172 } |
| 195 | 173 |
| 196 void Compositor::SetHostHasTransparentBackground( | 174 void Compositor::SetHostHasTransparentBackground( |
| 197 bool host_has_transparent_background) { | 175 bool host_has_transparent_background) { |
| 198 host_->set_has_transparent_background(host_has_transparent_background); | 176 host_->set_has_transparent_background(host_has_transparent_background); |
| 199 } | 177 } |
| 200 | 178 |
| 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() { | 179 void Compositor::ScheduleFullRedraw() { |
| 233 host_->SetNeedsRedraw(); | 180 host_->SetNeedsRedraw(); |
| 234 } | 181 } |
| 235 | 182 |
| 236 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 183 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 237 host_->SetNeedsRedrawRect(damage_rect); | 184 host_->SetNeedsRedrawRect(damage_rect); |
| 238 } | 185 } |
| 239 | 186 |
| 240 void Compositor::FinishAllRendering() { | 187 void Compositor::FinishAllRendering() { |
| 241 host_->FinishAllRendering(); | 188 host_->FinishAllRendering(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 257 } |
| 311 | 258 |
| 312 void Compositor::DidCommitAndDrawFrame() { | 259 void Compositor::DidCommitAndDrawFrame() { |
| 313 base::TimeTicks start_time = gfx::FrameTime::Now(); | 260 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 314 FOR_EACH_OBSERVER(CompositorObserver, | 261 FOR_EACH_OBSERVER(CompositorObserver, |
| 315 observer_list_, | 262 observer_list_, |
| 316 OnCompositingStarted(this, start_time)); | 263 OnCompositingStarted(this, start_time)); |
| 317 } | 264 } |
| 318 | 265 |
| 319 void Compositor::DidCompleteSwapBuffers() { | 266 void Compositor::DidCompleteSwapBuffers() { |
| 320 if (compositor_thread_loop_) { | 267 FOR_EACH_OBSERVER( |
| 321 NotifyEnd(); | 268 CompositorObserver, observer_list_, OnCompositingEnded(this)); |
| 322 } else { | |
| 323 DCHECK_EQ(swap_state_, SWAP_POSTED); | |
| 324 NotifyEnd(); | |
| 325 swap_state_ = SWAP_COMPLETED; | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 void Compositor::ScheduleComposite() { | |
| 330 if (!disable_schedule_composite_) | |
| 331 ScheduleDraw(); | |
| 332 } | |
| 333 | |
| 334 void Compositor::ScheduleAnimation() { | |
| 335 ScheduleComposite(); | |
| 336 } | 269 } |
| 337 | 270 |
| 338 void Compositor::DidPostSwapBuffers() { | 271 void Compositor::DidPostSwapBuffers() { |
| 339 DCHECK(!compositor_thread_loop_); | |
| 340 DCHECK_EQ(swap_state_, SWAP_NONE); | |
| 341 swap_state_ = SWAP_POSTED; | |
| 342 } | 272 } |
| 343 | 273 |
| 344 void Compositor::DidAbortSwapBuffers() { | 274 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, | 275 FOR_EACH_OBSERVER(CompositorObserver, |
| 353 observer_list_, | 276 observer_list_, |
| 354 OnCompositingAborted(this)); | 277 OnCompositingAborted(this)); |
| 355 } | 278 } |
| 356 | 279 |
| 357 void Compositor::ScheduleAnimationForLayerCollection() { | 280 void Compositor::ScheduleAnimationForLayerCollection() { |
| 358 host_->SetNeedsAnimate(); | 281 host_->SetNeedsAnimate(); |
| 359 } | 282 } |
| 360 | 283 |
| 361 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 284 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 362 return host_->debug_state(); | 285 return host_->debug_state(); |
| 363 } | 286 } |
| 364 | 287 |
| 365 void Compositor::SetLayerTreeDebugState( | 288 void Compositor::SetLayerTreeDebugState( |
| 366 const cc::LayerTreeDebugState& debug_state) { | 289 const cc::LayerTreeDebugState& debug_state) { |
| 367 host_->SetDebugState(debug_state); | 290 host_->SetDebugState(debug_state); |
| 368 } | 291 } |
| 369 | 292 |
| 370 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 293 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 371 if (!compositor_lock_) { | 294 if (!compositor_lock_) { |
| 372 compositor_lock_ = new CompositorLock(this); | 295 compositor_lock_ = new CompositorLock(this); |
| 373 if (compositor_thread_loop_) | 296 host_->SetDeferCommits(true); |
| 374 host_->SetDeferCommits(true); | |
| 375 FOR_EACH_OBSERVER(CompositorObserver, | 297 FOR_EACH_OBSERVER(CompositorObserver, |
| 376 observer_list_, | 298 observer_list_, |
| 377 OnCompositingLockStateChanged(this)); | 299 OnCompositingLockStateChanged(this)); |
| 378 } | 300 } |
| 379 return compositor_lock_; | 301 return compositor_lock_; |
| 380 } | 302 } |
| 381 | 303 |
| 382 void Compositor::UnlockCompositor() { | 304 void Compositor::UnlockCompositor() { |
| 383 DCHECK(compositor_lock_); | 305 DCHECK(compositor_lock_); |
| 384 compositor_lock_ = NULL; | 306 compositor_lock_ = NULL; |
| 385 if (compositor_thread_loop_) | 307 host_->SetDeferCommits(false); |
| 386 host_->SetDeferCommits(false); | |
| 387 FOR_EACH_OBSERVER(CompositorObserver, | 308 FOR_EACH_OBSERVER(CompositorObserver, |
| 388 observer_list_, | 309 observer_list_, |
| 389 OnCompositingLockStateChanged(this)); | 310 OnCompositingLockStateChanged(this)); |
| 390 } | 311 } |
| 391 | 312 |
| 392 void Compositor::CancelCompositorLock() { | 313 void Compositor::CancelCompositorLock() { |
| 393 if (compositor_lock_) | 314 if (compositor_lock_) |
| 394 compositor_lock_->CancelLock(); | 315 compositor_lock_->CancelLock(); |
| 395 } | 316 } |
| 396 | 317 |
| 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 | 318 } // namespace ui |
| OLD | NEW |