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/resources/resource_update_controller.h"
16 #include "cc/scheduler/rolling_time_delta_history.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
{ class SingleThreadTaskRunner
; }
25 class ContextProvider
;
26 class InputHandlerClient
;
28 class ResourceUpdateQueue
;
30 class ScopedThreadProxy
;
32 class ThreadProxy
: public Proxy
,
33 LayerTreeHostImplClient
,
35 ResourceUpdateControllerClient
{
37 static scoped_ptr
<Proxy
> Create(
38 LayerTreeHost
* layer_tree_host
,
39 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
41 virtual ~ThreadProxy();
43 // Proxy implementation
44 virtual bool CompositeAndReadback(void* pixels
,
45 const gfx::Rect
& rect
) OVERRIDE
;
46 virtual void FinishAllRendering() OVERRIDE
;
47 virtual bool IsStarted() const OVERRIDE
;
48 virtual void SetLayerTreeHostClientReady() OVERRIDE
;
49 virtual void SetVisible(bool visible
) OVERRIDE
;
50 virtual void CreateAndInitializeOutputSurface() OVERRIDE
;
51 virtual const RendererCapabilities
& GetRendererCapabilities() const OVERRIDE
;
52 virtual void SetNeedsAnimate() OVERRIDE
;
53 virtual void SetNeedsUpdateLayers() OVERRIDE
;
54 virtual void SetNeedsCommit() OVERRIDE
;
55 virtual void SetNeedsRedraw(const gfx::Rect
& damage_rect
) OVERRIDE
;
56 virtual void SetNextCommitWaitsForActivation() OVERRIDE
;
57 virtual void NotifyInputThrottledUntilCommit() OVERRIDE
;
58 virtual void SetDeferCommits(bool defer_commits
) OVERRIDE
;
59 virtual bool CommitRequested() const OVERRIDE
;
60 virtual bool BeginMainFrameRequested() const OVERRIDE
;
61 virtual void MainThreadHasStoppedFlinging() OVERRIDE
;
62 virtual void Start() OVERRIDE
;
63 virtual void Stop() OVERRIDE
;
64 virtual size_t MaxPartialTextureUpdates() const OVERRIDE
;
65 virtual void AcquireLayerTextures() OVERRIDE
;
66 virtual void ForceSerializeOnSwapBuffers() OVERRIDE
;
67 virtual scoped_ptr
<base::Value
> AsValue() const OVERRIDE
;
68 virtual bool CommitPendingForTesting() OVERRIDE
;
69 virtual scoped_ptr
<base::Value
> SchedulerStateAsValueForTesting() OVERRIDE
;
71 // LayerTreeHostImplClient implementation
72 virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE
;
73 virtual void DidSwapBuffersOnImplThread() OVERRIDE
{}
74 virtual void OnSwapBuffersCompleteOnImplThread() OVERRIDE
;
75 virtual void BeginImplFrame(const BeginFrameArgs
& args
) OVERRIDE
;
76 virtual void OnCanDrawStateChanged(bool can_draw
) OVERRIDE
;
77 virtual void NotifyReadyToActivate() OVERRIDE
;
78 // Please call these 2 functions through
79 // LayerTreeHostImpl's SetNeedsRedraw() and SetNeedsRedrawRect().
80 virtual void SetNeedsRedrawOnImplThread() OVERRIDE
;
81 virtual void SetNeedsRedrawRectOnImplThread(
82 const gfx::Rect
& dirty_rect
) OVERRIDE
;
83 virtual void SetNeedsManageTilesOnImplThread() OVERRIDE
;
84 virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE
;
85 virtual void SetNeedsCommitOnImplThread() OVERRIDE
;
86 virtual void PostAnimationEventsToMainThreadOnImplThread(
87 scoped_ptr
<AnimationEventsVector
> queue
,
88 base::Time wall_clock_time
) OVERRIDE
;
89 virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes
,
92 virtual void SendManagedMemoryStats() OVERRIDE
;
93 virtual bool IsInsideDraw() OVERRIDE
;
94 virtual void RenewTreePriority() OVERRIDE
;
95 virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay
)
97 virtual void DidActivatePendingTree() OVERRIDE
;
98 virtual void DidManageTiles() OVERRIDE
;
100 // SchedulerClient implementation
101 virtual void SetNeedsBeginImplFrame(bool enable
) OVERRIDE
;
102 virtual void ScheduledActionSendBeginMainFrame() OVERRIDE
;
103 virtual DrawSwapReadbackResult
ScheduledActionDrawAndSwapIfPossible()
105 virtual DrawSwapReadbackResult
ScheduledActionDrawAndSwapForced() OVERRIDE
;
106 virtual DrawSwapReadbackResult
ScheduledActionDrawAndReadback() OVERRIDE
;
107 virtual void ScheduledActionCommit() OVERRIDE
;
108 virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE
;
109 virtual void ScheduledActionActivatePendingTree() OVERRIDE
;
110 virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE
;
111 virtual void ScheduledActionAcquireLayerTexturesForMainThread() OVERRIDE
;
112 virtual void ScheduledActionManageTiles() OVERRIDE
;
113 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) OVERRIDE
;
114 virtual base::TimeDelta
DrawDurationEstimate() OVERRIDE
;
115 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() OVERRIDE
;
116 virtual base::TimeDelta
CommitToActivateDurationEstimate() OVERRIDE
;
117 virtual void PostBeginImplFrameDeadline(const base::Closure
& closure
,
118 base::TimeTicks deadline
) OVERRIDE
;
119 virtual void DidBeginImplFrameDeadline() OVERRIDE
;
121 // ResourceUpdateControllerClient implementation
122 virtual void ReadyToFinalizeTextureUpdates() OVERRIDE
;
125 ThreadProxy(LayerTreeHost
* layer_tree_host
,
126 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
);
128 struct BeginMainFrameAndCommitState
{
129 BeginMainFrameAndCommitState();
130 ~BeginMainFrameAndCommitState();
132 base::TimeTicks monotonic_frame_begin_time
;
133 scoped_ptr
<ScrollAndScaleSet
> scroll_info
;
134 size_t memory_allocation_limit_bytes
;
135 int memory_allocation_priority_cutoff
;
136 bool evicted_ui_resources
;
139 // Called on main thread.
141 scoped_ptr
<BeginMainFrameAndCommitState
> begin_main_frame_state
);
142 void DidCommitAndDrawFrame();
143 void DidCompleteSwapBuffers();
144 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> queue
,
145 base::Time wall_clock_time
);
146 void DoCreateAndInitializeOutputSurface();
147 // |capabilities| is set only when |success| is true.
148 void OnOutputSurfaceInitializeAttempted(
150 const RendererCapabilities
& capabilities
);
151 void SendCommitRequestToImplThreadIfNeeded();
153 // Called on impl thread.
154 struct ReadbackRequest
;
155 struct CommitPendingRequest
;
156 struct SchedulerStateRequest
;
158 void ForceCommitForReadbackOnImplThread(
159 CompletionEvent
* begin_main_frame_sent_completion
,
160 ReadbackRequest
* request
);
161 void StartCommitOnImplThread(
162 CompletionEvent
* completion
,
163 ResourceUpdateQueue
* queue
,
164 scoped_refptr
<ContextProvider
> offscreen_context_provider
);
165 void BeginMainFrameAbortedOnImplThread(bool did_handle
);
166 void RequestReadbackOnImplThread(ReadbackRequest
* request
);
167 void FinishAllRenderingOnImplThread(CompletionEvent
* completion
);
168 void InitializeImplOnImplThread(CompletionEvent
* completion
);
169 void SetLayerTreeHostClientReadyOnImplThread();
170 void SetVisibleOnImplThread(CompletionEvent
* completion
, bool visible
);
171 void UpdateBackgroundAnimateTicking();
172 void HasInitializedOutputSurfaceOnImplThread(
173 CompletionEvent
* completion
,
174 bool* has_initialized_output_surface
);
175 void InitializeOutputSurfaceOnImplThread(
176 CompletionEvent
* completion
,
177 scoped_ptr
<OutputSurface
> output_surface
,
178 scoped_refptr
<ContextProvider
> offscreen_context_provider
,
180 RendererCapabilities
* capabilities
);
181 void FinishGLOnImplThread(CompletionEvent
* completion
);
182 void LayerTreeHostClosedOnImplThread(CompletionEvent
* completion
);
183 void AcquireLayerTexturesForMainThreadOnImplThread(
184 CompletionEvent
* completion
);
185 DrawSwapReadbackResult
DrawSwapReadbackInternal(bool forced_draw
,
187 bool readback_requested
);
188 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent
* completion
);
189 void CheckOutputSurfaceStatusOnImplThread();
190 void CommitPendingOnImplThreadForTesting(CommitPendingRequest
* request
);
191 void SchedulerStateAsValueOnImplThreadForTesting(
192 SchedulerStateRequest
* request
);
193 void AsValueOnImplThread(CompletionEvent
* completion
,
194 base::DictionaryValue
* state
) const;
195 void RenewTreePriorityOnImplThread();
196 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile
);
197 void StartScrollbarAnimationOnImplThread();
198 void MainThreadHasStoppedFlingingOnImplThread();
199 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled
);
200 LayerTreeHost
* layer_tree_host();
201 const LayerTreeHost
* layer_tree_host() const;
202 PrioritizedResourceManager
* contents_texture_manager_on_main_thread();
203 PrioritizedResourceManager
* contents_texture_manager_on_impl_thread();
205 // Accessed on main thread only.
207 // Set only when SetNeedsAnimate is called.
208 bool animate_requested_
;
209 // Set only when SetNeedsCommit is called.
210 bool commit_requested_
;
211 // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
212 bool commit_request_sent_to_impl_thread_
;
213 // Set by BeginMainFrame
214 bool created_offscreen_context_provider_
;
215 base::CancelableClosure output_surface_creation_callback_
;
216 // Don't use this variable directly, go through layer_tree_host() to ensure it
217 // is only used on the main thread or if the main thread is blocked.
218 LayerTreeHost
* layer_tree_host_unsafe_
;
219 // Use one of the contents_texture_manager_on functions above instead of using
220 // this variable directly.
221 PrioritizedResourceManager
* contents_texture_manager_unsafe_
;
222 RendererCapabilities renderer_capabilities_main_thread_copy_
;
224 bool textures_acquired_
;
225 bool in_composite_and_readback_
;
226 bool manage_tiles_pending_
;
227 // Weak pointer to use when posting tasks to the impl thread.
228 base::WeakPtr
<ThreadProxy
> impl_thread_weak_ptr_
;
230 // Accessed on the main thread, or when main thread is blocked.
231 bool commit_waits_for_activation_
;
234 scoped_ptr
<LayerTreeHostImpl
> layer_tree_host_impl_
;
236 scoped_ptr
<Scheduler
> scheduler_on_impl_thread_
;
238 // Set when the main thread is waiting on a
239 // ScheduledActionSendBeginMainFrame to be issued.
241 begin_main_frame_sent_completion_event_on_impl_thread_
;
243 // Set when the main thread is waiting on a readback.
244 ReadbackRequest
* readback_request_on_impl_thread_
;
246 // Set when the main thread is waiting on a commit to complete.
247 CompletionEvent
* commit_completion_event_on_impl_thread_
;
249 // Set when the main thread is waiting on a pending tree activation.
250 CompletionEvent
* completion_event_for_commit_held_on_tree_activation_
;
252 // Set when the main thread is waiting on layers to be drawn.
253 CompletionEvent
* texture_acquisition_completion_event_on_impl_thread_
;
255 scoped_ptr
<ResourceUpdateController
>
256 current_resource_update_controller_on_impl_thread_
;
258 // Set when the next draw should post DidCommitAndDrawFrame to the main
260 bool next_frame_is_newly_committed_frame_on_impl_thread_
;
262 bool throttle_frame_production_
;
263 bool begin_impl_frame_scheduling_enabled_
;
264 bool using_synchronous_renderer_compositor_
;
268 bool can_cancel_commit_
;
271 bool input_throttled_until_commit_
;
272 scoped_ptr
<BeginMainFrameAndCommitState
> pending_deferred_commit_
;
274 base::TimeTicks smoothness_takes_priority_expiration_time_
;
275 bool renew_tree_priority_on_impl_thread_pending_
;
277 RollingTimeDeltaHistory draw_duration_history_
;
278 RollingTimeDeltaHistory begin_main_frame_to_commit_duration_history_
;
279 RollingTimeDeltaHistory commit_to_activate_duration_history_
;
281 // Used for computing samples added to
282 // begin_main_frame_to_commit_duration_history_ and
283 // activation_duration_history_.
284 base::TimeTicks begin_main_frame_sent_time_
;
285 base::TimeTicks commit_complete_time_
;
287 base::WeakPtr
<ThreadProxy
> main_thread_weak_ptr_
;
288 base::WeakPtrFactory
<ThreadProxy
> weak_factory_on_impl_thread_
;
289 base::WeakPtrFactory
<ThreadProxy
> weak_factory_
;
291 const int layer_tree_host_id_
;
293 DISALLOW_COPY_AND_ASSIGN(ThreadProxy
);
298 #endif // CC_TREES_THREAD_PROXY_H_