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