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