Don't change the input filter for the touch input scaler.
[chromium-blink-merge.git] / content / renderer / child_frame_compositing_helper.cc
blob4ac2315b6fa2d9ac92545a44cd8f294d6ad92d34
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/geometry/size_conversions.h"
29 #include "ui/gfx/skia_util.h"
31 namespace content {
33 ChildFrameCompositingHelper*
34 ChildFrameCompositingHelper::CreateForBrowserPlugin(
35 const base::WeakPtr<BrowserPlugin>& browser_plugin) {
36 return new ChildFrameCompositingHelper(
37 browser_plugin, NULL, NULL, browser_plugin->render_frame_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(),
45 render_frame_proxy,
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,
53 int host_routing_id)
54 : host_routing_id_(host_routing_id),
55 last_route_id_(0),
56 last_output_surface_id_(0),
57 last_host_id_(0),
58 ack_pending_(true),
59 opaque_(true),
60 browser_plugin_(browser_plugin),
61 render_frame_proxy_(render_frame_proxy),
62 frame_(frame) {}
64 ChildFrameCompositingHelper::~ChildFrameCompositingHelper() {
65 if (resource_collection_.get())
66 resource_collection_->SetClient(NULL);
69 BrowserPluginManager* ChildFrameCompositingHelper::GetBrowserPluginManager() {
70 if (!browser_plugin_)
71 return NULL;
73 return BrowserPluginManager::Get();
76 blink::WebPluginContainer* ChildFrameCompositingHelper::GetContainer() {
77 if (!browser_plugin_)
78 return NULL;
80 return browser_plugin_->container();
83 int ChildFrameCompositingHelper::GetInstanceID() {
84 if (!browser_plugin_)
85 return 0;
87 return browser_plugin_->browser_plugin_instance_id();
90 void ChildFrameCompositingHelper::SendCompositorFrameSwappedACKToBrowser(
91 FrameHostMsg_CompositorFrameSwappedACK_Params& params) {
92 // This function will be removed when BrowserPluginManager is removed and
93 // BrowserPlugin is modified to use a RenderFrame.
94 if (GetBrowserPluginManager()) {
95 GetBrowserPluginManager()->Send(
96 new BrowserPluginHostMsg_CompositorFrameSwappedACK(
97 GetInstanceID(), params));
98 } else if (render_frame_proxy_) {
99 render_frame_proxy_->Send(
100 new FrameHostMsg_CompositorFrameSwappedACK(host_routing_id_, params));
104 void ChildFrameCompositingHelper::SendReclaimCompositorResourcesToBrowser(
105 FrameHostMsg_ReclaimCompositorResources_Params& params) {
106 // This function will be removed when BrowserPluginManager is removed and
107 // BrowserPlugin is modified to use a RenderFrame.
108 if (GetBrowserPluginManager()) {
109 GetBrowserPluginManager()->Send(
110 new BrowserPluginHostMsg_ReclaimCompositorResources(
111 GetInstanceID(), params));
112 } else if (render_frame_proxy_) {
113 render_frame_proxy_->Send(
114 new FrameHostMsg_ReclaimCompositorResources(host_routing_id_, params));
118 void ChildFrameCompositingHelper::DidCommitCompositorFrame() {
119 if (!resource_collection_.get() || !ack_pending_)
120 return;
122 FrameHostMsg_CompositorFrameSwappedACK_Params params;
123 params.producing_host_id = last_host_id_;
124 params.producing_route_id = last_route_id_;
125 params.output_surface_id = last_output_surface_id_;
126 resource_collection_->TakeUnusedResourcesForChildCompositor(
127 &params.ack.resources);
129 SendCompositorFrameSwappedACKToBrowser(params);
131 ack_pending_ = false;
134 void ChildFrameCompositingHelper::EnableCompositing(bool enable) {
135 if (enable && !background_layer_.get()) {
136 background_layer_ = cc::SolidColorLayer::Create();
137 background_layer_->SetMasksToBounds(true);
138 background_layer_->SetBackgroundColor(
139 SkColorSetARGBInline(255, 255, 255, 255));
140 web_layer_.reset(new cc_blink::WebLayerImpl(background_layer_));
143 if (GetContainer()) {
144 GetContainer()->setWebLayer(enable ? web_layer_.get() : NULL);
145 } else if (frame_) {
146 frame_->setRemoteWebLayer(enable ? web_layer_.get() : NULL);
150 void ChildFrameCompositingHelper::CheckSizeAndAdjustLayerProperties(
151 const gfx::Size& new_size,
152 float device_scale_factor,
153 cc::Layer* layer) {
154 if (buffer_size_ != new_size) {
155 buffer_size_ = new_size;
156 // The container size is in DIP, so is the layer size.
157 // Buffer size is in physical pixels, so we need to adjust
158 // it by the device scale factor.
159 gfx::Size device_scale_adjusted_size = gfx::ToFlooredSize(
160 gfx::ScaleSize(buffer_size_, 1.0f / device_scale_factor));
161 layer->SetBounds(device_scale_adjusted_size);
164 // Manually manage background layer for transparent webview.
165 if (!opaque_)
166 background_layer_->SetIsDrawable(false);
169 void ChildFrameCompositingHelper::OnContainerDestroy() {
170 // If we have a pending ACK, then ACK now so we don't lose frames in the
171 // future.
172 DidCommitCompositorFrame();
174 if (GetContainer())
175 GetContainer()->setWebLayer(NULL);
177 if (resource_collection_.get())
178 resource_collection_->SetClient(NULL);
180 ack_pending_ = false;
181 resource_collection_ = NULL;
182 frame_provider_ = NULL;
183 delegated_layer_ = NULL;
184 background_layer_ = NULL;
185 web_layer_.reset();
188 void ChildFrameCompositingHelper::ChildFrameGone() {
189 background_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 0, 128, 0));
190 background_layer_->RemoveAllChildren();
191 background_layer_->SetIsDrawable(true);
192 background_layer_->SetContentsOpaque(true);
195 void ChildFrameCompositingHelper::OnCompositorFrameSwapped(
196 scoped_ptr<cc::CompositorFrame> frame,
197 int route_id,
198 uint32 output_surface_id,
199 int host_id,
200 base::SharedMemoryHandle handle) {
201 cc::DelegatedFrameData* frame_data = frame->delegated_frame_data.get();
202 // Do nothing if we are getting destroyed or have no frame data.
203 if (!frame_data || !background_layer_.get())
204 return;
206 DCHECK(!frame_data->render_pass_list.empty());
207 cc::RenderPass* root_pass = frame_data->render_pass_list.back();
208 gfx::Size frame_size = root_pass->output_rect.size();
210 if (last_route_id_ != route_id ||
211 last_output_surface_id_ != output_surface_id ||
212 last_host_id_ != host_id) {
213 // Resource ids are scoped by the output surface.
214 // If the originating output surface doesn't match the last one, it
215 // indicates the guest's output surface may have been recreated, in which
216 // case we should recreate the DelegatedRendererLayer, to avoid matching
217 // resources from the old one with resources from the new one which would
218 // have the same id.
219 frame_provider_ = NULL;
221 // Drop the cc::DelegatedFrameResourceCollection so that we will not return
222 // any resources from the old output surface with the new output surface id.
223 if (resource_collection_.get()) {
224 resource_collection_->SetClient(NULL);
226 if (resource_collection_->LoseAllResources())
227 SendReturnedDelegatedResources();
228 resource_collection_ = NULL;
230 last_output_surface_id_ = output_surface_id;
231 last_route_id_ = route_id;
232 last_host_id_ = host_id;
234 if (!resource_collection_.get()) {
235 resource_collection_ = new cc::DelegatedFrameResourceCollection;
236 resource_collection_->SetClient(this);
238 if (!frame_provider_.get() || frame_provider_->frame_size() != frame_size) {
239 frame_provider_ = new cc::DelegatedFrameProvider(
240 resource_collection_.get(), frame->delegated_frame_data.Pass());
241 if (delegated_layer_.get())
242 delegated_layer_->RemoveFromParent();
243 delegated_layer_ =
244 cc::DelegatedRendererLayer::Create(frame_provider_.get());
245 delegated_layer_->SetIsDrawable(true);
246 buffer_size_ = gfx::Size();
247 SetContentsOpaque(opaque_);
248 background_layer_->AddChild(delegated_layer_);
249 } else {
250 frame_provider_->SetFrameData(frame->delegated_frame_data.Pass());
253 CheckSizeAndAdjustLayerProperties(
254 frame_data->render_pass_list.back()->output_rect.size(),
255 frame->metadata.device_scale_factor,
256 delegated_layer_.get());
258 ack_pending_ = true;
261 void ChildFrameCompositingHelper::UpdateVisibility(bool visible) {
262 if (delegated_layer_.get())
263 delegated_layer_->SetIsDrawable(visible);
266 void ChildFrameCompositingHelper::UnusedResourcesAreAvailable() {
267 if (ack_pending_)
268 return;
270 SendReturnedDelegatedResources();
273 void ChildFrameCompositingHelper::SendReturnedDelegatedResources() {
274 FrameHostMsg_ReclaimCompositorResources_Params params;
275 if (resource_collection_.get())
276 resource_collection_->TakeUnusedResourcesForChildCompositor(
277 &params.ack.resources);
278 DCHECK(!params.ack.resources.empty());
280 params.route_id = last_route_id_;
281 params.output_surface_id = last_output_surface_id_;
282 params.renderer_host_id = last_host_id_;
283 SendReclaimCompositorResourcesToBrowser(params);
286 void ChildFrameCompositingHelper::SetContentsOpaque(bool opaque) {
287 opaque_ = opaque;
288 if (delegated_layer_.get())
289 delegated_layer_->SetContentsOpaque(opaque_);
292 } // namespace content