• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CC_TREES_THREAD_PROXY_H_
6 #define CC_TREES_THREAD_PROXY_H_
7 
8 #include <string>
9 
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"
20 
21 namespace base { class SingleThreadTaskRunner; }
22 
23 namespace cc {
24 
25 class ContextProvider;
26 class InputHandlerClient;
27 class LayerTreeHost;
28 class ResourceUpdateQueue;
29 class Scheduler;
30 class ScopedThreadProxy;
31 
32 class ThreadProxy : public Proxy,
33                     LayerTreeHostImplClient,
34                     SchedulerClient,
35                     ResourceUpdateControllerClient {
36  public:
37   static scoped_ptr<Proxy> Create(
38       LayerTreeHost* layer_tree_host,
39       scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
40 
41   virtual ~ThreadProxy();
42 
43   // Proxy implementation
44   virtual bool CompositeAndReadback(void* pixels, gfx::Rect rect) OVERRIDE;
45   virtual void FinishAllRendering() OVERRIDE;
46   virtual bool IsStarted() const OVERRIDE;
47   virtual void SetLayerTreeHostClientReady() OVERRIDE;
48   virtual void SetVisible(bool visible) OVERRIDE;
49   virtual void CreateAndInitializeOutputSurface() OVERRIDE;
50   virtual const RendererCapabilities& GetRendererCapabilities() const OVERRIDE;
51   virtual void SetNeedsAnimate() OVERRIDE;
52   virtual void SetNeedsUpdateLayers() OVERRIDE;
53   virtual void SetNeedsCommit() OVERRIDE;
54   virtual void SetNeedsRedraw(gfx::Rect damage_rect) OVERRIDE;
55   virtual void SetNextCommitWaitsForActivation() OVERRIDE;
56   virtual void NotifyInputThrottledUntilCommit() OVERRIDE;
57   virtual void SetDeferCommits(bool defer_commits) OVERRIDE;
58   virtual bool CommitRequested() const OVERRIDE;
59   virtual bool BeginMainFrameRequested() const OVERRIDE;
60   virtual void MainThreadHasStoppedFlinging() OVERRIDE;
61   virtual void Start(scoped_ptr<OutputSurface> first_output_surface) OVERRIDE;
62   virtual void Stop() OVERRIDE;
63   virtual size_t MaxPartialTextureUpdates() const OVERRIDE;
64   virtual void AcquireLayerTextures() OVERRIDE;
65   virtual void ForceSerializeOnSwapBuffers() OVERRIDE;
66   virtual scoped_ptr<base::Value> AsValue() const OVERRIDE;
67   virtual bool CommitPendingForTesting() OVERRIDE;
68   virtual scoped_ptr<base::Value> SchedulerStateAsValueForTesting() OVERRIDE;
69 
70   // LayerTreeHostImplClient implementation
71   virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE;
72   virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE;
DidSwapBuffersOnImplThread()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(gfx::Rect dirty_rect) OVERRIDE;
82   virtual void SetNeedsManageTilesOnImplThread() OVERRIDE;
83   virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE;
84   virtual void SetNeedsCommitOnImplThread() OVERRIDE;
85   virtual void PostAnimationEventsToMainThreadOnImplThread(
86       scoped_ptr<AnimationEventsVector> queue,
87       base::Time wall_clock_time) OVERRIDE;
88   virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
89                                                        int priority_cutoff)
90       OVERRIDE;
91   virtual void SendManagedMemoryStats() OVERRIDE;
92   virtual bool IsInsideDraw() OVERRIDE;
93   virtual void RenewTreePriority() OVERRIDE;
94   virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay)
95       OVERRIDE;
96   virtual void DidActivatePendingTree() OVERRIDE;
97   virtual void DidManageTiles() OVERRIDE;
98 
99   // SchedulerClient implementation
100   virtual void SetNeedsBeginImplFrame(bool enable) OVERRIDE;
101   virtual void ScheduledActionSendBeginMainFrame() OVERRIDE;
102   virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapIfPossible()
103       OVERRIDE;
104   virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapForced() OVERRIDE;
105   virtual DrawSwapReadbackResult ScheduledActionDrawAndReadback() OVERRIDE;
106   virtual void ScheduledActionCommit() OVERRIDE;
107   virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE;
108   virtual void ScheduledActionActivatePendingTree() OVERRIDE;
109   virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE;
110   virtual void ScheduledActionAcquireLayerTexturesForMainThread() OVERRIDE;
111   virtual void ScheduledActionManageTiles() OVERRIDE;
112   virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) OVERRIDE;
113   virtual base::TimeDelta DrawDurationEstimate() OVERRIDE;
114   virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() OVERRIDE;
115   virtual base::TimeDelta CommitToActivateDurationEstimate() OVERRIDE;
116   virtual void PostBeginImplFrameDeadline(const base::Closure& closure,
117                                           base::TimeTicks deadline) OVERRIDE;
118   virtual void DidBeginImplFrameDeadline() OVERRIDE;
119 
120   // ResourceUpdateControllerClient implementation
121   virtual void ReadyToFinalizeTextureUpdates() OVERRIDE;
122 
123  private:
124   ThreadProxy(LayerTreeHost* layer_tree_host,
125               scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
126 
127   struct BeginMainFrameAndCommitState {
128     BeginMainFrameAndCommitState();
129     ~BeginMainFrameAndCommitState();
130 
131     base::TimeTicks monotonic_frame_begin_time;
132     scoped_ptr<ScrollAndScaleSet> scroll_info;
133     size_t memory_allocation_limit_bytes;
134     int memory_allocation_priority_cutoff;
135     bool evicted_ui_resources;
136   };
137 
138   // Called on main thread.
139   void SetRendererCapabilitiesMainThreadCopy(
140       const RendererCapabilities& capabilities);
141   void BeginMainFrame(
142       scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
143   void DidCommitAndDrawFrame();
144   void DidCompleteSwapBuffers();
145   void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue,
146                           base::Time wall_clock_time);
147   void DoCreateAndInitializeOutputSurface();
148   // |capabilities| is set only when |success| is true.
149   void OnOutputSurfaceInitializeAttempted(
150       bool success,
151       const RendererCapabilities& capabilities);
152   void SendCommitRequestToImplThreadIfNeeded();
153 
154   // Called on impl thread.
155   struct ReadbackRequest;
156   struct CommitPendingRequest;
157   struct SchedulerStateRequest;
158 
159   void ForceCommitForReadbackOnImplThread(
160       CompletionEvent* begin_main_frame_sent_completion,
161       ReadbackRequest* request);
162   void StartCommitOnImplThread(
163       CompletionEvent* completion,
164       ResourceUpdateQueue* queue,
165       scoped_refptr<ContextProvider> offscreen_context_provider);
166   void BeginMainFrameAbortedOnImplThread(bool did_handle);
167   void RequestReadbackOnImplThread(ReadbackRequest* request);
168   void FinishAllRenderingOnImplThread(CompletionEvent* completion);
169   void InitializeImplOnImplThread(CompletionEvent* completion);
170   void SetLayerTreeHostClientReadyOnImplThread();
171   void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
172   void UpdateBackgroundAnimateTicking();
173   void HasInitializedOutputSurfaceOnImplThread(
174       CompletionEvent* completion,
175       bool* has_initialized_output_surface);
176   void InitializeOutputSurfaceOnImplThread(
177       CompletionEvent* completion,
178       scoped_ptr<OutputSurface> output_surface,
179       scoped_refptr<ContextProvider> offscreen_context_provider,
180       bool* success,
181       RendererCapabilities* capabilities);
182   void FinishGLOnImplThread(CompletionEvent* completion);
183   void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
184   void AcquireLayerTexturesForMainThreadOnImplThread(
185       CompletionEvent* completion);
186   DrawSwapReadbackResult DrawSwapReadbackInternal(bool forced_draw,
187                                                   bool swap_requested,
188                                                   bool readback_requested);
189   void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
190   void CheckOutputSurfaceStatusOnImplThread();
191   void CommitPendingOnImplThreadForTesting(CommitPendingRequest* request);
192   void SchedulerStateAsValueOnImplThreadForTesting(
193       SchedulerStateRequest* request);
194   void AsValueOnImplThread(CompletionEvent* completion,
195                            base::DictionaryValue* state) const;
196   void RenewTreePriorityOnImplThread();
197   void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
198   void StartScrollbarAnimationOnImplThread();
199   void MainThreadHasStoppedFlingingOnImplThread();
200   void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
201   LayerTreeHost* layer_tree_host();
202   const LayerTreeHost* layer_tree_host() const;
203   PrioritizedResourceManager* contents_texture_manager_on_main_thread();
204   PrioritizedResourceManager* contents_texture_manager_on_impl_thread();
205 
206   // Accessed on main thread only.
207 
208   // Set only when SetNeedsAnimate is called.
209   bool animate_requested_;
210   // Set only when SetNeedsCommit is called.
211   bool commit_requested_;
212   // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
213   bool commit_request_sent_to_impl_thread_;
214   // Set by BeginMainFrame
215   bool created_offscreen_context_provider_;
216   base::CancelableClosure output_surface_creation_callback_;
217   // Don't use this variable directly, go through layer_tree_host() to ensure it
218   // is only used on the main thread or if the main thread is blocked.
219   LayerTreeHost* layer_tree_host_unsafe_;
220   // Use one of the contents_texture_manager_on functions above instead of using
221   // this variable directly.
222   PrioritizedResourceManager* contents_texture_manager_unsafe_;
223   RendererCapabilities renderer_capabilities_main_thread_copy_;
224   bool started_;
225   bool textures_acquired_;
226   bool in_composite_and_readback_;
227   bool manage_tiles_pending_;
228   // Weak pointer to use when posting tasks to the impl thread.
229   base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
230   // Holds the first output surface passed from Start. Should not be used for
231   // anything else.
232   scoped_ptr<OutputSurface> first_output_surface_;
233 
234   // Accessed on the main thread, or when main thread is blocked.
235   bool commit_waits_for_activation_;
236   bool inside_commit_;
237 
238   scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_;
239 
240   scoped_ptr<Scheduler> scheduler_on_impl_thread_;
241 
242   // Set when the main thread is waiting on a
243   // ScheduledActionSendBeginMainFrame to be issued.
244   CompletionEvent*
245       begin_main_frame_sent_completion_event_on_impl_thread_;
246 
247   // Set when the main thread is waiting on a readback.
248   ReadbackRequest* readback_request_on_impl_thread_;
249 
250   // Set when the main thread is waiting on a commit to complete.
251   CompletionEvent* commit_completion_event_on_impl_thread_;
252 
253   // Set when the main thread is waiting on a pending tree activation.
254   CompletionEvent* completion_event_for_commit_held_on_tree_activation_;
255 
256   // Set when the main thread is waiting on layers to be drawn.
257   CompletionEvent* texture_acquisition_completion_event_on_impl_thread_;
258 
259   scoped_ptr<ResourceUpdateController>
260       current_resource_update_controller_on_impl_thread_;
261 
262   // Set when the next draw should post DidCommitAndDrawFrame to the main
263   // thread.
264   bool next_frame_is_newly_committed_frame_on_impl_thread_;
265 
266   bool throttle_frame_production_;
267   bool begin_impl_frame_scheduling_enabled_;
268   bool using_synchronous_renderer_compositor_;
269 
270   bool inside_draw_;
271 
272   bool can_cancel_commit_;
273 
274   bool defer_commits_;
275   bool input_throttled_until_commit_;
276   scoped_ptr<BeginMainFrameAndCommitState> pending_deferred_commit_;
277 
278   base::TimeTicks smoothness_takes_priority_expiration_time_;
279   bool renew_tree_priority_on_impl_thread_pending_;
280 
281   RollingTimeDeltaHistory draw_duration_history_;
282   RollingTimeDeltaHistory begin_main_frame_to_commit_duration_history_;
283   RollingTimeDeltaHistory commit_to_activate_duration_history_;
284 
285   // Used for computing samples added to
286   // begin_main_frame_to_commit_duration_history_ and
287   // activation_duration_history_.
288   base::TimeTicks begin_main_frame_sent_time_;
289   base::TimeTicks commit_complete_time_;
290 
291   base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
292   base::WeakPtrFactory<ThreadProxy> weak_factory_on_impl_thread_;
293   base::WeakPtrFactory<ThreadProxy> weak_factory_;
294 
295   const int layer_tree_host_id_;
296 
297   DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
298 };
299 
300 }  // namespace cc
301 
302 #endif  // CC_TREES_THREAD_PROXY_H_
303