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