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/trees/layer_tree_host_impl.h" 14 #include "cc/trees/proxy.h" 15 16 namespace cc { 17 18 class ContextProvider; 19 class LayerTreeHost; 20 class LayerTreeHostSingleThreadClient; 21 22 class CC_EXPORT SingleThreadProxy : public Proxy, 23 NON_EXPORTED_BASE(LayerTreeHostImplClient) { 24 public: 25 static scoped_ptr<Proxy> Create( 26 LayerTreeHost* layer_tree_host, 27 LayerTreeHostSingleThreadClient* client); 28 virtual ~SingleThreadProxy(); 29 30 // Proxy implementation 31 virtual void FinishAllRendering() OVERRIDE; 32 virtual bool IsStarted() const OVERRIDE; 33 virtual void SetLayerTreeHostClientReady() OVERRIDE; 34 virtual void SetVisible(bool visible) OVERRIDE; 35 virtual const RendererCapabilities& GetRendererCapabilities() const OVERRIDE; 36 virtual void SetNeedsAnimate() OVERRIDE; 37 virtual void SetNeedsUpdateLayers() OVERRIDE; 38 virtual void SetNeedsCommit() OVERRIDE; 39 virtual void SetNeedsRedraw(const gfx::Rect& damage_rect) OVERRIDE; 40 virtual void SetNextCommitWaitsForActivation() OVERRIDE; 41 virtual void NotifyInputThrottledUntilCommit() OVERRIDE {} 42 virtual void SetDeferCommits(bool defer_commits) OVERRIDE; 43 virtual bool CommitRequested() const OVERRIDE; 44 virtual bool BeginMainFrameRequested() const OVERRIDE; 45 virtual void MainThreadHasStoppedFlinging() OVERRIDE {} 46 virtual void Start() OVERRIDE; 47 virtual void Stop() OVERRIDE; 48 virtual size_t MaxPartialTextureUpdates() const OVERRIDE; 49 virtual void ForceSerializeOnSwapBuffers() OVERRIDE; 50 virtual scoped_ptr<base::Value> AsValue() const OVERRIDE; 51 virtual bool CommitPendingForTesting() OVERRIDE; 52 53 // LayerTreeHostImplClient implementation 54 virtual void UpdateRendererCapabilitiesOnImplThread() OVERRIDE; 55 virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE; 56 virtual void CommitVSyncParameters(base::TimeTicks timebase, 57 base::TimeDelta interval) OVERRIDE {} 58 virtual void SetEstimatedParentDrawTime(base::TimeDelta draw_time) OVERRIDE {} 59 virtual void SetMaxSwapsPendingOnImplThread(int max) OVERRIDE {} 60 virtual void DidSwapBuffersOnImplThread() OVERRIDE; 61 virtual void DidSwapBuffersCompleteOnImplThread() OVERRIDE; 62 virtual void BeginFrame(const BeginFrameArgs& args) OVERRIDE {} 63 virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE; 64 virtual void NotifyReadyToActivate() OVERRIDE; 65 virtual void SetNeedsRedrawOnImplThread() OVERRIDE; 66 virtual void SetNeedsRedrawRectOnImplThread( 67 const gfx::Rect& dirty_rect) OVERRIDE; 68 virtual void SetNeedsAnimateOnImplThread() OVERRIDE; 69 virtual void SetNeedsManageTilesOnImplThread() OVERRIDE; 70 virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE; 71 virtual void SetNeedsCommitOnImplThread() OVERRIDE; 72 virtual void PostAnimationEventsToMainThreadOnImplThread( 73 scoped_ptr<AnimationEventsVector> events) OVERRIDE; 74 virtual bool ReduceContentsTextureMemoryOnImplThread( 75 size_t limit_bytes, 76 int priority_cutoff) OVERRIDE; 77 virtual bool IsInsideDraw() OVERRIDE; 78 virtual void RenewTreePriority() OVERRIDE {} 79 virtual void PostDelayedScrollbarFadeOnImplThread( 80 const base::Closure& start_fade, 81 base::TimeDelta delay) OVERRIDE {} 82 virtual void DidActivatePendingTree() OVERRIDE {} 83 virtual void DidManageTiles() OVERRIDE {} 84 virtual void SetDebugState(const LayerTreeDebugState& debug_state) OVERRIDE {} 85 86 // Attempts to create the context and renderer synchronously. Calls 87 // LayerTreeHost::OnCreateAndInitializeOutputSurfaceAttempted with the result. 88 void CreateAndInitializeOutputSurface(); 89 90 // Called by the legacy path where RenderWidget does the scheduling. 91 void CompositeImmediately(base::TimeTicks frame_begin_time); 92 93 private: 94 SingleThreadProxy(LayerTreeHost* layer_tree_host, 95 LayerTreeHostSingleThreadClient* client); 96 97 void DoCommit(scoped_ptr<ResourceUpdateQueue> queue); 98 bool DoComposite(base::TimeTicks frame_begin_time, 99 LayerTreeHostImpl::FrameData* frame); 100 void DidSwapFrame(); 101 102 bool ShouldComposite() const; 103 void UpdateBackgroundAnimateTicking(); 104 105 // Accessed on main thread only. 106 LayerTreeHost* layer_tree_host_; 107 LayerTreeHostSingleThreadClient* client_; 108 109 // Used on the Thread, but checked on main thread during 110 // initialization/shutdown. 111 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_; 112 RendererCapabilities renderer_capabilities_for_main_thread_; 113 114 bool next_frame_is_newly_committed_frame_; 115 116 bool inside_draw_; 117 118 DISALLOW_COPY_AND_ASSIGN(SingleThreadProxy); 119 }; 120 121 // For use in the single-threaded case. In debug builds, it pretends that the 122 // code is running on the impl thread to satisfy assertion checks. 123 class DebugScopedSetImplThread { 124 public: 125 explicit DebugScopedSetImplThread(Proxy* proxy) : proxy_(proxy) { 126 #if DCHECK_IS_ON 127 previous_value_ = proxy_->impl_thread_is_overridden_; 128 proxy_->SetCurrentThreadIsImplThread(true); 129 #endif 130 } 131 ~DebugScopedSetImplThread() { 132 #if DCHECK_IS_ON 133 proxy_->SetCurrentThreadIsImplThread(previous_value_); 134 #endif 135 } 136 137 private: 138 bool previous_value_; 139 Proxy* proxy_; 140 141 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThread); 142 }; 143 144 // For use in the single-threaded case. In debug builds, it pretends that the 145 // code is running on the main thread to satisfy assertion checks. 146 class DebugScopedSetMainThread { 147 public: 148 explicit DebugScopedSetMainThread(Proxy* proxy) : proxy_(proxy) { 149 #if DCHECK_IS_ON 150 previous_value_ = proxy_->impl_thread_is_overridden_; 151 proxy_->SetCurrentThreadIsImplThread(false); 152 #endif 153 } 154 ~DebugScopedSetMainThread() { 155 #if DCHECK_IS_ON 156 proxy_->SetCurrentThreadIsImplThread(previous_value_); 157 #endif 158 } 159 160 private: 161 bool previous_value_; 162 Proxy* proxy_; 163 164 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetMainThread); 165 }; 166 167 // For use in the single-threaded case. In debug builds, it pretends that the 168 // code is running on the impl thread and that the main thread is blocked to 169 // satisfy assertion checks 170 class DebugScopedSetImplThreadAndMainThreadBlocked { 171 public: 172 explicit DebugScopedSetImplThreadAndMainThreadBlocked(Proxy* proxy) 173 : impl_thread_(proxy), main_thread_blocked_(proxy) {} 174 175 private: 176 DebugScopedSetImplThread impl_thread_; 177 DebugScopedSetMainThreadBlocked main_thread_blocked_; 178 179 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThreadAndMainThreadBlocked); 180 }; 181 182 } // namespace cc 183 184 #endif // CC_TREES_SINGLE_THREAD_PROXY_H_ 185