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 base::WeakPtrFactory<ProxyMain> proxy_main_weak_factory; | |
153 bool initialized; | |
154 | |
155 explicit MainThreadOnly(ProxyMain* proxy_main); | |
156 ~MainThreadOnly(); | |
157 }; | |
158 | |
159 // The members of this struct should be accessed on the impl thread only. | |
160 struct CompositorThreadOnly { | |
161 scoped_ptr<ProxyImpl> proxy_impl; | |
162 scoped_ptr<base::WeakPtrFactory<ProxyImpl>> proxy_impl_weak_factory; | |
vmpstr
2015/12/04 00:25:41
Can you leave a comment here explaining that this
Khushal
2015/12/04 22:45:23
Done.
| |
163 | |
164 // Used on the impl thread to queue calls to ProxyMain to be run on the main | |
165 // thread. Since the weak pointer is invalidated after the impl-thread tear | |
166 // down in SynchronouslyCloseImpl, this ensures that any tasks posted to | |
167 // ProxyMain from the impl thread are abandoned after the impl side has been | |
168 // destroyed. | |
169 base::WeakPtr<ProxyMain> proxy_main_weak_ptr; | |
170 | |
171 explicit CompositorThreadOnly(base::WeakPtr<ProxyMain> proxy_main_weak_ptr); | |
vmpstr
2015/12/04 00:25:41
Move these to the top please
Khushal
2015/12/04 22:45:22
Done.
| |
172 ~CompositorThreadOnly(); | |
173 }; | |
174 | |
175 // Called on impl thread. | |
176 void InitializeImplOnImpl( | |
177 CompletionEvent* completion, | |
178 LayerTreeHost* layer_tree_host, | |
179 scoped_ptr<BeginFrameSource> external_begin_frame_source); | |
180 void CloseImplOnImpl(CompletionEvent* completion); | |
181 | |
182 bool IsInitialized() const; | |
183 | |
133 base::SingleThreadTaskRunner* MainThreadTaskRunner() const; | 184 base::SingleThreadTaskRunner* MainThreadTaskRunner() const; |
134 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const; | 185 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const; |
135 | 186 bool IsMainThread() const; |
136 ProxyMain* proxy_main_; | 187 bool IsImplThread() const; |
137 | |
138 ProxyImpl* proxy_impl_; | |
139 | 188 |
140 TaskRunnerProvider* task_runner_provider_; | 189 TaskRunnerProvider* task_runner_provider_; |
141 | 190 |
142 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 191 // Use accessors instead of this variable directly. |
192 MainThreadOnly main_thread_only_vars_unsafe_; | |
193 MainThreadOnly& main(); | |
194 const MainThreadOnly& main() const; | |
vmpstr
2015/12/04 00:25:41
I understand the logical grouping, but I think we
Khushal
2015/12/04 22:45:23
Done.
| |
143 | 195 |
144 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner_; | 196 // Use accessors instead of this variable directly. |
197 CompositorThreadOnly compositor_thread_vars_unsafe_; | |
198 CompositorThreadOnly& impl(); | |
199 const CompositorThreadOnly& impl() const; | |
200 | |
201 // Used on the main thread to safely queue calls to ProxyImpl to be run on the | |
202 // impl thread. | |
203 base::WeakPtr<ProxyImpl> proxy_impl_weak_ptr_; | |
145 | 204 |
146 DISALLOW_COPY_AND_ASSIGN(ThreadedChannel); | 205 DISALLOW_COPY_AND_ASSIGN(ThreadedChannel); |
147 }; | 206 }; |
148 | 207 |
149 } // namespace cc | 208 } // namespace cc |
150 | 209 |
151 #endif // CC_TREES_THREADED_CHANNEL_H_ | 210 #endif // CC_TREES_THREADED_CHANNEL_H_ |
OLD | NEW |