Task Manager: Remove goat teleporter.
[chromium-blink-merge.git] / cc / trees / thread_proxy.cc
blobd015d283f6bd94d431d6e50dc61f2fe800162e78
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 "cc/trees/scoped_abort_remaining_swap_promises.h"
29 #include "gpu/command_buffer/client/gles2_interface.h"
30 #include "ui/gfx/frame_time.h"
32 namespace cc {
34 namespace {
36 // Measured in seconds.
37 const double kSmoothnessTakesPriorityExpirationDelay = 0.25;
39 unsigned int nextBeginFrameId = 0;
41 } // namespace
43 struct ThreadProxy::SchedulerStateRequest {
44 CompletionEvent completion;
45 scoped_ptr<base::Value> state;
48 scoped_ptr<Proxy> ThreadProxy::Create(
49 LayerTreeHost* layer_tree_host,
50 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
51 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) {
52 return make_scoped_ptr(new ThreadProxy(layer_tree_host,
53 main_task_runner,
54 impl_task_runner)).PassAs<Proxy>();
57 ThreadProxy::ThreadProxy(
58 LayerTreeHost* layer_tree_host,
59 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
60 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner)
61 : Proxy(main_task_runner, impl_task_runner),
62 main_thread_only_vars_unsafe_(this, layer_tree_host->id()),
63 main_thread_or_blocked_vars_unsafe_(layer_tree_host),
64 compositor_thread_vars_unsafe_(
65 this,
66 layer_tree_host->id(),
67 layer_tree_host->rendering_stats_instrumentation()) {
68 TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy");
69 DCHECK(IsMainThread());
70 DCHECK(this->layer_tree_host());
73 ThreadProxy::MainThreadOnly::MainThreadOnly(ThreadProxy* proxy,
74 int layer_tree_host_id)
75 : layer_tree_host_id(layer_tree_host_id),
76 animate_requested(false),
77 commit_requested(false),
78 commit_request_sent_to_impl_thread(false),
79 started(false),
80 manage_tiles_pending(false),
81 can_cancel_commit(true),
82 defer_commits(false),
83 weak_factory(proxy) {}
85 ThreadProxy::MainThreadOnly::~MainThreadOnly() {}
87 ThreadProxy::MainThreadOrBlockedMainThread::MainThreadOrBlockedMainThread(
88 LayerTreeHost* host)
89 : layer_tree_host(host),
90 commit_waits_for_activation(false),
91 main_thread_inside_commit(false) {}
93 ThreadProxy::MainThreadOrBlockedMainThread::~MainThreadOrBlockedMainThread() {}
95 PrioritizedResourceManager*
96 ThreadProxy::MainThreadOrBlockedMainThread::contents_texture_manager() {
97 return layer_tree_host->contents_texture_manager();
100 ThreadProxy::CompositorThreadOnly::CompositorThreadOnly(
101 ThreadProxy* proxy,
102 int layer_tree_host_id,
103 RenderingStatsInstrumentation* rendering_stats_instrumentation)
104 : layer_tree_host_id(layer_tree_host_id),
105 contents_texture_manager(NULL),
106 commit_completion_event(NULL),
107 completion_event_for_commit_held_on_tree_activation(NULL),
108 next_frame_is_newly_committed_frame(false),
109 inside_draw(false),
110 input_throttled_until_commit(false),
111 animations_frozen_until_next_draw(false),
112 did_commit_after_animating(false),
113 smoothness_priority_expiration_notifier(
114 proxy->ImplThreadTaskRunner(),
115 base::Bind(&ThreadProxy::RenewTreePriority, base::Unretained(proxy)),
116 base::TimeDelta::FromMilliseconds(
117 kSmoothnessTakesPriorityExpirationDelay * 1000)),
118 timing_history(rendering_stats_instrumentation),
119 weak_factory(proxy) {
122 ThreadProxy::CompositorThreadOnly::~CompositorThreadOnly() {}
124 ThreadProxy::~ThreadProxy() {
125 TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy");
126 DCHECK(IsMainThread());
127 DCHECK(!main().started);
130 void ThreadProxy::FinishAllRendering() {
131 DCHECK(Proxy::IsMainThread());
132 DCHECK(!main().defer_commits);
134 // Make sure all GL drawing is finished on the impl thread.
135 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
136 CompletionEvent completion;
137 Proxy::ImplThreadTaskRunner()->PostTask(
138 FROM_HERE,
139 base::Bind(&ThreadProxy::FinishAllRenderingOnImplThread,
140 impl_thread_weak_ptr_,
141 &completion));
142 completion.Wait();
145 bool ThreadProxy::IsStarted() const {
146 DCHECK(Proxy::IsMainThread());
147 return main().started;
150 void ThreadProxy::SetLayerTreeHostClientReady() {
151 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReady");
152 Proxy::ImplThreadTaskRunner()->PostTask(
153 FROM_HERE,
154 base::Bind(&ThreadProxy::SetLayerTreeHostClientReadyOnImplThread,
155 impl_thread_weak_ptr_));
158 void ThreadProxy::SetLayerTreeHostClientReadyOnImplThread() {
159 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReadyOnImplThread");
160 impl().scheduler->SetCanStart();
163 void ThreadProxy::SetVisible(bool visible) {
164 TRACE_EVENT0("cc", "ThreadProxy::SetVisible");
165 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
167 CompletionEvent completion;
168 Proxy::ImplThreadTaskRunner()->PostTask(
169 FROM_HERE,
170 base::Bind(&ThreadProxy::SetVisibleOnImplThread,
171 impl_thread_weak_ptr_,
172 &completion,
173 visible));
174 completion.Wait();
177 void ThreadProxy::SetVisibleOnImplThread(CompletionEvent* completion,
178 bool visible) {
179 TRACE_EVENT0("cc", "ThreadProxy::SetVisibleOnImplThread");
180 impl().layer_tree_host_impl->SetVisible(visible);
181 impl().scheduler->SetVisible(visible);
182 UpdateBackgroundAnimateTicking();
183 completion->Signal();
186 void ThreadProxy::UpdateBackgroundAnimateTicking() {
187 bool should_background_tick =
188 !impl().scheduler->WillDrawIfNeeded() &&
189 impl().layer_tree_host_impl->active_tree()->root_layer();
190 impl().layer_tree_host_impl->UpdateBackgroundAnimateTicking(
191 should_background_tick);
192 if (should_background_tick)
193 impl().animations_frozen_until_next_draw = false;
196 void ThreadProxy::DidLoseOutputSurface() {
197 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurface");
198 DCHECK(IsMainThread());
199 layer_tree_host()->DidLoseOutputSurface();
202 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
204 // Return lost resources to their owners immediately.
205 BlockingTaskRunner::CapturePostTasks blocked(
206 blocking_main_thread_task_runner());
208 CompletionEvent completion;
209 Proxy::ImplThreadTaskRunner()->PostTask(
210 FROM_HERE,
211 base::Bind(&ThreadProxy::DeleteContentsTexturesOnImplThread,
212 impl_thread_weak_ptr_,
213 &completion));
214 completion.Wait();
218 void ThreadProxy::CreateAndInitializeOutputSurface() {
219 TRACE_EVENT0("cc", "ThreadProxy::DoCreateAndInitializeOutputSurface");
220 DCHECK(IsMainThread());
222 scoped_ptr<OutputSurface> output_surface =
223 layer_tree_host()->CreateOutputSurface();
225 if (output_surface) {
226 Proxy::ImplThreadTaskRunner()->PostTask(
227 FROM_HERE,
228 base::Bind(&ThreadProxy::InitializeOutputSurfaceOnImplThread,
229 impl_thread_weak_ptr_,
230 base::Passed(&output_surface)));
231 return;
234 DidInitializeOutputSurface(false, RendererCapabilities());
237 void ThreadProxy::DidInitializeOutputSurface(
238 bool success,
239 const RendererCapabilities& capabilities) {
240 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeOutputSurface");
241 DCHECK(IsMainThread());
242 main().renderer_capabilities_main_thread_copy = capabilities;
243 layer_tree_host()->OnCreateAndInitializeOutputSurfaceAttempted(success);
245 if (!success) {
246 Proxy::MainThreadTaskRunner()->PostTask(
247 FROM_HERE,
248 base::Bind(&ThreadProxy::CreateAndInitializeOutputSurface,
249 main_thread_weak_ptr_));
253 void ThreadProxy::SetRendererCapabilitiesMainThreadCopy(
254 const RendererCapabilities& capabilities) {
255 main().renderer_capabilities_main_thread_copy = capabilities;
258 void ThreadProxy::SendCommitRequestToImplThreadIfNeeded() {
259 DCHECK(IsMainThread());
260 if (main().commit_request_sent_to_impl_thread)
261 return;
262 main().commit_request_sent_to_impl_thread = true;
263 Proxy::ImplThreadTaskRunner()->PostTask(
264 FROM_HERE,
265 base::Bind(&ThreadProxy::SetNeedsCommitOnImplThread,
266 impl_thread_weak_ptr_));
269 const RendererCapabilities& ThreadProxy::GetRendererCapabilities() const {
270 DCHECK(IsMainThread());
271 DCHECK(!layer_tree_host()->output_surface_lost());
272 return main().renderer_capabilities_main_thread_copy;
275 void ThreadProxy::SetNeedsAnimate() {
276 DCHECK(IsMainThread());
277 if (main().animate_requested)
278 return;
280 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimate");
281 main().animate_requested = true;
282 SendCommitRequestToImplThreadIfNeeded();
285 void ThreadProxy::SetNeedsUpdateLayers() {
286 DCHECK(IsMainThread());
288 if (main().commit_request_sent_to_impl_thread)
289 return;
290 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsUpdateLayers");
292 SendCommitRequestToImplThreadIfNeeded();
295 void ThreadProxy::SetNeedsCommit() {
296 DCHECK(IsMainThread());
297 // Unconditionally set here to handle SetNeedsCommit calls during a commit.
298 main().can_cancel_commit = false;
300 if (main().commit_requested)
301 return;
302 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommit");
303 main().commit_requested = true;
305 SendCommitRequestToImplThreadIfNeeded();
308 void ThreadProxy::UpdateRendererCapabilitiesOnImplThread() {
309 DCHECK(IsImplThread());
310 Proxy::MainThreadTaskRunner()->PostTask(
311 FROM_HERE,
312 base::Bind(&ThreadProxy::SetRendererCapabilitiesMainThreadCopy,
313 main_thread_weak_ptr_,
314 impl()
315 .layer_tree_host_impl->GetRendererCapabilities()
316 .MainThreadCapabilities()));
319 void ThreadProxy::DidLoseOutputSurfaceOnImplThread() {
320 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurfaceOnImplThread");
321 DCHECK(IsImplThread());
322 Proxy::MainThreadTaskRunner()->PostTask(
323 FROM_HERE,
324 base::Bind(&ThreadProxy::DidLoseOutputSurface, main_thread_weak_ptr_));
325 impl().scheduler->DidLoseOutputSurface();
328 void ThreadProxy::CommitVSyncParameters(base::TimeTicks timebase,
329 base::TimeDelta interval) {
330 impl().scheduler->CommitVSyncParameters(timebase, interval);
333 void ThreadProxy::SetEstimatedParentDrawTime(base::TimeDelta draw_time) {
334 impl().scheduler->SetEstimatedParentDrawTime(draw_time);
337 void ThreadProxy::SetMaxSwapsPendingOnImplThread(int max) {
338 impl().scheduler->SetMaxSwapsPending(max);
341 void ThreadProxy::DidSwapBuffersOnImplThread() {
342 impl().scheduler->DidSwapBuffers();
345 void ThreadProxy::DidSwapBuffersCompleteOnImplThread() {
346 TRACE_EVENT0("cc", "ThreadProxy::DidSwapBuffersCompleteOnImplThread");
347 DCHECK(IsImplThread());
348 impl().scheduler->DidSwapBuffersComplete();
349 Proxy::MainThreadTaskRunner()->PostTask(
350 FROM_HERE,
351 base::Bind(&ThreadProxy::DidCompleteSwapBuffers, main_thread_weak_ptr_));
354 void ThreadProxy::SetNeedsBeginFrame(bool enable) {
355 TRACE_EVENT1("cc", "ThreadProxy::SetNeedsBeginFrame", "enable", enable);
356 impl().layer_tree_host_impl->SetNeedsBeginFrame(enable);
357 UpdateBackgroundAnimateTicking();
360 void ThreadProxy::BeginFrame(const BeginFrameArgs& args) {
361 impl().scheduler->BeginFrame(args);
364 void ThreadProxy::WillBeginImplFrame(const BeginFrameArgs& args) {
365 impl().layer_tree_host_impl->WillBeginImplFrame(args);
368 void ThreadProxy::OnCanDrawStateChanged(bool can_draw) {
369 TRACE_EVENT1(
370 "cc", "ThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw);
371 DCHECK(IsImplThread());
372 impl().scheduler->SetCanDraw(can_draw);
373 UpdateBackgroundAnimateTicking();
376 void ThreadProxy::NotifyReadyToActivate() {
377 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToActivate");
378 impl().scheduler->NotifyReadyToActivate();
381 void ThreadProxy::SetNeedsCommitOnImplThread() {
382 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommitOnImplThread");
383 DCHECK(IsImplThread());
384 impl().scheduler->SetNeedsCommit();
387 void ThreadProxy::PostAnimationEventsToMainThreadOnImplThread(
388 scoped_ptr<AnimationEventsVector> events) {
389 TRACE_EVENT0("cc",
390 "ThreadProxy::PostAnimationEventsToMainThreadOnImplThread");
391 DCHECK(IsImplThread());
392 Proxy::MainThreadTaskRunner()->PostTask(
393 FROM_HERE,
394 base::Bind(&ThreadProxy::SetAnimationEvents,
395 main_thread_weak_ptr_,
396 base::Passed(&events)));
399 bool ThreadProxy::ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
400 int priority_cutoff) {
401 DCHECK(IsImplThread());
403 if (!impl().contents_texture_manager)
404 return false;
405 if (!impl().layer_tree_host_impl->resource_provider())
406 return false;
408 bool reduce_result =
409 impl().contents_texture_manager->ReduceMemoryOnImplThread(
410 limit_bytes,
411 priority_cutoff,
412 impl().layer_tree_host_impl->resource_provider());
413 if (!reduce_result)
414 return false;
416 // The texture upload queue may reference textures that were just purged,
417 // clear them from the queue.
418 if (impl().current_resource_update_controller) {
419 impl()
420 .current_resource_update_controller->DiscardUploadsToEvictedResources();
422 return true;
425 bool ThreadProxy::IsInsideDraw() { return impl().inside_draw; }
427 void ThreadProxy::SetNeedsRedraw(const gfx::Rect& damage_rect) {
428 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedraw");
429 DCHECK(IsMainThread());
430 Proxy::ImplThreadTaskRunner()->PostTask(
431 FROM_HERE,
432 base::Bind(&ThreadProxy::SetNeedsRedrawRectOnImplThread,
433 impl_thread_weak_ptr_,
434 damage_rect));
437 void ThreadProxy::SetNextCommitWaitsForActivation() {
438 DCHECK(IsMainThread());
439 DCHECK(!blocked_main().main_thread_inside_commit);
440 blocked_main().commit_waits_for_activation = true;
443 void ThreadProxy::SetDeferCommits(bool defer_commits) {
444 DCHECK(IsMainThread());
445 if (main().defer_commits == defer_commits)
446 return;
448 main().defer_commits = defer_commits;
449 if (main().defer_commits)
450 TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::SetDeferCommits", this);
451 else
452 TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::SetDeferCommits", this);
454 if (!main().defer_commits && main().pending_deferred_commit) {
455 Proxy::MainThreadTaskRunner()->PostTask(
456 FROM_HERE,
457 base::Bind(&ThreadProxy::BeginMainFrame,
458 main_thread_weak_ptr_,
459 base::Passed(&main().pending_deferred_commit)));
463 bool ThreadProxy::CommitRequested() const {
464 DCHECK(IsMainThread());
465 return main().commit_requested;
468 bool ThreadProxy::BeginMainFrameRequested() const {
469 DCHECK(IsMainThread());
470 return main().commit_request_sent_to_impl_thread;
473 void ThreadProxy::SetNeedsRedrawOnImplThread() {
474 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedrawOnImplThread");
475 DCHECK(IsImplThread());
476 impl().scheduler->SetNeedsRedraw();
479 void ThreadProxy::SetNeedsAnimateOnImplThread() {
480 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimateOnImplThread");
481 DCHECK(IsImplThread());
482 impl().scheduler->SetNeedsAnimate();
485 void ThreadProxy::SetNeedsManageTilesOnImplThread() {
486 DCHECK(IsImplThread());
487 impl().scheduler->SetNeedsManageTiles();
490 void ThreadProxy::SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) {
491 DCHECK(IsImplThread());
492 impl().layer_tree_host_impl->SetViewportDamage(damage_rect);
493 SetNeedsRedrawOnImplThread();
496 void ThreadProxy::SetSwapUsedIncompleteTileOnImplThread(
497 bool used_incomplete_tile) {
498 DCHECK(IsImplThread());
499 if (used_incomplete_tile) {
500 TRACE_EVENT_INSTANT0("cc",
501 "ThreadProxy::SetSwapUsedIncompleteTileOnImplThread",
502 TRACE_EVENT_SCOPE_THREAD);
504 impl().scheduler->SetSwapUsedIncompleteTile(used_incomplete_tile);
507 void ThreadProxy::DidInitializeVisibleTileOnImplThread() {
508 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeVisibleTileOnImplThread");
509 DCHECK(IsImplThread());
510 impl().scheduler->SetNeedsRedraw();
513 void ThreadProxy::MainThreadHasStoppedFlinging() {
514 DCHECK(IsMainThread());
515 Proxy::ImplThreadTaskRunner()->PostTask(
516 FROM_HERE,
517 base::Bind(&ThreadProxy::MainThreadHasStoppedFlingingOnImplThread,
518 impl_thread_weak_ptr_));
521 void ThreadProxy::MainThreadHasStoppedFlingingOnImplThread() {
522 DCHECK(IsImplThread());
523 impl().layer_tree_host_impl->MainThreadHasStoppedFlinging();
526 void ThreadProxy::NotifyInputThrottledUntilCommit() {
527 DCHECK(IsMainThread());
528 Proxy::ImplThreadTaskRunner()->PostTask(
529 FROM_HERE,
530 base::Bind(&ThreadProxy::SetInputThrottledUntilCommitOnImplThread,
531 impl_thread_weak_ptr_,
532 true));
535 void ThreadProxy::SetInputThrottledUntilCommitOnImplThread(bool is_throttled) {
536 DCHECK(IsImplThread());
537 if (is_throttled == impl().input_throttled_until_commit)
538 return;
539 impl().input_throttled_until_commit = is_throttled;
540 RenewTreePriority();
543 LayerTreeHost* ThreadProxy::layer_tree_host() {
544 return blocked_main().layer_tree_host;
547 const LayerTreeHost* ThreadProxy::layer_tree_host() const {
548 return blocked_main().layer_tree_host;
551 ThreadProxy::MainThreadOnly& ThreadProxy::main() {
552 DCHECK(IsMainThread());
553 return main_thread_only_vars_unsafe_;
555 const ThreadProxy::MainThreadOnly& ThreadProxy::main() const {
556 DCHECK(IsMainThread());
557 return main_thread_only_vars_unsafe_;
560 ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main() {
561 DCHECK(IsMainThread() || IsMainThreadBlocked());
562 return main_thread_or_blocked_vars_unsafe_;
565 const ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main()
566 const {
567 DCHECK(IsMainThread() || IsMainThreadBlocked());
568 return main_thread_or_blocked_vars_unsafe_;
571 ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() {
572 DCHECK(IsImplThread());
573 return compositor_thread_vars_unsafe_;
576 const ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() const {
577 DCHECK(IsImplThread());
578 return compositor_thread_vars_unsafe_;
581 void ThreadProxy::Start() {
582 DCHECK(IsMainThread());
583 DCHECK(Proxy::HasImplThread());
585 // Create LayerTreeHostImpl.
586 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
587 CompletionEvent completion;
588 Proxy::ImplThreadTaskRunner()->PostTask(
589 FROM_HERE,
590 base::Bind(&ThreadProxy::InitializeImplOnImplThread,
591 base::Unretained(this),
592 &completion));
593 completion.Wait();
595 main_thread_weak_ptr_ = main().weak_factory.GetWeakPtr();
597 main().started = true;
600 void ThreadProxy::Stop() {
601 TRACE_EVENT0("cc", "ThreadProxy::Stop");
602 DCHECK(IsMainThread());
603 DCHECK(main().started);
605 // Synchronously finishes pending GL operations and deletes the impl.
606 // The two steps are done as separate post tasks, so that tasks posted
607 // by the GL implementation due to the Finish can be executed by the
608 // renderer before shutting it down.
610 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
612 CompletionEvent completion;
613 Proxy::ImplThreadTaskRunner()->PostTask(
614 FROM_HERE,
615 base::Bind(&ThreadProxy::FinishGLOnImplThread,
616 impl_thread_weak_ptr_,
617 &completion));
618 completion.Wait();
621 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
623 CompletionEvent completion;
624 Proxy::ImplThreadTaskRunner()->PostTask(
625 FROM_HERE,
626 base::Bind(&ThreadProxy::LayerTreeHostClosedOnImplThread,
627 impl_thread_weak_ptr_,
628 &completion));
629 completion.Wait();
632 main().weak_factory.InvalidateWeakPtrs();
633 blocked_main().layer_tree_host = NULL;
634 main().started = false;
637 void ThreadProxy::ForceSerializeOnSwapBuffers() {
638 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
639 CompletionEvent completion;
640 Proxy::ImplThreadTaskRunner()->PostTask(
641 FROM_HERE,
642 base::Bind(&ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread,
643 impl_thread_weak_ptr_,
644 &completion));
645 completion.Wait();
648 void ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread(
649 CompletionEvent* completion) {
650 if (impl().layer_tree_host_impl->renderer())
651 impl().layer_tree_host_impl->renderer()->DoNoOp();
652 completion->Signal();
655 bool ThreadProxy::SupportsImplScrolling() const {
656 return true;
659 void ThreadProxy::SetDebugState(const LayerTreeDebugState& debug_state) {
660 Proxy::ImplThreadTaskRunner()->PostTask(
661 FROM_HERE,
662 base::Bind(&ThreadProxy::SetDebugStateOnImplThread,
663 impl_thread_weak_ptr_,
664 debug_state));
667 void ThreadProxy::SetDebugStateOnImplThread(
668 const LayerTreeDebugState& debug_state) {
669 DCHECK(IsImplThread());
670 impl().scheduler->SetContinuousPainting(debug_state.continuous_painting);
673 void ThreadProxy::FinishAllRenderingOnImplThread(CompletionEvent* completion) {
674 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread");
675 DCHECK(IsImplThread());
676 impl().layer_tree_host_impl->FinishAllRendering();
677 completion->Signal();
680 void ThreadProxy::ScheduledActionSendBeginMainFrame() {
681 VLOG(2) << "ThreadProxy::ScheduledActionSendBeginMainFrame";
682 unsigned int begin_frame_id = nextBeginFrameId++;
683 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
684 benchmark_instrumentation::kSendBeginFrame, begin_frame_id);
685 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state(
686 new BeginMainFrameAndCommitState);
687 begin_main_frame_state->begin_frame_id = begin_frame_id;
688 begin_main_frame_state->begin_frame_args =
689 impl().layer_tree_host_impl->CurrentBeginFrameArgs();
690 begin_main_frame_state->scroll_info =
691 impl().layer_tree_host_impl->ProcessScrollDeltas();
693 if (!impl().layer_tree_host_impl->settings().impl_side_painting) {
694 DCHECK_GT(impl().layer_tree_host_impl->memory_allocation_limit_bytes(), 0u);
696 begin_main_frame_state->memory_allocation_limit_bytes =
697 impl().layer_tree_host_impl->memory_allocation_limit_bytes();
698 begin_main_frame_state->memory_allocation_priority_cutoff =
699 impl().layer_tree_host_impl->memory_allocation_priority_cutoff();
700 begin_main_frame_state->evicted_ui_resources =
701 impl().layer_tree_host_impl->EvictedUIResourcesExist();
702 Proxy::MainThreadTaskRunner()->PostTask(
703 FROM_HERE,
704 base::Bind(&ThreadProxy::BeginMainFrame,
705 main_thread_weak_ptr_,
706 base::Passed(&begin_main_frame_state)));
707 devtools_instrumentation::DidRequestMainThreadFrame(
708 impl().layer_tree_host_id);
709 impl().timing_history.DidBeginMainFrame();
712 void ThreadProxy::BeginMainFrame(
713 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
714 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
715 benchmark_instrumentation::kDoBeginFrame,
716 begin_main_frame_state->begin_frame_id);
717 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
718 DCHECK(IsMainThread());
720 VLOG(2) << "ThreadProxy::BeginMainFrame - BEGIN";
722 if (main().defer_commits) {
723 main().pending_deferred_commit = begin_main_frame_state.Pass();
724 layer_tree_host()->DidDeferCommit();
725 TRACE_EVENT_INSTANT0(
726 "cc", "EarlyOut_DeferCommits", TRACE_EVENT_SCOPE_THREAD);
727 VLOG(2) << "ThreadProxy::BeginMainFrame: EarlyOut_DeferCommits";
728 return;
731 // If the commit finishes, LayerTreeHost will transfer its swap promises to
732 // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the
733 // remaining swap promises.
734 ScopedAbortRemainingSwapPromises swap_promise_checker(layer_tree_host());
736 main().commit_requested = false;
737 main().commit_request_sent_to_impl_thread = false;
738 main().animate_requested = false;
740 if (!layer_tree_host()->visible()) {
741 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
742 VLOG(2) << "ThreadProxy::BeginMainFrame: EarlyOut_NotVisible";
743 bool did_handle = false;
744 Proxy::ImplThreadTaskRunner()->PostTask(
745 FROM_HERE,
746 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
747 impl_thread_weak_ptr_,
748 did_handle));
749 return;
752 if (layer_tree_host()->output_surface_lost()) {
753 TRACE_EVENT_INSTANT0(
754 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD);
755 VLOG(2) << "ThreadProxy::BeginMainFrame: EarlyOut_OutputSurfaceLost";
756 bool did_handle = false;
757 Proxy::ImplThreadTaskRunner()->PostTask(
758 FROM_HERE,
759 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
760 impl_thread_weak_ptr_,
761 did_handle));
762 return;
765 // Do not notify the impl thread of commit requests that occur during
766 // the apply/animate/layout part of the BeginMainFrameAndCommit process since
767 // those commit requests will get painted immediately. Once we have done
768 // the paint, main().commit_requested will be set to false to allow new commit
769 // requests to be scheduled.
770 // On the other hand, the animate_requested flag should remain cleared
771 // here so that any animation requests generated by the apply or animate
772 // callbacks will trigger another frame.
773 main().commit_requested = true;
774 main().commit_request_sent_to_impl_thread = true;
776 layer_tree_host()->ApplyScrollAndScale(
777 begin_main_frame_state->scroll_info.get());
779 layer_tree_host()->WillBeginMainFrame();
781 layer_tree_host()->BeginMainFrame(begin_main_frame_state->begin_frame_args);
782 layer_tree_host()->AnimateLayers(
783 begin_main_frame_state->begin_frame_args.frame_time);
784 blocked_main().last_monotonic_frame_begin_time =
785 begin_main_frame_state->begin_frame_args.frame_time;
787 // Unlink any backings that the impl thread has evicted, so that we know to
788 // re-paint them in UpdateLayers.
789 if (blocked_main().contents_texture_manager()) {
790 blocked_main().contents_texture_manager()->UnlinkAndClearEvictedBackings();
792 blocked_main().contents_texture_manager()->SetMaxMemoryLimitBytes(
793 begin_main_frame_state->memory_allocation_limit_bytes);
794 blocked_main().contents_texture_manager()->SetExternalPriorityCutoff(
795 begin_main_frame_state->memory_allocation_priority_cutoff);
798 // Recreate all UI resources if there were evicted UI resources when the impl
799 // thread initiated the commit.
800 if (begin_main_frame_state->evicted_ui_resources)
801 layer_tree_host()->RecreateUIResources();
803 layer_tree_host()->Layout();
804 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
806 // Clear the commit flag after updating animations and layout here --- objects
807 // that only layout when painted will trigger another SetNeedsCommit inside
808 // UpdateLayers.
809 main().commit_requested = false;
810 main().commit_request_sent_to_impl_thread = false;
811 bool can_cancel_this_commit =
812 main().can_cancel_commit && !begin_main_frame_state->evicted_ui_resources;
813 main().can_cancel_commit = true;
815 scoped_ptr<ResourceUpdateQueue> queue =
816 make_scoped_ptr(new ResourceUpdateQueue);
818 bool updated = layer_tree_host()->UpdateLayers(queue.get());
820 layer_tree_host()->WillCommit();
822 // Before calling animate, we set main().animate_requested to false. If it is
823 // true now, it means SetNeedAnimate was called again, but during a state when
824 // main().commit_request_sent_to_impl_thread = true. We need to force that
825 // call to happen again now so that the commit request is sent to the impl
826 // thread.
827 if (main().animate_requested) {
828 // Forces SetNeedsAnimate to consider posting a commit task.
829 main().animate_requested = false;
830 SetNeedsAnimate();
833 if (!updated && can_cancel_this_commit) {
834 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
835 VLOG(2) << "ThreadProxy::BeginMainFrame: EarlyOut_NoUpdates";
836 bool did_handle = true;
837 Proxy::ImplThreadTaskRunner()->PostTask(
838 FROM_HERE,
839 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
840 impl_thread_weak_ptr_,
841 did_handle));
843 // Although the commit is internally aborted, this is because it has been
844 // detected to be a no-op. From the perspective of an embedder, this commit
845 // went through, and input should no longer be throttled, etc.
846 layer_tree_host()->CommitComplete();
847 layer_tree_host()->DidBeginMainFrame();
848 layer_tree_host()->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE);
849 return;
852 // Notify the impl thread that the main thread is ready to commit. This will
853 // begin the commit process, which is blocking from the main thread's
854 // point of view, but asynchronously performed on the impl thread,
855 // coordinated by the Scheduler.
857 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit");
859 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
861 // This CapturePostTasks should be destroyed before CommitComplete() is
862 // called since that goes out to the embedder, and we want the embedder
863 // to receive its callbacks before that.
864 BlockingTaskRunner::CapturePostTasks blocked(
865 blocking_main_thread_task_runner());
867 CompletionEvent completion;
868 Proxy::ImplThreadTaskRunner()->PostTask(
869 FROM_HERE,
870 base::Bind(&ThreadProxy::StartCommitOnImplThread,
871 impl_thread_weak_ptr_,
872 &completion,
873 queue.release()));
874 completion.Wait();
876 RenderingStatsInstrumentation* stats_instrumentation =
877 layer_tree_host()->rendering_stats_instrumentation();
878 benchmark_instrumentation::IssueMainThreadRenderingStatsEvent(
879 stats_instrumentation->main_thread_rendering_stats());
880 stats_instrumentation->AccumulateAndClearMainThreadStats();
883 layer_tree_host()->CommitComplete();
884 layer_tree_host()->DidBeginMainFrame();
887 void ThreadProxy::StartCommitOnImplThread(CompletionEvent* completion,
888 ResourceUpdateQueue* raw_queue) {
889 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread");
890 DCHECK(!impl().commit_completion_event);
891 DCHECK(IsImplThread() && IsMainThreadBlocked());
892 DCHECK(impl().scheduler);
893 DCHECK(impl().scheduler->CommitPending());
895 if (!impl().layer_tree_host_impl) {
896 TRACE_EVENT_INSTANT0(
897 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD);
898 completion->Signal();
899 return;
902 // Ideally, we should inform to impl thread when BeginMainFrame is started.
903 // But, we can avoid a PostTask in here.
904 impl().scheduler->NotifyBeginMainFrameStarted();
906 scoped_ptr<ResourceUpdateQueue> queue(raw_queue);
908 if (impl().contents_texture_manager) {
909 DCHECK_EQ(impl().contents_texture_manager,
910 blocked_main().contents_texture_manager());
911 } else {
912 // Cache this pointer that was created on the main thread side to avoid a
913 // data race between creating it and using it on the compositor thread.
914 impl().contents_texture_manager = blocked_main().contents_texture_manager();
917 if (impl().contents_texture_manager) {
918 if (impl().contents_texture_manager->LinkedEvictedBackingsExist()) {
919 // Clear any uploads we were making to textures linked to evicted
920 // resources
921 queue->ClearUploadsToEvictedResources();
922 // Some textures in the layer tree are invalid. Kick off another commit
923 // to fill them again.
924 SetNeedsCommitOnImplThread();
927 impl().contents_texture_manager->PushTexturePrioritiesToBackings();
930 impl().commit_completion_event = completion;
931 impl().current_resource_update_controller = ResourceUpdateController::Create(
932 this,
933 Proxy::ImplThreadTaskRunner(),
934 queue.Pass(),
935 impl().layer_tree_host_impl->resource_provider());
936 impl().current_resource_update_controller->PerformMoreUpdates(
937 impl().scheduler->AnticipatedDrawTime());
940 void ThreadProxy::BeginMainFrameAbortedOnImplThread(bool did_handle) {
941 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread");
942 DCHECK(IsImplThread());
943 DCHECK(impl().scheduler);
944 DCHECK(impl().scheduler->CommitPending());
945 DCHECK(!impl().layer_tree_host_impl->pending_tree());
947 if (did_handle)
948 SetInputThrottledUntilCommitOnImplThread(false);
949 impl().layer_tree_host_impl->BeginMainFrameAborted(did_handle);
950 impl().scheduler->BeginMainFrameAborted(did_handle);
953 void ThreadProxy::ScheduledActionAnimate() {
954 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate");
955 DCHECK(IsImplThread());
957 if (!impl().animations_frozen_until_next_draw) {
958 impl().animation_time =
959 impl().layer_tree_host_impl->CurrentBeginFrameArgs().frame_time;
961 impl().layer_tree_host_impl->Animate(impl().animation_time);
962 impl().did_commit_after_animating = false;
965 void ThreadProxy::ScheduledActionCommit() {
966 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit");
967 DCHECK(IsImplThread());
968 DCHECK(IsMainThreadBlocked());
969 DCHECK(impl().commit_completion_event);
970 DCHECK(impl().current_resource_update_controller);
972 // Complete all remaining texture updates.
973 impl().current_resource_update_controller->Finalize();
974 impl().current_resource_update_controller.reset();
976 if (impl().animations_frozen_until_next_draw) {
977 impl().animation_time = std::max(
978 impl().animation_time, blocked_main().last_monotonic_frame_begin_time);
980 impl().did_commit_after_animating = true;
982 blocked_main().main_thread_inside_commit = true;
983 impl().layer_tree_host_impl->BeginCommit();
984 layer_tree_host()->BeginCommitOnImplThread(impl().layer_tree_host_impl.get());
985 layer_tree_host()->FinishCommitOnImplThread(
986 impl().layer_tree_host_impl.get());
987 blocked_main().main_thread_inside_commit = false;
989 bool hold_commit = layer_tree_host()->settings().impl_side_painting &&
990 blocked_main().commit_waits_for_activation;
991 blocked_main().commit_waits_for_activation = false;
993 if (hold_commit) {
994 // For some layer types in impl-side painting, the commit is held until
995 // the sync tree is activated. It's also possible that the
996 // sync tree has already activated if there was no work to be done.
997 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD);
998 impl().completion_event_for_commit_held_on_tree_activation =
999 impl().commit_completion_event;
1000 impl().commit_completion_event = NULL;
1001 } else {
1002 impl().commit_completion_event->Signal();
1003 impl().commit_completion_event = NULL;
1006 // Delay this step until afer the main thread has been released as it's
1007 // often a good bit of work to update the tree and prepare the new frame.
1008 impl().layer_tree_host_impl->CommitComplete();
1010 SetInputThrottledUntilCommitOnImplThread(false);
1012 UpdateBackgroundAnimateTicking();
1014 impl().next_frame_is_newly_committed_frame = true;
1016 impl().timing_history.DidCommit();
1019 void ThreadProxy::ScheduledActionUpdateVisibleTiles() {
1020 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionUpdateVisibleTiles");
1021 DCHECK(IsImplThread());
1022 impl().layer_tree_host_impl->UpdateVisibleTiles();
1025 void ThreadProxy::ScheduledActionActivateSyncTree() {
1026 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree");
1027 DCHECK(IsImplThread());
1028 impl().layer_tree_host_impl->ActivateSyncTree();
1031 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() {
1032 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation");
1033 DCHECK(IsImplThread());
1034 Proxy::MainThreadTaskRunner()->PostTask(
1035 FROM_HERE,
1036 base::Bind(&ThreadProxy::CreateAndInitializeOutputSurface,
1037 main_thread_weak_ptr_));
1040 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) {
1041 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap");
1042 DrawResult result;
1044 DCHECK(IsImplThread());
1045 DCHECK(impl().layer_tree_host_impl.get());
1047 impl().timing_history.DidStartDrawing();
1048 base::AutoReset<bool> mark_inside(&impl().inside_draw, true);
1050 if (impl().did_commit_after_animating) {
1051 impl().layer_tree_host_impl->Animate(impl().animation_time);
1052 impl().did_commit_after_animating = false;
1055 if (impl().layer_tree_host_impl->pending_tree())
1056 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties();
1058 // This method is called on a forced draw, regardless of whether we are able
1059 // to produce a frame, as the calling site on main thread is blocked until its
1060 // request completes, and we signal completion here. If CanDraw() is false, we
1061 // will indicate success=false to the caller, but we must still signal
1062 // completion to avoid deadlock.
1064 // We guard PrepareToDraw() with CanDraw() because it always returns a valid
1065 // frame, so can only be used when such a frame is possible. Since
1066 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
1067 // CanDraw() as well.
1069 LayerTreeHostImpl::FrameData frame;
1070 bool draw_frame = false;
1072 if (impl().layer_tree_host_impl->CanDraw()) {
1073 result = impl().layer_tree_host_impl->PrepareToDraw(&frame);
1074 draw_frame = forced_draw || result == DRAW_SUCCESS;
1075 } else {
1076 result = DRAW_ABORTED_CANT_DRAW;
1079 if (draw_frame) {
1080 impl().layer_tree_host_impl->DrawLayers(
1081 &frame, impl().scheduler->LastBeginImplFrameTime());
1082 result = DRAW_SUCCESS;
1083 impl().animations_frozen_until_next_draw = false;
1084 } else if (result == DRAW_ABORTED_CHECKERBOARD_ANIMATIONS &&
1085 !impl().layer_tree_host_impl->settings().impl_side_painting) {
1086 // Without impl-side painting, the animated layer that is checkerboarding
1087 // will continue to checkerboard until the next commit. If this layer
1088 // continues to move during the commit, it may continue to checkerboard
1089 // after the commit since the region rasterized during the commit will not
1090 // match the region that is currently visible; eventually this
1091 // checkerboarding will be displayed when we force a draw. To avoid this,
1092 // we freeze animations until we successfully draw.
1093 impl().animations_frozen_until_next_draw = true;
1094 } else {
1095 DCHECK_NE(DRAW_SUCCESS, result);
1097 impl().layer_tree_host_impl->DidDrawAllLayers(frame);
1099 bool start_ready_animations = draw_frame;
1100 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations);
1102 if (draw_frame) {
1103 bool did_request_swap = impl().layer_tree_host_impl->SwapBuffers(frame);
1105 // We don't know if we have incomplete tiles if we didn't actually swap.
1106 if (did_request_swap) {
1107 DCHECK(!frame.has_no_damage);
1108 SetSwapUsedIncompleteTileOnImplThread(frame.contains_incomplete_tile);
1112 // Tell the main thread that the the newly-commited frame was drawn.
1113 if (impl().next_frame_is_newly_committed_frame) {
1114 impl().next_frame_is_newly_committed_frame = false;
1115 Proxy::MainThreadTaskRunner()->PostTask(
1116 FROM_HERE,
1117 base::Bind(&ThreadProxy::DidCommitAndDrawFrame, main_thread_weak_ptr_));
1120 if (result == DRAW_SUCCESS)
1121 impl().timing_history.DidFinishDrawing();
1123 DCHECK_NE(INVALID_RESULT, result);
1124 return result;
1127 void ThreadProxy::ScheduledActionManageTiles() {
1128 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionManageTiles");
1129 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1130 impl().layer_tree_host_impl->ManageTiles();
1133 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() {
1134 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap");
1136 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to
1137 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should
1138 // never generate this call when it can't draw.
1139 DCHECK(impl().layer_tree_host_impl->CanDraw());
1141 bool forced_draw = false;
1142 return DrawSwapInternal(forced_draw);
1145 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() {
1146 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced");
1147 bool forced_draw = true;
1148 return DrawSwapInternal(forced_draw);
1151 void ThreadProxy::DidAnticipatedDrawTimeChange(base::TimeTicks time) {
1152 if (impl().current_resource_update_controller)
1153 impl().current_resource_update_controller->PerformMoreUpdates(time);
1156 base::TimeDelta ThreadProxy::DrawDurationEstimate() {
1157 return impl().timing_history.DrawDurationEstimate();
1160 base::TimeDelta ThreadProxy::BeginMainFrameToCommitDurationEstimate() {
1161 return impl().timing_history.BeginMainFrameToCommitDurationEstimate();
1164 base::TimeDelta ThreadProxy::CommitToActivateDurationEstimate() {
1165 return impl().timing_history.CommitToActivateDurationEstimate();
1168 void ThreadProxy::DidBeginImplFrameDeadline() {
1169 impl().layer_tree_host_impl->ResetCurrentBeginFrameArgsForNextFrame();
1172 void ThreadProxy::ReadyToFinalizeTextureUpdates() {
1173 DCHECK(IsImplThread());
1174 impl().scheduler->NotifyReadyToCommit();
1177 void ThreadProxy::DidCommitAndDrawFrame() {
1178 DCHECK(IsMainThread());
1179 layer_tree_host()->DidCommitAndDrawFrame();
1182 void ThreadProxy::DidCompleteSwapBuffers() {
1183 DCHECK(IsMainThread());
1184 layer_tree_host()->DidCompleteSwapBuffers();
1187 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
1188 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents");
1189 DCHECK(IsMainThread());
1190 layer_tree_host()->SetAnimationEvents(events.Pass());
1193 void ThreadProxy::InitializeImplOnImplThread(CompletionEvent* completion) {
1194 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread");
1195 DCHECK(IsImplThread());
1196 impl().layer_tree_host_impl =
1197 layer_tree_host()->CreateLayerTreeHostImpl(this);
1198 SchedulerSettings scheduler_settings(layer_tree_host()->settings());
1199 impl().scheduler = Scheduler::Create(this,
1200 scheduler_settings,
1201 impl().layer_tree_host_id,
1202 ImplThreadTaskRunner());
1203 impl().scheduler->SetVisible(impl().layer_tree_host_impl->visible());
1205 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr();
1206 completion->Signal();
1209 void ThreadProxy::DeleteContentsTexturesOnImplThread(
1210 CompletionEvent* completion) {
1211 TRACE_EVENT0("cc", "ThreadProxy::DeleteContentsTexturesOnImplThread");
1212 DCHECK(IsImplThread());
1213 DCHECK(IsMainThreadBlocked());
1214 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1215 impl().layer_tree_host_impl->resource_provider());
1216 completion->Signal();
1219 void ThreadProxy::InitializeOutputSurfaceOnImplThread(
1220 scoped_ptr<OutputSurface> output_surface) {
1221 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread");
1222 DCHECK(IsImplThread());
1224 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get();
1225 bool success = host_impl->InitializeRenderer(output_surface.Pass());
1226 RendererCapabilities capabilities;
1227 if (success) {
1228 capabilities =
1229 host_impl->GetRendererCapabilities().MainThreadCapabilities();
1232 Proxy::MainThreadTaskRunner()->PostTask(
1233 FROM_HERE,
1234 base::Bind(&ThreadProxy::DidInitializeOutputSurface,
1235 main_thread_weak_ptr_,
1236 success,
1237 capabilities));
1239 if (success)
1240 impl().scheduler->DidCreateAndInitializeOutputSurface();
1243 void ThreadProxy::FinishGLOnImplThread(CompletionEvent* completion) {
1244 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread");
1245 DCHECK(IsImplThread());
1246 if (impl().layer_tree_host_impl->output_surface()) {
1247 ContextProvider* context_provider =
1248 impl().layer_tree_host_impl->output_surface()->context_provider();
1249 if (context_provider)
1250 context_provider->ContextGL()->Finish();
1252 completion->Signal();
1255 void ThreadProxy::LayerTreeHostClosedOnImplThread(CompletionEvent* completion) {
1256 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread");
1257 DCHECK(IsImplThread());
1258 DCHECK(IsMainThreadBlocked());
1259 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1260 impl().layer_tree_host_impl->resource_provider());
1261 impl().current_resource_update_controller.reset();
1262 impl().layer_tree_host_impl->SetNeedsBeginFrame(false);
1263 impl().scheduler.reset();
1264 impl().layer_tree_host_impl.reset();
1265 impl().weak_factory.InvalidateWeakPtrs();
1266 // We need to explicitly cancel the notifier, since it isn't using weak ptrs.
1267 // TODO(vmpstr): We should see if we can make it use weak ptrs and still keep
1268 // the convention of having a weak ptr factory initialized last. Alternatively
1269 // we should moved the notifier (and RenewTreePriority) to LTHI. See
1270 // crbug.com/411972
1271 impl().smoothness_priority_expiration_notifier.Cancel();
1272 impl().contents_texture_manager = NULL;
1273 completion->Signal();
1276 size_t ThreadProxy::MaxPartialTextureUpdates() const {
1277 return ResourceUpdateController::MaxPartialTextureUpdates();
1280 ThreadProxy::BeginMainFrameAndCommitState::BeginMainFrameAndCommitState()
1281 : memory_allocation_limit_bytes(0),
1282 memory_allocation_priority_cutoff(0),
1283 evicted_ui_resources(false) {}
1285 ThreadProxy::BeginMainFrameAndCommitState::~BeginMainFrameAndCommitState() {}
1287 void ThreadProxy::AsValueInto(base::debug::TracedValue* state) const {
1288 CompletionEvent completion;
1290 DebugScopedSetMainThreadBlocked main_thread_blocked(
1291 const_cast<ThreadProxy*>(this));
1292 scoped_refptr<base::debug::TracedValue> state_refptr(state);
1293 Proxy::ImplThreadTaskRunner()->PostTask(
1294 FROM_HERE,
1295 base::Bind(&ThreadProxy::AsValueOnImplThread,
1296 impl_thread_weak_ptr_,
1297 &completion,
1298 state_refptr));
1299 completion.Wait();
1303 void ThreadProxy::AsValueOnImplThread(CompletionEvent* completion,
1304 base::debug::TracedValue* state) const {
1305 state->BeginDictionary("layer_tree_host_impl");
1306 impl().layer_tree_host_impl->AsValueInto(state);
1307 state->EndDictionary();
1308 completion->Signal();
1311 bool ThreadProxy::MainFrameWillHappenForTesting() {
1312 DCHECK(IsMainThread());
1313 CompletionEvent completion;
1314 bool main_frame_will_happen = false;
1316 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1317 Proxy::ImplThreadTaskRunner()->PostTask(
1318 FROM_HERE,
1319 base::Bind(&ThreadProxy::MainFrameWillHappenOnImplThreadForTesting,
1320 impl_thread_weak_ptr_,
1321 &completion,
1322 &main_frame_will_happen));
1323 completion.Wait();
1325 return main_frame_will_happen;
1328 void ThreadProxy::MainFrameWillHappenOnImplThreadForTesting(
1329 CompletionEvent* completion,
1330 bool* main_frame_will_happen) {
1331 DCHECK(IsImplThread());
1332 if (impl().layer_tree_host_impl->output_surface()) {
1333 *main_frame_will_happen = impl().scheduler->MainFrameForTestingWillHappen();
1334 } else {
1335 *main_frame_will_happen = false;
1337 completion->Signal();
1340 void ThreadProxy::RenewTreePriority() {
1341 DCHECK(IsImplThread());
1342 bool smoothness_takes_priority =
1343 impl().layer_tree_host_impl->pinch_gesture_active() ||
1344 impl().layer_tree_host_impl->page_scale_animation_active() ||
1345 impl().layer_tree_host_impl->IsCurrentlyScrolling();
1347 // Schedule expiration if smoothness currently takes priority.
1348 if (smoothness_takes_priority)
1349 impl().smoothness_priority_expiration_notifier.Schedule();
1351 // We use the same priority for both trees by default.
1352 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES;
1354 // Smoothness takes priority if we have an expiration for it scheduled.
1355 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification())
1356 priority = SMOOTHNESS_TAKES_PRIORITY;
1358 // New content always takes priority when the active tree has
1359 // evicted resources or there is an invalid viewport size.
1360 if (impl().layer_tree_host_impl->active_tree()->ContentsTexturesPurged() ||
1361 impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() ||
1362 impl().layer_tree_host_impl->EvictedUIResourcesExist() ||
1363 impl().input_throttled_until_commit) {
1364 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active
1365 // tree might be freed. We need to set RequiresHighResToDraw to ensure that
1366 // high res tiles will be required to activate pending tree.
1367 impl().layer_tree_host_impl->active_tree()->SetRequiresHighResToDraw();
1368 priority = NEW_CONTENT_TAKES_PRIORITY;
1371 impl().layer_tree_host_impl->SetTreePriority(priority);
1373 // Only put the scheduler in impl latency prioritization mode if we don't
1374 // have a scroll listener. This gives the scroll listener a better chance of
1375 // handling scroll updates within the same frame. The tree itself is still
1376 // kept in prefer smoothness mode to allow checkerboarding.
1377 impl().scheduler->SetImplLatencyTakesPriority(
1378 priority == SMOOTHNESS_TAKES_PRIORITY &&
1379 !impl().layer_tree_host_impl->scroll_affects_scroll_handler());
1381 // Notify the the client of this compositor via the output surface.
1382 // TODO(epenner): Route this to compositor-thread instead of output-surface
1383 // after GTFO refactor of compositor-thread (http://crbug/170828).
1384 if (impl().layer_tree_host_impl->output_surface()) {
1385 impl()
1386 .layer_tree_host_impl->output_surface()
1387 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY);
1391 void ThreadProxy::PostDelayedScrollbarFadeOnImplThread(
1392 const base::Closure& start_fade,
1393 base::TimeDelta delay) {
1394 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, start_fade, delay);
1397 void ThreadProxy::DidActivateSyncTree() {
1398 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread");
1399 DCHECK(IsImplThread());
1401 if (impl().completion_event_for_commit_held_on_tree_activation) {
1402 TRACE_EVENT_INSTANT0(
1403 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD);
1404 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1405 impl().completion_event_for_commit_held_on_tree_activation->Signal();
1406 impl().completion_event_for_commit_held_on_tree_activation = NULL;
1409 UpdateBackgroundAnimateTicking();
1411 impl().timing_history.DidActivateSyncTree();
1414 void ThreadProxy::DidManageTiles() {
1415 DCHECK(IsImplThread());
1416 impl().scheduler->DidManageTiles();
1419 } // namespace cc