Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / cc / trees / thread_proxy.cc
blob2e41ea7d0ef34800fe4d12c9009d79cc71892015
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/trees/thread_proxy.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/auto_reset.h"
11 #include "base/bind.h"
12 #include "base/debug/trace_event.h"
13 #include "base/debug/trace_event_argument.h"
14 #include "base/debug/trace_event_synthetic_delay.h"
15 #include "cc/base/swap_promise.h"
16 #include "cc/debug/benchmark_instrumentation.h"
17 #include "cc/debug/devtools_instrumentation.h"
18 #include "cc/input/input_handler.h"
19 #include "cc/output/context_provider.h"
20 #include "cc/output/output_surface.h"
21 #include "cc/quads/draw_quad.h"
22 #include "cc/resources/prioritized_resource_manager.h"
23 #include "cc/scheduler/delay_based_time_source.h"
24 #include "cc/scheduler/scheduler.h"
25 #include "cc/trees/blocking_task_runner.h"
26 #include "cc/trees/layer_tree_host.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "gpu/command_buffer/client/gles2_interface.h"
29 #include "ui/gfx/frame_time.h"
31 namespace cc {
33 namespace {
35 // Measured in seconds.
36 const double kSmoothnessTakesPriorityExpirationDelay = 0.25;
38 unsigned int nextBeginFrameId = 0;
40 class SwapPromiseChecker {
41 public:
42 explicit SwapPromiseChecker(LayerTreeHost* layer_tree_host)
43 : layer_tree_host_(layer_tree_host) {}
45 ~SwapPromiseChecker() {
46 layer_tree_host_->BreakSwapPromises(SwapPromise::COMMIT_FAILS);
49 private:
50 LayerTreeHost* layer_tree_host_;
53 } // namespace
55 struct ThreadProxy::SchedulerStateRequest {
56 CompletionEvent completion;
57 scoped_ptr<base::Value> state;
60 scoped_ptr<Proxy> ThreadProxy::Create(
61 LayerTreeHost* layer_tree_host,
62 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
63 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) {
64 return make_scoped_ptr(new ThreadProxy(layer_tree_host,
65 main_task_runner,
66 impl_task_runner)).PassAs<Proxy>();
69 ThreadProxy::ThreadProxy(
70 LayerTreeHost* layer_tree_host,
71 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
72 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner)
73 : Proxy(main_task_runner, impl_task_runner),
74 main_thread_only_vars_unsafe_(this, layer_tree_host->id()),
75 main_thread_or_blocked_vars_unsafe_(layer_tree_host),
76 compositor_thread_vars_unsafe_(
77 this,
78 layer_tree_host->id(),
79 layer_tree_host->rendering_stats_instrumentation()) {
80 TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy");
81 DCHECK(IsMainThread());
82 DCHECK(this->layer_tree_host());
85 ThreadProxy::MainThreadOnly::MainThreadOnly(ThreadProxy* proxy,
86 int layer_tree_host_id)
87 : layer_tree_host_id(layer_tree_host_id),
88 animate_requested(false),
89 commit_requested(false),
90 commit_request_sent_to_impl_thread(false),
91 started(false),
92 manage_tiles_pending(false),
93 can_cancel_commit(true),
94 defer_commits(false),
95 weak_factory(proxy) {}
97 ThreadProxy::MainThreadOnly::~MainThreadOnly() {}
99 ThreadProxy::MainThreadOrBlockedMainThread::MainThreadOrBlockedMainThread(
100 LayerTreeHost* host)
101 : layer_tree_host(host),
102 commit_waits_for_activation(false),
103 main_thread_inside_commit(false) {}
105 ThreadProxy::MainThreadOrBlockedMainThread::~MainThreadOrBlockedMainThread() {}
107 PrioritizedResourceManager*
108 ThreadProxy::MainThreadOrBlockedMainThread::contents_texture_manager() {
109 return layer_tree_host->contents_texture_manager();
112 ThreadProxy::CompositorThreadOnly::CompositorThreadOnly(
113 ThreadProxy* proxy,
114 int layer_tree_host_id,
115 RenderingStatsInstrumentation* rendering_stats_instrumentation)
116 : layer_tree_host_id(layer_tree_host_id),
117 contents_texture_manager(NULL),
118 commit_completion_event(NULL),
119 completion_event_for_commit_held_on_tree_activation(NULL),
120 next_frame_is_newly_committed_frame(false),
121 inside_draw(false),
122 input_throttled_until_commit(false),
123 animations_frozen_until_next_draw(false),
124 did_commit_after_animating(false),
125 smoothness_priority_expiration_notifier(
126 proxy->ImplThreadTaskRunner(),
127 base::Bind(&ThreadProxy::RenewTreePriority, base::Unretained(proxy)),
128 base::TimeDelta::FromMilliseconds(
129 kSmoothnessTakesPriorityExpirationDelay * 1000)),
130 timing_history(rendering_stats_instrumentation),
131 weak_factory(proxy) {
134 ThreadProxy::CompositorThreadOnly::~CompositorThreadOnly() {}
136 ThreadProxy::~ThreadProxy() {
137 TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy");
138 DCHECK(IsMainThread());
139 DCHECK(!main().started);
142 void ThreadProxy::FinishAllRendering() {
143 DCHECK(Proxy::IsMainThread());
144 DCHECK(!main().defer_commits);
146 // Make sure all GL drawing is finished on the impl thread.
147 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
148 CompletionEvent completion;
149 Proxy::ImplThreadTaskRunner()->PostTask(
150 FROM_HERE,
151 base::Bind(&ThreadProxy::FinishAllRenderingOnImplThread,
152 impl_thread_weak_ptr_,
153 &completion));
154 completion.Wait();
157 bool ThreadProxy::IsStarted() const {
158 DCHECK(Proxy::IsMainThread());
159 return main().started;
162 void ThreadProxy::SetLayerTreeHostClientReady() {
163 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReady");
164 Proxy::ImplThreadTaskRunner()->PostTask(
165 FROM_HERE,
166 base::Bind(&ThreadProxy::SetLayerTreeHostClientReadyOnImplThread,
167 impl_thread_weak_ptr_));
170 void ThreadProxy::SetLayerTreeHostClientReadyOnImplThread() {
171 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReadyOnImplThread");
172 impl().scheduler->SetCanStart();
175 void ThreadProxy::SetVisible(bool visible) {
176 TRACE_EVENT0("cc", "ThreadProxy::SetVisible");
177 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
179 CompletionEvent completion;
180 Proxy::ImplThreadTaskRunner()->PostTask(
181 FROM_HERE,
182 base::Bind(&ThreadProxy::SetVisibleOnImplThread,
183 impl_thread_weak_ptr_,
184 &completion,
185 visible));
186 completion.Wait();
189 void ThreadProxy::SetVisibleOnImplThread(CompletionEvent* completion,
190 bool visible) {
191 TRACE_EVENT0("cc", "ThreadProxy::SetVisibleOnImplThread");
192 impl().layer_tree_host_impl->SetVisible(visible);
193 impl().scheduler->SetVisible(visible);
194 UpdateBackgroundAnimateTicking();
195 completion->Signal();
198 void ThreadProxy::UpdateBackgroundAnimateTicking() {
199 bool should_background_tick =
200 !impl().scheduler->WillDrawIfNeeded() &&
201 impl().layer_tree_host_impl->active_tree()->root_layer();
202 impl().layer_tree_host_impl->UpdateBackgroundAnimateTicking(
203 should_background_tick);
204 if (should_background_tick)
205 impl().animations_frozen_until_next_draw = false;
208 void ThreadProxy::DidLoseOutputSurface() {
209 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurface");
210 DCHECK(IsMainThread());
211 layer_tree_host()->DidLoseOutputSurface();
214 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
216 // Return lost resources to their owners immediately.
217 BlockingTaskRunner::CapturePostTasks blocked;
219 CompletionEvent completion;
220 Proxy::ImplThreadTaskRunner()->PostTask(
221 FROM_HERE,
222 base::Bind(&ThreadProxy::DeleteContentsTexturesOnImplThread,
223 impl_thread_weak_ptr_,
224 &completion));
225 completion.Wait();
229 void ThreadProxy::CreateAndInitializeOutputSurface() {
230 TRACE_EVENT0("cc", "ThreadProxy::DoCreateAndInitializeOutputSurface");
231 DCHECK(IsMainThread());
233 scoped_ptr<OutputSurface> output_surface =
234 layer_tree_host()->CreateOutputSurface();
236 if (output_surface) {
237 Proxy::ImplThreadTaskRunner()->PostTask(
238 FROM_HERE,
239 base::Bind(&ThreadProxy::InitializeOutputSurfaceOnImplThread,
240 impl_thread_weak_ptr_,
241 base::Passed(&output_surface)));
242 return;
245 DidInitializeOutputSurface(false, RendererCapabilities());
248 void ThreadProxy::DidInitializeOutputSurface(
249 bool success,
250 const RendererCapabilities& capabilities) {
251 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeOutputSurface");
252 DCHECK(IsMainThread());
253 main().renderer_capabilities_main_thread_copy = capabilities;
254 layer_tree_host()->OnCreateAndInitializeOutputSurfaceAttempted(success);
256 if (!success) {
257 Proxy::MainThreadTaskRunner()->PostTask(
258 FROM_HERE,
259 base::Bind(&ThreadProxy::CreateAndInitializeOutputSurface,
260 main_thread_weak_ptr_));
264 void ThreadProxy::SetRendererCapabilitiesMainThreadCopy(
265 const RendererCapabilities& capabilities) {
266 main().renderer_capabilities_main_thread_copy = capabilities;
269 void ThreadProxy::SendCommitRequestToImplThreadIfNeeded() {
270 DCHECK(IsMainThread());
271 if (main().commit_request_sent_to_impl_thread)
272 return;
273 main().commit_request_sent_to_impl_thread = true;
274 Proxy::ImplThreadTaskRunner()->PostTask(
275 FROM_HERE,
276 base::Bind(&ThreadProxy::SetNeedsCommitOnImplThread,
277 impl_thread_weak_ptr_));
280 const RendererCapabilities& ThreadProxy::GetRendererCapabilities() const {
281 DCHECK(IsMainThread());
282 DCHECK(!layer_tree_host()->output_surface_lost());
283 return main().renderer_capabilities_main_thread_copy;
286 void ThreadProxy::SetNeedsAnimate() {
287 DCHECK(IsMainThread());
288 if (main().animate_requested)
289 return;
291 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimate");
292 main().animate_requested = true;
293 SendCommitRequestToImplThreadIfNeeded();
296 void ThreadProxy::SetNeedsUpdateLayers() {
297 DCHECK(IsMainThread());
299 if (main().commit_request_sent_to_impl_thread)
300 return;
301 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsUpdateLayers");
303 SendCommitRequestToImplThreadIfNeeded();
306 void ThreadProxy::SetNeedsCommit() {
307 DCHECK(IsMainThread());
308 // Unconditionally set here to handle SetNeedsCommit calls during a commit.
309 main().can_cancel_commit = false;
311 if (main().commit_requested)
312 return;
313 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommit");
314 main().commit_requested = true;
316 SendCommitRequestToImplThreadIfNeeded();
319 void ThreadProxy::UpdateRendererCapabilitiesOnImplThread() {
320 DCHECK(IsImplThread());
321 Proxy::MainThreadTaskRunner()->PostTask(
322 FROM_HERE,
323 base::Bind(&ThreadProxy::SetRendererCapabilitiesMainThreadCopy,
324 main_thread_weak_ptr_,
325 impl()
326 .layer_tree_host_impl->GetRendererCapabilities()
327 .MainThreadCapabilities()));
330 void ThreadProxy::DidLoseOutputSurfaceOnImplThread() {
331 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurfaceOnImplThread");
332 DCHECK(IsImplThread());
333 Proxy::MainThreadTaskRunner()->PostTask(
334 FROM_HERE,
335 base::Bind(&ThreadProxy::DidLoseOutputSurface, main_thread_weak_ptr_));
336 impl().scheduler->DidLoseOutputSurface();
339 void ThreadProxy::CommitVSyncParameters(base::TimeTicks timebase,
340 base::TimeDelta interval) {
341 impl().scheduler->CommitVSyncParameters(timebase, interval);
344 void ThreadProxy::SetEstimatedParentDrawTime(base::TimeDelta draw_time) {
345 impl().scheduler->SetEstimatedParentDrawTime(draw_time);
348 void ThreadProxy::SetMaxSwapsPendingOnImplThread(int max) {
349 impl().scheduler->SetMaxSwapsPending(max);
352 void ThreadProxy::DidSwapBuffersOnImplThread() {
353 impl().scheduler->DidSwapBuffers();
356 void ThreadProxy::DidSwapBuffersCompleteOnImplThread() {
357 TRACE_EVENT0("cc", "ThreadProxy::DidSwapBuffersCompleteOnImplThread");
358 DCHECK(IsImplThread());
359 impl().scheduler->DidSwapBuffersComplete();
360 Proxy::MainThreadTaskRunner()->PostTask(
361 FROM_HERE,
362 base::Bind(&ThreadProxy::DidCompleteSwapBuffers, main_thread_weak_ptr_));
365 void ThreadProxy::SetNeedsBeginFrame(bool enable) {
366 TRACE_EVENT1("cc", "ThreadProxy::SetNeedsBeginFrame", "enable", enable);
367 impl().layer_tree_host_impl->SetNeedsBeginFrame(enable);
368 UpdateBackgroundAnimateTicking();
371 void ThreadProxy::BeginFrame(const BeginFrameArgs& args) {
372 impl().scheduler->BeginFrame(args);
375 void ThreadProxy::WillBeginImplFrame(const BeginFrameArgs& args) {
376 impl().layer_tree_host_impl->WillBeginImplFrame(args);
379 void ThreadProxy::OnCanDrawStateChanged(bool can_draw) {
380 TRACE_EVENT1(
381 "cc", "ThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw);
382 DCHECK(IsImplThread());
383 impl().scheduler->SetCanDraw(can_draw);
384 UpdateBackgroundAnimateTicking();
387 void ThreadProxy::NotifyReadyToActivate() {
388 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToActivate");
389 impl().scheduler->NotifyReadyToActivate();
392 void ThreadProxy::SetNeedsCommitOnImplThread() {
393 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommitOnImplThread");
394 DCHECK(IsImplThread());
395 impl().scheduler->SetNeedsCommit();
398 void ThreadProxy::PostAnimationEventsToMainThreadOnImplThread(
399 scoped_ptr<AnimationEventsVector> events) {
400 TRACE_EVENT0("cc",
401 "ThreadProxy::PostAnimationEventsToMainThreadOnImplThread");
402 DCHECK(IsImplThread());
403 Proxy::MainThreadTaskRunner()->PostTask(
404 FROM_HERE,
405 base::Bind(&ThreadProxy::SetAnimationEvents,
406 main_thread_weak_ptr_,
407 base::Passed(&events)));
410 bool ThreadProxy::ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
411 int priority_cutoff) {
412 DCHECK(IsImplThread());
414 if (!impl().contents_texture_manager)
415 return false;
416 if (!impl().layer_tree_host_impl->resource_provider())
417 return false;
419 bool reduce_result =
420 impl().contents_texture_manager->ReduceMemoryOnImplThread(
421 limit_bytes,
422 priority_cutoff,
423 impl().layer_tree_host_impl->resource_provider());
424 if (!reduce_result)
425 return false;
427 // The texture upload queue may reference textures that were just purged,
428 // clear them from the queue.
429 if (impl().current_resource_update_controller) {
430 impl()
431 .current_resource_update_controller->DiscardUploadsToEvictedResources();
433 return true;
436 bool ThreadProxy::IsInsideDraw() { return impl().inside_draw; }
438 void ThreadProxy::SetNeedsRedraw(const gfx::Rect& damage_rect) {
439 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedraw");
440 DCHECK(IsMainThread());
441 Proxy::ImplThreadTaskRunner()->PostTask(
442 FROM_HERE,
443 base::Bind(&ThreadProxy::SetNeedsRedrawRectOnImplThread,
444 impl_thread_weak_ptr_,
445 damage_rect));
448 void ThreadProxy::SetNextCommitWaitsForActivation() {
449 DCHECK(IsMainThread());
450 DCHECK(!blocked_main().main_thread_inside_commit);
451 blocked_main().commit_waits_for_activation = true;
454 void ThreadProxy::SetDeferCommits(bool defer_commits) {
455 DCHECK(IsMainThread());
456 DCHECK_NE(main().defer_commits, defer_commits);
457 main().defer_commits = defer_commits;
459 if (main().defer_commits)
460 TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::SetDeferCommits", this);
461 else
462 TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::SetDeferCommits", this);
464 if (!main().defer_commits && main().pending_deferred_commit) {
465 Proxy::MainThreadTaskRunner()->PostTask(
466 FROM_HERE,
467 base::Bind(&ThreadProxy::BeginMainFrame,
468 main_thread_weak_ptr_,
469 base::Passed(&main().pending_deferred_commit)));
473 bool ThreadProxy::CommitRequested() const {
474 DCHECK(IsMainThread());
475 return main().commit_requested;
478 bool ThreadProxy::BeginMainFrameRequested() const {
479 DCHECK(IsMainThread());
480 return main().commit_request_sent_to_impl_thread;
483 void ThreadProxy::SetNeedsRedrawOnImplThread() {
484 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedrawOnImplThread");
485 DCHECK(IsImplThread());
486 impl().scheduler->SetNeedsRedraw();
489 void ThreadProxy::SetNeedsAnimateOnImplThread() {
490 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimateOnImplThread");
491 DCHECK(IsImplThread());
492 impl().scheduler->SetNeedsAnimate();
495 void ThreadProxy::SetNeedsManageTilesOnImplThread() {
496 DCHECK(IsImplThread());
497 impl().scheduler->SetNeedsManageTiles();
500 void ThreadProxy::SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) {
501 DCHECK(IsImplThread());
502 impl().layer_tree_host_impl->SetViewportDamage(damage_rect);
503 SetNeedsRedrawOnImplThread();
506 void ThreadProxy::SetSwapUsedIncompleteTileOnImplThread(
507 bool used_incomplete_tile) {
508 DCHECK(IsImplThread());
509 if (used_incomplete_tile) {
510 TRACE_EVENT_INSTANT0("cc",
511 "ThreadProxy::SetSwapUsedIncompleteTileOnImplThread",
512 TRACE_EVENT_SCOPE_THREAD);
514 impl().scheduler->SetSwapUsedIncompleteTile(used_incomplete_tile);
517 void ThreadProxy::DidInitializeVisibleTileOnImplThread() {
518 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeVisibleTileOnImplThread");
519 DCHECK(IsImplThread());
520 impl().scheduler->SetNeedsRedraw();
523 void ThreadProxy::MainThreadHasStoppedFlinging() {
524 DCHECK(IsMainThread());
525 Proxy::ImplThreadTaskRunner()->PostTask(
526 FROM_HERE,
527 base::Bind(&ThreadProxy::MainThreadHasStoppedFlingingOnImplThread,
528 impl_thread_weak_ptr_));
531 void ThreadProxy::MainThreadHasStoppedFlingingOnImplThread() {
532 DCHECK(IsImplThread());
533 impl().layer_tree_host_impl->MainThreadHasStoppedFlinging();
536 void ThreadProxy::NotifyInputThrottledUntilCommit() {
537 DCHECK(IsMainThread());
538 Proxy::ImplThreadTaskRunner()->PostTask(
539 FROM_HERE,
540 base::Bind(&ThreadProxy::SetInputThrottledUntilCommitOnImplThread,
541 impl_thread_weak_ptr_,
542 true));
545 void ThreadProxy::SetInputThrottledUntilCommitOnImplThread(bool is_throttled) {
546 DCHECK(IsImplThread());
547 if (is_throttled == impl().input_throttled_until_commit)
548 return;
549 impl().input_throttled_until_commit = is_throttled;
550 RenewTreePriority();
553 LayerTreeHost* ThreadProxy::layer_tree_host() {
554 return blocked_main().layer_tree_host;
557 const LayerTreeHost* ThreadProxy::layer_tree_host() const {
558 return blocked_main().layer_tree_host;
561 ThreadProxy::MainThreadOnly& ThreadProxy::main() {
562 DCHECK(IsMainThread());
563 return main_thread_only_vars_unsafe_;
565 const ThreadProxy::MainThreadOnly& ThreadProxy::main() const {
566 DCHECK(IsMainThread());
567 return main_thread_only_vars_unsafe_;
570 ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main() {
571 DCHECK(IsMainThread() || IsMainThreadBlocked());
572 return main_thread_or_blocked_vars_unsafe_;
575 const ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main()
576 const {
577 DCHECK(IsMainThread() || IsMainThreadBlocked());
578 return main_thread_or_blocked_vars_unsafe_;
581 ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() {
582 DCHECK(IsImplThread());
583 return compositor_thread_vars_unsafe_;
586 const ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() const {
587 DCHECK(IsImplThread());
588 return compositor_thread_vars_unsafe_;
591 void ThreadProxy::Start() {
592 DCHECK(IsMainThread());
593 DCHECK(Proxy::HasImplThread());
595 // Create LayerTreeHostImpl.
596 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
597 CompletionEvent completion;
598 Proxy::ImplThreadTaskRunner()->PostTask(
599 FROM_HERE,
600 base::Bind(&ThreadProxy::InitializeImplOnImplThread,
601 base::Unretained(this),
602 &completion));
603 completion.Wait();
605 main_thread_weak_ptr_ = main().weak_factory.GetWeakPtr();
607 main().started = true;
610 void ThreadProxy::Stop() {
611 TRACE_EVENT0("cc", "ThreadProxy::Stop");
612 DCHECK(IsMainThread());
613 DCHECK(main().started);
615 // Synchronously finishes pending GL operations and deletes the impl.
616 // The two steps are done as separate post tasks, so that tasks posted
617 // by the GL implementation due to the Finish can be executed by the
618 // renderer before shutting it down.
620 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
622 CompletionEvent completion;
623 Proxy::ImplThreadTaskRunner()->PostTask(
624 FROM_HERE,
625 base::Bind(&ThreadProxy::FinishGLOnImplThread,
626 impl_thread_weak_ptr_,
627 &completion));
628 completion.Wait();
631 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
633 CompletionEvent completion;
634 Proxy::ImplThreadTaskRunner()->PostTask(
635 FROM_HERE,
636 base::Bind(&ThreadProxy::LayerTreeHostClosedOnImplThread,
637 impl_thread_weak_ptr_,
638 &completion));
639 completion.Wait();
642 main().weak_factory.InvalidateWeakPtrs();
643 blocked_main().layer_tree_host = NULL;
644 main().started = false;
647 void ThreadProxy::ForceSerializeOnSwapBuffers() {
648 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
649 CompletionEvent completion;
650 Proxy::ImplThreadTaskRunner()->PostTask(
651 FROM_HERE,
652 base::Bind(&ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread,
653 impl_thread_weak_ptr_,
654 &completion));
655 completion.Wait();
658 void ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread(
659 CompletionEvent* completion) {
660 if (impl().layer_tree_host_impl->renderer())
661 impl().layer_tree_host_impl->renderer()->DoNoOp();
662 completion->Signal();
665 bool ThreadProxy::SupportsImplScrolling() const {
666 return true;
669 void ThreadProxy::SetDebugState(const LayerTreeDebugState& debug_state) {
670 Proxy::ImplThreadTaskRunner()->PostTask(
671 FROM_HERE,
672 base::Bind(&ThreadProxy::SetDebugStateOnImplThread,
673 impl_thread_weak_ptr_,
674 debug_state));
677 void ThreadProxy::SetDebugStateOnImplThread(
678 const LayerTreeDebugState& debug_state) {
679 DCHECK(IsImplThread());
680 impl().scheduler->SetContinuousPainting(debug_state.continuous_painting);
683 void ThreadProxy::FinishAllRenderingOnImplThread(CompletionEvent* completion) {
684 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread");
685 DCHECK(IsImplThread());
686 impl().layer_tree_host_impl->FinishAllRendering();
687 completion->Signal();
690 void ThreadProxy::ScheduledActionSendBeginMainFrame() {
691 unsigned int begin_frame_id = nextBeginFrameId++;
692 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
693 benchmark_instrumentation::kSendBeginFrame, begin_frame_id);
694 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state(
695 new BeginMainFrameAndCommitState);
696 begin_main_frame_state->begin_frame_id = begin_frame_id;
697 begin_main_frame_state->begin_frame_args =
698 impl().layer_tree_host_impl->CurrentBeginFrameArgs();
699 begin_main_frame_state->scroll_info =
700 impl().layer_tree_host_impl->ProcessScrollDeltas();
702 if (!impl().layer_tree_host_impl->settings().impl_side_painting) {
703 DCHECK_GT(impl().layer_tree_host_impl->memory_allocation_limit_bytes(), 0u);
705 begin_main_frame_state->memory_allocation_limit_bytes =
706 impl().layer_tree_host_impl->memory_allocation_limit_bytes();
707 begin_main_frame_state->memory_allocation_priority_cutoff =
708 impl().layer_tree_host_impl->memory_allocation_priority_cutoff();
709 begin_main_frame_state->evicted_ui_resources =
710 impl().layer_tree_host_impl->EvictedUIResourcesExist();
711 Proxy::MainThreadTaskRunner()->PostTask(
712 FROM_HERE,
713 base::Bind(&ThreadProxy::BeginMainFrame,
714 main_thread_weak_ptr_,
715 base::Passed(&begin_main_frame_state)));
716 devtools_instrumentation::DidRequestMainThreadFrame(
717 impl().layer_tree_host_id);
718 impl().timing_history.DidBeginMainFrame();
721 void ThreadProxy::BeginMainFrame(
722 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
723 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
724 benchmark_instrumentation::kDoBeginFrame,
725 begin_main_frame_state->begin_frame_id);
726 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
727 DCHECK(IsMainThread());
729 if (main().defer_commits) {
730 main().pending_deferred_commit = begin_main_frame_state.Pass();
731 layer_tree_host()->DidDeferCommit();
732 TRACE_EVENT_INSTANT0(
733 "cc", "EarlyOut_DeferCommits", TRACE_EVENT_SCOPE_THREAD);
734 return;
737 // If the commit finishes, LayerTreeHost will transfer its swap promises to
738 // LayerTreeImpl. The destructor of SwapPromiseChecker checks LayerTressHost's
739 // swap promises.
740 SwapPromiseChecker swap_promise_checker(layer_tree_host());
742 main().commit_requested = false;
743 main().commit_request_sent_to_impl_thread = false;
744 main().animate_requested = false;
746 if (!layer_tree_host()->visible()) {
747 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
748 bool did_handle = false;
749 Proxy::ImplThreadTaskRunner()->PostTask(
750 FROM_HERE,
751 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
752 impl_thread_weak_ptr_,
753 did_handle));
754 return;
757 if (layer_tree_host()->output_surface_lost()) {
758 TRACE_EVENT_INSTANT0(
759 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD);
760 bool did_handle = false;
761 Proxy::ImplThreadTaskRunner()->PostTask(
762 FROM_HERE,
763 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
764 impl_thread_weak_ptr_,
765 did_handle));
766 return;
769 // Do not notify the impl thread of commit requests that occur during
770 // the apply/animate/layout part of the BeginMainFrameAndCommit process since
771 // those commit requests will get painted immediately. Once we have done
772 // the paint, main().commit_requested will be set to false to allow new commit
773 // requests to be scheduled.
774 // On the other hand, the animate_requested flag should remain cleared
775 // here so that any animation requests generated by the apply or animate
776 // callbacks will trigger another frame.
777 main().commit_requested = true;
778 main().commit_request_sent_to_impl_thread = true;
780 layer_tree_host()->ApplyScrollAndScale(
781 begin_main_frame_state->scroll_info.get());
783 layer_tree_host()->WillBeginMainFrame();
785 layer_tree_host()->BeginMainFrame(begin_main_frame_state->begin_frame_args);
786 layer_tree_host()->AnimateLayers(
787 begin_main_frame_state->begin_frame_args.frame_time);
788 blocked_main().last_monotonic_frame_begin_time =
789 begin_main_frame_state->begin_frame_args.frame_time;
791 // Unlink any backings that the impl thread has evicted, so that we know to
792 // re-paint them in UpdateLayers.
793 if (blocked_main().contents_texture_manager()) {
794 blocked_main().contents_texture_manager()->UnlinkAndClearEvictedBackings();
796 blocked_main().contents_texture_manager()->SetMaxMemoryLimitBytes(
797 begin_main_frame_state->memory_allocation_limit_bytes);
798 blocked_main().contents_texture_manager()->SetExternalPriorityCutoff(
799 begin_main_frame_state->memory_allocation_priority_cutoff);
802 // Recreate all UI resources if there were evicted UI resources when the impl
803 // thread initiated the commit.
804 if (begin_main_frame_state->evicted_ui_resources)
805 layer_tree_host()->RecreateUIResources();
807 layer_tree_host()->Layout();
808 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
810 // Clear the commit flag after updating animations and layout here --- objects
811 // that only layout when painted will trigger another SetNeedsCommit inside
812 // UpdateLayers.
813 main().commit_requested = false;
814 main().commit_request_sent_to_impl_thread = false;
815 bool can_cancel_this_commit =
816 main().can_cancel_commit && !begin_main_frame_state->evicted_ui_resources;
817 main().can_cancel_commit = true;
819 scoped_ptr<ResourceUpdateQueue> queue =
820 make_scoped_ptr(new ResourceUpdateQueue);
822 bool updated = layer_tree_host()->UpdateLayers(queue.get());
824 layer_tree_host()->WillCommit();
826 // Before calling animate, we set main().animate_requested to false. If it is
827 // true now, it means SetNeedAnimate was called again, but during a state when
828 // main().commit_request_sent_to_impl_thread = true. We need to force that
829 // call to happen again now so that the commit request is sent to the impl
830 // thread.
831 if (main().animate_requested) {
832 // Forces SetNeedsAnimate to consider posting a commit task.
833 main().animate_requested = false;
834 SetNeedsAnimate();
837 if (!updated && can_cancel_this_commit) {
838 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
839 bool did_handle = true;
840 Proxy::ImplThreadTaskRunner()->PostTask(
841 FROM_HERE,
842 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
843 impl_thread_weak_ptr_,
844 did_handle));
846 // Although the commit is internally aborted, this is because it has been
847 // detected to be a no-op. From the perspective of an embedder, this commit
848 // went through, and input should no longer be throttled, etc.
849 layer_tree_host()->CommitComplete();
850 layer_tree_host()->DidBeginMainFrame();
851 layer_tree_host()->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE);
852 return;
855 // Notify the impl thread that the main thread is ready to commit. This will
856 // begin the commit process, which is blocking from the main thread's
857 // point of view, but asynchronously performed on the impl thread,
858 // coordinated by the Scheduler.
860 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit");
862 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
864 // This CapturePostTasks should be destroyed before CommitComplete() is
865 // called since that goes out to the embedder, and we want the embedder
866 // to receive its callbacks before that.
867 BlockingTaskRunner::CapturePostTasks blocked;
869 CompletionEvent completion;
870 Proxy::ImplThreadTaskRunner()->PostTask(
871 FROM_HERE,
872 base::Bind(&ThreadProxy::StartCommitOnImplThread,
873 impl_thread_weak_ptr_,
874 &completion,
875 queue.release()));
876 completion.Wait();
878 RenderingStatsInstrumentation* stats_instrumentation =
879 layer_tree_host()->rendering_stats_instrumentation();
880 benchmark_instrumentation::IssueMainThreadRenderingStatsEvent(
881 stats_instrumentation->main_thread_rendering_stats());
882 stats_instrumentation->AccumulateAndClearMainThreadStats();
885 layer_tree_host()->CommitComplete();
886 layer_tree_host()->DidBeginMainFrame();
889 void ThreadProxy::StartCommitOnImplThread(CompletionEvent* completion,
890 ResourceUpdateQueue* raw_queue) {
891 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread");
892 DCHECK(!impl().commit_completion_event);
893 DCHECK(IsImplThread() && IsMainThreadBlocked());
894 DCHECK(impl().scheduler);
895 DCHECK(impl().scheduler->CommitPending());
897 if (!impl().layer_tree_host_impl) {
898 TRACE_EVENT_INSTANT0(
899 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD);
900 completion->Signal();
901 return;
904 // Ideally, we should inform to impl thread when BeginMainFrame is started.
905 // But, we can avoid a PostTask in here.
906 impl().scheduler->NotifyBeginMainFrameStarted();
908 scoped_ptr<ResourceUpdateQueue> queue(raw_queue);
910 if (impl().contents_texture_manager) {
911 DCHECK_EQ(impl().contents_texture_manager,
912 blocked_main().contents_texture_manager());
913 } else {
914 // Cache this pointer that was created on the main thread side to avoid a
915 // data race between creating it and using it on the compositor thread.
916 impl().contents_texture_manager = blocked_main().contents_texture_manager();
919 if (impl().contents_texture_manager) {
920 if (impl().contents_texture_manager->LinkedEvictedBackingsExist()) {
921 // Clear any uploads we were making to textures linked to evicted
922 // resources
923 queue->ClearUploadsToEvictedResources();
924 // Some textures in the layer tree are invalid. Kick off another commit
925 // to fill them again.
926 SetNeedsCommitOnImplThread();
929 impl().contents_texture_manager->PushTexturePrioritiesToBackings();
932 impl().commit_completion_event = completion;
933 impl().current_resource_update_controller = ResourceUpdateController::Create(
934 this,
935 Proxy::ImplThreadTaskRunner(),
936 queue.Pass(),
937 impl().layer_tree_host_impl->resource_provider());
938 impl().current_resource_update_controller->PerformMoreUpdates(
939 impl().scheduler->AnticipatedDrawTime());
942 void ThreadProxy::BeginMainFrameAbortedOnImplThread(bool did_handle) {
943 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread");
944 DCHECK(IsImplThread());
945 DCHECK(impl().scheduler);
946 DCHECK(impl().scheduler->CommitPending());
947 DCHECK(!impl().layer_tree_host_impl->pending_tree());
949 if (did_handle)
950 SetInputThrottledUntilCommitOnImplThread(false);
951 impl().layer_tree_host_impl->BeginMainFrameAborted(did_handle);
952 impl().scheduler->BeginMainFrameAborted(did_handle);
955 void ThreadProxy::ScheduledActionAnimate() {
956 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate");
957 DCHECK(IsImplThread());
959 if (!impl().animations_frozen_until_next_draw) {
960 impl().animation_time =
961 impl().layer_tree_host_impl->CurrentBeginFrameArgs().frame_time;
963 impl().layer_tree_host_impl->Animate(impl().animation_time);
964 impl().did_commit_after_animating = false;
967 void ThreadProxy::ScheduledActionCommit() {
968 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit");
969 DCHECK(IsImplThread());
970 DCHECK(IsMainThreadBlocked());
971 DCHECK(impl().commit_completion_event);
972 DCHECK(impl().current_resource_update_controller);
974 // Complete all remaining texture updates.
975 impl().current_resource_update_controller->Finalize();
976 impl().current_resource_update_controller.reset();
978 if (impl().animations_frozen_until_next_draw) {
979 impl().animation_time = std::max(
980 impl().animation_time, blocked_main().last_monotonic_frame_begin_time);
982 impl().did_commit_after_animating = true;
984 blocked_main().main_thread_inside_commit = true;
985 impl().layer_tree_host_impl->BeginCommit();
986 layer_tree_host()->BeginCommitOnImplThread(impl().layer_tree_host_impl.get());
987 layer_tree_host()->FinishCommitOnImplThread(
988 impl().layer_tree_host_impl.get());
989 blocked_main().main_thread_inside_commit = false;
991 bool hold_commit = layer_tree_host()->settings().impl_side_painting &&
992 blocked_main().commit_waits_for_activation;
993 blocked_main().commit_waits_for_activation = false;
995 if (hold_commit) {
996 // For some layer types in impl-side painting, the commit is held until
997 // the sync tree is activated. It's also possible that the
998 // sync tree has already activated if there was no work to be done.
999 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD);
1000 impl().completion_event_for_commit_held_on_tree_activation =
1001 impl().commit_completion_event;
1002 impl().commit_completion_event = NULL;
1003 } else {
1004 impl().commit_completion_event->Signal();
1005 impl().commit_completion_event = NULL;
1008 // Delay this step until afer the main thread has been released as it's
1009 // often a good bit of work to update the tree and prepare the new frame.
1010 impl().layer_tree_host_impl->CommitComplete();
1012 SetInputThrottledUntilCommitOnImplThread(false);
1014 UpdateBackgroundAnimateTicking();
1016 impl().next_frame_is_newly_committed_frame = true;
1018 impl().timing_history.DidCommit();
1021 void ThreadProxy::ScheduledActionUpdateVisibleTiles() {
1022 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionUpdateVisibleTiles");
1023 DCHECK(IsImplThread());
1024 impl().layer_tree_host_impl->UpdateVisibleTiles();
1027 void ThreadProxy::ScheduledActionActivateSyncTree() {
1028 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree");
1029 DCHECK(IsImplThread());
1030 impl().layer_tree_host_impl->ActivateSyncTree();
1033 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() {
1034 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation");
1035 DCHECK(IsImplThread());
1036 Proxy::MainThreadTaskRunner()->PostTask(
1037 FROM_HERE,
1038 base::Bind(&ThreadProxy::CreateAndInitializeOutputSurface,
1039 main_thread_weak_ptr_));
1042 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) {
1043 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap");
1044 DrawResult result;
1046 DCHECK(IsImplThread());
1047 DCHECK(impl().layer_tree_host_impl.get());
1049 impl().timing_history.DidStartDrawing();
1050 base::AutoReset<bool> mark_inside(&impl().inside_draw, true);
1052 if (impl().did_commit_after_animating) {
1053 impl().layer_tree_host_impl->Animate(impl().animation_time);
1054 impl().did_commit_after_animating = false;
1057 if (impl().layer_tree_host_impl->pending_tree())
1058 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties();
1060 // This method is called on a forced draw, regardless of whether we are able
1061 // to produce a frame, as the calling site on main thread is blocked until its
1062 // request completes, and we signal completion here. If CanDraw() is false, we
1063 // will indicate success=false to the caller, but we must still signal
1064 // completion to avoid deadlock.
1066 // We guard PrepareToDraw() with CanDraw() because it always returns a valid
1067 // frame, so can only be used when such a frame is possible. Since
1068 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
1069 // CanDraw() as well.
1071 LayerTreeHostImpl::FrameData frame;
1072 bool draw_frame = false;
1074 if (impl().layer_tree_host_impl->CanDraw()) {
1075 result = impl().layer_tree_host_impl->PrepareToDraw(&frame);
1076 draw_frame = forced_draw || result == DRAW_SUCCESS;
1077 } else {
1078 result = DRAW_ABORTED_CANT_DRAW;
1081 if (draw_frame) {
1082 impl().layer_tree_host_impl->DrawLayers(
1083 &frame, impl().scheduler->LastBeginImplFrameTime());
1084 result = DRAW_SUCCESS;
1085 impl().animations_frozen_until_next_draw = false;
1086 } else if (result == DRAW_ABORTED_CHECKERBOARD_ANIMATIONS &&
1087 !impl().layer_tree_host_impl->settings().impl_side_painting) {
1088 // Without impl-side painting, the animated layer that is checkerboarding
1089 // will continue to checkerboard until the next commit. If this layer
1090 // continues to move during the commit, it may continue to checkerboard
1091 // after the commit since the region rasterized during the commit will not
1092 // match the region that is currently visible; eventually this
1093 // checkerboarding will be displayed when we force a draw. To avoid this,
1094 // we freeze animations until we successfully draw.
1095 impl().animations_frozen_until_next_draw = true;
1096 } else {
1097 DCHECK_NE(DRAW_SUCCESS, result);
1099 impl().layer_tree_host_impl->DidDrawAllLayers(frame);
1101 bool start_ready_animations = draw_frame;
1102 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations);
1104 if (draw_frame) {
1105 bool did_request_swap = impl().layer_tree_host_impl->SwapBuffers(frame);
1107 // We don't know if we have incomplete tiles if we didn't actually swap.
1108 if (did_request_swap) {
1109 DCHECK(!frame.has_no_damage);
1110 SetSwapUsedIncompleteTileOnImplThread(frame.contains_incomplete_tile);
1114 // Tell the main thread that the the newly-commited frame was drawn.
1115 if (impl().next_frame_is_newly_committed_frame) {
1116 impl().next_frame_is_newly_committed_frame = false;
1117 Proxy::MainThreadTaskRunner()->PostTask(
1118 FROM_HERE,
1119 base::Bind(&ThreadProxy::DidCommitAndDrawFrame, main_thread_weak_ptr_));
1122 if (result == DRAW_SUCCESS)
1123 impl().timing_history.DidFinishDrawing();
1125 DCHECK_NE(INVALID_RESULT, result);
1126 return result;
1129 void ThreadProxy::ScheduledActionManageTiles() {
1130 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionManageTiles");
1131 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1132 impl().layer_tree_host_impl->ManageTiles();
1135 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() {
1136 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap");
1138 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to
1139 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should
1140 // never generate this call when it can't draw.
1141 DCHECK(impl().layer_tree_host_impl->CanDraw());
1143 bool forced_draw = false;
1144 return DrawSwapInternal(forced_draw);
1147 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() {
1148 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced");
1149 bool forced_draw = true;
1150 return DrawSwapInternal(forced_draw);
1153 void ThreadProxy::DidAnticipatedDrawTimeChange(base::TimeTicks time) {
1154 if (impl().current_resource_update_controller)
1155 impl().current_resource_update_controller->PerformMoreUpdates(time);
1158 base::TimeDelta ThreadProxy::DrawDurationEstimate() {
1159 return impl().timing_history.DrawDurationEstimate();
1162 base::TimeDelta ThreadProxy::BeginMainFrameToCommitDurationEstimate() {
1163 return impl().timing_history.BeginMainFrameToCommitDurationEstimate();
1166 base::TimeDelta ThreadProxy::CommitToActivateDurationEstimate() {
1167 return impl().timing_history.CommitToActivateDurationEstimate();
1170 void ThreadProxy::DidBeginImplFrameDeadline() {
1171 impl().layer_tree_host_impl->ResetCurrentBeginFrameArgsForNextFrame();
1174 void ThreadProxy::ReadyToFinalizeTextureUpdates() {
1175 DCHECK(IsImplThread());
1176 impl().scheduler->NotifyReadyToCommit();
1179 void ThreadProxy::DidCommitAndDrawFrame() {
1180 DCHECK(IsMainThread());
1181 layer_tree_host()->DidCommitAndDrawFrame();
1184 void ThreadProxy::DidCompleteSwapBuffers() {
1185 DCHECK(IsMainThread());
1186 layer_tree_host()->DidCompleteSwapBuffers();
1189 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
1190 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents");
1191 DCHECK(IsMainThread());
1192 layer_tree_host()->SetAnimationEvents(events.Pass());
1195 void ThreadProxy::InitializeImplOnImplThread(CompletionEvent* completion) {
1196 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread");
1197 DCHECK(IsImplThread());
1198 impl().layer_tree_host_impl =
1199 layer_tree_host()->CreateLayerTreeHostImpl(this);
1200 SchedulerSettings scheduler_settings(layer_tree_host()->settings());
1201 impl().scheduler = Scheduler::Create(this,
1202 scheduler_settings,
1203 impl().layer_tree_host_id,
1204 ImplThreadTaskRunner());
1205 impl().scheduler->SetVisible(impl().layer_tree_host_impl->visible());
1207 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr();
1208 completion->Signal();
1211 void ThreadProxy::DeleteContentsTexturesOnImplThread(
1212 CompletionEvent* completion) {
1213 TRACE_EVENT0("cc", "ThreadProxy::DeleteContentsTexturesOnImplThread");
1214 DCHECK(IsImplThread());
1215 DCHECK(IsMainThreadBlocked());
1216 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1217 impl().layer_tree_host_impl->resource_provider());
1218 completion->Signal();
1221 void ThreadProxy::InitializeOutputSurfaceOnImplThread(
1222 scoped_ptr<OutputSurface> output_surface) {
1223 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread");
1224 DCHECK(IsImplThread());
1226 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get();
1227 bool success = host_impl->InitializeRenderer(output_surface.Pass());
1228 RendererCapabilities capabilities;
1229 if (success) {
1230 capabilities =
1231 host_impl->GetRendererCapabilities().MainThreadCapabilities();
1234 Proxy::MainThreadTaskRunner()->PostTask(
1235 FROM_HERE,
1236 base::Bind(&ThreadProxy::DidInitializeOutputSurface,
1237 main_thread_weak_ptr_,
1238 success,
1239 capabilities));
1241 if (success)
1242 impl().scheduler->DidCreateAndInitializeOutputSurface();
1245 void ThreadProxy::FinishGLOnImplThread(CompletionEvent* completion) {
1246 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread");
1247 DCHECK(IsImplThread());
1248 if (impl().layer_tree_host_impl->output_surface()) {
1249 ContextProvider* context_provider =
1250 impl().layer_tree_host_impl->output_surface()->context_provider();
1251 if (context_provider)
1252 context_provider->ContextGL()->Finish();
1254 completion->Signal();
1257 void ThreadProxy::LayerTreeHostClosedOnImplThread(CompletionEvent* completion) {
1258 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread");
1259 DCHECK(IsImplThread());
1260 DCHECK(IsMainThreadBlocked());
1261 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1262 impl().layer_tree_host_impl->resource_provider());
1263 impl().current_resource_update_controller.reset();
1264 impl().layer_tree_host_impl->SetNeedsBeginFrame(false);
1265 impl().scheduler.reset();
1266 impl().layer_tree_host_impl.reset();
1267 impl().weak_factory.InvalidateWeakPtrs();
1268 impl().contents_texture_manager = NULL;
1269 completion->Signal();
1272 size_t ThreadProxy::MaxPartialTextureUpdates() const {
1273 return ResourceUpdateController::MaxPartialTextureUpdates();
1276 ThreadProxy::BeginMainFrameAndCommitState::BeginMainFrameAndCommitState()
1277 : memory_allocation_limit_bytes(0),
1278 memory_allocation_priority_cutoff(0),
1279 evicted_ui_resources(false) {}
1281 ThreadProxy::BeginMainFrameAndCommitState::~BeginMainFrameAndCommitState() {}
1283 void ThreadProxy::AsValueInto(base::debug::TracedValue* state) const {
1284 CompletionEvent completion;
1286 DebugScopedSetMainThreadBlocked main_thread_blocked(
1287 const_cast<ThreadProxy*>(this));
1288 scoped_refptr<base::debug::TracedValue> state_refptr(state);
1289 Proxy::ImplThreadTaskRunner()->PostTask(
1290 FROM_HERE,
1291 base::Bind(&ThreadProxy::AsValueOnImplThread,
1292 impl_thread_weak_ptr_,
1293 &completion,
1294 state_refptr));
1295 completion.Wait();
1299 void ThreadProxy::AsValueOnImplThread(CompletionEvent* completion,
1300 base::debug::TracedValue* state) const {
1301 state->BeginDictionary("layer_tree_host_impl");
1302 impl().layer_tree_host_impl->AsValueInto(state);
1303 state->EndDictionary();
1304 completion->Signal();
1307 bool ThreadProxy::MainFrameWillHappenForTesting() {
1308 DCHECK(IsMainThread());
1309 CompletionEvent completion;
1310 bool main_frame_will_happen = false;
1312 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1313 Proxy::ImplThreadTaskRunner()->PostTask(
1314 FROM_HERE,
1315 base::Bind(&ThreadProxy::MainFrameWillHappenOnImplThreadForTesting,
1316 impl_thread_weak_ptr_,
1317 &completion,
1318 &main_frame_will_happen));
1319 completion.Wait();
1321 return main_frame_will_happen;
1324 void ThreadProxy::MainFrameWillHappenOnImplThreadForTesting(
1325 CompletionEvent* completion,
1326 bool* main_frame_will_happen) {
1327 DCHECK(IsImplThread());
1328 if (impl().layer_tree_host_impl->output_surface()) {
1329 *main_frame_will_happen = impl().scheduler->MainFrameForTestingWillHappen();
1330 } else {
1331 *main_frame_will_happen = false;
1333 completion->Signal();
1336 void ThreadProxy::RenewTreePriority() {
1337 DCHECK(IsImplThread());
1338 bool smoothness_takes_priority =
1339 impl().layer_tree_host_impl->pinch_gesture_active() ||
1340 impl().layer_tree_host_impl->page_scale_animation_active() ||
1341 (impl().layer_tree_host_impl->IsCurrentlyScrolling() &&
1342 !impl().layer_tree_host_impl->scroll_affects_scroll_handler());
1344 // Schedule expiration if smoothness currently takes priority.
1345 if (smoothness_takes_priority)
1346 impl().smoothness_priority_expiration_notifier.Schedule();
1348 // We use the same priority for both trees by default.
1349 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES;
1351 // Smoothness takes priority if we have an expiration for it scheduled.
1352 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification())
1353 priority = SMOOTHNESS_TAKES_PRIORITY;
1355 // New content always takes priority when the active tree has
1356 // evicted resources or there is an invalid viewport size.
1357 if (impl().layer_tree_host_impl->active_tree()->ContentsTexturesPurged() ||
1358 impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() ||
1359 impl().layer_tree_host_impl->EvictedUIResourcesExist() ||
1360 impl().input_throttled_until_commit) {
1361 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active
1362 // tree might be freed. We need to set RequiresHighResToDraw to ensure that
1363 // high res tiles will be required to activate pending tree.
1364 impl().layer_tree_host_impl->active_tree()->SetRequiresHighResToDraw();
1365 priority = NEW_CONTENT_TAKES_PRIORITY;
1368 impl().layer_tree_host_impl->SetTreePriority(priority);
1369 impl().scheduler->SetSmoothnessTakesPriority(priority ==
1370 SMOOTHNESS_TAKES_PRIORITY);
1372 // Notify the the client of this compositor via the output surface.
1373 // TODO(epenner): Route this to compositor-thread instead of output-surface
1374 // after GTFO refactor of compositor-thread (http://crbug/170828).
1375 if (impl().layer_tree_host_impl->output_surface()) {
1376 impl()
1377 .layer_tree_host_impl->output_surface()
1378 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY);
1382 void ThreadProxy::PostDelayedScrollbarFadeOnImplThread(
1383 const base::Closure& start_fade,
1384 base::TimeDelta delay) {
1385 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, start_fade, delay);
1388 void ThreadProxy::DidActivateSyncTree() {
1389 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread");
1390 DCHECK(IsImplThread());
1392 if (impl().completion_event_for_commit_held_on_tree_activation) {
1393 TRACE_EVENT_INSTANT0(
1394 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD);
1395 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1396 impl().completion_event_for_commit_held_on_tree_activation->Signal();
1397 impl().completion_event_for_commit_held_on_tree_activation = NULL;
1400 UpdateBackgroundAnimateTicking();
1402 impl().timing_history.DidActivateSyncTree();
1405 void ThreadProxy::DidManageTiles() {
1406 DCHECK(IsImplThread());
1407 impl().scheduler->DidManageTiles();
1410 } // namespace cc