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 ClearCompositorFrame() override
;
155 void DidOverscroll(const DidOverscrollParams
& params
) override
;
156 void DidStopFlinging() override
;
157 uint32_t GetSurfaceIdNamespace() override
;
158 void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
159 const SkBitmap
& zoomed_bitmap
) override
;
160 scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget() override
;
161 void LockCompositingSurface() override
;
162 void UnlockCompositingSurface() override
;
163 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
165 size_t end_offset
) override
;
166 void OnDidNavigateMainFrameToNewPage() override
;
168 // cc::DelegatedFrameResourceCollectionClient implementation.
169 void UnusedResourcesAreAvailable() override
;
171 // cc::SurfaceFactoryClient implementation.
172 void ReturnResources(const cc::ReturnedResourceArray
& resources
) override
;
174 // ui::GestureProviderClient implementation.
175 void OnGestureEvent(const ui::GestureEventData
& gesture
) override
;
177 // ui::WindowAndroidObserver implementation.
178 void OnCompositingDidCommit() override
;
179 void OnRootWindowVisibilityChanged(bool visible
) override
;
180 void OnAttachCompositor() override
;
181 void OnDetachCompositor() override
;
182 void OnVSync(base::TimeTicks frame_time
,
183 base::TimeDelta vsync_period
) override
;
184 void OnAnimate(base::TimeTicks begin_frame_time
) override
;
185 void OnActivityStopped() override
;
186 void OnActivityStarted() override
;
188 // DelegatedFrameEvictor implementation
189 void EvictDelegatedFrame() override
;
191 // StylusTextSelectorClient implementation.
192 void OnStylusSelectBegin(float x0
, float y0
, float x1
, float y1
) override
;
193 void OnStylusSelectUpdate(float x
, float y
) override
;
194 void OnStylusSelectEnd() override
;
195 void OnStylusSelectTap(base::TimeTicks time
, float x
, float y
) override
;
197 // ui::TouchSelectionControllerClient implementation.
198 bool SupportsAnimation() const override
;
199 void SetNeedsAnimate() override
;
200 void MoveCaret(const gfx::PointF
& position
) override
;
201 void MoveRangeSelectionExtent(const gfx::PointF
& extent
) override
;
202 void SelectBetweenCoordinates(const gfx::PointF
& base
,
203 const gfx::PointF
& extent
) override
;
204 void OnSelectionEvent(ui::SelectionEventType event
) override
;
205 scoped_ptr
<ui::TouchHandleDrawable
> CreateDrawable() override
;
207 // Non-virtual methods
208 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
209 SkColor
GetCachedBackgroundColor() const;
210 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
211 void SendMouseEvent(const blink::WebMouseEvent
& event
);
212 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
213 void SendGestureEvent(const blink::WebGestureEvent
& event
);
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 void UpdateBackgroundColor(SkColor color
);
280 // Called after async screenshot task completes. Scales and crops the result
282 static void PrepareTextureCopyOutputResult(
283 const gfx::Size
& dst_size_in_pixel
,
284 SkColorType color_type
,
285 const base::TimeTicks
& start_time
,
286 ReadbackRequestCallback
& callback
,
287 scoped_ptr
<cc::CopyOutputResult
> result
);
288 static void PrepareTextureCopyOutputResultForDelegatedReadback(
289 const gfx::Size
& dst_size_in_pixel
,
290 SkColorType color_type
,
291 const base::TimeTicks
& start_time
,
292 scoped_refptr
<cc::Layer
> readback_layer
,
293 ReadbackRequestCallback
& callback
,
294 scoped_ptr
<cc::CopyOutputResult
> result
);
296 // DevTools ScreenCast support for Android WebView.
297 void SynchronousCopyContents(const gfx::Rect
& src_subrect_in_pixel
,
298 const gfx::Size
& dst_size_in_pixel
,
299 ReadbackRequestCallback
& callback
,
300 const SkColorType color_type
);
302 // If we have locks on a frame during a ContentViewCore swap or a context
303 // lost, the frame is no longer valid and we can safely release all the locks.
304 // Use this method to release all the locks.
305 void ReleaseLocksOnSurface();
307 // Drop any incoming frames from the renderer when there are locks on the
309 void RetainFrame(uint32 output_surface_id
,
310 scoped_ptr
<cc::CompositorFrame
> frame
);
312 void InternalSwapCompositorFrame(uint32 output_surface_id
,
313 scoped_ptr
<cc::CompositorFrame
> frame
);
314 void OnLostResources();
316 enum VSyncRequestType
{
317 FLUSH_INPUT
= 1 << 0,
318 BEGIN_FRAME
= 1 << 1,
319 PERSISTENT_BEGIN_FRAME
= 1 << 2
321 void RequestVSyncUpdate(uint32 requests
);
322 void StartObservingRootWindow();
323 void StopObservingRootWindow();
324 void SendBeginFrame(base::TimeTicks frame_time
, base::TimeDelta vsync_period
);
325 bool Animate(base::TimeTicks frame_time
);
326 void RequestDisallowInterceptTouchEvent();
329 RenderWidgetHostImpl
* host_
;
333 // Used to control action dispatch at the next |OnVSync()| call.
334 uint32 outstanding_vsync_requests_
;
338 // ContentViewCoreImpl is our interface to the view system.
339 ContentViewCoreImpl
* content_view_core_
;
341 // Cache the WindowAndroid instance exposed by ContentViewCore to avoid
342 // calling into ContentViewCore when it is being detached from the
343 // WebContents during destruction. The WindowAndroid has stronger lifetime
344 // guarantees, and should be safe to use for observer detachment.
345 // This will be non-null iff |content_view_core_| is non-null.
346 ui::WindowAndroid
* content_view_core_window_android_
;
348 ImeAdapterAndroid ime_adapter_android_
;
350 // Body background color of the underlying document.
351 SkColor cached_background_color_
;
353 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
354 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
355 scoped_refptr
<cc::Layer
> layer_
;
357 scoped_ptr
<cc::SurfaceIdAllocator
> id_allocator_
;
358 scoped_ptr
<cc::SurfaceFactory
> surface_factory_
;
359 cc::SurfaceId surface_id_
;
360 gfx::Size current_surface_size_
;
361 cc::ReturnedResourceArray surface_returned_resources_
;
362 gfx::Vector2dF location_bar_content_translation_
;
363 cc::ViewportSelection current_viewport_selection_
;
365 // The most recent texture size that was pushed to the texture layer.
366 gfx::Size texture_size_in_layer_
;
368 // The output surface id of the last received frame.
369 uint32_t last_output_surface_id_
;
372 std::queue
<base::Closure
> ack_callbacks_
;
374 // Used to control and render overscroll-related effects.
375 scoped_ptr
<OverscrollControllerAndroid
> overscroll_controller_
;
377 // Provides gesture synthesis given a stream of touch events (derived from
378 // Android MotionEvent's) and touch event acks.
379 ui::FilteredGestureProvider gesture_provider_
;
381 // Handles gesture based text selection
382 StylusTextSelector stylus_text_selector_
;
384 // Manages selection handle rendering and manipulation.
385 // This will always be NULL if |content_view_core_| is NULL.
386 scoped_ptr
<ui::TouchSelectionController
> selection_controller_
;
388 int accelerated_surface_route_id_
;
390 // Size to use if we have no backing ContentViewCore
391 gfx::Size default_size_
;
393 const bool using_browser_compositor_
;
395 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
397 size_t locks_on_frame_count_
;
398 bool observing_root_window_
;
400 struct LastFrameInfo
{
401 LastFrameInfo(uint32 output_id
,
402 scoped_ptr
<cc::CompositorFrame
> output_frame
);
404 uint32 output_surface_id
;
405 scoped_ptr
<cc::CompositorFrame
> frame
;
408 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
410 TextSurroundingSelectionCallback text_surrounding_selection_callback_
;
412 // The last scroll offset of the view.
413 gfx::Vector2dF last_scroll_offset_
;
415 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
417 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
420 } // namespace content
422 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_