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
> main_task_runner
,
43 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
45 virtual ~ThreadProxy();
47 struct BeginMainFrameAndCommitState
{
48 BeginMainFrameAndCommitState();
49 ~BeginMainFrameAndCommitState();
51 unsigned int begin_frame_id
;
52 BeginFrameArgs begin_frame_args
;
53 scoped_ptr
<ScrollAndScaleSet
> scroll_info
;
54 size_t memory_allocation_limit_bytes
;
55 int memory_allocation_priority_cutoff
;
56 bool evicted_ui_resources
;
59 struct MainThreadOnly
{
60 MainThreadOnly(ThreadProxy
* proxy
, int layer_tree_host_id
);
63 const int layer_tree_host_id
;
65 // Set only when SetNeedsAnimate is called.
66 bool animate_requested
;
67 // Set only when SetNeedsCommit is called.
68 bool commit_requested
;
69 // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
70 bool commit_request_sent_to_impl_thread
;
73 bool manage_tiles_pending
;
74 bool can_cancel_commit
;
77 RendererCapabilities renderer_capabilities_main_thread_copy
;
79 scoped_ptr
<BeginMainFrameAndCommitState
> pending_deferred_commit
;
80 base::WeakPtrFactory
<ThreadProxy
> weak_factory
;
83 // Accessed on the main thread, or when main thread is blocked.
84 struct MainThreadOrBlockedMainThread
{
85 explicit MainThreadOrBlockedMainThread(LayerTreeHost
* host
);
86 ~MainThreadOrBlockedMainThread();
88 PrioritizedResourceManager
* contents_texture_manager();
90 LayerTreeHost
* layer_tree_host
;
91 bool commit_waits_for_activation
;
92 bool main_thread_inside_commit
;
94 base::TimeTicks last_monotonic_frame_begin_time
;
97 struct CompositorThreadOnly
{
100 int layer_tree_host_id
,
101 RenderingStatsInstrumentation
* rendering_stats_instrumentation
);
102 ~CompositorThreadOnly();
104 const int layer_tree_host_id
;
106 // Copy of the main thread side contents texture manager for work
107 // that needs to be done on the compositor thread.
108 PrioritizedResourceManager
* contents_texture_manager
;
110 scoped_ptr
<Scheduler
> scheduler
;
112 // Set when the main thread is waiting on a
113 // ScheduledActionSendBeginMainFrame to be issued.
114 CompletionEvent
* begin_main_frame_sent_completion_event
;
116 // Set when the main thread is waiting on a commit to complete.
117 CompletionEvent
* commit_completion_event
;
119 // Set when the main thread is waiting on a pending tree activation.
120 CompletionEvent
* completion_event_for_commit_held_on_tree_activation
;
122 scoped_ptr
<ResourceUpdateController
> current_resource_update_controller
;
124 // Set when the next draw should post DidCommitAndDrawFrame to the main
126 bool next_frame_is_newly_committed_frame
;
130 bool input_throttled_until_commit
;
132 // Set when we freeze animations to avoid checkerboarding.
133 bool animations_frozen_until_next_draw
;
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
<LayerTreeHostImpl
> layer_tree_host_impl
;
145 base::WeakPtrFactory
<ThreadProxy
> weak_factory
;
148 const MainThreadOnly
& main() const;
149 const MainThreadOrBlockedMainThread
& blocked_main() const;
150 const CompositorThreadOnly
& impl() const;
152 // Proxy implementation
153 virtual void FinishAllRendering() OVERRIDE
;
154 virtual bool IsStarted() const OVERRIDE
;
155 virtual void SetLayerTreeHostClientReady() OVERRIDE
;
156 virtual void SetVisible(bool visible
) OVERRIDE
;
157 virtual const RendererCapabilities
& GetRendererCapabilities() const OVERRIDE
;
158 virtual void SetNeedsAnimate() OVERRIDE
;
159 virtual void SetNeedsUpdateLayers() OVERRIDE
;
160 virtual void SetNeedsCommit() OVERRIDE
;
161 virtual void SetNeedsRedraw(const gfx::Rect
& damage_rect
) OVERRIDE
;
162 virtual void SetNextCommitWaitsForActivation() OVERRIDE
;
163 virtual void NotifyInputThrottledUntilCommit() OVERRIDE
;
164 virtual void SetDeferCommits(bool defer_commits
) OVERRIDE
;
165 virtual bool CommitRequested() const OVERRIDE
;
166 virtual bool BeginMainFrameRequested() const OVERRIDE
;
167 virtual void MainThreadHasStoppedFlinging() OVERRIDE
;
168 virtual void Start() OVERRIDE
;
169 virtual void Stop() OVERRIDE
;
170 virtual size_t MaxPartialTextureUpdates() const OVERRIDE
;
171 virtual void ForceSerializeOnSwapBuffers() OVERRIDE
;
172 virtual bool SupportsImplScrolling() const OVERRIDE
;
173 virtual void SetDebugState(const LayerTreeDebugState
& debug_state
) OVERRIDE
;
174 virtual void AsValueInto(base::debug::TracedValue
* value
) const OVERRIDE
;
175 virtual bool MainFrameWillHappenForTesting() OVERRIDE
;
177 // LayerTreeHostImplClient implementation
178 virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE
;
179 virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE
;
180 virtual void CommitVSyncParameters(base::TimeTicks timebase
,
181 base::TimeDelta interval
) OVERRIDE
;
182 virtual void SetEstimatedParentDrawTime(base::TimeDelta draw_time
) OVERRIDE
;
183 virtual void BeginFrame(const BeginFrameArgs
& args
) OVERRIDE
;
184 virtual void SetMaxSwapsPendingOnImplThread(int max
) OVERRIDE
;
185 virtual void DidSwapBuffersOnImplThread() OVERRIDE
;
186 virtual void DidSwapBuffersCompleteOnImplThread() OVERRIDE
;
187 virtual void OnCanDrawStateChanged(bool can_draw
) OVERRIDE
;
188 virtual void NotifyReadyToActivate() OVERRIDE
;
189 // Please call these 3 functions through
190 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
191 // SetNeedsAnimate().
192 virtual void SetNeedsRedrawOnImplThread() OVERRIDE
;
193 virtual void SetNeedsRedrawRectOnImplThread(const gfx::Rect
& dirty_rect
)
195 virtual void SetNeedsAnimateOnImplThread() OVERRIDE
;
196 virtual void SetNeedsManageTilesOnImplThread() OVERRIDE
;
197 virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE
;
198 virtual void SetNeedsCommitOnImplThread() OVERRIDE
;
199 virtual void PostAnimationEventsToMainThreadOnImplThread(
200 scoped_ptr
<AnimationEventsVector
> queue
) OVERRIDE
;
201 virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes
,
204 virtual bool IsInsideDraw() OVERRIDE
;
205 virtual void RenewTreePriority() OVERRIDE
;
206 virtual void PostDelayedScrollbarFadeOnImplThread(
207 const base::Closure
& start_fade
,
208 base::TimeDelta delay
) OVERRIDE
;
209 virtual void DidActivateSyncTree() OVERRIDE
;
210 virtual void DidManageTiles() OVERRIDE
;
212 // SchedulerClient implementation
213 virtual void SetNeedsBeginFrame(bool enable
) OVERRIDE
;
214 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) OVERRIDE
;
215 virtual void ScheduledActionSendBeginMainFrame() OVERRIDE
;
216 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() OVERRIDE
;
217 virtual DrawResult
ScheduledActionDrawAndSwapForced() OVERRIDE
;
218 virtual void ScheduledActionAnimate() OVERRIDE
;
219 virtual void ScheduledActionCommit() OVERRIDE
;
220 virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE
;
221 virtual void ScheduledActionActivateSyncTree() OVERRIDE
;
222 virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE
;
223 virtual void ScheduledActionManageTiles() OVERRIDE
;
224 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) OVERRIDE
;
225 virtual base::TimeDelta
DrawDurationEstimate() OVERRIDE
;
226 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() OVERRIDE
;
227 virtual base::TimeDelta
CommitToActivateDurationEstimate() OVERRIDE
;
228 virtual void DidBeginImplFrameDeadline() OVERRIDE
;
230 // ResourceUpdateControllerClient implementation
231 virtual void ReadyToFinalizeTextureUpdates() OVERRIDE
;
234 ThreadProxy(LayerTreeHost
* layer_tree_host
,
235 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
236 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
239 // Called on main thread.
240 void SetRendererCapabilitiesMainThreadCopy(
241 const RendererCapabilities
& capabilities
);
243 scoped_ptr
<BeginMainFrameAndCommitState
> begin_main_frame_state
);
244 void DidCommitAndDrawFrame();
245 void DidCompleteSwapBuffers();
246 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> queue
);
247 void DidLoseOutputSurface();
248 void CreateAndInitializeOutputSurface();
249 void DidInitializeOutputSurface(bool success
,
250 const RendererCapabilities
& capabilities
);
251 void SendCommitRequestToImplThreadIfNeeded();
253 // Called on impl thread.
254 struct SchedulerStateRequest
;
256 void StartCommitOnImplThread(CompletionEvent
* completion
,
257 ResourceUpdateQueue
* queue
);
258 void BeginMainFrameAbortedOnImplThread(bool did_handle
);
259 void FinishAllRenderingOnImplThread(CompletionEvent
* completion
);
260 void InitializeImplOnImplThread(CompletionEvent
* completion
);
261 void SetLayerTreeHostClientReadyOnImplThread();
262 void SetVisibleOnImplThread(CompletionEvent
* completion
, bool visible
);
263 void UpdateBackgroundAnimateTicking();
264 void HasInitializedOutputSurfaceOnImplThread(
265 CompletionEvent
* completion
,
266 bool* has_initialized_output_surface
);
267 void DeleteContentsTexturesOnImplThread(CompletionEvent
* completion
);
268 void InitializeOutputSurfaceOnImplThread(
269 scoped_ptr
<OutputSurface
> output_surface
);
270 void FinishGLOnImplThread(CompletionEvent
* completion
);
271 void LayerTreeHostClosedOnImplThread(CompletionEvent
* completion
);
272 DrawResult
DrawSwapInternal(bool forced_draw
);
273 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent
* completion
);
274 void MainFrameWillHappenOnImplThreadForTesting(CompletionEvent
* completion
,
275 bool* main_frame_will_happen
);
276 void AsValueOnImplThread(CompletionEvent
* completion
,
277 base::debug::TracedValue
* 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_