Revert "Add sbox tests related to warming up of locales."
[chromium-blink-merge.git] / cc / trees / thread_proxy.cc
blob2240a10c71b40ceac159bdb4ac32d8d6603b43c8
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/trace_event/trace_event.h"
13 #include "base/trace_event/trace_event_argument.h"
14 #include "base/trace_event/trace_event_synthetic_delay.h"
15 #include "cc/debug/benchmark_instrumentation.h"
16 #include "cc/debug/devtools_instrumentation.h"
17 #include "cc/input/input_handler.h"
18 #include "cc/output/context_provider.h"
19 #include "cc/output/output_surface.h"
20 #include "cc/output/swap_promise.h"
21 #include "cc/quads/draw_quad.h"
22 #include "cc/scheduler/commit_earlyout_reason.h"
23 #include "cc/scheduler/compositor_timing_history.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"
31 namespace cc {
33 namespace {
35 // Measured in seconds.
36 const double kSmoothnessTakesPriorityExpirationDelay = 0.25;
38 unsigned int nextBeginFrameId = 0;
40 } // namespace
42 struct ThreadProxy::SchedulerStateRequest {
43 CompletionEvent completion;
44 scoped_ptr<base::Value> state;
47 scoped_ptr<Proxy> ThreadProxy::Create(
48 LayerTreeHost* layer_tree_host,
49 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
50 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
51 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
52 return make_scoped_ptr(new ThreadProxy(layer_tree_host,
53 main_task_runner,
54 impl_task_runner,
55 external_begin_frame_source.Pass()));
58 ThreadProxy::ThreadProxy(
59 LayerTreeHost* layer_tree_host,
60 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
61 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
62 scoped_ptr<BeginFrameSource> external_begin_frame_source)
63 : Proxy(main_task_runner, impl_task_runner),
64 main_thread_only_vars_unsafe_(this, layer_tree_host->id()),
65 main_thread_or_blocked_vars_unsafe_(layer_tree_host),
66 compositor_thread_vars_unsafe_(
67 this,
68 layer_tree_host->id(),
69 layer_tree_host->rendering_stats_instrumentation(),
70 external_begin_frame_source.Pass()) {
71 TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy");
72 DCHECK(IsMainThread());
73 DCHECK(this->layer_tree_host());
76 ThreadProxy::MainThreadOnly::MainThreadOnly(ThreadProxy* proxy,
77 int layer_tree_host_id)
78 : layer_tree_host_id(layer_tree_host_id),
79 max_requested_pipeline_stage(NO_PIPELINE_STAGE),
80 current_pipeline_stage(NO_PIPELINE_STAGE),
81 final_pipeline_stage(NO_PIPELINE_STAGE),
82 started(false),
83 prepare_tiles_pending(false),
84 defer_commits(false),
85 weak_factory(proxy) {}
87 ThreadProxy::MainThreadOnly::~MainThreadOnly() {}
89 ThreadProxy::MainThreadOrBlockedMainThread::MainThreadOrBlockedMainThread(
90 LayerTreeHost* host)
91 : layer_tree_host(host),
92 commit_waits_for_activation(false),
93 main_thread_inside_commit(false) {}
95 ThreadProxy::MainThreadOrBlockedMainThread::~MainThreadOrBlockedMainThread() {}
97 ThreadProxy::CompositorThreadOnly::CompositorThreadOnly(
98 ThreadProxy* proxy,
99 int layer_tree_host_id,
100 RenderingStatsInstrumentation* rendering_stats_instrumentation,
101 scoped_ptr<BeginFrameSource> external_begin_frame_source)
102 : layer_tree_host_id(layer_tree_host_id),
103 commit_completion_event(NULL),
104 completion_event_for_commit_held_on_tree_activation(NULL),
105 next_frame_is_newly_committed_frame(false),
106 inside_draw(false),
107 input_throttled_until_commit(false),
108 smoothness_priority_expiration_notifier(
109 proxy->ImplThreadTaskRunner(),
110 base::Bind(&ThreadProxy::RenewTreePriority, base::Unretained(proxy)),
111 base::TimeDelta::FromMilliseconds(
112 kSmoothnessTakesPriorityExpirationDelay * 1000)),
113 external_begin_frame_source(external_begin_frame_source.Pass()),
114 rendering_stats_instrumentation(rendering_stats_instrumentation),
115 weak_factory(proxy) {
118 ThreadProxy::CompositorThreadOnly::~CompositorThreadOnly() {}
120 ThreadProxy::~ThreadProxy() {
121 TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy");
122 DCHECK(IsMainThread());
123 DCHECK(!main().started);
126 void ThreadProxy::FinishAllRendering() {
127 DCHECK(Proxy::IsMainThread());
128 DCHECK(!main().defer_commits);
130 // Make sure all GL drawing is finished on the impl thread.
131 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
132 CompletionEvent completion;
133 Proxy::ImplThreadTaskRunner()->PostTask(
134 FROM_HERE,
135 base::Bind(&ThreadProxy::FinishAllRenderingOnImplThread,
136 impl_thread_weak_ptr_,
137 &completion));
138 completion.Wait();
141 bool ThreadProxy::IsStarted() const {
142 DCHECK(Proxy::IsMainThread());
143 return main().started;
146 bool ThreadProxy::CommitToActiveTree() const {
147 // With ThreadProxy, we use a pending tree and activate it once it's ready to
148 // draw to allow input to modify the active tree and draw during raster.
149 return false;
152 void ThreadProxy::SetLayerTreeHostClientReady() {
153 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReady");
154 Proxy::ImplThreadTaskRunner()->PostTask(
155 FROM_HERE,
156 base::Bind(&ThreadProxy::SetLayerTreeHostClientReadyOnImplThread,
157 impl_thread_weak_ptr_));
160 void ThreadProxy::SetLayerTreeHostClientReadyOnImplThread() {
161 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReadyOnImplThread");
162 impl().scheduler->SetCanStart();
165 void ThreadProxy::SetVisible(bool visible) {
166 TRACE_EVENT1("cc", "ThreadProxy::SetVisible", "visible", visible);
167 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
169 CompletionEvent completion;
170 Proxy::ImplThreadTaskRunner()->PostTask(
171 FROM_HERE,
172 base::Bind(&ThreadProxy::SetVisibleOnImplThread,
173 impl_thread_weak_ptr_,
174 &completion,
175 visible));
176 completion.Wait();
179 void ThreadProxy::SetVisibleOnImplThread(CompletionEvent* completion,
180 bool visible) {
181 TRACE_EVENT1("cc", "ThreadProxy::SetVisibleOnImplThread", "visible", visible);
182 impl().layer_tree_host_impl->SetVisible(visible);
183 impl().scheduler->SetVisible(visible);
184 completion->Signal();
187 void ThreadProxy::SetThrottleFrameProduction(bool throttle) {
188 TRACE_EVENT1("cc", "ThreadProxy::SetThrottleFrameProduction", "throttle",
189 throttle);
190 Proxy::ImplThreadTaskRunner()->PostTask(
191 FROM_HERE,
192 base::Bind(&ThreadProxy::SetThrottleFrameProductionOnImplThread,
193 impl_thread_weak_ptr_, throttle));
196 void ThreadProxy::SetThrottleFrameProductionOnImplThread(bool throttle) {
197 TRACE_EVENT1("cc", "ThreadProxy::SetThrottleFrameProductionOnImplThread",
198 "throttle", throttle);
199 impl().scheduler->SetThrottleFrameProduction(throttle);
202 void ThreadProxy::DidLoseOutputSurface() {
203 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurface");
204 DCHECK(IsMainThread());
205 layer_tree_host()->DidLoseOutputSurface();
208 void ThreadProxy::RequestNewOutputSurface() {
209 DCHECK(IsMainThread());
210 layer_tree_host()->RequestNewOutputSurface();
213 void ThreadProxy::SetOutputSurface(scoped_ptr<OutputSurface> output_surface) {
214 Proxy::ImplThreadTaskRunner()->PostTask(
215 FROM_HERE,
216 base::Bind(&ThreadProxy::InitializeOutputSurfaceOnImplThread,
217 impl_thread_weak_ptr_, base::Passed(&output_surface)));
220 void ThreadProxy::DidInitializeOutputSurface(
221 bool success,
222 const RendererCapabilities& capabilities) {
223 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeOutputSurface");
224 DCHECK(IsMainThread());
226 if (!success) {
227 layer_tree_host()->DidFailToInitializeOutputSurface();
228 return;
230 main().renderer_capabilities_main_thread_copy = capabilities;
231 layer_tree_host()->DidInitializeOutputSurface();
234 void ThreadProxy::SetRendererCapabilitiesMainThreadCopy(
235 const RendererCapabilities& capabilities) {
236 main().renderer_capabilities_main_thread_copy = capabilities;
239 bool ThreadProxy::SendCommitRequestToImplThreadIfNeeded(
240 CommitPipelineStage required_stage) {
241 DCHECK(IsMainThread());
242 DCHECK_NE(NO_PIPELINE_STAGE, required_stage);
243 bool already_posted =
244 main().max_requested_pipeline_stage != NO_PIPELINE_STAGE;
245 main().max_requested_pipeline_stage =
246 std::max(main().max_requested_pipeline_stage, required_stage);
247 if (already_posted)
248 return false;
249 Proxy::ImplThreadTaskRunner()->PostTask(
250 FROM_HERE,
251 base::Bind(&ThreadProxy::SetNeedsCommitOnImplThread,
252 impl_thread_weak_ptr_));
253 return true;
256 void ThreadProxy::DidCompletePageScaleAnimation() {
257 DCHECK(IsMainThread());
258 layer_tree_host()->DidCompletePageScaleAnimation();
261 const RendererCapabilities& ThreadProxy::GetRendererCapabilities() const {
262 DCHECK(IsMainThread());
263 DCHECK(!layer_tree_host()->output_surface_lost());
264 return main().renderer_capabilities_main_thread_copy;
267 void ThreadProxy::SetNeedsAnimate() {
268 DCHECK(IsMainThread());
269 if (SendCommitRequestToImplThreadIfNeeded(ANIMATE_PIPELINE_STAGE)) {
270 TRACE_EVENT_INSTANT0("cc", "ThreadProxy::SetNeedsAnimate",
271 TRACE_EVENT_SCOPE_THREAD);
275 void ThreadProxy::SetNeedsUpdateLayers() {
276 DCHECK(IsMainThread());
277 // If we are currently animating, make sure we also update the layers.
278 if (main().current_pipeline_stage == ANIMATE_PIPELINE_STAGE) {
279 main().final_pipeline_stage =
280 std::max(main().final_pipeline_stage, UPDATE_LAYERS_PIPELINE_STAGE);
281 return;
283 if (SendCommitRequestToImplThreadIfNeeded(UPDATE_LAYERS_PIPELINE_STAGE)) {
284 TRACE_EVENT_INSTANT0("cc", "ThreadProxy::SetNeedsUpdateLayers",
285 TRACE_EVENT_SCOPE_THREAD);
289 void ThreadProxy::SetNeedsCommit() {
290 DCHECK(IsMainThread());
291 // If we are currently animating, make sure we don't skip the commit. Note
292 // that requesting a commit during the layer update stage means we need to
293 // schedule another full commit.
294 if (main().current_pipeline_stage == ANIMATE_PIPELINE_STAGE) {
295 main().final_pipeline_stage =
296 std::max(main().final_pipeline_stage, COMMIT_PIPELINE_STAGE);
297 return;
299 if (SendCommitRequestToImplThreadIfNeeded(COMMIT_PIPELINE_STAGE)) {
300 TRACE_EVENT_INSTANT0("cc", "ThreadProxy::SetNeedsCommit",
301 TRACE_EVENT_SCOPE_THREAD);
305 void ThreadProxy::UpdateRendererCapabilitiesOnImplThread() {
306 DCHECK(IsImplThread());
307 Proxy::MainThreadTaskRunner()->PostTask(
308 FROM_HERE,
309 base::Bind(&ThreadProxy::SetRendererCapabilitiesMainThreadCopy,
310 main_thread_weak_ptr_,
311 impl()
312 .layer_tree_host_impl->GetRendererCapabilities()
313 .MainThreadCapabilities()));
316 void ThreadProxy::DidLoseOutputSurfaceOnImplThread() {
317 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurfaceOnImplThread");
318 DCHECK(IsImplThread());
319 Proxy::MainThreadTaskRunner()->PostTask(
320 FROM_HERE,
321 base::Bind(&ThreadProxy::DidLoseOutputSurface, main_thread_weak_ptr_));
322 impl().scheduler->DidLoseOutputSurface();
325 void ThreadProxy::CommitVSyncParameters(base::TimeTicks timebase,
326 base::TimeDelta interval) {
327 impl().scheduler->CommitVSyncParameters(timebase, interval);
330 void ThreadProxy::SetEstimatedParentDrawTime(base::TimeDelta draw_time) {
331 impl().scheduler->SetEstimatedParentDrawTime(draw_time);
334 void ThreadProxy::SetMaxSwapsPendingOnImplThread(int max) {
335 impl().scheduler->SetMaxSwapsPending(max);
338 void ThreadProxy::DidSwapBuffersOnImplThread() {
339 impl().scheduler->DidSwapBuffers();
342 void ThreadProxy::DidSwapBuffersCompleteOnImplThread() {
343 TRACE_EVENT0("cc,benchmark",
344 "ThreadProxy::DidSwapBuffersCompleteOnImplThread");
345 DCHECK(IsImplThread());
346 impl().scheduler->DidSwapBuffersComplete();
347 Proxy::MainThreadTaskRunner()->PostTask(
348 FROM_HERE,
349 base::Bind(&ThreadProxy::DidCompleteSwapBuffers, main_thread_weak_ptr_));
352 void ThreadProxy::WillBeginImplFrame(const BeginFrameArgs& args) {
353 impl().layer_tree_host_impl->WillBeginImplFrame(args);
354 if (impl().last_processed_begin_main_frame_args.IsValid()) {
355 // Last processed begin main frame args records the frame args that we sent
356 // to the main thread for the last frame that we've processed. If that is
357 // set, that means the current frame is one past the frame in which we've
358 // finished the processing.
359 impl().layer_tree_host_impl->RecordMainFrameTiming(
360 impl().last_processed_begin_main_frame_args,
361 impl().layer_tree_host_impl->CurrentBeginFrameArgs());
362 impl().last_processed_begin_main_frame_args = BeginFrameArgs();
366 void ThreadProxy::OnCanDrawStateChanged(bool can_draw) {
367 TRACE_EVENT1(
368 "cc", "ThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw);
369 DCHECK(IsImplThread());
370 impl().scheduler->SetCanDraw(can_draw);
373 void ThreadProxy::NotifyReadyToActivate() {
374 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToActivate");
375 impl().scheduler->NotifyReadyToActivate();
378 void ThreadProxy::NotifyReadyToDraw() {
379 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToDraw");
380 impl().scheduler->NotifyReadyToDraw();
383 void ThreadProxy::SetNeedsCommitOnImplThread() {
384 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommitOnImplThread");
385 DCHECK(IsImplThread());
386 impl().scheduler->SetNeedsBeginMainFrame();
389 void ThreadProxy::SetVideoNeedsBeginFrames(bool needs_begin_frames) {
390 TRACE_EVENT1("cc", "ThreadProxy::SetVideoNeedsBeginFrames",
391 "needs_begin_frames", needs_begin_frames);
392 DCHECK(IsImplThread());
393 // In tests the layer tree is destroyed after the scheduler is.
394 if (impl().scheduler)
395 impl().scheduler->SetVideoNeedsBeginFrames(needs_begin_frames);
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::IsInsideDraw() { return impl().inside_draw; }
412 void ThreadProxy::SetNeedsRedraw(const gfx::Rect& damage_rect) {
413 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedraw");
414 DCHECK(IsMainThread());
415 Proxy::ImplThreadTaskRunner()->PostTask(
416 FROM_HERE,
417 base::Bind(&ThreadProxy::SetNeedsRedrawRectOnImplThread,
418 impl_thread_weak_ptr_,
419 damage_rect));
422 void ThreadProxy::SetNextCommitWaitsForActivation() {
423 DCHECK(IsMainThread());
424 DCHECK(!blocked_main().main_thread_inside_commit);
425 blocked_main().commit_waits_for_activation = true;
428 void ThreadProxy::SetDeferCommits(bool defer_commits) {
429 DCHECK(IsMainThread());
430 if (main().defer_commits == defer_commits)
431 return;
433 main().defer_commits = defer_commits;
434 if (main().defer_commits)
435 TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::SetDeferCommits", this);
436 else
437 TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::SetDeferCommits", this);
439 Proxy::ImplThreadTaskRunner()->PostTask(
440 FROM_HERE,
441 base::Bind(&ThreadProxy::SetDeferCommitsOnImplThread,
442 impl_thread_weak_ptr_,
443 defer_commits));
446 void ThreadProxy::SetDeferCommitsOnImplThread(bool defer_commits) const {
447 DCHECK(IsImplThread());
448 impl().scheduler->SetDeferCommits(defer_commits);
451 bool ThreadProxy::CommitRequested() const {
452 DCHECK(IsMainThread());
453 // TODO(skyostil): Split this into something like CommitRequested() and
454 // CommitInProgress().
455 return main().current_pipeline_stage != NO_PIPELINE_STAGE ||
456 main().max_requested_pipeline_stage >= COMMIT_PIPELINE_STAGE;
459 bool ThreadProxy::BeginMainFrameRequested() const {
460 DCHECK(IsMainThread());
461 return main().max_requested_pipeline_stage != NO_PIPELINE_STAGE;
464 void ThreadProxy::SetNeedsRedrawOnImplThread() {
465 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedrawOnImplThread");
466 DCHECK(IsImplThread());
467 impl().scheduler->SetNeedsRedraw();
470 void ThreadProxy::SetNeedsAnimateOnImplThread() {
471 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimateOnImplThread");
472 DCHECK(IsImplThread());
473 impl().scheduler->SetNeedsAnimate();
476 void ThreadProxy::SetNeedsPrepareTilesOnImplThread() {
477 DCHECK(IsImplThread());
478 impl().scheduler->SetNeedsPrepareTiles();
481 void ThreadProxy::SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) {
482 DCHECK(IsImplThread());
483 impl().layer_tree_host_impl->SetViewportDamage(damage_rect);
484 SetNeedsRedrawOnImplThread();
487 void ThreadProxy::MainThreadHasStoppedFlinging() {
488 DCHECK(IsMainThread());
489 Proxy::ImplThreadTaskRunner()->PostTask(
490 FROM_HERE,
491 base::Bind(&ThreadProxy::MainThreadHasStoppedFlingingOnImplThread,
492 impl_thread_weak_ptr_));
495 void ThreadProxy::MainThreadHasStoppedFlingingOnImplThread() {
496 DCHECK(IsImplThread());
497 impl().layer_tree_host_impl->MainThreadHasStoppedFlinging();
500 void ThreadProxy::NotifyInputThrottledUntilCommit() {
501 DCHECK(IsMainThread());
502 Proxy::ImplThreadTaskRunner()->PostTask(
503 FROM_HERE,
504 base::Bind(&ThreadProxy::SetInputThrottledUntilCommitOnImplThread,
505 impl_thread_weak_ptr_,
506 true));
509 void ThreadProxy::SetInputThrottledUntilCommitOnImplThread(bool is_throttled) {
510 DCHECK(IsImplThread());
511 if (is_throttled == impl().input_throttled_until_commit)
512 return;
513 impl().input_throttled_until_commit = is_throttled;
514 RenewTreePriority();
517 LayerTreeHost* ThreadProxy::layer_tree_host() {
518 return blocked_main().layer_tree_host;
521 const LayerTreeHost* ThreadProxy::layer_tree_host() const {
522 return blocked_main().layer_tree_host;
525 ThreadProxy::MainThreadOnly& ThreadProxy::main() {
526 DCHECK(IsMainThread());
527 return main_thread_only_vars_unsafe_;
529 const ThreadProxy::MainThreadOnly& ThreadProxy::main() const {
530 DCHECK(IsMainThread());
531 return main_thread_only_vars_unsafe_;
534 ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main() {
535 DCHECK(IsMainThread() || IsMainThreadBlocked());
536 return main_thread_or_blocked_vars_unsafe_;
539 const ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main()
540 const {
541 DCHECK(IsMainThread() || IsMainThreadBlocked());
542 return main_thread_or_blocked_vars_unsafe_;
545 ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() {
546 DCHECK(IsImplThread());
547 return compositor_thread_vars_unsafe_;
550 const ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() const {
551 DCHECK(IsImplThread());
552 return compositor_thread_vars_unsafe_;
555 void ThreadProxy::Start() {
556 DCHECK(IsMainThread());
557 DCHECK(Proxy::HasImplThread());
559 // Create LayerTreeHostImpl.
560 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
561 CompletionEvent completion;
562 Proxy::ImplThreadTaskRunner()->PostTask(
563 FROM_HERE,
564 base::Bind(&ThreadProxy::InitializeImplOnImplThread,
565 base::Unretained(this),
566 &completion));
567 completion.Wait();
569 main_thread_weak_ptr_ = main().weak_factory.GetWeakPtr();
571 main().started = true;
574 void ThreadProxy::Stop() {
575 TRACE_EVENT0("cc", "ThreadProxy::Stop");
576 DCHECK(IsMainThread());
577 DCHECK(main().started);
579 // Synchronously finishes pending GL operations and deletes the impl.
580 // The two steps are done as separate post tasks, so that tasks posted
581 // by the GL implementation due to the Finish can be executed by the
582 // renderer before shutting it down.
584 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
586 CompletionEvent completion;
587 Proxy::ImplThreadTaskRunner()->PostTask(
588 FROM_HERE,
589 base::Bind(&ThreadProxy::FinishGLOnImplThread,
590 impl_thread_weak_ptr_,
591 &completion));
592 completion.Wait();
595 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
597 CompletionEvent completion;
598 Proxy::ImplThreadTaskRunner()->PostTask(
599 FROM_HERE,
600 base::Bind(&ThreadProxy::LayerTreeHostClosedOnImplThread,
601 impl_thread_weak_ptr_,
602 &completion));
603 completion.Wait();
606 main().weak_factory.InvalidateWeakPtrs();
607 blocked_main().layer_tree_host = NULL;
608 main().started = false;
611 void ThreadProxy::ForceSerializeOnSwapBuffers() {
612 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
613 CompletionEvent completion;
614 Proxy::ImplThreadTaskRunner()->PostTask(
615 FROM_HERE,
616 base::Bind(&ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread,
617 impl_thread_weak_ptr_,
618 &completion));
619 completion.Wait();
622 void ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread(
623 CompletionEvent* completion) {
624 if (impl().layer_tree_host_impl->renderer())
625 impl().layer_tree_host_impl->renderer()->DoNoOp();
626 completion->Signal();
629 bool ThreadProxy::SupportsImplScrolling() const {
630 return true;
633 void ThreadProxy::SetDebugState(const LayerTreeDebugState& debug_state) {
634 Proxy::ImplThreadTaskRunner()->PostTask(
635 FROM_HERE,
636 base::Bind(&ThreadProxy::SetDebugStateOnImplThread,
637 impl_thread_weak_ptr_,
638 debug_state));
641 void ThreadProxy::SetDebugStateOnImplThread(
642 const LayerTreeDebugState& debug_state) {
643 DCHECK(IsImplThread());
644 impl().scheduler->SetContinuousPainting(debug_state.continuous_painting);
647 void ThreadProxy::FinishAllRenderingOnImplThread(CompletionEvent* completion) {
648 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread");
649 DCHECK(IsImplThread());
650 impl().layer_tree_host_impl->FinishAllRendering();
651 completion->Signal();
654 void ThreadProxy::ScheduledActionSendBeginMainFrame() {
655 unsigned int begin_frame_id = nextBeginFrameId++;
656 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
657 benchmark_instrumentation::kSendBeginFrame, begin_frame_id);
658 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state(
659 new BeginMainFrameAndCommitState);
660 begin_main_frame_state->begin_frame_id = begin_frame_id;
661 begin_main_frame_state->begin_frame_args =
662 impl().layer_tree_host_impl->CurrentBeginFrameArgs();
663 begin_main_frame_state->scroll_info =
664 impl().layer_tree_host_impl->ProcessScrollDeltas();
665 begin_main_frame_state->memory_allocation_limit_bytes =
666 impl().layer_tree_host_impl->memory_allocation_limit_bytes();
667 begin_main_frame_state->evicted_ui_resources =
668 impl().layer_tree_host_impl->EvictedUIResourcesExist();
669 // TODO(vmpstr): This needs to be fixed if
670 // main_frame_before_activation_enabled is set, since we might run this code
671 // twice before recording a duration. crbug.com/469824
672 impl().last_begin_main_frame_args = begin_main_frame_state->begin_frame_args;
673 Proxy::MainThreadTaskRunner()->PostTask(
674 FROM_HERE,
675 base::Bind(&ThreadProxy::BeginMainFrame,
676 main_thread_weak_ptr_,
677 base::Passed(&begin_main_frame_state)));
678 devtools_instrumentation::DidRequestMainThreadFrame(
679 impl().layer_tree_host_id);
682 void ThreadProxy::SendBeginMainFrameNotExpectedSoon() {
683 Proxy::MainThreadTaskRunner()->PostTask(
684 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameNotExpectedSoon,
685 main_thread_weak_ptr_));
688 void ThreadProxy::BeginMainFrame(
689 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
690 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
691 benchmark_instrumentation::kDoBeginFrame,
692 begin_main_frame_state->begin_frame_id);
693 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
694 DCHECK(IsMainThread());
695 DCHECK_EQ(NO_PIPELINE_STAGE, main().current_pipeline_stage);
697 if (main().defer_commits) {
698 TRACE_EVENT_INSTANT0("cc", "EarlyOut_DeferCommit",
699 TRACE_EVENT_SCOPE_THREAD);
700 Proxy::ImplThreadTaskRunner()->PostTask(
701 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
702 impl_thread_weak_ptr_,
703 CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT));
704 return;
707 // If the commit finishes, LayerTreeHost will transfer its swap promises to
708 // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the
709 // remaining swap promises.
710 ScopedAbortRemainingSwapPromises swap_promise_checker(layer_tree_host());
712 main().final_pipeline_stage = main().max_requested_pipeline_stage;
713 main().max_requested_pipeline_stage = NO_PIPELINE_STAGE;
715 if (!layer_tree_host()->visible()) {
716 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
717 Proxy::ImplThreadTaskRunner()->PostTask(
718 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
719 impl_thread_weak_ptr_,
720 CommitEarlyOutReason::ABORTED_NOT_VISIBLE));
721 return;
724 if (layer_tree_host()->output_surface_lost()) {
725 TRACE_EVENT_INSTANT0(
726 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD);
727 Proxy::ImplThreadTaskRunner()->PostTask(
728 FROM_HERE,
729 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
730 impl_thread_weak_ptr_,
731 CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST));
732 return;
735 main().current_pipeline_stage = ANIMATE_PIPELINE_STAGE;
737 layer_tree_host()->ApplyScrollAndScale(
738 begin_main_frame_state->scroll_info.get());
740 layer_tree_host()->WillBeginMainFrame();
742 layer_tree_host()->BeginMainFrame(begin_main_frame_state->begin_frame_args);
743 layer_tree_host()->AnimateLayers(
744 begin_main_frame_state->begin_frame_args.frame_time);
746 // Recreate all UI resources if there were evicted UI resources when the impl
747 // thread initiated the commit.
748 if (begin_main_frame_state->evicted_ui_resources)
749 layer_tree_host()->RecreateUIResources();
751 layer_tree_host()->Layout();
752 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
754 bool can_cancel_this_commit =
755 main().final_pipeline_stage < COMMIT_PIPELINE_STAGE &&
756 !begin_main_frame_state->evicted_ui_resources;
758 main().current_pipeline_stage = UPDATE_LAYERS_PIPELINE_STAGE;
759 bool should_update_layers =
760 main().final_pipeline_stage >= UPDATE_LAYERS_PIPELINE_STAGE;
761 bool updated = should_update_layers && layer_tree_host()->UpdateLayers();
763 layer_tree_host()->WillCommit();
764 devtools_instrumentation::ScopedCommitTrace commit_task(
765 layer_tree_host()->id());
767 main().current_pipeline_stage = COMMIT_PIPELINE_STAGE;
768 if (!updated && can_cancel_this_commit) {
769 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
770 Proxy::ImplThreadTaskRunner()->PostTask(
771 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
772 impl_thread_weak_ptr_,
773 CommitEarlyOutReason::FINISHED_NO_UPDATES));
775 // Although the commit is internally aborted, this is because it has been
776 // detected to be a no-op. From the perspective of an embedder, this commit
777 // went through, and input should no longer be throttled, etc.
778 main().current_pipeline_stage = NO_PIPELINE_STAGE;
779 layer_tree_host()->CommitComplete();
780 layer_tree_host()->DidBeginMainFrame();
781 layer_tree_host()->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE);
782 return;
785 // Notify the impl thread that the main thread is ready to commit. This will
786 // begin the commit process, which is blocking from the main thread's
787 // point of view, but asynchronously performed on the impl thread,
788 // coordinated by the Scheduler.
790 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit");
792 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
794 // This CapturePostTasks should be destroyed before CommitComplete() is
795 // called since that goes out to the embedder, and we want the embedder
796 // to receive its callbacks before that.
797 BlockingTaskRunner::CapturePostTasks blocked(
798 blocking_main_thread_task_runner());
800 CompletionEvent completion;
801 Proxy::ImplThreadTaskRunner()->PostTask(
802 FROM_HERE, base::Bind(&ThreadProxy::StartCommitOnImplThread,
803 impl_thread_weak_ptr_, &completion));
804 completion.Wait();
807 main().current_pipeline_stage = NO_PIPELINE_STAGE;
808 layer_tree_host()->CommitComplete();
809 layer_tree_host()->DidBeginMainFrame();
812 void ThreadProxy::BeginMainFrameNotExpectedSoon() {
813 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameNotExpectedSoon");
814 DCHECK(IsMainThread());
815 layer_tree_host()->BeginMainFrameNotExpectedSoon();
818 void ThreadProxy::StartCommitOnImplThread(CompletionEvent* completion) {
819 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread");
820 DCHECK(!impl().commit_completion_event);
821 DCHECK(IsImplThread() && IsMainThreadBlocked());
822 DCHECK(impl().scheduler);
823 DCHECK(impl().scheduler->CommitPending());
825 if (!impl().layer_tree_host_impl) {
826 TRACE_EVENT_INSTANT0(
827 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD);
828 completion->Signal();
829 return;
832 // Ideally, we should inform to impl thread when BeginMainFrame is started.
833 // But, we can avoid a PostTask in here.
834 impl().scheduler->NotifyBeginMainFrameStarted();
835 impl().commit_completion_event = completion;
836 impl().scheduler->NotifyReadyToCommit();
839 void ThreadProxy::BeginMainFrameAbortedOnImplThread(
840 CommitEarlyOutReason reason) {
841 TRACE_EVENT1("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread", "reason",
842 CommitEarlyOutReasonToString(reason));
843 DCHECK(IsImplThread());
844 DCHECK(impl().scheduler);
845 DCHECK(impl().scheduler->CommitPending());
846 DCHECK(!impl().layer_tree_host_impl->pending_tree());
848 if (CommitEarlyOutHandledCommit(reason)) {
849 SetInputThrottledUntilCommitOnImplThread(false);
850 impl().last_processed_begin_main_frame_args =
851 impl().last_begin_main_frame_args;
853 impl().layer_tree_host_impl->BeginMainFrameAborted(reason);
854 impl().scheduler->BeginMainFrameAborted(reason);
857 void ThreadProxy::ScheduledActionAnimate() {
858 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate");
859 DCHECK(IsImplThread());
861 impl().layer_tree_host_impl->Animate();
864 void ThreadProxy::ScheduledActionCommit() {
865 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit");
866 DCHECK(IsImplThread());
867 DCHECK(IsMainThreadBlocked());
868 DCHECK(impl().commit_completion_event);
870 blocked_main().main_thread_inside_commit = true;
871 impl().layer_tree_host_impl->BeginCommit();
872 layer_tree_host()->FinishCommitOnImplThread(
873 impl().layer_tree_host_impl.get());
874 blocked_main().main_thread_inside_commit = false;
876 bool hold_commit = blocked_main().commit_waits_for_activation;
877 blocked_main().commit_waits_for_activation = false;
879 if (hold_commit) {
880 // For some layer types in impl-side painting, the commit is held until
881 // the sync tree is activated. It's also possible that the
882 // sync tree has already activated if there was no work to be done.
883 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD);
884 impl().completion_event_for_commit_held_on_tree_activation =
885 impl().commit_completion_event;
886 impl().commit_completion_event = NULL;
887 } else {
888 impl().commit_completion_event->Signal();
889 impl().commit_completion_event = NULL;
892 impl().scheduler->DidCommit();
894 // Delay this step until afer the main thread has been released as it's
895 // often a good bit of work to update the tree and prepare the new frame.
896 impl().layer_tree_host_impl->CommitComplete();
898 SetInputThrottledUntilCommitOnImplThread(false);
900 impl().next_frame_is_newly_committed_frame = true;
903 void ThreadProxy::ScheduledActionActivateSyncTree() {
904 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree");
905 DCHECK(IsImplThread());
906 impl().layer_tree_host_impl->ActivateSyncTree();
909 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() {
910 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation");
911 DCHECK(IsImplThread());
912 Proxy::MainThreadTaskRunner()->PostTask(
913 FROM_HERE,
914 base::Bind(&ThreadProxy::RequestNewOutputSurface, main_thread_weak_ptr_));
917 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) {
918 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap");
919 DrawResult result;
921 DCHECK(IsImplThread());
922 DCHECK(impl().layer_tree_host_impl.get());
924 base::AutoReset<bool> mark_inside(&impl().inside_draw, true);
926 if (impl().layer_tree_host_impl->pending_tree()) {
927 bool update_lcd_text = false;
928 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties(
929 update_lcd_text);
932 // This method is called on a forced draw, regardless of whether we are able
933 // to produce a frame, as the calling site on main thread is blocked until its
934 // request completes, and we signal completion here. If CanDraw() is false, we
935 // will indicate success=false to the caller, but we must still signal
936 // completion to avoid deadlock.
938 // We guard PrepareToDraw() with CanDraw() because it always returns a valid
939 // frame, so can only be used when such a frame is possible. Since
940 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
941 // CanDraw() as well.
943 LayerTreeHostImpl::FrameData frame;
944 bool draw_frame = false;
946 if (impl().layer_tree_host_impl->CanDraw()) {
947 result = impl().layer_tree_host_impl->PrepareToDraw(&frame);
948 draw_frame = forced_draw || result == DRAW_SUCCESS;
949 } else {
950 result = DRAW_ABORTED_CANT_DRAW;
953 if (draw_frame) {
954 impl().layer_tree_host_impl->DrawLayers(&frame);
955 result = DRAW_SUCCESS;
956 } else {
957 DCHECK_NE(DRAW_SUCCESS, result);
959 impl().layer_tree_host_impl->DidDrawAllLayers(frame);
961 bool start_ready_animations = draw_frame;
962 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations);
964 if (draw_frame)
965 impl().layer_tree_host_impl->SwapBuffers(frame);
967 // Tell the main thread that the the newly-commited frame was drawn.
968 if (impl().next_frame_is_newly_committed_frame) {
969 impl().next_frame_is_newly_committed_frame = false;
970 Proxy::MainThreadTaskRunner()->PostTask(
971 FROM_HERE,
972 base::Bind(&ThreadProxy::DidCommitAndDrawFrame, main_thread_weak_ptr_));
975 DCHECK_NE(INVALID_RESULT, result);
976 return result;
979 void ThreadProxy::ScheduledActionPrepareTiles() {
980 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionPrepareTiles");
981 impl().layer_tree_host_impl->PrepareTiles();
984 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() {
985 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap");
987 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to
988 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should
989 // never generate this call when it can't draw.
990 DCHECK(impl().layer_tree_host_impl->CanDraw());
992 bool forced_draw = false;
993 return DrawSwapInternal(forced_draw);
996 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() {
997 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced");
998 bool forced_draw = true;
999 return DrawSwapInternal(forced_draw);
1002 void ThreadProxy::ScheduledActionInvalidateOutputSurface() {
1003 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionInvalidateOutputSurface");
1004 DCHECK(impl().layer_tree_host_impl->output_surface());
1005 impl().layer_tree_host_impl->output_surface()->Invalidate();
1008 void ThreadProxy::DidFinishImplFrame() {
1009 impl().layer_tree_host_impl->DidFinishImplFrame();
1012 void ThreadProxy::SendBeginFramesToChildren(const BeginFrameArgs& args) {
1013 NOTREACHED() << "Only used by SingleThreadProxy";
1016 void ThreadProxy::SetAuthoritativeVSyncInterval(
1017 const base::TimeDelta& interval) {
1018 NOTREACHED() << "Only used by SingleThreadProxy";
1021 void ThreadProxy::DidCommitAndDrawFrame() {
1022 DCHECK(IsMainThread());
1023 layer_tree_host()->DidCommitAndDrawFrame();
1026 void ThreadProxy::DidCompleteSwapBuffers() {
1027 DCHECK(IsMainThread());
1028 layer_tree_host()->DidCompleteSwapBuffers();
1031 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
1032 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents");
1033 DCHECK(IsMainThread());
1034 layer_tree_host()->SetAnimationEvents(events.Pass());
1037 void ThreadProxy::InitializeImplOnImplThread(CompletionEvent* completion) {
1038 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread");
1039 DCHECK(IsImplThread());
1040 impl().layer_tree_host_impl =
1041 layer_tree_host()->CreateLayerTreeHostImpl(this);
1043 SchedulerSettings scheduler_settings(
1044 layer_tree_host()->settings().ToSchedulerSettings());
1046 scoped_ptr<CompositorTimingHistory> compositor_timing_history(
1047 new CompositorTimingHistory(CompositorTimingHistory::RENDERER_UMA,
1048 impl().rendering_stats_instrumentation));
1050 impl().scheduler = Scheduler::Create(
1051 this, scheduler_settings, impl().layer_tree_host_id,
1052 ImplThreadTaskRunner(), impl().external_begin_frame_source.get(),
1053 compositor_timing_history.Pass());
1055 impl().scheduler->SetVisible(impl().layer_tree_host_impl->visible());
1056 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr();
1057 completion->Signal();
1060 void ThreadProxy::InitializeOutputSurfaceOnImplThread(
1061 scoped_ptr<OutputSurface> output_surface) {
1062 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread");
1063 DCHECK(IsImplThread());
1065 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get();
1066 bool success = host_impl->InitializeRenderer(output_surface.Pass());
1067 RendererCapabilities capabilities;
1068 if (success) {
1069 capabilities =
1070 host_impl->GetRendererCapabilities().MainThreadCapabilities();
1073 Proxy::MainThreadTaskRunner()->PostTask(
1074 FROM_HERE,
1075 base::Bind(&ThreadProxy::DidInitializeOutputSurface,
1076 main_thread_weak_ptr_,
1077 success,
1078 capabilities));
1080 if (success)
1081 impl().scheduler->DidCreateAndInitializeOutputSurface();
1084 void ThreadProxy::FinishGLOnImplThread(CompletionEvent* completion) {
1085 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread");
1086 DCHECK(IsImplThread());
1087 if (impl().layer_tree_host_impl->output_surface()) {
1088 ContextProvider* context_provider =
1089 impl().layer_tree_host_impl->output_surface()->context_provider();
1090 if (context_provider)
1091 context_provider->ContextGL()->Finish();
1093 completion->Signal();
1096 void ThreadProxy::LayerTreeHostClosedOnImplThread(CompletionEvent* completion) {
1097 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread");
1098 DCHECK(IsImplThread());
1099 DCHECK(IsMainThreadBlocked());
1100 impl().scheduler = nullptr;
1101 impl().external_begin_frame_source = nullptr;
1102 impl().layer_tree_host_impl = nullptr;
1103 impl().weak_factory.InvalidateWeakPtrs();
1104 // We need to explicitly shutdown the notifier to destroy any weakptrs it is
1105 // holding while still on the compositor thread. This also ensures any
1106 // callbacks holding a ThreadProxy pointer are cancelled.
1107 impl().smoothness_priority_expiration_notifier.Shutdown();
1108 completion->Signal();
1111 ThreadProxy::BeginMainFrameAndCommitState::BeginMainFrameAndCommitState()
1112 : memory_allocation_limit_bytes(0),
1113 evicted_ui_resources(false) {}
1115 ThreadProxy::BeginMainFrameAndCommitState::~BeginMainFrameAndCommitState() {}
1117 bool ThreadProxy::MainFrameWillHappenForTesting() {
1118 DCHECK(IsMainThread());
1119 CompletionEvent completion;
1120 bool main_frame_will_happen = false;
1122 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1123 Proxy::ImplThreadTaskRunner()->PostTask(
1124 FROM_HERE,
1125 base::Bind(&ThreadProxy::MainFrameWillHappenOnImplThreadForTesting,
1126 impl_thread_weak_ptr_,
1127 &completion,
1128 &main_frame_will_happen));
1129 completion.Wait();
1131 return main_frame_will_happen;
1134 void ThreadProxy::SetChildrenNeedBeginFrames(bool children_need_begin_frames) {
1135 NOTREACHED() << "Only used by SingleThreadProxy";
1138 void ThreadProxy::MainFrameWillHappenOnImplThreadForTesting(
1139 CompletionEvent* completion,
1140 bool* main_frame_will_happen) {
1141 DCHECK(IsImplThread());
1142 if (impl().layer_tree_host_impl->output_surface()) {
1143 *main_frame_will_happen = impl().scheduler->MainFrameForTestingWillHappen();
1144 } else {
1145 *main_frame_will_happen = false;
1147 completion->Signal();
1150 void ThreadProxy::RenewTreePriority() {
1151 DCHECK(IsImplThread());
1152 bool smoothness_takes_priority =
1153 impl().layer_tree_host_impl->pinch_gesture_active() ||
1154 impl().layer_tree_host_impl->page_scale_animation_active() ||
1155 impl().layer_tree_host_impl->IsActivelyScrolling();
1157 // Schedule expiration if smoothness currently takes priority.
1158 if (smoothness_takes_priority)
1159 impl().smoothness_priority_expiration_notifier.Schedule();
1161 // We use the same priority for both trees by default.
1162 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES;
1164 // Smoothness takes priority if we have an expiration for it scheduled.
1165 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification())
1166 priority = SMOOTHNESS_TAKES_PRIORITY;
1168 // New content always takes priority when there is an invalid viewport size or
1169 // ui resources have been evicted.
1170 if (impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() ||
1171 impl().layer_tree_host_impl->EvictedUIResourcesExist() ||
1172 impl().input_throttled_until_commit) {
1173 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active
1174 // tree might be freed. We need to set RequiresHighResToDraw to ensure that
1175 // high res tiles will be required to activate pending tree.
1176 impl().layer_tree_host_impl->SetRequiresHighResToDraw();
1177 priority = NEW_CONTENT_TAKES_PRIORITY;
1180 impl().layer_tree_host_impl->SetTreePriority(priority);
1182 // Only put the scheduler in impl latency prioritization mode if we don't
1183 // have a scroll listener. This gives the scroll listener a better chance of
1184 // handling scroll updates within the same frame. The tree itself is still
1185 // kept in prefer smoothness mode to allow checkerboarding.
1186 impl().scheduler->SetImplLatencyTakesPriority(
1187 priority == SMOOTHNESS_TAKES_PRIORITY &&
1188 !impl().layer_tree_host_impl->scroll_affects_scroll_handler());
1190 // Notify the the client of this compositor via the output surface.
1191 // TODO(epenner): Route this to compositor-thread instead of output-surface
1192 // after GTFO refactor of compositor-thread (http://crbug/170828).
1193 if (impl().layer_tree_host_impl->output_surface()) {
1194 impl()
1195 .layer_tree_host_impl->output_surface()
1196 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY);
1200 void ThreadProxy::PostDelayedAnimationTaskOnImplThread(
1201 const base::Closure& task,
1202 base::TimeDelta delay) {
1203 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, task, delay);
1206 void ThreadProxy::DidActivateSyncTree() {
1207 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread");
1208 DCHECK(IsImplThread());
1210 if (impl().completion_event_for_commit_held_on_tree_activation) {
1211 TRACE_EVENT_INSTANT0(
1212 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD);
1213 impl().completion_event_for_commit_held_on_tree_activation->Signal();
1214 impl().completion_event_for_commit_held_on_tree_activation = NULL;
1217 impl().last_processed_begin_main_frame_args =
1218 impl().last_begin_main_frame_args;
1221 void ThreadProxy::WillPrepareTiles() {
1222 DCHECK(IsImplThread());
1223 impl().scheduler->WillPrepareTiles();
1226 void ThreadProxy::DidPrepareTiles() {
1227 DCHECK(IsImplThread());
1228 impl().scheduler->DidPrepareTiles();
1231 void ThreadProxy::DidCompletePageScaleAnimationOnImplThread() {
1232 DCHECK(IsImplThread());
1233 Proxy::MainThreadTaskRunner()->PostTask(
1234 FROM_HERE, base::Bind(&ThreadProxy::DidCompletePageScaleAnimation,
1235 main_thread_weak_ptr_));
1238 void ThreadProxy::OnDrawForOutputSurface() {
1239 DCHECK(IsImplThread());
1240 impl().scheduler->OnDrawForOutputSurface();
1243 void ThreadProxy::PostFrameTimingEventsOnImplThread(
1244 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
1245 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
1246 DCHECK(IsImplThread());
1247 Proxy::MainThreadTaskRunner()->PostTask(
1248 FROM_HERE,
1249 base::Bind(&ThreadProxy::PostFrameTimingEvents, main_thread_weak_ptr_,
1250 base::Passed(composite_events.Pass()),
1251 base::Passed(main_frame_events.Pass())));
1254 void ThreadProxy::PostFrameTimingEvents(
1255 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
1256 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
1257 DCHECK(IsMainThread());
1258 layer_tree_host()->RecordFrameTimingEvents(composite_events.Pass(),
1259 main_frame_events.Pass());
1262 } // namespace cc