| 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 if (!root_layer_) | |
| 210 return; | |
| 211 | |
| 212 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1); | |
| 213 | |
| 214 DCHECK_NE(swap_state_, SWAP_POSTED); | |
| 215 swap_state_ = SWAP_NONE; | |
| 216 | |
| 217 waiting_on_compositing_end_ = true; | |
| 218 last_started_frame_++; | |
| 219 if (!IsLocked()) { | |
| 220 // TODO(nduca): Temporary while compositor calls | |
| 221 // compositeImmediately() directly. | |
| 222 base::TimeTicks now = gfx::FrameTime::Now(); | |
| 223 Animate(now); | |
| 224 Layout(); | |
| 225 host_->Composite(now); | |
| 226 } | |
| 227 if (swap_state_ == SWAP_NONE) | |
| 228 NotifyEnd(); | |
| 229 } | |
| 230 | |
| 231 void Compositor::ScheduleFullRedraw() { | 179 void Compositor::ScheduleFullRedraw() { |
| 232 host_->SetNeedsRedraw(); | 180 host_->SetNeedsRedraw(); |
| 233 } | 181 } |
| 234 | 182 |
| 235 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 183 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 236 host_->SetNeedsRedrawRect(damage_rect); | 184 host_->SetNeedsRedrawRect(damage_rect); |
| 237 } | 185 } |
| 238 | 186 |
| 239 void Compositor::FinishAllRendering() { | 187 void Compositor::FinishAllRendering() { |
| 240 host_->FinishAllRendering(); | 188 host_->FinishAllRendering(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 250 } |
| 303 | 251 |
| 304 void Compositor::DidCommit() { | 252 void Compositor::DidCommit() { |
| 305 DCHECK(!IsLocked()); | 253 DCHECK(!IsLocked()); |
| 306 FOR_EACH_OBSERVER(CompositorObserver, | 254 FOR_EACH_OBSERVER(CompositorObserver, |
| 307 observer_list_, | 255 observer_list_, |
| 308 OnCompositingDidCommit(this)); | 256 OnCompositingDidCommit(this)); |
| 309 } | 257 } |
| 310 | 258 |
| 311 void Compositor::DidCommitAndDrawFrame() { | 259 void Compositor::DidCommitAndDrawFrame() { |
| 260 } |
| 261 |
| 262 void Compositor::DidCompleteSwapBuffers() { |
| 263 // DidPostSwapBuffers is a SingleThreadProxy-only feature. Synthetically |
| 264 // generate OnCompositingStarted messages for the threaded case so that |
| 265 // OnCompositingStarted/OnCompositingEnded messages match. |
| 266 if (compositor_thread_loop_) { |
| 267 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 268 FOR_EACH_OBSERVER(CompositorObserver, |
| 269 observer_list_, |
| 270 OnCompositingStarted(this, start_time)); |
| 271 } |
| 272 FOR_EACH_OBSERVER( |
| 273 CompositorObserver, observer_list_, OnCompositingEnded(this)); |
| 274 } |
| 275 |
| 276 void Compositor::DidPostSwapBuffers() { |
| 312 base::TimeTicks start_time = gfx::FrameTime::Now(); | 277 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 313 FOR_EACH_OBSERVER(CompositorObserver, | 278 FOR_EACH_OBSERVER(CompositorObserver, |
| 314 observer_list_, | 279 observer_list_, |
| 315 OnCompositingStarted(this, start_time)); | 280 OnCompositingStarted(this, start_time)); |
| 316 } | 281 } |
| 317 | 282 |
| 318 void Compositor::DidCompleteSwapBuffers() { | |
| 319 if (compositor_thread_loop_) { | |
| 320 NotifyEnd(); | |
| 321 } else { | |
| 322 DCHECK_EQ(swap_state_, SWAP_POSTED); | |
| 323 NotifyEnd(); | |
| 324 swap_state_ = SWAP_COMPLETED; | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 void Compositor::ScheduleComposite() { | |
| 329 if (!disable_schedule_composite_) | |
| 330 ScheduleDraw(); | |
| 331 } | |
| 332 | |
| 333 void Compositor::ScheduleAnimation() { | |
| 334 ScheduleComposite(); | |
| 335 } | |
| 336 | |
| 337 void Compositor::DidPostSwapBuffers() { | |
| 338 DCHECK(!compositor_thread_loop_); | |
| 339 DCHECK_EQ(swap_state_, SWAP_NONE); | |
| 340 swap_state_ = SWAP_POSTED; | |
| 341 } | |
| 342 | |
| 343 void Compositor::DidAbortSwapBuffers() { | 283 void Compositor::DidAbortSwapBuffers() { |
| 344 if (!compositor_thread_loop_) { | |
| 345 if (swap_state_ == SWAP_POSTED) { | |
| 346 NotifyEnd(); | |
| 347 swap_state_ = SWAP_COMPLETED; | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 FOR_EACH_OBSERVER(CompositorObserver, | 284 FOR_EACH_OBSERVER(CompositorObserver, |
| 352 observer_list_, | 285 observer_list_, |
| 353 OnCompositingAborted(this)); | 286 OnCompositingAborted(this)); |
| 354 } | 287 } |
| 355 | 288 |
| 356 void Compositor::ScheduleAnimationForLayerCollection() { | 289 void Compositor::ScheduleAnimationForLayerCollection() { |
| 357 host_->SetNeedsAnimate(); | 290 host_->SetNeedsAnimate(); |
| 358 } | 291 } |
| 359 | 292 |
| 360 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 293 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 361 return host_->debug_state(); | 294 return host_->debug_state(); |
| 362 } | 295 } |
| 363 | 296 |
| 364 void Compositor::SetLayerTreeDebugState( | 297 void Compositor::SetLayerTreeDebugState( |
| 365 const cc::LayerTreeDebugState& debug_state) { | 298 const cc::LayerTreeDebugState& debug_state) { |
| 366 host_->SetDebugState(debug_state); | 299 host_->SetDebugState(debug_state); |
| 367 } | 300 } |
| 368 | 301 |
| 369 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 302 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 370 if (!compositor_lock_) { | 303 if (!compositor_lock_) { |
| 371 compositor_lock_ = new CompositorLock(this); | 304 compositor_lock_ = new CompositorLock(this); |
| 372 if (compositor_thread_loop_) | 305 host_->SetDeferCommits(true); |
| 373 host_->SetDeferCommits(true); | |
| 374 FOR_EACH_OBSERVER(CompositorObserver, | 306 FOR_EACH_OBSERVER(CompositorObserver, |
| 375 observer_list_, | 307 observer_list_, |
| 376 OnCompositingLockStateChanged(this)); | 308 OnCompositingLockStateChanged(this)); |
| 377 } | 309 } |
| 378 return compositor_lock_; | 310 return compositor_lock_; |
| 379 } | 311 } |
| 380 | 312 |
| 381 void Compositor::UnlockCompositor() { | 313 void Compositor::UnlockCompositor() { |
| 382 DCHECK(compositor_lock_); | 314 DCHECK(compositor_lock_); |
| 383 compositor_lock_ = NULL; | 315 compositor_lock_ = NULL; |
| 384 if (compositor_thread_loop_) | 316 host_->SetDeferCommits(false); |
| 385 host_->SetDeferCommits(false); | |
| 386 FOR_EACH_OBSERVER(CompositorObserver, | 317 FOR_EACH_OBSERVER(CompositorObserver, |
| 387 observer_list_, | 318 observer_list_, |
| 388 OnCompositingLockStateChanged(this)); | 319 OnCompositingLockStateChanged(this)); |
| 389 } | 320 } |
| 390 | 321 |
| 391 void Compositor::CancelCompositorLock() { | 322 void Compositor::CancelCompositorLock() { |
| 392 if (compositor_lock_) | 323 if (compositor_lock_) |
| 393 compositor_lock_->CancelLock(); | 324 compositor_lock_->CancelLock(); |
| 394 } | 325 } |
| 395 | 326 |
| 396 void Compositor::NotifyEnd() { | |
| 397 last_ended_frame_++; | |
| 398 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); | |
| 399 waiting_on_compositing_end_ = false; | |
| 400 if (draw_on_compositing_end_) { | |
| 401 draw_on_compositing_end_ = false; | |
| 402 | |
| 403 // Call ScheduleDraw() instead of Draw() in order to allow other | |
| 404 // CompositorObservers to be notified before starting another | |
| 405 // draw cycle. | |
| 406 ScheduleDraw(); | |
| 407 } | |
| 408 FOR_EACH_OBSERVER(CompositorObserver, | |
| 409 observer_list_, | |
| 410 OnCompositingEnded(this)); | |
| 411 } | |
| 412 | |
| 413 } // namespace ui | 327 } // namespace ui |
| OLD | NEW |