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