OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 #include "cc/test/layer_tree_host_remote_for_testing.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "cc/animation/animation_host.h" | |
9 #include "cc/blimp/compositor_proto_state.h" | |
10 #include "cc/blimp/compositor_state_deserializer.h" | |
11 #include "cc/blimp/remote_compositor_bridge.h" | |
12 #include "cc/layers/layer.h" | |
13 #include "cc/proto/compositor_message.pb.h" | |
14 #include "cc/test/fake_image_serialization_processor.h" | |
15 #include "cc/test/remote_client_layer_factory.h" | |
16 #include "cc/trees/layer_tree_host_client.h" | |
17 #include "cc/trees/layer_tree_host_in_process.h" | |
18 #include "cc/trees/mutator_host.h" | |
19 | |
20 namespace cc { | |
21 | |
22 class LayerTreeHostRemoteForTesting::RemoteCompositorBridgeImpl | |
23 : public RemoteCompositorBridge { | |
24 public: | |
25 RemoteCompositorBridgeImpl( | |
26 scoped_refptr<base::SingleThreadTaskRunner> compositor_main_task_runner) | |
27 : RemoteCompositorBridge(std::move(compositor_main_task_runner)) {} | |
28 | |
29 ~RemoteCompositorBridgeImpl() override = default; | |
30 | |
31 void SetRemoteHost(LayerTreeHostRemoteForTesting* layer_tree_host_remote) { | |
32 DCHECK(layer_tree_host_remote); | |
33 layer_tree_host_remote_ = layer_tree_host_remote; | |
34 } | |
35 | |
36 // RemoteCompositorBridge implementation. | |
37 void BindToClient(RemoteCompositorBridgeClient* client) override { | |
38 DCHECK(!client_); | |
39 client_ = client; | |
40 } | |
41 void ScheduleMainFrame() override { | |
42 layer_tree_host_remote_->RemoteHostNeedsMainFrame(); | |
43 } | |
44 void ProcessCompositorStateUpdate( | |
45 std::unique_ptr<CompositorProtoState> compositor_proto_state) override { | |
46 layer_tree_host_remote_->ProcessRemoteCompositorUpdate( | |
47 std::move(compositor_proto_state)); | |
48 } | |
49 | |
50 private: | |
51 LayerTreeHostRemoteForTesting* layer_tree_host_remote_ = nullptr; | |
52 RemoteCompositorBridgeClient* client_ = nullptr; | |
53 }; | |
54 | |
55 class LayerTreeHostRemoteForTesting::LayerTreeHostInProcessClient | |
56 : public LayerTreeHostClient { | |
57 public: | |
58 LayerTreeHostInProcessClient( | |
59 LayerTreeHostRemoteForTesting* layer_tree_host_remote) | |
60 : layer_tree_host_remote_(layer_tree_host_remote) {} | |
61 | |
62 ~LayerTreeHostInProcessClient() override = default; | |
63 | |
64 void WillBeginMainFrame() override {} | |
65 void BeginMainFrame(const BeginFrameArgs& args) override { | |
66 layer_tree_host_remote_->BeginRemoteMainFrame(); | |
67 } | |
68 void BeginMainFrameNotExpectedSoon() override {} | |
69 void DidBeginMainFrame() override {} | |
70 void UpdateLayerTreeHost() override {} | |
71 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta, | |
72 const gfx::Vector2dF& outer_delta, | |
73 const gfx::Vector2dF& elastic_overscroll_delta, | |
74 float page_scale, | |
75 float top_controls_delta) override { | |
76 layer_tree_host_remote_->compositor_state_deserializer_ | |
77 ->ApplyViewportDeltas(inner_delta, outer_delta, | |
78 elastic_overscroll_delta, page_scale, | |
79 top_controls_delta); | |
80 } | |
81 void RequestNewCompositorFrameSink() override { | |
82 layer_tree_host_remote_->client()->RequestNewCompositorFrameSink(); | |
83 } | |
84 void DidInitializeCompositorFrameSink() override { | |
85 layer_tree_host_remote_->client()->DidInitializeCompositorFrameSink(); | |
86 } | |
87 void DidFailToInitializeCompositorFrameSink() override { | |
88 layer_tree_host_remote_->client()->DidFailToInitializeCompositorFrameSink(); | |
89 } | |
90 void WillCommit() override {} | |
91 void DidCommit() override {} | |
92 void DidCommitAndDrawFrame() override { | |
93 layer_tree_host_remote_->client()->DidCommitAndDrawFrame(); | |
94 } | |
95 void DidReceiveCompositorFrameAck() override { | |
96 layer_tree_host_remote_->client()->DidReceiveCompositorFrameAck(); | |
97 } | |
98 void DidCompletePageScaleAnimation() override { | |
99 NOTREACHED() << "The remote mode doesn't support sending animations"; | |
100 } | |
101 | |
102 private: | |
103 LayerTreeHostRemoteForTesting* layer_tree_host_remote_; | |
104 }; | |
105 | |
106 // static | |
107 std::unique_ptr<RemoteCompositorBridge> | |
108 LayerTreeHostRemoteForTesting::CreateRemoteCompositorBridge( | |
109 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) { | |
110 return base::MakeUnique<RemoteCompositorBridgeImpl>( | |
111 std::move(main_task_runner)); | |
112 } | |
113 | |
114 // static | |
115 std::unique_ptr<LayerTreeHostRemoteForTesting> | |
116 LayerTreeHostRemoteForTesting::Create( | |
117 LayerTreeHostClient* client, | |
118 MutatorHost* mutator_host, | |
119 LayerTreeSettings const* settings, | |
120 TaskGraphRunner* task_graph_runner, | |
121 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
122 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) { | |
123 std::unique_ptr<FakeImageSerializationProcessor> | |
124 image_serialization_processor = | |
125 base::MakeUnique<FakeImageSerializationProcessor>(); | |
126 | |
127 LayerTreeHostRemote::InitParams params; | |
128 params.client = client; | |
129 params.main_task_runner = main_task_runner; | |
130 params.mutator_host = mutator_host; | |
131 params.remote_compositor_bridge = | |
132 CreateRemoteCompositorBridge(main_task_runner); | |
133 params.engine_picture_cache = | |
134 image_serialization_processor->CreateEnginePictureCache(); | |
135 params.settings = settings; | |
136 | |
137 std::unique_ptr<LayerTreeHostRemoteForTesting> layer_tree_host = | |
138 base::WrapUnique(new LayerTreeHostRemoteForTesting(¶ms)); | |
139 layer_tree_host->Initialize(task_graph_runner, main_task_runner, | |
140 impl_task_runner, | |
141 std::move(image_serialization_processor)); | |
142 return layer_tree_host; | |
143 } | |
144 | |
145 LayerTreeHostRemoteForTesting::LayerTreeHostRemoteForTesting(InitParams* params) | |
146 : LayerTreeHostRemote(params), | |
147 layer_tree_host_in_process_client_( | |
148 base::MakeUnique<LayerTreeHostInProcessClient>(this)) {} | |
149 | |
150 LayerTreeHostRemoteForTesting::~LayerTreeHostRemoteForTesting() { | |
151 animation_host_->SetMutatorHostClient(nullptr); | |
152 compositor_state_deserializer_ = nullptr; | |
153 layer_tree_host_in_process_ = nullptr; | |
154 } | |
155 | |
156 void LayerTreeHostRemoteForTesting::SetVisible(bool visible) { | |
157 LayerTreeHostRemote::SetVisible(visible); | |
158 | |
159 // We need to tell the InProcessHost about visibility changes as well. | |
160 layer_tree_host_in_process_->SetVisible(visible); | |
161 } | |
162 | |
163 void LayerTreeHostRemoteForTesting::SetCompositorFrameSink( | |
164 std::unique_ptr<CompositorFrameSink> compositor_frame_sink) { | |
165 // Pass through to the InProcessHost since this should be called only in | |
166 // response to a request made by it. | |
167 layer_tree_host_in_process_->SetCompositorFrameSink( | |
168 std::move(compositor_frame_sink)); | |
169 } | |
170 | |
171 std::unique_ptr<CompositorFrameSink> | |
172 LayerTreeHostRemoteForTesting::ReleaseCompositorFrameSink() { | |
173 // Pass through to the InProcessHost since that holds the CompositorFrameSink. | |
174 return layer_tree_host_in_process_->ReleaseCompositorFrameSink(); | |
175 } | |
176 | |
177 void LayerTreeHostRemoteForTesting::SetNeedsRedrawRect( | |
178 const gfx::Rect& damage_rect) { | |
179 // Draw requests pass through to the InProcessHost. | |
180 layer_tree_host_in_process_->SetNeedsRedrawRect(damage_rect); | |
181 } | |
182 | |
183 void LayerTreeHostRemoteForTesting::SetNextCommitForcesRedraw() { | |
184 // Draw requests pass through to the InProcessHost. | |
185 layer_tree_host_in_process_->SetNextCommitForcesRedraw(); | |
186 } | |
187 | |
188 void LayerTreeHostRemoteForTesting::NotifyInputThrottledUntilCommit() { | |
189 // Pass through because the InProcessHost has the input handler. | |
190 layer_tree_host_in_process_->NotifyInputThrottledUntilCommit(); | |
191 } | |
192 | |
193 const base::WeakPtr<InputHandler>& | |
194 LayerTreeHostRemoteForTesting::GetInputHandler() const { | |
195 return layer_tree_host_in_process_->GetInputHandler(); | |
196 } | |
197 | |
198 void LayerTreeHostRemoteForTesting::Initialize( | |
199 TaskGraphRunner* task_graph_runner, | |
200 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
201 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, | |
202 std::unique_ptr<FakeImageSerializationProcessor> | |
203 image_serialization_processor) { | |
204 image_serialization_processor_ = std::move(image_serialization_processor); | |
205 RemoteCompositorBridgeImpl* remote_compositor_bridge_impl = | |
206 static_cast<RemoteCompositorBridgeImpl*>(remote_compositor_bridge()); | |
207 remote_compositor_bridge_impl->SetRemoteHost(this); | |
208 | |
209 animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN); | |
210 layer_tree_host_in_process_ = CreateLayerTreeHostInProcess( | |
211 layer_tree_host_in_process_client_.get(), task_graph_runner, | |
212 GetSettings(), main_task_runner, impl_task_runner, animation_host_.get()); | |
213 | |
214 compositor_state_deserializer_ = | |
215 base::MakeUnique<CompositorStateDeserializer>( | |
216 layer_tree_host_in_process_.get(), | |
217 image_serialization_processor_->CreateClientPictureCache(), this); | |
218 | |
219 // Override the LayerFactory since a lot of tests rely on the fact that Layers | |
220 // and LayerImpls have matching ids. | |
221 compositor_state_deserializer_->SetLayerFactoryForTesting( | |
222 base::MakeUnique<RemoteClientLayerFactory>()); | |
223 | |
224 // Override the TaskRunnerProvider since tests may rely on accessing the impl | |
225 // task runner using it. | |
226 SetTaskRunnerProviderForTesting( | |
227 TaskRunnerProvider::Create(main_task_runner, impl_task_runner)); | |
228 } | |
229 | |
230 std::unique_ptr<LayerTreeHostInProcess> | |
231 LayerTreeHostRemoteForTesting::CreateLayerTreeHostInProcess( | |
232 LayerTreeHostClient* client, | |
233 TaskGraphRunner* task_graph_runner, | |
234 const LayerTreeSettings& settings, | |
235 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
236 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, | |
237 MutatorHost* mutator_host) { | |
238 LayerTreeHostInProcess::InitParams params; | |
239 | |
240 params.client = client; | |
241 params.task_graph_runner = task_graph_runner; | |
242 params.settings = &settings; | |
243 params.main_task_runner = main_task_runner; | |
244 params.mutator_host = mutator_host; | |
245 | |
246 return LayerTreeHostInProcess::CreateThreaded(impl_task_runner, ¶ms); | |
247 } | |
248 | |
249 void LayerTreeHostRemoteForTesting::DidUpdateLocalState() { | |
250 client_state_dirty_ = true; | |
251 } | |
252 | |
253 void LayerTreeHostRemoteForTesting::DispatchDrawAndSubmitCallbacks() { | |
254 // Don't dispatch callbacks right after the commit on the remote host. Since | |
255 // tests rely on CompositorFrames being swapped on the CompositorFrameSink, | |
256 // we wait for these callbacks from the LayerTreeHostInProcess. | |
257 } | |
258 | |
259 void LayerTreeHostRemoteForTesting::RemoteHostNeedsMainFrame() { | |
260 layer_tree_host_in_process_->SetNeedsAnimate(); | |
261 } | |
262 | |
263 void LayerTreeHostRemoteForTesting::BeginRemoteMainFrame() { | |
264 // Send scroll/scale updates first if modified on the impl thread. | |
265 if (client_state_dirty_) { | |
266 client_state_dirty_ = false; | |
267 proto::ClientStateUpdate client_state_update; | |
268 compositor_state_deserializer_->PullClientStateUpdate(&client_state_update); | |
269 ApplyStateUpdateFromClient(client_state_update); | |
270 | |
271 // Tell the host on the client that the updates were applied to the state | |
272 // on the remote host, in case the main frame on the remote host is aborted. | |
273 compositor_state_deserializer_->DidApplyStateUpdatesOnEngine(); | |
274 } | |
275 | |
276 BeginMainFrame(); | |
277 } | |
278 | |
279 void LayerTreeHostRemoteForTesting::ProcessRemoteCompositorUpdate( | |
280 std::unique_ptr<CompositorProtoState> compositor_proto_state) { | |
281 DCHECK(layer_tree_host_in_process_->CommitRequested()); | |
282 | |
283 // Deserialize the update from the remote host into client side LTH in | |
284 // process. This bypasses the network layer. | |
285 const proto::LayerTreeHost& layer_tree_host_proto = | |
286 compositor_proto_state->compositor_message->layer_tree_host(); | |
287 compositor_state_deserializer_->DeserializeCompositorUpdate( | |
288 layer_tree_host_proto); | |
289 | |
290 const proto::LayerUpdate& layer_updates = | |
291 compositor_proto_state->compositor_message->layer_tree_host() | |
292 .layer_updates(); | |
293 for (int i = 0; i < layer_updates.layers_size(); ++i) { | |
294 int engine_layer_id = layer_updates.layers(i).id(); | |
295 Layer* engine_layer = GetLayerTree()->LayerById(engine_layer_id); | |
296 Layer* client_layer = | |
297 compositor_state_deserializer_->GetLayerForEngineId(engine_layer_id); | |
298 | |
299 // Copy test only layer data that are not serialized into network messages. | |
300 // So in test cases, layers on the client have the same states as their | |
301 // corresponding layers on the engine. | |
302 client_layer->SetForceRenderSurfaceForTesting( | |
303 engine_layer->force_render_surface_for_testing()); | |
304 } | |
305 | |
306 // The only case where the remote host would give a compositor update is if | |
307 // they wanted the main frame to go till the commit pipeline stage. So | |
308 // request one to make sure that the in process main frame also goes till | |
309 // the commit step. | |
310 layer_tree_host_in_process_->SetNeedsCommit(); | |
311 } | |
312 | |
313 } // namespace cc | |
OLD | NEW |