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/browser/frame_host/render_widget_host_view_child_frame.h"
7 #include "base/command_line.h"
8 #include "cc/surfaces/surface.h"
9 #include "cc/surfaces/surface_factory.h"
10 #include "cc/surfaces/surface_manager.h"
11 #include "cc/surfaces/surface_sequence.h"
12 #include "content/browser/accessibility/browser_accessibility_manager.h"
13 #include "content/browser/browser_plugin/browser_plugin_guest.h"
14 #include "content/browser/compositor/surface_utils.h"
15 #include "content/browser/frame_host/cross_process_frame_connector.h"
16 #include "content/browser/gpu/compositor_util.h"
17 #include "content/browser/renderer_host/render_view_host_impl.h"
18 #include "content/browser/renderer_host/render_widget_host_impl.h"
19 #include "content/common/gpu/gpu_messages.h"
20 #include "content/common/view_messages.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/common/content_switches.h"
26 RenderWidgetHostViewChildFrame::RenderWidgetHostViewChildFrame(
27 RenderWidgetHost
* widget_host
)
28 : host_(RenderWidgetHostImpl::From(widget_host
)),
29 use_surfaces_(UseSurfacesEnabled()),
30 next_surface_sequence_(1u),
31 last_output_surface_id_(0),
32 current_surface_scale_factor_(1.f
),
33 ack_pending_count_(0),
34 frame_connector_(nullptr),
37 id_allocator_
= CreateSurfaceIdAllocator();
42 RenderWidgetHostViewChildFrame::~RenderWidgetHostViewChildFrame() {
43 if (!surface_id_
.is_null())
44 surface_factory_
->Destroy(surface_id_
);
47 void RenderWidgetHostViewChildFrame::InitAsChild(
48 gfx::NativeView parent_view
) {
52 RenderWidgetHost
* RenderWidgetHostViewChildFrame::GetRenderWidgetHost() const {
56 void RenderWidgetHostViewChildFrame::SetSize(const gfx::Size
& size
) {
60 void RenderWidgetHostViewChildFrame::SetBounds(const gfx::Rect
& rect
) {
64 void RenderWidgetHostViewChildFrame::Focus() {
67 bool RenderWidgetHostViewChildFrame::HasFocus() const {
71 bool RenderWidgetHostViewChildFrame::IsSurfaceAvailableForCopy() const {
76 void RenderWidgetHostViewChildFrame::Show() {
77 if (!host_
->is_hidden())
79 host_
->WasShown(ui::LatencyInfo());
82 void RenderWidgetHostViewChildFrame::Hide() {
83 if (host_
->is_hidden())
88 bool RenderWidgetHostViewChildFrame::IsShowing() {
89 return !host_
->is_hidden();
92 gfx::Rect
RenderWidgetHostViewChildFrame::GetViewBounds() const {
95 rect
= frame_connector_
->ChildFrameRect();
99 gfx::Vector2dF
RenderWidgetHostViewChildFrame::GetLastScrollOffset() const {
100 return last_scroll_offset_
;
103 gfx::NativeView
RenderWidgetHostViewChildFrame::GetNativeView() const {
108 gfx::NativeViewId
RenderWidgetHostViewChildFrame::GetNativeViewId() const {
113 gfx::NativeViewAccessible
114 RenderWidgetHostViewChildFrame::GetNativeViewAccessible() {
119 void RenderWidgetHostViewChildFrame::SetBackgroundColor(SkColor color
) {
122 gfx::Size
RenderWidgetHostViewChildFrame::GetPhysicalBackingSize() const {
124 if (frame_connector_
)
125 size
= frame_connector_
->ChildFrameRect().size();
129 void RenderWidgetHostViewChildFrame::InitAsPopup(
130 RenderWidgetHostView
* parent_host_view
,
131 const gfx::Rect
& bounds
) {
135 void RenderWidgetHostViewChildFrame::InitAsFullscreen(
136 RenderWidgetHostView
* reference_host_view
) {
140 void RenderWidgetHostViewChildFrame::ImeCancelComposition() {
144 void RenderWidgetHostViewChildFrame::ImeCompositionRangeChanged(
145 const gfx::Range
& range
,
146 const std::vector
<gfx::Rect
>& character_bounds
) {
150 void RenderWidgetHostViewChildFrame::MovePluginWindows(
151 const std::vector
<WebPluginGeometry
>& moves
) {
154 void RenderWidgetHostViewChildFrame::UpdateCursor(const WebCursor
& cursor
) {
157 void RenderWidgetHostViewChildFrame::SetIsLoading(bool is_loading
) {
158 // It is valid for an inner WebContents's SetIsLoading() to end up here.
159 // This is because an inner WebContents's main frame's RenderWidgetHostView
160 // is a RenderWidgetHostViewChildFrame. In contrast, when there is no
161 // inner/outer WebContents, only subframe's RenderWidgetHostView can be a
162 // RenderWidgetHostViewChildFrame which do not get a SetIsLoading() call.
163 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
164 switches::kSitePerProcess
) &&
165 BrowserPluginGuest::IsGuest(
166 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(host_
)))) {
173 void RenderWidgetHostViewChildFrame::TextInputTypeChanged(
174 ui::TextInputType type
,
175 ui::TextInputMode input_mode
,
176 bool can_compose_inline
,
178 // TODO(kenrb): Implement.
181 void RenderWidgetHostViewChildFrame::RenderProcessGone(
182 base::TerminationStatus status
,
184 if (frame_connector_
)
185 frame_connector_
->RenderProcessGone();
189 void RenderWidgetHostViewChildFrame::Destroy() {
190 if (frame_connector_
) {
191 frame_connector_
->set_view(NULL
);
192 frame_connector_
= NULL
;
195 host_
->SetView(NULL
);
197 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
200 void RenderWidgetHostViewChildFrame::SetTooltipText(
201 const base::string16
& tooltip_text
) {
204 void RenderWidgetHostViewChildFrame::SelectionChanged(
205 const base::string16
& text
,
207 const gfx::Range
& range
) {
210 void RenderWidgetHostViewChildFrame::SelectionBoundsChanged(
211 const ViewHostMsg_SelectionBounds_Params
& params
) {
214 #if defined(OS_ANDROID)
215 void RenderWidgetHostViewChildFrame::LockCompositingSurface() {
218 void RenderWidgetHostViewChildFrame::UnlockCompositingSurface() {
222 void RenderWidgetHostViewChildFrame::SurfaceDrawn(uint32 output_surface_id
,
223 cc::SurfaceDrawStatus drawn
) {
224 cc::CompositorFrameAck ack
;
225 DCHECK(ack_pending_count_
> 0);
226 if (!surface_returned_resources_
.empty())
227 ack
.resources
.swap(surface_returned_resources_
);
229 host_
->Send(new ViewMsg_SwapCompositorFrameAck(host_
->GetRoutingID(),
230 output_surface_id
, ack
));
232 ack_pending_count_
--;
235 void RenderWidgetHostViewChildFrame::OnSwapCompositorFrame(
236 uint32 output_surface_id
,
237 scoped_ptr
<cc::CompositorFrame
> frame
) {
238 last_scroll_offset_
= frame
->metadata
.root_scroll_offset
;
240 if (!frame_connector_
)
243 // When not using surfaces, the frame just gets proxied to
244 // the embedder's renderer to be composited.
245 if (!frame
->delegated_frame_data
|| !use_surfaces_
) {
246 frame_connector_
->ChildFrameCompositorFrameSwapped(
248 host_
->GetProcess()->GetID(),
249 host_
->GetRoutingID(),
254 cc::RenderPass
* root_pass
=
255 frame
->delegated_frame_data
->render_pass_list
.back();
257 gfx::Size frame_size
= root_pass
->output_rect
.size();
258 float scale_factor
= frame
->metadata
.device_scale_factor
;
260 // Check whether we need to recreate the cc::Surface, which means the child
261 // frame renderer has changed its output surface, or size, or scale factor.
262 if (output_surface_id
!= last_output_surface_id_
&& surface_factory_
) {
263 surface_factory_
->Destroy(surface_id_
);
264 surface_factory_
.reset();
266 if (output_surface_id
!= last_output_surface_id_
||
267 frame_size
!= current_surface_size_
||
268 scale_factor
!= current_surface_scale_factor_
) {
269 ClearCompositorSurfaceIfNecessary();
270 last_output_surface_id_
= output_surface_id
;
271 current_surface_size_
= frame_size
;
272 current_surface_scale_factor_
= scale_factor
;
275 if (!surface_factory_
) {
276 cc::SurfaceManager
* manager
= GetSurfaceManager();
277 surface_factory_
= make_scoped_ptr(new cc::SurfaceFactory(manager
, this));
280 if (surface_id_
.is_null()) {
281 surface_id_
= id_allocator_
->GenerateId();
282 surface_factory_
->Create(surface_id_
);
284 cc::SurfaceSequence sequence
= cc::SurfaceSequence(
285 id_allocator_
->id_namespace(), next_surface_sequence_
++);
286 // The renderer process will satisfy this dependency when it creates a
288 cc::SurfaceManager
* manager
= GetSurfaceManager();
289 manager
->GetSurfaceForId(surface_id_
)->AddDestructionDependency(sequence
);
290 frame_connector_
->SetChildFrameSurface(surface_id_
, frame_size
,
291 scale_factor
, sequence
);
294 cc::SurfaceFactory::DrawCallback ack_callback
=
295 base::Bind(&RenderWidgetHostViewChildFrame::SurfaceDrawn
, AsWeakPtr(),
297 ack_pending_count_
++;
298 // If this value grows very large, something is going wrong.
299 DCHECK(ack_pending_count_
< 1000);
300 surface_factory_
->SubmitFrame(surface_id_
, frame
.Pass(), ack_callback
);
303 void RenderWidgetHostViewChildFrame::GetScreenInfo(
304 blink::WebScreenInfo
* results
) {
305 if (!frame_connector_
)
307 frame_connector_
->GetScreenInfo(results
);
310 gfx::Rect
RenderWidgetHostViewChildFrame::GetBoundsInRootWindow() {
311 // We do not have any root window specific parts in this view.
312 return GetViewBounds();
315 #if defined(USE_AURA)
316 void RenderWidgetHostViewChildFrame::ProcessAckedTouchEvent(
317 const TouchEventWithLatencyInfo
& touch
,
318 InputEventAckState ack_result
) {
320 #endif // defined(USE_AURA)
322 bool RenderWidgetHostViewChildFrame::LockMouse() {
326 void RenderWidgetHostViewChildFrame::UnlockMouse() {
329 uint32_t RenderWidgetHostViewChildFrame::GetSurfaceIdNamespace() {
333 return id_allocator_
->id_namespace();
336 #if defined(OS_MACOSX)
337 void RenderWidgetHostViewChildFrame::SetActive(bool active
) {
340 void RenderWidgetHostViewChildFrame::SetWindowVisibility(bool visible
) {
343 void RenderWidgetHostViewChildFrame::WindowFrameChanged() {
346 void RenderWidgetHostViewChildFrame::ShowDefinitionForSelection() {
349 bool RenderWidgetHostViewChildFrame::SupportsSpeech() const {
353 void RenderWidgetHostViewChildFrame::SpeakSelection() {
356 bool RenderWidgetHostViewChildFrame::IsSpeaking() const {
360 void RenderWidgetHostViewChildFrame::StopSpeaking() {
363 bool RenderWidgetHostViewChildFrame::PostProcessEventForPluginIme(
364 const NativeWebKeyboardEvent
& event
) {
367 #endif // defined(OS_MACOSX)
369 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurface(
370 const gfx::Rect
& src_subrect
,
371 const gfx::Size
& /* dst_size */,
372 ReadbackRequestCallback
& callback
,
373 const SkColorType preferred_color_type
) {
374 callback
.Run(SkBitmap(), READBACK_FAILED
);
377 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurfaceToVideoFrame(
378 const gfx::Rect
& src_subrect
,
379 const scoped_refptr
<media::VideoFrame
>& target
,
380 const base::Callback
<void(bool)>& callback
) {
385 bool RenderWidgetHostViewChildFrame::CanCopyToVideoFrame() const {
389 bool RenderWidgetHostViewChildFrame::HasAcceleratedSurface(
390 const gfx::Size
& desired_size
) {
394 gfx::GLSurfaceHandle
RenderWidgetHostViewChildFrame::GetCompositingSurface() {
395 return gfx::GLSurfaceHandle(gfx::kNullPluginWindow
, gfx::NULL_TRANSPORT
);
399 void RenderWidgetHostViewChildFrame::SetParentNativeViewAccessible(
400 gfx::NativeViewAccessible accessible_parent
) {
403 gfx::NativeViewId
RenderWidgetHostViewChildFrame::GetParentForWindowlessPlugin()
407 #endif // defined(OS_WIN)
409 // cc::SurfaceFactoryClient implementation.
410 void RenderWidgetHostViewChildFrame::ReturnResources(
411 const cc::ReturnedResourceArray
& resources
) {
412 if (resources
.empty())
415 if (!ack_pending_count_
&& host_
) {
416 cc::CompositorFrameAck ack
;
417 std::copy(resources
.begin(), resources
.end(),
418 std::back_inserter(ack
.resources
));
419 host_
->Send(new ViewMsg_ReclaimCompositorResources(
420 host_
->GetRoutingID(), last_output_surface_id_
, ack
));
424 std::copy(resources
.begin(), resources
.end(),
425 std::back_inserter(surface_returned_resources_
));
428 BrowserAccessibilityManager
*
429 RenderWidgetHostViewChildFrame::CreateBrowserAccessibilityManager(
430 BrowserAccessibilityDelegate
* delegate
) {
431 return BrowserAccessibilityManager::Create(
432 BrowserAccessibilityManager::GetEmptyDocument(), delegate
);
435 void RenderWidgetHostViewChildFrame::ClearCompositorSurfaceIfNecessary() {
436 if (surface_factory_
&& !surface_id_
.is_null())
437 surface_factory_
->Destroy(surface_id_
);
438 surface_id_
= cc::SurfaceId();
441 } // namespace content