1 // Copyright 2014 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.
5 #include "content/renderer/child_frame_compositing_helper.h"
7 #include "cc/blink/web_layer_impl.h"
8 #include "cc/layers/delegated_frame_provider.h"
9 #include "cc/layers/delegated_frame_resource_collection.h"
10 #include "cc/layers/delegated_renderer_layer.h"
11 #include "cc/layers/solid_color_layer.h"
12 #include "cc/layers/surface_layer.h"
13 #include "cc/output/context_provider.h"
14 #include "cc/output/copy_output_request.h"
15 #include "cc/output/copy_output_result.h"
16 #include "cc/resources/single_release_callback.h"
17 #include "content/common/browser_plugin/browser_plugin_messages.h"
18 #include "content/common/frame_messages.h"
19 #include "content/common/gpu/client/context_provider_command_buffer.h"
20 #include "content/renderer/browser_plugin/browser_plugin.h"
21 #include "content/renderer/browser_plugin/browser_plugin_manager.h"
22 #include "content/renderer/render_frame_impl.h"
23 #include "content/renderer/render_frame_proxy.h"
24 #include "content/renderer/render_thread_impl.h"
25 #include "skia/ext/image_operations.h"
26 #include "third_party/WebKit/public/web/WebFrame.h"
27 #include "third_party/WebKit/public/web/WebPluginContainer.h"
28 #include "third_party/khronos/GLES2/gl2.h"
29 #include "ui/gfx/geometry/size_conversions.h"
30 #include "ui/gfx/skia_util.h"
34 ChildFrameCompositingHelper
*
35 ChildFrameCompositingHelper::CreateForBrowserPlugin(
36 const base::WeakPtr
<BrowserPlugin
>& browser_plugin
) {
37 return new ChildFrameCompositingHelper(
38 browser_plugin
, nullptr, nullptr,
39 browser_plugin
->render_frame_routing_id());
42 ChildFrameCompositingHelper
*
43 ChildFrameCompositingHelper::CreateForRenderFrameProxy(
44 RenderFrameProxy
* render_frame_proxy
) {
45 return new ChildFrameCompositingHelper(base::WeakPtr
<BrowserPlugin
>(),
46 render_frame_proxy
->web_frame(),
48 render_frame_proxy
->routing_id());
51 ChildFrameCompositingHelper::ChildFrameCompositingHelper(
52 const base::WeakPtr
<BrowserPlugin
>& browser_plugin
,
53 blink::WebFrame
* frame
,
54 RenderFrameProxy
* render_frame_proxy
,
56 : host_routing_id_(host_routing_id
),
58 last_output_surface_id_(0),
62 browser_plugin_(browser_plugin
),
63 render_frame_proxy_(render_frame_proxy
),
68 ChildFrameCompositingHelper::~ChildFrameCompositingHelper() {
69 if (resource_collection_
.get())
70 resource_collection_
->SetClient(nullptr);
73 BrowserPluginManager
* ChildFrameCompositingHelper::GetBrowserPluginManager() {
77 return BrowserPluginManager::Get();
80 blink::WebPluginContainer
* ChildFrameCompositingHelper::GetContainer() {
84 return browser_plugin_
->container();
87 int ChildFrameCompositingHelper::GetInstanceID() {
91 return browser_plugin_
->browser_plugin_instance_id();
94 void ChildFrameCompositingHelper::SendCompositorFrameSwappedACKToBrowser(
95 FrameHostMsg_CompositorFrameSwappedACK_Params
& params
) {
96 // This function will be removed when BrowserPluginManager is removed and
97 // BrowserPlugin is modified to use a RenderFrame.
98 if (GetBrowserPluginManager()) {
99 GetBrowserPluginManager()->Send(
100 new BrowserPluginHostMsg_CompositorFrameSwappedACK(
101 GetInstanceID(), params
));
102 } else if (render_frame_proxy_
) {
103 render_frame_proxy_
->Send(
104 new FrameHostMsg_CompositorFrameSwappedACK(host_routing_id_
, params
));
108 void ChildFrameCompositingHelper::SendReclaimCompositorResourcesToBrowser(
109 FrameHostMsg_ReclaimCompositorResources_Params
& params
) {
110 // This function will be removed when BrowserPluginManager is removed and
111 // BrowserPlugin is modified to use a RenderFrame.
112 if (GetBrowserPluginManager()) {
113 GetBrowserPluginManager()->Send(
114 new BrowserPluginHostMsg_ReclaimCompositorResources(
115 GetInstanceID(), params
));
116 } else if (render_frame_proxy_
) {
117 render_frame_proxy_
->Send(
118 new FrameHostMsg_ReclaimCompositorResources(host_routing_id_
, params
));
122 void ChildFrameCompositingHelper::DidCommitCompositorFrame() {
123 if (!resource_collection_
.get() || !ack_pending_
)
126 FrameHostMsg_CompositorFrameSwappedACK_Params params
;
127 params
.producing_host_id
= last_host_id_
;
128 params
.producing_route_id
= last_route_id_
;
129 params
.output_surface_id
= last_output_surface_id_
;
130 resource_collection_
->TakeUnusedResourcesForChildCompositor(
131 ¶ms
.ack
.resources
);
133 SendCompositorFrameSwappedACKToBrowser(params
);
135 ack_pending_
= false;
138 void ChildFrameCompositingHelper::EnableCompositing(bool enable
) {
139 if (enable
&& !background_layer_
.get()) {
141 cc::SolidColorLayer::Create(cc_blink::WebLayerImpl::LayerSettings());
142 background_layer_
->SetMasksToBounds(true);
143 background_layer_
->SetBackgroundColor(
144 SkColorSetARGBInline(255, 255, 255, 255));
145 web_layer_
.reset(new cc_blink::WebLayerImpl(background_layer_
));
148 if (GetContainer()) {
149 GetContainer()->setWebLayer(enable
? web_layer_
.get() : nullptr);
151 frame_
->setRemoteWebLayer(enable
? web_layer_
.get() : nullptr);
155 void ChildFrameCompositingHelper::CheckSizeAndAdjustLayerProperties(
156 const gfx::Size
& new_size
,
157 float device_scale_factor
,
159 if (buffer_size_
!= new_size
) {
160 buffer_size_
= new_size
;
161 // The container size is in DIP, so is the layer size.
162 // Buffer size is in physical pixels, so we need to adjust
163 // it by the device scale factor.
164 gfx::Size device_scale_adjusted_size
= gfx::ToFlooredSize(
165 gfx::ScaleSize(buffer_size_
, 1.0f
/ device_scale_factor
));
166 layer
->SetBounds(device_scale_adjusted_size
);
169 // Manually manage background layer for transparent webview.
171 background_layer_
->SetIsDrawable(false);
174 void ChildFrameCompositingHelper::OnContainerDestroy() {
175 // If we have a pending ACK, then ACK now so we don't lose frames in the
177 DidCommitCompositorFrame();
180 GetContainer()->setWebLayer(nullptr);
182 if (resource_collection_
.get())
183 resource_collection_
->SetClient(nullptr);
185 ack_pending_
= false;
186 resource_collection_
= nullptr;
187 frame_provider_
= nullptr;
188 delegated_layer_
= nullptr;
189 background_layer_
= nullptr;
190 surface_layer_
= nullptr;
194 void ChildFrameCompositingHelper::ChildFrameGone() {
195 background_layer_
->SetBackgroundColor(SkColorSetARGBInline(255, 0, 128, 0));
196 background_layer_
->RemoveAllChildren();
197 background_layer_
->SetIsDrawable(true);
198 background_layer_
->SetContentsOpaque(true);
201 void ChildFrameCompositingHelper::OnCompositorFrameSwapped(
202 scoped_ptr
<cc::CompositorFrame
> frame
,
204 uint32 output_surface_id
,
206 base::SharedMemoryHandle handle
) {
207 cc::DelegatedFrameData
* frame_data
= frame
->delegated_frame_data
.get();
209 // Surface IDs and compositor frames should never be received
211 DCHECK(!surface_layer_
.get());
213 // Do nothing if we are getting destroyed or have no frame data.
214 if (!frame_data
|| !background_layer_
.get())
217 DCHECK(!frame_data
->render_pass_list
.empty());
218 cc::RenderPass
* root_pass
= frame_data
->render_pass_list
.back();
219 gfx::Size frame_size
= root_pass
->output_rect
.size();
221 if (last_route_id_
!= route_id
||
222 last_output_surface_id_
!= output_surface_id
||
223 last_host_id_
!= host_id
) {
224 // Resource ids are scoped by the output surface.
225 // If the originating output surface doesn't match the last one, it
226 // indicates the guest's output surface may have been recreated, in which
227 // case we should recreate the DelegatedRendererLayer, to avoid matching
228 // resources from the old one with resources from the new one which would
230 frame_provider_
= nullptr;
232 // Drop the cc::DelegatedFrameResourceCollection so that we will not return
233 // any resources from the old output surface with the new output surface id.
234 if (resource_collection_
.get()) {
235 resource_collection_
->SetClient(nullptr);
237 if (resource_collection_
->LoseAllResources())
238 SendReturnedDelegatedResources();
239 resource_collection_
= nullptr;
241 last_output_surface_id_
= output_surface_id
;
242 last_route_id_
= route_id
;
243 last_host_id_
= host_id
;
245 if (!resource_collection_
.get()) {
246 resource_collection_
= new cc::DelegatedFrameResourceCollection
;
247 resource_collection_
->SetClient(this);
249 if (!frame_provider_
.get() || frame_provider_
->frame_size() != frame_size
) {
250 frame_provider_
= new cc::DelegatedFrameProvider(
251 resource_collection_
.get(), frame
->delegated_frame_data
.Pass());
252 if (delegated_layer_
.get())
253 delegated_layer_
->RemoveFromParent();
254 delegated_layer_
= cc::DelegatedRendererLayer::Create(
255 cc_blink::WebLayerImpl::LayerSettings(), frame_provider_
.get());
256 delegated_layer_
->SetIsDrawable(true);
257 buffer_size_
= gfx::Size();
258 SetContentsOpaque(opaque_
);
259 background_layer_
->AddChild(delegated_layer_
);
261 frame_provider_
->SetFrameData(frame
->delegated_frame_data
.Pass());
264 CheckSizeAndAdjustLayerProperties(
265 frame_data
->render_pass_list
.back()->output_rect
.size(),
266 frame
->metadata
.device_scale_factor
,
267 delegated_layer_
.get());
273 void ChildFrameCompositingHelper::SatisfyCallback(
274 base::WeakPtr
<ChildFrameCompositingHelper
> helper
,
275 cc::SurfaceSequence sequence
) {
276 if (helper
&& helper
->render_frame_proxy_
) {
277 helper
->render_frame_proxy_
->Send(
278 new FrameHostMsg_SatisfySequence(helper
->host_routing_id_
, sequence
));
283 void ChildFrameCompositingHelper::RequireCallback(
284 base::WeakPtr
<ChildFrameCompositingHelper
> helper
,
286 cc::SurfaceSequence sequence
) {
287 if (helper
&& helper
->render_frame_proxy_
) {
288 helper
->render_frame_proxy_
->Send(new FrameHostMsg_RequireSequence(
289 helper
->host_routing_id_
, id
, sequence
));
293 void ChildFrameCompositingHelper::OnSetSurface(
294 const cc::SurfaceId
& surface_id
,
295 const gfx::Size
& frame_size
,
297 const cc::SurfaceSequence
& sequence
) {
298 // Surface IDs and compositor frames should never be received
300 DCHECK(!delegated_layer_
.get());
302 // Do nothing if we are getting destroyed.
303 if (!background_layer_
.get())
306 if (!surface_layer_
.get()) {
307 cc::SurfaceLayer::SatisfyCallback satisfy_callback
=
308 base::Bind(&ChildFrameCompositingHelper::SatisfyCallback
, GetWeakPtr());
309 cc::SurfaceLayer::RequireCallback require_callback
=
310 base::Bind(&ChildFrameCompositingHelper::RequireCallback
, GetWeakPtr());
312 cc::SurfaceLayer::Create(cc_blink::WebLayerImpl::LayerSettings(),
313 satisfy_callback
, require_callback
);
315 surface_layer_
->SetSurfaceId(surface_id
, scale_factor
, frame_size
);
316 UpdateVisibility(true);
317 SetContentsOpaque(opaque_
);
318 background_layer_
->AddChild(surface_layer_
);
320 // The RWHV creates a destruction dependency on the surface that needs to be
322 render_frame_proxy_
->Send(
323 new FrameHostMsg_SatisfySequence(host_routing_id_
, sequence
));
325 CheckSizeAndAdjustLayerProperties(frame_size
, scale_factor
,
326 surface_layer_
.get());
329 void ChildFrameCompositingHelper::UpdateVisibility(bool visible
) {
330 if (delegated_layer_
.get())
331 delegated_layer_
->SetIsDrawable(visible
);
332 if (surface_layer_
.get())
333 surface_layer_
->SetIsDrawable(visible
);
336 void ChildFrameCompositingHelper::UnusedResourcesAreAvailable() {
340 SendReturnedDelegatedResources();
343 void ChildFrameCompositingHelper::SendReturnedDelegatedResources() {
344 FrameHostMsg_ReclaimCompositorResources_Params params
;
345 if (resource_collection_
.get())
346 resource_collection_
->TakeUnusedResourcesForChildCompositor(
347 ¶ms
.ack
.resources
);
348 DCHECK(!params
.ack
.resources
.empty());
350 params
.route_id
= last_route_id_
;
351 params
.output_surface_id
= last_output_surface_id_
;
352 params
.renderer_host_id
= last_host_id_
;
353 SendReclaimCompositorResourcesToBrowser(params
);
356 void ChildFrameCompositingHelper::SetContentsOpaque(bool opaque
) {
358 if (delegated_layer_
.get())
359 delegated_layer_
->SetContentsOpaque(opaque_
);
360 if (surface_layer_
.get())
361 surface_layer_
->SetContentsOpaque(opaque_
);
364 } // namespace content