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 34 struct ViewHostMsg_TextInputState_Params; 35 36 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params; 37 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params; 38 39 namespace cc { 40 class CopyOutputResult; 41 class DelegatedFrameProvider; 42 class DelegatedRendererLayer; 43 class Layer; 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 DidOverscrollParams; 58 struct NativeWebKeyboardEvent; 59 60 // ----------------------------------------------------------------------------- 61 // See comments in render_widget_host_view.h about this class and its members. 62 // ----------------------------------------------------------------------------- 63 class 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 SkBitmap::Config config) 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 void CreateBrowserAccessibilityManagerIfNeeded() OVERRIDE; 152 virtual bool LockMouse() OVERRIDE; 153 virtual void UnlockMouse() OVERRIDE; 154 virtual void OnSwapCompositorFrame( 155 uint32 output_surface_id, 156 scoped_ptr<cc::CompositorFrame> frame) OVERRIDE; 157 virtual void DidOverscroll(const DidOverscrollParams& params) OVERRIDE; 158 virtual void DidStopFlinging() OVERRIDE; 159 virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect, 160 const SkBitmap& zoomed_bitmap) OVERRIDE; 161 virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget() 162 OVERRIDE; 163 virtual void LockCompositingSurface() OVERRIDE; 164 virtual void UnlockCompositingSurface() OVERRIDE; 165 virtual void OnTextSurroundingSelectionResponse(const base::string16& content, 166 size_t start_offset, 167 size_t end_offset) OVERRIDE; 168 169 // cc::DelegatedFrameResourceCollectionClient implementation. 170 virtual void UnusedResourcesAreAvailable() OVERRIDE; 171 172 // ui::GestureProviderClient implementation. 173 virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE; 174 175 // ui::WindowAndroidObserver implementation. 176 virtual void OnCompositingDidCommit() OVERRIDE; OnAttachCompositor()177 virtual void OnAttachCompositor() OVERRIDE {} 178 virtual void OnDetachCompositor() OVERRIDE; 179 virtual void OnVSync(base::TimeTicks frame_time, 180 base::TimeDelta vsync_period) OVERRIDE; 181 virtual void OnAnimate(base::TimeTicks begin_frame_time) OVERRIDE; 182 183 // ImageTransportFactoryAndroidObserver implementation. 184 virtual void OnLostResources() OVERRIDE; 185 186 // DelegatedFrameEvictor implementation 187 virtual void EvictDelegatedFrame() OVERRIDE; 188 189 virtual SkBitmap::Config PreferredReadbackFormat() OVERRIDE; 190 191 // GestureTextSelectorClient implementation. 192 virtual void ShowSelectionHandlesAutomatically() OVERRIDE; 193 virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE; 194 virtual void Unselect() OVERRIDE; 195 virtual void LongPress(base::TimeTicks time, float x, float y) OVERRIDE; 196 197 // Non-virtual methods 198 void SetContentViewCore(ContentViewCoreImpl* content_view_core); 199 SkColor GetCachedBackgroundColor() const; 200 void SendKeyEvent(const NativeWebKeyboardEvent& event); 201 void SendTouchEvent(const blink::WebTouchEvent& event); 202 void SendMouseEvent(const blink::WebMouseEvent& event); 203 void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event); 204 void SendGestureEvent(const blink::WebGestureEvent& event); 205 206 void OnDidChangeBodyBackgroundColor(SkColor color); 207 void OnStartContentIntent(const GURL& content_url); 208 void OnSetNeedsBeginFrame(bool enabled); 209 void OnSmartClipDataExtracted(const base::string16& text, 210 const base::string16& html, 211 const gfx::Rect rect); 212 213 bool OnTouchEvent(const ui::MotionEvent& event); 214 void ResetGestureDetection(); 215 void SetDoubleTapSupportEnabled(bool enabled); 216 void SetMultiTouchZoomSupportEnabled(bool enabled); 217 218 long GetNativeImeAdapter(); 219 220 void WasResized(); 221 222 void GetScaledContentBitmap( 223 float scale, 224 SkBitmap::Config bitmap_config, 225 gfx::Rect src_subrect, 226 const base::Callback<void(bool, const SkBitmap&)>& result_callback); 227 228 bool HasValidFrame() const; 229 230 void MoveCaret(const gfx::Point& point); 231 232 void SynchronousFrameMetadata( 233 const cc::CompositorFrameMetadata& frame_metadata); 234 235 void SetOverlayVideoMode(bool enabled); 236 237 typedef base::Callback< 238 void(const base::string16& content, int start_offset, int end_offset)> 239 TextSurroundingSelectionCallback; 240 void SetTextSurroundingSelectionCallback( 241 const TextSurroundingSelectionCallback& callback); 242 243 private: 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 void SendReturnedDelegatedResources(uint32 output_surface_id); 251 252 void OnFrameMetadataUpdated( 253 const cc::CompositorFrameMetadata& frame_metadata); 254 void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata); 255 void ResetClipping(); 256 void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size); 257 258 void AttachLayers(); 259 void RemoveLayers(); 260 261 // Called after async screenshot task completes. Scales and crops the result 262 // of the copy. 263 static void PrepareTextureCopyOutputResult( 264 const gfx::Size& dst_size_in_pixel, 265 const SkBitmap::Config config, 266 const base::TimeTicks& start_time, 267 const base::Callback<void(bool, const SkBitmap&)>& callback, 268 scoped_ptr<cc::CopyOutputResult> result); 269 static void PrepareTextureCopyOutputResultForDelegatedReadback( 270 const gfx::Size& dst_size_in_pixel, 271 const SkBitmap::Config config, 272 const base::TimeTicks& start_time, 273 scoped_refptr<cc::Layer> readback_layer, 274 const base::Callback<void(bool, const SkBitmap&)>& callback, 275 scoped_ptr<cc::CopyOutputResult> result); 276 277 // DevTools ScreenCast support for Android WebView. 278 void SynchronousCopyContents( 279 const gfx::Rect& src_subrect_in_pixel, 280 const gfx::Size& dst_size_in_pixel, 281 const base::Callback<void(bool, const SkBitmap&)>& callback, 282 const SkBitmap::Config config); 283 284 bool IsReadbackConfigSupported(SkBitmap::Config bitmap_config); 285 286 // If we have locks on a frame during a ContentViewCore swap or a context 287 // lost, the frame is no longer valid and we can safely release all the locks. 288 // Use this method to release all the locks. 289 void ReleaseLocksOnSurface(); 290 291 // Drop any incoming frames from the renderer when there are locks on the 292 // current frame. 293 void RetainFrame(uint32 output_surface_id, 294 scoped_ptr<cc::CompositorFrame> frame); 295 296 void InternalSwapCompositorFrame(uint32 output_surface_id, 297 scoped_ptr<cc::CompositorFrame> frame); 298 299 void SetNeedsAnimate(); 300 bool Animate(base::TimeTicks frame_time); 301 302 // The model object. 303 RenderWidgetHostImpl* host_; 304 305 // Used to track whether this render widget needs a BeginFrame. 306 bool needs_begin_frame_; 307 308 bool is_showing_; 309 310 // ContentViewCoreImpl is our interface to the view system. 311 ContentViewCoreImpl* content_view_core_; 312 313 ImeAdapterAndroid ime_adapter_android_; 314 315 // Body background color of the underlying document. 316 SkColor cached_background_color_; 317 318 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_; 319 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_; 320 scoped_refptr<cc::DelegatedRendererLayer> layer_; 321 322 // The most recent texture size that was pushed to the texture layer. 323 gfx::Size texture_size_in_layer_; 324 325 // The most recent content size that was pushed to the texture layer. 326 gfx::Size content_size_in_layer_; 327 328 // The device scale of the last received frame. 329 float device_scale_factor_; 330 331 // The output surface id of the last received frame. 332 uint32_t last_output_surface_id_; 333 334 base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_; 335 336 std::queue<base::Closure> ack_callbacks_; 337 338 const bool overscroll_effect_enabled_; 339 // Used to render overscroll overlays. 340 // Note: |overscroll_effect_| will never be NULL, even if it's never enabled. 341 scoped_ptr<OverscrollGlow> overscroll_effect_; 342 343 // Provides gesture synthesis given a stream of touch events (derived from 344 // Android MotionEvent's) and touch event acks. 345 ui::FilteredGestureProvider gesture_provider_; 346 347 // Handles gesture based text selection 348 GestureTextSelector gesture_text_selector_; 349 350 bool flush_input_requested_; 351 352 int accelerated_surface_route_id_; 353 354 // Size to use if we have no backing ContentViewCore 355 gfx::Size default_size_; 356 357 const bool using_synchronous_compositor_; 358 359 scoped_ptr<DelegatedFrameEvictor> frame_evictor_; 360 361 size_t locks_on_frame_count_; 362 bool observing_root_window_; 363 364 struct LastFrameInfo { 365 LastFrameInfo(uint32 output_id, 366 scoped_ptr<cc::CompositorFrame> output_frame); 367 ~LastFrameInfo(); 368 uint32 output_surface_id; 369 scoped_ptr<cc::CompositorFrame> frame; 370 }; 371 372 scoped_ptr<LastFrameInfo> last_frame_info_; 373 374 TextSurroundingSelectionCallback text_surrounding_selection_callback_; 375 376 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid); 377 }; 378 379 } // namespace content 380 381 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_ 382