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/layers/texture_layer_client.h"
19#include "cc/output/begin_frame_args.h"
20#include "content/browser/accessibility/browser_accessibility_manager.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/render_widget_host_view_base.h"
24#include "gpu/command_buffer/common/mailbox.h"
25#include "third_party/skia/include/core/SkColor.h"
26#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
27#include "ui/base/android/window_android_observer.h"
28#include "ui/gfx/size.h"
29#include "ui/gfx/vector2d_f.h"
30
31struct ViewHostMsg_TextInputState_Params;
32
33struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
34struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
35
36namespace cc {
37class CopyOutputResult;
38class DelegatedFrameProvider;
39class DelegatedRendererLayer;
40class Layer;
41class SingleReleaseCallback;
42class TextureLayer;
43}
44
45namespace blink {
46class WebExternalTextureLayer;
47class WebTouchEvent;
48class WebMouseEvent;
49}
50
51namespace content {
52class ContentViewCoreImpl;
53class OverscrollGlow;
54class RenderWidgetHost;
55class RenderWidgetHostImpl;
56struct NativeWebKeyboardEvent;
57
58// -----------------------------------------------------------------------------
59// See comments in render_widget_host_view.h about this class and its members.
60// -----------------------------------------------------------------------------
61class RenderWidgetHostViewAndroid
62    : public RenderWidgetHostViewBase,
63      public BrowserAccessibilityDelegate,
64      public cc::DelegatedFrameResourceCollectionClient,
65      public ImageTransportFactoryAndroidObserver,
66      public ui::WindowAndroidObserver {
67 public:
68  RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
69                              ContentViewCoreImpl* content_view_core);
70  virtual ~RenderWidgetHostViewAndroid();
71
72  // RenderWidgetHostView implementation.
73  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
74  virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
75  virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
76                           const gfx::Rect& pos) OVERRIDE;
77  virtual void InitAsFullscreen(
78      RenderWidgetHostView* reference_host_view) OVERRIDE;
79  virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
80  virtual void WasShown() OVERRIDE;
81  virtual void WasHidden() OVERRIDE;
82  virtual void SetSize(const gfx::Size& size) OVERRIDE;
83  virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
84  virtual gfx::NativeView GetNativeView() const OVERRIDE;
85  virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
86  virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
87  virtual void MovePluginWindows(
88      const gfx::Vector2d& scroll_offset,
89      const std::vector<WebPluginGeometry>& moves) OVERRIDE;
90  virtual void Focus() OVERRIDE;
91  virtual void Blur() OVERRIDE;
92  virtual bool HasFocus() const OVERRIDE;
93  virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
94  virtual void Show() OVERRIDE;
95  virtual void Hide() OVERRIDE;
96  virtual bool IsShowing() OVERRIDE;
97  virtual gfx::Rect GetViewBounds() const OVERRIDE;
98  virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
99  virtual float GetOverdrawBottomHeight() const OVERRIDE;
100  virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
101  virtual void SetIsLoading(bool is_loading) OVERRIDE;
102  virtual void TextInputTypeChanged(ui::TextInputType type,
103                                    ui::TextInputMode input_mode,
104                                    bool can_compose_inline) OVERRIDE;
105  virtual void ImeCancelComposition() OVERRIDE;
106  virtual void DidUpdateBackingStore(
107      const gfx::Rect& scroll_rect,
108      const gfx::Vector2d& scroll_delta,
109      const std::vector<gfx::Rect>& copy_rects,
110      const ui::LatencyInfo& latency_info) 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,
116                                size_t offset,
117                                const gfx::Range& range) OVERRIDE;
118  virtual void SelectionBoundsChanged(
119      const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
120  virtual void ScrollOffsetChanged() OVERRIDE;
121  virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
122  virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
123  virtual void AcceleratedSurfaceInitialized(int host_id,
124                                             int route_id) OVERRIDE;
125  virtual void AcceleratedSurfaceBuffersSwapped(
126      const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
127      int gpu_host_id) OVERRIDE;
128  virtual void AcceleratedSurfacePostSubBuffer(
129      const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
130      int gpu_host_id) OVERRIDE;
131  virtual void AcceleratedSurfaceSuspend() OVERRIDE;
132  virtual void AcceleratedSurfaceRelease() OVERRIDE;
133  virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
134  virtual void SetBackground(const SkBitmap& background) OVERRIDE;
135  virtual void CopyFromCompositingSurface(
136      const gfx::Rect& src_subrect,
137      const gfx::Size& dst_size,
138      const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE;
139  virtual void CopyFromCompositingSurfaceToVideoFrame(
140      const gfx::Rect& src_subrect,
141      const scoped_refptr<media::VideoFrame>& target,
142      const base::Callback<void(bool)>& callback) OVERRIDE;
143  virtual bool CanCopyToVideoFrame() const OVERRIDE;
144  virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE;
145  virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
146  virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
147  virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
148                                      InputEventAckState ack_result) OVERRIDE;
149  virtual void SetHasHorizontalScrollbar(
150      bool has_horizontal_scrollbar) OVERRIDE;
151  virtual void SetScrollOffsetPinning(
152      bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE;
153  virtual void UnhandledWheelEvent(
154      const blink::WebMouseWheelEvent& event) OVERRIDE;
155  virtual InputEventAckState FilterInputEvent(
156      const blink::WebInputEvent& input_event) OVERRIDE;
157  virtual void OnSetNeedsFlushInput() OVERRIDE;
158  virtual void GestureEventAck(int gesture_event_type,
159                               InputEventAckState ack_result) OVERRIDE;
160  virtual void OnAccessibilityEvents(
161      const std::vector<AccessibilityHostMsg_EventParams>&
162          params) OVERRIDE;
163  virtual bool LockMouse() OVERRIDE;
164  virtual void UnlockMouse() OVERRIDE;
165  virtual void HasTouchEventHandlers(bool need_touch_events) OVERRIDE;
166  virtual void OnSwapCompositorFrame(
167      uint32 output_surface_id,
168      scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
169  virtual void OnOverscrolled(gfx::Vector2dF accumulated_overscroll,
170                              gfx::Vector2dF current_fling_velocity) OVERRIDE;
171  virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
172                                       const SkBitmap& zoomed_bitmap) OVERRIDE;
173  virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
174      OVERRIDE;
175
176  // Implementation of BrowserAccessibilityDelegate:
177  virtual void SetAccessibilityFocus(int acc_obj_id) OVERRIDE;
178  virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
179  virtual void AccessibilityScrollToMakeVisible(
180      int acc_obj_id, gfx::Rect subfocus) OVERRIDE;
181  virtual void AccessibilityScrollToPoint(
182      int acc_obj_id, gfx::Point point) OVERRIDE;
183  virtual void AccessibilitySetTextSelection(
184      int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
185  virtual gfx::Point GetLastTouchEventLocation() const OVERRIDE;
186  virtual void FatalAccessibilityTreeError() OVERRIDE;
187
188  // cc::DelegatedFrameResourceCollectionClient implementation.
189  virtual void UnusedResourcesAreAvailable() OVERRIDE;
190
191  // ui::WindowAndroidObserver implementation.
192  virtual void OnCompositingDidCommit() OVERRIDE;
193  virtual void OnAttachCompositor() OVERRIDE {}
194  virtual void OnDetachCompositor() OVERRIDE;
195
196  // ImageTransportFactoryAndroidObserver implementation.
197  virtual void OnLostResources() OVERRIDE;
198
199  // Non-virtual methods
200  void SetContentViewCore(ContentViewCoreImpl* content_view_core);
201  SkColor GetCachedBackgroundColor() const;
202  void SendKeyEvent(const NativeWebKeyboardEvent& event);
203  void SendTouchEvent(const blink::WebTouchEvent& event);
204  void SendMouseEvent(const blink::WebMouseEvent& event);
205  void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
206  void SendGestureEvent(const blink::WebGestureEvent& event);
207  void SendBeginFrame(const cc::BeginFrameArgs& args);
208
209  void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params& params);
210  void OnDidChangeBodyBackgroundColor(SkColor color);
211  void OnStartContentIntent(const GURL& content_url);
212  void OnSetNeedsBeginFrame(bool enabled);
213  void OnSmartClipDataExtracted(const string16& result);
214
215  int GetNativeImeAdapter();
216
217  void WasResized();
218
219  blink::WebGLId GetScaledContentTexture(float scale, gfx::Size* out_size);
220  bool PopulateBitmapWithContents(jobject jbitmap);
221
222  bool HasValidFrame() const;
223
224  // Select all text between the given coordinates.
225  void SelectRange(const gfx::Point& start, const gfx::Point& end);
226
227  void MoveCaret(const gfx::Point& point);
228
229  void RequestContentClipping(const gfx::Rect& clipping,
230                              const gfx::Size& content_size);
231
232  // Returns true when animation ticks are still needed. This avoids a separate
233  // round-trip for requesting follow-up animation.
234  bool Animate(base::TimeTicks frame_time);
235
236  void SynchronousFrameMetadata(
237      const cc::CompositorFrameMetadata& frame_metadata);
238
239 private:
240  void BuffersSwapped(const gpu::Mailbox& mailbox,
241                      uint32_t output_surface_id,
242                      const base::Closure& ack_callback);
243
244  void RunAckCallbacks();
245
246  void DestroyDelegatedContent();
247  void SwapDelegatedFrame(uint32 output_surface_id,
248                          scoped_ptr<cc::DelegatedFrameData> frame_data);
249  void SendDelegatedFrameAck(uint32 output_surface_id);
250
251  void UpdateContentViewCoreFrameMetadata(
252      const cc::CompositorFrameMetadata& frame_metadata);
253  void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
254  void ResetClipping();
255  void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
256
257  void AttachLayers();
258  void RemoveLayers();
259
260  void UpdateAnimationSize(const cc::CompositorFrameMetadata& frame_metadata);
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 base::Callback<void(bool, const SkBitmap&)>& callback,
267      scoped_ptr<cc::CopyOutputResult> result);
268  static void PrepareBitmapCopyOutputResult(
269      const gfx::Size& dst_size_in_pixel,
270      const base::Callback<void(bool, const SkBitmap&)>& callback,
271      scoped_ptr<cc::CopyOutputResult> result);
272
273  // DevTools ScreenCast support for Android WebView.
274  void SynchronousCopyContents(
275      const gfx::Rect& src_subrect_in_pixel,
276      const gfx::Size& dst_size_in_pixel,
277      const base::Callback<void(bool, const SkBitmap&)>& callback);
278
279  // The model object.
280  RenderWidgetHostImpl* host_;
281
282  // Used to track whether this render widget needs a BeginFrame.
283  bool needs_begin_frame_;
284
285  // Whether or not this widget is potentially attached to the view hierarchy.
286  // This view may not actually be attached if this is true, but it should be
287  // treated as such, because as soon as a ContentViewCore is set the layer
288  // will be attached automatically.
289  bool are_layers_attached_;
290
291  // ContentViewCoreImpl is our interface to the view system.
292  ContentViewCoreImpl* content_view_core_;
293
294  ImeAdapterAndroid ime_adapter_android_;
295
296  // Body background color of the underlying document.
297  SkColor cached_background_color_;
298
299  // The texture layer for this view when using browser-side compositing.
300  scoped_refptr<cc::TextureLayer> texture_layer_;
301
302  scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
303  scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
304  scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
305
306  // The layer used for rendering the contents of this view.
307  // It is either owned by texture_layer_ or surface_texture_transport_
308  // depending on the mode.
309  scoped_refptr<cc::Layer> layer_;
310
311  // The most recent texture id that was pushed to the texture layer.
312  unsigned int texture_id_in_layer_;
313
314  // The most recent texture size that was pushed to the texture layer.
315  gfx::Size texture_size_in_layer_;
316
317  // The most recent content size that was pushed to the texture layer.
318  gfx::Size content_size_in_layer_;
319
320  // The mailbox of the previously received frame.
321  gpu::Mailbox current_mailbox_;
322
323  // The output surface id of the last received frame.
324  uint32_t last_output_surface_id_;
325
326  base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
327
328  std::queue<base::Closure> ack_callbacks_;
329
330  const bool overscroll_effect_enabled_;
331  // Used to render overscroll overlays.
332  // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
333  scoped_ptr<OverscrollGlow> overscroll_effect_;
334
335  bool flush_input_requested_;
336
337  int accelerated_surface_route_id_;
338
339  // Size to use if we have no backing ContentViewCore
340  gfx::Size default_size_;
341
342  const bool using_synchronous_compositor_;
343
344  DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
345};
346
347} // namespace content
348
349#endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
350