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_SINGLE_THREAD_PROXY_H_
      6 #define CC_TREES_SINGLE_THREAD_PROXY_H_
      7 
      8 #include <limits>
      9 
     10 #include "base/time/time.h"
     11 #include "cc/animation/animation_events.h"
     12 #include "cc/output/begin_frame_args.h"
     13 #include "cc/scheduler/scheduler.h"
     14 #include "cc/trees/layer_tree_host_impl.h"
     15 #include "cc/trees/proxy.h"
     16 #include "cc/trees/proxy_timing_history.h"
     17 
     18 namespace cc {
     19 
     20 class ContextProvider;
     21 class LayerTreeHost;
     22 class LayerTreeHostSingleThreadClient;
     23 
     24 class CC_EXPORT SingleThreadProxy : public Proxy,
     25                                     NON_EXPORTED_BASE(LayerTreeHostImplClient),
     26                                     SchedulerClient {
     27  public:
     28   static scoped_ptr<Proxy> Create(
     29       LayerTreeHost* layer_tree_host,
     30       LayerTreeHostSingleThreadClient* client,
     31       scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
     32   virtual ~SingleThreadProxy();
     33 
     34   // Proxy implementation
     35   virtual void FinishAllRendering() OVERRIDE;
     36   virtual bool IsStarted() const OVERRIDE;
     37   virtual void SetOutputSurface(scoped_ptr<OutputSurface>) OVERRIDE;
     38   virtual void SetLayerTreeHostClientReady() OVERRIDE;
     39   virtual void SetVisible(bool visible) OVERRIDE;
     40   virtual const RendererCapabilities& GetRendererCapabilities() const OVERRIDE;
     41   virtual void SetNeedsAnimate() OVERRIDE;
     42   virtual void SetNeedsUpdateLayers() OVERRIDE;
     43   virtual void SetNeedsCommit() OVERRIDE;
     44   virtual void SetNeedsRedraw(const gfx::Rect& damage_rect) OVERRIDE;
     45   virtual void SetNextCommitWaitsForActivation() OVERRIDE;
     46   virtual void NotifyInputThrottledUntilCommit() OVERRIDE {}
     47   virtual void SetDeferCommits(bool defer_commits) OVERRIDE;
     48   virtual bool CommitRequested() const OVERRIDE;
     49   virtual bool BeginMainFrameRequested() const OVERRIDE;
     50   virtual void MainThreadHasStoppedFlinging() OVERRIDE {}
     51   virtual void Start() OVERRIDE;
     52   virtual void Stop() OVERRIDE;
     53   virtual size_t MaxPartialTextureUpdates() const OVERRIDE;
     54   virtual void ForceSerializeOnSwapBuffers() OVERRIDE;
     55   virtual bool SupportsImplScrolling() const OVERRIDE;
     56   virtual void AsValueInto(base::debug::TracedValue* state) const OVERRIDE;
     57   virtual bool MainFrameWillHappenForTesting() OVERRIDE;
     58 
     59   // SchedulerClient implementation
     60   virtual void SetNeedsBeginFrame(bool enable) OVERRIDE;
     61   virtual void WillBeginImplFrame(const BeginFrameArgs& args) OVERRIDE;
     62   virtual void ScheduledActionSendBeginMainFrame() OVERRIDE;
     63   virtual DrawResult ScheduledActionDrawAndSwapIfPossible() OVERRIDE;
     64   virtual DrawResult ScheduledActionDrawAndSwapForced() OVERRIDE;
     65   virtual void ScheduledActionCommit() OVERRIDE;
     66   virtual void ScheduledActionAnimate() OVERRIDE;
     67   virtual void ScheduledActionUpdateVisibleTiles() OVERRIDE;
     68   virtual void ScheduledActionActivateSyncTree() OVERRIDE;
     69   virtual void ScheduledActionBeginOutputSurfaceCreation() OVERRIDE;
     70   virtual void ScheduledActionManageTiles() OVERRIDE;
     71   virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) OVERRIDE;
     72   virtual base::TimeDelta DrawDurationEstimate() OVERRIDE;
     73   virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() OVERRIDE;
     74   virtual base::TimeDelta CommitToActivateDurationEstimate() OVERRIDE;
     75   virtual void DidBeginImplFrameDeadline() OVERRIDE;
     76 
     77   // LayerTreeHostImplClient implementation
     78   virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE;
     79   virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE;
     80   virtual void CommitVSyncParameters(base::TimeTicks timebase,
     81                                      base::TimeDelta interval) OVERRIDE {}
     82   virtual void SetEstimatedParentDrawTime(base::TimeDelta draw_time) OVERRIDE {}
     83   virtual void SetMaxSwapsPendingOnImplThread(int max) OVERRIDE {}
     84   virtual void DidSwapBuffersOnImplThread() OVERRIDE;
     85   virtual void DidSwapBuffersCompleteOnImplThread() OVERRIDE;
     86   virtual void BeginFrame(const BeginFrameArgs& args) OVERRIDE;
     87   virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE;
     88   virtual void NotifyReadyToActivate() OVERRIDE;
     89   virtual void SetNeedsRedrawOnImplThread() OVERRIDE;
     90   virtual void SetNeedsRedrawRectOnImplThread(
     91       const gfx::Rect& dirty_rect) OVERRIDE;
     92   virtual void SetNeedsAnimateOnImplThread() OVERRIDE;
     93   virtual void SetNeedsManageTilesOnImplThread() OVERRIDE;
     94   virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE;
     95   virtual void SetNeedsCommitOnImplThread() OVERRIDE;
     96   virtual void PostAnimationEventsToMainThreadOnImplThread(
     97       scoped_ptr<AnimationEventsVector> events) OVERRIDE;
     98   virtual bool ReduceContentsTextureMemoryOnImplThread(
     99       size_t limit_bytes,
    100       int priority_cutoff) OVERRIDE;
    101   virtual bool IsInsideDraw() OVERRIDE;
    102   virtual void RenewTreePriority() OVERRIDE {}
    103   virtual void PostDelayedScrollbarFadeOnImplThread(
    104       const base::Closure& start_fade,
    105       base::TimeDelta delay) OVERRIDE {}
    106   virtual void DidActivateSyncTree() OVERRIDE {}
    107   virtual void DidManageTiles() OVERRIDE;
    108   virtual void SetDebugState(const LayerTreeDebugState& debug_state) OVERRIDE {}
    109 
    110   void RequestNewOutputSurface();
    111 
    112   // Called by the legacy path where RenderWidget does the scheduling.
    113   void CompositeImmediately(base::TimeTicks frame_begin_time);
    114 
    115  private:
    116   SingleThreadProxy(
    117       LayerTreeHost* layer_tree_host,
    118       LayerTreeHostSingleThreadClient* client,
    119       scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
    120 
    121   void BeginMainFrame();
    122   void BeginMainFrameAbortedOnImplThread();
    123   void DoCommit(const BeginFrameArgs& begin_frame_args);
    124   DrawResult DoComposite(base::TimeTicks frame_begin_time,
    125                          LayerTreeHostImpl::FrameData* frame);
    126   void DoSwap();
    127   void DidCommitAndDrawFrame();
    128 
    129   bool ShouldComposite() const;
    130   void UpdateBackgroundAnimateTicking();
    131 
    132   // Accessed on main thread only.
    133   LayerTreeHost* layer_tree_host_;
    134   LayerTreeHostSingleThreadClient* client_;
    135 
    136   // Used on the Thread, but checked on main thread during
    137   // initialization/shutdown.
    138   scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_;
    139   RendererCapabilities renderer_capabilities_for_main_thread_;
    140 
    141   // Accessed from both threads.
    142   scoped_ptr<Scheduler> scheduler_on_impl_thread_;
    143   ProxyTimingHistory timing_history_;
    144 
    145   bool next_frame_is_newly_committed_frame_;
    146 
    147   bool inside_draw_;
    148   bool defer_commits_;
    149   bool commit_was_deferred_;
    150   bool commit_requested_;
    151 
    152   base::WeakPtrFactory<SingleThreadProxy> weak_factory_;
    153 
    154   DISALLOW_COPY_AND_ASSIGN(SingleThreadProxy);
    155 };
    156 
    157 // For use in the single-threaded case. In debug builds, it pretends that the
    158 // code is running on the impl thread to satisfy assertion checks.
    159 class DebugScopedSetImplThread {
    160  public:
    161   explicit DebugScopedSetImplThread(Proxy* proxy) : proxy_(proxy) {
    162 #if DCHECK_IS_ON
    163     previous_value_ = proxy_->impl_thread_is_overridden_;
    164     proxy_->SetCurrentThreadIsImplThread(true);
    165 #endif
    166   }
    167   ~DebugScopedSetImplThread() {
    168 #if DCHECK_IS_ON
    169     proxy_->SetCurrentThreadIsImplThread(previous_value_);
    170 #endif
    171   }
    172 
    173  private:
    174   bool previous_value_;
    175   Proxy* proxy_;
    176 
    177   DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThread);
    178 };
    179 
    180 // For use in the single-threaded case. In debug builds, it pretends that the
    181 // code is running on the main thread to satisfy assertion checks.
    182 class DebugScopedSetMainThread {
    183  public:
    184   explicit DebugScopedSetMainThread(Proxy* proxy) : proxy_(proxy) {
    185 #if DCHECK_IS_ON
    186     previous_value_ = proxy_->impl_thread_is_overridden_;
    187     proxy_->SetCurrentThreadIsImplThread(false);
    188 #endif
    189   }
    190   ~DebugScopedSetMainThread() {
    191 #if DCHECK_IS_ON
    192     proxy_->SetCurrentThreadIsImplThread(previous_value_);
    193 #endif
    194   }
    195 
    196  private:
    197   bool previous_value_;
    198   Proxy* proxy_;
    199 
    200   DISALLOW_COPY_AND_ASSIGN(DebugScopedSetMainThread);
    201 };
    202 
    203 // For use in the single-threaded case. In debug builds, it pretends that the
    204 // code is running on the impl thread and that the main thread is blocked to
    205 // satisfy assertion checks
    206 class DebugScopedSetImplThreadAndMainThreadBlocked {
    207  public:
    208   explicit DebugScopedSetImplThreadAndMainThreadBlocked(Proxy* proxy)
    209       : impl_thread_(proxy), main_thread_blocked_(proxy) {}
    210 
    211  private:
    212   DebugScopedSetImplThread impl_thread_;
    213   DebugScopedSetMainThreadBlocked main_thread_blocked_;
    214 
    215   DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThreadAndMainThreadBlocked);
    216 };
    217 
    218 }  // namespace cc
    219 
    220 #endif  // CC_TREES_SINGLE_THREAD_PROXY_H_
    221