Home | History | Annotate | Download | only in trees
      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 NotifyInputThrottledUntilCommit() OVERRIDE;
     56   virtual void SetDeferCommits(bool defer_commits) OVERRIDE;
     57   virtual bool CommitRequested() const OVERRIDE;
     58   virtual void MainThreadHasStoppedFlinging() OVERRIDE;
     59   virtual void Start(scoped_ptr<OutputSurface> first_output_surface) OVERRIDE;
     60   virtual void Stop() OVERRIDE;
     61   virtual size_t MaxPartialTextureUpdates() const OVERRIDE;
     62   virtual void AcquireLayerTextures() OVERRIDE;
     63   virtual void ForceSerializeOnSwapBuffers() OVERRIDE;
     64   virtual scoped_ptr<base::Value> AsValue() const OVERRIDE;
     65   virtual bool CommitPendingForTesting() OVERRIDE;
     66   virtual std::string SchedulerStateAsStringForTesting() OVERRIDE;
     67 
     68   // LayerTreeHostImplClient implementation
     69   virtual void DidTryInitializeRendererOnImplThread(
     70       bool success,
     71       scoped_refptr<ContextProvider> offscreen_context_provider) OVERRIDE;
     72   virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE;
     73   virtual void OnSwapBuffersCompleteOnImplThread() OVERRIDE;
     74   virtual void BeginFrameOnImplThread(const BeginFrameArgs& args) OVERRIDE;
     75   virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE;
     76   virtual void OnHasPendingTreeStateChanged(bool has_pending_tree) OVERRIDE;
     77   virtual void SetNeedsRedrawOnImplThread() OVERRIDE;
     78   virtual void SetNeedsRedrawRectOnImplThread(gfx::Rect dirty_rect) 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 ReduceWastedContentsTextureMemoryOnImplThread() OVERRIDE;
     88   virtual void SendManagedMemoryStats() OVERRIDE;
     89   virtual bool IsInsideDraw() OVERRIDE;
     90   virtual void RenewTreePriority() OVERRIDE;
     91   virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay)
     92       OVERRIDE;
     93   virtual void DidActivatePendingTree() OVERRIDE;
     94 
     95   // SchedulerClient implementation
     96   virtual void SetNeedsBeginFrameOnImplThread(bool enable) OVERRIDE;
     97   virtual void ScheduledActionSendBeginFrameToMainThread() OVERRIDE;
     98   virtual ScheduledActionDrawAndSwapResult
     99       ScheduledActionDrawAndSwapIfPossible() OVERRIDE;
    100   virtual ScheduledActionDrawAndSwapResult ScheduledActionDrawAndSwapForced()
    101       OVERRIDE;
    102   virtual void ScheduledActionCommit() OVERRIDE;
    103   virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE;
    104   virtual void ScheduledActionActivatePendingTreeIfNeeded() OVERRIDE;
    105   virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE;
    106   virtual void ScheduledActionAcquireLayerTexturesForMainThread() OVERRIDE;
    107   virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) OVERRIDE;
    108   virtual base::TimeDelta DrawDurationEstimate() OVERRIDE;
    109   virtual base::TimeDelta BeginFrameToCommitDurationEstimate() OVERRIDE;
    110   virtual base::TimeDelta CommitToActivateDurationEstimate() OVERRIDE;
    111 
    112   // ResourceUpdateControllerClient implementation
    113   virtual void ReadyToFinalizeTextureUpdates() OVERRIDE;
    114 
    115  private:
    116   ThreadProxy(LayerTreeHost* layer_tree_host,
    117               scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
    118 
    119   struct BeginFrameAndCommitState {
    120     BeginFrameAndCommitState();
    121     ~BeginFrameAndCommitState();
    122 
    123     base::TimeTicks monotonic_frame_begin_time;
    124     scoped_ptr<ScrollAndScaleSet> scroll_info;
    125     size_t memory_allocation_limit_bytes;
    126   };
    127 
    128   // Called on main thread.
    129   void BeginFrameOnMainThread(
    130       scoped_ptr<BeginFrameAndCommitState> begin_frame_state);
    131   void DidCommitAndDrawFrame();
    132   void DidCompleteSwapBuffers();
    133   void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue,
    134                           base::Time wall_clock_time);
    135   void DoCreateAndInitializeOutputSurface();
    136   // |capabilities| is set only when |success| is true.
    137   void OnOutputSurfaceInitializeAttempted(
    138       bool success,
    139       const RendererCapabilities& capabilities);
    140   void SendCommitRequestToImplThreadIfNeeded();
    141 
    142   // Called on impl thread.
    143   struct ReadbackRequest;
    144   struct CommitPendingRequest;
    145   struct SchedulerStateRequest;
    146 
    147   void ForceCommitOnImplThread(CompletionEvent* completion);
    148   void StartCommitOnImplThread(
    149       CompletionEvent* completion,
    150       ResourceUpdateQueue* queue,
    151       scoped_refptr<cc::ContextProvider> offscreen_context_provider);
    152   void BeginFrameAbortedByMainThreadOnImplThread(bool did_handle);
    153   void RequestReadbackOnImplThread(ReadbackRequest* request);
    154   void FinishAllRenderingOnImplThread(CompletionEvent* completion);
    155   void InitializeImplOnImplThread(CompletionEvent* completion);
    156   void SetLayerTreeHostClientReadyOnImplThread();
    157   void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
    158   void HasInitializedOutputSurfaceOnImplThread(
    159       CompletionEvent* completion,
    160       bool* has_initialized_output_surface);
    161   void InitializeOutputSurfaceOnImplThread(
    162       CompletionEvent* completion,
    163       scoped_ptr<OutputSurface> output_surface,
    164       scoped_refptr<ContextProvider> offscreen_context_provider,
    165       bool* success,
    166       RendererCapabilities* capabilities);
    167   void FinishGLOnImplThread(CompletionEvent* completion);
    168   void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
    169   void AcquireLayerTexturesForMainThreadOnImplThread(
    170       CompletionEvent* completion);
    171   ScheduledActionDrawAndSwapResult ScheduledActionDrawAndSwapInternal(
    172       bool forced_draw);
    173   void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
    174   void CheckOutputSurfaceStatusOnImplThread();
    175   void CommitPendingOnImplThreadForTesting(CommitPendingRequest* request);
    176   void SchedulerStateAsStringOnImplThreadForTesting(
    177       SchedulerStateRequest* request);
    178   void AsValueOnImplThread(CompletionEvent* completion,
    179                            base::DictionaryValue* state) const;
    180   void RenewTreePriorityOnImplThread();
    181   void DidSwapUseIncompleteTileOnImplThread();
    182   void StartScrollbarAnimationOnImplThread();
    183   void MainThreadHasStoppedFlingingOnImplThread();
    184   void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
    185 
    186   // Accessed on main thread only.
    187 
    188   // Set only when SetNeedsAnimate is called.
    189   bool animate_requested_;
    190   // Set only when SetNeedsCommit is called.
    191   bool commit_requested_;
    192   // Set by SetNeedsCommit and SetNeedsAnimate.
    193   bool commit_request_sent_to_impl_thread_;
    194   // Set by BeginFrameOnMainThread
    195   bool created_offscreen_context_provider_;
    196   base::CancelableClosure output_surface_creation_callback_;
    197   LayerTreeHost* layer_tree_host_;
    198   RendererCapabilities renderer_capabilities_main_thread_copy_;
    199   bool started_;
    200   bool textures_acquired_;
    201   bool in_composite_and_readback_;
    202   bool manage_tiles_pending_;
    203   // Weak pointer to use when posting tasks to the impl thread.
    204   base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
    205   // Holds the first output surface passed from Start. Should not be used for
    206   // anything else.
    207   scoped_ptr<OutputSurface> first_output_surface_;
    208 
    209   base::WeakPtrFactory<ThreadProxy> weak_factory_on_impl_thread_;
    210 
    211   base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
    212   base::WeakPtrFactory<ThreadProxy> weak_factory_;
    213 
    214   scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_;
    215 
    216   scoped_ptr<Scheduler> scheduler_on_impl_thread_;
    217 
    218   // Set when the main thread is waiting on a
    219   // ScheduledActionSendBeginFrameToMainThread to be issued.
    220   CompletionEvent*
    221       begin_frame_sent_to_main_thread_completion_event_on_impl_thread_;
    222 
    223   // Set when the main thread is waiting on a readback.
    224   ReadbackRequest* readback_request_on_impl_thread_;
    225 
    226   // Set when the main thread is waiting on a commit to complete.
    227   CompletionEvent* commit_completion_event_on_impl_thread_;
    228 
    229   // Set when the main thread is waiting on a pending tree activation.
    230   CompletionEvent* completion_event_for_commit_held_on_tree_activation_;
    231 
    232   // Set when the main thread is waiting on layers to be drawn.
    233   CompletionEvent* texture_acquisition_completion_event_on_impl_thread_;
    234 
    235   scoped_ptr<ResourceUpdateController>
    236       current_resource_update_controller_on_impl_thread_;
    237 
    238   // Set when the next draw should post DidCommitAndDrawFrame to the main
    239   // thread.
    240   bool next_frame_is_newly_committed_frame_on_impl_thread_;
    241 
    242   bool throttle_frame_production_;
    243   bool begin_frame_scheduling_enabled_;
    244   bool using_synchronous_renderer_compositor_;
    245 
    246   bool inside_draw_;
    247 
    248   bool can_cancel_commit_;
    249 
    250   bool defer_commits_;
    251   bool input_throttled_until_commit_;
    252   scoped_ptr<BeginFrameAndCommitState> pending_deferred_commit_;
    253 
    254   base::TimeTicks smoothness_takes_priority_expiration_time_;
    255   bool renew_tree_priority_on_impl_thread_pending_;
    256 
    257   RollingTimeDeltaHistory draw_duration_history_;
    258   RollingTimeDeltaHistory begin_frame_to_commit_duration_history_;
    259   RollingTimeDeltaHistory commit_to_activate_duration_history_;
    260 
    261   // Used for computing samples added to
    262   // begin_frame_to_commit_draw_duration_history_ and
    263   // activation_duration_history_.
    264   base::TimeTicks begin_frame_sent_to_main_thread_time_;
    265   base::TimeTicks commit_complete_time_;
    266 
    267   DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
    268 };
    269 
    270 }  // namespace cc
    271 
    272 #endif  // CC_TREES_THREAD_PROXY_H_
    273