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 |
64 Compositor::Compositor(gfx::AcceleratedWidget widget, | 72 Compositor::Compositor(gfx::AcceleratedWidget widget, |
65 ui::ContextFactory* context_factory) | 73 ui::ContextFactory* context_factory) |
66 : context_factory_(context_factory), | 74 : context_factory_(context_factory), |
67 root_layer_(NULL), | 75 root_layer_(NULL), |
68 widget_(widget), | 76 widget_(widget), |
69 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), | 77 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), |
70 vsync_manager_(new CompositorVSyncManager()), | 78 vsync_manager_(new CompositorVSyncManager()), |
71 device_scale_factor_(0.0f), | 79 device_scale_factor_(0.0f), |
| 80 last_started_frame_(0), |
| 81 last_ended_frame_(0), |
72 disable_schedule_composite_(false), | 82 disable_schedule_composite_(false), |
73 compositor_lock_(NULL), | 83 compositor_lock_(NULL), |
74 layer_animator_collection_(this) { | 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), |
| 89 schedule_draw_factory_(this) { |
75 root_web_layer_ = cc::Layer::Create(); | 90 root_web_layer_ = cc::Layer::Create(); |
76 | 91 |
77 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 92 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
78 | 93 |
79 cc::LayerTreeSettings settings; | 94 cc::LayerTreeSettings settings; |
80 settings.refresh_rate = | 95 settings.refresh_rate = |
81 context_factory_->DoesCreateTestContexts() | 96 context_factory_->DoesCreateTestContexts() |
82 ? kTestRefreshRate | 97 ? kTestRefreshRate |
83 : kDefaultRefreshRate; | 98 : kDefaultRefreshRate; |
84 settings.main_frame_before_draw_enabled = false; | 99 settings.main_frame_before_draw_enabled = false; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 root_layer_->SetCompositor(NULL); | 163 root_layer_->SetCompositor(NULL); |
149 | 164 |
150 // Stop all outstanding draws before telling the ContextFactory to tear | 165 // Stop all outstanding draws before telling the ContextFactory to tear |
151 // down any contexts that the |host_| may rely upon. | 166 // down any contexts that the |host_| may rely upon. |
152 host_.reset(); | 167 host_.reset(); |
153 | 168 |
154 context_factory_->RemoveCompositor(this); | 169 context_factory_->RemoveCompositor(this); |
155 } | 170 } |
156 | 171 |
157 void Compositor::ScheduleDraw() { | 172 void Compositor::ScheduleDraw() { |
158 host_->SetNeedsCommit(); | 173 if (compositor_thread_loop_) { |
| 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 } |
159 } | 181 } |
160 | 182 |
161 void Compositor::SetRootLayer(Layer* root_layer) { | 183 void Compositor::SetRootLayer(Layer* root_layer) { |
162 if (root_layer_ == root_layer) | 184 if (root_layer_ == root_layer) |
163 return; | 185 return; |
164 if (root_layer_) | 186 if (root_layer_) |
165 root_layer_->SetCompositor(NULL); | 187 root_layer_->SetCompositor(NULL); |
166 root_layer_ = root_layer; | 188 root_layer_ = root_layer; |
167 if (root_layer_ && !root_layer_->GetCompositor()) | 189 if (root_layer_ && !root_layer_->GetCompositor()) |
168 root_layer_->SetCompositor(this); | 190 root_layer_->SetCompositor(this); |
169 root_web_layer_->RemoveAllChildren(); | 191 root_web_layer_->RemoveAllChildren(); |
170 if (root_layer_) | 192 if (root_layer_) |
171 root_web_layer_->AddChild(root_layer_->cc_layer()); | 193 root_web_layer_->AddChild(root_layer_->cc_layer()); |
172 } | 194 } |
173 | 195 |
174 void Compositor::SetHostHasTransparentBackground( | 196 void Compositor::SetHostHasTransparentBackground( |
175 bool host_has_transparent_background) { | 197 bool host_has_transparent_background) { |
176 host_->set_has_transparent_background(host_has_transparent_background); | 198 host_->set_has_transparent_background(host_has_transparent_background); |
177 } | 199 } |
178 | 200 |
| 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 |
179 void Compositor::ScheduleFullRedraw() { | 231 void Compositor::ScheduleFullRedraw() { |
180 host_->SetNeedsRedraw(); | 232 host_->SetNeedsRedraw(); |
181 } | 233 } |
182 | 234 |
183 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 235 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
184 host_->SetNeedsRedrawRect(damage_rect); | 236 host_->SetNeedsRedrawRect(damage_rect); |
185 } | 237 } |
186 | 238 |
187 void Compositor::FinishAllRendering() { | 239 void Compositor::FinishAllRendering() { |
188 host_->FinishAllRendering(); | 240 host_->FinishAllRendering(); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } | 302 } |
251 | 303 |
252 void Compositor::DidCommit() { | 304 void Compositor::DidCommit() { |
253 DCHECK(!IsLocked()); | 305 DCHECK(!IsLocked()); |
254 FOR_EACH_OBSERVER(CompositorObserver, | 306 FOR_EACH_OBSERVER(CompositorObserver, |
255 observer_list_, | 307 observer_list_, |
256 OnCompositingDidCommit(this)); | 308 OnCompositingDidCommit(this)); |
257 } | 309 } |
258 | 310 |
259 void Compositor::DidCommitAndDrawFrame() { | 311 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() { | |
277 base::TimeTicks start_time = gfx::FrameTime::Now(); | 312 base::TimeTicks start_time = gfx::FrameTime::Now(); |
278 FOR_EACH_OBSERVER(CompositorObserver, | 313 FOR_EACH_OBSERVER(CompositorObserver, |
279 observer_list_, | 314 observer_list_, |
280 OnCompositingStarted(this, start_time)); | 315 OnCompositingStarted(this, start_time)); |
281 } | 316 } |
282 | 317 |
| 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 |
283 void Compositor::DidAbortSwapBuffers() { | 343 void Compositor::DidAbortSwapBuffers() { |
| 344 if (!compositor_thread_loop_) { |
| 345 if (swap_state_ == SWAP_POSTED) { |
| 346 NotifyEnd(); |
| 347 swap_state_ = SWAP_COMPLETED; |
| 348 } |
| 349 } |
| 350 |
284 FOR_EACH_OBSERVER(CompositorObserver, | 351 FOR_EACH_OBSERVER(CompositorObserver, |
285 observer_list_, | 352 observer_list_, |
286 OnCompositingAborted(this)); | 353 OnCompositingAborted(this)); |
287 } | 354 } |
288 | 355 |
289 void Compositor::ScheduleAnimationForLayerCollection() { | 356 void Compositor::ScheduleAnimationForLayerCollection() { |
290 host_->SetNeedsAnimate(); | 357 host_->SetNeedsAnimate(); |
291 } | 358 } |
292 | 359 |
293 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 360 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
294 return host_->debug_state(); | 361 return host_->debug_state(); |
295 } | 362 } |
296 | 363 |
297 void Compositor::SetLayerTreeDebugState( | 364 void Compositor::SetLayerTreeDebugState( |
298 const cc::LayerTreeDebugState& debug_state) { | 365 const cc::LayerTreeDebugState& debug_state) { |
299 host_->SetDebugState(debug_state); | 366 host_->SetDebugState(debug_state); |
300 } | 367 } |
301 | 368 |
302 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 369 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
303 if (!compositor_lock_) { | 370 if (!compositor_lock_) { |
304 compositor_lock_ = new CompositorLock(this); | 371 compositor_lock_ = new CompositorLock(this); |
305 host_->SetDeferCommits(true); | 372 if (compositor_thread_loop_) |
| 373 host_->SetDeferCommits(true); |
306 FOR_EACH_OBSERVER(CompositorObserver, | 374 FOR_EACH_OBSERVER(CompositorObserver, |
307 observer_list_, | 375 observer_list_, |
308 OnCompositingLockStateChanged(this)); | 376 OnCompositingLockStateChanged(this)); |
309 } | 377 } |
310 return compositor_lock_; | 378 return compositor_lock_; |
311 } | 379 } |
312 | 380 |
313 void Compositor::UnlockCompositor() { | 381 void Compositor::UnlockCompositor() { |
314 DCHECK(compositor_lock_); | 382 DCHECK(compositor_lock_); |
315 compositor_lock_ = NULL; | 383 compositor_lock_ = NULL; |
316 host_->SetDeferCommits(false); | 384 if (compositor_thread_loop_) |
| 385 host_->SetDeferCommits(false); |
317 FOR_EACH_OBSERVER(CompositorObserver, | 386 FOR_EACH_OBSERVER(CompositorObserver, |
318 observer_list_, | 387 observer_list_, |
319 OnCompositingLockStateChanged(this)); | 388 OnCompositingLockStateChanged(this)); |
320 } | 389 } |
321 | 390 |
322 void Compositor::CancelCompositorLock() { | 391 void Compositor::CancelCompositorLock() { |
323 if (compositor_lock_) | 392 if (compositor_lock_) |
324 compositor_lock_->CancelLock(); | 393 compositor_lock_->CancelLock(); |
325 } | 394 } |
326 | 395 |
| 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 |
327 } // namespace ui | 413 } // namespace ui |
OLD | NEW |