a5aa33c83fd579ce86b3a7b1d385bf99136deb89
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_view_android.h
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/delegated_frame_evictor.h"
22 #include "content/browser/renderer_host/image_transport_factory_android.h"
23 #include "content/browser/renderer_host/ime_adapter_android.h"
24 #include "content/browser/renderer_host/render_widget_host_view_base.h"
25 #include "gpu/command_buffer/common/mailbox.h"
26 #include "third_party/skia/include/core/SkColor.h"
27 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
28 #include "ui/base/android/window_android_observer.h"
29 #include "ui/gfx/size.h"
30 #include "ui/gfx/vector2d_f.h"
31
32 struct ViewHostMsg_TextInputState_Params;
33
34 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
35 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
36
37 namespace cc {
38 class CopyOutputResult;
39 class DelegatedFrameProvider;
40 class DelegatedRendererLayer;
41 class Layer;
42 class SingleReleaseCallback;
43 class TextureLayer;
44 }
45
46 namespace blink {
47 class WebExternalTextureLayer;
48 class WebTouchEvent;
49 class WebMouseEvent;
50 }
51
52 namespace content {
53 class ContentViewCoreImpl;
54 class OverscrollGlow;
55 class RenderWidgetHost;
56 class RenderWidgetHostImpl;
57 struct NativeWebKeyboardEvent;
58
59 // -----------------------------------------------------------------------------
60 // See comments in render_widget_host_view.h about this class and its members.
61 // -----------------------------------------------------------------------------
62 class RenderWidgetHostViewAndroid
63     : public RenderWidgetHostViewBase,
64       public BrowserAccessibilityDelegate,
65       public cc::DelegatedFrameResourceCollectionClient,
66       public ImageTransportFactoryAndroidObserver,
67       public ui::WindowAndroidObserver,
68       public DelegatedFrameEvictorClient {
69  public:
70   RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
71                               ContentViewCoreImpl* content_view_core);
72   virtual ~RenderWidgetHostViewAndroid();
73
74   // RenderWidgetHostView implementation.
75   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
76   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
77   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
78                            const gfx::Rect& pos) OVERRIDE;
79   virtual void InitAsFullscreen(
80       RenderWidgetHostView* reference_host_view) OVERRIDE;
81   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
82   virtual void WasShown() OVERRIDE;
83   virtual void WasHidden() OVERRIDE;
84   virtual void SetSize(const gfx::Size& size) OVERRIDE;
85   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
86   virtual gfx::NativeView GetNativeView() const OVERRIDE;
87   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
88   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
89   virtual void MovePluginWindows(
90       const gfx::Vector2d& scroll_offset,
91       const std::vector<WebPluginGeometry>& moves) OVERRIDE;
92   virtual void Focus() OVERRIDE;
93   virtual void Blur() OVERRIDE;
94   virtual bool HasFocus() const OVERRIDE;
95   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
96   virtual void Show() OVERRIDE;
97   virtual void Hide() OVERRIDE;
98   virtual bool IsShowing() OVERRIDE;
99   virtual gfx::Rect GetViewBounds() const OVERRIDE;
100   virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
101   virtual float GetOverdrawBottomHeight() const OVERRIDE;
102   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
103   virtual void SetIsLoading(bool is_loading) OVERRIDE;
104   virtual void TextInputTypeChanged(ui::TextInputType type,
105                                     ui::TextInputMode input_mode,
106                                     bool can_compose_inline) OVERRIDE;
107   virtual void ImeCancelComposition() OVERRIDE;
108   virtual void FocusedNodeChanged(bool is_editable_node) OVERRIDE;
109   virtual void DidUpdateBackingStore(
110       const gfx::Rect& scroll_rect,
111       const gfx::Vector2d& scroll_delta,
112       const std::vector<gfx::Rect>& copy_rects,
113       const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE;
114   virtual void RenderProcessGone(base::TerminationStatus status,
115                                  int error_code) OVERRIDE;
116   virtual void Destroy() OVERRIDE;
117   virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE;
118   virtual void SelectionChanged(const base::string16& text,
119                                 size_t offset,
120                                 const gfx::Range& range) OVERRIDE;
121   virtual void SelectionBoundsChanged(
122       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
123   virtual void SelectionRootBoundsChanged(const gfx::Rect& bounds) OVERRIDE;
124   virtual void ScrollOffsetChanged() OVERRIDE;
125   virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
126   virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
127   virtual void AcceleratedSurfaceInitialized(int host_id,
128                                              int route_id) OVERRIDE;
129   virtual void AcceleratedSurfaceBuffersSwapped(
130       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
131       int gpu_host_id) OVERRIDE;
132   virtual void AcceleratedSurfacePostSubBuffer(
133       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
134       int gpu_host_id) OVERRIDE;
135   virtual void AcceleratedSurfaceSuspend() OVERRIDE;
136   virtual void AcceleratedSurfaceRelease() OVERRIDE;
137   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
138   virtual void SetBackground(const SkBitmap& background) OVERRIDE;
139   virtual void CopyFromCompositingSurface(
140       const gfx::Rect& src_subrect,
141       const gfx::Size& dst_size,
142       const base::Callback<void(bool, const SkBitmap&)>& callback,
143       const SkBitmap::Config config) OVERRIDE;
144   virtual void CopyFromCompositingSurfaceToVideoFrame(
145       const gfx::Rect& src_subrect,
146       const scoped_refptr<media::VideoFrame>& target,
147       const base::Callback<void(bool)>& callback) OVERRIDE;
148   virtual bool CanCopyToVideoFrame() const OVERRIDE;
149   virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE;
150   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
151   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
152   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
153                                       InputEventAckState ack_result) OVERRIDE;
154   virtual void SetHasHorizontalScrollbar(
155       bool has_horizontal_scrollbar) OVERRIDE;
156   virtual void SetScrollOffsetPinning(
157       bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE;
158   virtual void UnhandledWheelEvent(
159       const blink::WebMouseWheelEvent& event) OVERRIDE;
160   virtual InputEventAckState FilterInputEvent(
161       const blink::WebInputEvent& input_event) OVERRIDE;
162   virtual void OnSetNeedsFlushInput() OVERRIDE;
163   virtual void GestureEventAck(const blink::WebGestureEvent& event,
164                                InputEventAckState ack_result) OVERRIDE;
165   virtual void CreateBrowserAccessibilityManagerIfNeeded() OVERRIDE;
166   virtual bool LockMouse() OVERRIDE;
167   virtual void UnlockMouse() OVERRIDE;
168   virtual void OnSwapCompositorFrame(
169       uint32 output_surface_id,
170       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
171   virtual void OnOverscrolled(gfx::Vector2dF accumulated_overscroll,
172                               gfx::Vector2dF current_fling_velocity) OVERRIDE;
173   virtual void DidStopFlinging() OVERRIDE;
174   virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
175                                        const SkBitmap& zoomed_bitmap) OVERRIDE;
176   virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
177       OVERRIDE;
178   virtual void LockCompositingSurface() OVERRIDE;
179   virtual void UnlockCompositingSurface() OVERRIDE;
180
181   // Implementation of BrowserAccessibilityDelegate:
182   virtual void SetAccessibilityFocus(int acc_obj_id) OVERRIDE;
183   virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
184   virtual void AccessibilityScrollToMakeVisible(
185       int acc_obj_id, gfx::Rect subfocus) OVERRIDE;
186   virtual void AccessibilityScrollToPoint(
187       int acc_obj_id, gfx::Point point) OVERRIDE;
188   virtual void AccessibilitySetTextSelection(
189       int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
190   virtual gfx::Point GetLastTouchEventLocation() const OVERRIDE;
191   virtual void FatalAccessibilityTreeError() OVERRIDE;
192
193   // cc::DelegatedFrameResourceCollectionClient implementation.
194   virtual void UnusedResourcesAreAvailable() OVERRIDE;
195
196   // ui::WindowAndroidObserver implementation.
197   virtual void OnCompositingDidCommit() OVERRIDE;
198   virtual void OnAttachCompositor() OVERRIDE {}
199   virtual void OnDetachCompositor() OVERRIDE;
200
201   // ImageTransportFactoryAndroidObserver implementation.
202   virtual void OnLostResources() OVERRIDE;
203
204   // DelegatedFrameEvictor implementation
205   virtual void EvictDelegatedFrame() OVERRIDE;
206
207   virtual SkBitmap::Config PreferredReadbackFormat() OVERRIDE;
208
209   // Non-virtual methods
210   void SetContentViewCore(ContentViewCoreImpl* content_view_core);
211   SkColor GetCachedBackgroundColor() const;
212   void SendKeyEvent(const NativeWebKeyboardEvent& event);
213   void SendTouchEvent(const blink::WebTouchEvent& event);
214   void SendMouseEvent(const blink::WebMouseEvent& event);
215   void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
216   void SendGestureEvent(const blink::WebGestureEvent& event);
217   void SendBeginFrame(const cc::BeginFrameArgs& args);
218
219   void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params& params);
220   void OnDidChangeBodyBackgroundColor(SkColor color);
221   void OnStartContentIntent(const GURL& content_url);
222   void OnSetNeedsBeginFrame(bool enabled);
223   void OnSmartClipDataExtracted(const base::string16& result);
224
225   long GetNativeImeAdapter();
226
227   void WasResized();
228
229   void GetScaledContentBitmap(
230       float scale,
231       SkBitmap::Config bitmap_config,
232       gfx::Rect src_subrect,
233       const base::Callback<void(bool, const SkBitmap&)>& result_callback);
234
235   bool HasValidFrame() const;
236
237   void MoveCaret(const gfx::Point& point);
238
239   // Returns true when animation ticks are still needed. This avoids a separate
240   // round-trip for requesting follow-up animation.
241   bool Animate(base::TimeTicks frame_time);
242
243   void SynchronousFrameMetadata(
244       const cc::CompositorFrameMetadata& frame_metadata);
245
246   void SetOverlayVideoMode(bool enabled);
247
248  private:
249   void BuffersSwapped(const gpu::Mailbox& mailbox,
250                       uint32_t output_surface_id,
251                       const base::Closure& ack_callback);
252
253   void RunAckCallbacks();
254
255   void DestroyDelegatedContent();
256   void SwapDelegatedFrame(uint32 output_surface_id,
257                           scoped_ptr<cc::DelegatedFrameData> frame_data);
258   void SendDelegatedFrameAck(uint32 output_surface_id);
259   void SendReturnedDelegatedResources(uint32 output_surface_id);
260
261   void UpdateContentViewCoreFrameMetadata(
262       const cc::CompositorFrameMetadata& frame_metadata);
263   void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
264   void ResetClipping();
265   void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
266
267   void AttachLayers();
268   void RemoveLayers();
269
270   void UpdateAnimationSize(const cc::CompositorFrameMetadata& frame_metadata);
271
272   // Called after async screenshot task completes. Scales and crops the result
273   // of the copy.
274   static void PrepareTextureCopyOutputResult(
275       const gfx::Size& dst_size_in_pixel,
276       const SkBitmap::Config config,
277       const base::TimeTicks& start_time,
278       const base::Callback<void(bool, const SkBitmap&)>& callback,
279       scoped_ptr<cc::CopyOutputResult> result);
280   static void PrepareTextureCopyOutputResultForDelegatedReadback(
281       const gfx::Size& dst_size_in_pixel,
282       const SkBitmap::Config config,
283       const base::TimeTicks& start_time,
284       scoped_refptr<cc::Layer> readback_layer,
285       const base::Callback<void(bool, const SkBitmap&)>& callback,
286       scoped_ptr<cc::CopyOutputResult> result);
287
288   // DevTools ScreenCast support for Android WebView.
289   void SynchronousCopyContents(
290       const gfx::Rect& src_subrect_in_pixel,
291       const gfx::Size& dst_size_in_pixel,
292       const base::Callback<void(bool, const SkBitmap&)>& callback,
293       const SkBitmap::Config config);
294
295   bool IsReadbackConfigSupported(SkBitmap::Config bitmap_config);
296
297   // If we have locks on a frame during a ContentViewCore swap or a context
298   // lost, the frame is no longer valid and we can safely release all the locks.
299   // Use this method to release all the locks.
300   void ReleaseLocksOnSurface();
301
302   // Drop any incoming frames from the renderer when there are locks on the
303   // current frame.
304   void RetainFrame(uint32 output_surface_id,
305                    scoped_ptr<cc::CompositorFrame> frame);
306
307   void InternalSwapCompositorFrame(uint32 output_surface_id,
308                                    scoped_ptr<cc::CompositorFrame> frame);
309
310   // The model object.
311   RenderWidgetHostImpl* host_;
312
313   // Used to track whether this render widget needs a BeginFrame.
314   bool needs_begin_frame_;
315
316   bool is_showing_;
317
318   // ContentViewCoreImpl is our interface to the view system.
319   ContentViewCoreImpl* content_view_core_;
320
321   ImeAdapterAndroid ime_adapter_android_;
322
323   // Body background color of the underlying document.
324   SkColor cached_background_color_;
325
326   // The texture layer for this view when using browser-side compositing.
327   scoped_refptr<cc::TextureLayer> texture_layer_;
328
329   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
330   scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
331   scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
332
333   // The layer used for rendering the contents of this view.
334   // It is either owned by texture_layer_ or surface_texture_transport_
335   // depending on the mode.
336   scoped_refptr<cc::Layer> layer_;
337
338   // The most recent texture id that was pushed to the texture layer.
339   unsigned int texture_id_in_layer_;
340
341   // The most recent texture size that was pushed to the texture layer.
342   gfx::Size texture_size_in_layer_;
343
344   // The most recent content size that was pushed to the texture layer.
345   gfx::Size content_size_in_layer_;
346
347   // The mailbox of the previously received frame.
348   gpu::Mailbox current_mailbox_;
349
350   // The output surface id of the last received frame.
351   uint32_t last_output_surface_id_;
352
353   base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
354
355   std::queue<base::Closure> ack_callbacks_;
356
357   const bool overscroll_effect_enabled_;
358   // Used to render overscroll overlays.
359   // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
360   scoped_ptr<OverscrollGlow> overscroll_effect_;
361
362   bool flush_input_requested_;
363
364   int accelerated_surface_route_id_;
365
366   // Size to use if we have no backing ContentViewCore
367   gfx::Size default_size_;
368
369   const bool using_synchronous_compositor_;
370
371   scoped_ptr<DelegatedFrameEvictor> frame_evictor_;
372
373   bool using_delegated_renderer_;
374
375   size_t locks_on_frame_count_;
376   bool root_window_destroyed_;
377
378   struct LastFrameInfo {
379     LastFrameInfo(uint32 output_id,
380                   scoped_ptr<cc::CompositorFrame> output_frame);
381     ~LastFrameInfo();
382     uint32 output_surface_id;
383     scoped_ptr<cc::CompositorFrame> frame;
384   };
385
386   scoped_ptr<LastFrameInfo> last_frame_info_;
387
388   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
389 };
390
391 } // namespace content
392
393 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_