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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
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/render_widget_host_view_base.h"
24 #include "content/common/content_export.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/events/gesture_detection/filtered_gesture_provider.h"
30 #include "ui/gfx/size.h"
31 #include "ui/gfx/vector2d_f.h"
33 struct ViewHostMsg_TextInputState_Params;
35 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
36 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
39 class CopyOutputResult;
40 class DelegatedFrameProvider;
41 class DelegatedRendererLayer;
46 class WebExternalTextureLayer;
52 class ContentViewCoreImpl;
54 class RenderWidgetHost;
55 class RenderWidgetHostImpl;
56 struct DidOverscrollParams;
57 struct NativeWebKeyboardEvent;
59 // -----------------------------------------------------------------------------
60 // See comments in render_widget_host_view.h about this class and its members.
61 // -----------------------------------------------------------------------------
62 class CONTENT_EXPORT RenderWidgetHostViewAndroid
63 : public RenderWidgetHostViewBase,
64 public cc::DelegatedFrameResourceCollectionClient,
65 public ImageTransportFactoryAndroidObserver,
66 public ui::GestureProviderClient,
67 public ui::WindowAndroidObserver,
68 public DelegatedFrameEvictorClient {
70 RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
71 ContentViewCoreImpl* content_view_core);
72 virtual ~RenderWidgetHostViewAndroid();
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 std::vector<WebPluginGeometry>& moves) OVERRIDE;
91 virtual void Focus() OVERRIDE;
92 virtual void Blur() OVERRIDE;
93 virtual bool HasFocus() const OVERRIDE;
94 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
95 virtual void Show() OVERRIDE;
96 virtual void Hide() OVERRIDE;
97 virtual bool IsShowing() OVERRIDE;
98 virtual gfx::Rect GetViewBounds() const OVERRIDE;
99 virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
100 virtual float GetOverdrawBottomHeight() const OVERRIDE;
101 virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
102 virtual void SetIsLoading(bool is_loading) OVERRIDE;
103 virtual void TextInputStateChanged(
104 const ViewHostMsg_TextInputState_Params& params) OVERRIDE;
105 virtual void ImeCancelComposition() OVERRIDE;
106 virtual void FocusedNodeChanged(bool is_editable_node) OVERRIDE;
107 virtual void RenderProcessGone(base::TerminationStatus status,
108 int error_code) OVERRIDE;
109 virtual void Destroy() OVERRIDE;
110 virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE;
111 virtual void SelectionChanged(const base::string16& text,
113 const gfx::Range& range) OVERRIDE;
114 virtual void SelectionBoundsChanged(
115 const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
116 virtual void ScrollOffsetChanged() OVERRIDE;
117 virtual void AcceleratedSurfaceInitialized(int host_id,
118 int route_id) OVERRIDE;
119 virtual void AcceleratedSurfaceBuffersSwapped(
120 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
121 int gpu_host_id) OVERRIDE;
122 virtual void AcceleratedSurfacePostSubBuffer(
123 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
124 int gpu_host_id) OVERRIDE;
125 virtual void AcceleratedSurfaceSuspend() OVERRIDE;
126 virtual void AcceleratedSurfaceRelease() OVERRIDE;
127 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
128 virtual void SetBackgroundOpaque(bool transparent) OVERRIDE;
129 virtual void CopyFromCompositingSurface(
130 const gfx::Rect& src_subrect,
131 const gfx::Size& dst_size,
132 const base::Callback<void(bool, const SkBitmap&)>& callback,
133 const SkBitmap::Config config) OVERRIDE;
134 virtual void CopyFromCompositingSurfaceToVideoFrame(
135 const gfx::Rect& src_subrect,
136 const scoped_refptr<media::VideoFrame>& target,
137 const base::Callback<void(bool)>& callback) OVERRIDE;
138 virtual bool CanCopyToVideoFrame() const OVERRIDE;
139 virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE;
140 virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
141 virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
142 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
143 InputEventAckState ack_result) OVERRIDE;
144 virtual InputEventAckState FilterInputEvent(
145 const blink::WebInputEvent& input_event) OVERRIDE;
146 virtual void OnSetNeedsFlushInput() OVERRIDE;
147 virtual void GestureEventAck(const blink::WebGestureEvent& event,
148 InputEventAckState ack_result) OVERRIDE;
149 virtual void CreateBrowserAccessibilityManagerIfNeeded() OVERRIDE;
150 virtual bool LockMouse() OVERRIDE;
151 virtual void UnlockMouse() OVERRIDE;
152 virtual void OnSwapCompositorFrame(
153 uint32 output_surface_id,
154 scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
155 virtual void DidOverscroll(const DidOverscrollParams& params) OVERRIDE;
156 virtual void DidStopFlinging() OVERRIDE;
157 virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
158 const SkBitmap& zoomed_bitmap) OVERRIDE;
159 virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
161 virtual void LockCompositingSurface() OVERRIDE;
162 virtual void UnlockCompositingSurface() OVERRIDE;
163 virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
165 size_t end_offset) OVERRIDE;
167 // cc::DelegatedFrameResourceCollectionClient implementation.
168 virtual void UnusedResourcesAreAvailable() OVERRIDE;
170 // ui::GestureProviderClient implementation.
171 virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE;
173 // ui::WindowAndroidObserver implementation.
174 virtual void OnCompositingDidCommit() OVERRIDE;
175 virtual void OnAttachCompositor() OVERRIDE {}
176 virtual void OnDetachCompositor() OVERRIDE;
177 virtual void OnVSync(base::TimeTicks frame_time,
178 base::TimeDelta vsync_period) OVERRIDE;
179 virtual void OnAnimate(base::TimeTicks begin_frame_time) OVERRIDE;
181 // ImageTransportFactoryAndroidObserver implementation.
182 virtual void OnLostResources() OVERRIDE;
184 // DelegatedFrameEvictor implementation
185 virtual void EvictDelegatedFrame() OVERRIDE;
187 virtual SkBitmap::Config PreferredReadbackFormat() OVERRIDE;
189 // Non-virtual methods
190 void SetContentViewCore(ContentViewCoreImpl* content_view_core);
191 SkColor GetCachedBackgroundColor() const;
192 void SendKeyEvent(const NativeWebKeyboardEvent& event);
193 void SendTouchEvent(const blink::WebTouchEvent& event);
194 void SendMouseEvent(const blink::WebMouseEvent& event);
195 void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
196 void SendGestureEvent(const blink::WebGestureEvent& event);
198 void OnDidChangeBodyBackgroundColor(SkColor color);
199 void OnStartContentIntent(const GURL& content_url);
200 void OnSetNeedsBeginFrame(bool enabled);
201 void OnSmartClipDataExtracted(const base::string16& result);
203 bool OnTouchEvent(const ui::MotionEvent& event);
204 void ResetGestureDetection();
205 void SetDoubleTapSupportEnabled(bool enabled);
206 void SetMultiTouchZoomSupportEnabled(bool enabled);
208 long GetNativeImeAdapter();
212 void GetScaledContentBitmap(
214 SkBitmap::Config bitmap_config,
215 gfx::Rect src_subrect,
216 const base::Callback<void(bool, const SkBitmap&)>& result_callback);
218 bool HasValidFrame() const;
220 void MoveCaret(const gfx::Point& point);
222 void SynchronousFrameMetadata(
223 const cc::CompositorFrameMetadata& frame_metadata);
225 void SetOverlayVideoMode(bool enabled);
227 typedef base::Callback<
228 void(const base::string16& content, int start_offset, int end_offset)>
229 TextSurroundingSelectionCallback;
230 void SetTextSurroundingSelectionCallback(
231 const TextSurroundingSelectionCallback& callback);
234 void RunAckCallbacks();
236 void DestroyDelegatedContent();
237 void SwapDelegatedFrame(uint32 output_surface_id,
238 scoped_ptr<cc::DelegatedFrameData> frame_data);
239 void SendDelegatedFrameAck(uint32 output_surface_id);
240 void SendReturnedDelegatedResources(uint32 output_surface_id);
242 void OnFrameMetadataUpdated(
243 const cc::CompositorFrameMetadata& frame_metadata);
244 void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
245 void ResetClipping();
246 void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
251 // Called after async screenshot task completes. Scales and crops the result
253 static void PrepareTextureCopyOutputResult(
254 const gfx::Size& dst_size_in_pixel,
255 const SkBitmap::Config config,
256 const base::TimeTicks& start_time,
257 const base::Callback<void(bool, const SkBitmap&)>& callback,
258 scoped_ptr<cc::CopyOutputResult> result);
259 static void PrepareTextureCopyOutputResultForDelegatedReadback(
260 const gfx::Size& dst_size_in_pixel,
261 const SkBitmap::Config config,
262 const base::TimeTicks& start_time,
263 scoped_refptr<cc::Layer> readback_layer,
264 const base::Callback<void(bool, const SkBitmap&)>& callback,
265 scoped_ptr<cc::CopyOutputResult> result);
267 // DevTools ScreenCast support for Android WebView.
268 void SynchronousCopyContents(
269 const gfx::Rect& src_subrect_in_pixel,
270 const gfx::Size& dst_size_in_pixel,
271 const base::Callback<void(bool, const SkBitmap&)>& callback,
272 const SkBitmap::Config config);
274 bool IsReadbackConfigSupported(SkBitmap::Config bitmap_config);
276 // If we have locks on a frame during a ContentViewCore swap or a context
277 // lost, the frame is no longer valid and we can safely release all the locks.
278 // Use this method to release all the locks.
279 void ReleaseLocksOnSurface();
281 // Drop any incoming frames from the renderer when there are locks on the
283 void RetainFrame(uint32 output_surface_id,
284 scoped_ptr<cc::CompositorFrame> frame);
286 void InternalSwapCompositorFrame(uint32 output_surface_id,
287 scoped_ptr<cc::CompositorFrame> frame);
289 void SetNeedsAnimate();
290 bool Animate(base::TimeTicks frame_time);
293 RenderWidgetHostImpl* host_;
295 // Used to track whether this render widget needs a BeginFrame.
296 bool needs_begin_frame_;
300 // ContentViewCoreImpl is our interface to the view system.
301 ContentViewCoreImpl* content_view_core_;
303 ImeAdapterAndroid ime_adapter_android_;
305 // Body background color of the underlying document.
306 SkColor cached_background_color_;
308 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
309 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
310 scoped_refptr<cc::DelegatedRendererLayer> layer_;
312 // The most recent texture size that was pushed to the texture layer.
313 gfx::Size texture_size_in_layer_;
315 // The most recent content size that was pushed to the texture layer.
316 gfx::Size content_size_in_layer_;
318 // The device scale of the last received frame.
319 float device_scale_factor_;
321 // The output surface id of the last received frame.
322 uint32_t last_output_surface_id_;
324 base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
326 std::queue<base::Closure> ack_callbacks_;
328 const bool overscroll_effect_enabled_;
329 // Used to render overscroll overlays.
330 // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
331 scoped_ptr<OverscrollGlow> overscroll_effect_;
333 // Provides gesture synthesis given a stream of touch events (derived from
334 // Android MotionEvent's) and touch event acks.
335 ui::FilteredGestureProvider gesture_provider_;
337 bool flush_input_requested_;
339 int accelerated_surface_route_id_;
341 // Size to use if we have no backing ContentViewCore
342 gfx::Size default_size_;
344 const bool using_synchronous_compositor_;
346 scoped_ptr<DelegatedFrameEvictor> frame_evictor_;
348 size_t locks_on_frame_count_;
349 bool observing_root_window_;
351 struct LastFrameInfo {
352 LastFrameInfo(uint32 output_id,
353 scoped_ptr<cc::CompositorFrame> output_frame);
355 uint32 output_surface_id;
356 scoped_ptr<cc::CompositorFrame> frame;
359 scoped_ptr<LastFrameInfo> last_frame_info_;
361 TextSurroundingSelectionCallback text_surrounding_selection_callback_;
363 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
366 } // namespace content
368 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_