Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / cc / trees / single_thread_proxy.h
blob47c556d60b18390adfcd047efe933055cd6200fa
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_
8 #include <limits>
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"
20 namespace cc {
22 class BeginFrameSource;
23 class ContextProvider;
24 class LayerTreeHost;
25 class LayerTreeHostSingleThreadClient;
26 class ResourceUpdateQueue;
28 class CC_EXPORT SingleThreadProxy : public Proxy,
29 NON_EXPORTED_BASE(LayerTreeHostImplClient),
30 SchedulerClient {
31 public:
32 static scoped_ptr<Proxy> Create(
33 LayerTreeHost* layer_tree_host,
34 LayerTreeHostSingleThreadClient* client,
35 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
36 scoped_ptr<BeginFrameSource> external_begin_frame_source);
37 ~SingleThreadProxy() override;
39 // Proxy implementation
40 void FinishAllRendering() override;
41 bool IsStarted() const override;
42 bool CommitToActiveTree() const override;
43 void SetOutputSurface(scoped_ptr<OutputSurface>) override;
44 void SetLayerTreeHostClientReady() override;
45 void SetVisible(bool visible) override;
46 void SetThrottleFrameProduction(bool throttle) override;
47 const RendererCapabilities& GetRendererCapabilities() const override;
48 void SetNeedsAnimate() override;
49 void SetNeedsUpdateLayers() override;
50 void SetNeedsCommit() override;
51 void SetNeedsRedraw(const gfx::Rect& damage_rect) override;
52 void SetNextCommitWaitsForActivation() override;
53 void NotifyInputThrottledUntilCommit() override {}
54 void SetDeferCommits(bool defer_commits) override;
55 bool CommitRequested() const override;
56 bool BeginMainFrameRequested() const override;
57 void MainThreadHasStoppedFlinging() override {}
58 void Start() override;
59 void Stop() override;
60 size_t MaxPartialTextureUpdates() const override;
61 void ForceSerializeOnSwapBuffers() override;
62 bool SupportsImplScrolling() const override;
63 bool MainFrameWillHappenForTesting() override;
64 void SetChildrenNeedBeginFrames(bool children_need_begin_frames) override;
65 void SetAuthoritativeVSyncInterval(const base::TimeDelta& interval) override;
67 // SchedulerClient implementation
68 void WillBeginImplFrame(const BeginFrameArgs& args) override;
69 void ScheduledActionSendBeginMainFrame() override;
70 DrawResult ScheduledActionDrawAndSwapIfPossible() override;
71 DrawResult ScheduledActionDrawAndSwapForced() override;
72 void ScheduledActionCommit() override;
73 void ScheduledActionAnimate() override;
74 void ScheduledActionActivateSyncTree() override;
75 void ScheduledActionBeginOutputSurfaceCreation() override;
76 void ScheduledActionPrepareTiles() override;
77 void ScheduledActionInvalidateOutputSurface() override;
78 void DidAnticipatedDrawTimeChange(base::TimeTicks time) override;
79 base::TimeDelta DrawDurationEstimate() override;
80 base::TimeDelta BeginMainFrameToCommitDurationEstimate() override;
81 base::TimeDelta CommitToActivateDurationEstimate() override;
82 void DidBeginImplFrameDeadline() override;
83 void SendBeginFramesToChildren(const BeginFrameArgs& args) override;
84 void SendBeginMainFrameNotExpectedSoon() override;
86 // LayerTreeHostImplClient implementation
87 void UpdateRendererCapabilitiesOnImplThread() override;
88 void DidLoseOutputSurfaceOnImplThread() override;
89 void CommitVSyncParameters(base::TimeTicks timebase,
90 base::TimeDelta interval) override;
91 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override;
92 void SetMaxSwapsPendingOnImplThread(int max) override;
93 void DidSwapBuffersOnImplThread() override;
94 void DidSwapBuffersCompleteOnImplThread() override;
95 void OnCanDrawStateChanged(bool can_draw) override;
96 void NotifyReadyToActivate() override;
97 void NotifyReadyToDraw() override;
98 void SetNeedsRedrawOnImplThread() override;
99 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect) override;
100 void SetNeedsAnimateOnImplThread() override;
101 void SetNeedsPrepareTilesOnImplThread() override;
102 void SetNeedsCommitOnImplThread() override;
103 void PostAnimationEventsToMainThreadOnImplThread(
104 scoped_ptr<AnimationEventsVector> events) override;
105 bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
106 int priority_cutoff) override;
107 bool IsInsideDraw() override;
108 void RenewTreePriority() override {}
109 void PostDelayedAnimationTaskOnImplThread(const base::Closure& task,
110 base::TimeDelta delay) override {}
111 void DidActivateSyncTree() override;
112 void DidPrepareTiles() override;
113 void DidCompletePageScaleAnimationOnImplThread() override;
114 void OnDrawForOutputSurface() override;
116 void SetDebugState(const LayerTreeDebugState& debug_state) override {}
118 void RequestNewOutputSurface();
120 // Called by the legacy path where RenderWidget does the scheduling.
121 void CompositeImmediately(base::TimeTicks frame_begin_time);
123 protected:
124 SingleThreadProxy(
125 LayerTreeHost* layer_tree_host,
126 LayerTreeHostSingleThreadClient* client,
127 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
128 scoped_ptr<BeginFrameSource> external_begin_frame_source);
130 private:
131 void BeginMainFrame();
132 void BeginMainFrameAbortedOnImplThread(CommitEarlyOutReason reason);
133 void DoAnimate();
134 void DoBeginMainFrame(const BeginFrameArgs& begin_frame_args);
135 void DoCommit();
136 DrawResult DoComposite(base::TimeTicks frame_begin_time,
137 LayerTreeHostImpl::FrameData* frame);
138 void DoSwap();
139 void DidCommitAndDrawFrame();
140 void CommitComplete();
142 bool ShouldComposite() const;
143 void ScheduleRequestNewOutputSurface();
145 // Accessed on main thread only.
146 LayerTreeHost* layer_tree_host_;
147 LayerTreeHostSingleThreadClient* client_;
149 // Used on the Thread, but checked on main thread during
150 // initialization/shutdown.
151 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_;
152 RendererCapabilities renderer_capabilities_for_main_thread_;
154 // Accessed from both threads.
155 scoped_ptr<Scheduler> scheduler_on_impl_thread_;
156 ProxyTimingHistory timing_history_;
158 scoped_ptr<BlockingTaskRunner::CapturePostTasks> commit_blocking_task_runner_;
159 scoped_ptr<ResourceUpdateQueue> queue_for_commit_;
160 bool next_frame_is_newly_committed_frame_;
162 bool inside_draw_;
163 bool defer_commits_;
164 bool commit_requested_;
165 bool inside_synchronous_composite_;
167 // True if a request to the LayerTreeHostClient to create an output surface
168 // is still outstanding.
169 bool output_surface_creation_requested_;
171 // This is the callback for the scheduled RequestNewOutputSurface.
172 base::CancelableClosure output_surface_creation_callback_;
174 scoped_ptr<BeginFrameSource> external_begin_frame_source_;
176 base::WeakPtrFactory<SingleThreadProxy> weak_factory_;
178 DISALLOW_COPY_AND_ASSIGN(SingleThreadProxy);
181 // For use in the single-threaded case. In debug builds, it pretends that the
182 // code is running on the impl thread to satisfy assertion checks.
183 class DebugScopedSetImplThread {
184 public:
185 explicit DebugScopedSetImplThread(Proxy* proxy) : proxy_(proxy) {
186 #if DCHECK_IS_ON()
187 previous_value_ = proxy_->impl_thread_is_overridden_;
188 proxy_->SetCurrentThreadIsImplThread(true);
189 #endif
191 ~DebugScopedSetImplThread() {
192 #if DCHECK_IS_ON()
193 proxy_->SetCurrentThreadIsImplThread(previous_value_);
194 #endif
197 private:
198 bool previous_value_;
199 Proxy* proxy_;
201 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThread);
204 // For use in the single-threaded case. In debug builds, it pretends that the
205 // code is running on the main thread to satisfy assertion checks.
206 class DebugScopedSetMainThread {
207 public:
208 explicit DebugScopedSetMainThread(Proxy* proxy) : proxy_(proxy) {
209 #if DCHECK_IS_ON()
210 previous_value_ = proxy_->impl_thread_is_overridden_;
211 proxy_->SetCurrentThreadIsImplThread(false);
212 #endif
214 ~DebugScopedSetMainThread() {
215 #if DCHECK_IS_ON()
216 proxy_->SetCurrentThreadIsImplThread(previous_value_);
217 #endif
220 private:
221 bool previous_value_;
222 Proxy* proxy_;
224 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetMainThread);
227 // For use in the single-threaded case. In debug builds, it pretends that the
228 // code is running on the impl thread and that the main thread is blocked to
229 // satisfy assertion checks
230 class DebugScopedSetImplThreadAndMainThreadBlocked {
231 public:
232 explicit DebugScopedSetImplThreadAndMainThreadBlocked(Proxy* proxy)
233 : impl_thread_(proxy), main_thread_blocked_(proxy) {}
235 private:
236 DebugScopedSetImplThread impl_thread_;
237 DebugScopedSetMainThreadBlocked main_thread_blocked_;
239 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThreadAndMainThreadBlocked);
242 } // namespace cc
244 #endif // CC_TREES_SINGLE_THREAD_PROXY_H_