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 21 class SingleThreadProxy : public Proxy, LayerTreeHostImplClient { 22 public: 23 static scoped_ptr<Proxy> Create(LayerTreeHost* layer_tree_host); 24 virtual ~SingleThreadProxy(); 25 26 // Proxy implementation 27 virtual bool CompositeAndReadback(void* pixels, gfx::Rect rect) OVERRIDE; 28 virtual void FinishAllRendering() OVERRIDE; 29 virtual bool IsStarted() const OVERRIDE; 30 virtual void SetLayerTreeHostClientReady() OVERRIDE; 31 virtual void SetVisible(bool visible) OVERRIDE; 32 virtual void CreateAndInitializeOutputSurface() OVERRIDE; 33 virtual const RendererCapabilities& GetRendererCapabilities() const OVERRIDE; 34 virtual void SetNeedsAnimate() OVERRIDE; 35 virtual void SetNeedsUpdateLayers() OVERRIDE; 36 virtual void SetNeedsCommit() OVERRIDE; 37 virtual void SetNeedsRedraw(gfx::Rect damage_rect) OVERRIDE; 38 virtual void NotifyInputThrottledUntilCommit() OVERRIDE {} 39 virtual void SetDeferCommits(bool defer_commits) OVERRIDE; 40 virtual bool CommitRequested() const OVERRIDE; 41 virtual void MainThreadHasStoppedFlinging() OVERRIDE {} 42 virtual void Start(scoped_ptr<OutputSurface> first_output_surface) OVERRIDE; 43 virtual void Stop() OVERRIDE; 44 virtual size_t MaxPartialTextureUpdates() const OVERRIDE; 45 virtual void AcquireLayerTextures() OVERRIDE {} 46 virtual void ForceSerializeOnSwapBuffers() OVERRIDE; 47 virtual scoped_ptr<base::Value> AsValue() const OVERRIDE; 48 virtual bool CommitPendingForTesting() OVERRIDE; 49 50 // LayerTreeHostImplClient implementation 51 virtual void DidTryInitializeRendererOnImplThread( 52 bool success, 53 scoped_refptr<ContextProvider> offscreen_context_provider) OVERRIDE; 54 virtual void DidLoseOutputSurfaceOnImplThread() OVERRIDE; 55 virtual void OnSwapBuffersCompleteOnImplThread() OVERRIDE {} 56 virtual void BeginFrameOnImplThread(const BeginFrameArgs& args) 57 OVERRIDE {} 58 virtual void OnCanDrawStateChanged(bool can_draw) OVERRIDE; 59 virtual void OnHasPendingTreeStateChanged(bool have_pending_tree) OVERRIDE; 60 virtual void SetNeedsRedrawOnImplThread() OVERRIDE; 61 virtual void SetNeedsRedrawRectOnImplThread(gfx::Rect dirty_rect) OVERRIDE; 62 virtual void DidInitializeVisibleTileOnImplThread() OVERRIDE; 63 virtual void SetNeedsCommitOnImplThread() OVERRIDE; 64 virtual void PostAnimationEventsToMainThreadOnImplThread( 65 scoped_ptr<AnimationEventsVector> events, 66 base::Time wall_clock_time) OVERRIDE; 67 virtual bool ReduceContentsTextureMemoryOnImplThread( 68 size_t limit_bytes, 69 int priority_cutoff) OVERRIDE; 70 virtual void ReduceWastedContentsTextureMemoryOnImplThread() OVERRIDE; 71 virtual void SendManagedMemoryStats() OVERRIDE; 72 virtual bool IsInsideDraw() OVERRIDE; 73 virtual void RenewTreePriority() OVERRIDE {} 74 virtual void RequestScrollbarAnimationOnImplThread(base::TimeDelta delay) 75 OVERRIDE {} 76 virtual void DidActivatePendingTree() OVERRIDE {} 77 78 // Called by the legacy path where RenderWidget does the scheduling. 79 void CompositeImmediately(base::TimeTicks frame_begin_time); 80 81 private: 82 explicit SingleThreadProxy(LayerTreeHost* layer_tree_host); 83 84 void OnOutputSurfaceInitializeAttempted(bool success); 85 bool CommitAndComposite(base::TimeTicks frame_begin_time, 86 gfx::Rect device_viewport_damage_rect, 87 bool for_readback, 88 LayerTreeHostImpl::FrameData* frame); 89 void DoCommit(scoped_ptr<ResourceUpdateQueue> queue); 90 bool DoComposite( 91 scoped_refptr<cc::ContextProvider> offscreen_context_provider, 92 base::TimeTicks frame_begin_time, 93 gfx::Rect device_viewport_damage_rect, 94 bool for_readback, 95 LayerTreeHostImpl::FrameData* frame); 96 void DidSwapFrame(); 97 98 bool ShouldComposite() const; 99 100 // Accessed on main thread only. 101 LayerTreeHost* layer_tree_host_; 102 bool created_offscreen_context_provider_; 103 104 // Holds the first output surface passed from Start. Should not be used for 105 // anything else. 106 scoped_ptr<OutputSurface> first_output_surface_; 107 108 // Used on the Thread, but checked on main thread during 109 // initialization/shutdown. 110 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl_; 111 RendererCapabilities renderer_capabilities_for_main_thread_; 112 113 bool next_frame_is_newly_committed_frame_; 114 115 bool inside_draw_; 116 117 DISALLOW_COPY_AND_ASSIGN(SingleThreadProxy); 118 }; 119 120 // For use in the single-threaded case. In debug builds, it pretends that the 121 // code is running on the impl thread to satisfy assertion checks. 122 class DebugScopedSetImplThread { 123 public: 124 explicit DebugScopedSetImplThread(Proxy* proxy) : proxy_(proxy) { 125 #ifndef NDEBUG 126 previous_value_ = proxy_->impl_thread_is_overridden_; 127 proxy_->SetCurrentThreadIsImplThread(true); 128 #endif 129 } 130 ~DebugScopedSetImplThread() { 131 #ifndef NDEBUG 132 proxy_->SetCurrentThreadIsImplThread(previous_value_); 133 #endif 134 } 135 136 private: 137 bool previous_value_; 138 Proxy* proxy_; 139 140 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThread); 141 }; 142 143 // For use in the single-threaded case. In debug builds, it pretends that the 144 // code is running on the main thread to satisfy assertion checks. 145 class DebugScopedSetMainThread { 146 public: 147 explicit DebugScopedSetMainThread(Proxy* proxy) : proxy_(proxy) { 148 #ifndef NDEBUG 149 previous_value_ = proxy_->impl_thread_is_overridden_; 150 proxy_->SetCurrentThreadIsImplThread(false); 151 #endif 152 } 153 ~DebugScopedSetMainThread() { 154 #ifndef NDEBUG 155 proxy_->SetCurrentThreadIsImplThread(previous_value_); 156 #endif 157 } 158 159 private: 160 bool previous_value_; 161 Proxy* proxy_; 162 163 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetMainThread); 164 }; 165 166 // For use in the single-threaded case. In debug builds, it pretends that the 167 // code is running on the impl thread and that the main thread is blocked to 168 // satisfy assertion checks 169 class DebugScopedSetImplThreadAndMainThreadBlocked { 170 public: 171 explicit DebugScopedSetImplThreadAndMainThreadBlocked(Proxy* proxy) 172 : impl_thread_(proxy), main_thread_blocked_(proxy) {} 173 174 private: 175 DebugScopedSetImplThread impl_thread_; 176 DebugScopedSetMainThreadBlocked main_thread_blocked_; 177 178 DISALLOW_COPY_AND_ASSIGN(DebugScopedSetImplThreadAndMainThreadBlocked); 179 }; 180 181 } // namespace cc 182 183 #endif // CC_TREES_SINGLE_THREAD_PROXY_H_ 184