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::NativeView
GetNativeView() const OVERRIDE
;
114 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
115 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
116 virtual void MovePluginWindows(
117 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
;
118 virtual void Focus() OVERRIDE
;
119 virtual void Blur() OVERRIDE
;
120 virtual bool HasFocus() const OVERRIDE
;
121 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
122 virtual void Show() OVERRIDE
;
123 virtual void Hide() OVERRIDE
;
124 virtual bool IsShowing() OVERRIDE
;
125 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
126 virtual gfx::Size
GetPhysicalBackingSize() const OVERRIDE
;
127 virtual float GetTopControlsLayoutHeight() const OVERRIDE
;
128 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
;
129 virtual void SetIsLoading(bool is_loading
) OVERRIDE
;
130 virtual void TextInputStateChanged(
131 const ViewHostMsg_TextInputState_Params
& params
) OVERRIDE
;
132 virtual void ImeCancelComposition() OVERRIDE
;
133 virtual void FocusedNodeChanged(bool is_editable_node
) OVERRIDE
;
134 virtual void RenderProcessGone(base::TerminationStatus status
,
135 int error_code
) OVERRIDE
;
136 virtual void Destroy() OVERRIDE
;
137 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
;
138 virtual void SelectionChanged(const base::string16
& text
,
140 const gfx::Range
& range
) OVERRIDE
;
141 virtual void SelectionBoundsChanged(
142 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
;
143 virtual void AcceleratedSurfaceInitialized(int host_id
,
144 int route_id
) OVERRIDE
;
145 virtual void AcceleratedSurfaceBuffersSwapped(
146 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
147 int gpu_host_id
) OVERRIDE
;
148 virtual void AcceleratedSurfacePostSubBuffer(
149 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
150 int gpu_host_id
) OVERRIDE
;
151 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
152 virtual void AcceleratedSurfaceRelease() OVERRIDE
;
153 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
154 virtual void SetBackgroundOpaque(bool transparent
) OVERRIDE
;
155 virtual void CopyFromCompositingSurface(
156 const gfx::Rect
& src_subrect
,
157 const gfx::Size
& dst_size
,
158 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
159 const SkColorType color_type
) OVERRIDE
;
160 virtual void CopyFromCompositingSurfaceToVideoFrame(
161 const gfx::Rect
& src_subrect
,
162 const scoped_refptr
<media::VideoFrame
>& target
,
163 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
164 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
165 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
;
166 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
167 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
168 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
169 InputEventAckState ack_result
) OVERRIDE
;
170 virtual InputEventAckState
FilterInputEvent(
171 const blink::WebInputEvent
& input_event
) OVERRIDE
;
172 virtual void OnSetNeedsFlushInput() OVERRIDE
;
173 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
174 InputEventAckState ack_result
) OVERRIDE
;
175 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
176 BrowserAccessibilityDelegate
* delegate
) OVERRIDE
;
177 virtual bool LockMouse() OVERRIDE
;
178 virtual void UnlockMouse() OVERRIDE
;
179 virtual void OnSwapCompositorFrame(
180 uint32 output_surface_id
,
181 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
182 virtual void DidOverscroll(const DidOverscrollParams
& params
) OVERRIDE
;
183 virtual void DidStopFlinging() OVERRIDE
;
184 virtual void ShowDisambiguationPopup(const gfx::Rect
& target_rect
,
185 const SkBitmap
& zoomed_bitmap
) OVERRIDE
;
186 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget()
188 virtual void LockCompositingSurface() OVERRIDE
;
189 virtual void UnlockCompositingSurface() OVERRIDE
;
190 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
192 size_t end_offset
) OVERRIDE
;
194 // cc::DelegatedFrameResourceCollectionClient implementation.
195 virtual void UnusedResourcesAreAvailable() OVERRIDE
;
197 // ui::GestureProviderClient implementation.
198 virtual void OnGestureEvent(const ui::GestureEventData
& gesture
) OVERRIDE
;
200 // ui::WindowAndroidObserver implementation.
201 virtual void OnCompositingDidCommit() OVERRIDE
;
202 virtual void OnAttachCompositor() OVERRIDE
;
203 virtual void OnDetachCompositor() OVERRIDE
;
204 virtual void OnVSync(base::TimeTicks frame_time
,
205 base::TimeDelta vsync_period
) OVERRIDE
;
206 virtual void OnAnimate(base::TimeTicks begin_frame_time
) OVERRIDE
;
208 // ImageTransportFactoryAndroidObserver implementation.
209 virtual void OnLostResources() OVERRIDE
;
211 // DelegatedFrameEvictor implementation
212 virtual void EvictDelegatedFrame() OVERRIDE
;
214 virtual SkColorType
PreferredReadbackFormat() OVERRIDE
;
216 // GestureTextSelectorClient implementation.
217 virtual void ShowSelectionHandlesAutomatically() OVERRIDE
;
218 virtual void SelectRange(float x1
, float y1
, float x2
, float y2
) OVERRIDE
;
219 virtual void Unselect() OVERRIDE
;
220 virtual void LongPress(base::TimeTicks time
, float x
, float y
) OVERRIDE
;
222 // Non-virtual methods
223 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
224 SkColor
GetCachedBackgroundColor() const;
225 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
226 void SendTouchEvent(const blink::WebTouchEvent
& 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 bool HasValidFrame() const;
256 void MoveCaret(const gfx::Point
& point
);
257 void HideTextHandles();
258 void OnShowingPastePopup(const gfx::PointF
& point
);
260 void SynchronousFrameMetadata(
261 const cc::CompositorFrameMetadata
& frame_metadata
);
263 void SetOverlayVideoMode(bool enabled
);
265 typedef base::Callback
<
266 void(const base::string16
& content
, int start_offset
, int end_offset
)>
267 TextSurroundingSelectionCallback
;
268 void SetTextSurroundingSelectionCallback(
269 const TextSurroundingSelectionCallback
& callback
);
272 // TouchSelectionControllerClient implementation.
273 virtual bool SupportsAnimation() const OVERRIDE
;
274 virtual void SetNeedsAnimate() OVERRIDE
;
275 virtual void MoveCaret(const gfx::PointF
& position
) OVERRIDE
;
276 virtual void SelectBetweenCoordinates(const gfx::PointF
& start
,
277 const gfx::PointF
& end
) OVERRIDE
;
278 virtual void OnSelectionEvent(SelectionEventType event
,
279 const gfx::PointF
& anchor_position
) OVERRIDE
;
280 virtual scoped_ptr
<TouchHandleDrawable
> CreateDrawable() OVERRIDE
;
282 void RunAckCallbacks();
284 void DestroyDelegatedContent();
285 void SwapDelegatedFrame(uint32 output_surface_id
,
286 scoped_ptr
<cc::DelegatedFrameData
> frame_data
);
287 void SendDelegatedFrameAck(uint32 output_surface_id
);
288 void SendReturnedDelegatedResources(uint32 output_surface_id
);
290 void OnFrameMetadataUpdated(
291 const cc::CompositorFrameMetadata
& frame_metadata
);
292 void ComputeContentsSize(const cc::CompositorFrameMetadata
& frame_metadata
);
297 // Called after async screenshot task completes. Scales and crops the result
299 static void PrepareTextureCopyOutputResult(
300 const gfx::Size
& dst_size_in_pixel
,
301 const SkColorType color_type
,
302 const base::TimeTicks
& start_time
,
303 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
304 scoped_ptr
<cc::CopyOutputResult
> result
);
305 static void PrepareTextureCopyOutputResultForDelegatedReadback(
306 const gfx::Size
& dst_size_in_pixel
,
307 const SkColorType color_type
,
308 const base::TimeTicks
& start_time
,
309 scoped_refptr
<cc::Layer
> readback_layer
,
310 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
311 scoped_ptr
<cc::CopyOutputResult
> result
);
313 // DevTools ScreenCast support for Android WebView.
314 void SynchronousCopyContents(
315 const gfx::Rect
& src_subrect_in_pixel
,
316 const gfx::Size
& dst_size_in_pixel
,
317 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
318 const SkColorType color_type
);
320 bool IsReadbackConfigSupported(SkColorType color_type
);
322 // If we have locks on a frame during a ContentViewCore swap or a context
323 // lost, the frame is no longer valid and we can safely release all the locks.
324 // Use this method to release all the locks.
325 void ReleaseLocksOnSurface();
327 // Drop any incoming frames from the renderer when there are locks on the
329 void RetainFrame(uint32 output_surface_id
,
330 scoped_ptr
<cc::CompositorFrame
> frame
);
332 void InternalSwapCompositorFrame(uint32 output_surface_id
,
333 scoped_ptr
<cc::CompositorFrame
> frame
);
335 enum VSyncRequestType
{
336 FLUSH_INPUT
= 1 << 0,
337 BEGIN_FRAME
= 1 << 1,
338 PERSISTENT_BEGIN_FRAME
= 1 << 2
340 void RequestVSyncUpdate(uint32 requests
);
341 void StartObservingRootWindow();
342 void StopObservingRootWindow();
343 void SendBeginFrame(base::TimeTicks frame_time
, base::TimeDelta vsync_period
);
344 bool Animate(base::TimeTicks frame_time
);
346 void OnContentScrollingChange();
347 bool IsContentScrolling() const;
349 float GetDpiScale() const;
351 // Handles all unprocessed and pending readback requests.
352 void AbortPendingReadbackRequests();
355 RenderWidgetHostImpl
* host_
;
357 // Used to control action dispatch at the next |OnVSync()| call.
358 uint32 outstanding_vsync_requests_
;
362 // ContentViewCoreImpl is our interface to the view system.
363 ContentViewCoreImpl
* content_view_core_
;
365 ImeAdapterAndroid ime_adapter_android_
;
367 // Body background color of the underlying document.
368 SkColor cached_background_color_
;
370 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
371 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
372 scoped_refptr
<cc::DelegatedRendererLayer
> layer_
;
374 // The most recent texture size that was pushed to the texture layer.
375 gfx::Size texture_size_in_layer_
;
377 // The most recent content size that was pushed to the texture layer.
378 gfx::Size content_size_in_layer_
;
380 // The output surface id of the last received frame.
381 uint32_t last_output_surface_id_
;
383 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
385 std::queue
<base::Closure
> ack_callbacks_
;
387 const bool overscroll_effect_enabled_
;
388 // Used to render overscroll overlays.
389 scoped_ptr
<OverscrollGlow
> overscroll_effect_
;
391 // Provides gesture synthesis given a stream of touch events (derived from
392 // Android MotionEvent's) and touch event acks.
393 ui::FilteredGestureProvider gesture_provider_
;
395 // Handles gesture based text selection
396 GestureTextSelector gesture_text_selector_
;
398 // Manages selection handle rendering and manipulation.
399 // This will always be NULL if |content_view_core_| is NULL.
400 scoped_ptr
<TouchSelectionController
> selection_controller_
;
401 bool touch_scrolling_
;
402 size_t potentially_active_fling_count_
;
404 int accelerated_surface_route_id_
;
406 // Size to use if we have no backing ContentViewCore
407 gfx::Size default_size_
;
409 const bool using_synchronous_compositor_
;
411 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
413 size_t locks_on_frame_count_
;
414 bool observing_root_window_
;
416 struct LastFrameInfo
{
417 LastFrameInfo(uint32 output_id
,
418 scoped_ptr
<cc::CompositorFrame
> output_frame
);
420 uint32 output_surface_id
;
421 scoped_ptr
<cc::CompositorFrame
> frame
;
424 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
426 TextSurroundingSelectionCallback text_surrounding_selection_callback_
;
428 // List of readbackrequests waiting for arrival of a valid frame.
429 std::queue
<ReadbackRequest
> readbacks_waiting_for_frame_
;
431 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
434 } // namespace content
436 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_