1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/child_frame_compositing_helper.h"
#include "cc/blink/web_layer_impl.h"
#include "cc/layers/delegated_frame_provider.h"
#include "cc/layers/delegated_frame_resource_collection.h"
#include "cc/layers/delegated_renderer_layer.h"
#include "cc/layers/solid_color_layer.h"
#include "cc/layers/surface_layer.h"
#include "cc/output/context_provider.h"
#include "cc/output/copy_output_request.h"
#include "cc/output/copy_output_result.h"
#include "cc/resources/single_release_callback.h"
#include "content/child/thread_safe_sender.h"
#include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/frame_messages.h"
#include "content/common/gpu/client/context_provider_command_buffer.h"
#include "content/renderer/browser_plugin/browser_plugin.h"
#include "content/renderer/browser_plugin/browser_plugin_manager.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_frame_proxy.h"
#include "content/renderer/render_thread_impl.h"
#include "skia/ext/image_operations.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebPluginContainer.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/skia_util.h"
namespace content {
ChildFrameCompositingHelper*
ChildFrameCompositingHelper::CreateForBrowserPlugin(
const base::WeakPtr<BrowserPlugin>& browser_plugin) {
return new ChildFrameCompositingHelper(
browser_plugin, nullptr, nullptr,
browser_plugin->render_frame_routing_id());
}
ChildFrameCompositingHelper*
ChildFrameCompositingHelper::CreateForRenderFrameProxy(
RenderFrameProxy* render_frame_proxy) {
return new ChildFrameCompositingHelper(base::WeakPtr<BrowserPlugin>(),
render_frame_proxy->web_frame(),
render_frame_proxy,
render_frame_proxy->routing_id());
}
ChildFrameCompositingHelper::ChildFrameCompositingHelper(
const base::WeakPtr<BrowserPlugin>& browser_plugin,
blink::WebFrame* frame,
RenderFrameProxy* render_frame_proxy,
int host_routing_id)
: host_routing_id_(host_routing_id),
last_route_id_(0),
last_output_surface_id_(0),
last_host_id_(0),
ack_pending_(true),
opaque_(true),
browser_plugin_(browser_plugin),
render_frame_proxy_(render_frame_proxy),
frame_(frame) {
}
ChildFrameCompositingHelper::~ChildFrameCompositingHelper() {
if (resource_collection_.get())
resource_collection_->SetClient(nullptr);
}
BrowserPluginManager* ChildFrameCompositingHelper::GetBrowserPluginManager() {
if (!browser_plugin_)
return nullptr;
return BrowserPluginManager::Get();
}
blink::WebPluginContainer* ChildFrameCompositingHelper::GetContainer() {
if (!browser_plugin_)
return nullptr;
return browser_plugin_->container();
}
int ChildFrameCompositingHelper::GetInstanceID() {
if (!browser_plugin_)
return 0;
return browser_plugin_->browser_plugin_instance_id();
}
void ChildFrameCompositingHelper::SendCompositorFrameSwappedACKToBrowser(
FrameHostMsg_CompositorFrameSwappedACK_Params& params) {
// This function will be removed when BrowserPluginManager is removed and
// BrowserPlugin is modified to use a RenderFrame.
if (GetBrowserPluginManager()) {
GetBrowserPluginManager()->Send(
new BrowserPluginHostMsg_CompositorFrameSwappedACK(
GetInstanceID(), params));
} else if (render_frame_proxy_) {
render_frame_proxy_->Send(
new FrameHostMsg_CompositorFrameSwappedACK(host_routing_id_, params));
}
}
void ChildFrameCompositingHelper::SendReclaimCompositorResourcesToBrowser(
FrameHostMsg_ReclaimCompositorResources_Params& params) {
// This function will be removed when BrowserPluginManager is removed and
// BrowserPlugin is modified to use a RenderFrame.
if (GetBrowserPluginManager()) {
GetBrowserPluginManager()->Send(
new BrowserPluginHostMsg_ReclaimCompositorResources(
GetInstanceID(), params));
} else if (render_frame_proxy_) {
render_frame_proxy_->Send(
new FrameHostMsg_ReclaimCompositorResources(host_routing_id_, params));
}
}
void ChildFrameCompositingHelper::DidCommitCompositorFrame() {
if (!resource_collection_.get() || !ack_pending_)
return;
FrameHostMsg_CompositorFrameSwappedACK_Params params;
params.producing_host_id = last_host_id_;
params.producing_route_id = last_route_id_;
params.output_surface_id = last_output_surface_id_;
resource_collection_->TakeUnusedResourcesForChildCompositor(
¶ms.ack.resources);
SendCompositorFrameSwappedACKToBrowser(params);
ack_pending_ = false;
}
void ChildFrameCompositingHelper::EnableCompositing(bool enable) {
if (enable && !background_layer_.get()) {
background_layer_ =
cc::SolidColorLayer::Create(cc_blink::WebLayerImpl::LayerSettings());
background_layer_->SetMasksToBounds(true);
background_layer_->SetBackgroundColor(
SkColorSetARGBInline(255, 255, 255, 255));
web_layer_.reset(new cc_blink::WebLayerImpl(background_layer_));
}
if (GetContainer()) {
GetContainer()->setWebLayer(enable ? web_layer_.get() : nullptr);
} else if (frame_) {
frame_->setRemoteWebLayer(enable ? web_layer_.get() : nullptr);
}
}
void ChildFrameCompositingHelper::CheckSizeAndAdjustLayerProperties(
const gfx::Size& new_size,
float device_scale_factor,
cc::Layer* layer) {
if (buffer_size_ != new_size) {
buffer_size_ = new_size;
// The container size is in DIP, so is the layer size.
// Buffer size is in physical pixels, so we need to adjust
// it by the device scale factor.
gfx::Size device_scale_adjusted_size = gfx::ToFlooredSize(
gfx::ScaleSize(buffer_size_, 1.0f / device_scale_factor));
layer->SetBounds(device_scale_adjusted_size);
}
// Manually manage background layer for transparent webview.
if (!opaque_)
background_layer_->SetIsDrawable(false);
}
void ChildFrameCompositingHelper::OnContainerDestroy() {
// If we have a pending ACK, then ACK now so we don't lose frames in the
// future.
DidCommitCompositorFrame();
if (GetContainer())
GetContainer()->setWebLayer(nullptr);
if (resource_collection_.get())
resource_collection_->SetClient(nullptr);
ack_pending_ = false;
resource_collection_ = nullptr;
frame_provider_ = nullptr;
delegated_layer_ = nullptr;
background_layer_ = nullptr;
surface_layer_ = nullptr;
web_layer_.reset();
}
void ChildFrameCompositingHelper::ChildFrameGone() {
background_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 0, 128, 0));
background_layer_->RemoveAllChildren();
background_layer_->SetIsDrawable(true);
background_layer_->SetContentsOpaque(true);
}
void ChildFrameCompositingHelper::OnCompositorFrameSwapped(
scoped_ptr<cc::CompositorFrame> frame,
int route_id,
uint32 output_surface_id,
int host_id,
base::SharedMemoryHandle handle) {
cc::DelegatedFrameData* frame_data = frame->delegated_frame_data.get();
// Surface IDs and compositor frames should never be received
// interchangeably.
DCHECK(!surface_layer_.get());
// Do nothing if we are getting destroyed or have no frame data.
if (!frame_data || !background_layer_.get())
return;
DCHECK(!frame_data->render_pass_list.empty());
cc::RenderPass* root_pass = frame_data->render_pass_list.back();
gfx::Size frame_size = root_pass->output_rect.size();
if (last_route_id_ != route_id ||
last_output_surface_id_ != output_surface_id ||
last_host_id_ != host_id) {
// Resource ids are scoped by the output surface.
// If the originating output surface doesn't match the last one, it
// indicates the guest's output surface may have been recreated, in which
// case we should recreate the DelegatedRendererLayer, to avoid matching
// resources from the old one with resources from the new one which would
// have the same id.
frame_provider_ = nullptr;
// Drop the cc::DelegatedFrameResourceCollection so that we will not return
// any resources from the old output surface with the new output surface id.
if (resource_collection_.get()) {
resource_collection_->SetClient(nullptr);
if (resource_collection_->LoseAllResources())
SendReturnedDelegatedResources();
resource_collection_ = nullptr;
}
last_output_surface_id_ = output_surface_id;
last_route_id_ = route_id;
last_host_id_ = host_id;
}
if (!resource_collection_.get()) {
resource_collection_ = new cc::DelegatedFrameResourceCollection;
resource_collection_->SetClient(this);
}
if (!frame_provider_.get() || frame_provider_->frame_size() != frame_size) {
frame_provider_ = new cc::DelegatedFrameProvider(
resource_collection_.get(), frame->delegated_frame_data.Pass());
if (delegated_layer_.get())
delegated_layer_->RemoveFromParent();
delegated_layer_ = cc::DelegatedRendererLayer::Create(
cc_blink::WebLayerImpl::LayerSettings(), frame_provider_.get());
delegated_layer_->SetIsDrawable(true);
buffer_size_ = gfx::Size();
SetContentsOpaque(opaque_);
background_layer_->AddChild(delegated_layer_);
} else {
frame_provider_->SetFrameData(frame->delegated_frame_data.Pass());
}
CheckSizeAndAdjustLayerProperties(
frame_data->render_pass_list.back()->output_rect.size(),
frame->metadata.device_scale_factor,
delegated_layer_.get());
ack_pending_ = true;
}
// static
void ChildFrameCompositingHelper::SatisfyCallback(
scoped_refptr<ThreadSafeSender> sender,
int host_routing_id,
cc::SurfaceSequence sequence) {
// This may be called on either the main or impl thread.
sender->Send(new FrameHostMsg_SatisfySequence(host_routing_id, sequence));
}
// static
void ChildFrameCompositingHelper::SatisfyCallbackBrowserPlugin(
scoped_refptr<ThreadSafeSender> sender,
int host_routing_id,
int browser_plugin_instance_id,
cc::SurfaceSequence sequence) {
sender->Send(new BrowserPluginHostMsg_SatisfySequence(
host_routing_id, browser_plugin_instance_id, sequence));
}
// static
void ChildFrameCompositingHelper::RequireCallback(
scoped_refptr<ThreadSafeSender> sender,
int host_routing_id,
cc::SurfaceId id,
cc::SurfaceSequence sequence) {
// This may be called on either the main or impl thread.
sender->Send(new FrameHostMsg_RequireSequence(host_routing_id, id, sequence));
}
void ChildFrameCompositingHelper::RequireCallbackBrowserPlugin(
scoped_refptr<ThreadSafeSender> sender,
int host_routing_id,
int browser_plugin_instance_id,
cc::SurfaceId id,
cc::SurfaceSequence sequence) {
// This may be called on either the main or impl thread.
sender->Send(new BrowserPluginHostMsg_RequireSequence(
host_routing_id, browser_plugin_instance_id, id, sequence));
}
void ChildFrameCompositingHelper::OnSetSurface(
const cc::SurfaceId& surface_id,
const gfx::Size& frame_size,
float scale_factor,
const cc::SurfaceSequence& sequence) {
// Surface IDs and compositor frames should never be received
// interchangably.
DCHECK(!delegated_layer_.get());
// Do nothing if we are getting destroyed.
if (!background_layer_.get())
return;
if (!surface_layer_.get()) {
scoped_refptr<ThreadSafeSender> sender(
RenderThreadImpl::current()->thread_safe_sender());
cc::SurfaceLayer::SatisfyCallback satisfy_callback =
render_frame_proxy_
? base::Bind(&ChildFrameCompositingHelper::SatisfyCallback, sender,
host_routing_id_)
: base::Bind(
&ChildFrameCompositingHelper::SatisfyCallbackBrowserPlugin,
sender, host_routing_id_,
browser_plugin_->browser_plugin_instance_id());
cc::SurfaceLayer::RequireCallback require_callback =
render_frame_proxy_
? base::Bind(&ChildFrameCompositingHelper::RequireCallback, sender,
host_routing_id_)
: base::Bind(
&ChildFrameCompositingHelper::RequireCallbackBrowserPlugin,
sender, host_routing_id_,
browser_plugin_->browser_plugin_instance_id());
surface_layer_ =
cc::SurfaceLayer::Create(cc_blink::WebLayerImpl::LayerSettings(),
satisfy_callback, require_callback);
}
surface_layer_->SetSurfaceId(surface_id, scale_factor, frame_size);
UpdateVisibility(true);
SetContentsOpaque(opaque_);
background_layer_->AddChild(surface_layer_);
// The RWHV creates a destruction dependency on the surface that needs to be
// satisfied. Note: render_frame_proxy_ is null in the case our client is a
// BrowserPlugin; in this case the BrowserPlugin sends its own SatisfySequence
// message.
if (render_frame_proxy_) {
render_frame_proxy_->Send(
new FrameHostMsg_SatisfySequence(host_routing_id_, sequence));
} else if (browser_plugin_.get()) {
browser_plugin_->SendSatisfySequence(sequence);
}
CheckSizeAndAdjustLayerProperties(frame_size, scale_factor,
surface_layer_.get());
}
void ChildFrameCompositingHelper::UpdateVisibility(bool visible) {
if (delegated_layer_.get())
delegated_layer_->SetIsDrawable(visible);
if (surface_layer_.get())
surface_layer_->SetIsDrawable(visible);
}
void ChildFrameCompositingHelper::UnusedResourcesAreAvailable() {
if (ack_pending_)
return;
SendReturnedDelegatedResources();
}
void ChildFrameCompositingHelper::SendReturnedDelegatedResources() {
FrameHostMsg_ReclaimCompositorResources_Params params;
if (resource_collection_.get())
resource_collection_->TakeUnusedResourcesForChildCompositor(
¶ms.ack.resources);
DCHECK(!params.ack.resources.empty());
params.route_id = last_route_id_;
params.output_surface_id = last_output_surface_id_;
params.renderer_host_id = last_host_id_;
SendReclaimCompositorResourcesToBrowser(params);
}
void ChildFrameCompositingHelper::SetContentsOpaque(bool opaque) {
opaque_ = opaque;
if (delegated_layer_.get())
delegated_layer_->SetContentsOpaque(opaque_);
if (surface_layer_.get())
surface_layer_->SetContentsOpaque(opaque_);
}
} // namespace content
|