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/scheduler.h"
18 #include "cc/trees/layer_tree_host_impl.h"
19 #include "cc/trees/proxy.h"
20 #include "cc/trees/proxy_timing_history.h"
23 class SingleThreadTaskRunner
;
28 class ContextProvider
;
29 class InputHandlerClient
;
31 class ResourceUpdateQueue
;
33 class ScopedThreadProxy
;
35 class CC_EXPORT ThreadProxy
: public Proxy
,
36 NON_EXPORTED_BASE(LayerTreeHostImplClient
),
37 NON_EXPORTED_BASE(SchedulerClient
),
38 NON_EXPORTED_BASE(ResourceUpdateControllerClient
) {
40 static scoped_ptr
<Proxy
> Create(
41 LayerTreeHost
* layer_tree_host
,
42 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
44 virtual ~ThreadProxy();
46 struct BeginMainFrameAndCommitState
{
47 BeginMainFrameAndCommitState();
48 ~BeginMainFrameAndCommitState();
50 unsigned int begin_frame_id
;
51 base::TimeTicks monotonic_frame_begin_time
;
52 scoped_ptr
<ScrollAndScaleSet
> scroll_info
;
53 size_t memory_allocation_limit_bytes
;
54 int memory_allocation_priority_cutoff
;
55 bool evicted_ui_resources
;
58 struct MainThreadOnly
{
59 MainThreadOnly(ThreadProxy
* proxy
, int layer_tree_host_id
);
62 const int layer_tree_host_id
;
64 // Set only when SetNeedsAnimate is called.
65 bool animate_requested
;
66 // Set only when SetNeedsCommit is called.
67 bool commit_requested
;
68 // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
69 bool commit_request_sent_to_impl_thread
;
72 bool manage_tiles_pending
;
73 bool can_cancel_commit
;
76 RendererCapabilities renderer_capabilities_main_thread_copy
;
78 scoped_ptr
<BeginMainFrameAndCommitState
> pending_deferred_commit
;
79 base::WeakPtrFactory
<ThreadProxy
> weak_factory
;
82 // Accessed on the main thread, or when main thread is blocked.
83 struct MainThreadOrBlockedMainThread
{
84 explicit MainThreadOrBlockedMainThread(LayerTreeHost
* host
);
85 ~MainThreadOrBlockedMainThread();
87 PrioritizedResourceManager
* contents_texture_manager();
89 LayerTreeHost
* layer_tree_host
;
90 bool commit_waits_for_activation
;
91 bool main_thread_inside_commit
;
93 base::TimeTicks last_monotonic_frame_begin_time
;
96 struct ReadbackRequest
;
98 struct CompositorThreadOnly
{
99 CompositorThreadOnly(ThreadProxy
* proxy
, int layer_tree_host_id
);
100 ~CompositorThreadOnly();
102 const int layer_tree_host_id
;
104 // Copy of the main thread side contents texture manager for work
105 // that needs to be done on the compositor thread.
106 PrioritizedResourceManager
* contents_texture_manager
;
108 scoped_ptr
<Scheduler
> scheduler
;
110 // Set when the main thread is waiting on a
111 // ScheduledActionSendBeginMainFrame to be issued.
112 CompletionEvent
* begin_main_frame_sent_completion_event
;
114 // Set when the main thread is waiting on a commit to complete.
115 CompletionEvent
* commit_completion_event
;
117 // Set when the main thread is waiting on a pending tree activation.
118 CompletionEvent
* completion_event_for_commit_held_on_tree_activation
;
120 scoped_ptr
<ResourceUpdateController
> current_resource_update_controller
;
122 // Set when the next draw should post DidCommitAndDrawFrame to the main
124 bool next_frame_is_newly_committed_frame
;
128 bool input_throttled_until_commit
;
130 // Set when we freeze animations to avoid checkerboarding.
131 bool animations_frozen_until_next_draw
;
132 base::TimeTicks animation_time
;
134 // Whether a commit has been completed since the last time animations were
135 // ticked. If this happens, we need to animate again.
136 bool did_commit_after_animating
;
138 DelayedUniqueNotifier smoothness_priority_expiration_notifier
;
140 ProxyTimingHistory timing_history
;
142 scoped_ptr
<LayerTreeHostImpl
> layer_tree_host_impl
;
143 base::WeakPtrFactory
<ThreadProxy
> weak_factory
;
146 const MainThreadOnly
& main() const;
147 const MainThreadOrBlockedMainThread
& blocked_main() const;
148 const CompositorThreadOnly
& impl() const;
150 // Proxy implementation
151 virtual void FinishAllRendering() OVERRIDE
;
152 virtual bool IsStarted() const OVERRIDE
;
153 virtual void SetLayerTreeHostClientReady() OVERRIDE
;
154 virtual void SetVisible(bool visible
) OVERRIDE
;
155 virtual const RendererCapabilities
& GetRendererCapabilities() const OVERRIDE
;
156 virtual void SetNeedsAnimate() OVERRIDE
;
157 virtual void SetNeedsUpdateLayers() OVERRIDE
;
158 virtual void SetNeedsCommit() OVERRIDE
;
159 virtual void SetNeedsRedraw(const gfx::Rect
& damage_rect
) OVERRIDE
;
160 virtual void SetNextCommitWaitsForActivation() OVERRIDE
;
161 virtual void NotifyInputThrottledUntilCommit() OVERRIDE
;
162 virtual void SetDeferCommits(bool defer_commits
) OVERRIDE
;
163 virtual bool CommitRequested() const OVERRIDE
;
164 virtual bool BeginMainFrameRequested() const OVERRIDE
;
165 virtual void MainThreadHasStoppedFlinging() OVERRIDE
;
166 virtual void Start() OVERRIDE
;
167 virtual void Stop() OVERRIDE
;
168 virtual size_t MaxPartialTextureUpdates() const OVERRIDE
;
169 virtual void ForceSerializeOnSwapBuffers() OVERRIDE
;
170 virtual bool SupportsImplScrolling() const OVERRIDE
;
171 virtual void SetDebugState(const LayerTreeDebugState
& debug_state
) OVERRIDE
;
172 virtual scoped_ptr
<base::Value
> AsValue() const OVERRIDE
;
173 virtual bool CommitPendingForTesting() OVERRIDE
;
174 virtual scoped_ptr
<base::Value
> SchedulerAsValueForTesting() OVERRIDE
;
176 // LayerTreeHostImplClient implementation
177 virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE
;
178 virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE
;
179 virtual void CommitVSyncParameters(base::TimeTicks timebase
,
180 base::TimeDelta interval
) OVERRIDE
;
181 virtual void SetEstimatedParentDrawTime(base::TimeDelta draw_time
) OVERRIDE
;
182 virtual void BeginFrame(const BeginFrameArgs
& args
) OVERRIDE
;
183 virtual void SetMaxSwapsPendingOnImplThread(int max
) OVERRIDE
;
184 virtual void DidSwapBuffersOnImplThread() OVERRIDE
;
185 virtual void DidSwapBuffersCompleteOnImplThread() OVERRIDE
;
186 virtual void OnCanDrawStateChanged(bool can_draw
) OVERRIDE
;
187 virtual void NotifyReadyToActivate() OVERRIDE
;
188 // Please call these 3 functions through
189 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
190 // SetNeedsAnimate().
191 virtual void SetNeedsRedrawOnImplThread() OVERRIDE
;
192 virtual void SetNeedsRedrawRectOnImplThread(const gfx::Rect
& dirty_rect
)
194 virtual void SetNeedsAnimateOnImplThread() OVERRIDE
;
195 virtual void SetNeedsManageTilesOnImplThread() OVERRIDE
;
196 virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE
;
197 virtual void SetNeedsCommitOnImplThread() OVERRIDE
;
198 virtual void PostAnimationEventsToMainThreadOnImplThread(
199 scoped_ptr
<AnimationEventsVector
> queue
) OVERRIDE
;
200 virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes
,
203 virtual bool IsInsideDraw() OVERRIDE
;
204 virtual void RenewTreePriority() OVERRIDE
;
205 virtual void PostDelayedScrollbarFadeOnImplThread(
206 const base::Closure
& start_fade
,
207 base::TimeDelta delay
) OVERRIDE
;
208 virtual void DidActivateSyncTree() OVERRIDE
;
209 virtual void DidManageTiles() OVERRIDE
;
211 // SchedulerClient implementation
212 virtual void SetNeedsBeginFrame(bool enable
) OVERRIDE
;
213 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) OVERRIDE
;
214 virtual void ScheduledActionSendBeginMainFrame() OVERRIDE
;
215 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() OVERRIDE
;
216 virtual DrawResult
ScheduledActionDrawAndSwapForced() OVERRIDE
;
217 virtual void ScheduledActionAnimate() OVERRIDE
;
218 virtual void ScheduledActionCommit() OVERRIDE
;
219 virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE
;
220 virtual void ScheduledActionActivateSyncTree() OVERRIDE
;
221 virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE
;
222 virtual void ScheduledActionManageTiles() OVERRIDE
;
223 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) OVERRIDE
;
224 virtual base::TimeDelta
DrawDurationEstimate() OVERRIDE
;
225 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() OVERRIDE
;
226 virtual base::TimeDelta
CommitToActivateDurationEstimate() OVERRIDE
;
227 virtual void DidBeginImplFrameDeadline() OVERRIDE
;
229 // ResourceUpdateControllerClient implementation
230 virtual void ReadyToFinalizeTextureUpdates() OVERRIDE
;
233 ThreadProxy(LayerTreeHost
* layer_tree_host
,
234 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
237 // Called on main thread.
238 void SetRendererCapabilitiesMainThreadCopy(
239 const RendererCapabilities
& capabilities
);
241 scoped_ptr
<BeginMainFrameAndCommitState
> begin_main_frame_state
);
242 void DidCommitAndDrawFrame();
243 void DidCompleteSwapBuffers();
244 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> queue
);
245 void DidLoseOutputSurface();
246 void CreateAndInitializeOutputSurface();
247 void DidInitializeOutputSurface(bool success
,
248 const RendererCapabilities
& capabilities
);
249 void SendCommitRequestToImplThreadIfNeeded();
251 // Called on impl thread.
252 struct CommitPendingRequest
;
253 struct SchedulerStateRequest
;
255 void StartCommitOnImplThread(CompletionEvent
* completion
,
256 ResourceUpdateQueue
* queue
);
257 void BeginMainFrameAbortedOnImplThread(bool did_handle
);
258 void FinishAllRenderingOnImplThread(CompletionEvent
* completion
);
259 void InitializeImplOnImplThread(CompletionEvent
* completion
);
260 void SetLayerTreeHostClientReadyOnImplThread();
261 void SetVisibleOnImplThread(CompletionEvent
* completion
, bool visible
);
262 void UpdateBackgroundAnimateTicking();
263 void HasInitializedOutputSurfaceOnImplThread(
264 CompletionEvent
* completion
,
265 bool* has_initialized_output_surface
);
266 void DeleteContentsTexturesOnImplThread(CompletionEvent
* completion
);
267 void InitializeOutputSurfaceOnImplThread(
268 scoped_ptr
<OutputSurface
> output_surface
);
269 void FinishGLOnImplThread(CompletionEvent
* completion
);
270 void LayerTreeHostClosedOnImplThread(CompletionEvent
* completion
);
271 DrawResult
DrawSwapInternal(bool forced_draw
);
272 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent
* completion
);
273 void CheckOutputSurfaceStatusOnImplThread();
274 void CommitPendingOnImplThreadForTesting(CommitPendingRequest
* request
);
275 void SchedulerAsValueOnImplThreadForTesting(SchedulerStateRequest
* request
);
276 void AsValueOnImplThread(CompletionEvent
* completion
,
277 base::DictionaryValue
* state
) const;
278 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile
);
279 void MainThreadHasStoppedFlingingOnImplThread();
280 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled
);
281 void SetDebugStateOnImplThread(const LayerTreeDebugState
& debug_state
);
283 LayerTreeHost
* layer_tree_host();
284 const LayerTreeHost
* layer_tree_host() const;
286 // Use accessors instead of this variable directly.
287 MainThreadOnly main_thread_only_vars_unsafe_
;
288 MainThreadOnly
& main();
290 // Use accessors instead of this variable directly.
291 MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_
;
292 MainThreadOrBlockedMainThread
& blocked_main();
294 // Use accessors instead of this variable directly.
295 CompositorThreadOnly compositor_thread_vars_unsafe_
;
296 CompositorThreadOnly
& impl();
298 base::WeakPtr
<ThreadProxy
> main_thread_weak_ptr_
;
299 base::WeakPtr
<ThreadProxy
> impl_thread_weak_ptr_
;
301 DISALLOW_COPY_AND_ASSIGN(ThreadProxy
);
306 #endif // CC_TREES_THREAD_PROXY_H_