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 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 66 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
75 : context_factory_(context_factory), | 67 : context_factory_(context_factory), |
76 root_layer_(NULL), | 68 root_layer_(NULL), |
77 widget_(widget), | 69 widget_(widget), |
78 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), | 70 compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), |
79 task_runner_(task_runner), | 71 task_runner_(task_runner), |
80 vsync_manager_(new CompositorVSyncManager()), | 72 vsync_manager_(new CompositorVSyncManager()), |
81 device_scale_factor_(0.0f), | 73 device_scale_factor_(0.0f), |
82 last_started_frame_(0), | |
83 last_ended_frame_(0), | |
84 disable_schedule_composite_(false), | 74 disable_schedule_composite_(false), |
85 compositor_lock_(NULL), | 75 compositor_lock_(NULL), |
86 defer_draw_scheduling_(false), | 76 layer_animator_collection_(this) { |
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) { | |
92 root_web_layer_ = cc::Layer::Create(); | 77 root_web_layer_ = cc::Layer::Create(); |
93 | 78 |
94 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 79 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
95 | 80 |
96 cc::LayerTreeSettings settings; | 81 cc::LayerTreeSettings settings; |
97 settings.refresh_rate = | 82 settings.refresh_rate = |
98 context_factory_->DoesCreateTestContexts() | 83 context_factory_->DoesCreateTestContexts() |
99 ? kTestRefreshRate | 84 ? kTestRefreshRate |
100 : kDefaultRefreshRate; | 85 : kDefaultRefreshRate; |
101 settings.main_frame_before_draw_enabled = false; | 86 settings.main_frame_before_draw_enabled = false; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 root_layer_->SetCompositor(NULL); | 155 root_layer_->SetCompositor(NULL); |
171 | 156 |
172 // Stop all outstanding draws before telling the ContextFactory to tear | 157 // Stop all outstanding draws before telling the ContextFactory to tear |
173 // down any contexts that the |host_| may rely upon. | 158 // down any contexts that the |host_| may rely upon. |
174 host_.reset(); | 159 host_.reset(); |
175 | 160 |
176 context_factory_->RemoveCompositor(this); | 161 context_factory_->RemoveCompositor(this); |
177 } | 162 } |
178 | 163 |
179 void Compositor::ScheduleDraw() { | 164 void Compositor::ScheduleDraw() { |
180 if (compositor_thread_loop_) { | 165 host_->SetNeedsCommit(); |
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 } | |
188 } | 166 } |
189 | 167 |
190 void Compositor::SetRootLayer(Layer* root_layer) { | 168 void Compositor::SetRootLayer(Layer* root_layer) { |
191 if (root_layer_ == root_layer) | 169 if (root_layer_ == root_layer) |
192 return; | 170 return; |
193 if (root_layer_) | 171 if (root_layer_) |
194 root_layer_->SetCompositor(NULL); | 172 root_layer_->SetCompositor(NULL); |
195 root_layer_ = root_layer; | 173 root_layer_ = root_layer; |
196 if (root_layer_ && !root_layer_->GetCompositor()) | 174 if (root_layer_ && !root_layer_->GetCompositor()) |
197 root_layer_->SetCompositor(this); | 175 root_layer_->SetCompositor(this); |
198 root_web_layer_->RemoveAllChildren(); | 176 root_web_layer_->RemoveAllChildren(); |
199 if (root_layer_) | 177 if (root_layer_) |
200 root_web_layer_->AddChild(root_layer_->cc_layer()); | 178 root_web_layer_->AddChild(root_layer_->cc_layer()); |
201 } | 179 } |
202 | 180 |
203 void Compositor::SetHostHasTransparentBackground( | 181 void Compositor::SetHostHasTransparentBackground( |
204 bool host_has_transparent_background) { | 182 bool host_has_transparent_background) { |
205 host_->set_has_transparent_background(host_has_transparent_background); | 183 host_->set_has_transparent_background(host_has_transparent_background); |
206 } | 184 } |
207 | 185 |
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 | |
238 void Compositor::ScheduleFullRedraw() { | 186 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. |
239 host_->SetNeedsRedraw(); | 191 host_->SetNeedsRedraw(); |
| 192 host_->SetNeedsCommit(); |
240 } | 193 } |
241 | 194 |
242 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { | 195 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 196 // TODO(enne): Make this not commit. See ScheduleFullRedraw. |
243 host_->SetNeedsRedrawRect(damage_rect); | 197 host_->SetNeedsRedrawRect(damage_rect); |
| 198 host_->SetNeedsCommit(); |
244 } | 199 } |
245 | 200 |
246 void Compositor::FinishAllRendering() { | 201 void Compositor::FinishAllRendering() { |
247 host_->FinishAllRendering(); | 202 host_->FinishAllRendering(); |
248 } | 203 } |
249 | 204 |
250 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { | 205 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { |
251 scoped_ptr<cc::SwapPromise> swap_promise( | 206 scoped_ptr<cc::SwapPromise> swap_promise( |
252 new cc::LatencyInfoSwapPromise(latency_info)); | 207 new cc::LatencyInfoSwapPromise(latency_info)); |
253 host_->QueueSwapPromise(swap_promise.Pass()); | 208 host_->QueueSwapPromise(swap_promise.Pass()); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 } | 280 } |
326 | 281 |
327 void Compositor::DidCommit() { | 282 void Compositor::DidCommit() { |
328 DCHECK(!IsLocked()); | 283 DCHECK(!IsLocked()); |
329 FOR_EACH_OBSERVER(CompositorObserver, | 284 FOR_EACH_OBSERVER(CompositorObserver, |
330 observer_list_, | 285 observer_list_, |
331 OnCompositingDidCommit(this)); | 286 OnCompositingDidCommit(this)); |
332 } | 287 } |
333 | 288 |
334 void Compositor::DidCommitAndDrawFrame() { | 289 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() { |
335 base::TimeTicks start_time = gfx::FrameTime::Now(); | 307 base::TimeTicks start_time = gfx::FrameTime::Now(); |
336 FOR_EACH_OBSERVER(CompositorObserver, | 308 FOR_EACH_OBSERVER(CompositorObserver, |
337 observer_list_, | 309 observer_list_, |
338 OnCompositingStarted(this, start_time)); | 310 OnCompositingStarted(this, start_time)); |
339 } | 311 } |
340 | 312 |
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 | |
366 void Compositor::DidAbortSwapBuffers() { | 313 void Compositor::DidAbortSwapBuffers() { |
367 if (!compositor_thread_loop_) { | |
368 if (swap_state_ == SWAP_POSTED) { | |
369 NotifyEnd(); | |
370 swap_state_ = SWAP_COMPLETED; | |
371 } | |
372 } | |
373 | |
374 FOR_EACH_OBSERVER(CompositorObserver, | 314 FOR_EACH_OBSERVER(CompositorObserver, |
375 observer_list_, | 315 observer_list_, |
376 OnCompositingAborted(this)); | 316 OnCompositingAborted(this)); |
377 } | 317 } |
378 | 318 |
379 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 319 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
380 return host_->debug_state(); | 320 return host_->debug_state(); |
381 } | 321 } |
382 | 322 |
383 void Compositor::SetLayerTreeDebugState( | 323 void Compositor::SetLayerTreeDebugState( |
384 const cc::LayerTreeDebugState& debug_state) { | 324 const cc::LayerTreeDebugState& debug_state) { |
385 host_->SetDebugState(debug_state); | 325 host_->SetDebugState(debug_state); |
386 } | 326 } |
387 | 327 |
388 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 328 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
389 if (!compositor_lock_) { | 329 if (!compositor_lock_) { |
390 compositor_lock_ = new CompositorLock(this); | 330 compositor_lock_ = new CompositorLock(this); |
391 if (compositor_thread_loop_) | 331 host_->SetDeferCommits(true); |
392 host_->SetDeferCommits(true); | |
393 FOR_EACH_OBSERVER(CompositorObserver, | 332 FOR_EACH_OBSERVER(CompositorObserver, |
394 observer_list_, | 333 observer_list_, |
395 OnCompositingLockStateChanged(this)); | 334 OnCompositingLockStateChanged(this)); |
396 } | 335 } |
397 return compositor_lock_; | 336 return compositor_lock_; |
398 } | 337 } |
399 | 338 |
400 void Compositor::UnlockCompositor() { | 339 void Compositor::UnlockCompositor() { |
401 DCHECK(compositor_lock_); | 340 DCHECK(compositor_lock_); |
402 compositor_lock_ = NULL; | 341 compositor_lock_ = NULL; |
403 if (compositor_thread_loop_) | 342 host_->SetDeferCommits(false); |
404 host_->SetDeferCommits(false); | |
405 FOR_EACH_OBSERVER(CompositorObserver, | 343 FOR_EACH_OBSERVER(CompositorObserver, |
406 observer_list_, | 344 observer_list_, |
407 OnCompositingLockStateChanged(this)); | 345 OnCompositingLockStateChanged(this)); |
408 } | 346 } |
409 | 347 |
410 void Compositor::CancelCompositorLock() { | 348 void Compositor::CancelCompositorLock() { |
411 if (compositor_lock_) | 349 if (compositor_lock_) |
412 compositor_lock_->CancelLock(); | 350 compositor_lock_->CancelLock(); |
413 } | 351 } |
414 | 352 |
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 | |
432 } // namespace ui | 353 } // namespace ui |
OLD | NEW |