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 bool GetScreenColorProfile(std::vector
<char>* color_profile
) override
;
139 gfx::Rect
GetBoundsInRootWindow() override
;
140 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
141 void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
142 InputEventAckState ack_result
) override
;
143 InputEventAckState
FilterInputEvent(
144 const blink::WebInputEvent
& input_event
) override
;
145 void OnSetNeedsFlushInput() override
;
146 void GestureEventAck(const blink::WebGestureEvent
& event
,
147 InputEventAckState ack_result
) override
;
148 BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
149 BrowserAccessibilityDelegate
* delegate
) override
;
150 bool LockMouse() override
;
151 void UnlockMouse() override
;
152 void OnSwapCompositorFrame(uint32 output_surface_id
,
153 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
154 void DidOverscroll(const DidOverscrollParams
& params
) override
;
155 void DidStopFlinging() override
;
156 uint32_t GetSurfaceIdNamespace() override
;
157 void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
158 const SkBitmap
& zoomed_bitmap
) override
;
159 scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget() override
;
160 void LockCompositingSurface() override
;
161 void UnlockCompositingSurface() override
;
162 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
164 size_t end_offset
) override
;
165 void OnDidNavigateMainFrameToNewPage() override
;
167 // cc::DelegatedFrameResourceCollectionClient implementation.
168 void UnusedResourcesAreAvailable() override
;
170 // cc::SurfaceFactoryClient implementation.
171 void ReturnResources(const cc::ReturnedResourceArray
& resources
) override
;
173 // ui::GestureProviderClient implementation.
174 void OnGestureEvent(const ui::GestureEventData
& gesture
) override
;
176 // ui::WindowAndroidObserver implementation.
177 void OnCompositingDidCommit() override
;
178 void OnRootWindowVisibilityChanged(bool visible
) override
;
179 void OnAttachCompositor() override
;
180 void OnDetachCompositor() override
;
181 void OnVSync(base::TimeTicks frame_time
,
182 base::TimeDelta vsync_period
) override
;
183 void OnAnimate(base::TimeTicks begin_frame_time
) override
;
184 void OnActivityStopped() override
;
185 void OnActivityStarted() override
;
187 // DelegatedFrameEvictor implementation
188 void EvictDelegatedFrame() override
;
190 // StylusTextSelectorClient implementation.
191 void OnStylusSelectBegin(float x0
, float y0
, float x1
, float y1
) override
;
192 void OnStylusSelectUpdate(float x
, float y
) override
;
193 void OnStylusSelectEnd() override
;
194 void OnStylusSelectTap(base::TimeTicks time
, float x
, float y
) override
;
196 // ui::TouchSelectionControllerClient implementation.
197 bool SupportsAnimation() const override
;
198 void SetNeedsAnimate() override
;
199 void MoveCaret(const gfx::PointF
& position
) override
;
200 void MoveRangeSelectionExtent(const gfx::PointF
& extent
) override
;
201 void SelectBetweenCoordinates(const gfx::PointF
& base
,
202 const gfx::PointF
& extent
) override
;
203 void OnSelectionEvent(ui::SelectionEventType event
) override
;
204 scoped_ptr
<ui::TouchHandleDrawable
> CreateDrawable() override
;
206 // Non-virtual methods
207 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
208 SkColor
GetCachedBackgroundColor() const;
209 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
210 void SendMouseEvent(const blink::WebMouseEvent
& event
);
211 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
212 void SendGestureEvent(const blink::WebGestureEvent
& event
);
214 void OnDidChangeBodyBackgroundColor(SkColor color
);
215 void OnStartContentIntent(const GURL
& content_url
);
216 void OnSetNeedsBeginFrames(bool enabled
);
217 void OnSmartClipDataExtracted(const base::string16
& text
,
218 const base::string16
& html
,
219 const gfx::Rect rect
);
221 bool OnTouchEvent(const ui::MotionEvent
& event
);
222 bool OnTouchHandleEvent(const ui::MotionEvent
& event
);
223 void ResetGestureDetection();
224 void SetDoubleTapSupportEnabled(bool enabled
);
225 void SetMultiTouchZoomSupportEnabled(bool enabled
);
227 long GetNativeImeAdapter();
231 void GetScaledContentBitmap(float scale
,
232 SkColorType preferred_color_type
,
233 gfx::Rect src_subrect
,
234 ReadbackRequestCallback
& result_callback
);
236 scoped_refptr
<cc::Layer
> CreateDelegatedLayer() const;
238 bool HasValidFrame() const;
240 void MoveCaret(const gfx::Point
& point
);
241 void DismissTextHandles();
242 void SetTextHandlesTemporarilyHidden(bool hidden
);
243 void OnShowingPastePopup(const gfx::PointF
& point
);
244 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
246 void SynchronousFrameMetadata(
247 const cc::CompositorFrameMetadata
& frame_metadata
);
249 void SetOverlayVideoMode(bool enabled
);
251 typedef base::Callback
<
252 void(const base::string16
& content
, int start_offset
, int end_offset
)>
253 TextSurroundingSelectionCallback
;
254 void SetTextSurroundingSelectionCallback(
255 const TextSurroundingSelectionCallback
& callback
);
257 static void OnContextLost();
260 void RunAckCallbacks(cc::SurfaceDrawStatus status
);
262 void DestroyDelegatedContent();
263 void CheckOutputSurfaceChanged(uint32 output_surface_id
);
264 void SubmitCompositorFrame(scoped_ptr
<cc::CompositorFrame
> frame_data
);
265 void SwapDelegatedFrame(uint32 output_surface_id
,
266 scoped_ptr
<cc::CompositorFrame
> frame_data
);
267 void SendDelegatedFrameAck(uint32 output_surface_id
);
268 void SendReturnedDelegatedResources(uint32 output_surface_id
);
270 void OnFrameMetadataUpdated(
271 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_
;
360 gfx::Vector2dF location_bar_content_translation_
;
361 cc::ViewportSelection current_viewport_selection_
;
363 // The most recent texture size that was pushed to the texture layer.
364 gfx::Size texture_size_in_layer_
;
366 // The output surface id of the last received frame.
367 uint32_t last_output_surface_id_
;
370 std::queue
<base::Closure
> ack_callbacks_
;
372 // Used to control and render overscroll-related effects.
373 scoped_ptr
<OverscrollControllerAndroid
> overscroll_controller_
;
375 // Provides gesture synthesis given a stream of touch events (derived from
376 // Android MotionEvent's) and touch event acks.
377 ui::FilteredGestureProvider gesture_provider_
;
379 // Handles gesture based text selection
380 StylusTextSelector stylus_text_selector_
;
382 // Manages selection handle rendering and manipulation.
383 // This will always be NULL if |content_view_core_| is NULL.
384 scoped_ptr
<ui::TouchSelectionController
> selection_controller_
;
386 int accelerated_surface_route_id_
;
388 // Size to use if we have no backing ContentViewCore
389 gfx::Size default_size_
;
391 const bool using_browser_compositor_
;
393 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
395 size_t locks_on_frame_count_
;
396 bool observing_root_window_
;
398 struct LastFrameInfo
{
399 LastFrameInfo(uint32 output_id
,
400 scoped_ptr
<cc::CompositorFrame
> output_frame
);
402 uint32 output_surface_id
;
403 scoped_ptr
<cc::CompositorFrame
> frame
;
406 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
408 TextSurroundingSelectionCallback text_surrounding_selection_callback_
;
410 // The last scroll offset of the view.
411 gfx::Vector2dF last_scroll_offset_
;
413 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
415 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
418 } // namespace content
420 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_