Chromium Code Reviews| Index: cc/trees/proxy_main.cc |
| diff --git a/cc/trees/proxy_main.cc b/cc/trees/proxy_main.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cc9f4e272d9e53d9bdc22a5dd7b6586472dc0174 |
| --- /dev/null |
| +++ b/cc/trees/proxy_main.cc |
| @@ -0,0 +1,488 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cc/trees/proxy_main.h" |
| + |
| +#include <algorithm> |
| +#include <string> |
| + |
| +#include "base/auto_reset.h" |
| +#include "base/bind.h" |
| +#include "base/trace_event/trace_event.h" |
| +#include "base/trace_event/trace_event_argument.h" |
| +#include "base/trace_event/trace_event_synthetic_delay.h" |
| +#include "cc/debug/benchmark_instrumentation.h" |
| +#include "cc/debug/devtools_instrumentation.h" |
| +#include "cc/input/input_handler.h" |
| +#include "cc/input/top_controls_manager.h" |
| +#include "cc/output/context_provider.h" |
| +#include "cc/output/output_surface.h" |
| +#include "cc/output/swap_promise.h" |
| +#include "cc/quads/draw_quad.h" |
| +#include "cc/scheduler/commit_earlyout_reason.h" |
| +#include "cc/scheduler/compositor_timing_history.h" |
| +#include "cc/scheduler/scheduler.h" |
| +#include "cc/trees/blocking_task_runner.h" |
| +#include "cc/trees/layer_tree_host.h" |
| +#include "cc/trees/layer_tree_impl.h" |
| +#include "cc/trees/scoped_abort_remaining_swap_promises.h" |
| +#include "gpu/command_buffer/client/gles2_interface.h" |
| + |
| +namespace cc { |
| + |
| +scoped_ptr<ProxyMain> ProxyMain::Create( |
| + LayerTreeHost* layer_tree_host, |
| + scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, |
| + scoped_ptr<BeginFrameSource> external_begin_frame_source) { |
| + return make_scoped_ptr(new ProxyMain(layer_tree_host, main_task_runner, |
| + impl_task_runner, |
| + external_begin_frame_source.Pass())); |
| +} |
| + |
| +ProxyMain::ProxyMain( |
| + LayerTreeHost* layer_tree_host, |
| + scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, |
| + scoped_ptr<BeginFrameSource> external_begin_frame_source) |
| + : Proxy(main_task_runner, impl_task_runner), |
| + layer_tree_host_(layer_tree_host), |
| + layer_tree_host_id_(layer_tree_host->id()), |
| + max_requested_pipeline_stage_(NO_PIPELINE_STAGE), |
| + current_pipeline_stage_(NO_PIPELINE_STAGE), |
| + final_pipeline_stage_(NO_PIPELINE_STAGE), |
| + commit_waits_for_activation_(false), |
| + started_(false), |
| + prepare_tiles_pending_(false), |
| + defer_commits_(false), |
| + external_begin_frame_source_(external_begin_frame_source.Pass()), |
| + weak_factory_(this) { |
| + TRACE_EVENT0("cc", "ProxyMain::ProxyMain"); |
| + DCHECK(IsMainThread()); |
| + DCHECK(this->layer_tree_host_); |
| + main_thread_weak_ptr_ = weak_factory_.GetWeakPtr(); |
| +} |
| + |
| +ProxyMain::~ProxyMain() { |
| + TRACE_EVENT0("cc", "ProxyMain::~ProxyMain"); |
| + DCHECK(IsMainThread()); |
| + DCHECK(!started_); |
| + weak_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +void ProxyMain::SetChannel(scoped_ptr<ChannelMain> channel_main) { |
| + DCHECK(!channel_main_); |
| + channel_main_ = channel_main.Pass(); |
| +} |
| + |
| +void ProxyMain::FinishAllRendering() { |
| + DCHECK(Proxy::IsMainThread()); |
|
Wez
2015/11/10 01:41:51
nit: Why are some of these just IsMainThread, and
Khushal
2015/11/11 04:17:47
ThreadProxy implementation had that. Fixed.
|
| + DCHECK(!defer_commits_); |
| + |
| + // Make sure all GL drawing is finished on the impl thread. |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + CompletionEvent completion; |
| + channel_main_->FinishAllRenderingOnImpl(&completion); |
| + completion.Wait(); |
| +} |
| + |
| +bool ProxyMain::IsStarted() const { |
| + DCHECK(Proxy::IsMainThread()); |
| + return started_; |
| +} |
| + |
| +bool ProxyMain::CommitToActiveTree() const { |
| + // With ProxyMain, we use a pending tree and activate it once it's ready to |
| + // draw to allow input to modify the active tree and draw during raster. |
| + return false; |
| +} |
| + |
| +void ProxyMain::SetVisible(bool visible) { |
| + TRACE_EVENT1("cc", "ProxyMain::SetVisible", "visible", visible); |
| + channel_main_->SetVisibleOnImpl(visible); |
| +} |
| + |
| +void ProxyMain::SetThrottleFrameProduction(bool throttle) { |
| + TRACE_EVENT1("cc", "ProxyMain::SetThrottleFrameProduction", "throttle", |
| + throttle); |
| + channel_main_->SetThrottleFrameProductionOnImpl(throttle); |
| +} |
| + |
| +void ProxyMain::DidLoseOutputSurface() { |
| + TRACE_EVENT0("cc", "ProxyMain::DidLoseOutputSurface"); |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->DidLoseOutputSurface(); |
| +} |
| + |
| +void ProxyMain::RequestNewOutputSurface() { |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->RequestNewOutputSurface(); |
| +} |
| + |
| +void ProxyMain::SetOutputSurface(OutputSurface* output_surface) { |
| + channel_main_->InitializeOutputSurfaceOnImpl(output_surface); |
| +} |
| + |
| +void ProxyMain::ReleaseOutputSurface() { |
| + DCHECK(IsMainThread()); |
| + DCHECK(layer_tree_host_->output_surface_lost()); |
| + |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + CompletionEvent completion; |
| + channel_main_->ReleaseOutputSurfaceOnImpl(&completion); |
| + completion.Wait(); |
| +} |
| + |
| +void ProxyMain::DidInitializeOutputSurface( |
| + bool success, |
| + const RendererCapabilities& capabilities) { |
| + TRACE_EVENT0("cc", "ProxyMain::DidInitializeOutputSurface"); |
| + DCHECK(IsMainThread()); |
| + |
| + if (!success) { |
| + layer_tree_host_->DidFailToInitializeOutputSurface(); |
| + return; |
| + } |
| + renderer_capabilities_main_thread_copy_ = capabilities; |
| + layer_tree_host_->DidInitializeOutputSurface(); |
| +} |
| + |
| +void ProxyMain::SetRendererCapabilitiesMainCopy( |
| + const RendererCapabilities& capabilities) { |
| + renderer_capabilities_main_thread_copy_ = capabilities; |
| +} |
| + |
| +bool ProxyMain::SendCommitRequestToImplThreadIfNeeded( |
| + CommitPipelineStage required_stage) { |
| + DCHECK(IsMainThread()); |
| + DCHECK_NE(NO_PIPELINE_STAGE, required_stage); |
| + bool already_posted = max_requested_pipeline_stage_ != NO_PIPELINE_STAGE; |
| + max_requested_pipeline_stage_ = |
| + std::max(max_requested_pipeline_stage_, required_stage); |
| + if (already_posted) |
| + return false; |
| + channel_main_->SetNeedsCommitOnImpl(); |
| + return true; |
| +} |
| + |
| +void ProxyMain::DidCompletePageScaleAnimation() { |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->DidCompletePageScaleAnimation(); |
| +} |
| + |
| +const RendererCapabilities& ProxyMain::GetRendererCapabilities() const { |
| + DCHECK(IsMainThread()); |
| + DCHECK(!layer_tree_host_->output_surface_lost()); |
| + return renderer_capabilities_main_thread_copy_; |
| +} |
| + |
| +void ProxyMain::SetNeedsAnimate() { |
| + DCHECK(IsMainThread()); |
| + if (SendCommitRequestToImplThreadIfNeeded(ANIMATE_PIPELINE_STAGE)) { |
| + TRACE_EVENT_INSTANT0("cc", "ProxyMain::SetNeedsAnimate", |
| + TRACE_EVENT_SCOPE_THREAD); |
| + } |
| +} |
| + |
| +void ProxyMain::SetNeedsUpdateLayers() { |
| + DCHECK(IsMainThread()); |
| + // If we are currently animating, make sure we also update the layers. |
| + if (current_pipeline_stage_ == ANIMATE_PIPELINE_STAGE) { |
| + final_pipeline_stage_ = |
| + std::max(final_pipeline_stage_, UPDATE_LAYERS_PIPELINE_STAGE); |
| + return; |
| + } |
| + if (SendCommitRequestToImplThreadIfNeeded(UPDATE_LAYERS_PIPELINE_STAGE)) { |
| + TRACE_EVENT_INSTANT0("cc", "ProxyMain::SetNeedsUpdateLayers", |
| + TRACE_EVENT_SCOPE_THREAD); |
| + } |
| +} |
| + |
| +void ProxyMain::SetNeedsCommit() { |
| + DCHECK(IsMainThread()); |
| + // If we are currently animating, make sure we don't skip the commit. Note |
| + // that requesting a commit during the layer update stage means we need to |
| + // schedule another full commit. |
| + if (current_pipeline_stage_ == ANIMATE_PIPELINE_STAGE) { |
| + final_pipeline_stage_ = |
| + std::max(final_pipeline_stage_, COMMIT_PIPELINE_STAGE); |
| + return; |
| + } |
| + if (SendCommitRequestToImplThreadIfNeeded(COMMIT_PIPELINE_STAGE)) { |
| + TRACE_EVENT_INSTANT0("cc", "ProxyMain::SetNeedsCommit", |
| + TRACE_EVENT_SCOPE_THREAD); |
| + } |
| +} |
| + |
| +void ProxyMain::SetNeedsRedraw(const gfx::Rect& damage_rect) { |
| + TRACE_EVENT0("cc", "ProxyMain::SetNeedsRedraw"); |
| + DCHECK(IsMainThread()); |
| + channel_main_->SetNeedsRedrawOnImpl(damage_rect); |
| +} |
| + |
| +void ProxyMain::SetNextCommitWaitsForActivation() { |
| + DCHECK(IsMainThread()); |
| + commit_waits_for_activation_ = true; |
| +} |
| + |
| +void ProxyMain::SetDeferCommits(bool defer_commits) { |
| + DCHECK(IsMainThread()); |
| + if (defer_commits_ == defer_commits) |
| + return; |
| + |
| + defer_commits_ = defer_commits; |
| + if (defer_commits_) |
| + TRACE_EVENT_ASYNC_BEGIN0("cc", "ProxyMain::SetDeferCommits", this); |
| + else |
| + TRACE_EVENT_ASYNC_END0("cc", "ProxyMain::SetDeferCommits", this); |
| + |
| + channel_main_->SetDeferCommitsOnImpl(defer_commits); |
| +} |
| + |
| +bool ProxyMain::CommitRequested() const { |
| + DCHECK(IsMainThread()); |
| + // TODO(skyostil): Split this into something like CommitRequested() and |
| + // CommitInProgress(). |
| + return current_pipeline_stage_ != NO_PIPELINE_STAGE || |
| + max_requested_pipeline_stage_ >= COMMIT_PIPELINE_STAGE; |
| +} |
| + |
| +bool ProxyMain::BeginMainFrameRequested() const { |
| + DCHECK(IsMainThread()); |
| + return max_requested_pipeline_stage_ != NO_PIPELINE_STAGE; |
| +} |
| + |
| +void ProxyMain::MainThreadHasStoppedFlinging() { |
| + DCHECK(IsMainThread()); |
| + channel_main_->MainThreadHasStoppedFlingingOnImpl(); |
| +} |
| + |
| +void ProxyMain::NotifyInputThrottledUntilCommit() { |
| + DCHECK(IsMainThread()); |
| + channel_main_->SetInputThrottledUntilCommitOnImpl(true); |
| +} |
| + |
| +void ProxyMain::Start() { |
| + DCHECK(IsMainThread()); |
| + DCHECK(Proxy::HasImplThread()); |
| + DCHECK(channel_main_); |
| + |
| + // Create LayerTreeHostImpl. |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + CompletionEvent completion; |
| + channel_main_->InitializeImpl(&completion, layer_tree_host_, |
| + external_begin_frame_source_.Pass()); |
| + completion.Wait(); |
| + |
| + started_ = true; |
| +} |
| + |
| +void ProxyMain::Stop() { |
| + TRACE_EVENT0("cc", "ProxyMain::Stop"); |
| + DCHECK(IsMainThread()); |
| + DCHECK(started_); |
| + |
| + // Synchronously finishes pending GL operations and deletes the impl. |
| + // The two steps are done as separate post tasks, so that tasks posted |
| + // by the GL implementation due to the Finish can be executed by the |
| + // renderer before shutting it down. |
| + { |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + CompletionEvent completion; |
| + channel_main_->FinishGLOnImpl(&completion); |
| + completion.Wait(); |
| + } |
| + { |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + |
| + CompletionEvent completion; |
| + channel_main_->CloseImpl(&completion); |
| + completion.Wait(); |
| + } |
| + |
| + layer_tree_host_ = nullptr; |
| + started_ = false; |
| +} |
| + |
| +bool ProxyMain::SupportsImplScrolling() const { |
| + return true; |
| +} |
| + |
| +void ProxyMain::BeginMainFrame( |
| + scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) { |
| + benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task( |
| + benchmark_instrumentation::kDoBeginFrame, |
| + begin_main_frame_state->begin_frame_id); |
| + TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame"); |
| + DCHECK(IsMainThread()); |
| + DCHECK_EQ(NO_PIPELINE_STAGE, current_pipeline_stage_); |
| + |
| + if (defer_commits_) { |
| + TRACE_EVENT_INSTANT0("cc", "EarlyOut_DeferCommit", |
| + TRACE_EVENT_SCOPE_THREAD); |
| + channel_main_->BeginMainFrameAbortedOnImpl( |
| + CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT); |
| + return; |
| + } |
| + |
| + // If the commit finishes, LayerTreeHost will transfer its swap promises to |
| + // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the |
| + // remaining swap promises. |
| + ScopedAbortRemainingSwapPromises swap_promise_checker(layer_tree_host_); |
| + |
| + final_pipeline_stage_ = max_requested_pipeline_stage_; |
| + max_requested_pipeline_stage_ = NO_PIPELINE_STAGE; |
| + |
| + if (!layer_tree_host_->visible()) { |
| + TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD); |
| + channel_main_->BeginMainFrameAbortedOnImpl( |
| + CommitEarlyOutReason::ABORTED_NOT_VISIBLE); |
| + return; |
| + } |
| + |
| + if (layer_tree_host_->output_surface_lost()) { |
| + TRACE_EVENT_INSTANT0("cc", "EarlyOut_OutputSurfaceLost", |
| + TRACE_EVENT_SCOPE_THREAD); |
| + channel_main_->BeginMainFrameAbortedOnImpl( |
| + CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST); |
| + return; |
| + } |
| + |
| + current_pipeline_stage_ = ANIMATE_PIPELINE_STAGE; |
| + |
| + layer_tree_host_->ApplyScrollAndScale( |
| + begin_main_frame_state->scroll_info.get()); |
| + |
| + layer_tree_host_->WillBeginMainFrame(); |
| + |
| + layer_tree_host_->BeginMainFrame(begin_main_frame_state->begin_frame_args); |
| + layer_tree_host_->AnimateLayers( |
| + begin_main_frame_state->begin_frame_args.frame_time); |
| + |
| + // Recreate all UI resources if there were evicted UI resources when the impl |
| + // thread initiated the commit. |
| + if (begin_main_frame_state->evicted_ui_resources) |
| + layer_tree_host_->RecreateUIResources(); |
| + |
| + layer_tree_host_->RequestMainFrameUpdate(); |
| + TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame"); |
| + |
| + bool can_cancel_this_commit = final_pipeline_stage_ < COMMIT_PIPELINE_STAGE && |
| + !begin_main_frame_state->evicted_ui_resources; |
| + |
| + current_pipeline_stage_ = UPDATE_LAYERS_PIPELINE_STAGE; |
| + bool should_update_layers = |
| + final_pipeline_stage_ >= UPDATE_LAYERS_PIPELINE_STAGE; |
| + bool updated = should_update_layers && layer_tree_host_->UpdateLayers(); |
| + |
| + layer_tree_host_->WillCommit(); |
| + devtools_instrumentation::ScopedCommitTrace commit_task( |
| + layer_tree_host_->id()); |
| + |
| + current_pipeline_stage_ = COMMIT_PIPELINE_STAGE; |
| + if (!updated && can_cancel_this_commit) { |
| + TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD); |
| + channel_main_->BeginMainFrameAbortedOnImpl( |
| + CommitEarlyOutReason::FINISHED_NO_UPDATES); |
| + |
| + // Although the commit is internally aborted, this is because it has been |
| + // detected to be a no-op. From the perspective of an embedder, this commit |
| + // went through, and input should no longer be throttled, etc. |
| + current_pipeline_stage_ = NO_PIPELINE_STAGE; |
| + layer_tree_host_->CommitComplete(); |
| + layer_tree_host_->DidBeginMainFrame(); |
| + layer_tree_host_->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE); |
| + return; |
| + } |
| + |
| + // Notify the impl thread that the main thread is ready to commit. This will |
| + // begin the commit process, which is blocking from the main thread's |
| + // point of view, but asynchronously performed on the impl thread, |
| + // coordinated by the Scheduler. |
| + { |
| + TRACE_EVENT0("cc", "ProxyMain::BeginMainFrame::commit"); |
| + |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + |
| + // This CapturePostTasks should be destroyed before CommitComplete() is |
| + // called since that goes out to the embedder, and we want the embedder |
| + // to receive its callbacks before that. |
| + BlockingTaskRunner::CapturePostTasks blocked( |
| + blocking_main_thread_task_runner()); |
| + |
| + bool hold_commit_for_activation = commit_waits_for_activation_; |
| + commit_waits_for_activation_ = false; |
| + CompletionEvent completion; |
| + channel_main_->StartCommitOnImpl(&completion, layer_tree_host_, |
| + hold_commit_for_activation); |
| + completion.Wait(); |
| + } |
| + |
| + current_pipeline_stage_ = NO_PIPELINE_STAGE; |
| + layer_tree_host_->CommitComplete(); |
| + layer_tree_host_->DidBeginMainFrame(); |
| +} |
| + |
| +void ProxyMain::BeginMainFrameNotExpectedSoon() { |
| + TRACE_EVENT0("cc", "ProxyMain::BeginMainFrameNotExpectedSoon"); |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->BeginMainFrameNotExpectedSoon(); |
| +} |
| + |
| +void ProxyMain::SetAuthoritativeVSyncInterval(const base::TimeDelta& interval) { |
| + NOTREACHED() << "Only used by SingleProxyMain"; |
| +} |
| + |
| +void ProxyMain::DidCommitAndDrawFrame() { |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->DidCommitAndDrawFrame(); |
| +} |
| + |
| +void ProxyMain::DidCompleteSwapBuffers() { |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->DidCompleteSwapBuffers(); |
| +} |
| + |
| +void ProxyMain::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) { |
| + TRACE_EVENT0("cc", "ProxyMain::SetAnimationEvents"); |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->SetAnimationEvents(events.Pass()); |
| +} |
| + |
| +bool ProxyMain::MainFrameWillHappenForTesting() { |
| + DCHECK(IsMainThread()); |
| + bool main_frame_will_happen = false; |
| + { |
| + DebugScopedSetMainThreadBlocked main_thread_blocked(this); |
| + CompletionEvent completion; |
| + channel_main_->MainFrameWillHappenOnImplForTesting(&completion, |
| + &main_frame_will_happen); |
| + completion.Wait(); |
| + } |
| + return main_frame_will_happen; |
| +} |
| + |
| +void ProxyMain::SetChildrenNeedBeginFrames(bool children_need_begin_frames) { |
| + NOTREACHED() << "Only used by SingleThreadProxy"; |
| +} |
| + |
| +void ProxyMain::UpdateTopControlsState(TopControlsState constraints, |
| + TopControlsState current, |
| + bool animate) { |
| + channel_main_->UpdateTopControlsStateOnImpl(constraints, current, animate); |
| +} |
| + |
| +void ProxyMain::PostFrameTimingEventsOnMain( |
| + scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, |
| + scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) { |
| + DCHECK(IsMainThread()); |
| + layer_tree_host_->RecordFrameTimingEvents(composite_events.Pass(), |
| + main_frame_events.Pass()); |
| +} |
| + |
| +base::WeakPtr<ProxyMain> ProxyMain::GetWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| +} |
| + |
| +} // namespace cc |