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