blob: 8790ae2d8a82e5434e0bff9d509f569200799aa9 [file] [log] [blame]
[email protected]bffc8302014-01-23 20:52:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]0f21e8582013-01-11 11:06:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]bffc8302014-01-23 20:52:165#include "content/renderer/child_frame_compositing_helper.h"
[email protected]0f21e8582013-01-11 11:06:566
[email protected]09f67382013-10-09 21:03:457#include "cc/layers/delegated_frame_provider.h"
8#include "cc/layers/delegated_frame_resource_collection.h"
[email protected]f5b4b0f2013-04-02 18:16:289#include "cc/layers/delegated_renderer_layer.h"
[email protected]cc3cfaa2013-03-18 09:05:5210#include "cc/layers/solid_color_layer.h"
11#include "cc/layers/texture_layer.h"
[email protected]7f0d825f2013-03-18 07:24:3012#include "cc/output/context_provider.h"
[email protected]73405fb2013-12-11 04:59:3713#include "cc/output/copy_output_request.h"
14#include "cc/output/copy_output_result.h"
[email protected]9260757f2013-09-17 01:24:1615#include "cc/resources/single_release_callback.h"
[email protected]703dd662013-03-05 07:37:4216#include "content/common/browser_plugin/browser_plugin_messages.h"
[email protected]95d31822014-01-03 22:21:5517#include "content/common/frame_messages.h"
[email protected]a45c46e2013-03-07 01:04:4618#include "content/common/gpu/client/context_provider_command_buffer.h"
[email protected]94224ba62014-02-04 00:25:2419#include "content/renderer/browser_plugin/browser_plugin.h"
[email protected]0d25cb62013-01-21 15:42:2120#include "content/renderer/browser_plugin/browser_plugin_manager.h"
[email protected]e1c52952014-06-18 08:35:4321#include "content/renderer/compositor_bindings/web_layer_impl.h"
[email protected]bffc8302014-01-23 20:52:1622#include "content/renderer/render_frame_impl.h"
[email protected]e3244ed2014-06-20 20:04:2723#include "content/renderer/render_frame_proxy.h"
[email protected]0f21e8582013-01-11 11:06:5624#include "content/renderer/render_thread_impl.h"
[email protected]73405fb2013-12-11 04:59:3725#include "skia/ext/image_operations.h"
[email protected]5c30b5e02013-05-30 03:46:0826#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
[email protected]bffc8302014-01-23 20:52:1627#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]2255a9332013-06-17 05:12:3128#include "third_party/WebKit/public/web/WebPluginContainer.h"
[email protected]7f0d825f2013-03-18 07:24:3029#include "third_party/khronos/GLES2/gl2.h"
[email protected]7e95a6d2013-03-04 23:31:3430#include "ui/gfx/size_conversions.h"
[email protected]73405fb2013-12-11 04:59:3731#include "ui/gfx/skia_util.h"
[email protected]0f21e8582013-01-11 11:06:5632
33namespace content {
34
[email protected]bffc8302014-01-23 20:52:1635ChildFrameCompositingHelper::SwapBuffersInfo::SwapBuffersInfo()
[email protected]9f39c202013-06-18 16:06:4236 : route_id(0),
[email protected]53b4cc12013-07-18 23:02:3037 output_surface_id(0),
[email protected]9f39c202013-06-18 16:06:4238 host_id(0),
39 software_frame_id(0),
[email protected]bffc8302014-01-23 20:52:1640 shared_memory(NULL) {}
[email protected]9f39c202013-06-18 16:06:4241
[email protected]bffc8302014-01-23 20:52:1642ChildFrameCompositingHelper*
43ChildFrameCompositingHelper::CreateCompositingHelperForBrowserPlugin(
[email protected]94224ba62014-02-04 00:25:2444 const base::WeakPtr<BrowserPlugin>& browser_plugin) {
[email protected]bffc8302014-01-23 20:52:1645 return new ChildFrameCompositingHelper(
[email protected]94224ba62014-02-04 00:25:2446 browser_plugin, NULL, NULL, browser_plugin->render_view_routing_id());
[email protected]bffc8302014-01-23 20:52:1647}
48
49ChildFrameCompositingHelper*
50ChildFrameCompositingHelper::CreateCompositingHelperForRenderFrame(
51 blink::WebFrame* frame,
[email protected]e3244ed2014-06-20 20:04:2752 RenderFrameProxy* render_frame_proxy,
[email protected]bffc8302014-01-23 20:52:1653 int host_routing_id) {
54 return new ChildFrameCompositingHelper(
[email protected]e3244ed2014-06-20 20:04:2755 base::WeakPtr<BrowserPlugin>(), frame, render_frame_proxy,
56 host_routing_id);
[email protected]bffc8302014-01-23 20:52:1657}
58
59ChildFrameCompositingHelper::ChildFrameCompositingHelper(
[email protected]94224ba62014-02-04 00:25:2460 const base::WeakPtr<BrowserPlugin>& browser_plugin,
[email protected]bffc8302014-01-23 20:52:1661 blink::WebFrame* frame,
[email protected]e3244ed2014-06-20 20:04:2762 RenderFrameProxy* render_frame_proxy,
[email protected]0f21e8582013-01-11 11:06:5663 int host_routing_id)
[email protected]94224ba62014-02-04 00:25:2464 : host_routing_id_(host_routing_id),
[email protected]f5b4b0f2013-04-02 18:16:2865 last_route_id_(0),
[email protected]53b4cc12013-07-18 23:02:3066 last_output_surface_id_(0),
[email protected]f5b4b0f2013-04-02 18:16:2867 last_host_id_(0),
[email protected]0f21e8582013-01-11 11:06:5668 last_mailbox_valid_(false),
[email protected]0d25cb62013-01-21 15:42:2169 ack_pending_(true),
[email protected]9afdaef2013-09-26 07:24:3770 software_ack_pending_(false),
[email protected]6d3a46e2013-11-25 23:08:5371 opaque_(true),
[email protected]94224ba62014-02-04 00:25:2472 browser_plugin_(browser_plugin),
[email protected]e3244ed2014-06-20 20:04:2773 render_frame_proxy_(render_frame_proxy),
[email protected]bdca9be22014-02-18 00:37:4974 frame_(frame) {}
[email protected]bffc8302014-01-23 20:52:1675
76ChildFrameCompositingHelper::~ChildFrameCompositingHelper() {}
77
[email protected]94224ba62014-02-04 00:25:2478BrowserPluginManager* ChildFrameCompositingHelper::GetBrowserPluginManager() {
79 if (!browser_plugin_)
80 return NULL;
81
82 return browser_plugin_->browser_plugin_manager();
83}
84
85blink::WebPluginContainer* ChildFrameCompositingHelper::GetContainer() {
86 if (!browser_plugin_)
87 return NULL;
88
89 return browser_plugin_->container();
90}
91
92int ChildFrameCompositingHelper::GetInstanceID() {
93 if (!browser_plugin_)
94 return 0;
95
96 return browser_plugin_->guest_instance_id();
97}
98
[email protected]bffc8302014-01-23 20:52:1699void ChildFrameCompositingHelper::SendCompositorFrameSwappedACKToBrowser(
100 FrameHostMsg_CompositorFrameSwappedACK_Params& params) {
101 // This function will be removed when BrowserPluginManager is removed and
102 // BrowserPlugin is modified to use a RenderFrame.
[email protected]94224ba62014-02-04 00:25:24103 if (GetBrowserPluginManager()) {
104 GetBrowserPluginManager()->Send(
[email protected]bffc8302014-01-23 20:52:16105 new BrowserPluginHostMsg_CompositorFrameSwappedACK(
[email protected]94224ba62014-02-04 00:25:24106 host_routing_id_, GetInstanceID(), params));
[email protected]e3244ed2014-06-20 20:04:27107 } else if (render_frame_proxy_) {
108 render_frame_proxy_->Send(
[email protected]bffc8302014-01-23 20:52:16109 new FrameHostMsg_CompositorFrameSwappedACK(host_routing_id_, params));
110 }
[email protected]0f21e8582013-01-11 11:06:56111}
112
[email protected]bffc8302014-01-23 20:52:16113void ChildFrameCompositingHelper::SendBuffersSwappedACKToBrowser(
114 FrameHostMsg_BuffersSwappedACK_Params& params) {
115 // This function will be removed when BrowserPluginManager is removed and
116 // BrowserPlugin is modified to use a RenderFrame.
[email protected]94224ba62014-02-04 00:25:24117 if (GetBrowserPluginManager()) {
118 GetBrowserPluginManager()->Send(new BrowserPluginHostMsg_BuffersSwappedACK(
[email protected]eb455d382014-04-12 10:54:23119 host_routing_id_, params));
[email protected]e3244ed2014-06-20 20:04:27120 } else if (render_frame_proxy_) {
121 render_frame_proxy_->Send(
[email protected]bffc8302014-01-23 20:52:16122 new FrameHostMsg_BuffersSwappedACK(host_routing_id_, params));
123 }
[email protected]0f21e8582013-01-11 11:06:56124}
125
[email protected]bffc8302014-01-23 20:52:16126void ChildFrameCompositingHelper::SendReclaimCompositorResourcesToBrowser(
127 FrameHostMsg_ReclaimCompositorResources_Params& params) {
128 // This function will be removed when BrowserPluginManager is removed and
129 // BrowserPlugin is modified to use a RenderFrame.
[email protected]94224ba62014-02-04 00:25:24130 if (GetBrowserPluginManager()) {
131 GetBrowserPluginManager()->Send(
[email protected]bffc8302014-01-23 20:52:16132 new BrowserPluginHostMsg_ReclaimCompositorResources(
[email protected]94224ba62014-02-04 00:25:24133 host_routing_id_, GetInstanceID(), params));
[email protected]e3244ed2014-06-20 20:04:27134 } else if (render_frame_proxy_) {
135 render_frame_proxy_->Send(
[email protected]bffc8302014-01-23 20:52:16136 new FrameHostMsg_ReclaimCompositorResources(host_routing_id_, params));
137 }
138}
139
140void ChildFrameCompositingHelper::CopyFromCompositingSurface(
[email protected]73405fb2013-12-11 04:59:37141 int request_id,
142 gfx::Rect source_rect,
143 gfx::Size dest_size) {
144 CHECK(background_layer_);
145 scoped_ptr<cc::CopyOutputRequest> request =
146 cc::CopyOutputRequest::CreateBitmapRequest(base::Bind(
[email protected]bffc8302014-01-23 20:52:16147 &ChildFrameCompositingHelper::CopyFromCompositingSurfaceHasResult,
[email protected]73405fb2013-12-11 04:59:37148 this,
149 request_id,
150 dest_size));
151 request->set_area(source_rect);
152 background_layer_->RequestCopyOfOutput(request.Pass());
153}
154
[email protected]bffc8302014-01-23 20:52:16155void ChildFrameCompositingHelper::DidCommitCompositorFrame() {
[email protected]9afdaef2013-09-26 07:24:37156 if (software_ack_pending_) {
[email protected]95d31822014-01-03 22:21:55157 FrameHostMsg_CompositorFrameSwappedACK_Params params;
158 params.producing_host_id = last_host_id_;
159 params.producing_route_id = last_route_id_;
160 params.output_surface_id = last_output_surface_id_;
[email protected]9afdaef2013-09-26 07:24:37161 if (!unacked_software_frames_.empty()) {
[email protected]95d31822014-01-03 22:21:55162 params.ack.last_software_frame_id = unacked_software_frames_.back();
[email protected]9afdaef2013-09-26 07:24:37163 unacked_software_frames_.pop_back();
164 }
165
[email protected]bffc8302014-01-23 20:52:16166 SendCompositorFrameSwappedACKToBrowser(params);
[email protected]9afdaef2013-09-26 07:24:37167
168 software_ack_pending_ = false;
169 }
[email protected]09f67382013-10-09 21:03:45170 if (!resource_collection_.get() || !ack_pending_)
[email protected]f5b4b0f2013-04-02 18:16:28171 return;
[email protected]396fbb72013-01-23 02:33:43172
[email protected]95d31822014-01-03 22:21:55173 FrameHostMsg_CompositorFrameSwappedACK_Params params;
174 params.producing_host_id = last_host_id_;
175 params.producing_route_id = last_route_id_;
176 params.output_surface_id = last_output_surface_id_;
177 resource_collection_->TakeUnusedResourcesForChildCompositor(
178 &params.ack.resources);
[email protected]f5b4b0f2013-04-02 18:16:28179
[email protected]bffc8302014-01-23 20:52:16180 SendCompositorFrameSwappedACKToBrowser(params);
[email protected]f5b4b0f2013-04-02 18:16:28181
182 ack_pending_ = false;
183}
184
[email protected]bffc8302014-01-23 20:52:16185void ChildFrameCompositingHelper::EnableCompositing(bool enable) {
[email protected]fc72bb12013-06-02 21:13:46186 if (enable && !background_layer_.get()) {
[email protected]67be9b1d2013-03-08 02:27:45187 background_layer_ = cc::SolidColorLayer::Create();
[email protected]7aba6662013-03-12 10:17:34188 background_layer_->SetMasksToBounds(true);
189 background_layer_->SetBackgroundColor(
[email protected]396fbb72013-01-23 02:33:43190 SkColorSetARGBInline(255, 255, 255, 255));
[email protected]e1c52952014-06-18 08:35:43191 web_layer_.reset(new WebLayerImpl(background_layer_));
[email protected]0f21e8582013-01-11 11:06:56192 }
193
[email protected]94224ba62014-02-04 00:25:24194 if (GetContainer()) {
195 GetContainer()->setWebLayer(enable ? web_layer_.get() : NULL);
196 } else if (frame_) {
[email protected]bffc8302014-01-23 20:52:16197 frame_->setRemoteWebLayer(enable ? web_layer_.get() : NULL);
198 }
[email protected]0f21e8582013-01-11 11:06:56199}
200
[email protected]bffc8302014-01-23 20:52:16201void ChildFrameCompositingHelper::CheckSizeAndAdjustLayerProperties(
[email protected]f5b4b0f2013-04-02 18:16:28202 const gfx::Size& new_size,
203 float device_scale_factor,
204 cc::Layer* layer) {
205 if (buffer_size_ != new_size) {
206 buffer_size_ = new_size;
207 // The container size is in DIP, so is the layer size.
208 // Buffer size is in physical pixels, so we need to adjust
209 // it by the device scale factor.
210 gfx::Size device_scale_adjusted_size = gfx::ToFlooredSize(
211 gfx::ScaleSize(buffer_size_, 1.0f / device_scale_factor));
212 layer->SetBounds(device_scale_adjusted_size);
213 }
[email protected]6d3a46e2013-11-25 23:08:53214
215 // Manually manage background layer for transparent webview.
216 if (!opaque_)
217 background_layer_->SetIsDrawable(false);
[email protected]f5b4b0f2013-04-02 18:16:28218}
219
[email protected]bffc8302014-01-23 20:52:16220void ChildFrameCompositingHelper::MailboxReleased(SwapBuffersInfo mailbox,
[email protected]df41e252014-02-03 23:39:50221 uint32 sync_point,
[email protected]bffc8302014-01-23 20:52:16222 bool lost_resource) {
[email protected]9f39c202013-06-18 16:06:42223 if (mailbox.type == SOFTWARE_COMPOSITOR_FRAME) {
224 delete mailbox.shared_memory;
225 mailbox.shared_memory = NULL;
226 } else if (lost_resource) {
227 // Reset mailbox's name if the resource was lost.
228 mailbox.name.SetZero();
[email protected]7ba3ca72013-04-11 06:37:25229 }
230
[email protected]c049b2e2013-06-11 04:39:30231 // This means the GPU process crashed or guest crashed.
[email protected]53b4cc12013-07-18 23:02:30232 if (last_host_id_ != mailbox.host_id ||
233 last_output_surface_id_ != mailbox.output_surface_id ||
234 last_route_id_ != mailbox.route_id)
[email protected]4f7bbdc2013-01-29 02:31:39235 return;
236
[email protected]9afdaef2013-09-26 07:24:37237 if (mailbox.type == SOFTWARE_COMPOSITOR_FRAME)
238 unacked_software_frames_.push_back(mailbox.software_frame_id);
239
[email protected]c049b2e2013-06-11 04:39:30240 // We need to send an ACK to for every buffer sent to us.
241 // However, if a buffer is freed up from
[email protected]0d25cb62013-01-21 15:42:21242 // the compositor in cases like switching back to SW mode without a new
[email protected]c049b2e2013-06-11 04:39:30243 // buffer arriving, no ACK is needed.
[email protected]0d25cb62013-01-21 15:42:21244 if (!ack_pending_) {
[email protected]0d25cb62013-01-21 15:42:21245 last_mailbox_valid_ = false;
246 return;
247 }
248 ack_pending_ = false;
[email protected]c049b2e2013-06-11 04:39:30249 switch (mailbox.type) {
250 case TEXTURE_IMAGE_TRANSPORT: {
[email protected]95d31822014-01-03 22:21:55251 FrameHostMsg_BuffersSwappedACK_Params params;
252 params.gpu_host_id = mailbox.host_id;
253 params.gpu_route_id = mailbox.route_id;
[email protected]7c6c09e2014-02-15 00:16:17254 params.mailbox = mailbox.name;
[email protected]95d31822014-01-03 22:21:55255 params.sync_point = sync_point;
[email protected]bffc8302014-01-23 20:52:16256 SendBuffersSwappedACKToBrowser(params);
[email protected]c049b2e2013-06-11 04:39:30257 break;
258 }
[email protected]9f39c202013-06-18 16:06:42259 case GL_COMPOSITOR_FRAME: {
[email protected]95d31822014-01-03 22:21:55260 FrameHostMsg_CompositorFrameSwappedACK_Params params;
261 params.producing_host_id = mailbox.host_id;
262 params.producing_route_id = mailbox.route_id;
263 params.output_surface_id = mailbox.output_surface_id;
264 params.ack.gl_frame_data.reset(new cc::GLFrameData());
265 params.ack.gl_frame_data->mailbox = mailbox.name;
266 params.ack.gl_frame_data->size = mailbox.size;
267 params.ack.gl_frame_data->sync_point = sync_point;
[email protected]bffc8302014-01-23 20:52:16268 SendCompositorFrameSwappedACKToBrowser(params);
[email protected]c049b2e2013-06-11 04:39:30269 break;
270 }
[email protected]9afdaef2013-09-26 07:24:37271 case SOFTWARE_COMPOSITOR_FRAME:
[email protected]9f39c202013-06-18 16:06:42272 break;
[email protected]c049b2e2013-06-11 04:39:30273 }
[email protected]0d25cb62013-01-21 15:42:21274}
275
[email protected]bffc8302014-01-23 20:52:16276void ChildFrameCompositingHelper::OnContainerDestroy() {
[email protected]94224ba62014-02-04 00:25:24277 if (GetContainer())
278 GetContainer()->setWebLayer(NULL);
[email protected]0d25cb62013-01-21 15:42:21279
[email protected]b0030b72013-11-15 20:35:53280 if (resource_collection_)
281 resource_collection_->SetClient(NULL);
282
[email protected]7873fe4e2013-11-14 18:35:22283 ack_pending_ = false;
284 software_ack_pending_ = false;
[email protected]b0030b72013-11-15 20:35:53285 resource_collection_ = NULL;
286 frame_provider_ = NULL;
[email protected]0d25cb62013-01-21 15:42:21287 texture_layer_ = NULL;
[email protected]f5b4b0f2013-04-02 18:16:28288 delegated_layer_ = NULL;
[email protected]396fbb72013-01-23 02:33:43289 background_layer_ = NULL;
[email protected]0d25cb62013-01-21 15:42:21290 web_layer_.reset();
291}
292
[email protected]f49722f2014-01-30 17:54:50293void ChildFrameCompositingHelper::ChildFrameGone() {
294 background_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 0, 128, 0));
295 background_layer_->RemoveAllChildren();
296 background_layer_->SetIsDrawable(true);
297 background_layer_->SetContentsOpaque(true);
298}
299
[email protected]bffc8302014-01-23 20:52:16300void ChildFrameCompositingHelper::OnBuffersSwappedPrivate(
[email protected]9f39c202013-06-18 16:06:42301 const SwapBuffersInfo& mailbox,
[email protected]df41e252014-02-03 23:39:50302 uint32 sync_point,
[email protected]19fa81a02013-03-04 18:23:32303 float device_scale_factor) {
[email protected]fc72bb12013-06-02 21:13:46304 DCHECK(!delegated_layer_.get());
[email protected]4f7bbdc2013-01-29 02:31:39305 // If these mismatch, we are either just starting up, GPU process crashed or
306 // guest renderer crashed.
307 // In this case, we are communicating with a new image transport
308 // surface and must ACK with the new ID's and an empty mailbox.
[email protected]53b4cc12013-07-18 23:02:30309 if (last_route_id_ != mailbox.route_id ||
310 last_output_surface_id_ != mailbox.output_surface_id ||
311 last_host_id_ != mailbox.host_id)
[email protected]4f7bbdc2013-01-29 02:31:39312 last_mailbox_valid_ = false;
313
[email protected]c049b2e2013-06-11 04:39:30314 last_route_id_ = mailbox.route_id;
[email protected]53b4cc12013-07-18 23:02:30315 last_output_surface_id_ = mailbox.output_surface_id;
[email protected]c049b2e2013-06-11 04:39:30316 last_host_id_ = mailbox.host_id;
[email protected]4f7bbdc2013-01-29 02:31:39317
[email protected]0d25cb62013-01-21 15:42:21318 ack_pending_ = true;
319 // Browser plugin getting destroyed, do a fast ACK.
[email protected]fc72bb12013-06-02 21:13:46320 if (!background_layer_.get()) {
[email protected]c049b2e2013-06-11 04:39:30321 MailboxReleased(mailbox, sync_point, false);
[email protected]0d25cb62013-01-21 15:42:21322 return;
323 }
324
[email protected]fc72bb12013-06-02 21:13:46325 if (!texture_layer_.get()) {
[email protected]e8e4ae232013-04-12 00:26:01326 texture_layer_ = cc::TextureLayer::CreateForMailbox(NULL);
[email protected]f5b4b0f2013-04-02 18:16:28327 texture_layer_->SetIsDrawable(true);
[email protected]6d3a46e2013-11-25 23:08:53328 SetContentsOpaque(opaque_);
[email protected]f5b4b0f2013-04-02 18:16:28329
330 background_layer_->AddChild(texture_layer_);
331 }
332
[email protected]0d25cb62013-01-21 15:42:21333 // The size of browser plugin container is not always equal to the size
334 // of the buffer that arrives here. This could be for a number of reasons,
335 // including autosize and a resize in progress.
336 // During resize, the container size changes first and then some time
337 // later, a new buffer with updated size will arrive. During this process,
338 // we need to make sure that things are still displayed pixel perfect.
[email protected]396fbb72013-01-23 02:33:43339 // We accomplish this by modifying bounds of the texture layer only
340 // when a new buffer arrives.
341 // Visually, this will either display a smaller part of the buffer
342 // or introduce a gutter around it.
[email protected]bffc8302014-01-23 20:52:16343 CheckSizeAndAdjustLayerProperties(
344 mailbox.size, device_scale_factor, texture_layer_.get());
[email protected]0f21e8582013-01-11 11:06:56345
[email protected]9f39c202013-06-18 16:06:42346 bool is_software_frame = mailbox.type == SOFTWARE_COMPOSITOR_FRAME;
[email protected]bffc8302014-01-23 20:52:16347 bool current_mailbox_valid = is_software_frame ? mailbox.shared_memory != NULL
348 : !mailbox.name.IsZero();
[email protected]9f39c202013-06-18 16:06:42349 if (!is_software_frame && !last_mailbox_valid_) {
350 SwapBuffersInfo empty_info = mailbox;
351 empty_info.name.SetZero();
[email protected]c049b2e2013-06-11 04:39:30352 MailboxReleased(empty_info, 0, false);
[email protected]0d25cb62013-01-21 15:42:21353 if (!current_mailbox_valid)
354 return;
355 }
[email protected]c2bfe4e2013-01-16 01:10:20356
[email protected]9f39c202013-06-18 16:06:42357 cc::TextureMailbox texture_mailbox;
[email protected]9260757f2013-09-17 01:24:16358 scoped_ptr<cc::SingleReleaseCallback> release_callback;
[email protected]c2bfe4e2013-01-16 01:10:20359 if (current_mailbox_valid) {
[email protected]bffc8302014-01-23 20:52:16360 release_callback =
361 cc::SingleReleaseCallback::Create(
362 base::Bind(&ChildFrameCompositingHelper::MailboxReleased,
363 scoped_refptr<ChildFrameCompositingHelper>(this),
364 mailbox)).Pass();
[email protected]df41e252014-02-03 23:39:50365 if (is_software_frame) {
[email protected]9260757f2013-09-17 01:24:16366 texture_mailbox = cc::TextureMailbox(mailbox.shared_memory, mailbox.size);
[email protected]df41e252014-02-03 23:39:50367 } else {
368 texture_mailbox =
369 cc::TextureMailbox(mailbox.name, GL_TEXTURE_2D, sync_point);
370 }
[email protected]c2bfe4e2013-01-16 01:10:20371 }
[email protected]9f39c202013-06-18 16:06:42372
373 texture_layer_->SetFlipped(!is_software_frame);
[email protected]9260757f2013-09-17 01:24:16374 texture_layer_->SetTextureMailbox(texture_mailbox, release_callback.Pass());
[email protected]7aba6662013-03-12 10:17:34375 texture_layer_->SetNeedsDisplay();
[email protected]c2bfe4e2013-01-16 01:10:20376 last_mailbox_valid_ = current_mailbox_valid;
377}
378
[email protected]bffc8302014-01-23 20:52:16379void ChildFrameCompositingHelper::OnBuffersSwapped(
[email protected]c049b2e2013-06-11 04:39:30380 const gfx::Size& size,
[email protected]7c6c09e2014-02-15 00:16:17381 const gpu::Mailbox& mailbox,
[email protected]c049b2e2013-06-11 04:39:30382 int gpu_route_id,
383 int gpu_host_id,
384 float device_scale_factor) {
[email protected]9f39c202013-06-18 16:06:42385 SwapBuffersInfo swap_info;
[email protected]7c6c09e2014-02-15 00:16:17386 swap_info.name = mailbox;
[email protected]c049b2e2013-06-11 04:39:30387 swap_info.type = TEXTURE_IMAGE_TRANSPORT;
388 swap_info.size = size;
389 swap_info.route_id = gpu_route_id;
[email protected]53b4cc12013-07-18 23:02:30390 swap_info.output_surface_id = 0;
[email protected]c049b2e2013-06-11 04:39:30391 swap_info.host_id = gpu_host_id;
392 OnBuffersSwappedPrivate(swap_info, 0, device_scale_factor);
393}
394
[email protected]bffc8302014-01-23 20:52:16395void ChildFrameCompositingHelper::OnCompositorFrameSwapped(
[email protected]f5b4b0f2013-04-02 18:16:28396 scoped_ptr<cc::CompositorFrame> frame,
397 int route_id,
[email protected]53b4cc12013-07-18 23:02:30398 uint32 output_surface_id,
[email protected]f1970082014-04-09 04:29:56399 int host_id,
400 base::SharedMemoryHandle handle) {
[email protected]bffc8302014-01-23 20:52:16401
[email protected]c049b2e2013-06-11 04:39:30402 if (frame->gl_frame_data) {
[email protected]9f39c202013-06-18 16:06:42403 SwapBuffersInfo swap_info;
[email protected]c049b2e2013-06-11 04:39:30404 swap_info.name = frame->gl_frame_data->mailbox;
[email protected]9f39c202013-06-18 16:06:42405 swap_info.type = GL_COMPOSITOR_FRAME;
[email protected]c049b2e2013-06-11 04:39:30406 swap_info.size = frame->gl_frame_data->size;
407 swap_info.route_id = route_id;
[email protected]53b4cc12013-07-18 23:02:30408 swap_info.output_surface_id = output_surface_id;
[email protected]c049b2e2013-06-11 04:39:30409 swap_info.host_id = host_id;
410 OnBuffersSwappedPrivate(swap_info,
411 frame->gl_frame_data->sync_point,
412 frame->metadata.device_scale_factor);
413 return;
414 }
415
[email protected]9f39c202013-06-18 16:06:42416 if (frame->software_frame_data) {
417 cc::SoftwareFrameData* frame_data = frame->software_frame_data.get();
418
419 SwapBuffersInfo swap_info;
420 swap_info.type = SOFTWARE_COMPOSITOR_FRAME;
421 swap_info.size = frame_data->size;
422 swap_info.route_id = route_id;
[email protected]53b4cc12013-07-18 23:02:30423 swap_info.output_surface_id = output_surface_id;
[email protected]9f39c202013-06-18 16:06:42424 swap_info.host_id = host_id;
425 swap_info.software_frame_id = frame_data->id;
426
427 scoped_ptr<base::SharedMemory> shared_memory(
[email protected]f1970082014-04-09 04:29:56428 new base::SharedMemory(handle, true));
[email protected]9f39c202013-06-18 16:06:42429 const size_t size_in_bytes = 4 * frame_data->size.GetArea();
430 if (!shared_memory->Map(size_in_bytes)) {
[email protected]bffc8302014-01-23 20:52:16431 LOG(ERROR) << "Failed to map shared memory of size " << size_in_bytes;
[email protected]9f39c202013-06-18 16:06:42432 // Send ACK right away.
[email protected]9afdaef2013-09-26 07:24:37433 software_ack_pending_ = true;
[email protected]9f39c202013-06-18 16:06:42434 MailboxReleased(swap_info, 0, false);
[email protected]9afdaef2013-09-26 07:24:37435 DidCommitCompositorFrame();
[email protected]9f39c202013-06-18 16:06:42436 return;
437 }
438
439 swap_info.shared_memory = shared_memory.release();
[email protected]bffc8302014-01-23 20:52:16440 OnBuffersSwappedPrivate(swap_info, 0, frame->metadata.device_scale_factor);
[email protected]9afdaef2013-09-26 07:24:37441 software_ack_pending_ = true;
442 last_route_id_ = route_id;
443 last_output_surface_id_ = output_surface_id;
444 last_host_id_ = host_id;
[email protected]9f39c202013-06-18 16:06:42445 return;
446 }
447
[email protected]fc72bb12013-06-02 21:13:46448 DCHECK(!texture_layer_.get());
[email protected]f5b4b0f2013-04-02 18:16:28449
[email protected]09f67382013-10-09 21:03:45450 cc::DelegatedFrameData* frame_data = frame->delegated_frame_data.get();
[email protected]b0030b72013-11-15 20:35:53451 // Do nothing if we are getting destroyed or have no frame data.
452 if (!frame_data || !background_layer_)
[email protected]f5b4b0f2013-04-02 18:16:28453 return;
454
[email protected]09f67382013-10-09 21:03:45455 DCHECK(!frame_data->render_pass_list.empty());
456 cc::RenderPass* root_pass = frame_data->render_pass_list.back();
457 gfx::Size frame_size = root_pass->output_rect.size();
458
[email protected]317f17d2013-11-09 05:03:48459 if (last_route_id_ != route_id ||
460 last_output_surface_id_ != output_surface_id ||
461 last_host_id_ != host_id) {
462 // Resource ids are scoped by the output surface.
463 // If the originating output surface doesn't match the last one, it
464 // indicates the guest's output surface may have been recreated, in which
465 // case we should recreate the DelegatedRendererLayer, to avoid matching
466 // resources from the old one with resources from the new one which would
467 // have the same id.
468 frame_provider_ = NULL;
469
470 // Drop the cc::DelegatedFrameResourceCollection so that we will not return
471 // any resources from the old output surface with the new output surface id.
472 if (resource_collection_) {
[email protected]b0030b72013-11-15 20:35:53473 resource_collection_->SetClient(NULL);
474
475 if (resource_collection_->LoseAllResources())
476 SendReturnedDelegatedResources();
[email protected]317f17d2013-11-09 05:03:48477 resource_collection_ = NULL;
478 }
479 last_output_surface_id_ = output_surface_id;
480 last_route_id_ = route_id;
481 last_host_id_ = host_id;
482 }
[email protected]09f67382013-10-09 21:03:45483 if (!resource_collection_) {
484 resource_collection_ = new cc::DelegatedFrameResourceCollection;
[email protected]b0030b72013-11-15 20:35:53485 resource_collection_->SetClient(this);
[email protected]09f67382013-10-09 21:03:45486 }
487 if (!frame_provider_.get() || frame_provider_->frame_size() != frame_size) {
488 frame_provider_ = new cc::DelegatedFrameProvider(
489 resource_collection_.get(), frame->delegated_frame_data.Pass());
490 if (delegated_layer_.get())
491 delegated_layer_->RemoveFromParent();
492 delegated_layer_ =
[email protected]b59c1612013-12-04 00:56:49493 cc::DelegatedRendererLayer::Create(frame_provider_.get());
[email protected]09f67382013-10-09 21:03:45494 delegated_layer_->SetIsDrawable(true);
[email protected]6d3a46e2013-11-25 23:08:53495 SetContentsOpaque(opaque_);
[email protected]09f67382013-10-09 21:03:45496 background_layer_->AddChild(delegated_layer_);
497 } else {
498 frame_provider_->SetFrameData(frame->delegated_frame_data.Pass());
499 }
500
[email protected]6d3a46e2013-11-25 23:08:53501 CheckSizeAndAdjustLayerProperties(
[email protected]f5b4b0f2013-04-02 18:16:28502 frame_data->render_pass_list.back()->output_rect.size(),
503 frame->metadata.device_scale_factor,
504 delegated_layer_.get());
505
[email protected]f5b4b0f2013-04-02 18:16:28506 ack_pending_ = true;
507}
508
[email protected]bffc8302014-01-23 20:52:16509void ChildFrameCompositingHelper::UpdateVisibility(bool visible) {
[email protected]fc72bb12013-06-02 21:13:46510 if (texture_layer_.get())
[email protected]7aba6662013-03-12 10:17:34511 texture_layer_->SetIsDrawable(visible);
[email protected]fc72bb12013-06-02 21:13:46512 if (delegated_layer_.get())
[email protected]f5b4b0f2013-04-02 18:16:28513 delegated_layer_->SetIsDrawable(visible);
[email protected]69b79122013-02-14 19:16:45514}
515
[email protected]bffc8302014-01-23 20:52:16516void ChildFrameCompositingHelper::UnusedResourcesAreAvailable() {
[email protected]b0030b72013-11-15 20:35:53517 if (ack_pending_)
518 return;
519
520 SendReturnedDelegatedResources();
521}
522
[email protected]bffc8302014-01-23 20:52:16523void ChildFrameCompositingHelper::SendReturnedDelegatedResources() {
524 FrameHostMsg_ReclaimCompositorResources_Params params;
[email protected]b0030b72013-11-15 20:35:53525 if (resource_collection_)
[email protected]bffc8302014-01-23 20:52:16526 resource_collection_->TakeUnusedResourcesForChildCompositor(
527 &params.ack.resources);
528 DCHECK(!params.ack.resources.empty());
[email protected]b0030b72013-11-15 20:35:53529
[email protected]bffc8302014-01-23 20:52:16530 params.route_id = last_route_id_;
531 params.output_surface_id = last_output_surface_id_;
532 params.renderer_host_id = last_host_id_;
533 SendReclaimCompositorResourcesToBrowser(params);
[email protected]b0030b72013-11-15 20:35:53534}
535
[email protected]bffc8302014-01-23 20:52:16536void ChildFrameCompositingHelper::SetContentsOpaque(bool opaque) {
[email protected]6d3a46e2013-11-25 23:08:53537 opaque_ = opaque;
538
539 if (texture_layer_.get())
540 texture_layer_->SetContentsOpaque(opaque_);
541 if (delegated_layer_.get())
542 delegated_layer_->SetContentsOpaque(opaque_);
543}
544
[email protected]bffc8302014-01-23 20:52:16545void ChildFrameCompositingHelper::CopyFromCompositingSurfaceHasResult(
[email protected]73405fb2013-12-11 04:59:37546 int request_id,
547 gfx::Size dest_size,
548 scoped_ptr<cc::CopyOutputResult> result) {
549 scoped_ptr<SkBitmap> bitmap;
550 if (result && result->HasBitmap() && !result->size().IsEmpty())
551 bitmap = result->TakeBitmap();
552
553 SkBitmap resized_bitmap;
554 if (bitmap) {
[email protected]bffc8302014-01-23 20:52:16555 resized_bitmap =
556 skia::ImageOperations::Resize(*bitmap,
557 skia::ImageOperations::RESIZE_BEST,
558 dest_size.width(),
559 dest_size.height());
[email protected]73405fb2013-12-11 04:59:37560 }
[email protected]94224ba62014-02-04 00:25:24561 if (GetBrowserPluginManager()) {
562 GetBrowserPluginManager()->Send(
563 new BrowserPluginHostMsg_CopyFromCompositingSurfaceAck(
564 host_routing_id_, GetInstanceID(), request_id, resized_bitmap));
565 }
[email protected]73405fb2013-12-11 04:59:37566}
567
[email protected]0f21e8582013-01-11 11:06:56568} // namespace content