1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/i18n/rtl.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/process/process.h"
17 #include "cc/layers/delegated_frame_resource_collection.h"
18 #include "cc/output/begin_frame_args.h"
19 #include "content/browser/accessibility/browser_accessibility_manager.h"
20 #include "content/browser/renderer_host/delegated_frame_evictor.h"
21 #include "content/browser/renderer_host/image_transport_factory_android.h"
22 #include "content/browser/renderer_host/ime_adapter_android.h"
23 #include "content/browser/renderer_host/render_widget_host_view_base.h"
24 #include "content/common/content_export.h"
25 #include "gpu/command_buffer/common/mailbox.h"
26 #include "third_party/skia/include/core/SkColor.h"
27 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
28 #include "ui/base/android/window_android_observer.h"
29 #include "ui/events/gesture_detection/filtered_gesture_provider.h"
30 #include "ui/gfx/size.h"
31 #include "ui/gfx/vector2d_f.h"
33 struct ViewHostMsg_TextInputState_Params
;
35 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
;
36 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
;
39 class CopyOutputResult
;
40 class DelegatedFrameProvider
;
41 class DelegatedRendererLayer
;
46 class WebExternalTextureLayer
;
52 class ContentViewCoreImpl
;
54 class RenderWidgetHost
;
55 class RenderWidgetHostImpl
;
56 struct DidOverscrollParams
;
57 struct NativeWebKeyboardEvent
;
59 // -----------------------------------------------------------------------------
60 // See comments in render_widget_host_view.h about this class and its members.
61 // -----------------------------------------------------------------------------
62 class CONTENT_EXPORT RenderWidgetHostViewAndroid
63 : public RenderWidgetHostViewBase
,
64 public cc::DelegatedFrameResourceCollectionClient
,
65 public ImageTransportFactoryAndroidObserver
,
66 public ui::GestureProviderClient
,
67 public ui::WindowAndroidObserver
,
68 public DelegatedFrameEvictorClient
{
70 RenderWidgetHostViewAndroid(RenderWidgetHostImpl
* widget
,
71 ContentViewCoreImpl
* content_view_core
);
72 virtual ~RenderWidgetHostViewAndroid();
74 // RenderWidgetHostView implementation.
75 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
76 virtual void InitAsChild(gfx::NativeView parent_view
) OVERRIDE
;
77 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
78 const gfx::Rect
& pos
) OVERRIDE
;
79 virtual void InitAsFullscreen(
80 RenderWidgetHostView
* reference_host_view
) OVERRIDE
;
81 virtual RenderWidgetHost
* GetRenderWidgetHost() const OVERRIDE
;
82 virtual void WasShown() OVERRIDE
;
83 virtual void WasHidden() OVERRIDE
;
84 virtual void SetSize(const gfx::Size
& size
) OVERRIDE
;
85 virtual void SetBounds(const gfx::Rect
& rect
) OVERRIDE
;
86 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
87 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
88 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
89 virtual void MovePluginWindows(
90 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
;
91 virtual void Focus() OVERRIDE
;
92 virtual void Blur() OVERRIDE
;
93 virtual bool HasFocus() const OVERRIDE
;
94 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
95 virtual void Show() OVERRIDE
;
96 virtual void Hide() OVERRIDE
;
97 virtual bool IsShowing() OVERRIDE
;
98 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
99 virtual gfx::Size
GetPhysicalBackingSize() const OVERRIDE
;
100 virtual float GetOverdrawBottomHeight() const OVERRIDE
;
101 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
;
102 virtual void SetIsLoading(bool is_loading
) OVERRIDE
;
103 virtual void TextInputTypeChanged(ui::TextInputType type
,
104 ui::TextInputMode input_mode
,
105 bool can_compose_inline
) OVERRIDE
;
106 virtual void ImeCancelComposition() OVERRIDE
;
107 virtual void FocusedNodeChanged(bool is_editable_node
) OVERRIDE
;
108 virtual void RenderProcessGone(base::TerminationStatus status
,
109 int error_code
) OVERRIDE
;
110 virtual void Destroy() OVERRIDE
;
111 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
;
112 virtual void SelectionChanged(const base::string16
& text
,
114 const gfx::Range
& range
) OVERRIDE
;
115 virtual void SelectionBoundsChanged(
116 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
;
117 virtual void SelectionRootBoundsChanged(const gfx::Rect
& bounds
) OVERRIDE
;
118 virtual void ScrollOffsetChanged() OVERRIDE
;
119 virtual void OnAcceleratedCompositingStateChange() OVERRIDE
;
120 virtual void AcceleratedSurfaceInitialized(int host_id
,
121 int route_id
) OVERRIDE
;
122 virtual void AcceleratedSurfaceBuffersSwapped(
123 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
124 int gpu_host_id
) OVERRIDE
;
125 virtual void AcceleratedSurfacePostSubBuffer(
126 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
127 int gpu_host_id
) OVERRIDE
;
128 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
129 virtual void AcceleratedSurfaceRelease() OVERRIDE
;
130 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
131 virtual void SetBackground(const SkBitmap
& background
) OVERRIDE
;
132 virtual void CopyFromCompositingSurface(
133 const gfx::Rect
& src_subrect
,
134 const gfx::Size
& dst_size
,
135 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
136 const SkBitmap::Config config
) OVERRIDE
;
137 virtual void CopyFromCompositingSurfaceToVideoFrame(
138 const gfx::Rect
& src_subrect
,
139 const scoped_refptr
<media::VideoFrame
>& target
,
140 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
141 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
142 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
;
143 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
144 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
145 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
146 InputEventAckState ack_result
) OVERRIDE
;
147 virtual void SetScrollOffsetPinning(
148 bool is_pinned_to_left
, bool is_pinned_to_right
) OVERRIDE
;
149 virtual void UnhandledWheelEvent(
150 const blink::WebMouseWheelEvent
& event
) OVERRIDE
;
151 virtual InputEventAckState
FilterInputEvent(
152 const blink::WebInputEvent
& input_event
) OVERRIDE
;
153 virtual void OnSetNeedsFlushInput() OVERRIDE
;
154 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
155 InputEventAckState ack_result
) OVERRIDE
;
156 virtual void CreateBrowserAccessibilityManagerIfNeeded() OVERRIDE
;
157 virtual bool LockMouse() OVERRIDE
;
158 virtual void UnlockMouse() OVERRIDE
;
159 virtual void OnSwapCompositorFrame(
160 uint32 output_surface_id
,
161 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
162 virtual void DidStopFlinging() OVERRIDE
;
163 virtual void ShowDisambiguationPopup(const gfx::Rect
& target_rect
,
164 const SkBitmap
& zoomed_bitmap
) OVERRIDE
;
165 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget()
167 virtual void LockCompositingSurface() OVERRIDE
;
168 virtual void UnlockCompositingSurface() OVERRIDE
;
170 // cc::DelegatedFrameResourceCollectionClient implementation.
171 virtual void UnusedResourcesAreAvailable() OVERRIDE
;
173 // ui::GestureProviderClient implementation.
174 virtual void OnGestureEvent(const ui::GestureEventData
& gesture
) OVERRIDE
;
176 // ui::WindowAndroidObserver implementation.
177 virtual void OnCompositingDidCommit() OVERRIDE
;
178 virtual void OnAttachCompositor() OVERRIDE
{}
179 virtual void OnDetachCompositor() OVERRIDE
;
180 virtual void OnWillDestroyWindow() OVERRIDE
;
181 virtual void OnVSync(base::TimeTicks frame_time
,
182 base::TimeDelta vsync_period
) OVERRIDE
;
184 // ImageTransportFactoryAndroidObserver implementation.
185 virtual void OnLostResources() OVERRIDE
;
187 // DelegatedFrameEvictor implementation
188 virtual void EvictDelegatedFrame() OVERRIDE
;
190 virtual SkBitmap::Config
PreferredReadbackFormat() OVERRIDE
;
192 // Non-virtual methods
193 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
194 SkColor
GetCachedBackgroundColor() const;
195 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
196 void SendTouchEvent(const blink::WebTouchEvent
& event
);
197 void SendMouseEvent(const blink::WebMouseEvent
& event
);
198 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
199 void SendGestureEvent(const blink::WebGestureEvent
& event
);
201 void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params
& params
);
202 void OnDidChangeBodyBackgroundColor(SkColor color
);
203 void OnDidOverscroll(const DidOverscrollParams
& params
);
204 void OnStartContentIntent(const GURL
& content_url
);
205 void OnSetNeedsBeginFrame(bool enabled
);
206 void OnSmartClipDataExtracted(const base::string16
& result
);
208 bool OnTouchEvent(const ui::MotionEvent
& event
);
209 void ResetGestureDetection();
210 void SetDoubleTapSupportEnabled(bool enabled
);
211 void SetMultiTouchZoomSupportEnabled(bool enabled
);
213 long GetNativeImeAdapter();
217 void GetScaledContentBitmap(
219 SkBitmap::Config bitmap_config
,
220 gfx::Rect src_subrect
,
221 const base::Callback
<void(bool, const SkBitmap
&)>& result_callback
);
223 bool HasValidFrame() const;
225 void MoveCaret(const gfx::Point
& point
);
227 void SynchronousFrameMetadata(
228 const cc::CompositorFrameMetadata
& frame_metadata
);
230 void SetOverlayVideoMode(bool enabled
);
233 void RunAckCallbacks();
235 void DestroyDelegatedContent();
236 void SwapDelegatedFrame(uint32 output_surface_id
,
237 scoped_ptr
<cc::DelegatedFrameData
> frame_data
);
238 void SendDelegatedFrameAck(uint32 output_surface_id
);
239 void SendReturnedDelegatedResources(uint32 output_surface_id
);
241 void OnFrameMetadataUpdated(
242 const cc::CompositorFrameMetadata
& frame_metadata
);
243 void ComputeContentsSize(const cc::CompositorFrameMetadata
& frame_metadata
);
244 void ResetClipping();
245 void ClipContents(const gfx::Rect
& clipping
, const gfx::Size
& content_size
);
250 // Called after async screenshot task completes. Scales and crops the result
252 static void PrepareTextureCopyOutputResult(
253 const gfx::Size
& dst_size_in_pixel
,
254 const SkBitmap::Config config
,
255 const base::TimeTicks
& start_time
,
256 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
257 scoped_ptr
<cc::CopyOutputResult
> result
);
258 static void PrepareTextureCopyOutputResultForDelegatedReadback(
259 const gfx::Size
& dst_size_in_pixel
,
260 const SkBitmap::Config config
,
261 const base::TimeTicks
& start_time
,
262 scoped_refptr
<cc::Layer
> readback_layer
,
263 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
264 scoped_ptr
<cc::CopyOutputResult
> result
);
266 // DevTools ScreenCast support for Android WebView.
267 void SynchronousCopyContents(
268 const gfx::Rect
& src_subrect_in_pixel
,
269 const gfx::Size
& dst_size_in_pixel
,
270 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
271 const SkBitmap::Config config
);
273 bool IsReadbackConfigSupported(SkBitmap::Config bitmap_config
);
275 // If we have locks on a frame during a ContentViewCore swap or a context
276 // lost, the frame is no longer valid and we can safely release all the locks.
277 // Use this method to release all the locks.
278 void ReleaseLocksOnSurface();
280 // Drop any incoming frames from the renderer when there are locks on the
282 void RetainFrame(uint32 output_surface_id
,
283 scoped_ptr
<cc::CompositorFrame
> frame
);
285 void InternalSwapCompositorFrame(uint32 output_surface_id
,
286 scoped_ptr
<cc::CompositorFrame
> frame
);
288 void SetNeedsAnimate();
289 bool Animate(base::TimeTicks frame_time
);
293 RenderWidgetHostImpl
* host_
;
295 // Used to track whether this render widget needs a BeginFrame.
296 bool needs_begin_frame_
;
300 // ContentViewCoreImpl is our interface to the view system.
301 ContentViewCoreImpl
* content_view_core_
;
303 ImeAdapterAndroid ime_adapter_android_
;
305 // Body background color of the underlying document.
306 SkColor cached_background_color_
;
308 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
309 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
310 scoped_refptr
<cc::DelegatedRendererLayer
> layer_
;
312 // The most recent texture size that was pushed to the texture layer.
313 gfx::Size texture_size_in_layer_
;
315 // The most recent content size that was pushed to the texture layer.
316 gfx::Size content_size_in_layer_
;
318 // The output surface id of the last received frame.
319 uint32_t last_output_surface_id_
;
321 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
323 std::queue
<base::Closure
> ack_callbacks_
;
325 const bool overscroll_effect_enabled_
;
326 // Used to render overscroll overlays.
327 // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
328 scoped_ptr
<OverscrollGlow
> overscroll_effect_
;
330 // Provides gesture synthesis given a stream of touch events (derived from
331 // Android MotionEvent's) and touch event acks.
332 ui::FilteredGestureProvider gesture_provider_
;
334 bool flush_input_requested_
;
336 int accelerated_surface_route_id_
;
338 // Size to use if we have no backing ContentViewCore
339 gfx::Size default_size_
;
341 const bool using_synchronous_compositor_
;
343 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
345 size_t locks_on_frame_count_
;
346 bool observing_root_window_
;
348 struct LastFrameInfo
{
349 LastFrameInfo(uint32 output_id
,
350 scoped_ptr
<cc::CompositorFrame
> output_frame
);
352 uint32 output_surface_id
;
353 scoped_ptr
<cc::CompositorFrame
> frame
;
356 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
358 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
361 } // namespace content
363 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_