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 // -----------------------------------------------------------------------------
62 // See comments in render_widget_host_view.h about this class and its members.
63 // -----------------------------------------------------------------------------
64 class CONTENT_EXPORT RenderWidgetHostViewAndroid
65 : public RenderWidgetHostViewBase,
66 public cc::DelegatedFrameResourceCollectionClient,
67 public ImageTransportFactoryAndroidObserver,
68 public ui::GestureProviderClient,
69 public ui::WindowAndroidObserver,
70 public DelegatedFrameEvictorClient,
71 public GestureTextSelectorClient,
72 public TouchSelectionControllerClient {
74 RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
75 ContentViewCoreImpl* content_view_core);
76 virtual ~RenderWidgetHostViewAndroid();
78 // RenderWidgetHostView implementation.
79 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
80 virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
81 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
82 const gfx::Rect& pos) OVERRIDE;
83 virtual void InitAsFullscreen(
84 RenderWidgetHostView* reference_host_view) OVERRIDE;
85 virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
86 virtual void WasShown() OVERRIDE;
87 virtual void WasHidden() OVERRIDE;
88 virtual void SetSize(const gfx::Size& size) OVERRIDE;
89 virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
90 virtual gfx::NativeView GetNativeView() const OVERRIDE;
91 virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
92 virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
93 virtual void MovePluginWindows(
94 const std::vector<WebPluginGeometry>& moves) OVERRIDE;
95 virtual void Focus() OVERRIDE;
96 virtual void Blur() OVERRIDE;
97 virtual bool HasFocus() const OVERRIDE;
98 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
99 virtual void Show() OVERRIDE;
100 virtual void Hide() OVERRIDE;
101 virtual bool IsShowing() OVERRIDE;
102 virtual gfx::Rect GetViewBounds() const OVERRIDE;
103 virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
104 virtual float GetOverdrawBottomHeight() const OVERRIDE;
105 virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
106 virtual void SetIsLoading(bool is_loading) OVERRIDE;
107 virtual void TextInputStateChanged(
108 const ViewHostMsg_TextInputState_Params& params) OVERRIDE;
109 virtual void ImeCancelComposition() OVERRIDE;
110 virtual void FocusedNodeChanged(bool is_editable_node) OVERRIDE;
111 virtual void RenderProcessGone(base::TerminationStatus status,
112 int error_code) OVERRIDE;
113 virtual void Destroy() OVERRIDE;
114 virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE;
115 virtual void SelectionChanged(const base::string16& text,
117 const gfx::Range& range) OVERRIDE;
118 virtual void SelectionBoundsChanged(
119 const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
120 virtual void ScrollOffsetChanged() OVERRIDE;
121 virtual void AcceleratedSurfaceInitialized(int host_id,
122 int route_id) OVERRIDE;
123 virtual void AcceleratedSurfaceBuffersSwapped(
124 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
125 int gpu_host_id) OVERRIDE;
126 virtual void AcceleratedSurfacePostSubBuffer(
127 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
128 int gpu_host_id) OVERRIDE;
129 virtual void AcceleratedSurfaceSuspend() OVERRIDE;
130 virtual void AcceleratedSurfaceRelease() OVERRIDE;
131 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
132 virtual void SetBackgroundOpaque(bool transparent) OVERRIDE;
133 virtual void CopyFromCompositingSurface(
134 const gfx::Rect& src_subrect,
135 const gfx::Size& dst_size,
136 const base::Callback<void(bool, const SkBitmap&)>& callback,
137 const SkColorType color_type) OVERRIDE;
138 virtual void CopyFromCompositingSurfaceToVideoFrame(
139 const gfx::Rect& src_subrect,
140 const scoped_refptr<media::VideoFrame>& target,
141 const base::Callback<void(bool)>& callback) OVERRIDE;
142 virtual bool CanCopyToVideoFrame() const OVERRIDE;
143 virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE;
144 virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
145 virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
146 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
147 InputEventAckState ack_result) OVERRIDE;
148 virtual InputEventAckState FilterInputEvent(
149 const blink::WebInputEvent& input_event) OVERRIDE;
150 virtual void OnSetNeedsFlushInput() OVERRIDE;
151 virtual void GestureEventAck(const blink::WebGestureEvent& event,
152 InputEventAckState ack_result) OVERRIDE;
153 virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
154 BrowserAccessibilityDelegate* delegate) OVERRIDE;
155 virtual bool LockMouse() OVERRIDE;
156 virtual void UnlockMouse() OVERRIDE;
157 virtual void OnSwapCompositorFrame(
158 uint32 output_surface_id,
159 scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
160 virtual void DidOverscroll(const DidOverscrollParams& params) OVERRIDE;
161 virtual void DidStopFlinging() OVERRIDE;
162 virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
163 const SkBitmap& zoomed_bitmap) OVERRIDE;
164 virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
166 virtual void LockCompositingSurface() OVERRIDE;
167 virtual void UnlockCompositingSurface() OVERRIDE;
168 virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
170 size_t end_offset) OVERRIDE;
172 // cc::DelegatedFrameResourceCollectionClient implementation.
173 virtual void UnusedResourcesAreAvailable() OVERRIDE;
175 // ui::GestureProviderClient implementation.
176 virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE;
178 // ui::WindowAndroidObserver implementation.
179 virtual void OnCompositingDidCommit() OVERRIDE;
180 virtual void OnAttachCompositor() OVERRIDE;
181 virtual void OnDetachCompositor() OVERRIDE;
182 virtual void OnVSync(base::TimeTicks frame_time,
183 base::TimeDelta vsync_period) OVERRIDE;
184 virtual void OnAnimate(base::TimeTicks begin_frame_time) OVERRIDE;
186 // ImageTransportFactoryAndroidObserver implementation.
187 virtual void OnLostResources() OVERRIDE;
189 // DelegatedFrameEvictor implementation
190 virtual void EvictDelegatedFrame() OVERRIDE;
192 virtual SkColorType PreferredReadbackFormat() OVERRIDE;
194 // GestureTextSelectorClient implementation.
195 virtual void ShowSelectionHandlesAutomatically() OVERRIDE;
196 virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE;
197 virtual void Unselect() OVERRIDE;
198 virtual void LongPress(base::TimeTicks time, float x, float y) OVERRIDE;
200 // Non-virtual methods
201 void SetContentViewCore(ContentViewCoreImpl* content_view_core);
202 SkColor GetCachedBackgroundColor() const;
203 void SendKeyEvent(const NativeWebKeyboardEvent& event);
204 void SendTouchEvent(const blink::WebTouchEvent& event);
205 void SendMouseEvent(const blink::WebMouseEvent& event);
206 void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
207 void SendGestureEvent(const blink::WebGestureEvent& event);
209 void OnDidChangeBodyBackgroundColor(SkColor color);
210 void OnStartContentIntent(const GURL& content_url);
211 void OnSetNeedsBeginFrame(bool enabled);
212 void OnSmartClipDataExtracted(const base::string16& text,
213 const base::string16& html,
214 const gfx::Rect rect);
216 bool OnTouchEvent(const ui::MotionEvent& event);
217 bool OnTouchHandleEvent(const ui::MotionEvent& event);
218 void ResetGestureDetection();
219 void SetDoubleTapSupportEnabled(bool enabled);
220 void SetMultiTouchZoomSupportEnabled(bool enabled);
222 long GetNativeImeAdapter();
226 void GetScaledContentBitmap(
228 SkColorType color_type,
229 gfx::Rect src_subrect,
230 const base::Callback<void(bool, const SkBitmap&)>& result_callback);
232 bool HasValidFrame() const;
234 void MoveCaret(const gfx::Point& point);
235 void HideTextHandles();
236 void OnShowingPastePopup(const gfx::PointF& point);
238 void SynchronousFrameMetadata(
239 const cc::CompositorFrameMetadata& frame_metadata);
241 void SetOverlayVideoMode(bool enabled);
243 typedef base::Callback<
244 void(const base::string16& content, int start_offset, int end_offset)>
245 TextSurroundingSelectionCallback;
246 void SetTextSurroundingSelectionCallback(
247 const TextSurroundingSelectionCallback& callback);
250 // TouchSelectionControllerClient implementation.
251 virtual bool SupportsAnimation() const OVERRIDE;
252 virtual void SetNeedsAnimate() OVERRIDE;
253 virtual void MoveCaret(const gfx::PointF& position) OVERRIDE;
254 virtual void SelectBetweenCoordinates(const gfx::PointF& start,
255 const gfx::PointF& end) OVERRIDE;
256 virtual void OnSelectionEvent(SelectionEventType event,
257 const gfx::PointF& anchor_position) OVERRIDE;
258 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() OVERRIDE;
260 void RunAckCallbacks();
262 void DestroyDelegatedContent();
263 void SwapDelegatedFrame(uint32 output_surface_id,
264 scoped_ptr<cc::DelegatedFrameData> frame_data);
265 void SendDelegatedFrameAck(uint32 output_surface_id);
266 void SendReturnedDelegatedResources(uint32 output_surface_id);
268 void OnFrameMetadataUpdated(
269 const cc::CompositorFrameMetadata& frame_metadata);
270 void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
275 // Called after async screenshot task completes. Scales and crops the result
277 static void PrepareTextureCopyOutputResult(
278 const gfx::Size& dst_size_in_pixel,
279 const SkColorType color_type,
280 const base::TimeTicks& start_time,
281 const base::Callback<void(bool, const SkBitmap&)>& callback,
282 scoped_ptr<cc::CopyOutputResult> result);
283 static void PrepareTextureCopyOutputResultForDelegatedReadback(
284 const gfx::Size& dst_size_in_pixel,
285 const SkColorType color_type,
286 const base::TimeTicks& start_time,
287 scoped_refptr<cc::Layer> readback_layer,
288 const base::Callback<void(bool, const SkBitmap&)>& callback,
289 scoped_ptr<cc::CopyOutputResult> result);
291 // DevTools ScreenCast support for Android WebView.
292 void SynchronousCopyContents(
293 const gfx::Rect& src_subrect_in_pixel,
294 const gfx::Size& dst_size_in_pixel,
295 const base::Callback<void(bool, const SkBitmap&)>& callback,
296 const SkColorType color_type);
298 bool IsReadbackConfigSupported(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);
313 enum VSyncRequestType {
314 FLUSH_INPUT = 1 << 0,
315 BEGIN_FRAME = 1 << 1,
316 PERSISTENT_BEGIN_FRAME = 1 << 2
318 void RequestVSyncUpdate(uint32 requests);
319 void StartObservingRootWindow();
320 void StopObservingRootWindow();
321 void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period);
322 bool Animate(base::TimeTicks frame_time);
324 void OnContentScrollingChange();
325 bool IsContentScrolling() const;
327 float GetDpiScale() const;
330 RenderWidgetHostImpl* host_;
332 // Used to control action dispatch at the next |OnVSync()| call.
333 uint32 outstanding_vsync_requests_;
337 // ContentViewCoreImpl is our interface to the view system.
338 ContentViewCoreImpl* content_view_core_;
340 ImeAdapterAndroid ime_adapter_android_;
342 // Body background color of the underlying document.
343 SkColor cached_background_color_;
345 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
346 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
347 scoped_refptr<cc::DelegatedRendererLayer> layer_;
349 // The most recent texture size that was pushed to the texture layer.
350 gfx::Size texture_size_in_layer_;
352 // The most recent content size that was pushed to the texture layer.
353 gfx::Size content_size_in_layer_;
355 // The output surface id of the last received frame.
356 uint32_t last_output_surface_id_;
358 base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
360 std::queue<base::Closure> ack_callbacks_;
362 const bool overscroll_effect_enabled_;
363 // Used to render overscroll overlays.
364 scoped_ptr<OverscrollGlow> overscroll_effect_;
366 // Provides gesture synthesis given a stream of touch events (derived from
367 // Android MotionEvent's) and touch event acks.
368 ui::FilteredGestureProvider gesture_provider_;
370 // Handles gesture based text selection
371 GestureTextSelector gesture_text_selector_;
373 // Manages selection handle rendering and manipulation.
374 // This will always be NULL if |content_view_core_| is NULL.
375 scoped_ptr<TouchSelectionController> selection_controller_;
376 bool touch_scrolling_;
377 size_t potentially_active_fling_count_;
379 int accelerated_surface_route_id_;
381 // Size to use if we have no backing ContentViewCore
382 gfx::Size default_size_;
384 const bool using_synchronous_compositor_;
386 scoped_ptr<DelegatedFrameEvictor> frame_evictor_;
388 size_t locks_on_frame_count_;
389 bool observing_root_window_;
391 struct LastFrameInfo {
392 LastFrameInfo(uint32 output_id,
393 scoped_ptr<cc::CompositorFrame> output_frame);
395 uint32 output_surface_id;
396 scoped_ptr<cc::CompositorFrame> frame;
399 scoped_ptr<LastFrameInfo> last_frame_info_;
401 TextSurroundingSelectionCallback text_surrounding_selection_callback_;
403 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
406 } // namespace content
408 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_