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/input/gesture_text_selector.h"
24 #include "content/browser/renderer_host/input/touch_selection_controller.h"
25 #include "content/browser/renderer_host/render_widget_host_view_base.h"
26 #include "content/common/content_export.h"
27 #include "gpu/command_buffer/common/mailbox.h"
28 #include "third_party/skia/include/core/SkColor.h"
29 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
30 #include "ui/base/android/window_android_observer.h"
31 #include "ui/events/gesture_detection/filtered_gesture_provider.h"
32 #include "ui/gfx/size.h"
33 #include "ui/gfx/vector2d_f.h"
35 struct ViewHostMsg_TextInputState_Params
;
37 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
;
38 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
;
41 class CopyOutputResult
;
42 class DelegatedFrameProvider
;
43 class DelegatedRendererLayer
;
48 class WebExternalTextureLayer
;
54 class ContentViewCoreImpl
;
56 class RenderWidgetHost
;
57 class RenderWidgetHostImpl
;
58 struct DidOverscrollParams
;
59 struct NativeWebKeyboardEvent
;
61 class ReadbackRequest
{
63 explicit ReadbackRequest(
65 SkColorType color_type
,
66 gfx::Rect src_subrect
,
67 const base::Callback
<void(bool, const SkBitmap
&)>& result_callback
);
69 float GetScale() { return scale_
; }
70 SkColorType
GetColorFormat() { return color_type_
; }
71 const gfx::Rect
GetCaptureRect() { return src_subrect_
; }
72 const base::Callback
<void(bool, const SkBitmap
&)>& GetResultCallback() {
73 return result_callback_
;
79 SkColorType color_type_
;
80 gfx::Rect src_subrect_
;
81 base::Callback
<void(bool, const SkBitmap
&)> result_callback_
;
84 // -----------------------------------------------------------------------------
85 // See comments in render_widget_host_view.h about this class and its members.
86 // -----------------------------------------------------------------------------
87 class CONTENT_EXPORT RenderWidgetHostViewAndroid
88 : public RenderWidgetHostViewBase
,
89 public cc::DelegatedFrameResourceCollectionClient
,
90 public ImageTransportFactoryAndroidObserver
,
91 public ui::GestureProviderClient
,
92 public ui::WindowAndroidObserver
,
93 public DelegatedFrameEvictorClient
,
94 public GestureTextSelectorClient
,
95 public TouchSelectionControllerClient
{
97 RenderWidgetHostViewAndroid(RenderWidgetHostImpl
* widget
,
98 ContentViewCoreImpl
* content_view_core
);
99 virtual ~RenderWidgetHostViewAndroid();
101 // RenderWidgetHostView implementation.
102 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
103 virtual void InitAsChild(gfx::NativeView parent_view
) OVERRIDE
;
104 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
105 const gfx::Rect
& pos
) OVERRIDE
;
106 virtual void InitAsFullscreen(
107 RenderWidgetHostView
* reference_host_view
) OVERRIDE
;
108 virtual RenderWidgetHost
* GetRenderWidgetHost() const OVERRIDE
;
109 virtual void WasShown() OVERRIDE
;
110 virtual void WasHidden() OVERRIDE
;
111 virtual void SetSize(const gfx::Size
& size
) OVERRIDE
;
112 virtual void SetBounds(const gfx::Rect
& rect
) OVERRIDE
;
113 virtual gfx::Vector2dF
GetLastScrollOffset() const OVERRIDE
;
114 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
115 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
116 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
117 virtual void MovePluginWindows(
118 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
;
119 virtual void Focus() OVERRIDE
;
120 virtual void Blur() OVERRIDE
;
121 virtual bool HasFocus() const OVERRIDE
;
122 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
123 virtual void Show() OVERRIDE
;
124 virtual void Hide() OVERRIDE
;
125 virtual bool IsShowing() OVERRIDE
;
126 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
127 virtual gfx::Size
GetPhysicalBackingSize() const OVERRIDE
;
128 virtual float GetTopControlsLayoutHeight() const OVERRIDE
;
129 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
;
130 virtual void SetIsLoading(bool is_loading
) OVERRIDE
;
131 virtual void TextInputStateChanged(
132 const ViewHostMsg_TextInputState_Params
& params
) OVERRIDE
;
133 virtual void ImeCancelComposition() OVERRIDE
;
134 virtual void FocusedNodeChanged(bool is_editable_node
) OVERRIDE
;
135 virtual void RenderProcessGone(base::TerminationStatus status
,
136 int error_code
) OVERRIDE
;
137 virtual void Destroy() OVERRIDE
;
138 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
;
139 virtual void SelectionChanged(const base::string16
& text
,
141 const gfx::Range
& range
) OVERRIDE
;
142 virtual void SelectionBoundsChanged(
143 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
;
144 virtual void AcceleratedSurfaceInitialized(int host_id
,
145 int route_id
) OVERRIDE
;
146 virtual void AcceleratedSurfaceBuffersSwapped(
147 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
148 int gpu_host_id
) OVERRIDE
;
149 virtual void AcceleratedSurfacePostSubBuffer(
150 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
151 int gpu_host_id
) OVERRIDE
;
152 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
153 virtual void AcceleratedSurfaceRelease() OVERRIDE
;
154 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
155 virtual void SetBackgroundOpaque(bool transparent
) OVERRIDE
;
156 virtual void CopyFromCompositingSurface(
157 const gfx::Rect
& src_subrect
,
158 const gfx::Size
& dst_size
,
159 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
160 const SkColorType color_type
) OVERRIDE
;
161 virtual void CopyFromCompositingSurfaceToVideoFrame(
162 const gfx::Rect
& src_subrect
,
163 const scoped_refptr
<media::VideoFrame
>& target
,
164 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
165 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
166 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
;
167 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
168 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
169 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
170 InputEventAckState ack_result
) OVERRIDE
;
171 virtual InputEventAckState
FilterInputEvent(
172 const blink::WebInputEvent
& input_event
) OVERRIDE
;
173 virtual void OnSetNeedsFlushInput() OVERRIDE
;
174 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
175 InputEventAckState ack_result
) OVERRIDE
;
176 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
177 BrowserAccessibilityDelegate
* delegate
) OVERRIDE
;
178 virtual bool LockMouse() OVERRIDE
;
179 virtual void UnlockMouse() OVERRIDE
;
180 virtual void OnSwapCompositorFrame(
181 uint32 output_surface_id
,
182 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
183 virtual void DidOverscroll(const DidOverscrollParams
& params
) OVERRIDE
;
184 virtual void DidStopFlinging() OVERRIDE
;
185 virtual void ShowDisambiguationPopup(const gfx::Rect
& target_rect
,
186 const SkBitmap
& zoomed_bitmap
) OVERRIDE
;
187 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget()
189 virtual void LockCompositingSurface() OVERRIDE
;
190 virtual void UnlockCompositingSurface() OVERRIDE
;
191 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
193 size_t end_offset
) OVERRIDE
;
195 // cc::DelegatedFrameResourceCollectionClient implementation.
196 virtual void UnusedResourcesAreAvailable() OVERRIDE
;
198 // ui::GestureProviderClient implementation.
199 virtual void OnGestureEvent(const ui::GestureEventData
& gesture
) OVERRIDE
;
201 // ui::WindowAndroidObserver implementation.
202 virtual void OnCompositingDidCommit() OVERRIDE
;
203 virtual void OnAttachCompositor() OVERRIDE
;
204 virtual void OnDetachCompositor() OVERRIDE
;
205 virtual void OnVSync(base::TimeTicks frame_time
,
206 base::TimeDelta vsync_period
) OVERRIDE
;
207 virtual void OnAnimate(base::TimeTicks begin_frame_time
) OVERRIDE
;
209 // ImageTransportFactoryAndroidObserver implementation.
210 virtual void OnLostResources() OVERRIDE
;
212 // DelegatedFrameEvictor implementation
213 virtual void EvictDelegatedFrame() OVERRIDE
;
215 virtual SkColorType
PreferredReadbackFormat() OVERRIDE
;
217 // GestureTextSelectorClient implementation.
218 virtual void ShowSelectionHandlesAutomatically() OVERRIDE
;
219 virtual void SelectRange(float x1
, float y1
, float x2
, float y2
) OVERRIDE
;
220 virtual void Unselect() OVERRIDE
;
221 virtual void LongPress(base::TimeTicks time
, float x
, float y
) OVERRIDE
;
223 // Non-virtual methods
224 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
225 SkColor
GetCachedBackgroundColor() const;
226 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
227 void SendMouseEvent(const blink::WebMouseEvent
& event
);
228 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
229 void SendGestureEvent(const blink::WebGestureEvent
& event
);
231 void OnDidChangeBodyBackgroundColor(SkColor color
);
232 void OnStartContentIntent(const GURL
& content_url
);
233 void OnSetNeedsBeginFrame(bool enabled
);
234 void OnSmartClipDataExtracted(const base::string16
& text
,
235 const base::string16
& html
,
236 const gfx::Rect rect
);
238 bool OnTouchEvent(const ui::MotionEvent
& event
);
239 bool OnTouchHandleEvent(const ui::MotionEvent
& event
);
240 void ResetGestureDetection();
241 void SetDoubleTapSupportEnabled(bool enabled
);
242 void SetMultiTouchZoomSupportEnabled(bool enabled
);
244 long GetNativeImeAdapter();
248 void GetScaledContentBitmap(
250 SkColorType color_type
,
251 gfx::Rect src_subrect
,
252 const base::Callback
<void(bool, const SkBitmap
&)>& result_callback
);
254 scoped_refptr
<cc::DelegatedRendererLayer
>
255 CreateDelegatedLayerForFrameProvider() const;
257 bool HasValidFrame() const;
259 void MoveCaret(const gfx::Point
& point
);
260 void HideTextHandles();
261 void OnShowingPastePopup(const gfx::PointF
& point
);
263 void SynchronousFrameMetadata(
264 const cc::CompositorFrameMetadata
& frame_metadata
);
266 void SetOverlayVideoMode(bool enabled
);
268 typedef base::Callback
<
269 void(const base::string16
& content
, int start_offset
, int end_offset
)>
270 TextSurroundingSelectionCallback
;
271 void SetTextSurroundingSelectionCallback(
272 const TextSurroundingSelectionCallback
& callback
);
275 // TouchSelectionControllerClient implementation.
276 virtual bool SupportsAnimation() const OVERRIDE
;
277 virtual void SetNeedsAnimate() OVERRIDE
;
278 virtual void MoveCaret(const gfx::PointF
& position
) OVERRIDE
;
279 virtual void SelectBetweenCoordinates(const gfx::PointF
& start
,
280 const gfx::PointF
& end
) OVERRIDE
;
281 virtual void OnSelectionEvent(SelectionEventType event
,
282 const gfx::PointF
& anchor_position
) OVERRIDE
;
283 virtual scoped_ptr
<TouchHandleDrawable
> CreateDrawable() OVERRIDE
;
285 void RunAckCallbacks();
287 void DestroyDelegatedContent();
288 void SwapDelegatedFrame(uint32 output_surface_id
,
289 scoped_ptr
<cc::DelegatedFrameData
> frame_data
);
290 void SendDelegatedFrameAck(uint32 output_surface_id
);
291 void SendReturnedDelegatedResources(uint32 output_surface_id
);
293 void OnFrameMetadataUpdated(
294 const cc::CompositorFrameMetadata
& frame_metadata
);
295 void ComputeContentsSize(const cc::CompositorFrameMetadata
& frame_metadata
);
300 // Called after async screenshot task completes. Scales and crops the result
302 static void PrepareTextureCopyOutputResult(
303 const gfx::Size
& dst_size_in_pixel
,
304 const SkColorType color_type
,
305 const base::TimeTicks
& start_time
,
306 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
307 scoped_ptr
<cc::CopyOutputResult
> result
);
308 static void PrepareTextureCopyOutputResultForDelegatedReadback(
309 const gfx::Size
& dst_size_in_pixel
,
310 const SkColorType color_type
,
311 const base::TimeTicks
& start_time
,
312 scoped_refptr
<cc::Layer
> readback_layer
,
313 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
314 scoped_ptr
<cc::CopyOutputResult
> result
);
316 // DevTools ScreenCast support for Android WebView.
317 void SynchronousCopyContents(
318 const gfx::Rect
& src_subrect_in_pixel
,
319 const gfx::Size
& dst_size_in_pixel
,
320 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
321 const SkColorType color_type
);
323 bool IsReadbackConfigSupported(SkColorType color_type
);
325 // If we have locks on a frame during a ContentViewCore swap or a context
326 // lost, the frame is no longer valid and we can safely release all the locks.
327 // Use this method to release all the locks.
328 void ReleaseLocksOnSurface();
330 // Drop any incoming frames from the renderer when there are locks on the
332 void RetainFrame(uint32 output_surface_id
,
333 scoped_ptr
<cc::CompositorFrame
> frame
);
335 void InternalSwapCompositorFrame(uint32 output_surface_id
,
336 scoped_ptr
<cc::CompositorFrame
> frame
);
338 enum VSyncRequestType
{
339 FLUSH_INPUT
= 1 << 0,
340 BEGIN_FRAME
= 1 << 1,
341 PERSISTENT_BEGIN_FRAME
= 1 << 2
343 void RequestVSyncUpdate(uint32 requests
);
344 void StartObservingRootWindow();
345 void StopObservingRootWindow();
346 void SendBeginFrame(base::TimeTicks frame_time
, base::TimeDelta vsync_period
);
347 bool Animate(base::TimeTicks frame_time
);
349 void OnContentScrollingChange();
350 bool IsContentScrolling() const;
352 float GetDpiScale() const;
354 // Handles all unprocessed and pending readback requests.
355 void AbortPendingReadbackRequests();
358 RenderWidgetHostImpl
* host_
;
360 // Used to control action dispatch at the next |OnVSync()| call.
361 uint32 outstanding_vsync_requests_
;
365 // ContentViewCoreImpl is our interface to the view system.
366 ContentViewCoreImpl
* content_view_core_
;
368 ImeAdapterAndroid ime_adapter_android_
;
370 // Body background color of the underlying document.
371 SkColor cached_background_color_
;
373 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
374 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
375 scoped_refptr
<cc::DelegatedRendererLayer
> layer_
;
377 // The most recent texture size that was pushed to the texture layer.
378 gfx::Size texture_size_in_layer_
;
380 // The most recent content size that was pushed to the texture layer.
381 gfx::Size content_size_in_layer_
;
383 // The output surface id of the last received frame.
384 uint32_t last_output_surface_id_
;
386 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
388 std::queue
<base::Closure
> ack_callbacks_
;
390 const bool overscroll_effect_enabled_
;
391 // Used to render overscroll overlays.
392 scoped_ptr
<OverscrollGlow
> overscroll_effect_
;
394 // Provides gesture synthesis given a stream of touch events (derived from
395 // Android MotionEvent's) and touch event acks.
396 ui::FilteredGestureProvider gesture_provider_
;
398 // Handles gesture based text selection
399 GestureTextSelector gesture_text_selector_
;
401 // Manages selection handle rendering and manipulation.
402 // This will always be NULL if |content_view_core_| is NULL.
403 scoped_ptr
<TouchSelectionController
> selection_controller_
;
404 bool touch_scrolling_
;
405 size_t potentially_active_fling_count_
;
407 int accelerated_surface_route_id_
;
409 // Size to use if we have no backing ContentViewCore
410 gfx::Size default_size_
;
412 const bool using_synchronous_compositor_
;
414 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
416 size_t locks_on_frame_count_
;
417 bool observing_root_window_
;
419 struct LastFrameInfo
{
420 LastFrameInfo(uint32 output_id
,
421 scoped_ptr
<cc::CompositorFrame
> output_frame
);
423 uint32 output_surface_id
;
424 scoped_ptr
<cc::CompositorFrame
> frame
;
427 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
429 TextSurroundingSelectionCallback text_surrounding_selection_callback_
;
431 // List of readbackrequests waiting for arrival of a valid frame.
432 std::queue
<ReadbackRequest
> readbacks_waiting_for_frame_
;
434 // The last scroll offset of the view.
435 gfx::Vector2dF last_scroll_offset_
;
437 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
440 } // namespace content
442 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_