Get rid of the rest of CreateStringValue
[chromium-blink-merge.git] / cc / trees / thread_proxy.cc
blob941d7df18ff2a8e23820eba9fb6bdc6f640161b9
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_synthetic_delay.h"
14 #include "base/metrics/histogram.h"
15 #include "cc/base/swap_promise.h"
16 #include "cc/debug/benchmark_instrumentation.h"
17 #include "cc/debug/devtools_instrumentation.h"
18 #include "cc/input/input_handler.h"
19 #include "cc/output/context_provider.h"
20 #include "cc/output/output_surface.h"
21 #include "cc/quads/draw_quad.h"
22 #include "cc/resources/prioritized_resource_manager.h"
23 #include "cc/scheduler/delay_based_time_source.h"
24 #include "cc/scheduler/scheduler.h"
25 #include "cc/trees/blocking_task_runner.h"
26 #include "cc/trees/layer_tree_host.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "gpu/command_buffer/client/gles2_interface.h"
29 #include "ui/gfx/frame_time.h"
31 namespace {
33 // Measured in seconds.
34 const double kSmoothnessTakesPriorityExpirationDelay = 0.25;
36 unsigned int nextBeginFrameId = 0;
38 class SwapPromiseChecker {
39 public:
40 explicit SwapPromiseChecker(cc::LayerTreeHost* layer_tree_host)
41 : layer_tree_host_(layer_tree_host) {}
43 ~SwapPromiseChecker() {
44 layer_tree_host_->BreakSwapPromises(cc::SwapPromise::COMMIT_FAILS);
47 private:
48 cc::LayerTreeHost* layer_tree_host_;
51 } // namespace
53 namespace cc {
55 struct ThreadProxy::CommitPendingRequest {
56 CompletionEvent completion;
57 bool commit_pending;
60 struct ThreadProxy::SchedulerStateRequest {
61 CompletionEvent completion;
62 scoped_ptr<base::Value> state;
65 scoped_ptr<Proxy> ThreadProxy::Create(
66 LayerTreeHost* layer_tree_host,
67 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
68 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) {
69 return make_scoped_ptr(new ThreadProxy(layer_tree_host,
70 main_task_runner,
71 impl_task_runner)).PassAs<Proxy>();
74 ThreadProxy::ThreadProxy(
75 LayerTreeHost* layer_tree_host,
76 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
77 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner)
78 : Proxy(main_task_runner, impl_task_runner),
79 main_thread_only_vars_unsafe_(this, layer_tree_host->id()),
80 main_thread_or_blocked_vars_unsafe_(layer_tree_host),
81 compositor_thread_vars_unsafe_(this, layer_tree_host->id()) {
82 TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy");
83 DCHECK(IsMainThread());
84 DCHECK(this->layer_tree_host());
87 ThreadProxy::MainThreadOnly::MainThreadOnly(ThreadProxy* proxy,
88 int layer_tree_host_id)
89 : layer_tree_host_id(layer_tree_host_id),
90 animate_requested(false),
91 commit_requested(false),
92 commit_request_sent_to_impl_thread(false),
93 started(false),
94 manage_tiles_pending(false),
95 can_cancel_commit(true),
96 defer_commits(false),
97 weak_factory(proxy) {}
99 ThreadProxy::MainThreadOnly::~MainThreadOnly() {}
101 ThreadProxy::MainThreadOrBlockedMainThread::MainThreadOrBlockedMainThread(
102 LayerTreeHost* host)
103 : layer_tree_host(host),
104 commit_waits_for_activation(false),
105 main_thread_inside_commit(false) {}
107 ThreadProxy::MainThreadOrBlockedMainThread::~MainThreadOrBlockedMainThread() {}
109 PrioritizedResourceManager*
110 ThreadProxy::MainThreadOrBlockedMainThread::contents_texture_manager() {
111 return layer_tree_host->contents_texture_manager();
114 ThreadProxy::CompositorThreadOnly::CompositorThreadOnly(ThreadProxy* proxy,
115 int layer_tree_host_id)
116 : layer_tree_host_id(layer_tree_host_id),
117 contents_texture_manager(NULL),
118 commit_completion_event(NULL),
119 completion_event_for_commit_held_on_tree_activation(NULL),
120 next_frame_is_newly_committed_frame(false),
121 inside_draw(false),
122 input_throttled_until_commit(false),
123 animations_frozen_until_next_draw(false),
124 did_commit_after_animating(false),
125 smoothness_priority_expiration_notifier(
126 proxy->ImplThreadTaskRunner(),
127 base::Bind(&ThreadProxy::RenewTreePriority, base::Unretained(proxy)),
128 base::TimeDelta::FromMilliseconds(
129 kSmoothnessTakesPriorityExpirationDelay * 1000)),
130 weak_factory(proxy) {
133 ThreadProxy::CompositorThreadOnly::~CompositorThreadOnly() {}
135 ThreadProxy::~ThreadProxy() {
136 TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy");
137 DCHECK(IsMainThread());
138 DCHECK(!main().started);
141 void ThreadProxy::FinishAllRendering() {
142 DCHECK(Proxy::IsMainThread());
143 DCHECK(!main().defer_commits);
145 // Make sure all GL drawing is finished on the impl thread.
146 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
147 CompletionEvent completion;
148 Proxy::ImplThreadTaskRunner()->PostTask(
149 FROM_HERE,
150 base::Bind(&ThreadProxy::FinishAllRenderingOnImplThread,
151 impl_thread_weak_ptr_,
152 &completion));
153 completion.Wait();
156 bool ThreadProxy::IsStarted() const {
157 DCHECK(Proxy::IsMainThread());
158 return main().started;
161 void ThreadProxy::SetLayerTreeHostClientReady() {
162 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReady");
163 Proxy::ImplThreadTaskRunner()->PostTask(
164 FROM_HERE,
165 base::Bind(&ThreadProxy::SetLayerTreeHostClientReadyOnImplThread,
166 impl_thread_weak_ptr_));
169 void ThreadProxy::SetLayerTreeHostClientReadyOnImplThread() {
170 TRACE_EVENT0("cc", "ThreadProxy::SetLayerTreeHostClientReadyOnImplThread");
171 impl().scheduler->SetCanStart();
174 void ThreadProxy::SetVisible(bool visible) {
175 TRACE_EVENT0("cc", "ThreadProxy::SetVisible");
176 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
178 CompletionEvent completion;
179 Proxy::ImplThreadTaskRunner()->PostTask(
180 FROM_HERE,
181 base::Bind(&ThreadProxy::SetVisibleOnImplThread,
182 impl_thread_weak_ptr_,
183 &completion,
184 visible));
185 completion.Wait();
188 void ThreadProxy::SetVisibleOnImplThread(CompletionEvent* completion,
189 bool visible) {
190 TRACE_EVENT0("cc", "ThreadProxy::SetVisibleOnImplThread");
191 impl().layer_tree_host_impl->SetVisible(visible);
192 impl().scheduler->SetVisible(visible);
193 UpdateBackgroundAnimateTicking();
194 completion->Signal();
197 void ThreadProxy::UpdateBackgroundAnimateTicking() {
198 bool should_background_tick =
199 !impl().scheduler->WillDrawIfNeeded() &&
200 impl().layer_tree_host_impl->active_tree()->root_layer();
201 impl().layer_tree_host_impl->UpdateBackgroundAnimateTicking(
202 should_background_tick);
203 if (should_background_tick)
204 impl().animations_frozen_until_next_draw = false;
207 void ThreadProxy::DidLoseOutputSurface() {
208 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurface");
209 DCHECK(IsMainThread());
210 layer_tree_host()->DidLoseOutputSurface();
213 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
215 // Return lost resources to their owners immediately.
216 BlockingTaskRunner::CapturePostTasks blocked;
218 CompletionEvent completion;
219 Proxy::ImplThreadTaskRunner()->PostTask(
220 FROM_HERE,
221 base::Bind(&ThreadProxy::DeleteContentsTexturesOnImplThread,
222 impl_thread_weak_ptr_,
223 &completion));
224 completion.Wait();
228 void ThreadProxy::CreateAndInitializeOutputSurface() {
229 TRACE_EVENT0("cc", "ThreadProxy::DoCreateAndInitializeOutputSurface");
230 DCHECK(IsMainThread());
232 scoped_ptr<OutputSurface> output_surface =
233 layer_tree_host()->CreateOutputSurface();
235 if (output_surface) {
236 Proxy::ImplThreadTaskRunner()->PostTask(
237 FROM_HERE,
238 base::Bind(&ThreadProxy::InitializeOutputSurfaceOnImplThread,
239 impl_thread_weak_ptr_,
240 base::Passed(&output_surface)));
241 return;
244 DidInitializeOutputSurface(false, RendererCapabilities());
247 void ThreadProxy::DidInitializeOutputSurface(
248 bool success,
249 const RendererCapabilities& capabilities) {
250 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeOutputSurface");
251 DCHECK(IsMainThread());
252 main().renderer_capabilities_main_thread_copy = capabilities;
253 layer_tree_host()->OnCreateAndInitializeOutputSurfaceAttempted(success);
255 if (!success) {
256 Proxy::MainThreadTaskRunner()->PostTask(
257 FROM_HERE,
258 base::Bind(&ThreadProxy::CreateAndInitializeOutputSurface,
259 main_thread_weak_ptr_));
263 void ThreadProxy::SetRendererCapabilitiesMainThreadCopy(
264 const RendererCapabilities& capabilities) {
265 main().renderer_capabilities_main_thread_copy = capabilities;
268 void ThreadProxy::SendCommitRequestToImplThreadIfNeeded() {
269 DCHECK(IsMainThread());
270 if (main().commit_request_sent_to_impl_thread)
271 return;
272 main().commit_request_sent_to_impl_thread = true;
273 Proxy::ImplThreadTaskRunner()->PostTask(
274 FROM_HERE,
275 base::Bind(&ThreadProxy::SetNeedsCommitOnImplThread,
276 impl_thread_weak_ptr_));
279 const RendererCapabilities& ThreadProxy::GetRendererCapabilities() const {
280 DCHECK(IsMainThread());
281 DCHECK(!layer_tree_host()->output_surface_lost());
282 return main().renderer_capabilities_main_thread_copy;
285 void ThreadProxy::SetNeedsAnimate() {
286 DCHECK(IsMainThread());
287 if (main().animate_requested)
288 return;
290 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimate");
291 main().animate_requested = true;
292 SendCommitRequestToImplThreadIfNeeded();
295 void ThreadProxy::SetNeedsUpdateLayers() {
296 DCHECK(IsMainThread());
298 if (main().commit_request_sent_to_impl_thread)
299 return;
300 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsUpdateLayers");
302 SendCommitRequestToImplThreadIfNeeded();
305 void ThreadProxy::SetNeedsCommit() {
306 DCHECK(IsMainThread());
307 // Unconditionally set here to handle SetNeedsCommit calls during a commit.
308 main().can_cancel_commit = false;
310 if (main().commit_requested)
311 return;
312 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommit");
313 main().commit_requested = true;
315 SendCommitRequestToImplThreadIfNeeded();
318 void ThreadProxy::UpdateRendererCapabilitiesOnImplThread() {
319 DCHECK(IsImplThread());
320 Proxy::MainThreadTaskRunner()->PostTask(
321 FROM_HERE,
322 base::Bind(&ThreadProxy::SetRendererCapabilitiesMainThreadCopy,
323 main_thread_weak_ptr_,
324 impl()
325 .layer_tree_host_impl->GetRendererCapabilities()
326 .MainThreadCapabilities()));
329 void ThreadProxy::DidLoseOutputSurfaceOnImplThread() {
330 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurfaceOnImplThread");
331 DCHECK(IsImplThread());
332 CheckOutputSurfaceStatusOnImplThread();
335 void ThreadProxy::CheckOutputSurfaceStatusOnImplThread() {
336 TRACE_EVENT0("cc", "ThreadProxy::CheckOutputSurfaceStatusOnImplThread");
337 DCHECK(IsImplThread());
338 if (!impl().layer_tree_host_impl->IsContextLost())
339 return;
340 Proxy::MainThreadTaskRunner()->PostTask(
341 FROM_HERE,
342 base::Bind(&ThreadProxy::DidLoseOutputSurface, main_thread_weak_ptr_));
343 impl().scheduler->DidLoseOutputSurface();
346 void ThreadProxy::CommitVSyncParameters(base::TimeTicks timebase,
347 base::TimeDelta interval) {
348 impl().scheduler->CommitVSyncParameters(timebase, interval);
351 void ThreadProxy::SetEstimatedParentDrawTime(base::TimeDelta draw_time) {
352 impl().scheduler->SetEstimatedParentDrawTime(draw_time);
355 void ThreadProxy::SetMaxSwapsPendingOnImplThread(int max) {
356 impl().scheduler->SetMaxSwapsPending(max);
359 void ThreadProxy::DidSwapBuffersOnImplThread() {
360 impl().scheduler->DidSwapBuffers();
363 void ThreadProxy::DidSwapBuffersCompleteOnImplThread() {
364 TRACE_EVENT0("cc", "ThreadProxy::DidSwapBuffersCompleteOnImplThread");
365 DCHECK(IsImplThread());
366 impl().scheduler->DidSwapBuffersComplete();
367 Proxy::MainThreadTaskRunner()->PostTask(
368 FROM_HERE,
369 base::Bind(&ThreadProxy::DidCompleteSwapBuffers, main_thread_weak_ptr_));
372 void ThreadProxy::SetNeedsBeginFrame(bool enable) {
373 TRACE_EVENT1("cc", "ThreadProxy::SetNeedsBeginFrame", "enable", enable);
374 impl().layer_tree_host_impl->SetNeedsBeginFrame(enable);
375 UpdateBackgroundAnimateTicking();
378 void ThreadProxy::BeginFrame(const BeginFrameArgs& args) {
379 impl().scheduler->BeginFrame(args);
382 void ThreadProxy::WillBeginImplFrame(const BeginFrameArgs& args) {
383 impl().layer_tree_host_impl->WillBeginImplFrame(args);
386 void ThreadProxy::OnCanDrawStateChanged(bool can_draw) {
387 TRACE_EVENT1(
388 "cc", "ThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw);
389 DCHECK(IsImplThread());
390 impl().scheduler->SetCanDraw(can_draw);
391 UpdateBackgroundAnimateTicking();
394 void ThreadProxy::NotifyReadyToActivate() {
395 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToActivate");
396 impl().scheduler->NotifyReadyToActivate();
399 void ThreadProxy::SetNeedsCommitOnImplThread() {
400 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommitOnImplThread");
401 DCHECK(IsImplThread());
402 impl().scheduler->SetNeedsCommit();
405 void ThreadProxy::PostAnimationEventsToMainThreadOnImplThread(
406 scoped_ptr<AnimationEventsVector> events) {
407 TRACE_EVENT0("cc",
408 "ThreadProxy::PostAnimationEventsToMainThreadOnImplThread");
409 DCHECK(IsImplThread());
410 Proxy::MainThreadTaskRunner()->PostTask(
411 FROM_HERE,
412 base::Bind(&ThreadProxy::SetAnimationEvents,
413 main_thread_weak_ptr_,
414 base::Passed(&events)));
417 bool ThreadProxy::ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
418 int priority_cutoff) {
419 DCHECK(IsImplThread());
421 if (!impl().contents_texture_manager)
422 return false;
423 if (!impl().layer_tree_host_impl->resource_provider())
424 return false;
426 bool reduce_result =
427 impl().contents_texture_manager->ReduceMemoryOnImplThread(
428 limit_bytes,
429 priority_cutoff,
430 impl().layer_tree_host_impl->resource_provider());
431 if (!reduce_result)
432 return false;
434 // The texture upload queue may reference textures that were just purged,
435 // clear them from the queue.
436 if (impl().current_resource_update_controller) {
437 impl()
438 .current_resource_update_controller->DiscardUploadsToEvictedResources();
440 return true;
443 bool ThreadProxy::IsInsideDraw() { return impl().inside_draw; }
445 void ThreadProxy::SetNeedsRedraw(const gfx::Rect& damage_rect) {
446 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedraw");
447 DCHECK(IsMainThread());
448 Proxy::ImplThreadTaskRunner()->PostTask(
449 FROM_HERE,
450 base::Bind(&ThreadProxy::SetNeedsRedrawRectOnImplThread,
451 impl_thread_weak_ptr_,
452 damage_rect));
455 void ThreadProxy::SetNextCommitWaitsForActivation() {
456 DCHECK(IsMainThread());
457 DCHECK(!blocked_main().main_thread_inside_commit);
458 blocked_main().commit_waits_for_activation = true;
461 void ThreadProxy::SetDeferCommits(bool defer_commits) {
462 DCHECK(IsMainThread());
463 DCHECK_NE(main().defer_commits, defer_commits);
464 main().defer_commits = defer_commits;
466 if (main().defer_commits)
467 TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::SetDeferCommits", this);
468 else
469 TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::SetDeferCommits", this);
471 if (!main().defer_commits && main().pending_deferred_commit) {
472 Proxy::MainThreadTaskRunner()->PostTask(
473 FROM_HERE,
474 base::Bind(&ThreadProxy::BeginMainFrame,
475 main_thread_weak_ptr_,
476 base::Passed(&main().pending_deferred_commit)));
480 bool ThreadProxy::CommitRequested() const {
481 DCHECK(IsMainThread());
482 return main().commit_requested;
485 bool ThreadProxy::BeginMainFrameRequested() const {
486 DCHECK(IsMainThread());
487 return main().commit_request_sent_to_impl_thread;
490 void ThreadProxy::SetNeedsRedrawOnImplThread() {
491 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedrawOnImplThread");
492 DCHECK(IsImplThread());
493 impl().scheduler->SetNeedsRedraw();
496 void ThreadProxy::SetNeedsAnimateOnImplThread() {
497 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimateOnImplThread");
498 DCHECK(IsImplThread());
499 impl().scheduler->SetNeedsAnimate();
502 void ThreadProxy::SetNeedsManageTilesOnImplThread() {
503 DCHECK(IsImplThread());
504 impl().scheduler->SetNeedsManageTiles();
507 void ThreadProxy::SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) {
508 DCHECK(IsImplThread());
509 impl().layer_tree_host_impl->SetViewportDamage(damage_rect);
510 SetNeedsRedrawOnImplThread();
513 void ThreadProxy::SetSwapUsedIncompleteTileOnImplThread(
514 bool used_incomplete_tile) {
515 DCHECK(IsImplThread());
516 if (used_incomplete_tile) {
517 TRACE_EVENT_INSTANT0("cc",
518 "ThreadProxy::SetSwapUsedIncompleteTileOnImplThread",
519 TRACE_EVENT_SCOPE_THREAD);
521 impl().scheduler->SetSwapUsedIncompleteTile(used_incomplete_tile);
524 void ThreadProxy::DidInitializeVisibleTileOnImplThread() {
525 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeVisibleTileOnImplThread");
526 DCHECK(IsImplThread());
527 impl().scheduler->SetNeedsRedraw();
530 void ThreadProxy::MainThreadHasStoppedFlinging() {
531 DCHECK(IsMainThread());
532 Proxy::ImplThreadTaskRunner()->PostTask(
533 FROM_HERE,
534 base::Bind(&ThreadProxy::MainThreadHasStoppedFlingingOnImplThread,
535 impl_thread_weak_ptr_));
538 void ThreadProxy::MainThreadHasStoppedFlingingOnImplThread() {
539 DCHECK(IsImplThread());
540 impl().layer_tree_host_impl->MainThreadHasStoppedFlinging();
543 void ThreadProxy::NotifyInputThrottledUntilCommit() {
544 DCHECK(IsMainThread());
545 Proxy::ImplThreadTaskRunner()->PostTask(
546 FROM_HERE,
547 base::Bind(&ThreadProxy::SetInputThrottledUntilCommitOnImplThread,
548 impl_thread_weak_ptr_,
549 true));
552 void ThreadProxy::SetInputThrottledUntilCommitOnImplThread(bool is_throttled) {
553 DCHECK(IsImplThread());
554 if (is_throttled == impl().input_throttled_until_commit)
555 return;
556 impl().input_throttled_until_commit = is_throttled;
557 RenewTreePriority();
560 LayerTreeHost* ThreadProxy::layer_tree_host() {
561 return blocked_main().layer_tree_host;
564 const LayerTreeHost* ThreadProxy::layer_tree_host() const {
565 return blocked_main().layer_tree_host;
568 ThreadProxy::MainThreadOnly& ThreadProxy::main() {
569 DCHECK(IsMainThread());
570 return main_thread_only_vars_unsafe_;
572 const ThreadProxy::MainThreadOnly& ThreadProxy::main() const {
573 DCHECK(IsMainThread());
574 return main_thread_only_vars_unsafe_;
577 ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main() {
578 DCHECK(IsMainThread() || IsMainThreadBlocked());
579 return main_thread_or_blocked_vars_unsafe_;
582 const ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main()
583 const {
584 DCHECK(IsMainThread() || IsMainThreadBlocked());
585 return main_thread_or_blocked_vars_unsafe_;
588 ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() {
589 DCHECK(IsImplThread());
590 return compositor_thread_vars_unsafe_;
593 const ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() const {
594 DCHECK(IsImplThread());
595 return compositor_thread_vars_unsafe_;
598 void ThreadProxy::Start() {
599 DCHECK(IsMainThread());
600 DCHECK(Proxy::HasImplThread());
602 // Create LayerTreeHostImpl.
603 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
604 CompletionEvent completion;
605 Proxy::ImplThreadTaskRunner()->PostTask(
606 FROM_HERE,
607 base::Bind(&ThreadProxy::InitializeImplOnImplThread,
608 base::Unretained(this),
609 &completion));
610 completion.Wait();
612 main_thread_weak_ptr_ = main().weak_factory.GetWeakPtr();
614 main().started = true;
617 void ThreadProxy::Stop() {
618 TRACE_EVENT0("cc", "ThreadProxy::Stop");
619 DCHECK(IsMainThread());
620 DCHECK(main().started);
622 // Synchronously finishes pending GL operations and deletes the impl.
623 // The two steps are done as separate post tasks, so that tasks posted
624 // by the GL implementation due to the Finish can be executed by the
625 // renderer before shutting it down.
627 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
629 CompletionEvent completion;
630 Proxy::ImplThreadTaskRunner()->PostTask(
631 FROM_HERE,
632 base::Bind(&ThreadProxy::FinishGLOnImplThread,
633 impl_thread_weak_ptr_,
634 &completion));
635 completion.Wait();
638 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
640 CompletionEvent completion;
641 Proxy::ImplThreadTaskRunner()->PostTask(
642 FROM_HERE,
643 base::Bind(&ThreadProxy::LayerTreeHostClosedOnImplThread,
644 impl_thread_weak_ptr_,
645 &completion));
646 completion.Wait();
649 main().weak_factory.InvalidateWeakPtrs();
650 blocked_main().layer_tree_host = NULL;
651 main().started = false;
654 void ThreadProxy::ForceSerializeOnSwapBuffers() {
655 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
656 CompletionEvent completion;
657 Proxy::ImplThreadTaskRunner()->PostTask(
658 FROM_HERE,
659 base::Bind(&ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread,
660 impl_thread_weak_ptr_,
661 &completion));
662 completion.Wait();
665 void ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread(
666 CompletionEvent* completion) {
667 if (impl().layer_tree_host_impl->renderer())
668 impl().layer_tree_host_impl->renderer()->DoNoOp();
669 completion->Signal();
672 bool ThreadProxy::SupportsImplScrolling() const {
673 return true;
676 void ThreadProxy::SetDebugState(const LayerTreeDebugState& debug_state) {
677 Proxy::ImplThreadTaskRunner()->PostTask(
678 FROM_HERE,
679 base::Bind(&ThreadProxy::SetDebugStateOnImplThread,
680 impl_thread_weak_ptr_,
681 debug_state));
684 void ThreadProxy::SetDebugStateOnImplThread(
685 const LayerTreeDebugState& debug_state) {
686 DCHECK(IsImplThread());
687 impl().scheduler->SetContinuousPainting(debug_state.continuous_painting);
690 void ThreadProxy::FinishAllRenderingOnImplThread(CompletionEvent* completion) {
691 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread");
692 DCHECK(IsImplThread());
693 impl().layer_tree_host_impl->FinishAllRendering();
694 completion->Signal();
697 void ThreadProxy::ScheduledActionSendBeginMainFrame() {
698 unsigned int begin_frame_id = nextBeginFrameId++;
699 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
700 benchmark_instrumentation::kSendBeginFrame, begin_frame_id);
701 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state(
702 new BeginMainFrameAndCommitState);
703 begin_main_frame_state->begin_frame_id = begin_frame_id;
704 begin_main_frame_state->monotonic_frame_begin_time =
705 impl().layer_tree_host_impl->CurrentFrameTimeTicks();
706 begin_main_frame_state->scroll_info =
707 impl().layer_tree_host_impl->ProcessScrollDeltas();
709 if (!impl().layer_tree_host_impl->settings().impl_side_painting) {
710 DCHECK_GT(impl().layer_tree_host_impl->memory_allocation_limit_bytes(), 0u);
712 begin_main_frame_state->memory_allocation_limit_bytes =
713 impl().layer_tree_host_impl->memory_allocation_limit_bytes();
714 begin_main_frame_state->memory_allocation_priority_cutoff =
715 impl().layer_tree_host_impl->memory_allocation_priority_cutoff();
716 begin_main_frame_state->evicted_ui_resources =
717 impl().layer_tree_host_impl->EvictedUIResourcesExist();
718 Proxy::MainThreadTaskRunner()->PostTask(
719 FROM_HERE,
720 base::Bind(&ThreadProxy::BeginMainFrame,
721 main_thread_weak_ptr_,
722 base::Passed(&begin_main_frame_state)));
723 devtools_instrumentation::DidRequestMainThreadFrame(
724 impl().layer_tree_host_id);
725 impl().timing_history.DidBeginMainFrame();
728 void ThreadProxy::BeginMainFrame(
729 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
730 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
731 benchmark_instrumentation::kDoBeginFrame,
732 begin_main_frame_state->begin_frame_id);
733 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
734 DCHECK(IsMainThread());
736 if (main().defer_commits) {
737 main().pending_deferred_commit = begin_main_frame_state.Pass();
738 layer_tree_host()->DidDeferCommit();
739 TRACE_EVENT_INSTANT0(
740 "cc", "EarlyOut_DeferCommits", TRACE_EVENT_SCOPE_THREAD);
741 return;
744 // If the commit finishes, LayerTreeHost will transfer its swap promises to
745 // LayerTreeImpl. The destructor of SwapPromiseChecker checks LayerTressHost's
746 // swap promises.
747 SwapPromiseChecker swap_promise_checker(layer_tree_host());
749 main().commit_requested = false;
750 main().commit_request_sent_to_impl_thread = false;
751 main().animate_requested = false;
753 if (!layer_tree_host()->visible()) {
754 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
755 bool did_handle = false;
756 Proxy::ImplThreadTaskRunner()->PostTask(
757 FROM_HERE,
758 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
759 impl_thread_weak_ptr_,
760 did_handle));
761 return;
764 if (layer_tree_host()->output_surface_lost()) {
765 TRACE_EVENT_INSTANT0(
766 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD);
767 bool did_handle = false;
768 Proxy::ImplThreadTaskRunner()->PostTask(
769 FROM_HERE,
770 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
771 impl_thread_weak_ptr_,
772 did_handle));
773 return;
776 // Do not notify the impl thread of commit requests that occur during
777 // the apply/animate/layout part of the BeginMainFrameAndCommit process since
778 // those commit requests will get painted immediately. Once we have done
779 // the paint, main().commit_requested will be set to false to allow new commit
780 // requests to be scheduled.
781 // On the other hand, the animate_requested flag should remain cleared
782 // here so that any animation requests generated by the apply or animate
783 // callbacks will trigger another frame.
784 main().commit_requested = true;
785 main().commit_request_sent_to_impl_thread = true;
787 layer_tree_host()->ApplyScrollAndScale(*begin_main_frame_state->scroll_info);
789 layer_tree_host()->WillBeginMainFrame();
791 layer_tree_host()->UpdateClientAnimations(
792 begin_main_frame_state->monotonic_frame_begin_time);
793 layer_tree_host()->AnimateLayers(
794 begin_main_frame_state->monotonic_frame_begin_time);
795 blocked_main().last_monotonic_frame_begin_time =
796 begin_main_frame_state->monotonic_frame_begin_time;
798 // Unlink any backings that the impl thread has evicted, so that we know to
799 // re-paint them in UpdateLayers.
800 if (blocked_main().contents_texture_manager()) {
801 blocked_main().contents_texture_manager()->UnlinkAndClearEvictedBackings();
803 blocked_main().contents_texture_manager()->SetMaxMemoryLimitBytes(
804 begin_main_frame_state->memory_allocation_limit_bytes);
805 blocked_main().contents_texture_manager()->SetExternalPriorityCutoff(
806 begin_main_frame_state->memory_allocation_priority_cutoff);
809 // Recreate all UI resources if there were evicted UI resources when the impl
810 // thread initiated the commit.
811 if (begin_main_frame_state->evicted_ui_resources)
812 layer_tree_host()->RecreateUIResources();
814 layer_tree_host()->Layout();
815 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
817 // Clear the commit flag after updating animations and layout here --- objects
818 // that only layout when painted will trigger another SetNeedsCommit inside
819 // UpdateLayers.
820 main().commit_requested = false;
821 main().commit_request_sent_to_impl_thread = false;
822 bool can_cancel_this_commit =
823 main().can_cancel_commit && !begin_main_frame_state->evicted_ui_resources;
824 main().can_cancel_commit = true;
826 scoped_ptr<ResourceUpdateQueue> queue =
827 make_scoped_ptr(new ResourceUpdateQueue);
829 bool updated = layer_tree_host()->UpdateLayers(queue.get());
831 layer_tree_host()->WillCommit();
833 // Before calling animate, we set main().animate_requested to false. If it is
834 // true now, it means SetNeedAnimate was called again, but during a state when
835 // main().commit_request_sent_to_impl_thread = true. We need to force that
836 // call to happen again now so that the commit request is sent to the impl
837 // thread.
838 if (main().animate_requested) {
839 // Forces SetNeedsAnimate to consider posting a commit task.
840 main().animate_requested = false;
841 SetNeedsAnimate();
844 if (!updated && can_cancel_this_commit) {
845 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
846 bool did_handle = true;
847 Proxy::ImplThreadTaskRunner()->PostTask(
848 FROM_HERE,
849 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
850 impl_thread_weak_ptr_,
851 did_handle));
853 // Although the commit is internally aborted, this is because it has been
854 // detected to be a no-op. From the perspective of an embedder, this commit
855 // went through, and input should no longer be throttled, etc.
856 layer_tree_host()->CommitComplete();
857 layer_tree_host()->DidBeginMainFrame();
858 return;
861 // Notify the impl thread that the main thread is ready to commit. This will
862 // begin the commit process, which is blocking from the main thread's
863 // point of view, but asynchronously performed on the impl thread,
864 // coordinated by the Scheduler.
866 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit");
868 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
870 // This CapturePostTasks should be destroyed before CommitComplete() is
871 // called since that goes out to the embedder, and we want the embedder
872 // to receive its callbacks before that.
873 BlockingTaskRunner::CapturePostTasks blocked;
875 CompletionEvent completion;
876 Proxy::ImplThreadTaskRunner()->PostTask(
877 FROM_HERE,
878 base::Bind(&ThreadProxy::StartCommitOnImplThread,
879 impl_thread_weak_ptr_,
880 &completion,
881 queue.release()));
882 completion.Wait();
884 RenderingStatsInstrumentation* stats_instrumentation =
885 layer_tree_host()->rendering_stats_instrumentation();
886 benchmark_instrumentation::IssueMainThreadRenderingStatsEvent(
887 stats_instrumentation->main_thread_rendering_stats());
888 stats_instrumentation->AccumulateAndClearMainThreadStats();
891 layer_tree_host()->CommitComplete();
892 layer_tree_host()->DidBeginMainFrame();
895 void ThreadProxy::StartCommitOnImplThread(CompletionEvent* completion,
896 ResourceUpdateQueue* raw_queue) {
897 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread");
898 DCHECK(!impl().commit_completion_event);
899 DCHECK(IsImplThread() && IsMainThreadBlocked());
900 DCHECK(impl().scheduler);
901 DCHECK(impl().scheduler->CommitPending());
903 if (!impl().layer_tree_host_impl) {
904 TRACE_EVENT_INSTANT0(
905 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD);
906 completion->Signal();
907 return;
910 // Ideally, we should inform to impl thread when BeginMainFrame is started.
911 // But, we can avoid a PostTask in here.
912 impl().scheduler->NotifyBeginMainFrameStarted();
914 scoped_ptr<ResourceUpdateQueue> queue(raw_queue);
916 if (impl().contents_texture_manager) {
917 DCHECK_EQ(impl().contents_texture_manager,
918 blocked_main().contents_texture_manager());
919 } else {
920 // Cache this pointer that was created on the main thread side to avoid a
921 // data race between creating it and using it on the compositor thread.
922 impl().contents_texture_manager = blocked_main().contents_texture_manager();
925 if (impl().contents_texture_manager) {
926 if (impl().contents_texture_manager->LinkedEvictedBackingsExist()) {
927 // Clear any uploads we were making to textures linked to evicted
928 // resources
929 queue->ClearUploadsToEvictedResources();
930 // Some textures in the layer tree are invalid. Kick off another commit
931 // to fill them again.
932 SetNeedsCommitOnImplThread();
935 impl().contents_texture_manager->PushTexturePrioritiesToBackings();
938 impl().commit_completion_event = completion;
939 impl().current_resource_update_controller = ResourceUpdateController::Create(
940 this,
941 Proxy::ImplThreadTaskRunner(),
942 queue.Pass(),
943 impl().layer_tree_host_impl->resource_provider());
944 impl().current_resource_update_controller->PerformMoreUpdates(
945 impl().scheduler->AnticipatedDrawTime());
948 void ThreadProxy::BeginMainFrameAbortedOnImplThread(bool did_handle) {
949 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread");
950 DCHECK(IsImplThread());
951 DCHECK(impl().scheduler);
952 DCHECK(impl().scheduler->CommitPending());
953 DCHECK(!impl().layer_tree_host_impl->pending_tree());
955 if (did_handle)
956 SetInputThrottledUntilCommitOnImplThread(false);
957 impl().layer_tree_host_impl->BeginMainFrameAborted(did_handle);
958 impl().scheduler->BeginMainFrameAborted(did_handle);
961 void ThreadProxy::ScheduledActionAnimate() {
962 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate");
963 DCHECK(IsImplThread());
965 if (!impl().animations_frozen_until_next_draw) {
966 impl().animation_time =
967 impl().layer_tree_host_impl->CurrentFrameTimeTicks();
969 impl().layer_tree_host_impl->Animate(impl().animation_time);
970 impl().did_commit_after_animating = false;
973 void ThreadProxy::ScheduledActionCommit() {
974 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit");
975 DCHECK(IsImplThread());
976 DCHECK(IsMainThreadBlocked());
977 DCHECK(impl().commit_completion_event);
978 DCHECK(impl().current_resource_update_controller);
980 // Complete all remaining texture updates.
981 impl().current_resource_update_controller->Finalize();
982 impl().current_resource_update_controller.reset();
984 if (impl().animations_frozen_until_next_draw) {
985 impl().animation_time = std::max(
986 impl().animation_time, blocked_main().last_monotonic_frame_begin_time);
988 impl().did_commit_after_animating = true;
990 blocked_main().main_thread_inside_commit = true;
991 impl().layer_tree_host_impl->BeginCommit();
992 layer_tree_host()->BeginCommitOnImplThread(impl().layer_tree_host_impl.get());
993 layer_tree_host()->FinishCommitOnImplThread(
994 impl().layer_tree_host_impl.get());
995 blocked_main().main_thread_inside_commit = false;
997 bool hold_commit = layer_tree_host()->settings().impl_side_painting &&
998 blocked_main().commit_waits_for_activation;
999 blocked_main().commit_waits_for_activation = false;
1001 if (hold_commit) {
1002 // For some layer types in impl-side painting, the commit is held until
1003 // the sync tree is activated. It's also possible that the
1004 // sync tree has already activated if there was no work to be done.
1005 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD);
1006 impl().completion_event_for_commit_held_on_tree_activation =
1007 impl().commit_completion_event;
1008 impl().commit_completion_event = NULL;
1009 } else {
1010 impl().commit_completion_event->Signal();
1011 impl().commit_completion_event = NULL;
1014 // Delay this step until afer the main thread has been released as it's
1015 // often a good bit of work to update the tree and prepare the new frame.
1016 impl().layer_tree_host_impl->CommitComplete();
1018 SetInputThrottledUntilCommitOnImplThread(false);
1020 UpdateBackgroundAnimateTicking();
1022 impl().next_frame_is_newly_committed_frame = true;
1024 impl().timing_history.DidCommit();
1027 void ThreadProxy::ScheduledActionUpdateVisibleTiles() {
1028 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionUpdateVisibleTiles");
1029 DCHECK(IsImplThread());
1030 impl().layer_tree_host_impl->UpdateVisibleTiles();
1033 void ThreadProxy::ScheduledActionActivateSyncTree() {
1034 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree");
1035 DCHECK(IsImplThread());
1036 impl().layer_tree_host_impl->ActivateSyncTree();
1039 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() {
1040 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation");
1041 DCHECK(IsImplThread());
1042 Proxy::MainThreadTaskRunner()->PostTask(
1043 FROM_HERE,
1044 base::Bind(&ThreadProxy::CreateAndInitializeOutputSurface,
1045 main_thread_weak_ptr_));
1048 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) {
1049 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap");
1050 DrawResult result;
1052 DCHECK(IsImplThread());
1053 DCHECK(impl().layer_tree_host_impl.get());
1055 impl().timing_history.DidStartDrawing();
1056 base::TimeDelta draw_duration_estimate = DrawDurationEstimate();
1057 base::AutoReset<bool> mark_inside(&impl().inside_draw, true);
1059 if (impl().did_commit_after_animating) {
1060 impl().layer_tree_host_impl->Animate(impl().animation_time);
1061 impl().did_commit_after_animating = false;
1064 if (impl().layer_tree_host_impl->pending_tree())
1065 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties();
1067 // This method is called on a forced draw, regardless of whether we are able
1068 // to produce a frame, as the calling site on main thread is blocked until its
1069 // request completes, and we signal completion here. If CanDraw() is false, we
1070 // will indicate success=false to the caller, but we must still signal
1071 // completion to avoid deadlock.
1073 // We guard PrepareToDraw() with CanDraw() because it always returns a valid
1074 // frame, so can only be used when such a frame is possible. Since
1075 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
1076 // CanDraw() as well.
1078 LayerTreeHostImpl::FrameData frame;
1079 bool draw_frame = false;
1081 if (impl().layer_tree_host_impl->CanDraw()) {
1082 result = impl().layer_tree_host_impl->PrepareToDraw(&frame);
1083 draw_frame = forced_draw || result == DRAW_SUCCESS;
1084 } else {
1085 result = DRAW_ABORTED_CANT_DRAW;
1088 if (draw_frame) {
1089 impl().layer_tree_host_impl->DrawLayers(&frame);
1090 result = DRAW_SUCCESS;
1091 impl().animations_frozen_until_next_draw = false;
1092 } else if (result == DRAW_ABORTED_CHECKERBOARD_ANIMATIONS &&
1093 !impl().layer_tree_host_impl->settings().impl_side_painting) {
1094 // Without impl-side painting, the animated layer that is checkerboarding
1095 // will continue to checkerboard until the next commit. If this layer
1096 // continues to move during the commit, it may continue to checkerboard
1097 // after the commit since the region rasterized during the commit will not
1098 // match the region that is currently visible; eventually this
1099 // checkerboarding will be displayed when we force a draw. To avoid this,
1100 // we freeze animations until we successfully draw.
1101 impl().animations_frozen_until_next_draw = true;
1102 } else {
1103 DCHECK_NE(DRAW_SUCCESS, result);
1105 impl().layer_tree_host_impl->DidDrawAllLayers(frame);
1107 bool start_ready_animations = draw_frame;
1108 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations);
1110 if (draw_frame) {
1111 bool did_request_swap = impl().layer_tree_host_impl->SwapBuffers(frame);
1113 // We don't know if we have incomplete tiles if we didn't actually swap.
1114 if (did_request_swap) {
1115 DCHECK(!frame.has_no_damage);
1116 SetSwapUsedIncompleteTileOnImplThread(frame.contains_incomplete_tile);
1120 // Tell the main thread that the the newly-commited frame was drawn.
1121 if (impl().next_frame_is_newly_committed_frame) {
1122 impl().next_frame_is_newly_committed_frame = false;
1123 Proxy::MainThreadTaskRunner()->PostTask(
1124 FROM_HERE,
1125 base::Bind(&ThreadProxy::DidCommitAndDrawFrame, main_thread_weak_ptr_));
1128 if (draw_frame)
1129 CheckOutputSurfaceStatusOnImplThread();
1131 if (result == DRAW_SUCCESS) {
1132 base::TimeDelta draw_duration = impl().timing_history.DidFinishDrawing();
1134 base::TimeDelta draw_duration_overestimate;
1135 base::TimeDelta draw_duration_underestimate;
1136 if (draw_duration > draw_duration_estimate)
1137 draw_duration_underestimate = draw_duration - draw_duration_estimate;
1138 else
1139 draw_duration_overestimate = draw_duration_estimate - draw_duration;
1140 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer.DrawDuration",
1141 draw_duration,
1142 base::TimeDelta::FromMilliseconds(1),
1143 base::TimeDelta::FromMilliseconds(100),
1144 50);
1145 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer.DrawDurationUnderestimate",
1146 draw_duration_underestimate,
1147 base::TimeDelta::FromMilliseconds(1),
1148 base::TimeDelta::FromMilliseconds(100),
1149 50);
1150 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer.DrawDurationOverestimate",
1151 draw_duration_overestimate,
1152 base::TimeDelta::FromMilliseconds(1),
1153 base::TimeDelta::FromMilliseconds(100),
1154 50);
1157 DCHECK_NE(INVALID_RESULT, result);
1158 return result;
1161 void ThreadProxy::ScheduledActionManageTiles() {
1162 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionManageTiles");
1163 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1164 impl().layer_tree_host_impl->ManageTiles();
1167 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() {
1168 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap");
1170 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to
1171 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should
1172 // never generate this call when it can't draw.
1173 DCHECK(impl().layer_tree_host_impl->CanDraw());
1175 bool forced_draw = false;
1176 return DrawSwapInternal(forced_draw);
1179 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() {
1180 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced");
1181 bool forced_draw = true;
1182 return DrawSwapInternal(forced_draw);
1185 void ThreadProxy::DidAnticipatedDrawTimeChange(base::TimeTicks time) {
1186 if (impl().current_resource_update_controller)
1187 impl().current_resource_update_controller->PerformMoreUpdates(time);
1190 base::TimeDelta ThreadProxy::DrawDurationEstimate() {
1191 return impl().timing_history.DrawDurationEstimate();
1194 base::TimeDelta ThreadProxy::BeginMainFrameToCommitDurationEstimate() {
1195 return impl().timing_history.BeginMainFrameToCommitDurationEstimate();
1198 base::TimeDelta ThreadProxy::CommitToActivateDurationEstimate() {
1199 return impl().timing_history.CommitToActivateDurationEstimate();
1202 void ThreadProxy::DidBeginImplFrameDeadline() {
1203 impl().layer_tree_host_impl->ResetCurrentFrameTimeForNextFrame();
1206 void ThreadProxy::ReadyToFinalizeTextureUpdates() {
1207 DCHECK(IsImplThread());
1208 impl().scheduler->NotifyReadyToCommit();
1211 void ThreadProxy::DidCommitAndDrawFrame() {
1212 DCHECK(IsMainThread());
1213 layer_tree_host()->DidCommitAndDrawFrame();
1216 void ThreadProxy::DidCompleteSwapBuffers() {
1217 DCHECK(IsMainThread());
1218 layer_tree_host()->DidCompleteSwapBuffers();
1221 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
1222 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents");
1223 DCHECK(IsMainThread());
1224 layer_tree_host()->SetAnimationEvents(events.Pass());
1227 void ThreadProxy::InitializeImplOnImplThread(CompletionEvent* completion) {
1228 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread");
1229 DCHECK(IsImplThread());
1230 impl().layer_tree_host_impl =
1231 layer_tree_host()->CreateLayerTreeHostImpl(this);
1232 SchedulerSettings scheduler_settings(layer_tree_host()->settings());
1233 impl().scheduler = Scheduler::Create(this,
1234 scheduler_settings,
1235 impl().layer_tree_host_id,
1236 ImplThreadTaskRunner());
1237 impl().scheduler->SetVisible(impl().layer_tree_host_impl->visible());
1239 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr();
1240 completion->Signal();
1243 void ThreadProxy::DeleteContentsTexturesOnImplThread(
1244 CompletionEvent* completion) {
1245 TRACE_EVENT0("cc", "ThreadProxy::DeleteContentsTexturesOnImplThread");
1246 DCHECK(IsImplThread());
1247 DCHECK(IsMainThreadBlocked());
1248 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1249 impl().layer_tree_host_impl->resource_provider());
1250 completion->Signal();
1253 void ThreadProxy::InitializeOutputSurfaceOnImplThread(
1254 scoped_ptr<OutputSurface> output_surface) {
1255 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread");
1256 DCHECK(IsImplThread());
1258 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get();
1259 bool success = host_impl->InitializeRenderer(output_surface.Pass());
1260 RendererCapabilities capabilities;
1261 if (success) {
1262 capabilities =
1263 host_impl->GetRendererCapabilities().MainThreadCapabilities();
1266 Proxy::MainThreadTaskRunner()->PostTask(
1267 FROM_HERE,
1268 base::Bind(&ThreadProxy::DidInitializeOutputSurface,
1269 main_thread_weak_ptr_,
1270 success,
1271 capabilities));
1273 if (success)
1274 impl().scheduler->DidCreateAndInitializeOutputSurface();
1277 void ThreadProxy::FinishGLOnImplThread(CompletionEvent* completion) {
1278 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread");
1279 DCHECK(IsImplThread());
1280 if (impl().layer_tree_host_impl->output_surface()) {
1281 ContextProvider* context_provider =
1282 impl().layer_tree_host_impl->output_surface()->context_provider();
1283 if (context_provider)
1284 context_provider->ContextGL()->Finish();
1286 completion->Signal();
1289 void ThreadProxy::LayerTreeHostClosedOnImplThread(CompletionEvent* completion) {
1290 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread");
1291 DCHECK(IsImplThread());
1292 DCHECK(IsMainThreadBlocked());
1293 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1294 impl().layer_tree_host_impl->resource_provider());
1295 impl().current_resource_update_controller.reset();
1296 impl().layer_tree_host_impl->SetNeedsBeginFrame(false);
1297 impl().scheduler.reset();
1298 impl().layer_tree_host_impl.reset();
1299 impl().weak_factory.InvalidateWeakPtrs();
1300 impl().contents_texture_manager = NULL;
1301 completion->Signal();
1304 size_t ThreadProxy::MaxPartialTextureUpdates() const {
1305 return ResourceUpdateController::MaxPartialTextureUpdates();
1308 ThreadProxy::BeginMainFrameAndCommitState::BeginMainFrameAndCommitState()
1309 : memory_allocation_limit_bytes(0),
1310 memory_allocation_priority_cutoff(0),
1311 evicted_ui_resources(false) {}
1313 ThreadProxy::BeginMainFrameAndCommitState::~BeginMainFrameAndCommitState() {}
1315 scoped_ptr<base::Value> ThreadProxy::AsValue() const {
1316 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
1318 CompletionEvent completion;
1320 DebugScopedSetMainThreadBlocked main_thread_blocked(
1321 const_cast<ThreadProxy*>(this));
1322 Proxy::ImplThreadTaskRunner()->PostTask(
1323 FROM_HERE,
1324 base::Bind(&ThreadProxy::AsValueOnImplThread,
1325 impl_thread_weak_ptr_,
1326 &completion,
1327 state.get()));
1328 completion.Wait();
1330 return state.PassAs<base::Value>();
1333 void ThreadProxy::AsValueOnImplThread(CompletionEvent* completion,
1334 base::DictionaryValue* state) const {
1335 state->Set("layer_tree_host_impl",
1336 impl().layer_tree_host_impl->AsValue().release());
1337 completion->Signal();
1340 bool ThreadProxy::CommitPendingForTesting() {
1341 DCHECK(IsMainThread());
1342 CommitPendingRequest commit_pending_request;
1344 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1345 Proxy::ImplThreadTaskRunner()->PostTask(
1346 FROM_HERE,
1347 base::Bind(&ThreadProxy::CommitPendingOnImplThreadForTesting,
1348 impl_thread_weak_ptr_,
1349 &commit_pending_request));
1350 commit_pending_request.completion.Wait();
1352 return commit_pending_request.commit_pending;
1355 void ThreadProxy::CommitPendingOnImplThreadForTesting(
1356 CommitPendingRequest* request) {
1357 DCHECK(IsImplThread());
1358 if (impl().layer_tree_host_impl->output_surface())
1359 request->commit_pending = impl().scheduler->CommitPending();
1360 else
1361 request->commit_pending = false;
1362 request->completion.Signal();
1365 scoped_ptr<base::Value> ThreadProxy::SchedulerAsValueForTesting() {
1366 if (IsImplThread())
1367 return impl().scheduler->AsValue().Pass();
1369 SchedulerStateRequest scheduler_state_request;
1371 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1372 Proxy::ImplThreadTaskRunner()->PostTask(
1373 FROM_HERE,
1374 base::Bind(&ThreadProxy::SchedulerAsValueOnImplThreadForTesting,
1375 impl_thread_weak_ptr_,
1376 &scheduler_state_request));
1377 scheduler_state_request.completion.Wait();
1379 return scheduler_state_request.state.Pass();
1382 void ThreadProxy::SchedulerAsValueOnImplThreadForTesting(
1383 SchedulerStateRequest* request) {
1384 DCHECK(IsImplThread());
1385 request->state = impl().scheduler->AsValue();
1386 request->completion.Signal();
1389 void ThreadProxy::RenewTreePriority() {
1390 DCHECK(IsImplThread());
1391 bool smoothness_takes_priority =
1392 impl().layer_tree_host_impl->pinch_gesture_active() ||
1393 impl().layer_tree_host_impl->page_scale_animation_active() ||
1394 (impl().layer_tree_host_impl->IsCurrentlyScrolling() &&
1395 !impl().layer_tree_host_impl->scroll_affects_scroll_handler());
1397 // Schedule expiration if smoothness currently takes priority.
1398 if (smoothness_takes_priority)
1399 impl().smoothness_priority_expiration_notifier.Schedule();
1401 // We use the same priority for both trees by default.
1402 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES;
1404 // Smoothness takes priority if we have an expiration for it scheduled.
1405 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification())
1406 priority = SMOOTHNESS_TAKES_PRIORITY;
1408 // New content always takes priority when the active tree has
1409 // evicted resources or there is an invalid viewport size.
1410 if (impl().layer_tree_host_impl->active_tree()->ContentsTexturesPurged() ||
1411 impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() ||
1412 impl().layer_tree_host_impl->EvictedUIResourcesExist() ||
1413 impl().input_throttled_until_commit) {
1414 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active
1415 // tree might be freed. We need to set RequiresHighResToDraw to ensure that
1416 // high res tiles will be required to activate pending tree.
1417 impl().layer_tree_host_impl->active_tree()->SetRequiresHighResToDraw();
1418 priority = NEW_CONTENT_TAKES_PRIORITY;
1421 impl().layer_tree_host_impl->SetTreePriority(priority);
1422 impl().scheduler->SetSmoothnessTakesPriority(priority ==
1423 SMOOTHNESS_TAKES_PRIORITY);
1425 // Notify the the client of this compositor via the output surface.
1426 // TODO(epenner): Route this to compositor-thread instead of output-surface
1427 // after GTFO refactor of compositor-thread (http://crbug/170828).
1428 if (impl().layer_tree_host_impl->output_surface()) {
1429 impl()
1430 .layer_tree_host_impl->output_surface()
1431 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY);
1435 void ThreadProxy::PostDelayedScrollbarFadeOnImplThread(
1436 const base::Closure& start_fade,
1437 base::TimeDelta delay) {
1438 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, start_fade, delay);
1441 void ThreadProxy::DidActivateSyncTree() {
1442 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread");
1443 DCHECK(IsImplThread());
1445 if (impl().completion_event_for_commit_held_on_tree_activation) {
1446 TRACE_EVENT_INSTANT0(
1447 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD);
1448 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1449 impl().completion_event_for_commit_held_on_tree_activation->Signal();
1450 impl().completion_event_for_commit_held_on_tree_activation = NULL;
1453 UpdateBackgroundAnimateTicking();
1455 impl().timing_history.DidActivateSyncTree();
1458 void ThreadProxy::DidManageTiles() {
1459 DCHECK(IsImplThread());
1460 impl().scheduler->DidManageTiles();
1463 } // namespace cc