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_impl.h"
18 #include "content/common/gpu/gpu_messages.h"
19 #include "content/common/view_messages.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/common/browser_plugin_guest_mode.h"
25 RenderWidgetHostViewChildFrame::RenderWidgetHostViewChildFrame(
26 RenderWidgetHost
* widget_host
)
27 : host_(RenderWidgetHostImpl::From(widget_host
)),
28 use_surfaces_(UseSurfacesEnabled()),
29 next_surface_sequence_(1u),
30 last_output_surface_id_(0),
31 current_surface_scale_factor_(1.f
),
32 ack_pending_count_(0),
33 frame_connector_(nullptr),
36 id_allocator_
= CreateSurfaceIdAllocator();
41 RenderWidgetHostViewChildFrame::~RenderWidgetHostViewChildFrame() {
42 if (!surface_id_
.is_null())
43 surface_factory_
->Destroy(surface_id_
);
46 void RenderWidgetHostViewChildFrame::InitAsChild(
47 gfx::NativeView parent_view
) {
51 RenderWidgetHost
* RenderWidgetHostViewChildFrame::GetRenderWidgetHost() const {
55 void RenderWidgetHostViewChildFrame::SetSize(const gfx::Size
& size
) {
59 void RenderWidgetHostViewChildFrame::SetBounds(const gfx::Rect
& rect
) {
63 void RenderWidgetHostViewChildFrame::Focus() {
66 bool RenderWidgetHostViewChildFrame::HasFocus() const {
70 bool RenderWidgetHostViewChildFrame::IsSurfaceAvailableForCopy() const {
75 void RenderWidgetHostViewChildFrame::Show() {
76 if (!host_
->is_hidden())
78 host_
->WasShown(ui::LatencyInfo());
81 void RenderWidgetHostViewChildFrame::Hide() {
82 if (host_
->is_hidden())
87 bool RenderWidgetHostViewChildFrame::IsShowing() {
88 return !host_
->is_hidden();
91 gfx::Rect
RenderWidgetHostViewChildFrame::GetViewBounds() const {
94 rect
= frame_connector_
->ChildFrameRect();
98 gfx::Vector2dF
RenderWidgetHostViewChildFrame::GetLastScrollOffset() const {
99 return last_scroll_offset_
;
102 gfx::NativeView
RenderWidgetHostViewChildFrame::GetNativeView() const {
107 gfx::NativeViewId
RenderWidgetHostViewChildFrame::GetNativeViewId() const {
112 gfx::NativeViewAccessible
113 RenderWidgetHostViewChildFrame::GetNativeViewAccessible() {
118 void RenderWidgetHostViewChildFrame::SetBackgroundColor(SkColor color
) {
121 gfx::Size
RenderWidgetHostViewChildFrame::GetPhysicalBackingSize() const {
123 if (frame_connector_
)
124 size
= frame_connector_
->ChildFrameRect().size();
128 void RenderWidgetHostViewChildFrame::InitAsPopup(
129 RenderWidgetHostView
* parent_host_view
,
130 const gfx::Rect
& bounds
) {
134 void RenderWidgetHostViewChildFrame::InitAsFullscreen(
135 RenderWidgetHostView
* reference_host_view
) {
139 void RenderWidgetHostViewChildFrame::ImeCancelComposition() {
143 void RenderWidgetHostViewChildFrame::ImeCompositionRangeChanged(
144 const gfx::Range
& range
,
145 const std::vector
<gfx::Rect
>& character_bounds
) {
149 void RenderWidgetHostViewChildFrame::MovePluginWindows(
150 const std::vector
<WebPluginGeometry
>& moves
) {
153 void RenderWidgetHostViewChildFrame::UpdateCursor(const WebCursor
& cursor
) {
156 void RenderWidgetHostViewChildFrame::SetIsLoading(bool is_loading
) {
157 // It is valid for an inner WebContents's SetIsLoading() to end up here.
158 // This is because an inner WebContents's main frame's RenderWidgetHostView
159 // is a RenderWidgetHostViewChildFrame. In contrast, when there is no
160 // inner/outer WebContents, only subframe's RenderWidgetHostView can be a
161 // RenderWidgetHostViewChildFrame which do not get a SetIsLoading() call.
162 if (BrowserPluginGuestMode::UseCrossProcessFramesForGuests() &&
163 BrowserPluginGuest::IsGuest(
164 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(host_
)))) {
171 void RenderWidgetHostViewChildFrame::TextInputStateChanged(
172 const ViewHostMsg_TextInputState_Params
& params
) {
173 // TODO(kenrb): Implement.
176 void RenderWidgetHostViewChildFrame::RenderProcessGone(
177 base::TerminationStatus status
,
179 if (frame_connector_
)
180 frame_connector_
->RenderProcessGone();
184 void RenderWidgetHostViewChildFrame::Destroy() {
185 if (frame_connector_
) {
186 frame_connector_
->set_view(NULL
);
187 frame_connector_
= NULL
;
190 host_
->SetView(NULL
);
192 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
195 void RenderWidgetHostViewChildFrame::SetTooltipText(
196 const base::string16
& tooltip_text
) {
199 void RenderWidgetHostViewChildFrame::SelectionChanged(
200 const base::string16
& text
,
202 const gfx::Range
& range
) {
205 void RenderWidgetHostViewChildFrame::SelectionBoundsChanged(
206 const ViewHostMsg_SelectionBounds_Params
& params
) {
209 #if defined(OS_ANDROID)
210 void RenderWidgetHostViewChildFrame::LockCompositingSurface() {
213 void RenderWidgetHostViewChildFrame::UnlockCompositingSurface() {
217 void RenderWidgetHostViewChildFrame::SurfaceDrawn(uint32 output_surface_id
,
218 cc::SurfaceDrawStatus drawn
) {
219 cc::CompositorFrameAck ack
;
220 DCHECK(ack_pending_count_
> 0);
221 if (!surface_returned_resources_
.empty())
222 ack
.resources
.swap(surface_returned_resources_
);
224 host_
->Send(new ViewMsg_SwapCompositorFrameAck(host_
->GetRoutingID(),
225 output_surface_id
, ack
));
227 ack_pending_count_
--;
230 void RenderWidgetHostViewChildFrame::OnSwapCompositorFrame(
231 uint32 output_surface_id
,
232 scoped_ptr
<cc::CompositorFrame
> frame
) {
233 last_scroll_offset_
= frame
->metadata
.root_scroll_offset
;
235 if (!frame_connector_
)
238 // When not using surfaces, the frame just gets proxied to
239 // the embedder's renderer to be composited.
240 if (!frame
->delegated_frame_data
|| !use_surfaces_
) {
241 frame_connector_
->ChildFrameCompositorFrameSwapped(
243 host_
->GetProcess()->GetID(),
244 host_
->GetRoutingID(),
249 cc::RenderPass
* root_pass
=
250 frame
->delegated_frame_data
->render_pass_list
.back();
252 gfx::Size frame_size
= root_pass
->output_rect
.size();
253 float scale_factor
= frame
->metadata
.device_scale_factor
;
255 // Check whether we need to recreate the cc::Surface, which means the child
256 // frame renderer has changed its output surface, or size, or scale factor.
257 if (output_surface_id
!= last_output_surface_id_
&& surface_factory_
) {
258 surface_factory_
->Destroy(surface_id_
);
259 surface_factory_
.reset();
261 if (output_surface_id
!= last_output_surface_id_
||
262 frame_size
!= current_surface_size_
||
263 scale_factor
!= current_surface_scale_factor_
) {
264 ClearCompositorSurfaceIfNecessary();
265 last_output_surface_id_
= output_surface_id
;
266 current_surface_size_
= frame_size
;
267 current_surface_scale_factor_
= scale_factor
;
270 if (!surface_factory_
) {
271 cc::SurfaceManager
* manager
= GetSurfaceManager();
272 surface_factory_
= make_scoped_ptr(new cc::SurfaceFactory(manager
, this));
275 if (surface_id_
.is_null()) {
276 surface_id_
= id_allocator_
->GenerateId();
277 surface_factory_
->Create(surface_id_
);
279 cc::SurfaceSequence sequence
= cc::SurfaceSequence(
280 id_allocator_
->id_namespace(), next_surface_sequence_
++);
281 // The renderer process will satisfy this dependency when it creates a
283 cc::SurfaceManager
* manager
= GetSurfaceManager();
284 manager
->GetSurfaceForId(surface_id_
)->AddDestructionDependency(sequence
);
285 frame_connector_
->SetChildFrameSurface(surface_id_
, frame_size
,
286 scale_factor
, sequence
);
289 cc::SurfaceFactory::DrawCallback ack_callback
=
290 base::Bind(&RenderWidgetHostViewChildFrame::SurfaceDrawn
, AsWeakPtr(),
292 ack_pending_count_
++;
293 // If this value grows very large, something is going wrong.
294 DCHECK(ack_pending_count_
< 1000);
295 surface_factory_
->SubmitFrame(surface_id_
, frame
.Pass(), ack_callback
);
298 void RenderWidgetHostViewChildFrame::GetScreenInfo(
299 blink::WebScreenInfo
* results
) {
300 if (!frame_connector_
)
302 frame_connector_
->GetScreenInfo(results
);
305 gfx::Rect
RenderWidgetHostViewChildFrame::GetBoundsInRootWindow() {
306 // We do not have any root window specific parts in this view.
307 return GetViewBounds();
310 #if defined(USE_AURA)
311 void RenderWidgetHostViewChildFrame::ProcessAckedTouchEvent(
312 const TouchEventWithLatencyInfo
& touch
,
313 InputEventAckState ack_result
) {
315 #endif // defined(USE_AURA)
317 bool RenderWidgetHostViewChildFrame::LockMouse() {
321 void RenderWidgetHostViewChildFrame::UnlockMouse() {
324 uint32_t RenderWidgetHostViewChildFrame::GetSurfaceIdNamespace() {
328 return id_allocator_
->id_namespace();
331 #if defined(OS_MACOSX)
332 void RenderWidgetHostViewChildFrame::SetActive(bool active
) {
335 void RenderWidgetHostViewChildFrame::SetWindowVisibility(bool visible
) {
338 void RenderWidgetHostViewChildFrame::WindowFrameChanged() {
341 void RenderWidgetHostViewChildFrame::ShowDefinitionForSelection() {
344 bool RenderWidgetHostViewChildFrame::SupportsSpeech() const {
348 void RenderWidgetHostViewChildFrame::SpeakSelection() {
351 bool RenderWidgetHostViewChildFrame::IsSpeaking() const {
355 void RenderWidgetHostViewChildFrame::StopSpeaking() {
358 bool RenderWidgetHostViewChildFrame::PostProcessEventForPluginIme(
359 const NativeWebKeyboardEvent
& event
) {
362 #endif // defined(OS_MACOSX)
364 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurface(
365 const gfx::Rect
& src_subrect
,
366 const gfx::Size
& /* dst_size */,
367 ReadbackRequestCallback
& callback
,
368 const SkColorType preferred_color_type
) {
369 callback
.Run(SkBitmap(), READBACK_FAILED
);
372 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurfaceToVideoFrame(
373 const gfx::Rect
& src_subrect
,
374 const scoped_refptr
<media::VideoFrame
>& target
,
375 const base::Callback
<void(bool)>& callback
) {
380 bool RenderWidgetHostViewChildFrame::CanCopyToVideoFrame() const {
384 bool RenderWidgetHostViewChildFrame::HasAcceleratedSurface(
385 const gfx::Size
& desired_size
) {
389 gfx::GLSurfaceHandle
RenderWidgetHostViewChildFrame::GetCompositingSurface() {
390 return gfx::GLSurfaceHandle(gfx::kNullPluginWindow
, gfx::NULL_TRANSPORT
);
394 void RenderWidgetHostViewChildFrame::SetParentNativeViewAccessible(
395 gfx::NativeViewAccessible accessible_parent
) {
398 gfx::NativeViewId
RenderWidgetHostViewChildFrame::GetParentForWindowlessPlugin()
402 #endif // defined(OS_WIN)
404 // cc::SurfaceFactoryClient implementation.
405 void RenderWidgetHostViewChildFrame::ReturnResources(
406 const cc::ReturnedResourceArray
& resources
) {
407 if (resources
.empty())
410 if (!ack_pending_count_
&& host_
) {
411 cc::CompositorFrameAck ack
;
412 std::copy(resources
.begin(), resources
.end(),
413 std::back_inserter(ack
.resources
));
414 host_
->Send(new ViewMsg_ReclaimCompositorResources(
415 host_
->GetRoutingID(), last_output_surface_id_
, ack
));
419 std::copy(resources
.begin(), resources
.end(),
420 std::back_inserter(surface_returned_resources_
));
423 BrowserAccessibilityManager
*
424 RenderWidgetHostViewChildFrame::CreateBrowserAccessibilityManager(
425 BrowserAccessibilityDelegate
* delegate
) {
426 return BrowserAccessibilityManager::Create(
427 BrowserAccessibilityManager::GetEmptyDocument(), delegate
);
430 void RenderWidgetHostViewChildFrame::ClearCompositorSurfaceIfNecessary() {
431 if (surface_factory_
&& !surface_id_
.is_null())
432 surface_factory_
->Destroy(surface_id_
);
433 surface_id_
= cc::SurfaceId();
436 } // namespace content