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/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/base/android/window_android_observer.h"
28 #include "ui/gfx/size.h"
29 #include "ui/gfx/vector2d_f.h"
31 struct ViewHostMsg_TextInputState_Params
;
33 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
;
34 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
;
37 class CopyOutputResult
;
38 class DelegatedFrameProvider
;
39 class DelegatedRendererLayer
;
41 class SingleReleaseCallback
;
46 class WebExternalTextureLayer
;
52 class ContentViewCoreImpl
;
54 class RenderWidgetHost
;
55 class RenderWidgetHostImpl
;
56 struct NativeWebKeyboardEvent
;
58 // -----------------------------------------------------------------------------
59 // See comments in render_widget_host_view.h about this class and its members.
60 // -----------------------------------------------------------------------------
61 class RenderWidgetHostViewAndroid
62 : public RenderWidgetHostViewBase
,
63 public BrowserAccessibilityDelegate
,
64 public cc::DelegatedFrameResourceCollectionClient
,
65 public ImageTransportFactoryAndroidObserver
,
66 public ui::WindowAndroidObserver
{
68 RenderWidgetHostViewAndroid(RenderWidgetHostImpl
* widget
,
69 ContentViewCoreImpl
* content_view_core
);
70 virtual ~RenderWidgetHostViewAndroid();
72 // RenderWidgetHostView implementation.
73 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
74 virtual void InitAsChild(gfx::NativeView parent_view
) OVERRIDE
;
75 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
76 const gfx::Rect
& pos
) OVERRIDE
;
77 virtual void InitAsFullscreen(
78 RenderWidgetHostView
* reference_host_view
) OVERRIDE
;
79 virtual RenderWidgetHost
* GetRenderWidgetHost() const OVERRIDE
;
80 virtual void WasShown() OVERRIDE
;
81 virtual void WasHidden() OVERRIDE
;
82 virtual void SetSize(const gfx::Size
& size
) OVERRIDE
;
83 virtual void SetBounds(const gfx::Rect
& rect
) OVERRIDE
;
84 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
85 virtual gfx::NativeViewId
GetNativeViewId() const OVERRIDE
;
86 virtual gfx::NativeViewAccessible
GetNativeViewAccessible() OVERRIDE
;
87 virtual void MovePluginWindows(
88 const gfx::Vector2d
& scroll_offset
,
89 const std::vector
<WebPluginGeometry
>& moves
) OVERRIDE
;
90 virtual void Focus() OVERRIDE
;
91 virtual void Blur() OVERRIDE
;
92 virtual bool HasFocus() const OVERRIDE
;
93 virtual bool IsSurfaceAvailableForCopy() const OVERRIDE
;
94 virtual void Show() OVERRIDE
;
95 virtual void Hide() OVERRIDE
;
96 virtual bool IsShowing() OVERRIDE
;
97 virtual gfx::Rect
GetViewBounds() const OVERRIDE
;
98 virtual gfx::Size
GetPhysicalBackingSize() const OVERRIDE
;
99 virtual float GetOverdrawBottomHeight() const OVERRIDE
;
100 virtual void UpdateCursor(const WebCursor
& cursor
) OVERRIDE
;
101 virtual void SetIsLoading(bool is_loading
) OVERRIDE
;
102 virtual void TextInputTypeChanged(ui::TextInputType type
,
103 ui::TextInputMode input_mode
,
104 bool can_compose_inline
) OVERRIDE
;
105 virtual void ImeCancelComposition() OVERRIDE
;
106 virtual void DidUpdateBackingStore(
107 const gfx::Rect
& scroll_rect
,
108 const gfx::Vector2d
& scroll_delta
,
109 const std::vector
<gfx::Rect
>& copy_rects
,
110 const ui::LatencyInfo
& latency_info
) OVERRIDE
;
111 virtual void RenderProcessGone(base::TerminationStatus status
,
112 int error_code
) OVERRIDE
;
113 virtual void Destroy() OVERRIDE
;
114 virtual void SetTooltipText(const base::string16
& tooltip_text
) OVERRIDE
;
115 virtual void SelectionChanged(const base::string16
& text
,
117 const gfx::Range
& range
) OVERRIDE
;
118 virtual void SelectionBoundsChanged(
119 const ViewHostMsg_SelectionBounds_Params
& params
) OVERRIDE
;
120 virtual void ScrollOffsetChanged() OVERRIDE
;
121 virtual BackingStore
* AllocBackingStore(const gfx::Size
& size
) OVERRIDE
;
122 virtual void OnAcceleratedCompositingStateChange() OVERRIDE
;
123 virtual void AcceleratedSurfaceInitialized(int host_id
,
124 int route_id
) OVERRIDE
;
125 virtual void AcceleratedSurfaceBuffersSwapped(
126 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params
,
127 int gpu_host_id
) OVERRIDE
;
128 virtual void AcceleratedSurfacePostSubBuffer(
129 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params
,
130 int gpu_host_id
) OVERRIDE
;
131 virtual void AcceleratedSurfaceSuspend() OVERRIDE
;
132 virtual void AcceleratedSurfaceRelease() OVERRIDE
;
133 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) OVERRIDE
;
134 virtual void SetBackground(const SkBitmap
& background
) OVERRIDE
;
135 virtual void CopyFromCompositingSurface(
136 const gfx::Rect
& src_subrect
,
137 const gfx::Size
& dst_size
,
138 const base::Callback
<void(bool, const SkBitmap
&)>& callback
) OVERRIDE
;
139 virtual void CopyFromCompositingSurfaceToVideoFrame(
140 const gfx::Rect
& src_subrect
,
141 const scoped_refptr
<media::VideoFrame
>& target
,
142 const base::Callback
<void(bool)>& callback
) OVERRIDE
;
143 virtual bool CanCopyToVideoFrame() const OVERRIDE
;
144 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) OVERRIDE
;
145 virtual gfx::Rect
GetBoundsInRootWindow() OVERRIDE
;
146 virtual gfx::GLSurfaceHandle
GetCompositingSurface() OVERRIDE
;
147 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
148 InputEventAckState ack_result
) OVERRIDE
;
149 virtual void SetHasHorizontalScrollbar(
150 bool has_horizontal_scrollbar
) OVERRIDE
;
151 virtual void SetScrollOffsetPinning(
152 bool is_pinned_to_left
, bool is_pinned_to_right
) OVERRIDE
;
153 virtual void UnhandledWheelEvent(
154 const blink::WebMouseWheelEvent
& event
) OVERRIDE
;
155 virtual InputEventAckState
FilterInputEvent(
156 const blink::WebInputEvent
& input_event
) OVERRIDE
;
157 virtual void OnSetNeedsFlushInput() OVERRIDE
;
158 virtual void GestureEventAck(int gesture_event_type
,
159 InputEventAckState ack_result
) OVERRIDE
;
160 virtual void OnAccessibilityEvents(
161 const std::vector
<AccessibilityHostMsg_EventParams
>&
163 virtual bool LockMouse() OVERRIDE
;
164 virtual void UnlockMouse() OVERRIDE
;
165 virtual void HasTouchEventHandlers(bool need_touch_events
) OVERRIDE
;
166 virtual void OnSwapCompositorFrame(
167 uint32 output_surface_id
,
168 scoped_ptr
<cc::CompositorFrame
> frame
) OVERRIDE
;
169 virtual void OnOverscrolled(gfx::Vector2dF accumulated_overscroll
,
170 gfx::Vector2dF current_fling_velocity
) OVERRIDE
;
171 virtual void ShowDisambiguationPopup(const gfx::Rect
& target_rect
,
172 const SkBitmap
& zoomed_bitmap
) OVERRIDE
;
173 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget()
176 // Implementation of BrowserAccessibilityDelegate:
177 virtual void SetAccessibilityFocus(int acc_obj_id
) OVERRIDE
;
178 virtual void AccessibilityDoDefaultAction(int acc_obj_id
) OVERRIDE
;
179 virtual void AccessibilityScrollToMakeVisible(
180 int acc_obj_id
, gfx::Rect subfocus
) OVERRIDE
;
181 virtual void AccessibilityScrollToPoint(
182 int acc_obj_id
, gfx::Point point
) OVERRIDE
;
183 virtual void AccessibilitySetTextSelection(
184 int acc_obj_id
, int start_offset
, int end_offset
) OVERRIDE
;
185 virtual gfx::Point
GetLastTouchEventLocation() const OVERRIDE
;
186 virtual void FatalAccessibilityTreeError() OVERRIDE
;
188 // cc::DelegatedFrameResourceCollectionClient implementation.
189 virtual void UnusedResourcesAreAvailable() OVERRIDE
;
191 // ui::WindowAndroidObserver implementation.
192 virtual void OnCompositingDidCommit() OVERRIDE
;
193 virtual void OnAttachCompositor() OVERRIDE
{}
194 virtual void OnDetachCompositor() OVERRIDE
;
196 // ImageTransportFactoryAndroidObserver implementation.
197 virtual void OnLostResources() OVERRIDE
;
199 // Non-virtual methods
200 void SetContentViewCore(ContentViewCoreImpl
* content_view_core
);
201 SkColor
GetCachedBackgroundColor() const;
202 void SendKeyEvent(const NativeWebKeyboardEvent
& event
);
203 void SendTouchEvent(const blink::WebTouchEvent
& event
);
204 void SendMouseEvent(const blink::WebMouseEvent
& event
);
205 void SendMouseWheelEvent(const blink::WebMouseWheelEvent
& event
);
206 void SendGestureEvent(const blink::WebGestureEvent
& event
);
207 void SendBeginFrame(const cc::BeginFrameArgs
& args
);
209 void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params
& params
);
210 void OnDidChangeBodyBackgroundColor(SkColor color
);
211 void OnStartContentIntent(const GURL
& content_url
);
212 void OnSetNeedsBeginFrame(bool enabled
);
214 int GetNativeImeAdapter();
218 blink::WebGLId
GetScaledContentTexture(float scale
, gfx::Size
* out_size
);
219 bool PopulateBitmapWithContents(jobject jbitmap
);
221 bool HasValidFrame() const;
223 // Select all text between the given coordinates.
224 void SelectRange(const gfx::Point
& start
, const gfx::Point
& end
);
226 void MoveCaret(const gfx::Point
& point
);
228 void RequestContentClipping(const gfx::Rect
& clipping
,
229 const gfx::Size
& content_size
);
231 // Returns true when animation ticks are still needed. This avoids a separate
232 // round-trip for requesting follow-up animation.
233 bool Animate(base::TimeTicks frame_time
);
235 void SynchronousFrameMetadata(
236 const cc::CompositorFrameMetadata
& frame_metadata
);
239 void BuffersSwapped(const gpu::Mailbox
& mailbox
,
240 uint32_t output_surface_id
,
241 const base::Closure
& ack_callback
);
243 void RunAckCallbacks();
245 void DestroyDelegatedContent();
246 void SwapDelegatedFrame(uint32 output_surface_id
,
247 scoped_ptr
<cc::DelegatedFrameData
> frame_data
);
248 void SendDelegatedFrameAck(uint32 output_surface_id
);
250 void UpdateContentViewCoreFrameMetadata(
251 const cc::CompositorFrameMetadata
& frame_metadata
);
252 void ComputeContentsSize(const cc::CompositorFrameMetadata
& frame_metadata
);
253 void ResetClipping();
254 void ClipContents(const gfx::Rect
& clipping
, const gfx::Size
& content_size
);
259 void UpdateAnimationSize(const cc::CompositorFrameMetadata
& frame_metadata
);
261 // Called after async screenshot task completes. Scales and crops the result
263 static void PrepareTextureCopyOutputResult(
264 const gfx::Size
& dst_size_in_pixel
,
265 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
266 scoped_ptr
<cc::CopyOutputResult
> result
);
267 static void PrepareBitmapCopyOutputResult(
268 const gfx::Size
& dst_size_in_pixel
,
269 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
270 scoped_ptr
<cc::CopyOutputResult
> result
);
272 // DevTools ScreenCast support for Android WebView.
273 void SynchronousCopyContents(
274 const gfx::Rect
& src_subrect_in_pixel
,
275 const gfx::Size
& dst_size_in_pixel
,
276 const base::Callback
<void(bool, const SkBitmap
&)>& callback
);
279 RenderWidgetHostImpl
* host_
;
281 // Used to track whether this render widget needs a BeginFrame.
282 bool needs_begin_frame_
;
284 // Whether or not this widget is potentially attached to the view hierarchy.
285 // This view may not actually be attached if this is true, but it should be
286 // treated as such, because as soon as a ContentViewCore is set the layer
287 // will be attached automatically.
288 bool are_layers_attached_
;
290 // ContentViewCoreImpl is our interface to the view system.
291 ContentViewCoreImpl
* content_view_core_
;
293 ImeAdapterAndroid ime_adapter_android_
;
295 // Body background color of the underlying document.
296 SkColor cached_background_color_
;
298 // The texture layer for this view when using browser-side compositing.
299 scoped_refptr
<cc::TextureLayer
> texture_layer_
;
301 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
302 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
303 scoped_refptr
<cc::DelegatedRendererLayer
> delegated_renderer_layer_
;
305 // The layer used for rendering the contents of this view.
306 // It is either owned by texture_layer_ or surface_texture_transport_
307 // depending on the mode.
308 scoped_refptr
<cc::Layer
> layer_
;
310 // The most recent texture id that was pushed to the texture layer.
311 unsigned int texture_id_in_layer_
;
313 // The most recent texture size that was pushed to the texture layer.
314 gfx::Size texture_size_in_layer_
;
316 // The most recent content size that was pushed to the texture layer.
317 gfx::Size content_size_in_layer_
;
319 // The mailbox of the previously received frame.
320 gpu::Mailbox current_mailbox_
;
322 // The output surface id of the last received frame.
323 uint32_t last_output_surface_id_
;
325 base::WeakPtrFactory
<RenderWidgetHostViewAndroid
> weak_ptr_factory_
;
327 std::queue
<base::Closure
> ack_callbacks_
;
329 const bool overscroll_effect_enabled_
;
330 // Used to render overscroll overlays.
331 // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
332 scoped_ptr
<OverscrollGlow
> overscroll_effect_
;
334 bool flush_input_requested_
;
336 int accelerated_surface_route_id_
;
338 // Size to use if we have no backing ContentViewCore
339 gfx::Size default_size_
;
341 const bool using_synchronous_compositor_
;
343 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid
);
346 } // namespace content
348 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_