- add sources.
[platform/framework/web/crosswalk.git] / src / cc / trees / thread_proxy.h
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 DidLoseOutputSurfaceOnImplThread() OVERRIDE;
72   virtual void OnSwapBuffersCompleteOnImplThread() OVERRIDE;
73   virtual void BeginImplFrame(const BeginFrameArgs& args) OVERRIDE;
74   virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE;
75   virtual void NotifyReadyToActivate() OVERRIDE;
76   virtual void SetNeedsRedrawOnImplThread() OVERRIDE;
77   virtual void SetNeedsRedrawRectOnImplThread(gfx::Rect dirty_rect) OVERRIDE;
78   virtual void SetNeedsManageTilesOnImplThread() OVERRIDE;
79   virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE;
80   virtual void SetNeedsCommitOnImplThread() OVERRIDE;
81   virtual void PostAnimationEventsToMainThreadOnImplThread(
82       scoped_ptr<AnimationEventsVector> queue,
83       base::Time wall_clock_time) OVERRIDE;
84   virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
85                                                        int priority_cutoff)
86       OVERRIDE;
87   virtual void SendManagedMemoryStats() OVERRIDE;
88   virtual bool IsInsideDraw() OVERRIDE;
89   virtual void RenewTreePriority() OVERRIDE;
90   virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay)
91       OVERRIDE;
92   virtual void DidActivatePendingTree() OVERRIDE;
93
94   // SchedulerClient implementation
95   virtual void SetNeedsBeginImplFrame(bool enable) OVERRIDE;
96   virtual void ScheduledActionSendBeginMainFrame() OVERRIDE;
97   virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapIfPossible()
98       OVERRIDE;
99   virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapForced() OVERRIDE;
100   virtual DrawSwapReadbackResult ScheduledActionDrawAndReadback() OVERRIDE;
101   virtual void ScheduledActionCommit() OVERRIDE;
102   virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE;
103   virtual void ScheduledActionActivatePendingTree() OVERRIDE;
104   virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE;
105   virtual void ScheduledActionAcquireLayerTexturesForMainThread() OVERRIDE;
106   virtual void ScheduledActionManageTiles() OVERRIDE;
107   virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) OVERRIDE;
108   virtual base::TimeDelta DrawDurationEstimate() OVERRIDE;
109   virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() OVERRIDE;
110   virtual base::TimeDelta CommitToActivateDurationEstimate() OVERRIDE;
111   virtual void PostBeginImplFrameDeadline(const base::Closure& closure,
112                                           base::TimeTicks deadline) OVERRIDE;
113   virtual void DidBeginImplFrameDeadline() OVERRIDE;
114
115   // ResourceUpdateControllerClient implementation
116   virtual void ReadyToFinalizeTextureUpdates() OVERRIDE;
117
118  private:
119   ThreadProxy(LayerTreeHost* layer_tree_host,
120               scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
121
122   struct BeginMainFrameAndCommitState {
123     BeginMainFrameAndCommitState();
124     ~BeginMainFrameAndCommitState();
125
126     base::TimeTicks monotonic_frame_begin_time;
127     scoped_ptr<ScrollAndScaleSet> scroll_info;
128     size_t memory_allocation_limit_bytes;
129     int memory_allocation_priority_cutoff;
130     bool evicted_ui_resources;
131   };
132
133   // Called on main thread.
134   void BeginMainFrame(
135       scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
136   void DidCommitAndDrawFrame();
137   void DidCompleteSwapBuffers();
138   void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue,
139                           base::Time wall_clock_time);
140   void DoCreateAndInitializeOutputSurface();
141   // |capabilities| is set only when |success| is true.
142   void OnOutputSurfaceInitializeAttempted(
143       bool success,
144       const RendererCapabilities& capabilities);
145   void SendCommitRequestToImplThreadIfNeeded();
146
147   // Called on impl thread.
148   struct ReadbackRequest;
149   struct CommitPendingRequest;
150   struct SchedulerStateRequest;
151
152   void ForceCommitForReadbackOnImplThread(
153       CompletionEvent* begin_main_frame_sent_completion,
154       ReadbackRequest* request);
155   void StartCommitOnImplThread(
156       CompletionEvent* completion,
157       ResourceUpdateQueue* queue,
158       scoped_refptr<cc::ContextProvider> offscreen_context_provider);
159   void BeginMainFrameAbortedOnImplThread(bool did_handle);
160   void RequestReadbackOnImplThread(ReadbackRequest* request);
161   void FinishAllRenderingOnImplThread(CompletionEvent* completion);
162   void InitializeImplOnImplThread(CompletionEvent* completion);
163   void SetLayerTreeHostClientReadyOnImplThread();
164   void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
165   void UpdateBackgroundAnimateTicking();
166   void HasInitializedOutputSurfaceOnImplThread(
167       CompletionEvent* completion,
168       bool* has_initialized_output_surface);
169   void InitializeOutputSurfaceOnImplThread(
170       CompletionEvent* completion,
171       scoped_ptr<OutputSurface> output_surface,
172       scoped_refptr<ContextProvider> offscreen_context_provider,
173       bool* success,
174       RendererCapabilities* capabilities);
175   void FinishGLOnImplThread(CompletionEvent* completion);
176   void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
177   void AcquireLayerTexturesForMainThreadOnImplThread(
178       CompletionEvent* completion);
179   DrawSwapReadbackResult DrawSwapReadbackInternal(bool forced_draw,
180                                                   bool swap_requested,
181                                                   bool readback_requested);
182   void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
183   void CheckOutputSurfaceStatusOnImplThread();
184   void CommitPendingOnImplThreadForTesting(CommitPendingRequest* request);
185   void SchedulerStateAsValueOnImplThreadForTesting(
186       SchedulerStateRequest* request);
187   void AsValueOnImplThread(CompletionEvent* completion,
188                            base::DictionaryValue* state) const;
189   void RenewTreePriorityOnImplThread();
190   void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
191   void StartScrollbarAnimationOnImplThread();
192   void MainThreadHasStoppedFlingingOnImplThread();
193   void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
194
195   // Accessed on main thread only.
196
197   // Set only when SetNeedsAnimate is called.
198   bool animate_requested_;
199   // Set only when SetNeedsCommit is called.
200   bool commit_requested_;
201   // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
202   bool commit_request_sent_to_impl_thread_;
203   // Set by BeginMainFrame
204   bool created_offscreen_context_provider_;
205   base::CancelableClosure output_surface_creation_callback_;
206   LayerTreeHost* layer_tree_host_;
207   RendererCapabilities renderer_capabilities_main_thread_copy_;
208   bool started_;
209   bool textures_acquired_;
210   bool in_composite_and_readback_;
211   bool manage_tiles_pending_;
212   // Weak pointer to use when posting tasks to the impl thread.
213   base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
214   // Holds the first output surface passed from Start. Should not be used for
215   // anything else.
216   scoped_ptr<OutputSurface> first_output_surface_;
217
218   // Accessed on the main thread, or when main thread is blocked.
219   bool commit_waits_for_activation_;
220   bool inside_commit_;
221
222   scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_;
223
224   scoped_ptr<Scheduler> scheduler_on_impl_thread_;
225
226   // Set when the main thread is waiting on a
227   // ScheduledActionSendBeginMainFrame to be issued.
228   CompletionEvent*
229       begin_main_frame_sent_completion_event_on_impl_thread_;
230
231   // Set when the main thread is waiting on a readback.
232   ReadbackRequest* readback_request_on_impl_thread_;
233
234   // Set when the main thread is waiting on a commit to complete.
235   CompletionEvent* commit_completion_event_on_impl_thread_;
236
237   // Set when the main thread is waiting on a pending tree activation.
238   CompletionEvent* completion_event_for_commit_held_on_tree_activation_;
239
240   // Set when the main thread is waiting on layers to be drawn.
241   CompletionEvent* texture_acquisition_completion_event_on_impl_thread_;
242
243   scoped_ptr<ResourceUpdateController>
244       current_resource_update_controller_on_impl_thread_;
245
246   // Set when the next draw should post DidCommitAndDrawFrame to the main
247   // thread.
248   bool next_frame_is_newly_committed_frame_on_impl_thread_;
249
250   bool throttle_frame_production_;
251   bool begin_impl_frame_scheduling_enabled_;
252   bool using_synchronous_renderer_compositor_;
253
254   bool inside_draw_;
255
256   bool can_cancel_commit_;
257
258   bool defer_commits_;
259   bool input_throttled_until_commit_;
260   scoped_ptr<BeginMainFrameAndCommitState> pending_deferred_commit_;
261
262   base::TimeTicks smoothness_takes_priority_expiration_time_;
263   bool renew_tree_priority_on_impl_thread_pending_;
264
265   RollingTimeDeltaHistory draw_duration_history_;
266   RollingTimeDeltaHistory begin_main_frame_to_commit_duration_history_;
267   RollingTimeDeltaHistory commit_to_activate_duration_history_;
268
269   // Used for computing samples added to
270   // begin_main_frame_to_commit_duration_history_ and
271   // activation_duration_history_.
272   base::TimeTicks begin_main_frame_sent_time_;
273   base::TimeTicks commit_complete_time_;
274
275   base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
276   base::WeakPtrFactory<ThreadProxy> weak_factory_on_impl_thread_;
277   base::WeakPtrFactory<ThreadProxy> weak_factory_;
278
279   DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
280 };
281
282 }  // namespace cc
283
284 #endif  // CC_TREES_THREAD_PROXY_H_