Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / trees / thread_proxy.cc
blob59ce0a7869a3c988802e2bf6a1f646dc150b2696
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/resources/prioritized_resource_manager.h"
23 #include "cc/scheduler/commit_earlyout_reason.h"
24 #include "cc/scheduler/delay_based_time_source.h"
25 #include "cc/scheduler/scheduler.h"
26 #include "cc/trees/blocking_task_runner.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "cc/trees/layer_tree_impl.h"
29 #include "cc/trees/scoped_abort_remaining_swap_promises.h"
30 #include "gpu/command_buffer/client/gles2_interface.h"
32 namespace cc {
34 namespace {
36 // Measured in seconds.
37 const double kSmoothnessTakesPriorityExpirationDelay = 0.25;
39 unsigned int nextBeginFrameId = 0;
41 } // namespace
43 struct ThreadProxy::SchedulerStateRequest {
44 CompletionEvent completion;
45 scoped_ptr<base::Value> state;
48 scoped_ptr<Proxy> ThreadProxy::Create(
49 LayerTreeHost* layer_tree_host,
50 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
51 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
52 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
53 return make_scoped_ptr(new ThreadProxy(layer_tree_host,
54 main_task_runner,
55 impl_task_runner,
56 external_begin_frame_source.Pass()));
59 ThreadProxy::ThreadProxy(
60 LayerTreeHost* layer_tree_host,
61 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
62 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
63 scoped_ptr<BeginFrameSource> external_begin_frame_source)
64 : Proxy(main_task_runner, impl_task_runner),
65 main_thread_only_vars_unsafe_(this, layer_tree_host->id()),
66 main_thread_or_blocked_vars_unsafe_(layer_tree_host),
67 compositor_thread_vars_unsafe_(
68 this,
69 layer_tree_host->id(),
70 layer_tree_host->rendering_stats_instrumentation(),
71 external_begin_frame_source.Pass()) {
72 TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy");
73 DCHECK(IsMainThread());
74 DCHECK(this->layer_tree_host());
77 ThreadProxy::MainThreadOnly::MainThreadOnly(ThreadProxy* proxy,
78 int layer_tree_host_id)
79 : layer_tree_host_id(layer_tree_host_id),
80 animate_requested(false),
81 commit_requested(false),
82 commit_request_sent_to_impl_thread(false),
83 started(false),
84 prepare_tiles_pending(false),
85 can_cancel_commit(true),
86 defer_commits(false),
87 weak_factory(proxy) {
90 ThreadProxy::MainThreadOnly::~MainThreadOnly() {}
92 ThreadProxy::MainThreadOrBlockedMainThread::MainThreadOrBlockedMainThread(
93 LayerTreeHost* host)
94 : layer_tree_host(host),
95 commit_waits_for_activation(false),
96 main_thread_inside_commit(false) {}
98 ThreadProxy::MainThreadOrBlockedMainThread::~MainThreadOrBlockedMainThread() {}
100 PrioritizedResourceManager*
101 ThreadProxy::MainThreadOrBlockedMainThread::contents_texture_manager() {
102 return layer_tree_host->contents_texture_manager();
105 ThreadProxy::CompositorThreadOnly::CompositorThreadOnly(
106 ThreadProxy* proxy,
107 int layer_tree_host_id,
108 RenderingStatsInstrumentation* rendering_stats_instrumentation,
109 scoped_ptr<BeginFrameSource> external_begin_frame_source)
110 : layer_tree_host_id(layer_tree_host_id),
111 contents_texture_manager(NULL),
112 commit_completion_event(NULL),
113 completion_event_for_commit_held_on_tree_activation(NULL),
114 next_frame_is_newly_committed_frame(false),
115 inside_draw(false),
116 input_throttled_until_commit(false),
117 smoothness_priority_expiration_notifier(
118 proxy->ImplThreadTaskRunner(),
119 base::Bind(&ThreadProxy::RenewTreePriority, base::Unretained(proxy)),
120 base::TimeDelta::FromMilliseconds(
121 kSmoothnessTakesPriorityExpirationDelay * 1000)),
122 timing_history(rendering_stats_instrumentation),
123 external_begin_frame_source(external_begin_frame_source.Pass()),
124 weak_factory(proxy) {
127 ThreadProxy::CompositorThreadOnly::~CompositorThreadOnly() {}
129 ThreadProxy::~ThreadProxy() {
130 TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy");
131 DCHECK(IsMainThread());
132 DCHECK(!main().started);
135 void ThreadProxy::FinishAllRendering() {
136 DCHECK(Proxy::IsMainThread());
137 DCHECK(!main().defer_commits);
139 // Make sure all GL drawing is finished on the impl thread.
140 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
141 CompletionEvent completion;
142 Proxy::ImplThreadTaskRunner()->PostTask(
143 FROM_HERE,
144 base::Bind(&ThreadProxy::FinishAllRenderingOnImplThread,
145 impl_thread_weak_ptr_,
146 &completion));
147 completion.Wait();
150 bool ThreadProxy::IsStarted() const {
151 DCHECK(Proxy::IsMainThread());
152 return main().started;
155 bool ThreadProxy::CommitToActiveTree() const {
156 // With ThreadProxy and impl-side painting, we use a pending tree and activate
157 // it once it's ready to draw.
158 return !impl().layer_tree_host_impl->settings().impl_side_painting;
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_EVENT1("cc", "ThreadProxy::SetVisible", "visible", visible);
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_EVENT1("cc", "ThreadProxy::SetVisibleOnImplThread", "visible", visible);
191 impl().layer_tree_host_impl->SetVisible(visible);
192 impl().scheduler->SetVisible(visible);
193 completion->Signal();
196 void ThreadProxy::SetThrottleFrameProduction(bool throttle) {
197 TRACE_EVENT1("cc", "ThreadProxy::SetThrottleFrameProduction", "throttle",
198 throttle);
199 Proxy::ImplThreadTaskRunner()->PostTask(
200 FROM_HERE,
201 base::Bind(&ThreadProxy::SetThrottleFrameProductionOnImplThread,
202 impl_thread_weak_ptr_, throttle));
205 void ThreadProxy::SetThrottleFrameProductionOnImplThread(bool throttle) {
206 TRACE_EVENT1("cc", "ThreadProxy::SetThrottleFrameProductionOnImplThread",
207 "throttle", throttle);
208 impl().scheduler->SetThrottleFrameProduction(throttle);
211 void ThreadProxy::DidLoseOutputSurface() {
212 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurface");
213 DCHECK(IsMainThread());
214 layer_tree_host()->DidLoseOutputSurface();
217 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
219 // Return lost resources to their owners immediately.
220 BlockingTaskRunner::CapturePostTasks blocked(
221 blocking_main_thread_task_runner());
223 CompletionEvent completion;
224 Proxy::ImplThreadTaskRunner()->PostTask(
225 FROM_HERE,
226 base::Bind(&ThreadProxy::DeleteContentsTexturesOnImplThread,
227 impl_thread_weak_ptr_,
228 &completion));
229 completion.Wait();
233 void ThreadProxy::RequestNewOutputSurface() {
234 DCHECK(IsMainThread());
235 layer_tree_host()->RequestNewOutputSurface();
238 void ThreadProxy::SetOutputSurface(scoped_ptr<OutputSurface> output_surface) {
239 Proxy::ImplThreadTaskRunner()->PostTask(
240 FROM_HERE,
241 base::Bind(&ThreadProxy::InitializeOutputSurfaceOnImplThread,
242 impl_thread_weak_ptr_, base::Passed(&output_surface)));
245 void ThreadProxy::DidInitializeOutputSurface(
246 bool success,
247 const RendererCapabilities& capabilities) {
248 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeOutputSurface");
249 DCHECK(IsMainThread());
251 if (!success) {
252 layer_tree_host()->DidFailToInitializeOutputSurface();
253 return;
255 main().renderer_capabilities_main_thread_copy = capabilities;
256 layer_tree_host()->DidInitializeOutputSurface();
259 void ThreadProxy::SetRendererCapabilitiesMainThreadCopy(
260 const RendererCapabilities& capabilities) {
261 main().renderer_capabilities_main_thread_copy = capabilities;
264 void ThreadProxy::SendCommitRequestToImplThreadIfNeeded() {
265 DCHECK(IsMainThread());
266 if (main().commit_request_sent_to_impl_thread)
267 return;
268 main().commit_request_sent_to_impl_thread = true;
269 Proxy::ImplThreadTaskRunner()->PostTask(
270 FROM_HERE,
271 base::Bind(&ThreadProxy::SetNeedsCommitOnImplThread,
272 impl_thread_weak_ptr_));
275 void ThreadProxy::DidCompletePageScaleAnimation() {
276 DCHECK(IsMainThread());
277 layer_tree_host()->DidCompletePageScaleAnimation();
280 const RendererCapabilities& ThreadProxy::GetRendererCapabilities() const {
281 DCHECK(IsMainThread());
282 DCHECK(!layer_tree_host()->output_surface_lost());
283 return main().renderer_capabilities_main_thread_copy;
286 void ThreadProxy::SetNeedsAnimate() {
287 DCHECK(IsMainThread());
288 if (main().animate_requested)
289 return;
291 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimate");
292 main().animate_requested = true;
293 SendCommitRequestToImplThreadIfNeeded();
296 void ThreadProxy::SetNeedsUpdateLayers() {
297 DCHECK(IsMainThread());
299 if (main().commit_request_sent_to_impl_thread)
300 return;
301 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsUpdateLayers");
303 SendCommitRequestToImplThreadIfNeeded();
306 void ThreadProxy::SetNeedsCommit() {
307 DCHECK(IsMainThread());
308 // Unconditionally set here to handle SetNeedsCommit calls during a commit.
309 main().can_cancel_commit = false;
311 if (main().commit_requested)
312 return;
313 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommit");
314 main().commit_requested = true;
316 SendCommitRequestToImplThreadIfNeeded();
319 void ThreadProxy::UpdateRendererCapabilitiesOnImplThread() {
320 DCHECK(IsImplThread());
321 Proxy::MainThreadTaskRunner()->PostTask(
322 FROM_HERE,
323 base::Bind(&ThreadProxy::SetRendererCapabilitiesMainThreadCopy,
324 main_thread_weak_ptr_,
325 impl()
326 .layer_tree_host_impl->GetRendererCapabilities()
327 .MainThreadCapabilities()));
330 void ThreadProxy::DidLoseOutputSurfaceOnImplThread() {
331 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurfaceOnImplThread");
332 DCHECK(IsImplThread());
333 Proxy::MainThreadTaskRunner()->PostTask(
334 FROM_HERE,
335 base::Bind(&ThreadProxy::DidLoseOutputSurface, main_thread_weak_ptr_));
336 impl().scheduler->DidLoseOutputSurface();
339 void ThreadProxy::CommitVSyncParameters(base::TimeTicks timebase,
340 base::TimeDelta interval) {
341 impl().scheduler->CommitVSyncParameters(timebase, interval);
344 void ThreadProxy::SetEstimatedParentDrawTime(base::TimeDelta draw_time) {
345 impl().scheduler->SetEstimatedParentDrawTime(draw_time);
348 void ThreadProxy::SetMaxSwapsPendingOnImplThread(int max) {
349 impl().scheduler->SetMaxSwapsPending(max);
352 void ThreadProxy::DidSwapBuffersOnImplThread() {
353 impl().scheduler->DidSwapBuffers();
356 void ThreadProxy::DidSwapBuffersCompleteOnImplThread() {
357 TRACE_EVENT0("cc,benchmark",
358 "ThreadProxy::DidSwapBuffersCompleteOnImplThread");
359 DCHECK(IsImplThread());
360 impl().scheduler->DidSwapBuffersComplete();
361 Proxy::MainThreadTaskRunner()->PostTask(
362 FROM_HERE,
363 base::Bind(&ThreadProxy::DidCompleteSwapBuffers, main_thread_weak_ptr_));
366 void ThreadProxy::WillBeginImplFrame(const BeginFrameArgs& args) {
367 impl().layer_tree_host_impl->WillBeginImplFrame(args);
368 if (impl().last_processed_begin_main_frame_args.IsValid()) {
369 // Last processed begin main frame args records the frame args that we sent
370 // to the main thread for the last frame that we've processed. If that is
371 // set, that means the current frame is one past the frame in which we've
372 // finished the processing.
373 impl().layer_tree_host_impl->RecordMainFrameTiming(
374 impl().last_processed_begin_main_frame_args,
375 impl().layer_tree_host_impl->CurrentBeginFrameArgs());
376 impl().last_processed_begin_main_frame_args = BeginFrameArgs();
380 void ThreadProxy::OnCanDrawStateChanged(bool can_draw) {
381 TRACE_EVENT1(
382 "cc", "ThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw);
383 DCHECK(IsImplThread());
384 impl().scheduler->SetCanDraw(can_draw);
387 void ThreadProxy::NotifyReadyToActivate() {
388 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToActivate");
389 impl().scheduler->NotifyReadyToActivate();
392 void ThreadProxy::NotifyReadyToDraw() {
393 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToDraw");
394 impl().scheduler->NotifyReadyToDraw();
397 void ThreadProxy::SetNeedsCommitOnImplThread() {
398 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommitOnImplThread");
399 DCHECK(IsImplThread());
400 impl().scheduler->SetNeedsCommit();
403 void ThreadProxy::SetVideoNeedsBeginFrames(bool needs_begin_frames) {
404 TRACE_EVENT1("cc", "ThreadProxy::SetVideoNeedsBeginFrames",
405 "needs_begin_frames", needs_begin_frames);
406 DCHECK(IsImplThread());
407 // In tests the layer tree is destroyed after the scheduler is.
408 if (impl().scheduler)
409 impl().scheduler->SetVideoNeedsBeginFrames(needs_begin_frames);
412 void ThreadProxy::PostAnimationEventsToMainThreadOnImplThread(
413 scoped_ptr<AnimationEventsVector> events) {
414 TRACE_EVENT0("cc",
415 "ThreadProxy::PostAnimationEventsToMainThreadOnImplThread");
416 DCHECK(IsImplThread());
417 Proxy::MainThreadTaskRunner()->PostTask(
418 FROM_HERE,
419 base::Bind(&ThreadProxy::SetAnimationEvents,
420 main_thread_weak_ptr_,
421 base::Passed(&events)));
424 bool ThreadProxy::ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
425 int priority_cutoff) {
426 DCHECK(IsImplThread());
428 if (!impl().contents_texture_manager)
429 return false;
430 if (!impl().layer_tree_host_impl->resource_provider())
431 return false;
433 bool reduce_result =
434 impl().contents_texture_manager->ReduceMemoryOnImplThread(
435 limit_bytes,
436 priority_cutoff,
437 impl().layer_tree_host_impl->resource_provider());
438 if (!reduce_result)
439 return false;
441 // The texture upload queue may reference textures that were just purged,
442 // clear them from the queue.
443 if (impl().current_resource_update_controller) {
444 impl()
445 .current_resource_update_controller->DiscardUploadsToEvictedResources();
447 return true;
450 bool ThreadProxy::IsInsideDraw() { return impl().inside_draw; }
452 void ThreadProxy::SetNeedsRedraw(const gfx::Rect& damage_rect) {
453 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedraw");
454 DCHECK(IsMainThread());
455 Proxy::ImplThreadTaskRunner()->PostTask(
456 FROM_HERE,
457 base::Bind(&ThreadProxy::SetNeedsRedrawRectOnImplThread,
458 impl_thread_weak_ptr_,
459 damage_rect));
462 void ThreadProxy::SetNextCommitWaitsForActivation() {
463 DCHECK(IsMainThread());
464 DCHECK(!blocked_main().main_thread_inside_commit);
465 blocked_main().commit_waits_for_activation = true;
468 void ThreadProxy::SetDeferCommits(bool defer_commits) {
469 DCHECK(IsMainThread());
470 if (main().defer_commits == defer_commits)
471 return;
473 main().defer_commits = defer_commits;
474 if (main().defer_commits)
475 TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::SetDeferCommits", this);
476 else
477 TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::SetDeferCommits", this);
479 Proxy::ImplThreadTaskRunner()->PostTask(
480 FROM_HERE,
481 base::Bind(&ThreadProxy::SetDeferCommitsOnImplThread,
482 impl_thread_weak_ptr_,
483 defer_commits));
486 void ThreadProxy::SetDeferCommitsOnImplThread(bool defer_commits) const {
487 DCHECK(IsImplThread());
488 impl().scheduler->SetDeferCommits(defer_commits);
491 bool ThreadProxy::CommitRequested() const {
492 DCHECK(IsMainThread());
493 return main().commit_requested;
496 bool ThreadProxy::BeginMainFrameRequested() const {
497 DCHECK(IsMainThread());
498 return main().commit_request_sent_to_impl_thread;
501 void ThreadProxy::SetNeedsRedrawOnImplThread() {
502 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedrawOnImplThread");
503 DCHECK(IsImplThread());
504 impl().scheduler->SetNeedsRedraw();
507 void ThreadProxy::SetNeedsAnimateOnImplThread() {
508 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimateOnImplThread");
509 DCHECK(IsImplThread());
510 impl().scheduler->SetNeedsAnimate();
513 void ThreadProxy::SetNeedsPrepareTilesOnImplThread() {
514 DCHECK(IsImplThread());
515 impl().scheduler->SetNeedsPrepareTiles();
518 void ThreadProxy::SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) {
519 DCHECK(IsImplThread());
520 impl().layer_tree_host_impl->SetViewportDamage(damage_rect);
521 SetNeedsRedrawOnImplThread();
524 void ThreadProxy::MainThreadHasStoppedFlinging() {
525 DCHECK(IsMainThread());
526 Proxy::ImplThreadTaskRunner()->PostTask(
527 FROM_HERE,
528 base::Bind(&ThreadProxy::MainThreadHasStoppedFlingingOnImplThread,
529 impl_thread_weak_ptr_));
532 void ThreadProxy::MainThreadHasStoppedFlingingOnImplThread() {
533 DCHECK(IsImplThread());
534 impl().layer_tree_host_impl->MainThreadHasStoppedFlinging();
537 void ThreadProxy::NotifyInputThrottledUntilCommit() {
538 DCHECK(IsMainThread());
539 Proxy::ImplThreadTaskRunner()->PostTask(
540 FROM_HERE,
541 base::Bind(&ThreadProxy::SetInputThrottledUntilCommitOnImplThread,
542 impl_thread_weak_ptr_,
543 true));
546 void ThreadProxy::SetInputThrottledUntilCommitOnImplThread(bool is_throttled) {
547 DCHECK(IsImplThread());
548 if (is_throttled == impl().input_throttled_until_commit)
549 return;
550 impl().input_throttled_until_commit = is_throttled;
551 RenewTreePriority();
554 LayerTreeHost* ThreadProxy::layer_tree_host() {
555 return blocked_main().layer_tree_host;
558 const LayerTreeHost* ThreadProxy::layer_tree_host() const {
559 return blocked_main().layer_tree_host;
562 ThreadProxy::MainThreadOnly& ThreadProxy::main() {
563 DCHECK(IsMainThread());
564 return main_thread_only_vars_unsafe_;
566 const ThreadProxy::MainThreadOnly& ThreadProxy::main() const {
567 DCHECK(IsMainThread());
568 return main_thread_only_vars_unsafe_;
571 ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main() {
572 DCHECK(IsMainThread() || IsMainThreadBlocked());
573 return main_thread_or_blocked_vars_unsafe_;
576 const ThreadProxy::MainThreadOrBlockedMainThread& ThreadProxy::blocked_main()
577 const {
578 DCHECK(IsMainThread() || IsMainThreadBlocked());
579 return main_thread_or_blocked_vars_unsafe_;
582 ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() {
583 DCHECK(IsImplThread());
584 return compositor_thread_vars_unsafe_;
587 const ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() const {
588 DCHECK(IsImplThread());
589 return compositor_thread_vars_unsafe_;
592 void ThreadProxy::Start() {
593 DCHECK(IsMainThread());
594 DCHECK(Proxy::HasImplThread());
596 // Create LayerTreeHostImpl.
597 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
598 CompletionEvent completion;
599 Proxy::ImplThreadTaskRunner()->PostTask(
600 FROM_HERE,
601 base::Bind(&ThreadProxy::InitializeImplOnImplThread,
602 base::Unretained(this),
603 &completion));
604 completion.Wait();
606 main_thread_weak_ptr_ = main().weak_factory.GetWeakPtr();
608 main().started = true;
611 void ThreadProxy::Stop() {
612 TRACE_EVENT0("cc", "ThreadProxy::Stop");
613 DCHECK(IsMainThread());
614 DCHECK(main().started);
616 // Synchronously finishes pending GL operations and deletes the impl.
617 // The two steps are done as separate post tasks, so that tasks posted
618 // by the GL implementation due to the Finish can be executed by the
619 // renderer before shutting it down.
621 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
623 CompletionEvent completion;
624 Proxy::ImplThreadTaskRunner()->PostTask(
625 FROM_HERE,
626 base::Bind(&ThreadProxy::FinishGLOnImplThread,
627 impl_thread_weak_ptr_,
628 &completion));
629 completion.Wait();
632 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
634 CompletionEvent completion;
635 Proxy::ImplThreadTaskRunner()->PostTask(
636 FROM_HERE,
637 base::Bind(&ThreadProxy::LayerTreeHostClosedOnImplThread,
638 impl_thread_weak_ptr_,
639 &completion));
640 completion.Wait();
643 main().weak_factory.InvalidateWeakPtrs();
644 blocked_main().layer_tree_host = NULL;
645 main().started = false;
648 void ThreadProxy::ForceSerializeOnSwapBuffers() {
649 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
650 CompletionEvent completion;
651 Proxy::ImplThreadTaskRunner()->PostTask(
652 FROM_HERE,
653 base::Bind(&ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread,
654 impl_thread_weak_ptr_,
655 &completion));
656 completion.Wait();
659 void ThreadProxy::ForceSerializeOnSwapBuffersOnImplThread(
660 CompletionEvent* completion) {
661 if (impl().layer_tree_host_impl->renderer())
662 impl().layer_tree_host_impl->renderer()->DoNoOp();
663 completion->Signal();
666 bool ThreadProxy::SupportsImplScrolling() const {
667 return true;
670 void ThreadProxy::SetDebugState(const LayerTreeDebugState& debug_state) {
671 Proxy::ImplThreadTaskRunner()->PostTask(
672 FROM_HERE,
673 base::Bind(&ThreadProxy::SetDebugStateOnImplThread,
674 impl_thread_weak_ptr_,
675 debug_state));
678 void ThreadProxy::SetDebugStateOnImplThread(
679 const LayerTreeDebugState& debug_state) {
680 DCHECK(IsImplThread());
681 impl().scheduler->SetContinuousPainting(debug_state.continuous_painting);
684 void ThreadProxy::FinishAllRenderingOnImplThread(CompletionEvent* completion) {
685 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread");
686 DCHECK(IsImplThread());
687 impl().layer_tree_host_impl->FinishAllRendering();
688 completion->Signal();
691 void ThreadProxy::ScheduledActionSendBeginMainFrame() {
692 unsigned int begin_frame_id = nextBeginFrameId++;
693 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
694 benchmark_instrumentation::kSendBeginFrame, begin_frame_id);
695 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state(
696 new BeginMainFrameAndCommitState);
697 begin_main_frame_state->begin_frame_id = begin_frame_id;
698 begin_main_frame_state->begin_frame_args =
699 impl().layer_tree_host_impl->CurrentBeginFrameArgs();
700 begin_main_frame_state->scroll_info =
701 impl().layer_tree_host_impl->ProcessScrollDeltas();
703 if (!impl().layer_tree_host_impl->settings().impl_side_painting) {
704 DCHECK_GT(impl().layer_tree_host_impl->memory_allocation_limit_bytes(), 0u);
706 begin_main_frame_state->memory_allocation_limit_bytes =
707 impl().layer_tree_host_impl->memory_allocation_limit_bytes();
708 begin_main_frame_state->memory_allocation_priority_cutoff =
709 impl().layer_tree_host_impl->memory_allocation_priority_cutoff();
710 begin_main_frame_state->evicted_ui_resources =
711 impl().layer_tree_host_impl->EvictedUIResourcesExist();
712 // TODO(vmpstr): This needs to be fixed if
713 // main_frame_before_activation_enabled is set, since we might run this code
714 // twice before recording a duration. crbug.com/469824
715 impl().last_begin_main_frame_args = begin_main_frame_state->begin_frame_args;
716 Proxy::MainThreadTaskRunner()->PostTask(
717 FROM_HERE,
718 base::Bind(&ThreadProxy::BeginMainFrame,
719 main_thread_weak_ptr_,
720 base::Passed(&begin_main_frame_state)));
721 devtools_instrumentation::DidRequestMainThreadFrame(
722 impl().layer_tree_host_id);
723 impl().timing_history.DidBeginMainFrame();
726 void ThreadProxy::SendBeginMainFrameNotExpectedSoon() {
727 Proxy::MainThreadTaskRunner()->PostTask(
728 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameNotExpectedSoon,
729 main_thread_weak_ptr_));
732 void ThreadProxy::BeginMainFrame(
733 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
734 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
735 benchmark_instrumentation::kDoBeginFrame,
736 begin_main_frame_state->begin_frame_id);
737 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
738 DCHECK(IsMainThread());
740 if (main().defer_commits) {
741 TRACE_EVENT_INSTANT0("cc", "EarlyOut_DeferCommit",
742 TRACE_EVENT_SCOPE_THREAD);
743 Proxy::ImplThreadTaskRunner()->PostTask(
744 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
745 impl_thread_weak_ptr_,
746 CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT));
747 return;
750 // If the commit finishes, LayerTreeHost will transfer its swap promises to
751 // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the
752 // remaining swap promises.
753 ScopedAbortRemainingSwapPromises swap_promise_checker(layer_tree_host());
755 main().commit_requested = false;
756 main().commit_request_sent_to_impl_thread = false;
757 main().animate_requested = false;
759 if (!layer_tree_host()->visible()) {
760 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
761 Proxy::ImplThreadTaskRunner()->PostTask(
762 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
763 impl_thread_weak_ptr_,
764 CommitEarlyOutReason::ABORTED_NOT_VISIBLE));
765 return;
768 if (layer_tree_host()->output_surface_lost()) {
769 TRACE_EVENT_INSTANT0(
770 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD);
771 Proxy::ImplThreadTaskRunner()->PostTask(
772 FROM_HERE,
773 base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
774 impl_thread_weak_ptr_,
775 CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST));
776 return;
779 // Do not notify the impl thread of commit requests that occur during
780 // the apply/animate/layout part of the BeginMainFrameAndCommit process since
781 // those commit requests will get painted immediately. Once we have done
782 // the paint, main().commit_requested will be set to false to allow new commit
783 // requests to be scheduled.
784 // On the other hand, the animate_requested flag should remain cleared
785 // here so that any animation requests generated by the apply or animate
786 // callbacks will trigger another frame.
787 main().commit_requested = true;
788 main().commit_request_sent_to_impl_thread = true;
790 layer_tree_host()->ApplyScrollAndScale(
791 begin_main_frame_state->scroll_info.get());
793 layer_tree_host()->WillBeginMainFrame();
795 layer_tree_host()->BeginMainFrame(begin_main_frame_state->begin_frame_args);
796 layer_tree_host()->AnimateLayers(
797 begin_main_frame_state->begin_frame_args.frame_time);
799 // Unlink any backings that the impl thread has evicted, so that we know to
800 // re-paint them in UpdateLayers.
801 if (blocked_main().contents_texture_manager()) {
802 blocked_main().contents_texture_manager()->UnlinkAndClearEvictedBackings();
804 blocked_main().contents_texture_manager()->SetMaxMemoryLimitBytes(
805 begin_main_frame_state->memory_allocation_limit_bytes);
806 blocked_main().contents_texture_manager()->SetExternalPriorityCutoff(
807 begin_main_frame_state->memory_allocation_priority_cutoff);
810 // Recreate all UI resources if there were evicted UI resources when the impl
811 // thread initiated the commit.
812 if (begin_main_frame_state->evicted_ui_resources)
813 layer_tree_host()->RecreateUIResources();
815 layer_tree_host()->Layout();
816 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
818 // Clear the commit flag after updating animations and layout here --- objects
819 // that only layout when painted will trigger another SetNeedsCommit inside
820 // UpdateLayers.
821 main().commit_requested = false;
822 main().commit_request_sent_to_impl_thread = false;
823 bool can_cancel_this_commit =
824 main().can_cancel_commit && !begin_main_frame_state->evicted_ui_resources;
825 main().can_cancel_commit = true;
827 scoped_ptr<ResourceUpdateQueue> queue =
828 make_scoped_ptr(new ResourceUpdateQueue);
830 bool updated = layer_tree_host()->UpdateLayers(queue.get());
832 layer_tree_host()->WillCommit();
833 devtools_instrumentation::ScopedCommitTrace commit_task(
834 layer_tree_host()->id());
836 // Before calling animate, we set main().animate_requested to false. If it is
837 // true now, it means SetNeedAnimate was called again, but during a state when
838 // main().commit_request_sent_to_impl_thread = true. We need to force that
839 // call to happen again now so that the commit request is sent to the impl
840 // thread.
841 if (main().animate_requested) {
842 // Forces SetNeedsAnimate to consider posting a commit task.
843 main().animate_requested = false;
844 SetNeedsAnimate();
847 if (!updated && can_cancel_this_commit) {
848 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
849 Proxy::ImplThreadTaskRunner()->PostTask(
850 FROM_HERE, base::Bind(&ThreadProxy::BeginMainFrameAbortedOnImplThread,
851 impl_thread_weak_ptr_,
852 CommitEarlyOutReason::FINISHED_NO_UPDATES));
854 // Although the commit is internally aborted, this is because it has been
855 // detected to be a no-op. From the perspective of an embedder, this commit
856 // went through, and input should no longer be throttled, etc.
857 layer_tree_host()->CommitComplete();
858 layer_tree_host()->DidBeginMainFrame();
859 layer_tree_host()->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE);
860 return;
863 // Notify the impl thread that the main thread is ready to commit. This will
864 // begin the commit process, which is blocking from the main thread's
865 // point of view, but asynchronously performed on the impl thread,
866 // coordinated by the Scheduler.
868 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit");
870 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
872 // This CapturePostTasks should be destroyed before CommitComplete() is
873 // called since that goes out to the embedder, and we want the embedder
874 // to receive its callbacks before that.
875 BlockingTaskRunner::CapturePostTasks blocked(
876 blocking_main_thread_task_runner());
878 CompletionEvent completion;
879 Proxy::ImplThreadTaskRunner()->PostTask(
880 FROM_HERE,
881 base::Bind(&ThreadProxy::StartCommitOnImplThread,
882 impl_thread_weak_ptr_,
883 &completion,
884 queue.release()));
885 completion.Wait();
888 layer_tree_host()->CommitComplete();
889 layer_tree_host()->DidBeginMainFrame();
892 void ThreadProxy::BeginMainFrameNotExpectedSoon() {
893 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameNotExpectedSoon");
894 DCHECK(IsMainThread());
895 layer_tree_host()->BeginMainFrameNotExpectedSoon();
898 void ThreadProxy::StartCommitOnImplThread(CompletionEvent* completion,
899 ResourceUpdateQueue* raw_queue) {
900 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread");
901 DCHECK(!impl().commit_completion_event);
902 DCHECK(IsImplThread() && IsMainThreadBlocked());
903 DCHECK(impl().scheduler);
904 DCHECK(impl().scheduler->CommitPending());
906 if (!impl().layer_tree_host_impl) {
907 TRACE_EVENT_INSTANT0(
908 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD);
909 completion->Signal();
910 return;
913 // Ideally, we should inform to impl thread when BeginMainFrame is started.
914 // But, we can avoid a PostTask in here.
915 impl().scheduler->NotifyBeginMainFrameStarted();
917 scoped_ptr<ResourceUpdateQueue> queue(raw_queue);
919 if (impl().contents_texture_manager) {
920 DCHECK_EQ(impl().contents_texture_manager,
921 blocked_main().contents_texture_manager());
922 } else {
923 // Cache this pointer that was created on the main thread side to avoid a
924 // data race between creating it and using it on the compositor thread.
925 impl().contents_texture_manager = blocked_main().contents_texture_manager();
928 if (impl().contents_texture_manager) {
929 if (impl().contents_texture_manager->LinkedEvictedBackingsExist()) {
930 // Clear any uploads we were making to textures linked to evicted
931 // resources
932 queue->ClearUploadsToEvictedResources();
933 // Some textures in the layer tree are invalid. Kick off another commit
934 // to fill them again.
935 SetNeedsCommitOnImplThread();
938 impl().contents_texture_manager->PushTexturePrioritiesToBackings();
941 impl().commit_completion_event = completion;
942 impl().current_resource_update_controller = ResourceUpdateController::Create(
943 this,
944 Proxy::ImplThreadTaskRunner(),
945 queue.Pass(),
946 impl().layer_tree_host_impl->resource_provider());
947 impl().current_resource_update_controller->PerformMoreUpdates(
948 impl().scheduler->AnticipatedDrawTime());
951 void ThreadProxy::BeginMainFrameAbortedOnImplThread(
952 CommitEarlyOutReason reason) {
953 TRACE_EVENT1("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread", "reason",
954 CommitEarlyOutReasonToString(reason));
955 DCHECK(IsImplThread());
956 DCHECK(impl().scheduler);
957 DCHECK(impl().scheduler->CommitPending());
958 DCHECK(!impl().layer_tree_host_impl->pending_tree());
960 if (CommitEarlyOutHandledCommit(reason)) {
961 SetInputThrottledUntilCommitOnImplThread(false);
962 impl().last_processed_begin_main_frame_args =
963 impl().last_begin_main_frame_args;
965 impl().layer_tree_host_impl->BeginMainFrameAborted(reason);
966 impl().scheduler->BeginMainFrameAborted(reason);
969 void ThreadProxy::ScheduledActionAnimate() {
970 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate");
971 DCHECK(IsImplThread());
973 // Don't animate if there is no root layer.
974 // TODO(mithro): Both Animate and UpdateAnimationState already have a
975 // "!active_tree_->root_layer()" check?
976 if (!impl().layer_tree_host_impl->active_tree()->root_layer()) {
977 return;
980 impl().animation_time =
981 impl().layer_tree_host_impl->CurrentBeginFrameArgs().frame_time;
982 impl().layer_tree_host_impl->Animate(impl().animation_time);
984 // If animations are not visible, update the state now as
985 // ScheduledActionDrawAndSwapIfPossible will never be called.
986 if (!impl().layer_tree_host_impl->AnimationsAreVisible()) {
987 impl().layer_tree_host_impl->UpdateAnimationState(true);
991 void ThreadProxy::ScheduledActionCommit() {
992 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit");
993 DCHECK(IsImplThread());
994 DCHECK(IsMainThreadBlocked());
995 DCHECK(impl().commit_completion_event);
996 DCHECK(impl().current_resource_update_controller);
998 // Complete all remaining texture updates.
999 impl().current_resource_update_controller->Finalize();
1000 impl().current_resource_update_controller = nullptr;
1002 blocked_main().main_thread_inside_commit = true;
1003 impl().layer_tree_host_impl->BeginCommit();
1004 layer_tree_host()->BeginCommitOnImplThread(impl().layer_tree_host_impl.get());
1005 layer_tree_host()->FinishCommitOnImplThread(
1006 impl().layer_tree_host_impl.get());
1007 blocked_main().main_thread_inside_commit = false;
1009 bool hold_commit = layer_tree_host()->settings().impl_side_painting &&
1010 blocked_main().commit_waits_for_activation;
1011 blocked_main().commit_waits_for_activation = false;
1013 if (hold_commit) {
1014 // For some layer types in impl-side painting, the commit is held until
1015 // the sync tree is activated. It's also possible that the
1016 // sync tree has already activated if there was no work to be done.
1017 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD);
1018 impl().completion_event_for_commit_held_on_tree_activation =
1019 impl().commit_completion_event;
1020 impl().commit_completion_event = NULL;
1021 } else {
1022 impl().commit_completion_event->Signal();
1023 impl().commit_completion_event = NULL;
1026 // Delay this step until afer the main thread has been released as it's
1027 // often a good bit of work to update the tree and prepare the new frame.
1028 impl().layer_tree_host_impl->CommitComplete();
1030 SetInputThrottledUntilCommitOnImplThread(false);
1032 impl().next_frame_is_newly_committed_frame = true;
1034 impl().timing_history.DidCommit();
1037 void ThreadProxy::ScheduledActionActivateSyncTree() {
1038 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree");
1039 DCHECK(IsImplThread());
1040 impl().layer_tree_host_impl->ActivateSyncTree();
1043 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() {
1044 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation");
1045 DCHECK(IsImplThread());
1046 Proxy::MainThreadTaskRunner()->PostTask(
1047 FROM_HERE,
1048 base::Bind(&ThreadProxy::RequestNewOutputSurface, main_thread_weak_ptr_));
1051 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) {
1052 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap");
1053 DrawResult result;
1055 DCHECK(IsImplThread());
1056 DCHECK(impl().layer_tree_host_impl.get());
1058 impl().timing_history.DidStartDrawing();
1059 base::AutoReset<bool> mark_inside(&impl().inside_draw, true);
1061 if (impl().layer_tree_host_impl->pending_tree()) {
1062 bool update_lcd_text = false;
1063 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties(
1064 update_lcd_text);
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 } else {
1092 DCHECK_NE(DRAW_SUCCESS, result);
1094 impl().layer_tree_host_impl->DidDrawAllLayers(frame);
1096 bool start_ready_animations = draw_frame;
1097 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations);
1099 if (draw_frame)
1100 impl().layer_tree_host_impl->SwapBuffers(frame);
1102 // Tell the main thread that the the newly-commited frame was drawn.
1103 if (impl().next_frame_is_newly_committed_frame) {
1104 impl().next_frame_is_newly_committed_frame = false;
1105 Proxy::MainThreadTaskRunner()->PostTask(
1106 FROM_HERE,
1107 base::Bind(&ThreadProxy::DidCommitAndDrawFrame, main_thread_weak_ptr_));
1110 if (result == DRAW_SUCCESS)
1111 impl().timing_history.DidFinishDrawing();
1113 DCHECK_NE(INVALID_RESULT, result);
1114 return result;
1117 void ThreadProxy::ScheduledActionPrepareTiles() {
1118 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionPrepareTiles");
1119 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1120 impl().layer_tree_host_impl->PrepareTiles();
1123 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() {
1124 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap");
1126 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to
1127 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should
1128 // never generate this call when it can't draw.
1129 DCHECK(impl().layer_tree_host_impl->CanDraw());
1131 bool forced_draw = false;
1132 return DrawSwapInternal(forced_draw);
1135 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() {
1136 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced");
1137 bool forced_draw = true;
1138 return DrawSwapInternal(forced_draw);
1141 void ThreadProxy::ScheduledActionInvalidateOutputSurface() {
1142 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionInvalidateOutputSurface");
1143 DCHECK(impl().layer_tree_host_impl->output_surface());
1144 impl().layer_tree_host_impl->output_surface()->Invalidate();
1147 void ThreadProxy::DidAnticipatedDrawTimeChange(base::TimeTicks time) {
1148 if (impl().current_resource_update_controller)
1149 impl().current_resource_update_controller->PerformMoreUpdates(time);
1152 base::TimeDelta ThreadProxy::DrawDurationEstimate() {
1153 return impl().timing_history.DrawDurationEstimate();
1156 base::TimeDelta ThreadProxy::BeginMainFrameToCommitDurationEstimate() {
1157 return impl().timing_history.BeginMainFrameToCommitDurationEstimate();
1160 base::TimeDelta ThreadProxy::CommitToActivateDurationEstimate() {
1161 return impl().timing_history.CommitToActivateDurationEstimate();
1164 void ThreadProxy::DidFinishImplFrame() {
1165 impl().layer_tree_host_impl->DidFinishImplFrame();
1168 void ThreadProxy::SendBeginFramesToChildren(const BeginFrameArgs& args) {
1169 NOTREACHED() << "Only used by SingleThreadProxy";
1172 void ThreadProxy::SetAuthoritativeVSyncInterval(
1173 const base::TimeDelta& interval) {
1174 NOTREACHED() << "Only used by SingleThreadProxy";
1177 void ThreadProxy::ReadyToFinalizeTextureUpdates() {
1178 DCHECK(IsImplThread());
1179 impl().scheduler->NotifyReadyToCommit();
1182 void ThreadProxy::DidCommitAndDrawFrame() {
1183 DCHECK(IsMainThread());
1184 layer_tree_host()->DidCommitAndDrawFrame();
1187 void ThreadProxy::DidCompleteSwapBuffers() {
1188 DCHECK(IsMainThread());
1189 layer_tree_host()->DidCompleteSwapBuffers();
1192 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
1193 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents");
1194 DCHECK(IsMainThread());
1195 layer_tree_host()->SetAnimationEvents(events.Pass());
1198 void ThreadProxy::InitializeImplOnImplThread(CompletionEvent* completion) {
1199 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread");
1200 DCHECK(IsImplThread());
1201 impl().layer_tree_host_impl =
1202 layer_tree_host()->CreateLayerTreeHostImpl(this);
1203 SchedulerSettings scheduler_settings(
1204 layer_tree_host()->settings().ToSchedulerSettings());
1205 impl().scheduler = Scheduler::Create(
1206 this,
1207 scheduler_settings,
1208 impl().layer_tree_host_id,
1209 ImplThreadTaskRunner(),
1210 impl().external_begin_frame_source.Pass());
1211 impl().scheduler->SetVisible(impl().layer_tree_host_impl->visible());
1212 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr();
1213 completion->Signal();
1216 void ThreadProxy::DeleteContentsTexturesOnImplThread(
1217 CompletionEvent* completion) {
1218 TRACE_EVENT0("cc", "ThreadProxy::DeleteContentsTexturesOnImplThread");
1219 DCHECK(IsImplThread());
1220 DCHECK(IsMainThreadBlocked());
1221 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1222 impl().layer_tree_host_impl->resource_provider());
1223 completion->Signal();
1226 void ThreadProxy::InitializeOutputSurfaceOnImplThread(
1227 scoped_ptr<OutputSurface> output_surface) {
1228 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread");
1229 DCHECK(IsImplThread());
1231 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get();
1232 bool success = host_impl->InitializeRenderer(output_surface.Pass());
1233 RendererCapabilities capabilities;
1234 if (success) {
1235 capabilities =
1236 host_impl->GetRendererCapabilities().MainThreadCapabilities();
1239 Proxy::MainThreadTaskRunner()->PostTask(
1240 FROM_HERE,
1241 base::Bind(&ThreadProxy::DidInitializeOutputSurface,
1242 main_thread_weak_ptr_,
1243 success,
1244 capabilities));
1246 if (success)
1247 impl().scheduler->DidCreateAndInitializeOutputSurface();
1250 void ThreadProxy::FinishGLOnImplThread(CompletionEvent* completion) {
1251 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread");
1252 DCHECK(IsImplThread());
1253 if (impl().layer_tree_host_impl->output_surface()) {
1254 ContextProvider* context_provider =
1255 impl().layer_tree_host_impl->output_surface()->context_provider();
1256 if (context_provider)
1257 context_provider->ContextGL()->Finish();
1259 completion->Signal();
1262 void ThreadProxy::LayerTreeHostClosedOnImplThread(CompletionEvent* completion) {
1263 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread");
1264 DCHECK(IsImplThread());
1265 DCHECK(IsMainThreadBlocked());
1266 layer_tree_host()->DeleteContentsTexturesOnImplThread(
1267 impl().layer_tree_host_impl->resource_provider());
1268 impl().current_resource_update_controller = nullptr;
1269 impl().scheduler = nullptr;
1270 impl().layer_tree_host_impl = nullptr;
1271 impl().weak_factory.InvalidateWeakPtrs();
1272 // We need to explicitly shutdown the notifier to destroy any weakptrs it is
1273 // holding while still on the compositor thread. This also ensures any
1274 // callbacks holding a ThreadProxy pointer are cancelled.
1275 impl().smoothness_priority_expiration_notifier.Shutdown();
1276 impl().contents_texture_manager = NULL;
1277 completion->Signal();
1280 size_t ThreadProxy::MaxPartialTextureUpdates() const {
1281 return ResourceUpdateController::MaxPartialTextureUpdates();
1284 ThreadProxy::BeginMainFrameAndCommitState::BeginMainFrameAndCommitState()
1285 : memory_allocation_limit_bytes(0),
1286 memory_allocation_priority_cutoff(0),
1287 evicted_ui_resources(false) {}
1289 ThreadProxy::BeginMainFrameAndCommitState::~BeginMainFrameAndCommitState() {}
1291 bool ThreadProxy::MainFrameWillHappenForTesting() {
1292 DCHECK(IsMainThread());
1293 CompletionEvent completion;
1294 bool main_frame_will_happen = false;
1296 DebugScopedSetMainThreadBlocked main_thread_blocked(this);
1297 Proxy::ImplThreadTaskRunner()->PostTask(
1298 FROM_HERE,
1299 base::Bind(&ThreadProxy::MainFrameWillHappenOnImplThreadForTesting,
1300 impl_thread_weak_ptr_,
1301 &completion,
1302 &main_frame_will_happen));
1303 completion.Wait();
1305 return main_frame_will_happen;
1308 void ThreadProxy::SetChildrenNeedBeginFrames(bool children_need_begin_frames) {
1309 NOTREACHED() << "Only used by SingleThreadProxy";
1312 void ThreadProxy::MainFrameWillHappenOnImplThreadForTesting(
1313 CompletionEvent* completion,
1314 bool* main_frame_will_happen) {
1315 DCHECK(IsImplThread());
1316 if (impl().layer_tree_host_impl->output_surface()) {
1317 *main_frame_will_happen = impl().scheduler->MainFrameForTestingWillHappen();
1318 } else {
1319 *main_frame_will_happen = false;
1321 completion->Signal();
1324 void ThreadProxy::RenewTreePriority() {
1325 DCHECK(IsImplThread());
1326 bool smoothness_takes_priority =
1327 impl().layer_tree_host_impl->pinch_gesture_active() ||
1328 impl().layer_tree_host_impl->page_scale_animation_active() ||
1329 impl().layer_tree_host_impl->IsActivelyScrolling();
1331 // Schedule expiration if smoothness currently takes priority.
1332 if (smoothness_takes_priority)
1333 impl().smoothness_priority_expiration_notifier.Schedule();
1335 // We use the same priority for both trees by default.
1336 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES;
1338 // Smoothness takes priority if we have an expiration for it scheduled.
1339 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification())
1340 priority = SMOOTHNESS_TAKES_PRIORITY;
1342 // New content always takes priority when the active tree has
1343 // evicted resources or there is an invalid viewport size.
1344 if (impl().layer_tree_host_impl->active_tree()->ContentsTexturesPurged() ||
1345 impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() ||
1346 impl().layer_tree_host_impl->EvictedUIResourcesExist() ||
1347 impl().input_throttled_until_commit) {
1348 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active
1349 // tree might be freed. We need to set RequiresHighResToDraw to ensure that
1350 // high res tiles will be required to activate pending tree.
1351 impl().layer_tree_host_impl->SetRequiresHighResToDraw();
1352 priority = NEW_CONTENT_TAKES_PRIORITY;
1355 impl().layer_tree_host_impl->SetTreePriority(priority);
1357 // Only put the scheduler in impl latency prioritization mode if we don't
1358 // have a scroll listener. This gives the scroll listener a better chance of
1359 // handling scroll updates within the same frame. The tree itself is still
1360 // kept in prefer smoothness mode to allow checkerboarding.
1361 impl().scheduler->SetImplLatencyTakesPriority(
1362 priority == SMOOTHNESS_TAKES_PRIORITY &&
1363 !impl().layer_tree_host_impl->scroll_affects_scroll_handler());
1365 // Notify the the client of this compositor via the output surface.
1366 // TODO(epenner): Route this to compositor-thread instead of output-surface
1367 // after GTFO refactor of compositor-thread (http://crbug/170828).
1368 if (impl().layer_tree_host_impl->output_surface()) {
1369 impl()
1370 .layer_tree_host_impl->output_surface()
1371 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY);
1375 void ThreadProxy::PostDelayedAnimationTaskOnImplThread(
1376 const base::Closure& task,
1377 base::TimeDelta delay) {
1378 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, task, delay);
1381 void ThreadProxy::DidActivateSyncTree() {
1382 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread");
1383 DCHECK(IsImplThread());
1385 if (impl().completion_event_for_commit_held_on_tree_activation) {
1386 TRACE_EVENT_INSTANT0(
1387 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD);
1388 DCHECK(impl().layer_tree_host_impl->settings().impl_side_painting);
1389 impl().completion_event_for_commit_held_on_tree_activation->Signal();
1390 impl().completion_event_for_commit_held_on_tree_activation = NULL;
1393 impl().timing_history.DidActivateSyncTree();
1394 impl().last_processed_begin_main_frame_args =
1395 impl().last_begin_main_frame_args;
1398 void ThreadProxy::DidPrepareTiles() {
1399 DCHECK(IsImplThread());
1400 impl().scheduler->DidPrepareTiles();
1403 void ThreadProxy::DidCompletePageScaleAnimationOnImplThread() {
1404 DCHECK(IsImplThread());
1405 Proxy::MainThreadTaskRunner()->PostTask(
1406 FROM_HERE, base::Bind(&ThreadProxy::DidCompletePageScaleAnimation,
1407 main_thread_weak_ptr_));
1410 void ThreadProxy::OnDrawForOutputSurface() {
1411 DCHECK(IsImplThread());
1412 impl().scheduler->OnDrawForOutputSurface();
1415 } // namespace cc