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/ime_adapter_android.h"
22 #include "content/browser/renderer_host/input/stylus_text_selector.h"
23 #include "content/browser/renderer_host/render_widget_host_view_base.h"
24 #include "content/common/content_export.h"
25 #include "content/public/browser/readback_types.h"
26 #include "gpu/command_buffer/common/mailbox.h"
27 #include "third_party/skia/include/core/SkColor.h"
28 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
29 #include "ui/android/window_android_observer.h"
30 #include "ui/events/gesture_detection/filtered_gesture_provider.h"
31 #include "ui/gfx/geometry/size.h"
32 #include "ui/gfx/geometry/vector2d_f.h"
33 #include "ui/touch_selection/touch_selection_controller.h"
35 struct ViewHostMsg_TextInputState_Params
;
38 class CopyOutputResult
;
39 class DelegatedFrameProvider
;
40 class DelegatedRendererLayer
;
45 class WebExternalTextureLayer
;
51 class ContentViewCoreImpl
;
52 class OverscrollControllerAndroid
;
53 class RenderWidgetHost
;
54 class RenderWidgetHostImpl
;
55 struct DidOverscrollParams
;
56 struct NativeWebKeyboardEvent
;
58 class ReadbackRequest
{
60 explicit ReadbackRequest(float scale
,
61 SkColorType color_type
,
62 gfx::Rect src_subrect
,
63 ReadbackRequestCallback
& result_callback
);
65 float GetScale() { return scale_
; }
66 SkColorType
GetColorFormat() { return color_type_
; }
67 const gfx::Rect
GetCaptureRect() { return src_subrect_
; }
68 ReadbackRequestCallback
& GetResultCallback() { return result_callback_
; }
73 SkColorType color_type_
;
74 gfx::Rect src_subrect_
;
75 ReadbackRequestCallback result_callback_
;
78 // -----------------------------------------------------------------------------
79 // See comments in render_widget_host_view.h about this class and its members.
80 // -----------------------------------------------------------------------------
81 class CONTENT_EXPORT RenderWidgetHostViewAndroid
82 : public RenderWidgetHostViewBase
,
83 public cc::DelegatedFrameResourceCollectionClient
,
84 public ui::GestureProviderClient
,
85 public ui::WindowAndroidObserver
,
86 public DelegatedFrameEvictorClient
,
87 public StylusTextSelectorClient
,
88 public ui::TouchSelectionControllerClient
{
90 RenderWidgetHostViewAndroid(RenderWidgetHostImpl
* widget
,
91 ContentViewCoreImpl
* content_view_core
);
92 ~RenderWidgetHostViewAndroid() override
;
94 // RenderWidgetHostView implementation.
95 bool OnMessageReceived(const IPC::Message
& msg
) override
;
96 void InitAsChild(gfx::NativeView parent_view
) override
;
97 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
98 const gfx::Rect
& pos
) override
;
99 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
;
100 RenderWidgetHost
* GetRenderWidgetHost() const override
;
101 void SetSize(const gfx::Size
& size
) override
;
102 void SetBounds(const gfx::Rect
& rect
) override
;
103 gfx::Vector2dF
GetLastScrollOffset() const override
;
104 gfx::NativeView
GetNativeView() const override
;
105 gfx::NativeViewId
GetNativeViewId() const override
;
106 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
107 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
;
108 void Focus() override
;
109 void Blur() override
;
110 bool HasFocus() const override
;
111 bool IsSurfaceAvailableForCopy() const override
;
112 void Show() override
;
113 void Hide() override
;
114 bool IsShowing() override
;
115 gfx::Rect
GetViewBounds() const override
;
116 gfx::Size
GetPhysicalBackingSize() const override
;
117 bool DoTopControlsShrinkBlinkSize() const override
;
118 float GetTopControlsHeight() const override
;
119 void UpdateCursor(const WebCursor
& cursor
) override
;
120 void SetIsLoading(bool is_loading
) override
;
121 void TextInputTypeChanged(ui::TextInputType type
,
122 ui::TextInputMode input_mode
,
123 bool can_compose_inline
,
125 void ImeCancelComposition() override
;
126 void ImeCompositionRangeChanged(
127 const gfx::Range
& range
,
128 const std::vector
<gfx::Rect
>& character_bounds
) override
;
129 void FocusedNodeChanged(bool is_editable_node
) override
;
130 void RenderProcessGone(base::TerminationStatus status
,
131 int error_code
) override
;
132 void Destroy() override
;
133 void SetTooltipText(const base::string16
& tooltip_text
) override
;
134 void SelectionChanged(const base::string16
& text
,
136 const gfx::Range
& range
) override
;
137 void SelectionBoundsChanged(
138 const ViewHostMsg_SelectionBounds_Params
& params
) override
;
139 void AcceleratedSurfaceInitialized(int route_id
) override
;
140 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
141 void SetBackgroundColor(SkColor color
) override
;
142 void CopyFromCompositingSurface(const gfx::Rect
& src_subrect
,
143 const gfx::Size
& dst_size
,
144 ReadbackRequestCallback
& callback
,
145 const SkColorType color_type
) override
;
146 void CopyFromCompositingSurfaceToVideoFrame(
147 const gfx::Rect
& src_subrect
,
148 const scoped_refptr
<media::VideoFrame
>& target
,
149 const base::Callback
<void(bool)>& callback
) override
;
150 bool CanCopyToVideoFrame() const override
;
151 void GetScreenInfo(blink::WebScreenInfo
* results
) override
;
152 gfx::Rect
GetBoundsInRootWindow() override
;
153 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
154 void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
155 InputEventAckState ack_result
) override
;
156 InputEventAckState
FilterInputEvent(
157 const blink::WebInputEvent
& input_event
) override
;
158 void OnSetNeedsFlushInput() override
;
159 void GestureEventAck(const blink::WebGestureEvent
& event
,
160 InputEventAckState ack_result
) override
;
161 BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
162 BrowserAccessibilityDelegate
* delegate
) override
;
163 bool LockMouse() override
;
164 void UnlockMouse() override
;
165 void OnSwapCompositorFrame(uint32 output_surface_id
,
166 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
167 void DidOverscroll(const DidOverscrollParams
& params
) override
;
168 void DidStopFlinging() override
;
169 void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
170 const SkBitmap
& zoomed_bitmap
) override
;
171 scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget() override
;
172 void LockCompositingSurface() override
;
173 void UnlockCompositingSurface() override
;
174 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
176 size_t end_offset
) override
;
177 void OnDidNavigateMainFrameToNewPage() override
;
179 // cc::DelegatedFrameResourceCollectionClient implementation.
180 void UnusedResourcesAreAvailable() override
;
182 // ui::GestureProviderClient implementation.
183 void OnGestureEvent(const ui::GestureEventData
& gesture
) override
;
185 // ui::WindowAndroidObserver implementation.
186 void OnCompositingDidCommit() override
;
187 void OnAttachCompositor() override
;
188 void OnDetachCompositor() override
;
189 void OnVSync(base::TimeTicks frame_time
,
190 base::TimeDelta vsync_period
) override
;
191 void OnAnimate(base::TimeTicks begin_frame_time
) override
;
193 // DelegatedFrameEvictor implementation
194 void EvictDelegatedFrame() override
;
196 SkColorType
PreferredReadbackFormat() override
;
198 // StylusTextSelectorClient implementation.
199 void OnStylusSelectBegin(float x0
, float y0
, float x1
, float y1
) override
;
200 void OnStylusSelectUpdate(float x
, float y
) override
;
201 void OnStylusSelectEnd() override
;
202 void OnStylusSelectTap(base::TimeTicks time
, float x
, float y
) override
;
204 // ui::TouchSelectionControllerClient implementation.
205 bool SupportsAnimation() const override
;
206 void SetNeedsAnimate() override
;
207 void MoveCaret(const gfx::PointF
& position
) override
;
208 void MoveRangeSelectionExtent(const gfx::PointF
& extent
) override
;
209 void SelectBetweenCoordinates(const gfx::PointF
& base
,
210 const gfx::PointF
& extent
) override
;
211 void OnSelectionEvent(ui::SelectionEventType event
,
212 const gfx::PointF
& anchor_position
) override
;
213 scoped_ptr
<ui::TouchHandleDrawable
> CreateDrawable() override
;
215 // Non-virtual methods
216 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
217 SkColor
GetCachedBackgroundColor() const;
218 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
219 void SendMouseEvent(const blink::WebMouseEvent
& event
);
220 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
221 void SendGestureEvent(const blink::WebGestureEvent
& event
);
223 void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params
& params
);
224 void OnDidChangeBodyBackgroundColor(SkColor color
);
225 void OnStartContentIntent(const GURL
& content_url
);
226 void OnSetNeedsBeginFrames(bool enabled
);
227 void OnSmartClipDataExtracted(const base::string16
& text
,
228 const base::string16
& html
,
229 const gfx::Rect rect
);
231 bool OnTouchEvent(const ui::MotionEvent
& event
);
232 bool OnTouchHandleEvent(const ui::MotionEvent
& event
);
233 void ResetGestureDetection();
234 void SetDoubleTapSupportEnabled(bool enabled
);
235 void SetMultiTouchZoomSupportEnabled(bool enabled
);
237 long GetNativeImeAdapter();
241 void GetScaledContentBitmap(float scale
,
242 SkColorType color_type
,
243 gfx::Rect src_subrect
,
244 ReadbackRequestCallback
& result_callback
);
246 scoped_refptr
<cc::DelegatedRendererLayer
>
247 CreateDelegatedLayerForFrameProvider() const;
249 bool HasValidFrame() const;
251 void MoveCaret(const gfx::Point
& point
);
252 void DismissTextHandles();
253 void SetTextHandlesTemporarilyHidden(bool hidden
);
254 void OnShowingPastePopup(const gfx::PointF
& point
);
255 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
257 void SynchronousFrameMetadata(
258 const cc::CompositorFrameMetadata
& frame_metadata
);
260 void SetOverlayVideoMode(bool enabled
);
262 typedef base::Callback
<
263 void(const base::string16
& content
, int start_offset
, int end_offset
)>
264 TextSurroundingSelectionCallback
;
265 void SetTextSurroundingSelectionCallback(
266 const TextSurroundingSelectionCallback
& callback
);
268 static void OnContextLost();
271 void RunAckCallbacks();
273 void DestroyDelegatedContent();
274 void SwapDelegatedFrame(uint32 output_surface_id
,
275 scoped_ptr
<cc::DelegatedFrameData
> frame_data
);
276 void SendDelegatedFrameAck(uint32 output_surface_id
);
277 void SendReturnedDelegatedResources(uint32 output_surface_id
);
279 void OnFrameMetadataUpdated(
280 const cc::CompositorFrameMetadata
& frame_metadata
);
281 void ComputeContentsSize(const cc::CompositorFrameMetadata
& frame_metadata
);
286 // Called after async screenshot task completes. Scales and crops the result
288 static void PrepareTextureCopyOutputResult(
289 const gfx::Size
& dst_size_in_pixel
,
290 const SkColorType color_type
,
291 const base::TimeTicks
& start_time
,
292 ReadbackRequestCallback
& callback
,
293 scoped_ptr
<cc::CopyOutputResult
> result
);
294 static void PrepareTextureCopyOutputResultForDelegatedReadback(
295 const gfx::Size
& dst_size_in_pixel
,
296 const SkColorType color_type
,
297 const base::TimeTicks
& start_time
,
298 scoped_refptr
<cc::Layer
> readback_layer
,
299 ReadbackRequestCallback
& callback
,
300 scoped_ptr
<cc::CopyOutputResult
> result
);
302 // DevTools ScreenCast support for Android WebView.
303 void SynchronousCopyContents(const gfx::Rect
& src_subrect_in_pixel
,
304 const gfx::Size
& dst_size_in_pixel
,
305 ReadbackRequestCallback
& callback
,
306 const SkColorType color_type
);
308 // If we have locks on a frame during a ContentViewCore swap or a context
309 // lost, the frame is no longer valid and we can safely release all the locks.
310 // Use this method to release all the locks.
311 void ReleaseLocksOnSurface();
313 // Drop any incoming frames from the renderer when there are locks on the
315 void RetainFrame(uint32 output_surface_id
,
316 scoped_ptr
<cc::CompositorFrame
> frame
);
318 void InternalSwapCompositorFrame(uint32 output_surface_id
,
319 scoped_ptr
<cc::CompositorFrame
> frame
);
320 void OnLostResources();
322 enum VSyncRequestType
{
323 FLUSH_INPUT
= 1 << 0,
324 BEGIN_FRAME
= 1 << 1,
325 PERSISTENT_BEGIN_FRAME
= 1 << 2
327 void RequestVSyncUpdate(uint32 requests
);
328 void StartObservingRootWindow();
329 void StopObservingRootWindow();
330 void SendBeginFrame(base::TimeTicks frame_time
, base::TimeDelta vsync_period
);
331 bool Animate(base::TimeTicks frame_time
);
333 // Handles all unprocessed and pending readback requests.
334 void AbortPendingReadbackRequests();
337 RenderWidgetHostImpl
* host_
;
339 // Used to control action dispatch at the next |OnVSync()| call.
340 uint32 outstanding_vsync_requests_
;
344 // ContentViewCoreImpl is our interface to the view system.
345 ContentViewCoreImpl
* content_view_core_
;
347 ImeAdapterAndroid ime_adapter_android_
;
349 // Body background color of the underlying document.
350 SkColor cached_background_color_
;
352 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
353 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
354 scoped_refptr
<cc::DelegatedRendererLayer
> layer_
;
356 // The most recent texture size that was pushed to the texture layer.
357 gfx::Size texture_size_in_layer_
;
359 // The most recent content size that was pushed to the texture layer.
360 gfx::Size content_size_in_layer_
;
362 // The output surface id of the last received frame.
363 uint32_t last_output_surface_id_
;
366 std::queue
<base::Closure
> ack_callbacks_
;
368 // Used to control and render overscroll-related effects.
369 scoped_ptr
<OverscrollControllerAndroid
> overscroll_controller_
;
371 // Provides gesture synthesis given a stream of touch events (derived from
372 // Android MotionEvent's) and touch event acks.
373 ui::FilteredGestureProvider gesture_provider_
;
375 // Handles gesture based text selection
376 StylusTextSelector stylus_text_selector_
;
378 // Manages selection handle rendering and manipulation.
379 // This will always be NULL if |content_view_core_| is NULL.
380 scoped_ptr
<ui::TouchSelectionController
> selection_controller_
;
382 int accelerated_surface_route_id_
;
384 // Size to use if we have no backing ContentViewCore
385 gfx::Size default_size_
;
387 const bool using_browser_compositor_
;
389 scoped_ptr
<DelegatedFrameEvictor
> frame_evictor_
;
391 size_t locks_on_frame_count_
;
392 bool observing_root_window_
;
394 struct LastFrameInfo
{
395 LastFrameInfo(uint32 output_id
,
396 scoped_ptr
<cc::CompositorFrame
> output_frame
);
398 uint32 output_surface_id
;
399 scoped_ptr
<cc::CompositorFrame
> frame
;
402 scoped_ptr
<LastFrameInfo
> last_frame_info_
;
404 TextSurroundingSelectionCallback text_surrounding_selection_callback_
;
406 // List of readbackrequests waiting for arrival of a valid frame.
407 std::queue
<ReadbackRequest
> readbacks_waiting_for_frame_
;
409 // The last scroll offset of the view.
410 gfx::Vector2dF last_scroll_offset_
;
412 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
414 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
417 } // namespace content
419 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_