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