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" | |
11 #include "cc/base/cc_export.h" | 10 #include "cc/base/cc_export.h" |
12 #include "cc/trees/channel_impl.h" | 11 #include "cc/trees/channel_impl.h" |
13 #include "cc/trees/channel_main.h" | 12 #include "cc/trees/channel_main.h" |
14 #include "cc/trees/proxy_common.h" | 13 #include "cc/trees/proxy_common.h" |
15 #include "cc/trees/proxy_impl.h" | 14 #include "cc/trees/proxy_impl.h" |
16 #include "cc/trees/proxy_main.h" | 15 #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; | |
26 class ProxyImpl; | 25 class ProxyImpl; |
27 class ProxyMain; | 26 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::SynchronouslyInitializeImpl ---PostTask---> ThreadedChannel:: | 42 // ChannelMain::InitializeImpl ---PostTask---> ThreadedChannel:: |
43 // InitializeImplOnImpl | 43 // InitializeImplOnImplThread |
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->Stop() | 59 // ProxyMain->LayerTreeHostClosed |
60 // | | 60 // | |
61 // --------------------------------------------------------------------------- | 61 // --------------------------------------------------------------------------- |
62 // ChannelMain::SynchronouslyCloseImpl ---PostTask---> ThreadedChannel:: | 62 // ChannelMain::SetLayerTreeClosedOnImpl---PostTask---> ProxyImpl-> |
63 // CloseImplOnImpl | 63 // SetLayerTreeClosed |
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. | |
75 | 65 |
76 class CC_EXPORT ThreadedChannel : public ChannelMain, public ChannelImpl { | 66 class CC_EXPORT ThreadedChannel : public ChannelMain, public ChannelImpl { |
77 public: | 67 public: |
78 static scoped_ptr<ThreadedChannel> Create( | 68 static scoped_ptr<ThreadedChannel> Create( |
79 ProxyMain* proxy_main, | 69 // TODO(khushalsagar): Make this ProxyMain* and write the initialization |
| 70 // sequence. Currently ThreadProxy implements both so we pass the pointer |
| 71 // and set ProxyImpl. |
| 72 ThreadProxy* thread_proxy, |
80 TaskRunnerProvider* task_runner_provider); | 73 TaskRunnerProvider* task_runner_provider); |
81 | 74 |
82 ~ThreadedChannel() override; | 75 ~ThreadedChannel() override; |
83 | 76 |
84 // ChannelMain Implementation | 77 // ChannelMain Implementation |
85 void SetThrottleFrameProductionOnImpl(bool throttle) override; | 78 void SetThrottleFrameProductionOnImpl(bool throttle) override; |
86 void UpdateTopControlsStateOnImpl(TopControlsState constraints, | 79 void UpdateTopControlsStateOnImpl(TopControlsState constraints, |
87 TopControlsState current, | 80 TopControlsState current, |
88 bool animate) override; | 81 bool animate) override; |
89 void InitializeOutputSurfaceOnImpl(OutputSurface* output_surface) override; | 82 void InitializeOutputSurfaceOnImpl(OutputSurface* output_surface) override; |
90 void MainThreadHasStoppedFlingingOnImpl() override; | 83 void MainThreadHasStoppedFlingingOnImpl() override; |
91 void SetInputThrottledUntilCommitOnImpl(bool is_throttled) override; | 84 void SetInputThrottledUntilCommitOnImpl(bool is_throttled) override; |
92 void SetDeferCommitsOnImpl(bool defer_commits) override; | 85 void SetDeferCommitsOnImpl(bool defer_commits) override; |
93 void SetNeedsCommitOnImpl() override; | 86 void SetNeedsCommitOnImpl() override; |
94 void BeginMainFrameAbortedOnImpl( | 87 void BeginMainFrameAbortedOnImpl( |
95 CommitEarlyOutReason reason, | 88 CommitEarlyOutReason reason, |
96 base::TimeTicks main_thread_start_time) override; | 89 base::TimeTicks main_thread_start_time) override; |
97 void SetNeedsRedrawOnImpl(const gfx::Rect& damage_rect) override; | 90 void SetNeedsRedrawOnImpl(const gfx::Rect& damage_rect) override; |
98 void SetVisibleOnImpl(bool visible) override; | 91 void SetVisibleOnImpl(bool visible) override; |
99 | 92 |
100 // Blocking calls to ProxyImpl | 93 // Blocking calls to ProxyImpl |
101 void FinishAllRenderingOnImpl(CompletionEvent* completion) override; | 94 void FinishAllRenderingOnImpl(CompletionEvent* completion) override; |
102 void ReleaseOutputSurfaceOnImpl(CompletionEvent* completion) override; | 95 void ReleaseOutputSurfaceOnImpl(CompletionEvent* completion) override; |
| 96 void FinishGLOnImpl(CompletionEvent* completion) override; |
103 void MainFrameWillHappenOnImplForTesting( | 97 void MainFrameWillHappenOnImplForTesting( |
104 CompletionEvent* completion, | 98 CompletionEvent* completion, |
105 bool* main_frame_will_happen) override; | 99 bool* main_frame_will_happen) override; |
106 void StartCommitOnImpl(CompletionEvent* completion, | 100 void StartCommitOnImpl(CompletionEvent* completion, |
107 LayerTreeHost* layer_tree_host, | 101 LayerTreeHost* layer_tree_host, |
108 base::TimeTicks main_thread_start_time, | 102 base::TimeTicks main_thread_start_time, |
109 bool hold_commit_for_activation) override; | 103 bool hold_commit_for_activation) override; |
110 void SynchronouslyInitializeImpl( | 104 void InitializeImplOnImpl(CompletionEvent* completion, |
111 LayerTreeHost* layer_tree_host, | 105 LayerTreeHost* layer_tree_host) override; |
112 scoped_ptr<BeginFrameSource> external_begin_frame_source) override; | 106 void LayerTreeHostClosedOnImpl(CompletionEvent* completion) override; |
113 void SynchronouslyCloseImpl() override; | |
114 | 107 |
115 // ChannelImpl Implementation | 108 // ChannelImpl Implementation |
116 void DidCompleteSwapBuffers() override; | 109 void DidCompleteSwapBuffers() override; |
117 void SetRendererCapabilitiesMainCopy( | 110 void SetRendererCapabilitiesMainCopy( |
118 const RendererCapabilities& capabilities) override; | 111 const RendererCapabilities& capabilities) override; |
119 void BeginMainFrameNotExpectedSoon() override; | 112 void BeginMainFrameNotExpectedSoon() override; |
120 void DidCommitAndDrawFrame() override; | 113 void DidCommitAndDrawFrame() override; |
121 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue) override; | 114 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue) override; |
122 void DidLoseOutputSurface() override; | 115 void DidLoseOutputSurface() override; |
123 void RequestNewOutputSurface() override; | 116 void RequestNewOutputSurface() override; |
124 void DidInitializeOutputSurface( | 117 void DidInitializeOutputSurface( |
125 bool success, | 118 bool success, |
126 const RendererCapabilities& capabilities) override; | 119 const RendererCapabilities& capabilities) override; |
127 void DidCompletePageScaleAnimation() override; | 120 void DidCompletePageScaleAnimation() override; |
128 void PostFrameTimingEventsOnMain( | 121 void PostFrameTimingEventsOnMain( |
129 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, | 122 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, |
130 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) | 123 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) |
131 override; | 124 override; |
132 void BeginMainFrame( | 125 void BeginMainFrame( |
133 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) override; | 126 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) override; |
134 | 127 |
135 // Should be called on impl thread only. | |
136 ProxyImpl* GetProxyImplForTesting() const; | |
137 | |
138 protected: | 128 protected: |
139 ThreadedChannel(ProxyMain* proxy_main, | 129 ThreadedChannel(ThreadProxy* thread_proxy, |
140 TaskRunnerProvider* task_runner_provider); | 130 TaskRunnerProvider* task_runner_provider); |
141 | 131 |
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 | |
149 private: | 132 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 | |
191 base::SingleThreadTaskRunner* MainThreadTaskRunner() const; | 133 base::SingleThreadTaskRunner* MainThreadTaskRunner() const; |
192 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const; | 134 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const; |
193 bool IsMainThread() const; | 135 |
194 bool IsImplThread() const; | 136 ProxyMain* proxy_main_; |
| 137 |
| 138 ProxyImpl* proxy_impl_; |
195 | 139 |
196 TaskRunnerProvider* task_runner_provider_; | 140 TaskRunnerProvider* task_runner_provider_; |
197 | 141 |
198 MainThreadOnly& main(); | 142 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
199 const MainThreadOnly& main() const; | |
200 | 143 |
201 CompositorThreadOnly& impl(); | 144 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner_; |
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_; | |
213 | 145 |
214 DISALLOW_COPY_AND_ASSIGN(ThreadedChannel); | 146 DISALLOW_COPY_AND_ASSIGN(ThreadedChannel); |
215 }; | 147 }; |
216 | 148 |
217 } // namespace cc | 149 } // namespace cc |
218 | 150 |
219 #endif // CC_TREES_THREADED_CHANNEL_H_ | 151 #endif // CC_TREES_THREADED_CHANNEL_H_ |
OLD | NEW |