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_
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/resources/resource_update_controller.h"
17 #include "cc/scheduler/commit_earlyout_reason.h"
18 #include "cc/scheduler/scheduler.h"
19 #include "cc/trees/layer_tree_host_impl.h"
20 #include "cc/trees/proxy.h"
21 #include "cc/trees/proxy_timing_history.h"
24 class SingleThreadTaskRunner
;
29 class BeginFrameSource
;
30 class ContextProvider
;
31 class InputHandlerClient
;
33 class PrioritizedResourceManager
;
34 class ResourceUpdateQueue
;
36 class ScopedThreadProxy
;
38 class CC_EXPORT ThreadProxy
: public Proxy
,
39 NON_EXPORTED_BASE(LayerTreeHostImplClient
),
40 NON_EXPORTED_BASE(SchedulerClient
),
41 NON_EXPORTED_BASE(ResourceUpdateControllerClient
) {
43 static scoped_ptr
<Proxy
> Create(
44 LayerTreeHost
* layer_tree_host
,
45 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
46 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
47 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
49 ~ThreadProxy() override
;
51 struct BeginMainFrameAndCommitState
{
52 BeginMainFrameAndCommitState();
53 ~BeginMainFrameAndCommitState();
55 unsigned int begin_frame_id
;
56 BeginFrameArgs begin_frame_args
;
57 scoped_ptr
<ScrollAndScaleSet
> scroll_info
;
58 size_t memory_allocation_limit_bytes
;
59 int memory_allocation_priority_cutoff
;
60 bool evicted_ui_resources
;
63 struct MainThreadOnly
{
64 MainThreadOnly(ThreadProxy
* proxy
, int layer_tree_host_id
);
67 const int layer_tree_host_id
;
69 // Set only when SetNeedsAnimate is called.
70 bool animate_requested
;
71 // Set only when SetNeedsCommit is called.
72 bool commit_requested
;
73 // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
74 bool commit_request_sent_to_impl_thread
;
77 bool prepare_tiles_pending
;
78 bool can_cancel_commit
;
81 RendererCapabilities renderer_capabilities_main_thread_copy
;
83 base::WeakPtrFactory
<ThreadProxy
> weak_factory
;
86 // Accessed on the main thread, or when main thread is blocked.
87 struct MainThreadOrBlockedMainThread
{
88 explicit MainThreadOrBlockedMainThread(LayerTreeHost
* host
);
89 ~MainThreadOrBlockedMainThread();
91 PrioritizedResourceManager
* contents_texture_manager();
93 LayerTreeHost
* layer_tree_host
;
94 bool commit_waits_for_activation
;
95 bool main_thread_inside_commit
;
98 struct CompositorThreadOnly
{
101 int layer_tree_host_id
,
102 RenderingStatsInstrumentation
* rendering_stats_instrumentation
,
103 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
104 ~CompositorThreadOnly();
106 const int layer_tree_host_id
;
108 // Copy of the main thread side contents texture manager for work
109 // that needs to be done on the compositor thread.
110 PrioritizedResourceManager
* contents_texture_manager
;
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 scoped_ptr
<ResourceUpdateController
> current_resource_update_controller
;
126 // Set when the next draw should post DidCommitAndDrawFrame to the main
128 bool next_frame_is_newly_committed_frame
;
132 bool input_throttled_until_commit
;
134 base::TimeTicks animation_time
;
136 // Whether a commit has been completed since the last time animations were
137 // ticked. If this happens, we need to animate again.
138 bool did_commit_after_animating
;
140 DelayedUniqueNotifier smoothness_priority_expiration_notifier
;
142 ProxyTimingHistory timing_history
;
144 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
;
146 // Values used to keep track of frame durations. Used only in frame timing.
147 BeginFrameArgs last_begin_main_frame_args
;
148 BeginFrameArgs last_processed_begin_main_frame_args
;
150 scoped_ptr
<LayerTreeHostImpl
> layer_tree_host_impl
;
151 base::WeakPtrFactory
<ThreadProxy
> weak_factory
;
154 const MainThreadOnly
& main() const;
155 const MainThreadOrBlockedMainThread
& blocked_main() const;
156 const CompositorThreadOnly
& impl() const;
158 // Proxy implementation
159 void FinishAllRendering() override
;
160 bool IsStarted() const override
;
161 bool CommitToActiveTree() const override
;
162 void SetOutputSurface(scoped_ptr
<OutputSurface
>) override
;
163 void SetLayerTreeHostClientReady() override
;
164 void SetVisible(bool visible
) override
;
165 void SetThrottleFrameProduction(bool throttle
) override
;
166 const RendererCapabilities
& GetRendererCapabilities() const override
;
167 void SetNeedsAnimate() override
;
168 void SetNeedsUpdateLayers() override
;
169 void SetNeedsCommit() override
;
170 void SetNeedsRedraw(const gfx::Rect
& damage_rect
) override
;
171 void SetNextCommitWaitsForActivation() override
;
172 void NotifyInputThrottledUntilCommit() override
;
173 void SetDeferCommits(bool defer_commits
) override
;
174 bool CommitRequested() const override
;
175 bool BeginMainFrameRequested() const override
;
176 void MainThreadHasStoppedFlinging() override
;
177 void Start() override
;
178 void Stop() override
;
179 size_t MaxPartialTextureUpdates() const override
;
180 void ForceSerializeOnSwapBuffers() override
;
181 bool SupportsImplScrolling() const override
;
182 void SetDebugState(const LayerTreeDebugState
& debug_state
) override
;
183 bool MainFrameWillHappenForTesting() override
;
184 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
) override
;
185 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
) override
;
187 // LayerTreeHostImplClient implementation
188 void UpdateRendererCapabilitiesOnImplThread() override
;
189 void DidLoseOutputSurfaceOnImplThread() override
;
190 void CommitVSyncParameters(base::TimeTicks timebase
,
191 base::TimeDelta interval
) override
;
192 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
) override
;
193 void SetMaxSwapsPendingOnImplThread(int max
) override
;
194 void DidSwapBuffersOnImplThread() override
;
195 void DidSwapBuffersCompleteOnImplThread() override
;
196 void OnCanDrawStateChanged(bool can_draw
) override
;
197 void NotifyReadyToActivate() override
;
198 void NotifyReadyToDraw() override
;
199 // Please call these 3 functions through
200 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
201 // SetNeedsAnimate().
202 void SetNeedsRedrawOnImplThread() override
;
203 void SetNeedsRedrawRectOnImplThread(const gfx::Rect
& dirty_rect
) override
;
204 void SetNeedsAnimateOnImplThread() override
;
205 void SetNeedsPrepareTilesOnImplThread() override
;
206 void SetNeedsCommitOnImplThread() override
;
207 void SetVideoNeedsBeginFrames(bool needs_begin_frames
) override
;
208 void PostAnimationEventsToMainThreadOnImplThread(
209 scoped_ptr
<AnimationEventsVector
> queue
) override
;
210 bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes
,
211 int priority_cutoff
) override
;
212 bool IsInsideDraw() override
;
213 void RenewTreePriority() override
;
214 void PostDelayedAnimationTaskOnImplThread(const base::Closure
& task
,
215 base::TimeDelta delay
) override
;
216 void DidActivateSyncTree() override
;
217 void DidPrepareTiles() override
;
218 void DidCompletePageScaleAnimationOnImplThread() override
;
219 void OnDrawForOutputSurface() override
;
221 // SchedulerClient implementation
222 void WillBeginImplFrame(const BeginFrameArgs
& args
) override
;
223 void DidFinishImplFrame() override
;
224 void ScheduledActionSendBeginMainFrame() override
;
225 DrawResult
ScheduledActionDrawAndSwapIfPossible() override
;
226 DrawResult
ScheduledActionDrawAndSwapForced() override
;
227 void ScheduledActionAnimate() override
;
228 void ScheduledActionCommit() override
;
229 void ScheduledActionActivateSyncTree() override
;
230 void ScheduledActionBeginOutputSurfaceCreation() override
;
231 void ScheduledActionPrepareTiles() override
;
232 void ScheduledActionInvalidateOutputSurface() override
;
233 void DidAnticipatedDrawTimeChange(base::TimeTicks time
) override
;
234 base::TimeDelta
DrawDurationEstimate() override
;
235 base::TimeDelta
BeginMainFrameToCommitDurationEstimate() override
;
236 base::TimeDelta
CommitToActivateDurationEstimate() override
;
237 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) override
;
238 void SendBeginMainFrameNotExpectedSoon() override
;
240 // ResourceUpdateControllerClient implementation
241 void ReadyToFinalizeTextureUpdates() override
;
245 LayerTreeHost
* layer_tree_host
,
246 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
247 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
248 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
251 // Called on main thread.
252 void SetRendererCapabilitiesMainThreadCopy(
253 const RendererCapabilities
& capabilities
);
255 scoped_ptr
<BeginMainFrameAndCommitState
> begin_main_frame_state
);
256 void BeginMainFrameNotExpectedSoon();
257 void DidCommitAndDrawFrame();
258 void DidCompleteSwapBuffers();
259 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> queue
);
260 void DidLoseOutputSurface();
261 void RequestNewOutputSurface();
262 void DidInitializeOutputSurface(bool success
,
263 const RendererCapabilities
& capabilities
);
264 void SendCommitRequestToImplThreadIfNeeded();
265 void DidCompletePageScaleAnimation();
267 // Called on impl thread.
268 struct SchedulerStateRequest
;
270 void StartCommitOnImplThread(CompletionEvent
* completion
,
271 ResourceUpdateQueue
* queue
);
272 void BeginMainFrameAbortedOnImplThread(CommitEarlyOutReason reason
);
273 void FinishAllRenderingOnImplThread(CompletionEvent
* completion
);
274 void InitializeImplOnImplThread(CompletionEvent
* completion
);
275 void SetLayerTreeHostClientReadyOnImplThread();
276 void SetVisibleOnImplThread(CompletionEvent
* completion
, bool visible
);
277 void SetThrottleFrameProductionOnImplThread(bool throttle
);
278 void HasInitializedOutputSurfaceOnImplThread(
279 CompletionEvent
* completion
,
280 bool* has_initialized_output_surface
);
281 void DeleteContentsTexturesOnImplThread(CompletionEvent
* completion
);
282 void InitializeOutputSurfaceOnImplThread(
283 scoped_ptr
<OutputSurface
> output_surface
);
284 void FinishGLOnImplThread(CompletionEvent
* completion
);
285 void LayerTreeHostClosedOnImplThread(CompletionEvent
* completion
);
286 DrawResult
DrawSwapInternal(bool forced_draw
);
287 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent
* completion
);
288 void MainFrameWillHappenOnImplThreadForTesting(CompletionEvent
* completion
,
289 bool* main_frame_will_happen
);
290 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile
);
291 void MainThreadHasStoppedFlingingOnImplThread();
292 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled
);
293 void SetDebugStateOnImplThread(const LayerTreeDebugState
& debug_state
);
294 void SetDeferCommitsOnImplThread(bool defer_commits
) const;
296 LayerTreeHost
* layer_tree_host();
297 const LayerTreeHost
* layer_tree_host() const;
299 // Use accessors instead of this variable directly.
300 MainThreadOnly main_thread_only_vars_unsafe_
;
301 MainThreadOnly
& main();
303 // Use accessors instead of this variable directly.
304 MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_
;
305 MainThreadOrBlockedMainThread
& blocked_main();
307 // Use accessors instead of this variable directly.
308 CompositorThreadOnly compositor_thread_vars_unsafe_
;
309 CompositorThreadOnly
& impl();
311 base::WeakPtr
<ThreadProxy
> main_thread_weak_ptr_
;
312 base::WeakPtr
<ThreadProxy
> impl_thread_weak_ptr_
;
314 DISALLOW_COPY_AND_ASSIGN(ThreadProxy
);
319 #endif // CC_TREES_THREAD_PROXY_H_