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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 264 } |
310 | 265 |
311 void Compositor::DidCommit() { | 266 void Compositor::DidCommit() { |
312 DCHECK(!IsLocked()); | 267 DCHECK(!IsLocked()); |
313 FOR_EACH_OBSERVER(CompositorObserver, | 268 FOR_EACH_OBSERVER(CompositorObserver, |
314 observer_list_, | 269 observer_list_, |
315 OnCompositingDidCommit(this)); | 270 OnCompositingDidCommit(this)); |
316 } | 271 } |
317 | 272 |
318 void Compositor::DidCommitAndDrawFrame() { | 273 void Compositor::DidCommitAndDrawFrame() { |
| 274 } |
| 275 |
| 276 void Compositor::DidCompleteSwapBuffers() { |
| 277 // DidPostSwapBuffers is a SingleThreadProxy-only feature. Synthetically |
| 278 // generate OnCompositingStarted messages for the threaded case so that |
| 279 // OnCompositingStarted/OnCompositingEnded messages match. |
| 280 if (compositor_thread_loop_) { |
| 281 base::TimeTicks start_time = gfx::FrameTime::Now(); |
| 282 FOR_EACH_OBSERVER(CompositorObserver, |
| 283 observer_list_, |
| 284 OnCompositingStarted(this, start_time)); |
| 285 } |
| 286 FOR_EACH_OBSERVER( |
| 287 CompositorObserver, observer_list_, OnCompositingEnded(this)); |
| 288 } |
| 289 |
| 290 void Compositor::DidPostSwapBuffers() { |
319 base::TimeTicks start_time = gfx::FrameTime::Now(); | 291 base::TimeTicks start_time = gfx::FrameTime::Now(); |
320 FOR_EACH_OBSERVER(CompositorObserver, | 292 FOR_EACH_OBSERVER(CompositorObserver, |
321 observer_list_, | 293 observer_list_, |
322 OnCompositingStarted(this, start_time)); | 294 OnCompositingStarted(this, start_time)); |
323 } | 295 } |
324 | 296 |
325 void Compositor::DidCompleteSwapBuffers() { | |
326 if (compositor_thread_loop_) { | |
327 NotifyEnd(); | |
328 } else { | |
329 DCHECK_EQ(swap_state_, SWAP_POSTED); | |
330 NotifyEnd(); | |
331 swap_state_ = SWAP_COMPLETED; | |
332 } | |
333 } | |
334 | |
335 void Compositor::ScheduleComposite() { | |
336 if (!disable_schedule_composite_) | |
337 ScheduleDraw(); | |
338 } | |
339 | |
340 void Compositor::ScheduleAnimation() { | |
341 ScheduleComposite(); | |
342 } | |
343 | |
344 void Compositor::DidPostSwapBuffers() { | |
345 DCHECK(!compositor_thread_loop_); | |
346 DCHECK_EQ(swap_state_, SWAP_NONE); | |
347 swap_state_ = SWAP_POSTED; | |
348 } | |
349 | |
350 void Compositor::DidAbortSwapBuffers() { | 297 void Compositor::DidAbortSwapBuffers() { |
351 if (!compositor_thread_loop_) { | |
352 if (swap_state_ == SWAP_POSTED) { | |
353 NotifyEnd(); | |
354 swap_state_ = SWAP_COMPLETED; | |
355 } | |
356 } | |
357 | |
358 FOR_EACH_OBSERVER(CompositorObserver, | 298 FOR_EACH_OBSERVER(CompositorObserver, |
359 observer_list_, | 299 observer_list_, |
360 OnCompositingAborted(this)); | 300 OnCompositingAborted(this)); |
361 } | 301 } |
362 | 302 |
363 void Compositor::ScheduleAnimationForLayerCollection() { | 303 void Compositor::ScheduleAnimationForLayerCollection() { |
364 host_->SetNeedsAnimate(); | 304 host_->SetNeedsAnimate(); |
365 } | 305 } |
366 | 306 |
367 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { | 307 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
368 return host_->debug_state(); | 308 return host_->debug_state(); |
369 } | 309 } |
370 | 310 |
371 void Compositor::SetLayerTreeDebugState( | 311 void Compositor::SetLayerTreeDebugState( |
372 const cc::LayerTreeDebugState& debug_state) { | 312 const cc::LayerTreeDebugState& debug_state) { |
373 host_->SetDebugState(debug_state); | 313 host_->SetDebugState(debug_state); |
374 } | 314 } |
375 | 315 |
376 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 316 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
377 if (!compositor_lock_) { | 317 if (!compositor_lock_) { |
378 compositor_lock_ = new CompositorLock(this); | 318 compositor_lock_ = new CompositorLock(this); |
379 if (compositor_thread_loop_) | 319 host_->SetDeferCommits(true); |
380 host_->SetDeferCommits(true); | |
381 FOR_EACH_OBSERVER(CompositorObserver, | 320 FOR_EACH_OBSERVER(CompositorObserver, |
382 observer_list_, | 321 observer_list_, |
383 OnCompositingLockStateChanged(this)); | 322 OnCompositingLockStateChanged(this)); |
384 } | 323 } |
385 return compositor_lock_; | 324 return compositor_lock_; |
386 } | 325 } |
387 | 326 |
388 void Compositor::UnlockCompositor() { | 327 void Compositor::UnlockCompositor() { |
389 DCHECK(compositor_lock_); | 328 DCHECK(compositor_lock_); |
390 compositor_lock_ = NULL; | 329 compositor_lock_ = NULL; |
391 if (compositor_thread_loop_) | 330 host_->SetDeferCommits(false); |
392 host_->SetDeferCommits(false); | |
393 FOR_EACH_OBSERVER(CompositorObserver, | 331 FOR_EACH_OBSERVER(CompositorObserver, |
394 observer_list_, | 332 observer_list_, |
395 OnCompositingLockStateChanged(this)); | 333 OnCompositingLockStateChanged(this)); |
396 } | 334 } |
397 | 335 |
398 void Compositor::CancelCompositorLock() { | 336 void Compositor::CancelCompositorLock() { |
399 if (compositor_lock_) | 337 if (compositor_lock_) |
400 compositor_lock_->CancelLock(); | 338 compositor_lock_->CancelLock(); |
401 } | 339 } |
402 | 340 |
403 void Compositor::NotifyEnd() { | |
404 last_ended_frame_++; | |
405 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); | |
406 waiting_on_compositing_end_ = false; | |
407 if (draw_on_compositing_end_) { | |
408 draw_on_compositing_end_ = false; | |
409 | |
410 // Call ScheduleDraw() instead of Draw() in order to allow other | |
411 // CompositorObservers to be notified before starting another | |
412 // draw cycle. | |
413 ScheduleDraw(); | |
414 } | |
415 FOR_EACH_OBSERVER(CompositorObserver, | |
416 observer_list_, | |
417 OnCompositingEnded(this)); | |
418 } | |
419 | |
420 } // namespace ui | 341 } // namespace ui |
OLD | NEW |