Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / trees / thread_proxy.h
blob1a1406394772aac03d24c4b43d07d403801da1bd
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_THREAD_PROXY_H_
6 #define CC_TREES_THREAD_PROXY_H_
8 #include <string>
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"
21 namespace base {
22 class SingleThreadTaskRunner;
25 namespace cc {
27 class BeginFrameSource;
28 class ContextProvider;
29 class InputHandlerClient;
30 class LayerTreeHost;
31 class Scheduler;
32 class ScopedThreadProxy;
34 class CC_EXPORT ThreadProxy : public Proxy,
35 NON_EXPORTED_BASE(LayerTreeHostImplClient),
36 NON_EXPORTED_BASE(SchedulerClient) {
37 public:
38 static scoped_ptr<Proxy> Create(
39 LayerTreeHost* layer_tree_host,
40 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
42 scoped_ptr<BeginFrameSource> external_begin_frame_source);
44 ~ThreadProxy() override;
46 struct BeginMainFrameAndCommitState {
47 BeginMainFrameAndCommitState();
48 ~BeginMainFrameAndCommitState();
50 unsigned int begin_frame_id;
51 BeginFrameArgs begin_frame_args;
52 scoped_ptr<ScrollAndScaleSet> scroll_info;
53 size_t memory_allocation_limit_bytes;
54 bool evicted_ui_resources;
57 // Commits between the main and impl threads are processed through a pipeline
58 // with the following stages. For efficiency we can early out at any stage if
59 // we decide that no further processing is necessary.
60 enum CommitPipelineStage {
61 NO_PIPELINE_STAGE,
62 ANIMATE_PIPELINE_STAGE,
63 UPDATE_LAYERS_PIPELINE_STAGE,
64 COMMIT_PIPELINE_STAGE,
67 struct MainThreadOnly {
68 MainThreadOnly(ThreadProxy* proxy, int layer_tree_host_id);
69 ~MainThreadOnly();
71 const int layer_tree_host_id;
73 // The furthest pipeline stage which has been requested for the next
74 // commit.
75 CommitPipelineStage max_requested_pipeline_stage;
76 // The commit pipeline stage that is currently being processed.
77 CommitPipelineStage current_pipeline_stage;
78 // The commit pipeline stage at which processing for the current commit
79 // will stop. Only valid while we are executing the pipeline (i.e.,
80 // |current_pipeline_stage| is set to a pipeline stage).
81 CommitPipelineStage final_pipeline_stage;
83 bool started;
84 bool prepare_tiles_pending;
85 bool defer_commits;
87 RendererCapabilities renderer_capabilities_main_thread_copy;
89 base::WeakPtrFactory<ThreadProxy> weak_factory;
92 // Accessed on the main thread, or when main thread is blocked.
93 struct MainThreadOrBlockedMainThread {
94 explicit MainThreadOrBlockedMainThread(LayerTreeHost* host);
95 ~MainThreadOrBlockedMainThread();
97 LayerTreeHost* layer_tree_host;
98 bool commit_waits_for_activation;
99 bool main_thread_inside_commit;
102 struct CompositorThreadOnly {
103 CompositorThreadOnly(
104 ThreadProxy* proxy,
105 int layer_tree_host_id,
106 RenderingStatsInstrumentation* rendering_stats_instrumentation,
107 scoped_ptr<BeginFrameSource> external_begin_frame_source);
108 ~CompositorThreadOnly();
110 const int layer_tree_host_id;
112 scoped_ptr<Scheduler> scheduler;
114 // Set when the main thread is waiting on a
115 // ScheduledActionSendBeginMainFrame to be issued.
116 CompletionEvent* begin_main_frame_sent_completion_event;
118 // Set when the main thread is waiting on a commit to complete.
119 CompletionEvent* commit_completion_event;
121 // Set when the main thread is waiting on a pending tree activation.
122 CompletionEvent* completion_event_for_commit_held_on_tree_activation;
124 // Set when the next draw should post DidCommitAndDrawFrame to the main
125 // thread.
126 bool next_frame_is_newly_committed_frame;
128 bool inside_draw;
130 bool input_throttled_until_commit;
132 // Whether a commit has been completed since the last time animations were
133 // ticked. If this happens, we need to animate again.
134 bool did_commit_after_animating;
136 DelayedUniqueNotifier smoothness_priority_expiration_notifier;
138 scoped_ptr<BeginFrameSource> external_begin_frame_source;
140 RenderingStatsInstrumentation* rendering_stats_instrumentation;
142 // Values used to keep track of frame durations. Used only in frame timing.
143 BeginFrameArgs last_begin_main_frame_args;
144 BeginFrameArgs last_processed_begin_main_frame_args;
146 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl;
147 base::WeakPtrFactory<ThreadProxy> weak_factory;
150 const MainThreadOnly& main() const;
151 const MainThreadOrBlockedMainThread& blocked_main() const;
152 const CompositorThreadOnly& impl() const;
154 // Proxy implementation
155 void FinishAllRendering() override;
156 bool IsStarted() const override;
157 bool CommitToActiveTree() const override;
158 void SetOutputSurface(scoped_ptr<OutputSurface>) override;
159 void SetLayerTreeHostClientReady() 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 void ForceSerializeOnSwapBuffers() override;
176 bool SupportsImplScrolling() const override;
177 bool MainFrameWillHappenForTesting() override;
178 void SetChildrenNeedBeginFrames(bool children_need_begin_frames) override;
179 void SetAuthoritativeVSyncInterval(const base::TimeDelta& interval) override;
180 scoped_ptr<OutputSurface> ReleaseOutputSurface() override;
182 // LayerTreeHostImplClient implementation
183 void UpdateRendererCapabilitiesOnImplThread() override;
184 void DidLoseOutputSurfaceOnImplThread() override;
185 void CommitVSyncParameters(base::TimeTicks timebase,
186 base::TimeDelta interval) override;
187 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override;
188 void SetMaxSwapsPendingOnImplThread(int max) override;
189 void DidSwapBuffersOnImplThread() override;
190 void DidSwapBuffersCompleteOnImplThread() override;
191 void OnCanDrawStateChanged(bool can_draw) override;
192 void NotifyReadyToActivate() override;
193 void NotifyReadyToDraw() override;
194 // Please call these 3 functions through
195 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
196 // SetNeedsAnimate().
197 void SetNeedsRedrawOnImplThread() override;
198 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect) override;
199 void SetNeedsAnimateOnImplThread() override;
200 void SetNeedsPrepareTilesOnImplThread() override;
201 void SetNeedsCommitOnImplThread() override;
202 void SetVideoNeedsBeginFrames(bool needs_begin_frames) override;
203 void PostAnimationEventsToMainThreadOnImplThread(
204 scoped_ptr<AnimationEventsVector> queue) override;
205 bool IsInsideDraw() override;
206 void RenewTreePriority() override;
207 void PostDelayedAnimationTaskOnImplThread(const base::Closure& task,
208 base::TimeDelta delay) override;
209 void DidActivateSyncTree() override;
210 void WillPrepareTiles() override;
211 void DidPrepareTiles() override;
212 void DidCompletePageScaleAnimationOnImplThread() override;
213 void OnDrawForOutputSurface() override;
214 // This should only be called by LayerTreeHostImpl::PostFrameTimingEvents.
215 void PostFrameTimingEventsOnImplThread(
216 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
217 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
218 override;
220 // SchedulerClient implementation
221 void WillBeginImplFrame(const BeginFrameArgs& args) override;
222 void DidFinishImplFrame() override;
223 void ScheduledActionSendBeginMainFrame() override;
224 DrawResult ScheduledActionDrawAndSwapIfPossible() override;
225 DrawResult ScheduledActionDrawAndSwapForced() override;
226 void ScheduledActionAnimate() override;
227 void ScheduledActionCommit() override;
228 void ScheduledActionActivateSyncTree() override;
229 void ScheduledActionBeginOutputSurfaceCreation() override;
230 void ScheduledActionPrepareTiles() override;
231 void ScheduledActionInvalidateOutputSurface() override;
232 void SendBeginFramesToChildren(const BeginFrameArgs& args) override;
233 void SendBeginMainFrameNotExpectedSoon() override;
235 protected:
236 ThreadProxy(
237 LayerTreeHost* layer_tree_host,
238 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
239 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
240 scoped_ptr<BeginFrameSource> external_begin_frame_source);
242 private:
243 // Called on main thread.
244 void SetRendererCapabilitiesMainThreadCopy(
245 const RendererCapabilities& capabilities);
246 void BeginMainFrame(
247 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
248 void BeginMainFrameNotExpectedSoon();
249 void DidCommitAndDrawFrame();
250 void DidCompleteSwapBuffers();
251 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue);
252 void DidLoseOutputSurface();
253 void RequestNewOutputSurface();
254 void DidInitializeOutputSurface(bool success,
255 const RendererCapabilities& capabilities);
256 // Returns |true| if the request was actually sent, |false| if one was
257 // already outstanding.
258 bool SendCommitRequestToImplThreadIfNeeded(
259 CommitPipelineStage required_stage);
260 void DidCompletePageScaleAnimation();
262 // Called on impl thread.
263 struct SchedulerStateRequest;
265 void StartCommitOnImplThread(CompletionEvent* completion);
266 void BeginMainFrameAbortedOnImplThread(CommitEarlyOutReason reason);
267 void FinishAllRenderingOnImplThread(CompletionEvent* completion);
268 void InitializeImplOnImplThread(CompletionEvent* completion);
269 void SetLayerTreeHostClientReadyOnImplThread();
270 void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
271 void SetThrottleFrameProductionOnImplThread(bool throttle);
272 void HasInitializedOutputSurfaceOnImplThread(
273 CompletionEvent* completion,
274 bool* has_initialized_output_surface);
275 void DeleteContentsTexturesOnImplThread(CompletionEvent* completion);
276 void InitializeOutputSurfaceOnImplThread(
277 scoped_ptr<OutputSurface> output_surface);
278 void ReleaseOutputSurfaceOnImplThread(
279 CompletionEvent* completion,
280 scoped_ptr<OutputSurface>* output_surface);
281 void FinishGLOnImplThread(CompletionEvent* completion);
282 void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
283 DrawResult DrawSwapInternal(bool forced_draw);
284 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
285 void MainFrameWillHappenOnImplThreadForTesting(CompletionEvent* completion,
286 bool* main_frame_will_happen);
287 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
288 void MainThreadHasStoppedFlingingOnImplThread();
289 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
290 void SetDeferCommitsOnImplThread(bool defer_commits) const;
291 void PostFrameTimingEvents(
292 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
293 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events);
295 LayerTreeHost* layer_tree_host();
296 const LayerTreeHost* layer_tree_host() const;
298 // Use accessors instead of this variable directly.
299 MainThreadOnly main_thread_only_vars_unsafe_;
300 MainThreadOnly& main();
302 // Use accessors instead of this variable directly.
303 MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_;
304 MainThreadOrBlockedMainThread& blocked_main();
306 // Use accessors instead of this variable directly.
307 CompositorThreadOnly compositor_thread_vars_unsafe_;
308 CompositorThreadOnly& impl();
310 base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
311 base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
313 DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
316 } // namespace cc
318 #endif // CC_TREES_THREAD_PROXY_H_