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/output/context_provider.h"
13 #include "cc/output/copy_output_request.h"
14 #include "cc/output/copy_output_result.h"
15 #include "cc/resources/single_release_callback.h"
16 #include "content/common/browser_plugin/browser_plugin_messages.h"
17 #include "content/common/frame_messages.h"
18 #include "content/common/gpu/client/context_provider_command_buffer.h"
19 #include "content/renderer/browser_plugin/browser_plugin.h"
20 #include "content/renderer/browser_plugin/browser_plugin_manager.h"
21 #include "content/renderer/render_frame_impl.h"
22 #include "content/renderer/render_frame_proxy.h"
23 #include "content/renderer/render_thread_impl.h"
24 #include "skia/ext/image_operations.h"
25 #include "third_party/WebKit/public/web/WebFrame.h"
26 #include "third_party/WebKit/public/web/WebPluginContainer.h"
27 #include "third_party/khronos/GLES2/gl2.h"
28 #include "ui/gfx/size_conversions.h"
29 #include "ui/gfx/skia_util.h"
33 ChildFrameCompositingHelper
*
34 ChildFrameCompositingHelper::CreateForBrowserPlugin(
35 const base::WeakPtr
<BrowserPlugin
>& browser_plugin
) {
36 return new ChildFrameCompositingHelper(
37 browser_plugin
, NULL
, NULL
, browser_plugin
->render_view_routing_id());
40 ChildFrameCompositingHelper
*
41 ChildFrameCompositingHelper::CreateForRenderFrameProxy(
42 RenderFrameProxy
* render_frame_proxy
) {
43 return new ChildFrameCompositingHelper(base::WeakPtr
<BrowserPlugin
>(),
44 render_frame_proxy
->web_frame(),
46 render_frame_proxy
->routing_id());
49 ChildFrameCompositingHelper::ChildFrameCompositingHelper(
50 const base::WeakPtr
<BrowserPlugin
>& browser_plugin
,
51 blink::WebFrame
* frame
,
52 RenderFrameProxy
* render_frame_proxy
,
54 : host_routing_id_(host_routing_id
),
56 last_output_surface_id_(0),
60 browser_plugin_(browser_plugin
),
61 render_frame_proxy_(render_frame_proxy
),
64 ChildFrameCompositingHelper::~ChildFrameCompositingHelper() {}
66 BrowserPluginManager
* ChildFrameCompositingHelper::GetBrowserPluginManager() {
70 return browser_plugin_
->browser_plugin_manager();
73 blink::WebPluginContainer
* ChildFrameCompositingHelper::GetContainer() {
77 return browser_plugin_
->container();
80 int ChildFrameCompositingHelper::GetInstanceID() {
84 return browser_plugin_
->browser_plugin_instance_id();
87 void ChildFrameCompositingHelper::SendCompositorFrameSwappedACKToBrowser(
88 FrameHostMsg_CompositorFrameSwappedACK_Params
& params
) {
89 // This function will be removed when BrowserPluginManager is removed and
90 // BrowserPlugin is modified to use a RenderFrame.
91 if (GetBrowserPluginManager()) {
92 GetBrowserPluginManager()->Send(
93 new BrowserPluginHostMsg_CompositorFrameSwappedACK(
94 host_routing_id_
, GetInstanceID(), params
));
95 } else if (render_frame_proxy_
) {
96 render_frame_proxy_
->Send(
97 new FrameHostMsg_CompositorFrameSwappedACK(host_routing_id_
, params
));
101 void ChildFrameCompositingHelper::SendReclaimCompositorResourcesToBrowser(
102 FrameHostMsg_ReclaimCompositorResources_Params
& params
) {
103 // This function will be removed when BrowserPluginManager is removed and
104 // BrowserPlugin is modified to use a RenderFrame.
105 if (GetBrowserPluginManager()) {
106 GetBrowserPluginManager()->Send(
107 new BrowserPluginHostMsg_ReclaimCompositorResources(
108 host_routing_id_
, GetInstanceID(), params
));
109 } else if (render_frame_proxy_
) {
110 render_frame_proxy_
->Send(
111 new FrameHostMsg_ReclaimCompositorResources(host_routing_id_
, params
));
115 void ChildFrameCompositingHelper::DidCommitCompositorFrame() {
116 if (!resource_collection_
.get() || !ack_pending_
)
119 FrameHostMsg_CompositorFrameSwappedACK_Params params
;
120 params
.producing_host_id
= last_host_id_
;
121 params
.producing_route_id
= last_route_id_
;
122 params
.output_surface_id
= last_output_surface_id_
;
123 resource_collection_
->TakeUnusedResourcesForChildCompositor(
124 ¶ms
.ack
.resources
);
126 SendCompositorFrameSwappedACKToBrowser(params
);
128 ack_pending_
= false;
131 void ChildFrameCompositingHelper::EnableCompositing(bool enable
) {
132 if (enable
&& !background_layer_
.get()) {
133 background_layer_
= cc::SolidColorLayer::Create();
134 background_layer_
->SetMasksToBounds(true);
135 background_layer_
->SetBackgroundColor(
136 SkColorSetARGBInline(255, 255, 255, 255));
137 web_layer_
.reset(new cc_blink::WebLayerImpl(background_layer_
));
140 if (GetContainer()) {
141 GetContainer()->setWebLayer(enable
? web_layer_
.get() : NULL
);
143 frame_
->setRemoteWebLayer(enable
? web_layer_
.get() : NULL
);
147 void ChildFrameCompositingHelper::CheckSizeAndAdjustLayerProperties(
148 const gfx::Size
& new_size
,
149 float device_scale_factor
,
151 if (buffer_size_
!= new_size
) {
152 buffer_size_
= new_size
;
153 // The container size is in DIP, so is the layer size.
154 // Buffer size is in physical pixels, so we need to adjust
155 // it by the device scale factor.
156 gfx::Size device_scale_adjusted_size
= gfx::ToFlooredSize(
157 gfx::ScaleSize(buffer_size_
, 1.0f
/ device_scale_factor
));
158 layer
->SetBounds(device_scale_adjusted_size
);
161 // Manually manage background layer for transparent webview.
163 background_layer_
->SetIsDrawable(false);
166 void ChildFrameCompositingHelper::OnContainerDestroy() {
167 // If we have a pending ACK, then ACK now so we don't lose frames in the
169 DidCommitCompositorFrame();
172 GetContainer()->setWebLayer(NULL
);
174 if (resource_collection_
.get())
175 resource_collection_
->SetClient(NULL
);
177 ack_pending_
= false;
178 resource_collection_
= NULL
;
179 frame_provider_
= NULL
;
180 delegated_layer_
= NULL
;
181 background_layer_
= NULL
;
185 void ChildFrameCompositingHelper::ChildFrameGone() {
186 background_layer_
->SetBackgroundColor(SkColorSetARGBInline(255, 0, 128, 0));
187 background_layer_
->RemoveAllChildren();
188 background_layer_
->SetIsDrawable(true);
189 background_layer_
->SetContentsOpaque(true);
192 void ChildFrameCompositingHelper::OnCompositorFrameSwapped(
193 scoped_ptr
<cc::CompositorFrame
> frame
,
195 uint32 output_surface_id
,
197 base::SharedMemoryHandle handle
) {
198 cc::DelegatedFrameData
* frame_data
= frame
->delegated_frame_data
.get();
199 // Do nothing if we are getting destroyed or have no frame data.
200 if (!frame_data
|| !background_layer_
.get())
203 DCHECK(!frame_data
->render_pass_list
.empty());
204 cc::RenderPass
* root_pass
= frame_data
->render_pass_list
.back();
205 gfx::Size frame_size
= root_pass
->output_rect
.size();
207 if (last_route_id_
!= route_id
||
208 last_output_surface_id_
!= output_surface_id
||
209 last_host_id_
!= host_id
) {
210 // Resource ids are scoped by the output surface.
211 // If the originating output surface doesn't match the last one, it
212 // indicates the guest's output surface may have been recreated, in which
213 // case we should recreate the DelegatedRendererLayer, to avoid matching
214 // resources from the old one with resources from the new one which would
216 frame_provider_
= NULL
;
218 // Drop the cc::DelegatedFrameResourceCollection so that we will not return
219 // any resources from the old output surface with the new output surface id.
220 if (resource_collection_
.get()) {
221 resource_collection_
->SetClient(NULL
);
223 if (resource_collection_
->LoseAllResources())
224 SendReturnedDelegatedResources();
225 resource_collection_
= NULL
;
227 last_output_surface_id_
= output_surface_id
;
228 last_route_id_
= route_id
;
229 last_host_id_
= host_id
;
231 if (!resource_collection_
.get()) {
232 resource_collection_
= new cc::DelegatedFrameResourceCollection
;
233 resource_collection_
->SetClient(this);
235 if (!frame_provider_
.get() || frame_provider_
->frame_size() != frame_size
) {
236 frame_provider_
= new cc::DelegatedFrameProvider(
237 resource_collection_
.get(), frame
->delegated_frame_data
.Pass());
238 if (delegated_layer_
.get())
239 delegated_layer_
->RemoveFromParent();
241 cc::DelegatedRendererLayer::Create(frame_provider_
.get());
242 delegated_layer_
->SetIsDrawable(true);
243 buffer_size_
= gfx::Size();
244 SetContentsOpaque(opaque_
);
245 background_layer_
->AddChild(delegated_layer_
);
247 frame_provider_
->SetFrameData(frame
->delegated_frame_data
.Pass());
250 CheckSizeAndAdjustLayerProperties(
251 frame_data
->render_pass_list
.back()->output_rect
.size(),
252 frame
->metadata
.device_scale_factor
,
253 delegated_layer_
.get());
258 void ChildFrameCompositingHelper::UpdateVisibility(bool visible
) {
259 if (delegated_layer_
.get())
260 delegated_layer_
->SetIsDrawable(visible
);
263 void ChildFrameCompositingHelper::UnusedResourcesAreAvailable() {
267 SendReturnedDelegatedResources();
270 void ChildFrameCompositingHelper::SendReturnedDelegatedResources() {
271 FrameHostMsg_ReclaimCompositorResources_Params params
;
272 if (resource_collection_
.get())
273 resource_collection_
->TakeUnusedResourcesForChildCompositor(
274 ¶ms
.ack
.resources
);
275 DCHECK(!params
.ack
.resources
.empty());
277 params
.route_id
= last_route_id_
;
278 params
.output_surface_id
= last_output_surface_id_
;
279 params
.renderer_host_id
= last_host_id_
;
280 SendReclaimCompositorResourcesToBrowser(params
);
283 void ChildFrameCompositingHelper::SetContentsOpaque(bool opaque
) {
285 if (delegated_layer_
.get())
286 delegated_layer_
->SetContentsOpaque(opaque_
);
289 } // namespace content