OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_TREES_THREADED_CHANNEL_H_ | 5 #ifndef CC_TREES_THREADED_CHANNEL_H_ |
6 #define CC_TREES_THREADED_CHANNEL_H_ | 6 #define CC_TREES_THREADED_CHANNEL_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" |
10 #include "cc/base/cc_export.h" | 11 #include "cc/base/cc_export.h" |
11 #include "cc/trees/channel_impl.h" | 12 #include "cc/trees/channel_impl.h" |
12 #include "cc/trees/channel_main.h" | 13 #include "cc/trees/channel_main.h" |
13 #include "cc/trees/proxy_common.h" | 14 #include "cc/trees/proxy_common.h" |
14 #include "cc/trees/proxy_impl.h" | 15 #include "cc/trees/proxy_impl.h" |
15 #include "cc/trees/proxy_main.h" | 16 #include "cc/trees/proxy_main.h" |
16 #include "cc/trees/thread_proxy.h" | |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
20 } | 20 } |
21 | 21 |
22 namespace cc { | 22 namespace cc { |
23 class ChannelImpl; | 23 class ChannelImpl; |
24 class ChannelMain; | 24 class ChannelMain; |
| 25 class LayerTreeHost; |
25 class ProxyImpl; | 26 class ProxyImpl; |
26 class ProxyMain; | 27 class ProxyMain; |
27 class ThreadProxy; | |
28 | 28 |
29 // An implementation of ChannelMain and ChannelImpl that sends commands between | 29 // An implementation of ChannelMain and ChannelImpl that sends commands between |
30 // ProxyMain and ProxyImpl across thread boundaries. | 30 // ProxyMain and ProxyImpl across thread boundaries. |
31 // | 31 // |
32 // LayerTreeHost creates ThreadedChannel and passes the ownership to ProxyMain. | 32 // LayerTreeHost creates ThreadedChannel and passes the ownership to ProxyMain. |
33 // The object life cycle and communication across threads is as follows: | 33 // The object life cycle and communication across threads is as follows: |
34 // | 34 // |
35 // | 35 // |
36 // Main Thread | Impl Thread | 36 // Main Thread | Impl Thread |
37 // LayerTreeHost->InitializeProxy | | 37 // LayerTreeHost->InitializeProxy | |
38 // | | | 38 // | | |
39 // ProxyMain->Start() | | 39 // ProxyMain->Start() | |
40 // | ThreadedChannel | 40 // | ThreadedChannel |
41 // --------------------------------------------------------------------------- | 41 // --------------------------------------------------------------------------- |
42 // ChannelMain::InitializeImpl ---PostTask---> ThreadedChannel:: | 42 // ChannelMain::SynchronouslyInitializeImpl ---PostTask---> ThreadedChannel:: |
43 // InitializeImplOnImplThread | 43 // InitializeImplOnImpl |
44 // | | 44 // | |
45 // ProxyImpl::Create | 45 // ProxyImpl::Create |
46 // | | 46 // | |
47 // ProxyImpl->Initialize() | 47 // ProxyImpl->Initialize() |
48 // . | 48 // . |
49 // . | 49 // . |
50 // ProxyImpl::ScheduledActionBegin | 50 // ProxyImpl::ScheduledActionBegin |
51 // OutputSurfaceCreation | 51 // OutputSurfaceCreation |
52 // | | 52 // | |
53 // ChannelImpl::RequestNewOutputSurface | 53 // ChannelImpl::RequestNewOutputSurface |
54 // ---------------------------------------------------------------------------- | 54 // ---------------------------------------------------------------------------- |
55 // | | 55 // | |
56 // ProxyMain->RequestNewOutputSurface()<----PostTask-------- | 56 // ProxyMain->RequestNewOutputSurface()<----PostTask-------- |
57 // . | 57 // . |
58 // . | 58 // . |
59 // ProxyMain->LayerTreeHostClosed | 59 // ProxyMain->Stop() |
60 // | | 60 // | |
61 // --------------------------------------------------------------------------- | 61 // --------------------------------------------------------------------------- |
62 // ChannelMain::SetLayerTreeClosedOnImpl---PostTask---> ProxyImpl-> | 62 // ChannelMain::SynchronouslyCloseImpl ---PostTask---> ThreadedChannel:: |
63 // SetLayerTreeClosed | 63 // CloseImplOnImpl |
64 // ---------------------------------------------------------------------------- | 64 // ---------------------------------------------------------------------------- |
| 65 // |
| 66 // ThreadedChannel is created and destroyed on the main thread but can be |
| 67 // called from main or impl thread. It is safe for the Threadedchannel to be |
| 68 // called on the impl thread because: |
| 69 // 1) The only impl-threaded callers of ThreadedChannel are the ThreadedChannel |
| 70 // itself and ProxyImpl which is created and owned by the ThreadedChannel. |
| 71 // 2) ThreadedChannel blocks the main thread in |
| 72 // ThreadedChannel::SynchronouslyCloseImpl to wait for the impl-thread teardown |
| 73 // to complete, so there is no risk of any queued tasks calling it on the impl |
| 74 // thread after it has been deleted on the main thread. |
65 | 75 |
66 class CC_EXPORT ThreadedChannel : public ChannelMain, public ChannelImpl { | 76 class CC_EXPORT ThreadedChannel : public ChannelMain, public ChannelImpl { |
67 public: | 77 public: |
68 static scoped_ptr<ThreadedChannel> Create( | 78 static scoped_ptr<ThreadedChannel> Create( |
69 // TODO(khushalsagar): Make this ProxyMain* and write the initialization | 79 ProxyMain* proxy_main, |
70 // sequence. Currently ThreadProxy implements both so we pass the pointer | |
71 // and set ProxyImpl. | |
72 ThreadProxy* thread_proxy, | |
73 TaskRunnerProvider* task_runner_provider); | 80 TaskRunnerProvider* task_runner_provider); |
74 | 81 |
75 ~ThreadedChannel() override; | 82 ~ThreadedChannel() override; |
76 | 83 |
77 // ChannelMain Implementation | 84 // ChannelMain Implementation |
78 void SetThrottleFrameProductionOnImpl(bool throttle) override; | 85 void SetThrottleFrameProductionOnImpl(bool throttle) override; |
79 void UpdateTopControlsStateOnImpl(TopControlsState constraints, | 86 void UpdateTopControlsStateOnImpl(TopControlsState constraints, |
80 TopControlsState current, | 87 TopControlsState current, |
81 bool animate) override; | 88 bool animate) override; |
82 void InitializeOutputSurfaceOnImpl(OutputSurface* output_surface) override; | 89 void InitializeOutputSurfaceOnImpl(OutputSurface* output_surface) override; |
83 void MainThreadHasStoppedFlingingOnImpl() override; | 90 void MainThreadHasStoppedFlingingOnImpl() override; |
84 void SetInputThrottledUntilCommitOnImpl(bool is_throttled) override; | 91 void SetInputThrottledUntilCommitOnImpl(bool is_throttled) override; |
85 void SetDeferCommitsOnImpl(bool defer_commits) override; | 92 void SetDeferCommitsOnImpl(bool defer_commits) override; |
86 void SetNeedsCommitOnImpl() override; | 93 void SetNeedsCommitOnImpl() override; |
87 void BeginMainFrameAbortedOnImpl( | 94 void BeginMainFrameAbortedOnImpl( |
88 CommitEarlyOutReason reason, | 95 CommitEarlyOutReason reason, |
89 base::TimeTicks main_thread_start_time) override; | 96 base::TimeTicks main_thread_start_time) override; |
90 void SetNeedsRedrawOnImpl(const gfx::Rect& damage_rect) override; | 97 void SetNeedsRedrawOnImpl(const gfx::Rect& damage_rect) override; |
91 void SetVisibleOnImpl(bool visible) override; | 98 void SetVisibleOnImpl(bool visible) override; |
92 | 99 |
93 // Blocking calls to ProxyImpl | 100 // Blocking calls to ProxyImpl |
94 void FinishAllRenderingOnImpl(CompletionEvent* completion) override; | 101 void FinishAllRenderingOnImpl(CompletionEvent* completion) override; |
95 void ReleaseOutputSurfaceOnImpl(CompletionEvent* completion) override; | 102 void ReleaseOutputSurfaceOnImpl(CompletionEvent* completion) override; |
96 void FinishGLOnImpl(CompletionEvent* completion) override; | |
97 void MainFrameWillHappenOnImplForTesting( | 103 void MainFrameWillHappenOnImplForTesting( |
98 CompletionEvent* completion, | 104 CompletionEvent* completion, |
99 bool* main_frame_will_happen) override; | 105 bool* main_frame_will_happen) override; |
100 void StartCommitOnImpl(CompletionEvent* completion, | 106 void StartCommitOnImpl(CompletionEvent* completion, |
101 LayerTreeHost* layer_tree_host, | 107 LayerTreeHost* layer_tree_host, |
102 base::TimeTicks main_thread_start_time, | 108 base::TimeTicks main_thread_start_time, |
103 bool hold_commit_for_activation) override; | 109 bool hold_commit_for_activation) override; |
104 void InitializeImplOnImpl(CompletionEvent* completion, | 110 void SynchronouslyInitializeImpl( |
105 LayerTreeHost* layer_tree_host) override; | 111 LayerTreeHost* layer_tree_host, |
106 void LayerTreeHostClosedOnImpl(CompletionEvent* completion) override; | 112 scoped_ptr<BeginFrameSource> external_begin_frame_source) override; |
| 113 void SynchronouslyCloseImpl() override; |
107 | 114 |
108 // ChannelImpl Implementation | 115 // ChannelImpl Implementation |
109 void DidCompleteSwapBuffers() override; | 116 void DidCompleteSwapBuffers() override; |
110 void SetRendererCapabilitiesMainCopy( | 117 void SetRendererCapabilitiesMainCopy( |
111 const RendererCapabilities& capabilities) override; | 118 const RendererCapabilities& capabilities) override; |
112 void BeginMainFrameNotExpectedSoon() override; | 119 void BeginMainFrameNotExpectedSoon() override; |
113 void DidCommitAndDrawFrame() override; | 120 void DidCommitAndDrawFrame() override; |
114 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue) override; | 121 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue) override; |
115 void DidLoseOutputSurface() override; | 122 void DidLoseOutputSurface() override; |
116 void RequestNewOutputSurface() override; | 123 void RequestNewOutputSurface() override; |
117 void DidInitializeOutputSurface( | 124 void DidInitializeOutputSurface( |
118 bool success, | 125 bool success, |
119 const RendererCapabilities& capabilities) override; | 126 const RendererCapabilities& capabilities) override; |
120 void DidCompletePageScaleAnimation() override; | 127 void DidCompletePageScaleAnimation() override; |
121 void PostFrameTimingEventsOnMain( | 128 void PostFrameTimingEventsOnMain( |
122 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, | 129 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, |
123 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) | 130 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) |
124 override; | 131 override; |
125 void BeginMainFrame( | 132 void BeginMainFrame( |
126 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) override; | 133 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) override; |
127 | 134 |
| 135 // Should be called on impl thread only. |
| 136 ProxyImpl* GetProxyImplForTesting() const; |
| 137 |
128 protected: | 138 protected: |
129 ThreadedChannel(ThreadProxy* thread_proxy, | 139 ThreadedChannel(ProxyMain* proxy_main, |
130 TaskRunnerProvider* task_runner_provider); | 140 TaskRunnerProvider* task_runner_provider); |
131 | 141 |
| 142 // Virtual for testing. |
| 143 virtual scoped_ptr<ProxyImpl> CreateProxyImpl( |
| 144 ChannelImpl* channel_impl, |
| 145 LayerTreeHost* layer_tree_host, |
| 146 TaskRunnerProvider* task_runner_provider, |
| 147 scoped_ptr<BeginFrameSource> external_begin_frame_source); |
| 148 |
132 private: | 149 private: |
| 150 // The members of this struct should be accessed on the main thread only. |
| 151 struct MainThreadOnly { |
| 152 explicit MainThreadOnly(ProxyMain* proxy_main); |
| 153 ~MainThreadOnly(); |
| 154 |
| 155 base::WeakPtrFactory<ProxyMain> proxy_main_weak_factory; |
| 156 bool initialized; |
| 157 }; |
| 158 |
| 159 // The members of this struct should be accessed on the impl thread only. |
| 160 struct CompositorThreadOnly { |
| 161 explicit CompositorThreadOnly(base::WeakPtr<ProxyMain> proxy_main_weak_ptr); |
| 162 ~CompositorThreadOnly(); |
| 163 |
| 164 scoped_ptr<ProxyImpl> proxy_impl; |
| 165 |
| 166 // We use a scoped_ptr for the weak ptr factory here since the factory is |
| 167 // created after ProxyImpl is created in InitializeImplOnImpl. Since the |
| 168 // weak ptrs are needed only by the ThreadedChannel to safely post tasks on |
| 169 // ProxyImpl to be run on the impl thread, we avoid creating it in ProxyImpl |
| 170 // and ensure that it is destroyed before ProxyImpl during the impl-thread |
| 171 // tear down in CloseImplOnImpl. |
| 172 scoped_ptr<base::WeakPtrFactory<ProxyImpl>> proxy_impl_weak_factory; |
| 173 |
| 174 // Used on the impl thread to queue calls to ProxyMain to be run on the main |
| 175 // thread. Since the weak pointer is invalidated after the impl-thread tear |
| 176 // down in SynchronouslyCloseImpl, this ensures that any tasks posted to |
| 177 // ProxyMain from the impl thread are abandoned after the impl side has been |
| 178 // destroyed. |
| 179 base::WeakPtr<ProxyMain> proxy_main_weak_ptr; |
| 180 }; |
| 181 |
| 182 // Called on impl thread. |
| 183 void InitializeImplOnImpl( |
| 184 CompletionEvent* completion, |
| 185 LayerTreeHost* layer_tree_host, |
| 186 scoped_ptr<BeginFrameSource> external_begin_frame_source); |
| 187 void CloseImplOnImpl(CompletionEvent* completion); |
| 188 |
| 189 bool IsInitialized() const; |
| 190 |
133 base::SingleThreadTaskRunner* MainThreadTaskRunner() const; | 191 base::SingleThreadTaskRunner* MainThreadTaskRunner() const; |
134 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const; | 192 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const; |
135 | 193 bool IsMainThread() const; |
136 ProxyMain* proxy_main_; | 194 bool IsImplThread() const; |
137 | |
138 ProxyImpl* proxy_impl_; | |
139 | 195 |
140 TaskRunnerProvider* task_runner_provider_; | 196 TaskRunnerProvider* task_runner_provider_; |
141 | 197 |
142 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 198 MainThreadOnly& main(); |
| 199 const MainThreadOnly& main() const; |
143 | 200 |
144 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner_; | 201 CompositorThreadOnly& impl(); |
| 202 const CompositorThreadOnly& impl() const; |
| 203 |
| 204 // Use accessors instead of this variable directly. |
| 205 MainThreadOnly main_thread_only_vars_unsafe_; |
| 206 |
| 207 // Use accessors instead of this variable directly. |
| 208 CompositorThreadOnly compositor_thread_vars_unsafe_; |
| 209 |
| 210 // Used on the main thread to safely queue calls to ProxyImpl to be run on the |
| 211 // impl thread. |
| 212 base::WeakPtr<ProxyImpl> proxy_impl_weak_ptr_; |
145 | 213 |
146 DISALLOW_COPY_AND_ASSIGN(ThreadedChannel); | 214 DISALLOW_COPY_AND_ASSIGN(ThreadedChannel); |
147 }; | 215 }; |
148 | 216 |
149 } // namespace cc | 217 } // namespace cc |
150 | 218 |
151 #endif // CC_TREES_THREADED_CHANNEL_H_ | 219 #endif // CC_TREES_THREADED_CHANNEL_H_ |
OLD | NEW |