Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: blimp/client/feature/compositor/blimp_compositor.cc

Issue 2274323002: Expose Blimp dependencies to the embedder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@khushal_baseline_1
Patch Set: Addressed Khushal's initial comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "blimp/client/feature/compositor/blimp_compositor.h" 5 #include "blimp/client/feature/compositor/blimp_compositor.h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/numerics/safe_conversions.h" 10 #include "base/numerics/safe_conversions.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
13 #include "base/threading/thread_local.h" 13 #include "base/threading/thread_local.h"
14 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "blimp/client/core/compositor/blimp_compositor_dependencies.h"
16 #include "blimp/client/core/compositor/delegated_output_surface.h" 17 #include "blimp/client/core/compositor/delegated_output_surface.h"
17 #include "blimp/client/feature/compositor/blimp_context_provider.h" 18 #include "blimp/client/public/compositor/compositor_dependencies.h"
18 #include "blimp/net/blimp_stats.h" 19 #include "blimp/net/blimp_stats.h"
19 #include "cc/animation/animation_host.h" 20 #include "cc/animation/animation_host.h"
20 #include "cc/layers/layer.h" 21 #include "cc/layers/layer.h"
21 #include "cc/layers/surface_layer.h" 22 #include "cc/layers/surface_layer.h"
22 #include "cc/output/output_surface.h" 23 #include "cc/output/output_surface.h"
23 #include "cc/proto/compositor_message.pb.h" 24 #include "cc/proto/compositor_message.pb.h"
24 #include "cc/surfaces/surface.h" 25 #include "cc/surfaces/surface.h"
25 #include "cc/surfaces/surface_factory.h" 26 #include "cc/surfaces/surface_factory.h"
26 #include "cc/surfaces/surface_id_allocator.h" 27 #include "cc/surfaces/surface_id_allocator.h"
27 #include "cc/surfaces/surface_manager.h" 28 #include "cc/surfaces/surface_manager.h"
28 #include "cc/trees/layer_tree_host.h" 29 #include "cc/trees/layer_tree_host.h"
30 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
29 #include "net/base/net_errors.h" 31 #include "net/base/net_errors.h"
30 #include "ui/gl/gl_surface.h" 32 #include "ui/gl/gl_surface.h"
31 33
32 namespace blimp { 34 namespace blimp {
33 namespace client { 35 namespace client {
34 36
35 namespace { 37 namespace {
36 38
37 void SatisfyCallback(cc::SurfaceManager* manager, 39 void SatisfyCallback(cc::SurfaceManager* manager,
38 const cc::SurfaceSequence& sequence) { 40 const cc::SurfaceSequence& sequence) {
39 std::vector<uint32_t> sequences; 41 std::vector<uint32_t> sequences;
40 sequences.push_back(sequence.sequence); 42 sequences.push_back(sequence.sequence);
41 manager->DidSatisfySequences(sequence.client_id, &sequences); 43 manager->DidSatisfySequences(sequence.client_id, &sequences);
42 } 44 }
43 45
44 void RequireCallback(cc::SurfaceManager* manager, 46 void RequireCallback(cc::SurfaceManager* manager,
45 const cc::SurfaceId& id, 47 const cc::SurfaceId& id,
46 const cc::SurfaceSequence& sequence) { 48 const cc::SurfaceSequence& sequence) {
47 cc::Surface* surface = manager->GetSurfaceForId(id); 49 cc::Surface* surface = manager->GetSurfaceForId(id);
48 if (!surface) { 50 if (!surface) {
49 LOG(ERROR) << "Attempting to require callback on nonexistent surface"; 51 LOG(ERROR) << "Attempting to require callback on nonexistent surface";
50 return; 52 return;
51 } 53 }
52 surface->AddDestructionDependency(sequence); 54 surface->AddDestructionDependency(sequence);
53 } 55 }
54 56
55 } // namespace 57 } // namespace
56 58
57 BlimpCompositor::BlimpCompositor(int render_widget_id, 59 BlimpCompositor::BlimpCompositor(
58 cc::SurfaceManager* surface_manager, 60 int render_widget_id,
59 uint32_t surface_client_id, 61 BlimpCompositorDependencies* compositor_dependencies,
60 BlimpCompositorClient* client) 62 BlimpCompositorClient* client)
61 : render_widget_id_(render_widget_id), 63 : render_widget_id_(render_widget_id),
62 client_(client), 64 client_(client),
65 compositor_dependencies_(compositor_dependencies),
63 host_should_be_visible_(false), 66 host_should_be_visible_(false),
64 surface_manager_(surface_manager), 67 output_surface_request_pending_(false),
65 surface_id_allocator_(
66 base::MakeUnique<cc::SurfaceIdAllocator>(surface_client_id)),
67 layer_(cc::Layer::Create()), 68 layer_(cc::Layer::Create()),
68 remote_proto_channel_receiver_(nullptr), 69 remote_proto_channel_receiver_(nullptr),
69 weak_ptr_factory_(this) { 70 weak_ptr_factory_(this) {
70 DCHECK(thread_checker_.CalledOnValidThread()); 71 DCHECK(thread_checker_.CalledOnValidThread());
71 surface_manager_->RegisterSurfaceClientId(surface_id_allocator_->client_id()); 72
73 surface_id_allocator_ = base::MakeUnique<cc::SurfaceIdAllocator>(
74 GetEmbedderDeps()->AllocateSurfaceId());
75 GetEmbedderDeps()->GetSurfaceManager()->RegisterSurfaceClientId(
76 surface_id_allocator_->client_id());
72 } 77 }
73 78
74 BlimpCompositor::~BlimpCompositor() { 79 BlimpCompositor::~BlimpCompositor() {
75 DCHECK(thread_checker_.CalledOnValidThread()); 80 DCHECK(thread_checker_.CalledOnValidThread());
76 81
77 if (host_) 82 if (host_)
78 DestroyLayerTreeHost(); 83 DestroyLayerTreeHost();
79 surface_manager_->InvalidateSurfaceClientId( 84
85 GetEmbedderDeps()->GetSurfaceManager()->InvalidateSurfaceClientId(
80 surface_id_allocator_->client_id()); 86 surface_id_allocator_->client_id());
81 } 87 }
82 88
83 void BlimpCompositor::SetVisible(bool visible) { 89 void BlimpCompositor::SetVisible(bool visible) {
84 host_should_be_visible_ = visible; 90 host_should_be_visible_ = visible;
85 if (host_) 91 if (host_)
86 host_->SetVisible(host_should_be_visible_); 92 host_->SetVisible(host_should_be_visible_);
87 } 93 }
88 94
89 bool BlimpCompositor::OnTouchEvent(const ui::MotionEvent& motion_event) { 95 bool BlimpCompositor::OnTouchEvent(const ui::MotionEvent& motion_event) {
90 if (input_manager_) 96 if (input_manager_)
91 return input_manager_->OnTouchEvent(motion_event); 97 return input_manager_->OnTouchEvent(motion_event);
92 return false; 98 return false;
93 } 99 }
94 100
95 void BlimpCompositor::RequestNewOutputSurface() { 101 void BlimpCompositor::RequestNewOutputSurface() {
96 DCHECK(!surface_factory_) 102 DCHECK(!surface_factory_);
97 << "Any connection to the old output surface should have been destroyed"; 103 DCHECK(!output_surface_request_pending_);
98 104
99 scoped_refptr<BlimpContextProvider> compositor_context_provider = 105 output_surface_request_pending_ = true;
100 BlimpContextProvider::Create(gfx::kNullAcceleratedWidget, 106 GetEmbedderDeps()->GetContextProvider(
101 client_->GetGpuMemoryBufferManager()); 107 base::Bind(&BlimpCompositor::OnContextProviderCreated,
108 weak_ptr_factory_.GetWeakPtr()));
109 }
102 110
103 // TODO(khushalsagar): Make a worker context and bind it to the current 111 void BlimpCompositor::DidInitializeOutputSurface() {
104 // thread: 112 output_surface_request_pending_ = false;
105 // Worker context is bound to the main thread in RenderThreadImpl. One day
106 // that will change and then this will have to be removed.
107 // worker_context_provider->BindToCurrentThread();
108
109 std::unique_ptr<DelegatedOutputSurface> delegated_output_surface =
110 base::MakeUnique<DelegatedOutputSurface>(
111 std::move(compositor_context_provider), nullptr,
112 base::ThreadTaskRunnerHandle::Get(), weak_ptr_factory_.GetWeakPtr());
113
114 host_->SetOutputSurface(std::move(delegated_output_surface));
115 } 113 }
116 114
117 void BlimpCompositor::DidCommitAndDrawFrame() { 115 void BlimpCompositor::DidCommitAndDrawFrame() {
118 BlimpStats::GetInstance()->Add(BlimpStats::COMMIT, 1); 116 BlimpStats::GetInstance()->Add(BlimpStats::COMMIT, 1);
119 } 117 }
120 118
121 void BlimpCompositor::SetProtoReceiver(ProtoReceiver* receiver) { 119 void BlimpCompositor::SetProtoReceiver(ProtoReceiver* receiver) {
122 remote_proto_channel_receiver_ = receiver; 120 remote_proto_channel_receiver_ = receiver;
123 } 121 }
124 122
(...skipping 27 matching lines...) Expand all
152 DestroyLayerTreeHost(); 150 DestroyLayerTreeHost();
153 break; 151 break;
154 default: 152 default:
155 // We should have a receiver if we're getting compositor messages that 153 // We should have a receiver if we're getting compositor messages that
156 // are not INITIALIZE_IMPL or CLOSE_IMPL. 154 // are not INITIALIZE_IMPL or CLOSE_IMPL.
157 DCHECK(remote_proto_channel_receiver_); 155 DCHECK(remote_proto_channel_receiver_);
158 remote_proto_channel_receiver_->OnProtoReceived(std::move(message)); 156 remote_proto_channel_receiver_->OnProtoReceived(std::move(message));
159 } 157 }
160 } 158 }
161 159
160 void BlimpCompositor::OnContextProviderCreated(
161 scoped_refptr<cc::ContextProvider> provider) {
162 DCHECK(!surface_factory_)
163 << "Any connection to the old output surface should have been destroyed";
164
165 // Make sure we still have a host and we're still expecting an output surface.
166 // This can happen if the host dies while the request is outstanding and we
167 // build a new one that hasn't asked for a surface yet.
168 if (!host_ || !output_surface_request_pending_)
Khushal 2016/08/25 05:02:53 Actually, we should need just the output_surface_r
David Trainor- moved to gerrit 2016/08/26 17:15:54 I get that we only need to check one, but having i
Khushal 2016/08/26 19:34:25 That's cool. Just didn't want to early out so some
169 return;
170
171 // TODO(khushalsagar): Make a worker context and bind it to the current
172 // thread:
173 // Worker context is bound to the main thread in RenderThreadImpl. One day
174 // that will change and then this will have to be removed.
175 // worker_context_provider->BindToCurrentThread();
176 std::unique_ptr<DelegatedOutputSurface> delegated_output_surface =
177 base::MakeUnique<DelegatedOutputSurface>(
178 provider, nullptr, base::ThreadTaskRunnerHandle::Get(),
179 weak_ptr_factory_.GetWeakPtr());
180
181 host_->SetOutputSurface(std::move(delegated_output_surface));
182 }
183
162 void BlimpCompositor::SendWebGestureEvent( 184 void BlimpCompositor::SendWebGestureEvent(
163 const blink::WebGestureEvent& gesture_event) { 185 const blink::WebGestureEvent& gesture_event) {
164 client_->SendWebGestureEvent(render_widget_id_, gesture_event); 186 client_->SendWebGestureEvent(render_widget_id_, gesture_event);
165 } 187 }
166 188
167 void BlimpCompositor::BindToClient( 189 void BlimpCompositor::BindToClient(
168 base::WeakPtr<OutputSurfaceProxyClient> client) { 190 base::WeakPtr<OutputSurfaceProxyClient> client) {
169 DCHECK(thread_checker_.CalledOnValidThread()); 191 DCHECK(thread_checker_.CalledOnValidThread());
170 DCHECK(!surface_factory_); 192 DCHECK(!surface_factory_);
171 193
172 output_surface_proxy_client_ = client; 194 output_surface_proxy_client_ = client;
173 surface_factory_ = 195 surface_factory_ = base::MakeUnique<cc::SurfaceFactory>(
174 base::MakeUnique<cc::SurfaceFactory>(surface_manager_, this); 196 GetEmbedderDeps()->GetSurfaceManager(), this);
175 } 197 }
176 198
177 void BlimpCompositor::SwapCompositorFrame(cc::CompositorFrame frame) { 199 void BlimpCompositor::SwapCompositorFrame(cc::CompositorFrame frame) {
178 DCHECK(thread_checker_.CalledOnValidThread()); 200 DCHECK(thread_checker_.CalledOnValidThread());
179 DCHECK(surface_factory_); 201 DCHECK(surface_factory_);
180 202
181 cc::RenderPass* root_pass = 203 cc::RenderPass* root_pass =
182 frame.delegated_frame_data->render_pass_list.back().get(); 204 frame.delegated_frame_data->render_pass_list.back().get();
183 gfx::Size surface_size = root_pass->output_rect.size(); 205 gfx::Size surface_size = root_pass->output_rect.size();
184 206
185 if (surface_id_.is_null() || current_surface_size_ != surface_size) { 207 if (surface_id_.is_null() || current_surface_size_ != surface_size) {
186 DestroyDelegatedContent(); 208 DestroyDelegatedContent();
187 DCHECK(layer_->children().empty()); 209 DCHECK(layer_->children().empty());
188 210
189 surface_id_ = surface_id_allocator_->GenerateId(); 211 surface_id_ = surface_id_allocator_->GenerateId();
190 surface_factory_->Create(surface_id_); 212 surface_factory_->Create(surface_id_);
191 current_surface_size_ = surface_size; 213 current_surface_size_ = surface_size;
192 214
193 // manager must outlive compositors using it. 215 // manager must outlive compositors using it.
216 cc::SurfaceManager* surface_manager =
217 GetEmbedderDeps()->GetSurfaceManager();
194 scoped_refptr<cc::SurfaceLayer> content_layer = cc::SurfaceLayer::Create( 218 scoped_refptr<cc::SurfaceLayer> content_layer = cc::SurfaceLayer::Create(
195 base::Bind(&SatisfyCallback, base::Unretained(surface_manager_)), 219 base::Bind(&SatisfyCallback, base::Unretained(surface_manager)),
196 base::Bind(&RequireCallback, base::Unretained(surface_manager_))); 220 base::Bind(&RequireCallback, base::Unretained(surface_manager)));
197 content_layer->SetSurfaceId(surface_id_, 1.f, surface_size); 221 content_layer->SetSurfaceId(surface_id_, 1.f, surface_size);
198 content_layer->SetBounds(current_surface_size_); 222 content_layer->SetBounds(current_surface_size_);
199 content_layer->SetIsDrawable(true); 223 content_layer->SetIsDrawable(true);
200 content_layer->SetContentsOpaque(true); 224 content_layer->SetContentsOpaque(true);
201 225
202 layer_->AddChild(content_layer); 226 layer_->AddChild(content_layer);
203 } 227 }
204 228
205 surface_factory_->SubmitCompositorFrame(surface_id_, std::move(frame), 229 surface_factory_->SubmitCompositorFrame(surface_id_, std::move(frame),
206 base::Closure()); 230 base::Closure());
207 } 231 }
208 232
209 void BlimpCompositor::DetachFromClient() { 233 void BlimpCompositor::DetachFromClient() {
210 DCHECK(thread_checker_.CalledOnValidThread()); 234 DCHECK(thread_checker_.CalledOnValidThread());
211 DCHECK(surface_factory_); 235 DCHECK(surface_factory_);
212 236
213 DestroyDelegatedContent(); 237 DestroyDelegatedContent();
214 surface_factory_.reset(); 238 surface_factory_.reset();
215 output_surface_proxy_client_ = nullptr; 239 output_surface_proxy_client_ = nullptr;
216 } 240 }
217 241
218 void BlimpCompositor::ReturnResources( 242 void BlimpCompositor::ReturnResources(
219 const cc::ReturnedResourceArray& resources) { 243 const cc::ReturnedResourceArray& resources) {
220 DCHECK(surface_factory_); 244 DCHECK(surface_factory_);
221 compositor_task_runner_->PostTask( 245 compositor_dependencies_->GetCompositorTaskRunner()->PostTask(
222 FROM_HERE, 246 FROM_HERE,
223 base::Bind(&OutputSurfaceProxyClient::ReclaimCompositorResources, 247 base::Bind(&OutputSurfaceProxyClient::ReclaimCompositorResources,
224 output_surface_proxy_client_, resources)); 248 output_surface_proxy_client_, resources));
225 } 249 }
226 250
251 CompositorDependencies* BlimpCompositor::GetEmbedderDeps() {
252 return compositor_dependencies_->GetEmbedderDependencies();
253 }
254
227 void BlimpCompositor::DestroyDelegatedContent() { 255 void BlimpCompositor::DestroyDelegatedContent() {
228 if (surface_id_.is_null()) 256 if (surface_id_.is_null())
229 return; 257 return;
230 258
231 // Remove any references for the surface layer that uses this |surface_id_|. 259 // Remove any references for the surface layer that uses this |surface_id_|.
232 layer_->RemoveAllChildren(); 260 layer_->RemoveAllChildren();
233 surface_factory_->Destroy(surface_id_); 261 surface_factory_->Destroy(surface_id_);
234 surface_id_ = cc::SurfaceId(); 262 surface_id_ = cc::SurfaceId();
235 } 263 }
236 264
237 void BlimpCompositor::CreateLayerTreeHost( 265 void BlimpCompositor::CreateLayerTreeHost(
238 const cc::proto::InitializeImpl& initialize_message) { 266 const cc::proto::InitializeImpl& initialize_message) {
239 DCHECK(!host_); 267 DCHECK(!host_);
240 VLOG(1) << "Creating LayerTreeHost for render widget: " << render_widget_id_; 268 VLOG(1) << "Creating LayerTreeHost for render widget: " << render_widget_id_;
241 269
270 // LayerTreeHost copies the settings inside Create(). So it's ok for this
271 // to die once LayerTreeHost is created.
272 std::unique_ptr<cc::LayerTreeSettings> settings =
273 GetEmbedderDeps()->GetLayerTreeSettings();
Khushal 2016/08/25 05:02:53 nit: Feel like it would be better to cache the uni
David Trainor- moved to gerrit 2016/08/26 17:15:54 Done.
274
242 // Create the LayerTreeHost 275 // Create the LayerTreeHost
243 cc::LayerTreeHost::InitParams params; 276 cc::LayerTreeHost::InitParams params;
244 params.client = this; 277 params.client = this;
245 params.task_graph_runner = client_->GetTaskGraphRunner(); 278 params.task_graph_runner = compositor_dependencies_->GetTaskGraphRunner();
246 params.gpu_memory_buffer_manager = client_->GetGpuMemoryBufferManager(); 279 params.gpu_memory_buffer_manager =
280 GetEmbedderDeps()->GetGpuMemoryBufferManager();
247 params.main_task_runner = base::ThreadTaskRunnerHandle::Get(); 281 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
248 params.image_serialization_processor = 282 params.image_serialization_processor =
249 client_->GetImageSerializationProcessor(); 283 compositor_dependencies_->GetImageSerializationProcessor();
250 params.settings = client_->GetLayerTreeSettings(); 284 params.settings = settings.get();
251 params.animation_host = cc::AnimationHost::CreateMainInstance(); 285 params.animation_host = cc::AnimationHost::CreateMainInstance();
252 286
253 compositor_task_runner_ = client_->GetCompositorTaskRunner(); 287 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner =
288 compositor_dependencies_->GetCompositorTaskRunner();
254 289
255 host_ = cc::LayerTreeHost::CreateRemoteClient( 290 host_ = cc::LayerTreeHost::CreateRemoteClient(
256 this /* remote_proto_channel */, compositor_task_runner_, &params); 291 this /* remote_proto_channel */, compositor_task_runner, &params);
257 host_->SetVisible(host_should_be_visible_); 292 host_->SetVisible(host_should_be_visible_);
258 293
259 DCHECK(!input_manager_); 294 DCHECK(!input_manager_);
260 input_manager_ = BlimpInputManager::Create( 295 input_manager_ = BlimpInputManager::Create(
261 this, base::ThreadTaskRunnerHandle::Get(), compositor_task_runner_, 296 this, base::ThreadTaskRunnerHandle::Get(), compositor_task_runner,
262 host_->GetInputHandler()); 297 host_->GetInputHandler());
263 } 298 }
264 299
265 void BlimpCompositor::DestroyLayerTreeHost() { 300 void BlimpCompositor::DestroyLayerTreeHost() {
266 DCHECK(host_); 301 DCHECK(host_);
267 VLOG(1) << "Destroying LayerTreeHost for render widget: " 302 VLOG(1) << "Destroying LayerTreeHost for render widget: "
268 << render_widget_id_; 303 << render_widget_id_;
269 // Tear down the output surface connection with the old LayerTreeHost 304 // Tear down the output surface connection with the old LayerTreeHost
270 // instance. 305 // instance.
271 DestroyDelegatedContent(); 306 DestroyDelegatedContent();
272 surface_factory_.reset(); 307 surface_factory_.reset();
273 308
274 // Destroy the old LayerTreeHost state. 309 // Destroy the old LayerTreeHost state.
275 host_.reset(); 310 host_.reset();
276 311
277 // Destroy the old input manager state. 312 // Destroy the old input manager state.
278 // It is important to destroy the LayerTreeHost before destroying the input 313 // It is important to destroy the LayerTreeHost before destroying the input
279 // manager as it has a reference to the cc::InputHandlerClient owned by the 314 // manager as it has a reference to the cc::InputHandlerClient owned by the
280 // BlimpInputManager. 315 // BlimpInputManager.
281 input_manager_.reset(); 316 input_manager_.reset();
282 317
318 output_surface_request_pending_ = false;
319
283 // Make sure we don't have a receiver at this point. 320 // Make sure we don't have a receiver at this point.
284 DCHECK(!remote_proto_channel_receiver_); 321 DCHECK(!remote_proto_channel_receiver_);
285 } 322 }
286 323
287 } // namespace client 324 } // namespace client
288 } // namespace blimp 325 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698