Home | History | Annotate | Download | only in renderer_host
      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_renderer_layer_client.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/gfx/size.h"
     28 #include "ui/gfx/vector2d_f.h"
     29 
     30 struct ViewHostMsg_TextInputState_Params;
     31 
     32 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
     33 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
     34 
     35 namespace cc {
     36 class CopyOutputResult;
     37 class DelegatedRendererLayer;
     38 class Layer;
     39 class TextureLayer;
     40 }
     41 
     42 namespace WebKit {
     43 class WebExternalTextureLayer;
     44 class WebTouchEvent;
     45 class WebMouseEvent;
     46 }
     47 
     48 namespace content {
     49 class ContentViewCoreImpl;
     50 class OverscrollGlow;
     51 class RenderWidgetHost;
     52 class RenderWidgetHostImpl;
     53 class SurfaceTextureTransportClient;
     54 struct NativeWebKeyboardEvent;
     55 
     56 // -----------------------------------------------------------------------------
     57 // See comments in render_widget_host_view.h about this class and its members.
     58 // -----------------------------------------------------------------------------
     59 class RenderWidgetHostViewAndroid
     60     : public RenderWidgetHostViewBase,
     61       public BrowserAccessibilityDelegate,
     62       public cc::TextureLayerClient,
     63       public cc::DelegatedRendererLayerClient,
     64       public ImageTransportFactoryAndroidObserver {
     65  public:
     66   RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
     67                               ContentViewCoreImpl* content_view_core);
     68   virtual ~RenderWidgetHostViewAndroid();
     69 
     70   // RenderWidgetHostView implementation.
     71   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
     72   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
     73   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
     74                            const gfx::Rect& pos) OVERRIDE;
     75   virtual void InitAsFullscreen(
     76       RenderWidgetHostView* reference_host_view) OVERRIDE;
     77   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
     78   virtual void WasShown() OVERRIDE;
     79   virtual void WasHidden() OVERRIDE;
     80   virtual void SetSize(const gfx::Size& size) OVERRIDE;
     81   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
     82   virtual gfx::NativeView GetNativeView() const OVERRIDE;
     83   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
     84   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
     85   virtual void MovePluginWindows(
     86       const gfx::Vector2d& scroll_offset,
     87       const std::vector<WebPluginGeometry>& moves) OVERRIDE;
     88   virtual void Focus() OVERRIDE;
     89   virtual void Blur() OVERRIDE;
     90   virtual bool HasFocus() const OVERRIDE;
     91   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
     92   virtual void Show() OVERRIDE;
     93   virtual void Hide() OVERRIDE;
     94   virtual bool IsShowing() OVERRIDE;
     95   virtual gfx::Rect GetViewBounds() const OVERRIDE;
     96   virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
     97   virtual float GetOverdrawBottomHeight() const OVERRIDE;
     98   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
     99   virtual void SetIsLoading(bool is_loading) OVERRIDE;
    100   virtual void TextInputTypeChanged(ui::TextInputType type,
    101                                     bool can_compose_inline,
    102                                     ui::TextInputMode input_mode) OVERRIDE;
    103   virtual void ImeCancelComposition() OVERRIDE;
    104   virtual void DidUpdateBackingStore(
    105       const gfx::Rect& scroll_rect,
    106       const gfx::Vector2d& scroll_delta,
    107       const std::vector<gfx::Rect>& copy_rects,
    108       const ui::LatencyInfo& latency_info) OVERRIDE;
    109   virtual void RenderProcessGone(base::TerminationStatus status,
    110                                  int error_code) OVERRIDE;
    111   virtual void Destroy() OVERRIDE;
    112   virtual void SetTooltipText(const string16& tooltip_text) OVERRIDE;
    113   virtual void SelectionChanged(const string16& text,
    114                                 size_t offset,
    115                                 const ui::Range& range) OVERRIDE;
    116   virtual void SelectionBoundsChanged(
    117       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
    118   virtual void ScrollOffsetChanged() OVERRIDE;
    119   virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
    120   virtual void OnAcceleratedCompositingStateChange() 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 SetBackground(const SkBitmap& background) 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) OVERRIDE;
    135   virtual void CopyFromCompositingSurfaceToVideoFrame(
    136       const gfx::Rect& src_subrect,
    137       const scoped_refptr<media::VideoFrame>& target,
    138       const base::Callback<void(bool)>& callback) OVERRIDE;
    139   virtual bool CanCopyToVideoFrame() const OVERRIDE;
    140   virtual void GetScreenInfo(WebKit::WebScreenInfo* results) OVERRIDE;
    141   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
    142   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
    143   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
    144                                       InputEventAckState ack_result) OVERRIDE;
    145   virtual void SetHasHorizontalScrollbar(
    146       bool has_horizontal_scrollbar) OVERRIDE;
    147   virtual void SetScrollOffsetPinning(
    148       bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE;
    149   virtual void UnhandledWheelEvent(
    150       const WebKit::WebMouseWheelEvent& event) OVERRIDE;
    151   virtual InputEventAckState FilterInputEvent(
    152       const WebKit::WebInputEvent& input_event) OVERRIDE;
    153   virtual void GestureEventAck(int gesture_event_type,
    154                                InputEventAckState ack_result) OVERRIDE;
    155   virtual void OnAccessibilityNotifications(
    156       const std::vector<AccessibilityHostMsg_NotificationParams>&
    157           params) OVERRIDE;
    158   virtual bool LockMouse() OVERRIDE;
    159   virtual void UnlockMouse() OVERRIDE;
    160   virtual void HasTouchEventHandlers(bool need_touch_events) OVERRIDE;
    161   virtual void OnSwapCompositorFrame(
    162       uint32 output_surface_id,
    163       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
    164   virtual void OnOverscrolled(gfx::Vector2dF accumulated_overscroll,
    165                               gfx::Vector2dF current_fling_velocity) OVERRIDE;
    166   virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
    167                                        const SkBitmap& zoomed_bitmap) OVERRIDE;
    168   virtual SmoothScrollGesture* CreateSmoothScrollGesture(
    169       bool scroll_down, int pixels_to_scroll, int mouse_event_x,
    170       int mouse_event_y) OVERRIDE;
    171 
    172   // Implementation of BrowserAccessibilityDelegate:
    173   virtual void SetAccessibilityFocus(int acc_obj_id) OVERRIDE;
    174   virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
    175   virtual void AccessibilityScrollToMakeVisible(
    176       int acc_obj_id, gfx::Rect subfocus) OVERRIDE;
    177   virtual void AccessibilityScrollToPoint(
    178       int acc_obj_id, gfx::Point point) OVERRIDE;
    179   virtual void AccessibilitySetTextSelection(
    180       int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
    181   virtual gfx::Point GetLastTouchEventLocation() const OVERRIDE;
    182   virtual void FatalAccessibilityTreeError() OVERRIDE;
    183 
    184   // cc::TextureLayerClient implementation.
    185   virtual unsigned PrepareTexture() OVERRIDE;
    186   virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE;
    187   virtual bool PrepareTextureMailbox(cc::TextureMailbox* mailbox,
    188                                      bool use_shared_memory) OVERRIDE;
    189 
    190   // cc::DelegatedRendererLayerClient implementation.
    191   virtual void DidCommitFrameData() OVERRIDE;
    192 
    193   // ImageTransportFactoryAndroidObserver implementation.
    194   virtual void OnLostResources() OVERRIDE;
    195 
    196   // Non-virtual methods
    197   void SetContentViewCore(ContentViewCoreImpl* content_view_core);
    198   SkColor GetCachedBackgroundColor() const;
    199   void SendKeyEvent(const NativeWebKeyboardEvent& event);
    200   void SendTouchEvent(const WebKit::WebTouchEvent& event);
    201   void SendMouseEvent(const WebKit::WebMouseEvent& event);
    202   void SendMouseWheelEvent(const WebKit::WebMouseWheelEvent& event);
    203   void SendGestureEvent(const WebKit::WebGestureEvent& event);
    204   void SendBeginFrame(const cc::BeginFrameArgs& args);
    205 
    206   void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params& params);
    207   void OnProcessImeBatchStateAck(bool is_begin);
    208   void OnDidChangeBodyBackgroundColor(SkColor color);
    209   void OnStartContentIntent(const GURL& content_url);
    210   void OnSetNeedsBeginFrame(bool enabled);
    211 
    212   int GetNativeImeAdapter();
    213 
    214   void WasResized();
    215 
    216   WebKit::WebGLId GetScaledContentTexture(float scale, gfx::Size* out_size);
    217   bool PopulateBitmapWithContents(jobject jbitmap);
    218 
    219   bool HasValidFrame() const;
    220 
    221   // Select all text between the given coordinates.
    222   void SelectRange(const gfx::Point& start, const gfx::Point& end);
    223 
    224   void MoveCaret(const gfx::Point& point);
    225 
    226   void RequestContentClipping(const gfx::Rect& clipping,
    227                               const gfx::Size& content_size);
    228 
    229   // Returns true when animation ticks are still needed. This avoids a separate
    230   // round-trip for requesting follow-up animation.
    231   bool Animate(base::TimeTicks frame_time);
    232 
    233   void SynchronousFrameMetadata(
    234       const cc::CompositorFrameMetadata& frame_metadata);
    235 
    236  private:
    237   void BuffersSwapped(const gpu::Mailbox& mailbox,
    238                       uint32_t output_surface_id,
    239                       const base::Closure& ack_callback);
    240 
    241   void RunAckCallbacks();
    242 
    243   void SwapDelegatedFrame(uint32 output_surface_id,
    244                           scoped_ptr<cc::DelegatedFrameData> frame_data);
    245   void SendDelegatedFrameAck(uint32 output_surface_id);
    246 
    247   void UpdateContentViewCoreFrameMetadata(
    248       const cc::CompositorFrameMetadata& frame_metadata);
    249   void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
    250   void ResetClipping();
    251   void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
    252 
    253   void AttachLayers();
    254   void RemoveLayers();
    255 
    256   void CreateOverscrollEffectIfNecessary();
    257   void UpdateAnimationSize(const cc::CompositorFrameMetadata& frame_metadata);
    258   void ScheduleAnimationIfNecessary();
    259 
    260   // Called after async screenshot task completes. Scales and crops the result
    261   // of the copy.
    262   static void PrepareTextureCopyOutputResult(
    263       const gfx::Size& dst_size_in_pixel,
    264       const base::Callback<void(bool, const SkBitmap&)>& callback,
    265       scoped_ptr<cc::CopyOutputResult> result);
    266   static void PrepareBitmapCopyOutputResult(
    267       const gfx::Size& dst_size_in_pixel,
    268       const base::Callback<void(bool, const SkBitmap&)>& callback,
    269       scoped_ptr<cc::CopyOutputResult> result);
    270 
    271   // The model object.
    272   RenderWidgetHostImpl* host_;
    273 
    274   // Used to track whether this render widget needs a BeginFrame.
    275   bool needs_begin_frame_;
    276 
    277   // Whether or not this widget is potentially attached to the view hierarchy.
    278   // This view may not actually be attached if this is true, but it should be
    279   // treated as such, because as soon as a ContentViewCore is set the layer
    280   // will be attached automatically.
    281   bool are_layers_attached_;
    282 
    283   // ContentViewCoreImpl is our interface to the view system.
    284   ContentViewCoreImpl* content_view_core_;
    285 
    286   ImeAdapterAndroid ime_adapter_android_;
    287 
    288   // Body background color of the underlying document.
    289   SkColor cached_background_color_;
    290 
    291   // The texture layer for this view when using browser-side compositing.
    292   scoped_refptr<cc::TextureLayer> texture_layer_;
    293 
    294   scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
    295 
    296   // The layer used for rendering the contents of this view.
    297   // It is either owned by texture_layer_ or surface_texture_transport_
    298   // depending on the mode.
    299   scoped_refptr<cc::Layer> layer_;
    300 
    301   // The most recent texture id that was pushed to the texture layer.
    302   unsigned int texture_id_in_layer_;
    303 
    304   // The most recent texture size that was pushed to the texture layer.
    305   gfx::Size texture_size_in_layer_;
    306 
    307   // The most recent content size that was pushed to the texture layer.
    308   gfx::Size content_size_in_layer_;
    309 
    310   // Used for image transport when needing to share resources across threads.
    311   scoped_ptr<SurfaceTextureTransportClient> surface_texture_transport_;
    312 
    313   // The mailbox of the previously received frame.
    314   gpu::Mailbox current_mailbox_;
    315   uint32_t current_mailbox_output_surface_id_;
    316 
    317   base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
    318 
    319   std::queue<base::Closure> ack_callbacks_;
    320 
    321   // Used to render overscroll overlays.
    322   bool overscroll_effect_enabled_;
    323   scoped_ptr<OverscrollGlow> overscroll_effect_;
    324 
    325   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
    326 };
    327 
    328 } // namespace content
    329 
    330 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
    331