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 "cc/surfaces/surface_factory_client.h"
20 #include "cc/surfaces/surface_id.h"
21 #include "content/browser/accessibility/browser_accessibility_manager.h"
22 #include "content/browser/renderer_host/delegated_frame_evictor.h"
23 #include "content/browser/renderer_host/ime_adapter_android.h"
24 #include "content/browser/renderer_host/input/stylus_text_selector.h"
25 #include "content/browser/renderer_host/render_widget_host_view_base.h"
26 #include "content/common/content_export.h"
27 #include "content/public/browser/readback_types.h"
28 #include "gpu/command_buffer/common/mailbox.h"
29 #include "third_party/skia/include/core/SkColor.h"
30 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
31 #include "ui/android/window_android_observer.h"
32 #include "ui/events/gesture_detection/filtered_gesture_provider.h"
33 #include "ui/gfx/geometry/size.h"
34 #include "ui/gfx/geometry/vector2d_f.h"
35 #include "ui/touch_selection/touch_selection_controller.h"
37 struct ViewHostMsg_TextInputState_Params
;
40 class CopyOutputResult
;
41 class DelegatedFrameProvider
;
42 class DelegatedRendererLayer
;
45 class SurfaceIdAllocator
;
46 enum class SurfaceDrawStatus
;
50 class WebExternalTextureLayer
;
56 class ContentViewCoreImpl
;
57 class OverscrollControllerAndroid
;
58 class RenderWidgetHost
;
59 class RenderWidgetHostImpl
;
60 struct DidOverscrollParams
;
61 struct NativeWebKeyboardEvent
;
63 // -----------------------------------------------------------------------------
64 // See comments in render_widget_host_view.h about this class and its members.
65 // -----------------------------------------------------------------------------
66 class CONTENT_EXPORT RenderWidgetHostViewAndroid
67 : public RenderWidgetHostViewBase
,
68 public cc::DelegatedFrameResourceCollectionClient
,
69 public cc::SurfaceFactoryClient
,
70 public ui::GestureProviderClient
,
71 public ui::WindowAndroidObserver
,
72 public DelegatedFrameEvictorClient
,
73 public StylusTextSelectorClient
,
74 public ui::TouchSelectionControllerClient
{
76 RenderWidgetHostViewAndroid(RenderWidgetHostImpl
* widget
,
77 ContentViewCoreImpl
* content_view_core
);
78 ~RenderWidgetHostViewAndroid() override
;
82 // RenderWidgetHostView implementation.
83 bool OnMessageReceived(const IPC::Message
& msg
) override
;
84 void InitAsChild(gfx::NativeView parent_view
) override
;
85 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
86 const gfx::Rect
& pos
) override
;
87 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
;
88 RenderWidgetHost
* GetRenderWidgetHost() const override
;
89 void SetSize(const gfx::Size
& size
) override
;
90 void SetBounds(const gfx::Rect
& rect
) override
;
91 gfx::Vector2dF
GetLastScrollOffset() const override
;
92 gfx::NativeView
GetNativeView() const override
;
93 gfx::NativeViewId
GetNativeViewId() const override
;
94 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
95 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
;
96 void Focus() override
;
97 bool HasFocus() const override
;
98 bool IsSurfaceAvailableForCopy() const override
;
100 void Hide() override
;
101 bool IsShowing() override
;
102 gfx::Rect
GetViewBounds() const override
;
103 gfx::Size
GetPhysicalBackingSize() const override
;
104 bool DoTopControlsShrinkBlinkSize() const override
;
105 float GetTopControlsHeight() const override
;
106 void UpdateCursor(const WebCursor
& cursor
) override
;
107 void SetIsLoading(bool is_loading
) override
;
108 void TextInputStateChanged(
109 const ViewHostMsg_TextInputState_Params
& params
) override
;
110 void ImeCancelComposition() override
;
111 void ImeCompositionRangeChanged(
112 const gfx::Range
& range
,
113 const std::vector
<gfx::Rect
>& character_bounds
) override
;
114 void FocusedNodeChanged(bool is_editable_node
) override
;
115 void RenderProcessGone(base::TerminationStatus status
,
116 int error_code
) override
;
117 void Destroy() override
;
118 void SetTooltipText(const base::string16
& tooltip_text
) override
;
119 void SelectionChanged(const base::string16
& text
,
121 const gfx::Range
& range
) override
;
122 void SelectionBoundsChanged(
123 const ViewHostMsg_SelectionBounds_Params
& params
) override
;
124 void AcceleratedSurfaceInitialized(int route_id
) override
;
125 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
126 void SetBackgroundColor(SkColor color
) override
;
127 void CopyFromCompositingSurface(
128 const gfx::Rect
& src_subrect
,
129 const gfx::Size
& dst_size
,
130 ReadbackRequestCallback
& callback
,
131 const SkColorType preferred_color_type
) override
;
132 void CopyFromCompositingSurfaceToVideoFrame(
133 const gfx::Rect
& src_subrect
,
134 const scoped_refptr
<media::VideoFrame
>& target
,
135 const base::Callback
<void(bool)>& callback
) override
;
136 bool CanCopyToVideoFrame() const override
;
137 void GetScreenInfo(blink::WebScreenInfo
* results
) override
;
138 gfx::Rect
GetBoundsInRootWindow() override
;
139 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
140 void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
141 InputEventAckState ack_result
) override
;
142 InputEventAckState
FilterInputEvent(
143 const blink::WebInputEvent
& input_event
) override
;
144 void OnSetNeedsFlushInput() override
;
145 void GestureEventAck(const blink::WebGestureEvent
& event
,
146 InputEventAckState ack_result
) override
;
147 BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
148 BrowserAccessibilityDelegate
* delegate
) override
;
149 bool LockMouse() override
;
150 void UnlockMouse() override
;
151 void OnSwapCompositorFrame(uint32 output_surface_id
,
152 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
153 void DidOverscroll(const DidOverscrollParams
& params
) override
;
154 void DidStopFlinging() override
;
155 uint32_t GetSurfaceIdNamespace() override
;
156 void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
157 const SkBitmap
& zoomed_bitmap
) override
;
158 scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget() override
;
159 void LockCompositingSurface() override
;
160 void UnlockCompositingSurface() override
;
161 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
163 size_t end_offset
) override
;
164 void OnDidNavigateMainFrameToNewPage() override
;
166 // cc::DelegatedFrameResourceCollectionClient implementation.
167 void UnusedResourcesAreAvailable() override
;
169 // cc::SurfaceFactoryClient implementation.
170 void ReturnResources(const cc::ReturnedResourceArray
& resources
) override
;
172 // ui::GestureProviderClient implementation.
173 void OnGestureEvent(const ui::GestureEventData
& gesture
) override
;
175 // ui::WindowAndroidObserver implementation.
176 void OnCompositingDidCommit() override
;
177 void OnRootWindowVisibilityChanged(bool visible
) override
;
178 void OnAttachCompositor() override
;
179 void OnDetachCompositor() override
;
180 void OnVSync(base::TimeTicks frame_time
,
181 base::TimeDelta vsync_period
) override
;
182 void OnAnimate(base::TimeTicks begin_frame_time
) override
;
183 void OnActivityStopped() override
;
184 void OnActivityStarted() override
;
186 // DelegatedFrameEvictor implementation
187 void EvictDelegatedFrame() override
;
189 // StylusTextSelectorClient implementation.
190 void OnStylusSelectBegin(float x0
, float y0
, float x1
, float y1
) override
;
191 void OnStylusSelectUpdate(float x
, float y
) override
;
192 void OnStylusSelectEnd() override
;
193 void OnStylusSelectTap(base::TimeTicks time
, float x
, float y
) override
;
195 // ui::TouchSelectionControllerClient implementation.
196 bool SupportsAnimation() const override
;
197 void SetNeedsAnimate() override
;
198 void MoveCaret(const gfx::PointF
& position
) override
;
199 void MoveRangeSelectionExtent(const gfx::PointF
& extent
) override
;
200 void SelectBetweenCoordinates(const gfx::PointF
& base
,
201 const gfx::PointF
& extent
) override
;
202 void OnSelectionEvent(ui::SelectionEventType event
) override
;
203 scoped_ptr
<ui::TouchHandleDrawable
> CreateDrawable() override
;
205 // Non-virtual methods
206 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
207 SkColor
GetCachedBackgroundColor() const;
208 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
209 void SendMouseEvent(const blink::WebMouseEvent
& event
);
210 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
211 void SendGestureEvent(const blink::WebGestureEvent
& event
);
213 void OnDidChangeBodyBackgroundColor(SkColor color
);
214 void OnStartContentIntent(const GURL
& content_url
);
215 void OnSetNeedsBeginFrames(bool enabled
);
216 void OnSmartClipDataExtracted(const base::string16
& text
,
217 const base::string16
& html
,
218 const gfx::Rect rect
);
220 bool OnTouchEvent(const ui::MotionEvent
& event
);
221 bool OnTouchHandleEvent(const ui::MotionEvent
& event
);
222 void ResetGestureDetection();
223 void SetDoubleTapSupportEnabled(bool enabled
);
224 void SetMultiTouchZoomSupportEnabled(bool enabled
);
226 long GetNativeImeAdapter();
230 void GetScaledContentBitmap(float scale
,
231 SkColorType preferred_color_type
,
232 gfx::Rect src_subrect
,
233 ReadbackRequestCallback
& result_callback
);
235 scoped_refptr
<cc::Layer
> CreateDelegatedLayer() const;
237 bool HasValidFrame() const;
239 void MoveCaret(const gfx::Point
& point
);
240 void DismissTextHandles();
241 void SetTextHandlesTemporarilyHidden(bool hidden
);
242 void OnShowingPastePopup(const gfx::PointF
& point
);
243 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
245 void SynchronousFrameMetadata(
246 const cc::CompositorFrameMetadata
& frame_metadata
);
248 void SetOverlayVideoMode(bool enabled
);
250 typedef base::Callback
<
251 void(const base::string16
& content
, int start_offset
, int end_offset
)>
252 TextSurroundingSelectionCallback
;
253 void SetTextSurroundingSelectionCallback(
254 const TextSurroundingSelectionCallback
& callback
);
256 static void OnContextLost();
259 void RunAckCallbacks(cc::SurfaceDrawStatus status
);
261 void DestroyDelegatedContent();
262 void CheckOutputSurfaceChanged(uint32 output_surface_id
);
263 void SubmitFrame(scoped_ptr
<cc::CompositorFrame
> frame_data
);
264 void SwapDelegatedFrame(uint32 output_surface_id
,
265 scoped_ptr
<cc::CompositorFrame
> frame_data
);
266 void SendDelegatedFrameAck(uint32 output_surface_id
);
267 void SendReturnedDelegatedResources(uint32 output_surface_id
);
269 void OnFrameMetadataUpdated(
270 const cc::CompositorFrameMetadata
& frame_metadata
);
271 void ComputeContentsSize(const cc::CompositorFrameMetadata
& frame_metadata
);
274 void HideInternal(bool hide_frontbuffer
, bool stop_observing_root_window
);
278 // Called after async screenshot task completes. Scales and crops the result
280 static void PrepareTextureCopyOutputResult(
281 const gfx::Size
& dst_size_in_pixel
,
282 SkColorType color_type
,
283 const base::TimeTicks
& start_time
,
284 ReadbackRequestCallback
& callback
,
285 scoped_ptr
<cc::CopyOutputResult
> result
);
286 static void PrepareTextureCopyOutputResultForDelegatedReadback(
287 const gfx::Size
& dst_size_in_pixel
,
288 SkColorType color_type
,
289 const base::TimeTicks
& start_time
,
290 scoped_refptr
<cc::Layer
> readback_layer
,
291 ReadbackRequestCallback
& callback
,
292 scoped_ptr
<cc::CopyOutputResult
> result
);
294 // DevTools ScreenCast support for Android WebView.
295 void SynchronousCopyContents(const gfx::Rect
& src_subrect_in_pixel
,
296 const gfx::Size
& dst_size_in_pixel
,
297 ReadbackRequestCallback
& callback
,
298 const SkColorType color_type
);
300 // If we have locks on a frame during a ContentViewCore swap or a context
301 // lost, the frame is no longer valid and we can safely release all the locks.
302 // Use this method to release all the locks.
303 void ReleaseLocksOnSurface();
305 // Drop any incoming frames from the renderer when there are locks on the
307 void RetainFrame(uint32 output_surface_id
,
308 scoped_ptr
<cc::CompositorFrame
> frame
);
310 void InternalSwapCompositorFrame(uint32 output_surface_id
,
311 scoped_ptr
<cc::CompositorFrame
> frame
);
312 void OnLostResources();
314 enum VSyncRequestType
{
315 FLUSH_INPUT
= 1 << 0,
316 BEGIN_FRAME
= 1 << 1,
317 PERSISTENT_BEGIN_FRAME
= 1 << 2
319 void RequestVSyncUpdate(uint32 requests
);
320 void StartObservingRootWindow();
321 void StopObservingRootWindow();
322 void SendBeginFrame(base::TimeTicks frame_time
, base::TimeDelta vsync_period
);
323 bool Animate(base::TimeTicks frame_time
);
324 void RequestDisallowInterceptTouchEvent();
327 RenderWidgetHostImpl
* host_
;
331 // Used to control action dispatch at the next |OnVSync()| call.
332 uint32 outstanding_vsync_requests_
;
336 // ContentViewCoreImpl is our interface to the view system.
337 ContentViewCoreImpl
* content_view_core_
;
339 // Cache the WindowAndroid instance exposed by ContentViewCore to avoid
340 // calling into ContentViewCore when it is being detached from the
341 // WebContents during destruction. The WindowAndroid has stronger lifetime
342 // guarantees, and should be safe to use for observer detachment.
343 // This will be non-null iff |content_view_core_| is non-null.
344 ui::WindowAndroid
* content_view_core_window_android_
;
346 ImeAdapterAndroid ime_adapter_android_
;
348 // Body background color of the underlying document.
349 SkColor cached_background_color_
;
351 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
352 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
353 scoped_refptr
<cc::Layer
> layer_
;
355 scoped_ptr
<cc::SurfaceIdAllocator
> id_allocator_
;
356 scoped_ptr
<cc::SurfaceFactory
> surface_factory_
;
357 cc::SurfaceId surface_id_
;
358 gfx::Size current_surface_size_
;
359 cc::ReturnedResourceArray surface_returned_resources_
;
361 // The most recent texture size that was pushed to the texture layer.
362 gfx::Size texture_size_in_layer_
;
364 // The most recent content size that was pushed to the texture layer.
365 gfx::Size content_size_in_layer_
;
367 // The output surface id of the last received frame.
368 uint32_t last_output_surface_id_
;
371 std::queue
<base::Closure
> ack_callbacks_
;
373 // Used to control and render overscroll-related effects.
374 scoped_ptr
<OverscrollControllerAndroid
> overscroll_controller_
;
376 // Provides gesture synthesis given a stream of touch events (derived from
377 // Android MotionEvent's) and touch event acks.
378 ui::FilteredGestureProvider gesture_provider_
;
380 // Handles gesture based text selection
381 StylusTextSelector stylus_text_selector_
;
383 // Manages selection handle rendering and manipulation.
384 // This will always be NULL if |content_view_core_| is NULL.
385 scoped_ptr
<ui::TouchSelectionController
> selection_controller_
;
387 int accelerated_surface_route_id_
;
389 // Size to use if we have no backing ContentViewCore
390 gfx::Size default_size_
;
392 const bool using_browser_compositor_
;
394 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
396 size_t locks_on_frame_count_
;
397 bool observing_root_window_
;
399 struct LastFrameInfo
{
400 LastFrameInfo(uint32 output_id
,
401 scoped_ptr
<cc::CompositorFrame
> output_frame
);
403 uint32 output_surface_id
;
404 scoped_ptr
<cc::CompositorFrame
> frame
;
407 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
409 TextSurroundingSelectionCallback text_surrounding_selection_callback_
;
411 // The last scroll offset of the view.
412 gfx::Vector2dF last_scroll_offset_
;
414 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
416 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
419 } // namespace content
421 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_