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 SetOutputSurface(scoped_ptr<OutputSurface>) OVERRIDE;
156 virtual void SetLayerTreeHostClientReady() OVERRIDE;
157 virtual void SetVisible(bool visible) OVERRIDE;
158 virtual const RendererCapabilities& GetRendererCapabilities() const OVERRIDE;
159 virtual void SetNeedsAnimate() OVERRIDE;
160 virtual void SetNeedsUpdateLayers() OVERRIDE;
161 virtual void SetNeedsCommit() OVERRIDE;
162 virtual void SetNeedsRedraw(const gfx::Rect& damage_rect) OVERRIDE;
163 virtual void SetNextCommitWaitsForActivation() OVERRIDE;
164 virtual void NotifyInputThrottledUntilCommit() OVERRIDE;
165 virtual void SetDeferCommits(bool defer_commits) OVERRIDE;
166 virtual bool CommitRequested() const OVERRIDE;
167 virtual bool BeginMainFrameRequested() const OVERRIDE;
168 virtual void MainThreadHasStoppedFlinging() OVERRIDE;
169 virtual void Start() OVERRIDE;
170 virtual void Stop() OVERRIDE;
171 virtual size_t MaxPartialTextureUpdates() const OVERRIDE;
172 virtual void ForceSerializeOnSwapBuffers() OVERRIDE;
173 virtual bool SupportsImplScrolling() const OVERRIDE;
174 virtual void SetDebugState(const LayerTreeDebugState& debug_state) OVERRIDE;
175 virtual void AsValueInto(base::debug::TracedValue* value) const OVERRIDE;
176 virtual bool MainFrameWillHappenForTesting() OVERRIDE;
178 // LayerTreeHostImplClient implementation
179 virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE;
180 virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE;
181 virtual void CommitVSyncParameters(base::TimeTicks timebase,
182 base::TimeDelta interval) OVERRIDE;
183 virtual void SetEstimatedParentDrawTime(base::TimeDelta draw_time) OVERRIDE;
184 virtual void BeginFrame(const BeginFrameArgs& args) OVERRIDE;
185 virtual void SetMaxSwapsPendingOnImplThread(int max) OVERRIDE;
186 virtual void DidSwapBuffersOnImplThread() OVERRIDE;
187 virtual void DidSwapBuffersCompleteOnImplThread() OVERRIDE;
188 virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE;
189 virtual void NotifyReadyToActivate() OVERRIDE;
190 // Please call these 3 functions through
191 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
192 // SetNeedsAnimate().
193 virtual void SetNeedsRedrawOnImplThread() OVERRIDE;
194 virtual void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect)
196 virtual void SetNeedsAnimateOnImplThread() OVERRIDE;
197 virtual void SetNeedsManageTilesOnImplThread() OVERRIDE;
198 virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE;
199 virtual void SetNeedsCommitOnImplThread() OVERRIDE;
200 virtual void PostAnimationEventsToMainThreadOnImplThread(
201 scoped_ptr<AnimationEventsVector> queue) OVERRIDE;
202 virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
205 virtual bool IsInsideDraw() OVERRIDE;
206 virtual void RenewTreePriority() OVERRIDE;
207 virtual void PostDelayedScrollbarFadeOnImplThread(
208 const base::Closure& start_fade,
209 base::TimeDelta delay) OVERRIDE;
210 virtual void DidActivateSyncTree() OVERRIDE;
211 virtual void DidManageTiles() OVERRIDE;
213 // SchedulerClient implementation
214 virtual void SetNeedsBeginFrame(bool enable) OVERRIDE;
215 virtual void WillBeginImplFrame(const BeginFrameArgs& args) OVERRIDE;
216 virtual void ScheduledActionSendBeginMainFrame() OVERRIDE;
217 virtual DrawResult ScheduledActionDrawAndSwapIfPossible() OVERRIDE;
218 virtual DrawResult ScheduledActionDrawAndSwapForced() OVERRIDE;
219 virtual void ScheduledActionAnimate() OVERRIDE;
220 virtual void ScheduledActionCommit() OVERRIDE;
221 virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE;
222 virtual void ScheduledActionActivateSyncTree() OVERRIDE;
223 virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE;
224 virtual void ScheduledActionManageTiles() OVERRIDE;
225 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) OVERRIDE;
226 virtual base::TimeDelta DrawDurationEstimate() OVERRIDE;
227 virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() OVERRIDE;
228 virtual base::TimeDelta CommitToActivateDurationEstimate() OVERRIDE;
229 virtual void DidBeginImplFrameDeadline() OVERRIDE;
231 // ResourceUpdateControllerClient implementation
232 virtual void ReadyToFinalizeTextureUpdates() OVERRIDE;
235 ThreadProxy(LayerTreeHost* layer_tree_host,
236 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
237 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
240 // Called on main thread.
241 void SetRendererCapabilitiesMainThreadCopy(
242 const RendererCapabilities& capabilities);
244 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
245 void DidCommitAndDrawFrame();
246 void DidCompleteSwapBuffers();
247 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue);
248 void DidLoseOutputSurface();
249 void RequestNewOutputSurface();
250 void DidInitializeOutputSurface(bool success,
251 const RendererCapabilities& capabilities);
252 void SendCommitRequestToImplThreadIfNeeded();
254 // Called on impl thread.
255 struct SchedulerStateRequest;
257 void StartCommitOnImplThread(CompletionEvent* completion,
258 ResourceUpdateQueue* queue);
259 void BeginMainFrameAbortedOnImplThread(bool did_handle);
260 void FinishAllRenderingOnImplThread(CompletionEvent* completion);
261 void InitializeImplOnImplThread(CompletionEvent* completion);
262 void SetLayerTreeHostClientReadyOnImplThread();
263 void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
264 void UpdateBackgroundAnimateTicking();
265 void HasInitializedOutputSurfaceOnImplThread(
266 CompletionEvent* completion,
267 bool* has_initialized_output_surface);
268 void DeleteContentsTexturesOnImplThread(CompletionEvent* completion);
269 void InitializeOutputSurfaceOnImplThread(
270 scoped_ptr<OutputSurface> output_surface);
271 void FinishGLOnImplThread(CompletionEvent* completion);
272 void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
273 DrawResult DrawSwapInternal(bool forced_draw);
274 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
275 void MainFrameWillHappenOnImplThreadForTesting(CompletionEvent* completion,
276 bool* main_frame_will_happen);
277 void AsValueOnImplThread(CompletionEvent* completion,
278 base::debug::TracedValue* state) const;
279 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
280 void MainThreadHasStoppedFlingingOnImplThread();
281 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
282 void SetDebugStateOnImplThread(const LayerTreeDebugState& debug_state);
284 LayerTreeHost* layer_tree_host();
285 const LayerTreeHost* layer_tree_host() const;
287 // Use accessors instead of this variable directly.
288 MainThreadOnly main_thread_only_vars_unsafe_;
289 MainThreadOnly& main();
291 // Use accessors instead of this variable directly.
292 MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_;
293 MainThreadOrBlockedMainThread& blocked_main();
295 // Use accessors instead of this variable directly.
296 CompositorThreadOnly compositor_thread_vars_unsafe_;
297 CompositorThreadOnly& impl();
299 base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
300 base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
302 DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
307 #endif // CC_TREES_THREAD_PROXY_H_