b0275b91969c65595de30ff284929f50b0bb0341
[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/scheduler.h"
17 #include "cc/trees/layer_tree_host_impl.h"
18 #include "cc/trees/proxy.h"
19 #include "cc/trees/proxy_timing_history.h"
20
21 namespace base {
22 class SingleThreadTaskRunner;
23 }
24
25 namespace cc {
26
27 class ContextProvider;
28 class InputHandlerClient;
29 class LayerTreeHost;
30 class ResourceUpdateQueue;
31 class Scheduler;
32 class ScopedThreadProxy;
33
34 class ThreadProxy : public Proxy,
35                     LayerTreeHostImplClient,
36                     SchedulerClient,
37                     ResourceUpdateControllerClient {
38  public:
39   static scoped_ptr<Proxy> Create(
40       LayerTreeHost* layer_tree_host,
41       scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
42
43   virtual ~ThreadProxy();
44
45   // Proxy implementation
46   virtual bool CompositeAndReadback(void* pixels,
47                                     const gfx::Rect& rect) OVERRIDE;
48   virtual void FinishAllRendering() OVERRIDE;
49   virtual bool IsStarted() const OVERRIDE;
50   virtual void SetLayerTreeHostClientReady() OVERRIDE;
51   virtual void SetVisible(bool visible) OVERRIDE;
52   virtual void CreateAndInitializeOutputSurface() OVERRIDE;
53   virtual const RendererCapabilities& GetRendererCapabilities() const OVERRIDE;
54   virtual void SetNeedsAnimate() OVERRIDE;
55   virtual void SetNeedsUpdateLayers() OVERRIDE;
56   virtual void SetNeedsCommit() OVERRIDE;
57   virtual void SetNeedsRedraw(const gfx::Rect& damage_rect) OVERRIDE;
58   virtual void SetNextCommitWaitsForActivation() OVERRIDE;
59   virtual void NotifyInputThrottledUntilCommit() OVERRIDE;
60   virtual void SetDeferCommits(bool defer_commits) OVERRIDE;
61   virtual bool CommitRequested() const OVERRIDE;
62   virtual bool BeginMainFrameRequested() const OVERRIDE;
63   virtual void MainThreadHasStoppedFlinging() OVERRIDE;
64   virtual void Start() OVERRIDE;
65   virtual void Stop() OVERRIDE;
66   virtual size_t MaxPartialTextureUpdates() const OVERRIDE;
67   virtual void AcquireLayerTextures() OVERRIDE;
68   virtual void ForceSerializeOnSwapBuffers() OVERRIDE;
69   virtual scoped_ptr<base::Value> AsValue() const OVERRIDE;
70   virtual bool CommitPendingForTesting() OVERRIDE;
71   virtual scoped_ptr<base::Value> SchedulerStateAsValueForTesting() OVERRIDE;
72
73   // LayerTreeHostImplClient implementation
74   virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE;
75   virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE;
76   virtual void DidSwapBuffersOnImplThread() OVERRIDE {}
77   virtual void OnSwapBuffersCompleteOnImplThread() OVERRIDE;
78   virtual void BeginImplFrame(const BeginFrameArgs& args) OVERRIDE;
79   virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE;
80   virtual void NotifyReadyToActivate() OVERRIDE;
81   // Please call these 2 functions through
82   // LayerTreeHostImpl's SetNeedsRedraw() and SetNeedsRedrawRect().
83   virtual void SetNeedsRedrawOnImplThread() OVERRIDE;
84   virtual void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect)
85       OVERRIDE;
86   virtual void SetNeedsManageTilesOnImplThread() OVERRIDE;
87   virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE;
88   virtual void SetNeedsCommitOnImplThread() OVERRIDE;
89   virtual void PostAnimationEventsToMainThreadOnImplThread(
90       scoped_ptr<AnimationEventsVector> queue,
91       base::Time wall_clock_time) OVERRIDE;
92   virtual bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
93                                                        int priority_cutoff)
94       OVERRIDE;
95   virtual void SendManagedMemoryStats() OVERRIDE;
96   virtual bool IsInsideDraw() OVERRIDE;
97   virtual void RenewTreePriority() OVERRIDE;
98   virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay)
99       OVERRIDE;
100   virtual void DidActivatePendingTree() OVERRIDE;
101   virtual void DidManageTiles() OVERRIDE;
102
103   // SchedulerClient implementation
104   virtual void SetNeedsBeginImplFrame(bool enable) OVERRIDE;
105   virtual void ScheduledActionSendBeginMainFrame() OVERRIDE;
106   virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapIfPossible()
107       OVERRIDE;
108   virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapForced() OVERRIDE;
109   virtual DrawSwapReadbackResult ScheduledActionDrawAndReadback() OVERRIDE;
110   virtual void ScheduledActionCommit() OVERRIDE;
111   virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE;
112   virtual void ScheduledActionActivatePendingTree() OVERRIDE;
113   virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE;
114   virtual void ScheduledActionAcquireLayerTexturesForMainThread() OVERRIDE;
115   virtual void ScheduledActionManageTiles() OVERRIDE;
116   virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) OVERRIDE;
117   virtual base::TimeDelta DrawDurationEstimate() OVERRIDE;
118   virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() OVERRIDE;
119   virtual base::TimeDelta CommitToActivateDurationEstimate() OVERRIDE;
120   virtual void PostBeginImplFrameDeadline(const base::Closure& closure,
121                                           base::TimeTicks deadline) OVERRIDE;
122   virtual void DidBeginImplFrameDeadline() OVERRIDE;
123
124   // ResourceUpdateControllerClient implementation
125   virtual void ReadyToFinalizeTextureUpdates() OVERRIDE;
126
127  private:
128   ThreadProxy(LayerTreeHost* layer_tree_host,
129               scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
130
131   struct BeginMainFrameAndCommitState {
132     BeginMainFrameAndCommitState();
133     ~BeginMainFrameAndCommitState();
134
135     base::TimeTicks monotonic_frame_begin_time;
136     scoped_ptr<ScrollAndScaleSet> scroll_info;
137     size_t memory_allocation_limit_bytes;
138     int memory_allocation_priority_cutoff;
139     bool evicted_ui_resources;
140   };
141
142   // Called on main thread.
143   void SetRendererCapabilitiesMainThreadCopy(
144       const RendererCapabilities& capabilities);
145   void BeginMainFrame(
146       scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
147   void DidCommitAndDrawFrame();
148   void DidCompleteSwapBuffers();
149   void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue,
150                           base::Time wall_clock_time);
151   void DoCreateAndInitializeOutputSurface();
152   // |capabilities| is set only when |success| is true.
153   void OnOutputSurfaceInitializeAttempted(
154       bool success,
155       const RendererCapabilities& capabilities);
156   void SendCommitRequestToImplThreadIfNeeded();
157
158   // Called on impl thread.
159   struct ReadbackRequest;
160   struct CommitPendingRequest;
161   struct SchedulerStateRequest;
162
163   void ForceCommitForReadbackOnImplThread(
164       CompletionEvent* begin_main_frame_sent_completion,
165       ReadbackRequest* request);
166   void StartCommitOnImplThread(
167       CompletionEvent* completion,
168       ResourceUpdateQueue* queue,
169       scoped_refptr<ContextProvider> offscreen_context_provider);
170   void BeginMainFrameAbortedOnImplThread(bool did_handle);
171   void RequestReadbackOnImplThread(ReadbackRequest* request);
172   void FinishAllRenderingOnImplThread(CompletionEvent* completion);
173   void InitializeImplOnImplThread(CompletionEvent* completion);
174   void SetLayerTreeHostClientReadyOnImplThread();
175   void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
176   void UpdateBackgroundAnimateTicking();
177   void HasInitializedOutputSurfaceOnImplThread(
178       CompletionEvent* completion,
179       bool* has_initialized_output_surface);
180   void InitializeOutputSurfaceOnImplThread(
181       CompletionEvent* completion,
182       scoped_ptr<OutputSurface> output_surface,
183       scoped_refptr<ContextProvider> offscreen_context_provider,
184       bool* success,
185       RendererCapabilities* capabilities);
186   void FinishGLOnImplThread(CompletionEvent* completion);
187   void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
188   void AcquireLayerTexturesForMainThreadOnImplThread(
189       CompletionEvent* completion);
190   DrawSwapReadbackResult DrawSwapReadbackInternal(bool forced_draw,
191                                                   bool swap_requested,
192                                                   bool readback_requested);
193   void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
194   void CheckOutputSurfaceStatusOnImplThread();
195   void CommitPendingOnImplThreadForTesting(CommitPendingRequest* request);
196   void SchedulerStateAsValueOnImplThreadForTesting(
197       SchedulerStateRequest* request);
198   void AsValueOnImplThread(CompletionEvent* completion,
199                            base::DictionaryValue* state) const;
200   void RenewTreePriorityOnImplThread();
201   void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
202   void StartScrollbarAnimationOnImplThread();
203   void MainThreadHasStoppedFlingingOnImplThread();
204   void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
205   LayerTreeHost* layer_tree_host();
206   const LayerTreeHost* layer_tree_host() const;
207
208   struct MainThreadOnly {
209     MainThreadOnly(ThreadProxy* proxy, int layer_tree_host_id);
210     ~MainThreadOnly();
211
212     const int layer_tree_host_id;
213
214     // Set only when SetNeedsAnimate is called.
215     bool animate_requested;
216     // Set only when SetNeedsCommit is called.
217     bool commit_requested;
218     // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
219     bool commit_request_sent_to_impl_thread;
220     // Set by BeginMainFrame
221     bool created_offscreen_context_provider;
222
223     bool started;
224     bool textures_acquired;
225     bool in_composite_and_readback;
226     bool manage_tiles_pending;
227     bool can_cancel_commit;
228     bool defer_commits;
229
230     base::CancelableClosure output_surface_creation_callback;
231     RendererCapabilities renderer_capabilities_main_thread_copy;
232
233     scoped_ptr<BeginMainFrameAndCommitState> pending_deferred_commit;
234     base::WeakPtrFactory<ThreadProxy> weak_factory;
235   };
236   // Use accessors instead of this variable directly.
237   MainThreadOnly main_thread_only_vars_unsafe_;
238   MainThreadOnly& main();
239   const MainThreadOnly& main() const;
240
241   // Accessed on the main thread, or when main thread is blocked.
242   struct MainThreadOrBlockedMainThread {
243     explicit MainThreadOrBlockedMainThread(LayerTreeHost* host);
244     ~MainThreadOrBlockedMainThread();
245
246     PrioritizedResourceManager* contents_texture_manager();
247
248     LayerTreeHost* layer_tree_host;
249     bool commit_waits_for_activation;
250     bool main_thread_inside_commit;
251
252     base::TimeTicks last_monotonic_frame_begin_time;
253   };
254   // Use accessors instead of this variable directly.
255   MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_;
256   MainThreadOrBlockedMainThread& blocked_main();
257   const MainThreadOrBlockedMainThread& blocked_main() const;
258
259   struct CompositorThreadOnly {
260     CompositorThreadOnly(ThreadProxy* proxy, int layer_tree_host_id);
261     ~CompositorThreadOnly();
262
263     const int layer_tree_host_id;
264
265     // Copy of the main thread side contents texture manager for work
266     // that needs to be done on the compositor thread.
267     PrioritizedResourceManager* contents_texture_manager;
268
269     scoped_ptr<Scheduler> scheduler;
270
271     // Set when the main thread is waiting on a
272     // ScheduledActionSendBeginMainFrame to be issued.
273     CompletionEvent* begin_main_frame_sent_completion_event;
274
275     // Set when the main thread is waiting on a readback.
276     ReadbackRequest* readback_request;
277
278     // Set when the main thread is waiting on a commit to complete.
279     CompletionEvent* commit_completion_event;
280
281     // Set when the main thread is waiting on a pending tree activation.
282     CompletionEvent* completion_event_for_commit_held_on_tree_activation;
283
284     // Set when the main thread is waiting on layers to be drawn.
285     CompletionEvent* texture_acquisition_completion_event;
286
287     scoped_ptr<ResourceUpdateController> current_resource_update_controller;
288
289     // Set when the next draw should post DidCommitAndDrawFrame to the main
290     // thread.
291     bool next_frame_is_newly_committed_frame;
292
293     bool inside_draw;
294
295     bool input_throttled_until_commit;
296
297     // Set when we freeze animations to avoid checkerboarding.
298     bool animations_frozen_until_next_draw;
299     base::TimeTicks animation_freeze_time;
300
301     base::TimeTicks smoothness_takes_priority_expiration_time;
302     bool renew_tree_priority_pending;
303
304     ProxyTimingHistory timing_history;
305
306     scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl;
307     base::WeakPtrFactory<ThreadProxy> weak_factory;
308   };
309   // Use accessors instead of this variable directly.
310   CompositorThreadOnly compositor_thread_vars_unsafe_;
311   CompositorThreadOnly& impl();
312   const CompositorThreadOnly& impl() const;
313
314   base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
315   base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
316
317   DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
318 };
319
320 }  // namespace cc
321
322 #endif  // CC_TREES_THREAD_PROXY_H_