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 #ifndef CC_TREES_SINGLE_THREAD_PROXY_H_
6 #define CC_TREES_SINGLE_THREAD_PROXY_H_
10 #include "base/cancelable_callback.h"
11 #include "base/time/time.h"
12 #include "cc/animation/animation_events.h"
13 #include "cc/output/begin_frame_args.h"
14 #include "cc/scheduler/scheduler.h"
15 #include "cc/trees/blocking_task_runner.h"
16 #include "cc/trees/layer_tree_host_impl.h"
17 #include "cc/trees/proxy.h"
18 #include "cc/trees/proxy_timing_history.h"
22 class ContextProvider;
24 class LayerTreeHostSingleThreadClient;
26 class CC_EXPORT SingleThreadProxy : public Proxy,
27 NON_EXPORTED_BASE(LayerTreeHostImplClient),
30 static scoped_ptr<Proxy> Create(
31 LayerTreeHost* layer_tree_host,
32 LayerTreeHostSingleThreadClient* client,
33 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
34 ~SingleThreadProxy() override;
36 // Proxy implementation
37 void FinishAllRendering() override;
38 bool IsStarted() const override;
39 void SetOutputSurface(scoped_ptr<OutputSurface>) override;
40 void SetLayerTreeHostClientReady() override;
41 void SetVisible(bool visible) override;
42 const RendererCapabilities& GetRendererCapabilities() const override;
43 void SetNeedsAnimate() override;
44 void SetNeedsUpdateLayers() override;
45 void SetNeedsCommit() override;
46 void SetNeedsRedraw(const gfx::Rect& damage_rect) override;
47 void SetNextCommitWaitsForActivation() override;
48 void NotifyInputThrottledUntilCommit() override {}
49 void SetDeferCommits(bool defer_commits) override;
50 bool CommitRequested() const override;
51 bool BeginMainFrameRequested() const override;
52 void MainThreadHasStoppedFlinging() override {}
53 void Start() override;
55 size_t MaxPartialTextureUpdates() const override;
56 void ForceSerializeOnSwapBuffers() override;
57 bool SupportsImplScrolling() const override;
58 void AsValueInto(base::debug::TracedValue* state) const override;
59 bool MainFrameWillHappenForTesting() override;
61 // SchedulerClient implementation
62 BeginFrameSource* ExternalBeginFrameSource() override;
63 void WillBeginImplFrame(const BeginFrameArgs& args) override;
64 void ScheduledActionSendBeginMainFrame() override;
65 DrawResult ScheduledActionDrawAndSwapIfPossible() override;
66 DrawResult ScheduledActionDrawAndSwapForced() override;
67 void ScheduledActionCommit() override;
68 void ScheduledActionAnimate() override;
69 void ScheduledActionUpdateVisibleTiles() override;
70 void ScheduledActionActivateSyncTree() override;
71 void ScheduledActionBeginOutputSurfaceCreation() override;
72 void ScheduledActionManageTiles() override;
73 void DidAnticipatedDrawTimeChange(base::TimeTicks time) override;
74 base::TimeDelta DrawDurationEstimate() override;
75 base::TimeDelta BeginMainFrameToCommitDurationEstimate() override;
76 base::TimeDelta CommitToActivateDurationEstimate() override;
77 void DidBeginImplFrameDeadline() override;
79 // LayerTreeHostImplClient implementation
80 void UpdateRendererCapabilitiesOnImplThread() override;
81 void DidLoseOutputSurfaceOnImplThread() override;
82 void CommitVSyncParameters(base::TimeTicks timebase,
83 base::TimeDelta interval) override {}
84 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override {}
85 void SetMaxSwapsPendingOnImplThread(int max) override {}
86 void DidSwapBuffersOnImplThread() override;
87 void DidSwapBuffersCompleteOnImplThread() override;
88 void OnCanDrawStateChanged(bool can_draw) override;
89 void NotifyReadyToActivate() override;
90 void SetNeedsRedrawOnImplThread() override;
91 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect) override;
92 void SetNeedsAnimateOnImplThread() override;
93 void SetNeedsManageTilesOnImplThread() override;
94 void DidInitializeVisibleTileOnImplThread() override;
95 void SetNeedsCommitOnImplThread() override;
96 void PostAnimationEventsToMainThreadOnImplThread(
97 scoped_ptr<AnimationEventsVector> events) override;
98 bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
99 int priority_cutoff) override;
100 bool IsInsideDraw() override;
101 void RenewTreePriority() override {}
102 void PostDelayedScrollbarFadeOnImplThread(const base::Closure& start_fade,
103 base::TimeDelta delay) override {}
104 void DidActivateSyncTree() override;
105 void DidManageTiles() override;
106 void SetDebugState(const LayerTreeDebugState& debug_state) override {}
108 void RequestNewOutputSurface();
110 // Called by the legacy path where RenderWidget does the scheduling.
111 void CompositeImmediately(base::TimeTicks frame_begin_time);
115 LayerTreeHost* layer_tree_host,
116 LayerTreeHostSingleThreadClient* client,
117 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
119 void BeginMainFrame();
120 void BeginMainFrameAbortedOnImplThread();
121 void DoBeginMainFrame(const BeginFrameArgs& begin_frame_args);
123 DrawResult DoComposite(base::TimeTicks frame_begin_time,
124 LayerTreeHostImpl::FrameData* frame);
126 void DidCommitAndDrawFrame();
127 void CommitComplete();
129 bool ShouldComposite() const;
130 void UpdateBackgroundAnimateTicking();
131 void ScheduleRequestNewOutputSurface();
133 // Accessed on main thread only.
134 LayerTreeHost* layer_tree_host_;
135 LayerTreeHostSingleThreadClient* client_;
137 // Used on the Thread, but checked on main thread during
138 // initialization/shutdown.
139 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_;
140 RendererCapabilities renderer_capabilities_for_main_thread_;
142 // Accessed from both threads.
143 scoped_ptr<Scheduler> scheduler_on_impl_thread_;
144 ProxyTimingHistory timing_history_;
146 scoped_ptr<BlockingTaskRunner::CapturePostTasks> commit_blocking_task_runner_;
147 scoped_ptr<ResourceUpdateQueue> queue_for_commit_;
148 bool next_frame_is_newly_committed_frame_;
152 bool commit_was_deferred_;
153 bool commit_requested_;
154 bool inside_synchronous_composite_;
156 // True if a request to the LayerTreeHostClient to create an output surface
157 // is still outstanding.
158 bool output_surface_creation_requested_;
160 // This is the callback for the scheduled RequestNewOutputSurface.
161 base::CancelableClosure output_surface_creation_callback_;
163 base::WeakPtrFactory<SingleThreadProxy> weak_factory_;
165 DISALLOW_COPY_AND_ASSIGN(SingleThreadProxy);
168 // For use in the single-threaded case. In debug builds, it pretends that the
169 // code is running on the impl thread to satisfy assertion checks.
170 class DebugScopedSetImplThread {
172 explicit DebugScopedSetImplThread(Proxy* proxy) : proxy_(proxy) {
174 previous_value_ = proxy_->impl_thread_is_overridden_;
175 proxy_->SetCurrentThreadIsImplThread(true);
178 ~DebugScopedSetImplThread() {
180 proxy_->SetCurrentThreadIsImplThread(previous_value_);
185 bool previous_value_;
188 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThread);
191 // For use in the single-threaded case. In debug builds, it pretends that the
192 // code is running on the main thread to satisfy assertion checks.
193 class DebugScopedSetMainThread {
195 explicit DebugScopedSetMainThread(Proxy* proxy) : proxy_(proxy) {
197 previous_value_ = proxy_->impl_thread_is_overridden_;
198 proxy_->SetCurrentThreadIsImplThread(false);
201 ~DebugScopedSetMainThread() {
203 proxy_->SetCurrentThreadIsImplThread(previous_value_);
208 bool previous_value_;
211 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetMainThread);
214 // For use in the single-threaded case. In debug builds, it pretends that the
215 // code is running on the impl thread and that the main thread is blocked to
216 // satisfy assertion checks
217 class DebugScopedSetImplThreadAndMainThreadBlocked {
219 explicit DebugScopedSetImplThreadAndMainThreadBlocked(Proxy* proxy)
220 : impl_thread_(proxy), main_thread_blocked_(proxy) {}
223 DebugScopedSetImplThread impl_thread_;
224 DebugScopedSetMainThreadBlocked main_thread_blocked_;
226 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThreadAndMainThreadBlocked);
231 #endif // CC_TREES_SINGLE_THREAD_PROXY_H_