cc: Implement shared worker contexts.
[chromium-blink-merge.git] / cc / trees / thread_proxy.h
blob6fff58230004154fcaf5234ae25772f38226a97e
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(OutputSurface* output_surface) 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;
181 // LayerTreeHostImplClient implementation
182 void UpdateRendererCapabilitiesOnImplThread() override;
183 void DidLoseOutputSurfaceOnImplThread() override;
184 void CommitVSyncParameters(base::TimeTicks timebase,
185 base::TimeDelta interval) override;
186 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override;
187 void SetMaxSwapsPendingOnImplThread(int max) override;
188 void DidSwapBuffersOnImplThread() override;
189 void DidSwapBuffersCompleteOnImplThread() override;
190 void OnCanDrawStateChanged(bool can_draw) override;
191 void NotifyReadyToActivate() override;
192 void NotifyReadyToDraw() override;
193 // Please call these 3 functions through
194 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
195 // SetNeedsAnimate().
196 void SetNeedsRedrawOnImplThread() override;
197 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect) override;
198 void SetNeedsAnimateOnImplThread() override;
199 void SetNeedsPrepareTilesOnImplThread() override;
200 void SetNeedsCommitOnImplThread() override;
201 void SetVideoNeedsBeginFrames(bool needs_begin_frames) override;
202 void PostAnimationEventsToMainThreadOnImplThread(
203 scoped_ptr<AnimationEventsVector> queue) override;
204 bool IsInsideDraw() override;
205 void RenewTreePriority() override;
206 void PostDelayedAnimationTaskOnImplThread(const base::Closure& task,
207 base::TimeDelta delay) override;
208 void DidActivateSyncTree() override;
209 void WillPrepareTiles() override;
210 void DidPrepareTiles() override;
211 void DidCompletePageScaleAnimationOnImplThread() override;
212 void OnDrawForOutputSurface() override;
213 // This should only be called by LayerTreeHostImpl::PostFrameTimingEvents.
214 void PostFrameTimingEventsOnImplThread(
215 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
216 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
217 override;
219 // SchedulerClient implementation
220 void WillBeginImplFrame(const BeginFrameArgs& args) override;
221 void DidFinishImplFrame() override;
222 void ScheduledActionSendBeginMainFrame() override;
223 DrawResult ScheduledActionDrawAndSwapIfPossible() override;
224 DrawResult ScheduledActionDrawAndSwapForced() override;
225 void ScheduledActionAnimate() override;
226 void ScheduledActionCommit() override;
227 void ScheduledActionActivateSyncTree() override;
228 void ScheduledActionBeginOutputSurfaceCreation() override;
229 void ScheduledActionPrepareTiles() override;
230 void ScheduledActionInvalidateOutputSurface() override;
231 void SendBeginFramesToChildren(const BeginFrameArgs& args) override;
232 void SendBeginMainFrameNotExpectedSoon() override;
234 protected:
235 ThreadProxy(
236 LayerTreeHost* layer_tree_host,
237 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
238 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
239 scoped_ptr<BeginFrameSource> external_begin_frame_source);
241 private:
242 // Called on main thread.
243 void SetRendererCapabilitiesMainThreadCopy(
244 const RendererCapabilities& capabilities);
245 void BeginMainFrame(
246 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
247 void BeginMainFrameNotExpectedSoon();
248 void DidCommitAndDrawFrame();
249 void DidCompleteSwapBuffers();
250 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue);
251 void DidLoseOutputSurface();
252 void RequestNewOutputSurface();
253 void DidInitializeOutputSurface(bool success,
254 const RendererCapabilities& capabilities);
255 // Returns |true| if the request was actually sent, |false| if one was
256 // already outstanding.
257 bool SendCommitRequestToImplThreadIfNeeded(
258 CommitPipelineStage required_stage);
259 void DidCompletePageScaleAnimation();
261 // Called on impl thread.
262 struct SchedulerStateRequest;
264 void StartCommitOnImplThread(CompletionEvent* completion);
265 void BeginMainFrameAbortedOnImplThread(CommitEarlyOutReason reason);
266 void FinishAllRenderingOnImplThread(CompletionEvent* completion);
267 void InitializeImplOnImplThread(CompletionEvent* completion);
268 void SetLayerTreeHostClientReadyOnImplThread();
269 void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
270 void SetThrottleFrameProductionOnImplThread(bool throttle);
271 void HasInitializedOutputSurfaceOnImplThread(
272 CompletionEvent* completion,
273 bool* has_initialized_output_surface);
274 void DeleteContentsTexturesOnImplThread(CompletionEvent* completion);
275 void InitializeOutputSurfaceOnImplThread(OutputSurface* output_surface);
276 void FinishGLOnImplThread(CompletionEvent* completion);
277 void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
278 DrawResult DrawSwapInternal(bool forced_draw);
279 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
280 void MainFrameWillHappenOnImplThreadForTesting(CompletionEvent* completion,
281 bool* main_frame_will_happen);
282 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
283 void MainThreadHasStoppedFlingingOnImplThread();
284 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
285 void SetDeferCommitsOnImplThread(bool defer_commits) const;
286 void PostFrameTimingEvents(
287 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
288 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events);
290 LayerTreeHost* layer_tree_host();
291 const LayerTreeHost* layer_tree_host() const;
293 // Use accessors instead of this variable directly.
294 MainThreadOnly main_thread_only_vars_unsafe_;
295 MainThreadOnly& main();
297 // Use accessors instead of this variable directly.
298 MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_;
299 MainThreadOrBlockedMainThread& blocked_main();
301 // Use accessors instead of this variable directly.
302 CompositorThreadOnly compositor_thread_vars_unsafe_;
303 CompositorThreadOnly& impl();
305 base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
306 base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
308 DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
311 } // namespace cc
313 #endif // CC_TREES_THREAD_PROXY_H_