Revert of cc: Implement shared worker contexts. (patchset #9 id:160001 of https:...
[chromium-blink-merge.git] / cc / trees / thread_proxy.cc
blobef49ff85d4d4081c011fe32c50a5ae0889458775
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::FinishAllRenderingOnImplThread(CompletionEvent* completion) {
634 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread");
635 DCHECK(IsImplThread());
636 impl().layer_tree_host_impl->FinishAllRendering();
637 completion->Signal();
640 void ThreadProxy::ScheduledActionSendBeginMainFrame() {
641 unsigned int begin_frame_id = nextBeginFrameId++;
642 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
643 benchmark_instrumentation::kSendBeginFrame, begin_frame_id);
644 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state(
645 new BeginMainFrameAndCommitState);
646 begin_main_frame_state->begin_frame_id = begin_frame_id;
647 begin_main_frame_state->begin_frame_args =
648 impl().layer_tree_host_impl->CurrentBeginFrameArgs();
649 begin_main_frame_state->scroll_info =
650 impl().layer_tree_host_impl->ProcessScrollDeltas();
651 begin_main_frame_state->memory_allocation_limit_bytes =
652 impl().layer_tree_host_impl->memory_allocation_limit_bytes();
653 begin_main_frame_state->evicted_ui_resources =
654 impl().layer_tree_host_impl->EvictedUIResourcesExist();
655 // TODO(vmpstr): This needs to be fixed if
656 // main_frame_before_activation_enabled is set, since we might run this code
657 // twice before recording a duration. crbug.com/469824
658 impl().last_begin_main_frame_args = begin_main_frame_state->begin_frame_args;
659 Proxy::MainThreadTaskRunner()->PostTask(
660 FROM_HERE,
661 base::Bind(&ThreadProxy::BeginMainFrame,
662 main_thread_weak_ptr_,
663 base::Passed(&begin_main_frame_state)));
664 devtools_instrumentation::DidRequestMainThreadFrame(
665 impl().layer_tree_host_id);
668 void ThreadProxy::SendBeginMainFrameNotExpectedSoon() {
669 Proxy::MainThreadTaskRunner()->PostTask(
670 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameNotExpectedSoon,
671 main_thread_weak_ptr_));
674 void ThreadProxy::BeginMainFrame(
675 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
676 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
677 benchmark_instrumentation::kDoBeginFrame,
678 begin_main_frame_state->begin_frame_id);
679 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
680 DCHECK(IsMainThread());
681 DCHECK_EQ(NO_PIPELINE_STAGE, main().current_pipeline_stage);
683 if (main().defer_commits) {
684 TRACE_EVENT_INSTANT0("cc", "EarlyOut_DeferCommit",
685 TRACE_EVENT_SCOPE_THREAD);
686 Proxy::ImplThreadTaskRunner()->PostTask(
687 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
688 impl_thread_weak_ptr_,
689 CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT));
690 return;
693 // If the commit finishes, LayerTreeHost will transfer its swap promises to
694 // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the
695 // remaining swap promises.
696 ScopedAbortRemainingSwapPromises swap_promise_checker(layer_tree_host());
698 main().final_pipeline_stage = main().max_requested_pipeline_stage;
699 main().max_requested_pipeline_stage = NO_PIPELINE_STAGE;
701 if (!layer_tree_host()->visible()) {
702 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
703 Proxy::ImplThreadTaskRunner()->PostTask(
704 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
705 impl_thread_weak_ptr_,
706 CommitEarlyOutReason::ABORTED_NOT_VISIBLE));
707 return;
710 if (layer_tree_host()->output_surface_lost()) {
711 TRACE_EVENT_INSTANT0(
712 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD);
713 Proxy::ImplThreadTaskRunner()->PostTask(
714 FROM_HERE,
715 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
716 impl_thread_weak_ptr_,
717 CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST));
718 return;
721 main().current_pipeline_stage = ANIMATE_PIPELINE_STAGE;
723 layer_tree_host()->ApplyScrollAndScale(
724 begin_main_frame_state->scroll_info.get());
726 layer_tree_host()->WillBeginMainFrame();
728 layer_tree_host()->BeginMainFrame(begin_main_frame_state->begin_frame_args);
729 layer_tree_host()->AnimateLayers(
730 begin_main_frame_state->begin_frame_args.frame_time);
732 // Recreate all UI resources if there were evicted UI resources when the impl
733 // thread initiated the commit.
734 if (begin_main_frame_state->evicted_ui_resources)
735 layer_tree_host()->RecreateUIResources();
737 layer_tree_host()->Layout();
738 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
740 bool can_cancel_this_commit =
741 main().final_pipeline_stage < COMMIT_PIPELINE_STAGE &&
742 !begin_main_frame_state->evicted_ui_resources;
744 main().current_pipeline_stage = UPDATE_LAYERS_PIPELINE_STAGE;
745 bool should_update_layers =
746 main().final_pipeline_stage >= UPDATE_LAYERS_PIPELINE_STAGE;
747 bool updated = should_update_layers && layer_tree_host()->UpdateLayers();
749 layer_tree_host()->WillCommit();
750 devtools_instrumentation::ScopedCommitTrace commit_task(
751 layer_tree_host()->id());
753 main().current_pipeline_stage = COMMIT_PIPELINE_STAGE;
754 if (!updated && can_cancel_this_commit) {
755 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
756 Proxy::ImplThreadTaskRunner()->PostTask(
757 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
758 impl_thread_weak_ptr_,
759 CommitEarlyOutReason::FINISHED_NO_UPDATES));
761 // Although the commit is internally aborted, this is because it has been
762 // detected to be a no-op. From the perspective of an embedder, this commit
763 // went through, and input should no longer be throttled, etc.
764 main().current_pipeline_stage = NO_PIPELINE_STAGE;
765 layer_tree_host()->CommitComplete();
766 layer_tree_host()->DidBeginMainFrame();
767 layer_tree_host()->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE);
768 return;
771 // Notify the impl thread that the main thread is ready to commit. This will
772 // begin the commit process, which is blocking from the main thread's
773 // point of view, but asynchronously performed on the impl thread,
774 // coordinated by the Scheduler.
776 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit");
778 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
780 // This CapturePostTasks should be destroyed before CommitComplete() is
781 // called since that goes out to the embedder, and we want the embedder
782 // to receive its callbacks before that.
783 BlockingTaskRunner::CapturePostTasks blocked(
784 blocking_main_thread_task_runner());
786 CompletionEvent completion;
787 Proxy::ImplThreadTaskRunner()->PostTask(
788 FROM_HERE, base::Bind(&ThreadProxy::StartCommitOnImplThread,
789 impl_thread_weak_ptr_, &completion));
790 completion.Wait();
793 main().current_pipeline_stage = NO_PIPELINE_STAGE;
794 layer_tree_host()->CommitComplete();
795 layer_tree_host()->DidBeginMainFrame();
798 void ThreadProxy::BeginMainFrameNotExpectedSoon() {
799 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameNotExpectedSoon");
800 DCHECK(IsMainThread());
801 layer_tree_host()->BeginMainFrameNotExpectedSoon();
804 void ThreadProxy::StartCommitOnImplThread(CompletionEvent* completion) {
805 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread");
806 DCHECK(!impl().commit_completion_event);
807 DCHECK(IsImplThread() && IsMainThreadBlocked());
808 DCHECK(impl().scheduler);
809 DCHECK(impl().scheduler->CommitPending());
811 if (!impl().layer_tree_host_impl) {
812 TRACE_EVENT_INSTANT0(
813 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD);
814 completion->Signal();
815 return;
818 // Ideally, we should inform to impl thread when BeginMainFrame is started.
819 // But, we can avoid a PostTask in here.
820 impl().scheduler->NotifyBeginMainFrameStarted();
821 impl().commit_completion_event = completion;
822 impl().scheduler->NotifyReadyToCommit();
825 void ThreadProxy::BeginMainFrameAbortedOnImplThread(
826 CommitEarlyOutReason reason) {
827 TRACE_EVENT1("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread", "reason",
828 CommitEarlyOutReasonToString(reason));
829 DCHECK(IsImplThread());
830 DCHECK(impl().scheduler);
831 DCHECK(impl().scheduler->CommitPending());
832 DCHECK(!impl().layer_tree_host_impl->pending_tree());
834 if (CommitEarlyOutHandledCommit(reason)) {
835 SetInputThrottledUntilCommitOnImplThread(false);
836 impl().last_processed_begin_main_frame_args =
837 impl().last_begin_main_frame_args;
839 impl().layer_tree_host_impl->BeginMainFrameAborted(reason);
840 impl().scheduler->BeginMainFrameAborted(reason);
843 void ThreadProxy::ScheduledActionAnimate() {
844 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate");
845 DCHECK(IsImplThread());
847 impl().layer_tree_host_impl->Animate();
850 void ThreadProxy::ScheduledActionCommit() {
851 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit");
852 DCHECK(IsImplThread());
853 DCHECK(IsMainThreadBlocked());
854 DCHECK(impl().commit_completion_event);
856 blocked_main().main_thread_inside_commit = true;
857 impl().layer_tree_host_impl->BeginCommit();
858 layer_tree_host()->FinishCommitOnImplThread(
859 impl().layer_tree_host_impl.get());
860 blocked_main().main_thread_inside_commit = false;
862 bool hold_commit = blocked_main().commit_waits_for_activation;
863 blocked_main().commit_waits_for_activation = false;
865 if (hold_commit) {
866 // For some layer types in impl-side painting, the commit is held until
867 // the sync tree is activated. It's also possible that the
868 // sync tree has already activated if there was no work to be done.
869 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD);
870 impl().completion_event_for_commit_held_on_tree_activation =
871 impl().commit_completion_event;
872 impl().commit_completion_event = NULL;
873 } else {
874 impl().commit_completion_event->Signal();
875 impl().commit_completion_event = NULL;
878 impl().scheduler->DidCommit();
880 // Delay this step until afer the main thread has been released as it's
881 // often a good bit of work to update the tree and prepare the new frame.
882 impl().layer_tree_host_impl->CommitComplete();
884 SetInputThrottledUntilCommitOnImplThread(false);
886 impl().next_frame_is_newly_committed_frame = true;
889 void ThreadProxy::ScheduledActionActivateSyncTree() {
890 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree");
891 DCHECK(IsImplThread());
892 impl().layer_tree_host_impl->ActivateSyncTree();
895 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() {
896 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation");
897 DCHECK(IsImplThread());
898 Proxy::MainThreadTaskRunner()->PostTask(
899 FROM_HERE,
900 base::Bind(&ThreadProxy::RequestNewOutputSurface, main_thread_weak_ptr_));
903 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) {
904 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap");
905 DrawResult result;
907 DCHECK(IsImplThread());
908 DCHECK(impl().layer_tree_host_impl.get());
910 base::AutoReset<bool> mark_inside(&impl().inside_draw, true);
912 if (impl().layer_tree_host_impl->pending_tree()) {
913 bool update_lcd_text = false;
914 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties(
915 update_lcd_text);
918 // This method is called on a forced draw, regardless of whether we are able
919 // to produce a frame, as the calling site on main thread is blocked until its
920 // request completes, and we signal completion here. If CanDraw() is false, we
921 // will indicate success=false to the caller, but we must still signal
922 // completion to avoid deadlock.
924 // We guard PrepareToDraw() with CanDraw() because it always returns a valid
925 // frame, so can only be used when such a frame is possible. Since
926 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
927 // CanDraw() as well.
929 LayerTreeHostImpl::FrameData frame;
930 bool draw_frame = false;
932 if (impl().layer_tree_host_impl->CanDraw()) {
933 result = impl().layer_tree_host_impl->PrepareToDraw(&frame);
934 draw_frame = forced_draw || result == DRAW_SUCCESS;
935 } else {
936 result = DRAW_ABORTED_CANT_DRAW;
939 if (draw_frame) {
940 impl().layer_tree_host_impl->DrawLayers(&frame);
941 result = DRAW_SUCCESS;
942 } else {
943 DCHECK_NE(DRAW_SUCCESS, result);
945 impl().layer_tree_host_impl->DidDrawAllLayers(frame);
947 bool start_ready_animations = draw_frame;
948 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations);
950 if (draw_frame)
951 impl().layer_tree_host_impl->SwapBuffers(frame);
953 // Tell the main thread that the the newly-commited frame was drawn.
954 if (impl().next_frame_is_newly_committed_frame) {
955 impl().next_frame_is_newly_committed_frame = false;
956 Proxy::MainThreadTaskRunner()->PostTask(
957 FROM_HERE,
958 base::Bind(&ThreadProxy::DidCommitAndDrawFrame, main_thread_weak_ptr_));
961 DCHECK_NE(INVALID_RESULT, result);
962 return result;
965 void ThreadProxy::ScheduledActionPrepareTiles() {
966 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionPrepareTiles");
967 impl().layer_tree_host_impl->PrepareTiles();
970 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() {
971 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap");
973 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to
974 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should
975 // never generate this call when it can't draw.
976 DCHECK(impl().layer_tree_host_impl->CanDraw());
978 bool forced_draw = false;
979 return DrawSwapInternal(forced_draw);
982 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() {
983 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced");
984 bool forced_draw = true;
985 return DrawSwapInternal(forced_draw);
988 void ThreadProxy::ScheduledActionInvalidateOutputSurface() {
989 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionInvalidateOutputSurface");
990 DCHECK(impl().layer_tree_host_impl->output_surface());
991 impl().layer_tree_host_impl->output_surface()->Invalidate();
994 void ThreadProxy::DidFinishImplFrame() {
995 impl().layer_tree_host_impl->DidFinishImplFrame();
998 void ThreadProxy::SendBeginFramesToChildren(const BeginFrameArgs& args) {
999 NOTREACHED() << "Only used by SingleThreadProxy";
1002 void ThreadProxy::SetAuthoritativeVSyncInterval(
1003 const base::TimeDelta& interval) {
1004 NOTREACHED() << "Only used by SingleThreadProxy";
1007 void ThreadProxy::DidCommitAndDrawFrame() {
1008 DCHECK(IsMainThread());
1009 layer_tree_host()->DidCommitAndDrawFrame();
1012 void ThreadProxy::DidCompleteSwapBuffers() {
1013 DCHECK(IsMainThread());
1014 layer_tree_host()->DidCompleteSwapBuffers();
1017 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
1018 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents");
1019 DCHECK(IsMainThread());
1020 layer_tree_host()->SetAnimationEvents(events.Pass());
1023 void ThreadProxy::InitializeImplOnImplThread(CompletionEvent* completion) {
1024 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread");
1025 DCHECK(IsImplThread());
1026 impl().layer_tree_host_impl =
1027 layer_tree_host()->CreateLayerTreeHostImpl(this);
1029 SchedulerSettings scheduler_settings(
1030 layer_tree_host()->settings().ToSchedulerSettings());
1032 scoped_ptr<CompositorTimingHistory> compositor_timing_history(
1033 new CompositorTimingHistory(CompositorTimingHistory::RENDERER_UMA,
1034 impl().rendering_stats_instrumentation));
1036 impl().scheduler = Scheduler::Create(
1037 this, scheduler_settings, impl().layer_tree_host_id,
1038 ImplThreadTaskRunner(), impl().external_begin_frame_source.get(),
1039 compositor_timing_history.Pass());
1041 impl().scheduler->SetVisible(impl().layer_tree_host_impl->visible());
1042 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr();
1043 completion->Signal();
1046 void ThreadProxy::InitializeOutputSurfaceOnImplThread(
1047 scoped_ptr<OutputSurface> output_surface) {
1048 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread");
1049 DCHECK(IsImplThread());
1051 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get();
1052 bool success = host_impl->InitializeRenderer(output_surface.Pass());
1053 RendererCapabilities capabilities;
1054 if (success) {
1055 capabilities =
1056 host_impl->GetRendererCapabilities().MainThreadCapabilities();
1059 Proxy::MainThreadTaskRunner()->PostTask(
1060 FROM_HERE,
1061 base::Bind(&ThreadProxy::DidInitializeOutputSurface,
1062 main_thread_weak_ptr_,
1063 success,
1064 capabilities));
1066 if (success)
1067 impl().scheduler->DidCreateAndInitializeOutputSurface();
1070 void ThreadProxy::FinishGLOnImplThread(CompletionEvent* completion) {
1071 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread");
1072 DCHECK(IsImplThread());
1073 if (impl().layer_tree_host_impl->output_surface()) {
1074 ContextProvider* context_provider =
1075 impl().layer_tree_host_impl->output_surface()->context_provider();
1076 if (context_provider)
1077 context_provider->ContextGL()->Finish();
1079 completion->Signal();
1082 void ThreadProxy::LayerTreeHostClosedOnImplThread(CompletionEvent* completion) {
1083 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread");
1084 DCHECK(IsImplThread());
1085 DCHECK(IsMainThreadBlocked());
1086 impl().scheduler = nullptr;
1087 impl().external_begin_frame_source = nullptr;
1088 impl().layer_tree_host_impl = nullptr;
1089 impl().weak_factory.InvalidateWeakPtrs();
1090 // We need to explicitly shutdown the notifier to destroy any weakptrs it is
1091 // holding while still on the compositor thread. This also ensures any
1092 // callbacks holding a ThreadProxy pointer are cancelled.
1093 impl().smoothness_priority_expiration_notifier.Shutdown();
1094 completion->Signal();
1097 ThreadProxy::BeginMainFrameAndCommitState::BeginMainFrameAndCommitState()
1098 : memory_allocation_limit_bytes(0),
1099 evicted_ui_resources(false) {}
1101 ThreadProxy::BeginMainFrameAndCommitState::~BeginMainFrameAndCommitState() {}
1103 bool ThreadProxy::MainFrameWillHappenForTesting() {
1104 DCHECK(IsMainThread());
1105 CompletionEvent completion;
1106 bool main_frame_will_happen = false;
1108 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1109 Proxy::ImplThreadTaskRunner()->PostTask(
1110 FROM_HERE,
1111 base::Bind(&ThreadProxy::MainFrameWillHappenOnImplThreadForTesting,
1112 impl_thread_weak_ptr_,
1113 &completion,
1114 &main_frame_will_happen));
1115 completion.Wait();
1117 return main_frame_will_happen;
1120 void ThreadProxy::SetChildrenNeedBeginFrames(bool children_need_begin_frames) {
1121 NOTREACHED() << "Only used by SingleThreadProxy";
1124 void ThreadProxy::MainFrameWillHappenOnImplThreadForTesting(
1125 CompletionEvent* completion,
1126 bool* main_frame_will_happen) {
1127 DCHECK(IsImplThread());
1128 if (impl().layer_tree_host_impl->output_surface()) {
1129 *main_frame_will_happen = impl().scheduler->MainFrameForTestingWillHappen();
1130 } else {
1131 *main_frame_will_happen = false;
1133 completion->Signal();
1136 void ThreadProxy::RenewTreePriority() {
1137 DCHECK(IsImplThread());
1138 bool smoothness_takes_priority =
1139 impl().layer_tree_host_impl->pinch_gesture_active() ||
1140 impl().layer_tree_host_impl->page_scale_animation_active() ||
1141 impl().layer_tree_host_impl->IsActivelyScrolling();
1143 // Schedule expiration if smoothness currently takes priority.
1144 if (smoothness_takes_priority)
1145 impl().smoothness_priority_expiration_notifier.Schedule();
1147 // We use the same priority for both trees by default.
1148 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES;
1150 // Smoothness takes priority if we have an expiration for it scheduled.
1151 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification())
1152 priority = SMOOTHNESS_TAKES_PRIORITY;
1154 // New content always takes priority when there is an invalid viewport size or
1155 // ui resources have been evicted.
1156 if (impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() ||
1157 impl().layer_tree_host_impl->EvictedUIResourcesExist() ||
1158 impl().input_throttled_until_commit) {
1159 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active
1160 // tree might be freed. We need to set RequiresHighResToDraw to ensure that
1161 // high res tiles will be required to activate pending tree.
1162 impl().layer_tree_host_impl->SetRequiresHighResToDraw();
1163 priority = NEW_CONTENT_TAKES_PRIORITY;
1166 impl().layer_tree_host_impl->SetTreePriority(priority);
1168 // Only put the scheduler in impl latency prioritization mode if we don't
1169 // have a scroll listener. This gives the scroll listener a better chance of
1170 // handling scroll updates within the same frame. The tree itself is still
1171 // kept in prefer smoothness mode to allow checkerboarding.
1172 impl().scheduler->SetImplLatencyTakesPriority(
1173 priority == SMOOTHNESS_TAKES_PRIORITY &&
1174 !impl().layer_tree_host_impl->scroll_affects_scroll_handler());
1176 // Notify the the client of this compositor via the output surface.
1177 // TODO(epenner): Route this to compositor-thread instead of output-surface
1178 // after GTFO refactor of compositor-thread (http://crbug/170828).
1179 if (impl().layer_tree_host_impl->output_surface()) {
1180 impl()
1181 .layer_tree_host_impl->output_surface()
1182 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY);
1186 void ThreadProxy::PostDelayedAnimationTaskOnImplThread(
1187 const base::Closure& task,
1188 base::TimeDelta delay) {
1189 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, task, delay);
1192 void ThreadProxy::DidActivateSyncTree() {
1193 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread");
1194 DCHECK(IsImplThread());
1196 if (impl().completion_event_for_commit_held_on_tree_activation) {
1197 TRACE_EVENT_INSTANT0(
1198 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD);
1199 impl().completion_event_for_commit_held_on_tree_activation->Signal();
1200 impl().completion_event_for_commit_held_on_tree_activation = NULL;
1203 impl().last_processed_begin_main_frame_args =
1204 impl().last_begin_main_frame_args;
1207 void ThreadProxy::WillPrepareTiles() {
1208 DCHECK(IsImplThread());
1209 impl().scheduler->WillPrepareTiles();
1212 void ThreadProxy::DidPrepareTiles() {
1213 DCHECK(IsImplThread());
1214 impl().scheduler->DidPrepareTiles();
1217 void ThreadProxy::DidCompletePageScaleAnimationOnImplThread() {
1218 DCHECK(IsImplThread());
1219 Proxy::MainThreadTaskRunner()->PostTask(
1220 FROM_HERE, base::Bind(&ThreadProxy::DidCompletePageScaleAnimation,
1221 main_thread_weak_ptr_));
1224 void ThreadProxy::OnDrawForOutputSurface() {
1225 DCHECK(IsImplThread());
1226 impl().scheduler->OnDrawForOutputSurface();
1229 void ThreadProxy::PostFrameTimingEventsOnImplThread(
1230 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
1231 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
1232 DCHECK(IsImplThread());
1233 Proxy::MainThreadTaskRunner()->PostTask(
1234 FROM_HERE,
1235 base::Bind(&ThreadProxy::PostFrameTimingEvents, main_thread_weak_ptr_,
1236 base::Passed(composite_events.Pass()),
1237 base::Passed(main_frame_events.Pass())));
1240 void ThreadProxy::PostFrameTimingEvents(
1241 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
1242 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
1243 DCHECK(IsMainThread());
1244 layer_tree_host()->RecordFrameTimingEvents(composite_events.Pass(),
1245 main_frame_events.Pass());
1248 } // namespace cc