render_widget_host_view_android.h revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
6#define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
7
8#include <map>
9#include <queue>
10
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/render_widget_host_view_base.h"
25#include "content/common/content_export.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/base/android/window_android_observer.h"
30#include "ui/events/gesture_detection/filtered_gesture_provider.h"
31#include "ui/gfx/size.h"
32#include "ui/gfx/vector2d_f.h"
33
34struct ViewHostMsg_TextInputState_Params;
35
36struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
37struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
38
39namespace cc {
40class CopyOutputResult;
41class DelegatedFrameProvider;
42class DelegatedRendererLayer;
43class Layer;
44}
45
46namespace blink {
47class WebExternalTextureLayer;
48class WebTouchEvent;
49class WebMouseEvent;
50}
51
52namespace content {
53class ContentViewCoreImpl;
54class OverscrollGlow;
55class RenderWidgetHost;
56class RenderWidgetHostImpl;
57struct DidOverscrollParams;
58struct NativeWebKeyboardEvent;
59
60// -----------------------------------------------------------------------------
61// See comments in render_widget_host_view.h about this class and its members.
62// -----------------------------------------------------------------------------
63class CONTENT_EXPORT RenderWidgetHostViewAndroid
64    : public RenderWidgetHostViewBase,
65      public cc::DelegatedFrameResourceCollectionClient,
66      public ImageTransportFactoryAndroidObserver,
67      public ui::GestureProviderClient,
68      public ui::WindowAndroidObserver,
69      public DelegatedFrameEvictorClient,
70      public GestureTextSelectorClient {
71 public:
72  RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
73                              ContentViewCoreImpl* content_view_core);
74  virtual ~RenderWidgetHostViewAndroid();
75
76  // RenderWidgetHostView implementation.
77  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
78  virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
79  virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
80                           const gfx::Rect& pos) OVERRIDE;
81  virtual void InitAsFullscreen(
82      RenderWidgetHostView* reference_host_view) OVERRIDE;
83  virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
84  virtual void WasShown() OVERRIDE;
85  virtual void WasHidden() OVERRIDE;
86  virtual void SetSize(const gfx::Size& size) OVERRIDE;
87  virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
88  virtual gfx::NativeView GetNativeView() const OVERRIDE;
89  virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
90  virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
91  virtual void MovePluginWindows(
92      const std::vector<WebPluginGeometry>& moves) OVERRIDE;
93  virtual void Focus() OVERRIDE;
94  virtual void Blur() OVERRIDE;
95  virtual bool HasFocus() const OVERRIDE;
96  virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
97  virtual void Show() OVERRIDE;
98  virtual void Hide() OVERRIDE;
99  virtual bool IsShowing() OVERRIDE;
100  virtual gfx::Rect GetViewBounds() const OVERRIDE;
101  virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
102  virtual float GetOverdrawBottomHeight() const OVERRIDE;
103  virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
104  virtual void SetIsLoading(bool is_loading) OVERRIDE;
105  virtual void TextInputStateChanged(
106      const ViewHostMsg_TextInputState_Params& params) OVERRIDE;
107  virtual void ImeCancelComposition() OVERRIDE;
108  virtual void FocusedNodeChanged(bool is_editable_node) OVERRIDE;
109  virtual void RenderProcessGone(base::TerminationStatus status,
110                                 int error_code) OVERRIDE;
111  virtual void Destroy() OVERRIDE;
112  virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE;
113  virtual void SelectionChanged(const base::string16& text,
114                                size_t offset,
115                                const gfx::Range& range) OVERRIDE;
116  virtual void SelectionBoundsChanged(
117      const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
118  virtual void ScrollOffsetChanged() OVERRIDE;
119  virtual void AcceleratedSurfaceInitialized(int host_id,
120                                             int route_id) OVERRIDE;
121  virtual void AcceleratedSurfaceBuffersSwapped(
122      const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
123      int gpu_host_id) OVERRIDE;
124  virtual void AcceleratedSurfacePostSubBuffer(
125      const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
126      int gpu_host_id) OVERRIDE;
127  virtual void AcceleratedSurfaceSuspend() OVERRIDE;
128  virtual void AcceleratedSurfaceRelease() OVERRIDE;
129  virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
130  virtual void SetBackgroundOpaque(bool transparent) OVERRIDE;
131  virtual void CopyFromCompositingSurface(
132      const gfx::Rect& src_subrect,
133      const gfx::Size& dst_size,
134      const base::Callback<void(bool, const SkBitmap&)>& callback,
135      const SkColorType color_type) OVERRIDE;
136  virtual void CopyFromCompositingSurfaceToVideoFrame(
137      const gfx::Rect& src_subrect,
138      const scoped_refptr<media::VideoFrame>& target,
139      const base::Callback<void(bool)>& callback) OVERRIDE;
140  virtual bool CanCopyToVideoFrame() const OVERRIDE;
141  virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE;
142  virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
143  virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
144  virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
145                                      InputEventAckState ack_result) OVERRIDE;
146  virtual InputEventAckState FilterInputEvent(
147      const blink::WebInputEvent& input_event) OVERRIDE;
148  virtual void OnSetNeedsFlushInput() OVERRIDE;
149  virtual void GestureEventAck(const blink::WebGestureEvent& event,
150                               InputEventAckState ack_result) OVERRIDE;
151  virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
152      BrowserAccessibilityDelegate* delegate) OVERRIDE;
153  virtual bool LockMouse() OVERRIDE;
154  virtual void UnlockMouse() OVERRIDE;
155  virtual void OnSwapCompositorFrame(
156      uint32 output_surface_id,
157      scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
158  virtual void DidOverscroll(const DidOverscrollParams& params) OVERRIDE;
159  virtual void DidStopFlinging() OVERRIDE;
160  virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
161                                       const SkBitmap& zoomed_bitmap) OVERRIDE;
162  virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
163      OVERRIDE;
164  virtual void LockCompositingSurface() OVERRIDE;
165  virtual void UnlockCompositingSurface() OVERRIDE;
166  virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
167                                                  size_t start_offset,
168                                                  size_t end_offset) OVERRIDE;
169
170  // cc::DelegatedFrameResourceCollectionClient implementation.
171  virtual void UnusedResourcesAreAvailable() OVERRIDE;
172
173  // ui::GestureProviderClient implementation.
174  virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE;
175
176  // ui::WindowAndroidObserver implementation.
177  virtual void OnCompositingDidCommit() OVERRIDE;
178  virtual void OnAttachCompositor() OVERRIDE {}
179  virtual void OnDetachCompositor() OVERRIDE;
180  virtual void OnVSync(base::TimeTicks frame_time,
181                       base::TimeDelta vsync_period) OVERRIDE;
182  virtual void OnAnimate(base::TimeTicks begin_frame_time) OVERRIDE;
183
184  // ImageTransportFactoryAndroidObserver implementation.
185  virtual void OnLostResources() OVERRIDE;
186
187  // DelegatedFrameEvictor implementation
188  virtual void EvictDelegatedFrame() OVERRIDE;
189
190  virtual SkColorType PreferredReadbackFormat() OVERRIDE;
191
192  // GestureTextSelectorClient implementation.
193  virtual void ShowSelectionHandlesAutomatically() OVERRIDE;
194  virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE;
195  virtual void Unselect() OVERRIDE;
196  virtual void LongPress(base::TimeTicks time, float x, float y) OVERRIDE;
197
198  // Non-virtual methods
199  void SetContentViewCore(ContentViewCoreImpl* content_view_core);
200  SkColor GetCachedBackgroundColor() const;
201  void SendKeyEvent(const NativeWebKeyboardEvent& event);
202  void SendTouchEvent(const blink::WebTouchEvent& event);
203  void SendMouseEvent(const blink::WebMouseEvent& event);
204  void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
205  void SendGestureEvent(const blink::WebGestureEvent& event);
206
207  void OnDidChangeBodyBackgroundColor(SkColor color);
208  void OnStartContentIntent(const GURL& content_url);
209  void OnSetNeedsBeginFrame(bool enabled);
210  void OnSmartClipDataExtracted(const base::string16& text,
211                                const base::string16& html,
212                                const gfx::Rect rect);
213
214  bool OnTouchEvent(const ui::MotionEvent& event);
215  void ResetGestureDetection();
216  void SetDoubleTapSupportEnabled(bool enabled);
217  void SetMultiTouchZoomSupportEnabled(bool enabled);
218
219  long GetNativeImeAdapter();
220
221  void WasResized();
222
223  void GetScaledContentBitmap(
224      float scale,
225      SkColorType color_type,
226      gfx::Rect src_subrect,
227      const base::Callback<void(bool, const SkBitmap&)>& result_callback);
228
229  bool HasValidFrame() const;
230
231  void MoveCaret(const gfx::Point& point);
232
233  void SynchronousFrameMetadata(
234      const cc::CompositorFrameMetadata& frame_metadata);
235
236  void SetOverlayVideoMode(bool enabled);
237
238  typedef base::Callback<
239      void(const base::string16& content, int start_offset, int end_offset)>
240      TextSurroundingSelectionCallback;
241  void SetTextSurroundingSelectionCallback(
242      const TextSurroundingSelectionCallback& callback);
243
244 private:
245  void RunAckCallbacks();
246
247  void DestroyDelegatedContent();
248  void SwapDelegatedFrame(uint32 output_surface_id,
249                          scoped_ptr<cc::DelegatedFrameData> frame_data);
250  void SendDelegatedFrameAck(uint32 output_surface_id);
251  void SendReturnedDelegatedResources(uint32 output_surface_id);
252
253  void OnFrameMetadataUpdated(
254      const cc::CompositorFrameMetadata& frame_metadata);
255  void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
256  void ResetClipping();
257  void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
258
259  void AttachLayers();
260  void RemoveLayers();
261
262  // Called after async screenshot task completes. Scales and crops the result
263  // of the copy.
264  static void PrepareTextureCopyOutputResult(
265      const gfx::Size& dst_size_in_pixel,
266      const SkColorType color_type,
267      const base::TimeTicks& start_time,
268      const base::Callback<void(bool, const SkBitmap&)>& callback,
269      scoped_ptr<cc::CopyOutputResult> result);
270  static void PrepareTextureCopyOutputResultForDelegatedReadback(
271      const gfx::Size& dst_size_in_pixel,
272      const SkColorType color_type,
273      const base::TimeTicks& start_time,
274      scoped_refptr<cc::Layer> readback_layer,
275      const base::Callback<void(bool, const SkBitmap&)>& callback,
276      scoped_ptr<cc::CopyOutputResult> result);
277
278  // DevTools ScreenCast support for Android WebView.
279  void SynchronousCopyContents(
280      const gfx::Rect& src_subrect_in_pixel,
281      const gfx::Size& dst_size_in_pixel,
282      const base::Callback<void(bool, const SkBitmap&)>& callback,
283      const SkColorType color_type);
284
285  bool IsReadbackConfigSupported(SkColorType color_type);
286
287  // If we have locks on a frame during a ContentViewCore swap or a context
288  // lost, the frame is no longer valid and we can safely release all the locks.
289  // Use this method to release all the locks.
290  void ReleaseLocksOnSurface();
291
292  // Drop any incoming frames from the renderer when there are locks on the
293  // current frame.
294  void RetainFrame(uint32 output_surface_id,
295                   scoped_ptr<cc::CompositorFrame> frame);
296
297  void InternalSwapCompositorFrame(uint32 output_surface_id,
298                                   scoped_ptr<cc::CompositorFrame> frame);
299
300  void SetNeedsAnimate();
301  bool Animate(base::TimeTicks frame_time);
302
303  // The model object.
304  RenderWidgetHostImpl* host_;
305
306  // Used to track whether this render widget needs a BeginFrame.
307  bool needs_begin_frame_;
308
309  bool is_showing_;
310
311  // ContentViewCoreImpl is our interface to the view system.
312  ContentViewCoreImpl* content_view_core_;
313
314  ImeAdapterAndroid ime_adapter_android_;
315
316  // Body background color of the underlying document.
317  SkColor cached_background_color_;
318
319  scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
320  scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
321  scoped_refptr<cc::DelegatedRendererLayer> layer_;
322
323  // The most recent texture size that was pushed to the texture layer.
324  gfx::Size texture_size_in_layer_;
325
326  // The most recent content size that was pushed to the texture layer.
327  gfx::Size content_size_in_layer_;
328
329  // The device scale of the last received frame.
330  float device_scale_factor_;
331
332  // The output surface id of the last received frame.
333  uint32_t last_output_surface_id_;
334
335  base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
336
337  std::queue<base::Closure> ack_callbacks_;
338
339  const bool overscroll_effect_enabled_;
340  // Used to render overscroll overlays.
341  // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
342  scoped_ptr<OverscrollGlow> overscroll_effect_;
343
344  // Provides gesture synthesis given a stream of touch events (derived from
345  // Android MotionEvent's) and touch event acks.
346  ui::FilteredGestureProvider gesture_provider_;
347
348  // Handles gesture based text selection
349  GestureTextSelector gesture_text_selector_;
350
351  bool flush_input_requested_;
352
353  int accelerated_surface_route_id_;
354
355  // Size to use if we have no backing ContentViewCore
356  gfx::Size default_size_;
357
358  const bool using_synchronous_compositor_;
359
360  scoped_ptr<DelegatedFrameEvictor> frame_evictor_;
361
362  size_t locks_on_frame_count_;
363  bool observing_root_window_;
364
365  struct LastFrameInfo {
366    LastFrameInfo(uint32 output_id,
367                  scoped_ptr<cc::CompositorFrame> output_frame);
368    ~LastFrameInfo();
369    uint32 output_surface_id;
370    scoped_ptr<cc::CompositorFrame> frame;
371  };
372
373  scoped_ptr<LastFrameInfo> last_frame_info_;
374
375  TextSurroundingSelectionCallback text_surrounding_selection_callback_;
376
377  DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
378};
379
380} // namespace content
381
382#endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
383