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 "cc/surfaces/surface.h"
8 #include "cc/surfaces/surface_factory.h"
9 #include "cc/surfaces/surface_manager.h"
10 #include "cc/surfaces/surface_sequence.h"
11 #include "content/browser/accessibility/browser_accessibility_manager.h"
12 #include "content/browser/browser_plugin/browser_plugin_guest.h"
13 #include "content/browser/compositor/surface_utils.h"
14 #include "content/browser/frame_host/cross_process_frame_connector.h"
15 #include "content/browser/gpu/compositor_util.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/renderer_host/render_widget_host_delegate.h"
18 #include "content/browser/renderer_host/render_widget_host_impl.h"
19 #include "content/browser/renderer_host/render_widget_host_input_event_router.h"
20 #include "content/common/gpu/gpu_messages.h"
21 #include "content/common/view_messages.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/common/browser_plugin_guest_mode.h"
24 #include "ui/gfx/geometry/size_conversions.h"
25 #include "ui/gfx/geometry/size_f.h"
29 RenderWidgetHostViewChildFrame::RenderWidgetHostViewChildFrame(
30 RenderWidgetHost
* widget_host
)
31 : host_(RenderWidgetHostImpl::From(widget_host
)),
32 use_surfaces_(UseSurfacesEnabled()),
33 next_surface_sequence_(1u),
34 last_output_surface_id_(0),
35 current_surface_scale_factor_(1.f
),
36 ack_pending_count_(0),
37 frame_connector_(nullptr),
40 id_allocator_
= CreateSurfaceIdAllocator();
41 if (host_
->delegate() && host_
->delegate()->GetInputEventRouter()) {
42 host_
->delegate()->GetInputEventRouter()->AddSurfaceIdNamespaceOwner(
43 GetSurfaceIdNamespace(), this);
50 RenderWidgetHostViewChildFrame::~RenderWidgetHostViewChildFrame() {
51 if (!surface_id_
.is_null())
52 surface_factory_
->Destroy(surface_id_
);
55 void RenderWidgetHostViewChildFrame::InitAsChild(
56 gfx::NativeView parent_view
) {
60 RenderWidgetHost
* RenderWidgetHostViewChildFrame::GetRenderWidgetHost() const {
64 void RenderWidgetHostViewChildFrame::SetSize(const gfx::Size
& size
) {
68 void RenderWidgetHostViewChildFrame::SetBounds(const gfx::Rect
& rect
) {
72 void RenderWidgetHostViewChildFrame::Focus() {
75 bool RenderWidgetHostViewChildFrame::HasFocus() const {
79 bool RenderWidgetHostViewChildFrame::IsSurfaceAvailableForCopy() const {
84 void RenderWidgetHostViewChildFrame::Show() {
85 if (!host_
->is_hidden())
87 host_
->WasShown(ui::LatencyInfo());
90 void RenderWidgetHostViewChildFrame::Hide() {
91 if (host_
->is_hidden())
96 bool RenderWidgetHostViewChildFrame::IsShowing() {
97 return !host_
->is_hidden();
100 gfx::Rect
RenderWidgetHostViewChildFrame::GetViewBounds() const {
102 if (frame_connector_
)
103 rect
= frame_connector_
->ChildFrameRect();
107 gfx::Vector2dF
RenderWidgetHostViewChildFrame::GetLastScrollOffset() const {
108 return last_scroll_offset_
;
111 gfx::NativeView
RenderWidgetHostViewChildFrame::GetNativeView() const {
116 gfx::NativeViewId
RenderWidgetHostViewChildFrame::GetNativeViewId() const {
121 gfx::NativeViewAccessible
122 RenderWidgetHostViewChildFrame::GetNativeViewAccessible() {
127 void RenderWidgetHostViewChildFrame::SetBackgroundColor(SkColor color
) {
130 gfx::Size
RenderWidgetHostViewChildFrame::GetPhysicalBackingSize() const {
132 if (frame_connector_
) {
133 size
= gfx::ToCeiledSize(
134 gfx::ScaleSize(frame_connector_
->ChildFrameRect().size(),
135 frame_connector_
->device_scale_factor()));
140 void RenderWidgetHostViewChildFrame::InitAsPopup(
141 RenderWidgetHostView
* parent_host_view
,
142 const gfx::Rect
& bounds
) {
146 void RenderWidgetHostViewChildFrame::InitAsFullscreen(
147 RenderWidgetHostView
* reference_host_view
) {
151 void RenderWidgetHostViewChildFrame::ImeCancelComposition() {
152 // TODO(kenrb): Fix OOPIF Ime.
155 void RenderWidgetHostViewChildFrame::ImeCompositionRangeChanged(
156 const gfx::Range
& range
,
157 const std::vector
<gfx::Rect
>& character_bounds
) {
158 // TODO(kenrb): Fix OOPIF Ime.
161 void RenderWidgetHostViewChildFrame::MovePluginWindows(
162 const std::vector
<WebPluginGeometry
>& moves
) {
165 void RenderWidgetHostViewChildFrame::UpdateCursor(const WebCursor
& cursor
) {
168 void RenderWidgetHostViewChildFrame::SetIsLoading(bool is_loading
) {
169 // It is valid for an inner WebContents's SetIsLoading() to end up here.
170 // This is because an inner WebContents's main frame's RenderWidgetHostView
171 // is a RenderWidgetHostViewChildFrame. In contrast, when there is no
172 // inner/outer WebContents, only subframe's RenderWidgetHostView can be a
173 // RenderWidgetHostViewChildFrame which do not get a SetIsLoading() call.
174 if (BrowserPluginGuestMode::UseCrossProcessFramesForGuests() &&
175 BrowserPluginGuest::IsGuest(
176 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(host_
)))) {
183 void RenderWidgetHostViewChildFrame::TextInputStateChanged(
184 const ViewHostMsg_TextInputState_Params
& params
) {
185 // TODO(kenrb): Implement.
188 void RenderWidgetHostViewChildFrame::RenderProcessGone(
189 base::TerminationStatus status
,
191 if (frame_connector_
)
192 frame_connector_
->RenderProcessGone();
196 void RenderWidgetHostViewChildFrame::Destroy() {
197 if (frame_connector_
) {
198 frame_connector_
->set_view(NULL
);
199 frame_connector_
= NULL
;
202 if (use_surfaces_
&& host_
->delegate() &&
203 host_
->delegate()->GetInputEventRouter()) {
204 host_
->delegate()->GetInputEventRouter()->RemoveSurfaceIdNamespaceOwner(
205 GetSurfaceIdNamespace());
208 host_
->SetView(NULL
);
210 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
213 void RenderWidgetHostViewChildFrame::SetTooltipText(
214 const base::string16
& tooltip_text
) {
217 void RenderWidgetHostViewChildFrame::SelectionChanged(
218 const base::string16
& text
,
220 const gfx::Range
& range
) {
223 void RenderWidgetHostViewChildFrame::SelectionBoundsChanged(
224 const ViewHostMsg_SelectionBounds_Params
& params
) {
227 #if defined(OS_ANDROID)
228 void RenderWidgetHostViewChildFrame::LockCompositingSurface() {
231 void RenderWidgetHostViewChildFrame::UnlockCompositingSurface() {
235 void RenderWidgetHostViewChildFrame::SurfaceDrawn(uint32 output_surface_id
,
236 cc::SurfaceDrawStatus drawn
) {
237 cc::CompositorFrameAck ack
;
238 DCHECK(ack_pending_count_
> 0);
239 if (!surface_returned_resources_
.empty())
240 ack
.resources
.swap(surface_returned_resources_
);
242 host_
->Send(new ViewMsg_SwapCompositorFrameAck(host_
->GetRoutingID(),
243 output_surface_id
, ack
));
245 ack_pending_count_
--;
248 void RenderWidgetHostViewChildFrame::OnSwapCompositorFrame(
249 uint32 output_surface_id
,
250 scoped_ptr
<cc::CompositorFrame
> frame
) {
251 last_scroll_offset_
= frame
->metadata
.root_scroll_offset
;
253 if (!frame_connector_
)
256 // When not using surfaces, the frame just gets proxied to
257 // the embedder's renderer to be composited.
258 if (!frame
->delegated_frame_data
|| !use_surfaces_
) {
259 frame_connector_
->ChildFrameCompositorFrameSwapped(
261 host_
->GetProcess()->GetID(),
262 host_
->GetRoutingID(),
267 cc::RenderPass
* root_pass
=
268 frame
->delegated_frame_data
->render_pass_list
.back();
270 gfx::Size frame_size
= root_pass
->output_rect
.size();
271 float scale_factor
= frame
->metadata
.device_scale_factor
;
273 // Check whether we need to recreate the cc::Surface, which means the child
274 // frame renderer has changed its output surface, or size, or scale factor.
275 if (output_surface_id
!= last_output_surface_id_
&& surface_factory_
) {
276 surface_factory_
->Destroy(surface_id_
);
277 surface_factory_
.reset();
279 if (output_surface_id
!= last_output_surface_id_
||
280 frame_size
!= current_surface_size_
||
281 scale_factor
!= current_surface_scale_factor_
) {
282 ClearCompositorSurfaceIfNecessary();
283 last_output_surface_id_
= output_surface_id
;
284 current_surface_size_
= frame_size
;
285 current_surface_scale_factor_
= scale_factor
;
288 if (!surface_factory_
) {
289 cc::SurfaceManager
* manager
= GetSurfaceManager();
290 surface_factory_
= make_scoped_ptr(new cc::SurfaceFactory(manager
, this));
293 if (surface_id_
.is_null()) {
294 surface_id_
= id_allocator_
->GenerateId();
295 surface_factory_
->Create(surface_id_
);
297 cc::SurfaceSequence sequence
= cc::SurfaceSequence(
298 id_allocator_
->id_namespace(), next_surface_sequence_
++);
299 // The renderer process will satisfy this dependency when it creates a
301 cc::SurfaceManager
* manager
= GetSurfaceManager();
302 manager
->GetSurfaceForId(surface_id_
)->AddDestructionDependency(sequence
);
303 frame_connector_
->SetChildFrameSurface(surface_id_
, frame_size
,
304 scale_factor
, sequence
);
307 cc::SurfaceFactory::DrawCallback ack_callback
=
308 base::Bind(&RenderWidgetHostViewChildFrame::SurfaceDrawn
, AsWeakPtr(),
310 ack_pending_count_
++;
311 // If this value grows very large, something is going wrong.
312 DCHECK(ack_pending_count_
< 1000);
313 surface_factory_
->SubmitCompositorFrame(surface_id_
, frame
.Pass(),
317 void RenderWidgetHostViewChildFrame::GetScreenInfo(
318 blink::WebScreenInfo
* results
) {
319 if (!frame_connector_
)
321 frame_connector_
->GetScreenInfo(results
);
324 bool RenderWidgetHostViewChildFrame::GetScreenColorProfile(
325 std::vector
<char>* color_profile
) {
326 if (!frame_connector_
)
328 DCHECK(color_profile
->empty());
329 NOTIMPLEMENTED(); // TODO(port): Implement.
333 gfx::Rect
RenderWidgetHostViewChildFrame::GetBoundsInRootWindow() {
334 // We do not have any root window specific parts in this view.
335 return GetViewBounds();
338 #if defined(USE_AURA)
339 void RenderWidgetHostViewChildFrame::ProcessAckedTouchEvent(
340 const TouchEventWithLatencyInfo
& touch
,
341 InputEventAckState ack_result
) {
343 #endif // defined(USE_AURA)
345 bool RenderWidgetHostViewChildFrame::LockMouse() {
349 void RenderWidgetHostViewChildFrame::UnlockMouse() {
352 uint32_t RenderWidgetHostViewChildFrame::GetSurfaceIdNamespace() {
356 return id_allocator_
->id_namespace();
359 void RenderWidgetHostViewChildFrame::ProcessKeyboardEvent(
360 const NativeWebKeyboardEvent
& event
) {
361 host_
->ForwardKeyboardEvent(event
);
364 void RenderWidgetHostViewChildFrame::ProcessMouseEvent(
365 const blink::WebMouseEvent
& event
) {
366 host_
->ForwardMouseEvent(event
);
369 void RenderWidgetHostViewChildFrame::ProcessMouseWheelEvent(
370 const blink::WebMouseWheelEvent
& event
) {
371 if (event
.deltaX
!= 0 || event
.deltaY
!= 0)
372 host_
->ForwardWheelEvent(event
);
375 #if defined(OS_MACOSX)
376 void RenderWidgetHostViewChildFrame::SetActive(bool active
) {
379 void RenderWidgetHostViewChildFrame::SetWindowVisibility(bool visible
) {
382 void RenderWidgetHostViewChildFrame::WindowFrameChanged() {
385 void RenderWidgetHostViewChildFrame::ShowDefinitionForSelection() {
388 bool RenderWidgetHostViewChildFrame::SupportsSpeech() const {
392 void RenderWidgetHostViewChildFrame::SpeakSelection() {
395 bool RenderWidgetHostViewChildFrame::IsSpeaking() const {
399 void RenderWidgetHostViewChildFrame::StopSpeaking() {
402 bool RenderWidgetHostViewChildFrame::PostProcessEventForPluginIme(
403 const NativeWebKeyboardEvent
& event
) {
406 #endif // defined(OS_MACOSX)
408 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurface(
409 const gfx::Rect
& src_subrect
,
410 const gfx::Size
& /* dst_size */,
411 ReadbackRequestCallback
& callback
,
412 const SkColorType preferred_color_type
) {
413 callback
.Run(SkBitmap(), READBACK_FAILED
);
416 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurfaceToVideoFrame(
417 const gfx::Rect
& src_subrect
,
418 const scoped_refptr
<media::VideoFrame
>& target
,
419 const base::Callback
<void(bool)>& callback
) {
424 bool RenderWidgetHostViewChildFrame::CanCopyToVideoFrame() const {
428 bool RenderWidgetHostViewChildFrame::HasAcceleratedSurface(
429 const gfx::Size
& desired_size
) {
433 gfx::GLSurfaceHandle
RenderWidgetHostViewChildFrame::GetCompositingSurface() {
434 return gfx::GLSurfaceHandle(gfx::kNullPluginWindow
, gfx::NULL_TRANSPORT
);
438 void RenderWidgetHostViewChildFrame::SetParentNativeViewAccessible(
439 gfx::NativeViewAccessible accessible_parent
) {
442 gfx::NativeViewId
RenderWidgetHostViewChildFrame::GetParentForWindowlessPlugin()
446 #endif // defined(OS_WIN)
448 // cc::SurfaceFactoryClient implementation.
449 void RenderWidgetHostViewChildFrame::ReturnResources(
450 const cc::ReturnedResourceArray
& resources
) {
451 if (resources
.empty())
454 if (!ack_pending_count_
&& host_
) {
455 cc::CompositorFrameAck ack
;
456 std::copy(resources
.begin(), resources
.end(),
457 std::back_inserter(ack
.resources
));
458 host_
->Send(new ViewMsg_ReclaimCompositorResources(
459 host_
->GetRoutingID(), last_output_surface_id_
, ack
));
463 std::copy(resources
.begin(), resources
.end(),
464 std::back_inserter(surface_returned_resources_
));
467 BrowserAccessibilityManager
*
468 RenderWidgetHostViewChildFrame::CreateBrowserAccessibilityManager(
469 BrowserAccessibilityDelegate
* delegate
) {
470 return BrowserAccessibilityManager::Create(
471 BrowserAccessibilityManager::GetEmptyDocument(), delegate
);
474 void RenderWidgetHostViewChildFrame::ClearCompositorSurfaceIfNecessary() {
475 if (surface_factory_
&& !surface_id_
.is_null())
476 surface_factory_
->Destroy(surface_id_
);
477 surface_id_
= cc::SurfaceId();
480 } // namespace content