OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CC_TREES_THREAD_PROXY_H_ | |
6 #define CC_TREES_THREAD_PROXY_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/time/time.h" | |
13 #include "cc/animation/animation_events.h" | |
14 #include "cc/base/completion_event.h" | |
15 #include "cc/base/delayed_unique_notifier.h" | |
16 #include "cc/scheduler/commit_earlyout_reason.h" | |
17 #include "cc/scheduler/scheduler.h" | |
18 #include "cc/trees/layer_tree_host_impl.h" | |
19 #include "cc/trees/proxy.h" | |
20 #include "cc/trees/threaded_channel.h" | |
21 | |
22 namespace base { | |
23 class SingleThreadTaskRunner; | |
24 } | |
25 | |
26 namespace cc { | |
27 | |
28 class BeginFrameSource; | |
29 class ChannelImpl; | |
30 class ChannelMain; | |
31 class ContextProvider; | |
32 class InputHandlerClient; | |
33 class LayerTreeHost; | |
34 class ProxyImpl; | |
35 class ProxyMain; | |
36 class Scheduler; | |
37 class ScopedThreadProxy; | |
38 class ThreadedChannel; | |
39 | |
40 class CC_EXPORT ThreadProxy : public Proxy, | |
41 public ProxyMain, | |
42 public ProxyImpl, | |
43 NON_EXPORTED_BASE(LayerTreeHostImplClient), | |
44 NON_EXPORTED_BASE(SchedulerClient) { | |
45 public: | |
46 static scoped_ptr<Proxy> Create( | |
47 LayerTreeHost* layer_tree_host, | |
48 TaskRunnerProvider* task_runner_provider, | |
49 scoped_ptr<BeginFrameSource> external_begin_frame_source); | |
50 | |
51 ~ThreadProxy() override; | |
52 | |
53 // Commits between the main and impl threads are processed through a pipeline | |
54 // with the following stages. For efficiency we can early out at any stage if | |
55 // we decide that no further processing is necessary. | |
56 enum CommitPipelineStage { | |
57 NO_PIPELINE_STAGE, | |
58 ANIMATE_PIPELINE_STAGE, | |
59 UPDATE_LAYERS_PIPELINE_STAGE, | |
60 COMMIT_PIPELINE_STAGE, | |
61 }; | |
62 | |
63 struct MainThreadOnly { | |
64 MainThreadOnly(ThreadProxy* proxy, LayerTreeHost* layer_tree_host); | |
65 ~MainThreadOnly(); | |
66 | |
67 const int layer_tree_host_id; | |
68 | |
69 LayerTreeHost* layer_tree_host; | |
70 | |
71 // The furthest pipeline stage which has been requested for the next | |
72 // commit. | |
73 CommitPipelineStage max_requested_pipeline_stage; | |
74 // The commit pipeline stage that is currently being processed. | |
75 CommitPipelineStage current_pipeline_stage; | |
76 // The commit pipeline stage at which processing for the current commit | |
77 // will stop. Only valid while we are executing the pipeline (i.e., | |
78 // |current_pipeline_stage| is set to a pipeline stage). | |
79 CommitPipelineStage final_pipeline_stage; | |
80 | |
81 bool commit_waits_for_activation; | |
82 | |
83 bool started; | |
84 bool prepare_tiles_pending; | |
85 bool defer_commits; | |
86 | |
87 RendererCapabilities renderer_capabilities_main_thread_copy; | |
88 | |
89 // TODO(khushalsagar): Make this scoped_ptr<ChannelMain> when ProxyMain | |
90 // and ProxyImpl are split. | |
91 ChannelMain* channel_main; | |
92 | |
93 base::WeakPtrFactory<ThreadProxy> weak_factory; | |
94 }; | |
95 | |
96 // Accessed on the impl thread when the main thread is blocked for a commit. | |
97 struct BlockedMainCommitOnly { | |
98 BlockedMainCommitOnly(); | |
99 ~BlockedMainCommitOnly(); | |
100 LayerTreeHost* layer_tree_host; | |
101 }; | |
102 | |
103 struct CompositorThreadOnly { | |
104 CompositorThreadOnly( | |
105 ThreadProxy* proxy, | |
106 int layer_tree_host_id, | |
107 RenderingStatsInstrumentation* rendering_stats_instrumentation, | |
108 scoped_ptr<BeginFrameSource> external_begin_frame_source); | |
109 ~CompositorThreadOnly(); | |
110 | |
111 const int layer_tree_host_id; | |
112 | |
113 scoped_ptr<Scheduler> scheduler; | |
114 | |
115 // Set when the main thread is waiting on a pending tree activation. | |
116 bool next_commit_waits_for_activation; | |
117 | |
118 // Set when the main thread is waiting on a commit to complete or on a | |
119 // pending tree activation. | |
120 CompletionEvent* commit_completion_event; | |
121 | |
122 // Set when the next draw should post DidCommitAndDrawFrame to the main | |
123 // thread. | |
124 bool next_frame_is_newly_committed_frame; | |
125 | |
126 bool inside_draw; | |
127 | |
128 bool input_throttled_until_commit; | |
129 | |
130 // Whether a commit has been completed since the last time animations were | |
131 // ticked. If this happens, we need to animate again. | |
132 bool did_commit_after_animating; | |
133 | |
134 DelayedUniqueNotifier smoothness_priority_expiration_notifier; | |
135 | |
136 scoped_ptr<BeginFrameSource> external_begin_frame_source; | |
137 | |
138 RenderingStatsInstrumentation* rendering_stats_instrumentation; | |
139 | |
140 // Values used to keep track of frame durations. Used only in frame timing. | |
141 BeginFrameArgs last_begin_main_frame_args; | |
142 BeginFrameArgs last_processed_begin_main_frame_args; | |
143 | |
144 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl; | |
145 | |
146 ChannelImpl* channel_impl; | |
147 | |
148 base::WeakPtrFactory<ThreadProxy> weak_factory; | |
149 }; | |
150 | |
151 const MainThreadOnly& main() const; | |
152 const CompositorThreadOnly& impl() const; | |
153 TaskRunnerProvider* task_runner_provider() { return task_runner_provider_; } | |
154 | |
155 // Proxy implementation | |
156 void FinishAllRendering() override; | |
157 bool IsStarted() const override; | |
158 bool CommitToActiveTree() const override; | |
159 void SetOutputSurface(OutputSurface* output_surface) override; | |
160 void SetVisible(bool visible) override; | |
161 void SetThrottleFrameProduction(bool throttle) override; | |
162 const RendererCapabilities& GetRendererCapabilities() const override; | |
163 void SetNeedsAnimate() override; | |
164 void SetNeedsUpdateLayers() override; | |
165 void SetNeedsCommit() override; | |
166 void SetNeedsRedraw(const gfx::Rect& damage_rect) override; | |
167 void SetNextCommitWaitsForActivation() override; | |
168 void NotifyInputThrottledUntilCommit() override; | |
169 void SetDeferCommits(bool defer_commits) override; | |
170 bool CommitRequested() const override; | |
171 bool BeginMainFrameRequested() const override; | |
172 void MainThreadHasStoppedFlinging() override; | |
173 void Start() override; | |
174 void Stop() override; | |
175 bool SupportsImplScrolling() const override; | |
176 bool MainFrameWillHappenForTesting() override; | |
177 void SetChildrenNeedBeginFrames(bool children_need_begin_frames) override; | |
178 void SetAuthoritativeVSyncInterval(const base::TimeDelta& interval) override; | |
179 void ReleaseOutputSurface() override; | |
180 void UpdateTopControlsState(TopControlsState constraints, | |
181 TopControlsState current, | |
182 bool animate) override; | |
183 | |
184 // LayerTreeHostImplClient implementation | |
185 void UpdateRendererCapabilitiesOnImplThread() override; | |
186 void DidLoseOutputSurfaceOnImplThread() override; | |
187 void CommitVSyncParameters(base::TimeTicks timebase, | |
188 base::TimeDelta interval) override; | |
189 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override; | |
190 void DidSwapBuffersOnImplThread() override; | |
191 void DidSwapBuffersCompleteOnImplThread() override; | |
192 void OnResourcelessSoftareDrawStateChanged(bool resourceless_draw) override; | |
193 void OnCanDrawStateChanged(bool can_draw) override; | |
194 void NotifyReadyToActivate() override; | |
195 void NotifyReadyToDraw() override; | |
196 // Please call these 3 functions through | |
197 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and | |
198 // SetNeedsOneBeginImplFrame(). | |
199 void SetNeedsRedrawOnImplThread() override; | |
200 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect) override; | |
201 void SetNeedsOneBeginImplFrameOnImplThread() override; | |
202 void SetNeedsPrepareTilesOnImplThread() override; | |
203 void SetNeedsCommitOnImplThread() override; | |
204 void SetVideoNeedsBeginFrames(bool needs_begin_frames) override; | |
205 void PostAnimationEventsToMainThreadOnImplThread( | |
206 scoped_ptr<AnimationEventsVector> queue) override; | |
207 bool IsInsideDraw() override; | |
208 void RenewTreePriority() override; | |
209 void PostDelayedAnimationTaskOnImplThread(const base::Closure& task, | |
210 base::TimeDelta delay) override; | |
211 void DidActivateSyncTree() override; | |
212 void WillPrepareTiles() override; | |
213 void DidPrepareTiles() override; | |
214 void DidCompletePageScaleAnimationOnImplThread() override; | |
215 void OnDrawForOutputSurface() override; | |
216 // This should only be called by LayerTreeHostImpl::PostFrameTimingEvents. | |
217 void PostFrameTimingEventsOnImplThread( | |
218 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, | |
219 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) | |
220 override; | |
221 | |
222 // SchedulerClient implementation | |
223 void WillBeginImplFrame(const BeginFrameArgs& args) override; | |
224 void DidFinishImplFrame() override; | |
225 void ScheduledActionSendBeginMainFrame(const BeginFrameArgs& args) override; | |
226 DrawResult ScheduledActionDrawAndSwapIfPossible() override; | |
227 DrawResult ScheduledActionDrawAndSwapForced() override; | |
228 void ScheduledActionCommit() override; | |
229 void ScheduledActionActivateSyncTree() override; | |
230 void ScheduledActionBeginOutputSurfaceCreation() override; | |
231 void ScheduledActionPrepareTiles() override; | |
232 void ScheduledActionInvalidateOutputSurface() override; | |
233 void SendBeginFramesToChildren(const BeginFrameArgs& args) override; | |
234 void SendBeginMainFrameNotExpectedSoon() override; | |
235 | |
236 // ProxyMain implementation | |
237 void SetChannel(scoped_ptr<ThreadedChannel> threaded_channel) override; | |
238 | |
239 protected: | |
240 ThreadProxy(LayerTreeHost* layer_tree_host, | |
241 TaskRunnerProvider* task_runner_provider, | |
242 scoped_ptr<BeginFrameSource> external_begin_frame_source); | |
243 | |
244 private: | |
245 friend class ThreadProxyForTest; | |
246 | |
247 // ProxyMain implementation. | |
248 base::WeakPtr<ProxyMain> GetMainWeakPtr() override; | |
249 void DidCompleteSwapBuffers() override; | |
250 void SetRendererCapabilitiesMainCopy( | |
251 const RendererCapabilities& capabilities) override; | |
252 void BeginMainFrameNotExpectedSoon() override; | |
253 void DidCommitAndDrawFrame() override; | |
254 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue) override; | |
255 void DidLoseOutputSurface() override; | |
256 void RequestNewOutputSurface() override; | |
257 void DidInitializeOutputSurface( | |
258 bool success, | |
259 const RendererCapabilities& capabilities) override; | |
260 void DidCompletePageScaleAnimation() override; | |
261 void PostFrameTimingEventsOnMain( | |
262 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, | |
263 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) | |
264 override; | |
265 void BeginMainFrame( | |
266 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) override; | |
267 | |
268 // ProxyImpl implementation | |
269 base::WeakPtr<ProxyImpl> GetImplWeakPtr() override; | |
270 void SetThrottleFrameProductionOnImpl(bool throttle) override; | |
271 void UpdateTopControlsStateOnImpl(TopControlsState constraints, | |
272 TopControlsState current, | |
273 bool animate) override; | |
274 void InitializeOutputSurfaceOnImpl(OutputSurface* output_surface) override; | |
275 void MainThreadHasStoppedFlingingOnImpl() override; | |
276 void SetInputThrottledUntilCommitOnImpl(bool is_throttled) override; | |
277 void SetDeferCommitsOnImpl(bool defer_commits) const override; | |
278 void FinishAllRenderingOnImpl(CompletionEvent* completion) override; | |
279 void SetVisibleOnImpl(bool visible) override; | |
280 void ReleaseOutputSurfaceOnImpl(CompletionEvent* completion) override; | |
281 void FinishGLOnImpl(CompletionEvent* completion) override; | |
282 void MainFrameWillHappenOnImplForTesting( | |
283 CompletionEvent* completion, | |
284 bool* main_frame_will_happen) override; | |
285 void SetNeedsCommitOnImpl() override; | |
286 void SetNeedsRedrawOnImpl(const gfx::Rect& damage_rect) override; | |
287 void BeginMainFrameAbortedOnImpl( | |
288 CommitEarlyOutReason reason, | |
289 base::TimeTicks main_thread_start_time) override; | |
290 void StartCommitOnImpl(CompletionEvent* completion, | |
291 LayerTreeHost* layer_tree_host, | |
292 base::TimeTicks main_thread_start_time, | |
293 bool hold_commit_for_activation) override; | |
294 void InitializeImplOnImpl(CompletionEvent* completion, | |
295 LayerTreeHost* layer_tree_host) override; | |
296 void LayerTreeHostClosedOnImpl(CompletionEvent* completion) override; | |
297 | |
298 // Returns |true| if the request was actually sent, |false| if one was | |
299 // already outstanding. | |
300 bool SendCommitRequestToImplThreadIfNeeded( | |
301 CommitPipelineStage required_stage); | |
302 | |
303 // Called on impl thread. | |
304 struct SchedulerStateRequest; | |
305 | |
306 DrawResult DrawSwapInternal(bool forced_draw); | |
307 | |
308 TaskRunnerProvider* task_runner_provider_; | |
309 | |
310 // Use accessors instead of this variable directly. | |
311 MainThreadOnly main_thread_only_vars_unsafe_; | |
312 MainThreadOnly& main(); | |
313 | |
314 // Use accessors instead of this variable directly. | |
315 BlockedMainCommitOnly main_thread_blocked_commit_vars_unsafe_; | |
316 BlockedMainCommitOnly& blocked_main_commit(); | |
317 | |
318 // Use accessors instead of this variable directly. | |
319 CompositorThreadOnly compositor_thread_vars_unsafe_; | |
320 CompositorThreadOnly& impl(); | |
321 | |
322 // TODO(khushalsagar): Remove this. Temporary variable to hold the channel. | |
323 scoped_ptr<ThreadedChannel> threaded_channel_; | |
324 | |
325 base::WeakPtr<ThreadProxy> main_thread_weak_ptr_; | |
326 base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_; | |
327 | |
328 DISALLOW_COPY_AND_ASSIGN(ThreadProxy); | |
329 }; | |
330 | |
331 } // namespace cc | |
332 | |
333 #endif // CC_TREES_THREAD_PROXY_H_ | |
OLD | NEW |