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 #include "content/browser/renderer_host/render_widget_host_view_android.h"
7 #include <android/bitmap.h>
9 #include "base/android/build_info.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/command_line.h"
14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/histogram.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/sys_info.h"
19 #include "base/threading/worker_pool.h"
20 #include "cc/layers/delegated_frame_provider.h"
21 #include "cc/layers/delegated_renderer_layer.h"
22 #include "cc/layers/layer.h"
23 #include "cc/layers/surface_layer.h"
24 #include "cc/output/compositor_frame.h"
25 #include "cc/output/compositor_frame_ack.h"
26 #include "cc/output/copy_output_request.h"
27 #include "cc/output/copy_output_result.h"
28 #include "cc/output/latency_info_swap_promise.h"
29 #include "cc/output/viewport_selection_bound.h"
30 #include "cc/resources/single_release_callback.h"
31 #include "cc/surfaces/surface.h"
32 #include "cc/surfaces/surface_factory.h"
33 #include "cc/surfaces/surface_id_allocator.h"
34 #include "cc/surfaces/surface_manager.h"
35 #include "cc/trees/layer_tree_host.h"
36 #include "content/browser/accessibility/browser_accessibility_manager_android.h"
37 #include "content/browser/android/composited_touch_handle_drawable.h"
38 #include "content/browser/android/content_view_core_impl.h"
39 #include "content/browser/android/edge_effect.h"
40 #include "content/browser/android/edge_effect_l.h"
41 #include "content/browser/android/in_process/synchronous_compositor_impl.h"
42 #include "content/browser/android/overscroll_controller_android.h"
43 #include "content/browser/devtools/render_frame_devtools_agent_host.h"
44 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
45 #include "content/browser/gpu/compositor_util.h"
46 #include "content/browser/gpu/gpu_data_manager_impl.h"
47 #include "content/browser/gpu/gpu_process_host_ui_shim.h"
48 #include "content/browser/gpu/gpu_surface_tracker.h"
49 #include "content/browser/media/media_web_contents_observer.h"
50 #include "content/browser/renderer_host/compositor_impl_android.h"
51 #include "content/browser/renderer_host/dip_util.h"
52 #include "content/browser/renderer_host/frame_metadata_util.h"
53 #include "content/browser/renderer_host/input/synthetic_gesture_target_android.h"
54 #include "content/browser/renderer_host/input/ui_touch_selection_helper.h"
55 #include "content/browser/renderer_host/input/web_input_event_builders_android.h"
56 #include "content/browser/renderer_host/input/web_input_event_util.h"
57 #include "content/browser/renderer_host/render_process_host_impl.h"
58 #include "content/browser/renderer_host/render_view_host_impl.h"
59 #include "content/browser/renderer_host/render_widget_host_impl.h"
60 #include "content/common/gpu/client/gl_helper.h"
61 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
62 #include "content/common/gpu/gpu_messages.h"
63 #include "content/common/gpu/gpu_process_launch_causes.h"
64 #include "content/common/input/did_overscroll_params.h"
65 #include "content/common/input_messages.h"
66 #include "content/common/view_messages.h"
67 #include "content/public/browser/android/compositor.h"
68 #include "content/public/browser/browser_thread.h"
69 #include "content/public/browser/devtools_agent_host.h"
70 #include "content/public/browser/render_view_host.h"
71 #include "content/public/browser/render_widget_host_iterator.h"
72 #include "content/public/common/content_switches.h"
73 #include "gpu/command_buffer/client/gles2_implementation.h"
74 #include "gpu/command_buffer/client/gles2_interface.h"
75 #include "gpu/config/gpu_driver_bug_workaround_type.h"
76 #include "skia/ext/image_operations.h"
77 #include "third_party/khronos/GLES2/gl2.h"
78 #include "third_party/khronos/GLES2/gl2ext.h"
79 #include "third_party/skia/include/core/SkCanvas.h"
80 #include "ui/android/window_android.h"
81 #include "ui/android/window_android_compositor.h"
82 #include "ui/events/blink/blink_event_util.h"
83 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
84 #include "ui/events/gesture_detection/motion_event.h"
85 #include "ui/gfx/android/device_display_info.h"
86 #include "ui/gfx/android/java_bitmap.h"
87 #include "ui/gfx/android/view_configuration.h"
88 #include "ui/gfx/display.h"
89 #include "ui/gfx/geometry/dip_util.h"
90 #include "ui/gfx/geometry/size_conversions.h"
91 #include "ui/gfx/screen.h"
92 #include "ui/touch_selection/touch_selection_controller.h"
98 void SatisfyCallback(cc::SurfaceManager
* manager
,
99 cc::SurfaceSequence sequence
) {
100 std::vector
<uint32_t> sequences
;
101 sequences
.push_back(sequence
.sequence
);
102 manager
->DidSatisfySequences(sequence
.id_namespace
, &sequences
);
105 void RequireCallback(cc::SurfaceManager
* manager
,
107 cc::SurfaceSequence sequence
) {
108 cc::Surface
* surface
= manager
->GetSurfaceForId(id
);
110 LOG(ERROR
) << "Attempting to require callback on nonexistent surface";
113 surface
->AddDestructionDependency(sequence
);
116 const int kUndefinedOutputSurfaceId
= -1;
118 static const char kAsyncReadBackString
[] = "Compositing.CopyFromSurfaceTime";
120 // Sends an acknowledgement to the renderer of a processed IME event.
121 void SendImeEventAck(RenderWidgetHostImpl
* host
) {
122 host
->Send(new ViewMsg_ImeEventAck(host
->GetRoutingID()));
126 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback
{
128 static GLHelperHolder
* Create();
129 ~GLHelperHolder() override
;
133 // WebGraphicsContextLostCallback implementation.
134 void onContextLost() override
;
136 GLHelper
* GetGLHelper() { return gl_helper_
.get(); }
137 bool IsLost() { return !context_
.get() || context_
->isContextLost(); }
141 static scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> CreateContext3D();
143 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context_
;
144 scoped_ptr
<GLHelper
> gl_helper_
;
146 DISALLOW_COPY_AND_ASSIGN(GLHelperHolder
);
149 GLHelperHolder
* GLHelperHolder::Create() {
150 GLHelperHolder
* holder
= new GLHelperHolder
;
151 holder
->Initialize();
156 GLHelperHolder::GLHelperHolder() {
159 GLHelperHolder::~GLHelperHolder() {
162 void GLHelperHolder::Initialize() {
163 context_
= CreateContext3D();
165 context_
->setContextLostCallback(this);
166 gl_helper_
.reset(new GLHelper(context_
->GetImplementation(),
167 context_
->GetContextSupport()));
171 void GLHelperHolder::onContextLost() {
172 // Need to post a task because the command buffer client cannot be deleted
173 // from within this callback.
174 LOG(ERROR
) << "Context lost.";
175 base::MessageLoop::current()->PostTask(
177 base::Bind(&RenderWidgetHostViewAndroid::OnContextLost
));
180 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>
181 GLHelperHolder::CreateContext3D() {
182 BrowserGpuChannelHostFactory
* factory
=
183 BrowserGpuChannelHostFactory::instance();
184 scoped_refptr
<GpuChannelHost
> gpu_channel_host(factory
->GetGpuChannel());
185 // GLHelper can only be used in asynchronous APIs for postprocessing after
186 // Browser Compositor operations (i.e. readback).
187 if (!gpu_channel_host
.get()) {
188 // The Browser Compositor is in charge of reestablishing the channel.
189 return scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>();
192 blink::WebGraphicsContext3D::Attributes attrs
;
193 attrs
.shareResources
= true;
194 GURL
url("chrome://gpu/RenderWidgetHostViewAndroid");
195 static const size_t kBytesPerPixel
= 4;
196 gfx::DeviceDisplayInfo display_info
;
197 size_t full_screen_texture_size_in_bytes
= display_info
.GetDisplayHeight() *
198 display_info
.GetDisplayWidth() *
200 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits
;
201 limits
.command_buffer_size
= 64 * 1024;
202 limits
.start_transfer_buffer_size
= 64 * 1024;
203 limits
.min_transfer_buffer_size
= 64 * 1024;
204 limits
.max_transfer_buffer_size
= std::min(
205 3 * full_screen_texture_size_in_bytes
, kDefaultMaxTransferBufferSize
);
206 limits
.mapped_memory_reclaim_limit
=
207 WebGraphicsContext3DCommandBufferImpl::kNoLimit
;
208 bool lose_context_when_out_of_memory
= false;
209 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context(
210 new WebGraphicsContext3DCommandBufferImpl(
212 url
, gpu_channel_host
.get(), attrs
, lose_context_when_out_of_memory
,
214 context
->SetContextType(BROWSER_OFFSCREEN_MAINTHREAD_CONTEXT
);
215 if (context
->InitializeOnCurrentThread()) {
216 context
->traceBeginCHROMIUM(
218 base::StringPrintf("CmdBufferImageTransportFactory-%p",
219 context
.get()).c_str());
224 return context
.Pass();
227 // This can only be used for readback postprocessing. It may return null if the
228 // channel was lost and not reestablished yet.
229 GLHelper
* GetPostReadbackGLHelper() {
230 static GLHelperHolder
* g_readback_helper_holder
= nullptr;
232 if (g_readback_helper_holder
&& g_readback_helper_holder
->IsLost()) {
233 delete g_readback_helper_holder
;
234 g_readback_helper_holder
= nullptr;
237 if (!g_readback_helper_holder
)
238 g_readback_helper_holder
= GLHelperHolder::Create();
240 return g_readback_helper_holder
->GetGLHelper();
243 void CopyFromCompositingSurfaceFinished(
244 ReadbackRequestCallback
& callback
,
245 scoped_ptr
<cc::SingleReleaseCallback
> release_callback
,
246 scoped_ptr
<SkBitmap
> bitmap
,
247 const base::TimeTicks
& start_time
,
248 scoped_ptr
<SkAutoLockPixels
> bitmap_pixels_lock
,
251 "cc", "RenderWidgetHostViewAndroid::CopyFromCompositingSurfaceFinished");
252 bitmap_pixels_lock
.reset();
253 uint32 sync_point
= 0;
255 GLHelper
* gl_helper
= GetPostReadbackGLHelper();
257 sync_point
= gl_helper
->InsertSyncPoint();
259 bool lost_resource
= sync_point
== 0;
260 release_callback
->Run(sync_point
, lost_resource
);
261 UMA_HISTOGRAM_TIMES(kAsyncReadBackString
,
262 base::TimeTicks::Now() - start_time
);
263 ReadbackResponse response
= result
? READBACK_SUCCESS
: READBACK_FAILED
;
264 callback
.Run(*bitmap
, response
);
267 scoped_ptr
<ui::TouchSelectionController
> CreateSelectionController(
268 ui::TouchSelectionControllerClient
* client
,
269 ContentViewCore
* content_view_core
) {
271 DCHECK(content_view_core
);
272 ui::TouchSelectionController::Config config
;
273 config
.tap_timeout
= base::TimeDelta::FromMilliseconds(
274 gfx::ViewConfiguration::GetTapTimeoutInMs());
275 config
.tap_slop
= gfx::ViewConfiguration::GetTouchSlopInDips();
276 config
.show_on_tap_for_empty_editable
= false;
277 config
.enable_longpress_drag_selection
=
278 base::CommandLine::ForCurrentProcess()->HasSwitch(
279 switches::kEnableLongpressDragSelection
);
280 return make_scoped_ptr(new ui::TouchSelectionController(client
, config
));
283 scoped_ptr
<OverscrollControllerAndroid
> CreateOverscrollController(
284 ContentViewCoreImpl
* content_view_core
) {
285 return make_scoped_ptr(new OverscrollControllerAndroid(content_view_core
));
288 ui::GestureProvider::Config
CreateGestureProviderConfig() {
289 ui::GestureProvider::Config config
= ui::GetGestureProviderConfig(
290 ui::GestureProviderConfigType::CURRENT_PLATFORM
);
291 config
.disable_click_delay
=
292 base::CommandLine::ForCurrentProcess()->HasSwitch(
293 switches::kDisableClickDelay
);
297 gfx::RectF
GetSelectionRect(const ui::TouchSelectionController
& controller
) {
298 gfx::RectF rect
= controller
.GetRectBetweenBounds();
302 rect
.Union(controller
.GetStartHandleRect());
303 rect
.Union(controller
.GetEndHandleRect());
307 } // anonymous namespace
309 RenderWidgetHostViewAndroid::LastFrameInfo::LastFrameInfo(
311 scoped_ptr
<cc::CompositorFrame
> output_frame
)
312 : output_surface_id(output_id
), frame(output_frame
.Pass()) {}
314 RenderWidgetHostViewAndroid::LastFrameInfo::~LastFrameInfo() {}
316 void RenderWidgetHostViewAndroid::OnContextLost() {
317 scoped_ptr
<RenderWidgetHostIterator
> widgets(
318 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
319 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
320 if (widget
->GetView()) {
321 static_cast<RenderWidgetHostViewAndroid
*>(widget
->GetView())
327 RenderWidgetHostViewAndroid::RenderWidgetHostViewAndroid(
328 RenderWidgetHostImpl
* widget_host
,
329 ContentViewCoreImpl
* content_view_core
)
330 : host_(widget_host
),
331 outstanding_vsync_requests_(0),
332 is_showing_(!widget_host
->is_hidden()),
333 content_view_core_(nullptr),
334 content_view_core_window_android_(nullptr),
335 ime_adapter_android_(this),
336 cached_background_color_(SK_ColorWHITE
),
337 last_output_surface_id_(kUndefinedOutputSurfaceId
),
338 gesture_provider_(CreateGestureProviderConfig(), this),
339 stylus_text_selector_(this),
340 accelerated_surface_route_id_(0),
341 using_browser_compositor_(CompositorImpl::IsInitialized()),
342 frame_evictor_(new DelegatedFrameEvictor(this)),
343 locks_on_frame_count_(0),
344 observing_root_window_(false),
345 weak_ptr_factory_(this) {
346 if (CompositorImpl::GetSurfaceManager())
347 id_allocator_
= CompositorImpl::CreateSurfaceIdAllocator();
348 host_
->SetView(this);
349 SetContentViewCore(content_view_core
);
352 RenderWidgetHostViewAndroid::~RenderWidgetHostViewAndroid() {
353 SetContentViewCore(NULL
);
354 DCHECK(ack_callbacks_
.empty());
355 if (resource_collection_
.get())
356 resource_collection_
->SetClient(NULL
);
357 DCHECK(!surface_factory_
);
358 DCHECK(surface_id_
.is_null());
361 void RenderWidgetHostViewAndroid::Blur() {
363 if (overscroll_controller_
)
364 overscroll_controller_
->Disable();
367 bool RenderWidgetHostViewAndroid::OnMessageReceived(
368 const IPC::Message
& message
) {
370 IPC_BEGIN_MESSAGE_MAP(RenderWidgetHostViewAndroid
, message
)
371 IPC_MESSAGE_HANDLER(ViewHostMsg_StartContentIntent
, OnStartContentIntent
)
372 IPC_MESSAGE_HANDLER(ViewHostMsg_DidChangeBodyBackgroundColor
,
373 OnDidChangeBodyBackgroundColor
)
374 IPC_MESSAGE_HANDLER(ViewHostMsg_SetNeedsBeginFrames
,
375 OnSetNeedsBeginFrames
)
376 IPC_MESSAGE_HANDLER(ViewHostMsg_TextInputStateChanged
,
377 OnTextInputStateChanged
)
378 IPC_MESSAGE_HANDLER(ViewHostMsg_SmartClipDataExtracted
,
379 OnSmartClipDataExtracted
)
380 IPC_MESSAGE_HANDLER(ViewHostMsg_ShowUnhandledTapUIIfNeeded
,
381 OnShowUnhandledTapUIIfNeeded
)
382 IPC_MESSAGE_UNHANDLED(handled
= false)
383 IPC_END_MESSAGE_MAP()
387 void RenderWidgetHostViewAndroid::InitAsChild(gfx::NativeView parent_view
) {
391 void RenderWidgetHostViewAndroid::InitAsPopup(
392 RenderWidgetHostView
* parent_host_view
, const gfx::Rect
& pos
) {
396 void RenderWidgetHostViewAndroid::InitAsFullscreen(
397 RenderWidgetHostView
* reference_host_view
) {
402 RenderWidgetHostViewAndroid::GetRenderWidgetHost() const {
406 void RenderWidgetHostViewAndroid::WasResized() {
410 void RenderWidgetHostViewAndroid::SetSize(const gfx::Size
& size
) {
411 // Ignore the given size as only the Java code has the power to
412 // resize the view on Android.
413 default_size_
= size
;
416 void RenderWidgetHostViewAndroid::SetBounds(const gfx::Rect
& rect
) {
417 SetSize(rect
.size());
420 void RenderWidgetHostViewAndroid::GetScaledContentBitmap(
422 SkColorType preferred_color_type
,
423 gfx::Rect src_subrect
,
424 ReadbackRequestCallback
& result_callback
) {
425 if (!host_
|| host_
->is_hidden() || !IsSurfaceAvailableForCopy()) {
426 result_callback
.Run(SkBitmap(), READBACK_SURFACE_UNAVAILABLE
);
429 gfx::Size bounds
= layer_
->bounds();
430 if (src_subrect
.IsEmpty())
431 src_subrect
= gfx::Rect(bounds
);
432 DCHECK_LE(src_subrect
.width() + src_subrect
.x(), bounds
.width());
433 DCHECK_LE(src_subrect
.height() + src_subrect
.y(), bounds
.height());
434 const gfx::Display
& display
=
435 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
436 float device_scale_factor
= display
.device_scale_factor();
437 DCHECK_GT(device_scale_factor
, 0);
438 gfx::Size
dst_size(gfx::ToCeiledSize(
439 gfx::ScaleSize(src_subrect
.size(), scale
/ device_scale_factor
)));
440 src_subrect
= gfx::ConvertRectToDIP(device_scale_factor
, src_subrect
);
442 CopyFromCompositingSurface(src_subrect
, dst_size
, result_callback
,
443 preferred_color_type
);
446 scoped_refptr
<cc::Layer
> RenderWidgetHostViewAndroid::CreateDelegatedLayer()
448 scoped_refptr
<cc::Layer
> delegated_layer
;
449 if (!surface_id_
.is_null()) {
450 cc::SurfaceManager
* manager
= CompositorImpl::GetSurfaceManager();
452 // manager must outlive compositors using it.
453 scoped_refptr
<cc::SurfaceLayer
> surface_layer
= cc::SurfaceLayer::Create(
454 Compositor::LayerSettings(),
455 base::Bind(&SatisfyCallback
, base::Unretained(manager
)),
456 base::Bind(&RequireCallback
, base::Unretained(manager
)));
457 surface_layer
->SetSurfaceId(surface_id_
, 1.f
, texture_size_in_layer_
);
458 delegated_layer
= surface_layer
;
460 DCHECK(frame_provider_
.get());
461 delegated_layer
= cc::DelegatedRendererLayer::Create(
462 Compositor::LayerSettings(), frame_provider_
);
464 delegated_layer
->SetBounds(content_size_in_layer_
);
465 delegated_layer
->SetIsDrawable(true);
466 delegated_layer
->SetContentsOpaque(true);
468 return delegated_layer
;
471 bool RenderWidgetHostViewAndroid::HasValidFrame() const {
472 if (!content_view_core_
)
477 if (texture_size_in_layer_
.IsEmpty())
479 // This tell us whether a valid frame has arrived or not.
480 if (!frame_evictor_
->HasFrame())
486 gfx::Vector2dF
RenderWidgetHostViewAndroid::GetLastScrollOffset() const {
487 return last_scroll_offset_
;
490 gfx::NativeView
RenderWidgetHostViewAndroid::GetNativeView() const {
491 return content_view_core_
->GetViewAndroid();
494 gfx::NativeViewId
RenderWidgetHostViewAndroid::GetNativeViewId() const {
495 return reinterpret_cast<gfx::NativeViewId
>(
496 const_cast<RenderWidgetHostViewAndroid
*>(this));
499 gfx::NativeViewAccessible
500 RenderWidgetHostViewAndroid::GetNativeViewAccessible() {
505 void RenderWidgetHostViewAndroid::MovePluginWindows(
506 const std::vector
<WebPluginGeometry
>& moves
) {
507 // We don't have plugin windows on Android. Do nothing. Note: this is called
508 // from RenderWidgetHost::OnUpdateRect which is itself invoked while
509 // processing the corresponding message from Renderer.
512 void RenderWidgetHostViewAndroid::Focus() {
514 if (overscroll_controller_
)
515 overscroll_controller_
->Enable();
516 if (content_view_core_
) {
517 WebContentsImpl
* web_contents_impl
=
518 static_cast<WebContentsImpl
*>(content_view_core_
->GetWebContents());
519 if (web_contents_impl
->ShowingInterstitialPage())
520 content_view_core_
->ForceUpdateImeAdapter(GetNativeImeAdapter());
524 bool RenderWidgetHostViewAndroid::HasFocus() const {
525 if (!content_view_core_
)
526 return false; // ContentViewCore not created yet.
528 return content_view_core_
->HasFocus();
531 bool RenderWidgetHostViewAndroid::IsSurfaceAvailableForCopy() const {
532 return HasValidFrame();
535 void RenderWidgetHostViewAndroid::Show() {
543 void RenderWidgetHostViewAndroid::Hide() {
549 bool hide_frontbuffer
= true;
550 bool stop_observing_root_window
= true;
551 HideInternal(hide_frontbuffer
, stop_observing_root_window
);
554 bool RenderWidgetHostViewAndroid::IsShowing() {
555 // ContentViewCoreImpl represents the native side of the Java
556 // ContentViewCore. It being NULL means that it is not attached
557 // to the View system yet, so we treat this RWHVA as hidden.
558 return is_showing_
&& content_view_core_
;
561 void RenderWidgetHostViewAndroid::LockCompositingSurface() {
562 DCHECK(HasValidFrame());
564 DCHECK(frame_evictor_
->HasFrame());
565 frame_evictor_
->LockFrame();
566 locks_on_frame_count_
++;
569 void RenderWidgetHostViewAndroid::UnlockCompositingSurface() {
570 if (!frame_evictor_
->HasFrame() || locks_on_frame_count_
== 0)
573 DCHECK(HasValidFrame());
574 frame_evictor_
->UnlockFrame();
575 locks_on_frame_count_
--;
577 if (locks_on_frame_count_
== 0) {
578 if (last_frame_info_
) {
579 InternalSwapCompositorFrame(last_frame_info_
->output_surface_id
,
580 last_frame_info_
->frame
.Pass());
581 last_frame_info_
.reset();
584 if (!is_showing_
&& layer_
.get())
585 layer_
->SetHideLayerAndSubtree(true);
589 void RenderWidgetHostViewAndroid::SetTextSurroundingSelectionCallback(
590 const TextSurroundingSelectionCallback
& callback
) {
591 // Only one outstanding request is allowed at any given time.
592 DCHECK(!callback
.is_null());
593 text_surrounding_selection_callback_
= callback
;
596 void RenderWidgetHostViewAndroid::OnTextSurroundingSelectionResponse(
597 const base::string16
& content
,
600 if (text_surrounding_selection_callback_
.is_null())
602 text_surrounding_selection_callback_
.Run(content
, start_offset
, end_offset
);
603 text_surrounding_selection_callback_
.Reset();
606 void RenderWidgetHostViewAndroid::OnShowUnhandledTapUIIfNeeded(int x_dip
,
608 if (!content_view_core_
)
610 // Validate the coordinates are within the viewport.
611 gfx::Size viewport_size
= content_view_core_
->GetViewportSizeDip();
612 if (x_dip
< 0 || x_dip
> viewport_size
.width() ||
613 y_dip
< 0 || y_dip
> viewport_size
.height())
615 content_view_core_
->OnShowUnhandledTapUIIfNeeded(x_dip
, y_dip
);
618 void RenderWidgetHostViewAndroid::ReleaseLocksOnSurface() {
619 if (!frame_evictor_
->HasFrame()) {
620 DCHECK_EQ(locks_on_frame_count_
, 0u);
623 while (locks_on_frame_count_
> 0) {
624 UnlockCompositingSurface();
626 RunAckCallbacks(cc::SurfaceDrawStatus::DRAW_SKIPPED
);
629 gfx::Rect
RenderWidgetHostViewAndroid::GetViewBounds() const {
630 if (!content_view_core_
)
631 return gfx::Rect(default_size_
);
633 return gfx::Rect(content_view_core_
->GetViewSize());
636 gfx::Size
RenderWidgetHostViewAndroid::GetPhysicalBackingSize() const {
637 if (!content_view_core_
)
640 return content_view_core_
->GetPhysicalBackingSize();
643 bool RenderWidgetHostViewAndroid::DoTopControlsShrinkBlinkSize() const {
644 if (!content_view_core_
)
647 // Whether or not Blink's viewport size should be shrunk by the height of the
649 return content_view_core_
->DoTopControlsShrinkBlinkSize();
652 float RenderWidgetHostViewAndroid::GetTopControlsHeight() const {
653 if (!content_view_core_
)
656 // The height of the top controls.
657 return content_view_core_
->GetTopControlsHeightDip();
660 void RenderWidgetHostViewAndroid::UpdateCursor(const WebCursor
& cursor
) {
661 // There are no cursors on Android.
664 void RenderWidgetHostViewAndroid::SetIsLoading(bool is_loading
) {
665 // Do nothing. The UI notification is handled through ContentViewClient which
666 // is TabContentsDelegate.
669 void RenderWidgetHostViewAndroid::TextInputTypeChanged(
670 ui::TextInputType type
,
671 ui::TextInputMode input_mode
,
672 bool can_compose_inline
,
674 // Unused on Android, which uses OnTextInputChanged instead.
677 long RenderWidgetHostViewAndroid::GetNativeImeAdapter() {
678 return reinterpret_cast<intptr_t>(&ime_adapter_android_
);
681 void RenderWidgetHostViewAndroid::OnTextInputStateChanged(
682 const ViewHostMsg_TextInputState_Params
& params
) {
683 // If the change is not originated from IME (e.g. Javascript, autofill),
684 // send back the renderer an acknowledgement, regardless of how we exit from
686 base::ScopedClosureRunner ack_caller
;
687 if (params
.is_non_ime_change
)
688 ack_caller
.Reset(base::Bind(&SendImeEventAck
, host_
));
693 content_view_core_
->UpdateImeAdapter(
694 GetNativeImeAdapter(),
695 static_cast<int>(params
.type
), params
.flags
,
696 params
.value
, params
.selection_start
, params
.selection_end
,
697 params
.composition_start
, params
.composition_end
,
698 params
.show_ime_if_needed
, params
.is_non_ime_change
);
701 void RenderWidgetHostViewAndroid::OnDidChangeBodyBackgroundColor(
703 if (cached_background_color_
== color
)
706 cached_background_color_
= color
;
707 if (content_view_core_
)
708 content_view_core_
->OnBackgroundColorChanged(color
);
711 void RenderWidgetHostViewAndroid::OnSetNeedsBeginFrames(bool enabled
) {
712 TRACE_EVENT1("cc", "RenderWidgetHostViewAndroid::OnSetNeedsBeginFrames",
715 RequestVSyncUpdate(PERSISTENT_BEGIN_FRAME
);
717 outstanding_vsync_requests_
&= ~PERSISTENT_BEGIN_FRAME
;
720 void RenderWidgetHostViewAndroid::OnStartContentIntent(
721 const GURL
& content_url
) {
722 if (content_view_core_
)
723 content_view_core_
->StartContentIntent(content_url
);
726 void RenderWidgetHostViewAndroid::OnSmartClipDataExtracted(
727 const base::string16
& text
,
728 const base::string16
& html
,
729 const gfx::Rect rect
) {
730 if (content_view_core_
)
731 content_view_core_
->OnSmartClipDataExtracted(text
, html
, rect
);
734 bool RenderWidgetHostViewAndroid::OnTouchEvent(
735 const ui::MotionEvent
& event
) {
739 // If a browser-based widget consumes the touch event, it's critical that
740 // touch event interception be disabled. This avoids issues with
741 // double-handling for embedder-detected gestures like side swipe.
742 if (selection_controller_
&&
743 selection_controller_
->WillHandleTouchEvent(event
)) {
744 RequestDisallowInterceptTouchEvent();
748 if (stylus_text_selector_
.OnTouchEvent(event
)) {
749 RequestDisallowInterceptTouchEvent();
753 ui::FilteredGestureProvider::TouchHandlingResult result
=
754 gesture_provider_
.OnTouchEvent(event
);
755 if (!result
.succeeded
)
758 blink::WebTouchEvent web_event
=
759 ui::CreateWebTouchEventFromMotionEvent(event
, result
.did_generate_scroll
);
760 host_
->ForwardTouchEventWithLatencyInfo(web_event
, ui::LatencyInfo());
762 // Send a proactive BeginFrame for this vsync to reduce scroll latency for
763 // scroll-inducing touch events. Note that Android's Choreographer ensures
764 // that BeginFrame requests made during ACTION_MOVE dispatch will be honored
765 // in the same vsync phase.
766 if (observing_root_window_
&& result
.did_generate_scroll
)
767 RequestVSyncUpdate(BEGIN_FRAME
);
772 bool RenderWidgetHostViewAndroid::OnTouchHandleEvent(
773 const ui::MotionEvent
& event
) {
774 return selection_controller_
&&
775 selection_controller_
->WillHandleTouchEvent(event
);
778 void RenderWidgetHostViewAndroid::ResetGestureDetection() {
779 const ui::MotionEvent
* current_down_event
=
780 gesture_provider_
.GetCurrentDownEvent();
781 if (!current_down_event
) {
782 // A hard reset ensures prevention of any timer-based events that might fire
783 // after a touch sequence has ended.
784 gesture_provider_
.ResetDetection();
788 scoped_ptr
<ui::MotionEvent
> cancel_event
= current_down_event
->Cancel();
789 if (gesture_provider_
.OnTouchEvent(*cancel_event
).succeeded
) {
790 bool causes_scrolling
= false;
791 host_
->ForwardTouchEventWithLatencyInfo(
792 ui::CreateWebTouchEventFromMotionEvent(*cancel_event
, causes_scrolling
),
797 void RenderWidgetHostViewAndroid::OnDidNavigateMainFrameToNewPage() {
798 ResetGestureDetection();
801 void RenderWidgetHostViewAndroid::SetDoubleTapSupportEnabled(bool enabled
) {
802 gesture_provider_
.SetDoubleTapSupportForPlatformEnabled(enabled
);
805 void RenderWidgetHostViewAndroid::SetMultiTouchZoomSupportEnabled(
807 gesture_provider_
.SetMultiTouchZoomSupportEnabled(enabled
);
810 void RenderWidgetHostViewAndroid::ImeCancelComposition() {
811 ime_adapter_android_
.CancelComposition();
814 void RenderWidgetHostViewAndroid::ImeCompositionRangeChanged(
815 const gfx::Range
& range
,
816 const std::vector
<gfx::Rect
>& character_bounds
) {
817 // TODO(yukawa): Implement this.
820 void RenderWidgetHostViewAndroid::FocusedNodeChanged(bool is_editable_node
) {
821 ime_adapter_android_
.FocusedNodeChanged(is_editable_node
);
824 void RenderWidgetHostViewAndroid::RenderProcessGone(
825 base::TerminationStatus status
, int error_code
) {
829 void RenderWidgetHostViewAndroid::Destroy() {
831 SetContentViewCore(NULL
);
833 if (!surface_id_
.is_null()) {
834 DCHECK(surface_factory_
.get());
835 surface_factory_
->Destroy(surface_id_
);
836 surface_id_
= cc::SurfaceId();
838 surface_factory_
.reset();
840 // The RenderWidgetHost's destruction led here, so don't call it.
846 void RenderWidgetHostViewAndroid::SetTooltipText(
847 const base::string16
& tooltip_text
) {
848 // Tooltips don't makes sense on Android.
851 void RenderWidgetHostViewAndroid::SelectionChanged(const base::string16
& text
,
853 const gfx::Range
& range
) {
854 RenderWidgetHostViewBase::SelectionChanged(text
, offset
, range
);
856 if (!content_view_core_
)
858 if (range
.is_empty()) {
859 content_view_core_
->OnSelectionChanged("");
863 DCHECK(!text
.empty());
864 size_t pos
= range
.GetMin() - offset
;
865 size_t n
= range
.length();
867 DCHECK(pos
+ n
<= text
.length()) << "The text can not fully cover range.";
868 if (pos
>= text
.length()) {
869 NOTREACHED() << "The text can not cover range.";
873 std::string utf8_selection
= base::UTF16ToUTF8(text
.substr(pos
, n
));
875 content_view_core_
->OnSelectionChanged(utf8_selection
);
878 void RenderWidgetHostViewAndroid::SelectionBoundsChanged(
879 const ViewHostMsg_SelectionBounds_Params
& params
) {
880 NOTREACHED() << "Selection bounds should be routed through the compositor.";
883 void RenderWidgetHostViewAndroid::SetBackgroundColor(SkColor color
) {
884 RenderWidgetHostViewBase::SetBackgroundColor(color
);
885 host_
->SetBackgroundOpaque(GetBackgroundOpaque());
886 OnDidChangeBodyBackgroundColor(color
);
889 void RenderWidgetHostViewAndroid::CopyFromCompositingSurface(
890 const gfx::Rect
& src_subrect
,
891 const gfx::Size
& dst_size
,
892 ReadbackRequestCallback
& callback
,
893 const SkColorType preferred_color_type
) {
894 TRACE_EVENT0("cc", "RenderWidgetHostViewAndroid::CopyFromCompositingSurface");
895 if (!host_
|| host_
->is_hidden()) {
896 callback
.Run(SkBitmap(), READBACK_SURFACE_UNAVAILABLE
);
899 base::TimeTicks start_time
= base::TimeTicks::Now();
900 if (using_browser_compositor_
&& !IsSurfaceAvailableForCopy()) {
901 callback
.Run(SkBitmap(), READBACK_SURFACE_UNAVAILABLE
);
904 const gfx::Display
& display
=
905 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
906 float device_scale_factor
= display
.device_scale_factor();
907 gfx::Size dst_size_in_pixel
=
908 gfx::ConvertRectToPixel(device_scale_factor
, gfx::Rect(dst_size
)).size();
909 gfx::Rect src_subrect_in_pixel
=
910 gfx::ConvertRectToPixel(device_scale_factor
, src_subrect
);
912 if (!using_browser_compositor_
) {
913 SynchronousCopyContents(src_subrect_in_pixel
, dst_size_in_pixel
, callback
,
914 preferred_color_type
);
915 UMA_HISTOGRAM_TIMES("Compositing.CopyFromSurfaceTimeSynchronous",
916 base::TimeTicks::Now() - start_time
);
920 scoped_ptr
<cc::CopyOutputRequest
> request
;
921 scoped_refptr
<cc::Layer
> readback_layer
;
922 DCHECK(content_view_core_window_android_
);
923 ui::WindowAndroidCompositor
* compositor
=
924 content_view_core_window_android_
->GetCompositor();
926 DCHECK(frame_provider_
.get() || !surface_id_
.is_null());
927 scoped_refptr
<cc::Layer
> layer
= CreateDelegatedLayer();
929 layer
->SetHideLayerAndSubtree(true);
930 compositor
->AttachLayerForReadback(layer
);
932 readback_layer
= layer
;
933 request
= cc::CopyOutputRequest::CreateRequest(
934 base::Bind(&RenderWidgetHostViewAndroid::
935 PrepareTextureCopyOutputResultForDelegatedReadback
,
936 dst_size_in_pixel
, preferred_color_type
, start_time
,
937 readback_layer
, callback
));
938 if (!src_subrect_in_pixel
.IsEmpty())
939 request
->set_area(src_subrect_in_pixel
);
940 readback_layer
->RequestCopyOfOutput(request
.Pass());
943 void RenderWidgetHostViewAndroid::CopyFromCompositingSurfaceToVideoFrame(
944 const gfx::Rect
& src_subrect
,
945 const scoped_refptr
<media::VideoFrame
>& target
,
946 const base::Callback
<void(bool)>& callback
) {
951 bool RenderWidgetHostViewAndroid::CanCopyToVideoFrame() const {
955 void RenderWidgetHostViewAndroid::ShowDisambiguationPopup(
956 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
) {
957 if (!content_view_core_
)
960 content_view_core_
->ShowDisambiguationPopup(rect_pixels
, zoomed_bitmap
);
963 scoped_ptr
<SyntheticGestureTarget
>
964 RenderWidgetHostViewAndroid::CreateSyntheticGestureTarget() {
965 return scoped_ptr
<SyntheticGestureTarget
>(new SyntheticGestureTargetAndroid(
966 host_
, content_view_core_
->CreateMotionEventSynthesizer()));
969 void RenderWidgetHostViewAndroid::SendDelegatedFrameAck(
970 uint32 output_surface_id
) {
972 cc::CompositorFrameAck ack
;
973 if (!surface_returned_resources_
.empty())
974 ack
.resources
.swap(surface_returned_resources_
);
975 if (resource_collection_
.get())
976 resource_collection_
->TakeUnusedResourcesForChildCompositor(&ack
.resources
);
977 host_
->Send(new ViewMsg_SwapCompositorFrameAck(host_
->GetRoutingID(),
978 output_surface_id
, ack
));
981 void RenderWidgetHostViewAndroid::SendReturnedDelegatedResources(
982 uint32 output_surface_id
) {
984 cc::CompositorFrameAck ack
;
985 if (!surface_returned_resources_
.empty()) {
986 ack
.resources
.swap(surface_returned_resources_
);
988 DCHECK(resource_collection_
.get());
989 resource_collection_
->TakeUnusedResourcesForChildCompositor(&ack
.resources
);
992 host_
->Send(new ViewMsg_ReclaimCompositorResources(host_
->GetRoutingID(),
993 output_surface_id
, ack
));
996 void RenderWidgetHostViewAndroid::UnusedResourcesAreAvailable() {
997 DCHECK(surface_id_
.is_null());
998 if (ack_callbacks_
.size())
1000 SendReturnedDelegatedResources(last_output_surface_id_
);
1003 void RenderWidgetHostViewAndroid::ReturnResources(
1004 const cc::ReturnedResourceArray
& resources
) {
1005 if (resources
.empty())
1007 std::copy(resources
.begin(), resources
.end(),
1008 std::back_inserter(surface_returned_resources_
));
1009 if (!ack_callbacks_
.size())
1010 SendReturnedDelegatedResources(last_output_surface_id_
);
1013 void RenderWidgetHostViewAndroid::DestroyDelegatedContent() {
1015 frame_provider_
= NULL
;
1016 if (!surface_id_
.is_null()) {
1017 DCHECK(surface_factory_
.get());
1018 surface_factory_
->Destroy(surface_id_
);
1019 surface_id_
= cc::SurfaceId();
1024 void RenderWidgetHostViewAndroid::CheckOutputSurfaceChanged(
1025 uint32 output_surface_id
) {
1026 if (output_surface_id
== last_output_surface_id_
)
1028 // Drop the cc::DelegatedFrameResourceCollection so that we will not return
1029 // any resources from the old output surface with the new output surface id.
1030 if (resource_collection_
.get()) {
1031 resource_collection_
->SetClient(NULL
);
1032 if (resource_collection_
->LoseAllResources())
1033 SendReturnedDelegatedResources(last_output_surface_id_
);
1034 resource_collection_
= NULL
;
1036 DestroyDelegatedContent();
1037 surface_factory_
.reset();
1038 if (!surface_returned_resources_
.empty())
1039 SendReturnedDelegatedResources(last_output_surface_id_
);
1041 last_output_surface_id_
= output_surface_id
;
1044 void RenderWidgetHostViewAndroid::SubmitFrame(
1045 scoped_ptr
<cc::CompositorFrame
> frame
) {
1046 cc::SurfaceManager
* manager
= CompositorImpl::GetSurfaceManager();
1048 if (!surface_factory_
) {
1049 surface_factory_
= make_scoped_ptr(new cc::SurfaceFactory(manager
, this));
1051 if (surface_id_
.is_null() ||
1052 texture_size_in_layer_
!= current_surface_size_
) {
1054 if (!surface_id_
.is_null())
1055 surface_factory_
->Destroy(surface_id_
);
1056 surface_id_
= id_allocator_
->GenerateId();
1057 surface_factory_
->Create(surface_id_
);
1058 layer_
= CreateDelegatedLayer();
1062 current_surface_size_
= texture_size_in_layer_
;
1066 cc::SurfaceFactory::DrawCallback ack_callback
=
1067 base::Bind(&RenderWidgetHostViewAndroid::RunAckCallbacks
,
1068 weak_ptr_factory_
.GetWeakPtr());
1069 surface_factory_
->SubmitFrame(surface_id_
, frame
.Pass(), ack_callback
);
1071 if (!resource_collection_
.get()) {
1072 resource_collection_
= new cc::DelegatedFrameResourceCollection
;
1073 resource_collection_
->SetClient(this);
1075 if (!frame_provider_
.get() ||
1076 texture_size_in_layer_
!= frame_provider_
->frame_size()) {
1078 frame_provider_
= new cc::DelegatedFrameProvider(
1079 resource_collection_
.get(), frame
->delegated_frame_data
.Pass());
1080 layer_
= cc::DelegatedRendererLayer::Create(Compositor::LayerSettings(),
1084 frame_provider_
->SetFrameData(frame
->delegated_frame_data
.Pass());
1089 void RenderWidgetHostViewAndroid::SwapDelegatedFrame(
1090 uint32 output_surface_id
,
1091 scoped_ptr
<cc::CompositorFrame
> frame
) {
1092 CheckOutputSurfaceChanged(output_surface_id
);
1093 bool has_content
= !texture_size_in_layer_
.IsEmpty();
1095 // DelegatedRendererLayerImpl applies the inverse device_scale_factor of the
1096 // renderer frame, assuming that the browser compositor will scale
1097 // it back up to device scale. But on Android we put our browser layers in
1098 // physical pixels and set our browser CC device_scale_factor to 1, so this
1099 // suppresses the transform. This line may need to be removed when fixing
1100 // http://crbug.com/384134 or http://crbug.com/310763
1101 frame
->delegated_frame_data
->device_scale_factor
= 1.0f
;
1104 DestroyDelegatedContent();
1106 SubmitFrame(frame
.Pass());
1110 layer_
->SetIsDrawable(true);
1111 layer_
->SetContentsOpaque(true);
1112 layer_
->SetBounds(content_size_in_layer_
);
1115 base::Closure ack_callback
=
1116 base::Bind(&RenderWidgetHostViewAndroid::SendDelegatedFrameAck
,
1117 weak_ptr_factory_
.GetWeakPtr(),
1120 ack_callbacks_
.push(ack_callback
);
1121 if (host_
->is_hidden())
1122 RunAckCallbacks(cc::SurfaceDrawStatus::DRAW_SKIPPED
);
1125 void RenderWidgetHostViewAndroid::ComputeContentsSize(
1126 const cc::CompositorFrameMetadata
& frame_metadata
) {
1127 // Calculate the content size. This should be 0 if the texture_size is 0.
1128 gfx::Vector2dF offset
;
1129 if (texture_size_in_layer_
.IsEmpty())
1130 content_size_in_layer_
= gfx::Size();
1131 content_size_in_layer_
= gfx::ToCeiledSize(gfx::ScaleSize(
1132 frame_metadata
.scrollable_viewport_size
,
1133 frame_metadata
.device_scale_factor
* frame_metadata
.page_scale_factor
));
1137 void RenderWidgetHostViewAndroid::InternalSwapCompositorFrame(
1138 uint32 output_surface_id
,
1139 scoped_ptr
<cc::CompositorFrame
> frame
) {
1140 last_scroll_offset_
= frame
->metadata
.root_scroll_offset
;
1141 if (!frame
->delegated_frame_data
) {
1142 LOG(ERROR
) << "Non-delegated renderer path no longer supported";
1146 if (locks_on_frame_count_
> 0) {
1147 DCHECK(HasValidFrame());
1148 RetainFrame(output_surface_id
, frame
.Pass());
1152 if (!CompositorImpl::GetSurfaceManager() && layer_
.get() &&
1153 layer_
->layer_tree_host()) {
1154 for (size_t i
= 0; i
< frame
->metadata
.latency_info
.size(); i
++) {
1155 scoped_ptr
<cc::SwapPromise
> swap_promise(
1156 new cc::LatencyInfoSwapPromise(frame
->metadata
.latency_info
[i
]));
1157 layer_
->layer_tree_host()->QueueSwapPromise(swap_promise
.Pass());
1161 DCHECK(!frame
->delegated_frame_data
->render_pass_list
.empty());
1163 cc::RenderPass
* root_pass
=
1164 frame
->delegated_frame_data
->render_pass_list
.back();
1165 texture_size_in_layer_
= root_pass
->output_rect
.size();
1166 ComputeContentsSize(frame
->metadata
);
1168 cc::CompositorFrameMetadata metadata
= frame
->metadata
;
1170 SwapDelegatedFrame(output_surface_id
, frame
.Pass());
1171 frame_evictor_
->SwappedFrame(!host_
->is_hidden());
1173 // As the metadata update may trigger view invalidation, always call it after
1174 // any potential compositor scheduling.
1175 OnFrameMetadataUpdated(metadata
);
1178 void RenderWidgetHostViewAndroid::OnSwapCompositorFrame(
1179 uint32 output_surface_id
,
1180 scoped_ptr
<cc::CompositorFrame
> frame
) {
1181 InternalSwapCompositorFrame(output_surface_id
, frame
.Pass());
1184 void RenderWidgetHostViewAndroid::RetainFrame(
1185 uint32 output_surface_id
,
1186 scoped_ptr
<cc::CompositorFrame
> frame
) {
1187 DCHECK(locks_on_frame_count_
);
1189 // Store the incoming frame so that it can be swapped when all the locks have
1190 // been released. If there is already a stored frame, then replace and skip
1191 // the previous one but make sure we still eventually send the ACK. Holding
1192 // the ACK also blocks the renderer when its max_frames_pending is reached.
1193 if (last_frame_info_
) {
1194 base::Closure ack_callback
=
1195 base::Bind(&RenderWidgetHostViewAndroid::SendDelegatedFrameAck
,
1196 weak_ptr_factory_
.GetWeakPtr(),
1197 last_frame_info_
->output_surface_id
);
1199 ack_callbacks_
.push(ack_callback
);
1202 last_frame_info_
.reset(new LastFrameInfo(output_surface_id
, frame
.Pass()));
1205 void RenderWidgetHostViewAndroid::SynchronousFrameMetadata(
1206 const cc::CompositorFrameMetadata
& frame_metadata
) {
1207 if (!content_view_core_
)
1210 // This is a subset of OnSwapCompositorFrame() used in the synchronous
1212 OnFrameMetadataUpdated(frame_metadata
);
1213 ComputeContentsSize(frame_metadata
);
1215 // DevTools ScreenCast support for Android WebView.
1216 WebContents
* web_contents
= content_view_core_
->GetWebContents();
1217 if (DevToolsAgentHost::HasFor(web_contents
)) {
1218 scoped_refptr
<DevToolsAgentHost
> dtah
=
1219 DevToolsAgentHost::GetOrCreateFor(web_contents
);
1220 // Unblock the compositor.
1221 BrowserThread::PostTask(
1222 BrowserThread::UI
, FROM_HERE
,
1224 &RenderFrameDevToolsAgentHost::SynchronousSwapCompositorFrame
,
1225 static_cast<RenderFrameDevToolsAgentHost
*>(dtah
.get()),
1230 void RenderWidgetHostViewAndroid::SetOverlayVideoMode(bool enabled
) {
1232 layer_
->SetContentsOpaque(!enabled
);
1235 bool RenderWidgetHostViewAndroid::SupportsAnimation() const {
1236 // The synchronous (WebView) compositor does not have a proper browser
1237 // compositor with which to drive animations.
1238 return using_browser_compositor_
;
1241 void RenderWidgetHostViewAndroid::SetNeedsAnimate() {
1242 DCHECK(content_view_core_window_android_
);
1243 DCHECK(using_browser_compositor_
);
1244 content_view_core_window_android_
->SetNeedsAnimate();
1247 void RenderWidgetHostViewAndroid::MoveCaret(const gfx::PointF
& position
) {
1248 MoveCaret(gfx::Point(position
.x(), position
.y()));
1251 void RenderWidgetHostViewAndroid::MoveRangeSelectionExtent(
1252 const gfx::PointF
& extent
) {
1253 DCHECK(content_view_core_
);
1254 content_view_core_
->MoveRangeSelectionExtent(extent
);
1257 void RenderWidgetHostViewAndroid::SelectBetweenCoordinates(
1258 const gfx::PointF
& base
,
1259 const gfx::PointF
& extent
) {
1260 DCHECK(content_view_core_
);
1261 content_view_core_
->SelectBetweenCoordinates(base
, extent
);
1264 void RenderWidgetHostViewAndroid::OnSelectionEvent(
1265 ui::SelectionEventType event
) {
1266 DCHECK(content_view_core_
);
1267 DCHECK(selection_controller_
);
1268 // If a selection drag has started, it has taken over the active touch
1269 // sequence. Immediately cancel gesture detection and any downstream touch
1270 // listeners (e.g., web content) to communicate this transfer.
1271 if (event
== ui::SELECTION_HANDLES_SHOWN
)
1272 ResetGestureDetection();
1273 content_view_core_
->OnSelectionEvent(
1274 event
, selection_controller_
->GetStartPosition(),
1275 GetSelectionRect(*selection_controller_
));
1278 scoped_ptr
<ui::TouchHandleDrawable
>
1279 RenderWidgetHostViewAndroid::CreateDrawable() {
1280 DCHECK(content_view_core_
);
1281 if (!using_browser_compositor_
)
1282 return content_view_core_
->CreatePopupTouchHandleDrawable();
1284 return scoped_ptr
<ui::TouchHandleDrawable
>(new CompositedTouchHandleDrawable(
1285 content_view_core_
->GetLayer().get(),
1286 content_view_core_
->GetDpiScale(),
1287 // Use the activity context (instead of the application context) to ensure
1288 // proper handle theming.
1289 content_view_core_
->GetContext().obj()));
1292 void RenderWidgetHostViewAndroid::SynchronousCopyContents(
1293 const gfx::Rect
& src_subrect_in_pixel
,
1294 const gfx::Size
& dst_size_in_pixel
,
1295 ReadbackRequestCallback
& callback
,
1296 const SkColorType color_type
) {
1297 gfx::Size input_size_in_pixel
;
1298 if (src_subrect_in_pixel
.IsEmpty())
1299 input_size_in_pixel
= content_size_in_layer_
;
1301 input_size_in_pixel
= src_subrect_in_pixel
.size();
1303 gfx::Size output_size_in_pixel
;
1304 if (dst_size_in_pixel
.IsEmpty())
1305 output_size_in_pixel
= input_size_in_pixel
;
1307 output_size_in_pixel
= dst_size_in_pixel
;
1308 int output_width
= output_size_in_pixel
.width();
1309 int output_height
= output_size_in_pixel
.height();
1311 SynchronousCompositor
* compositor
=
1312 SynchronousCompositorImpl::FromID(host_
->GetProcess()->GetID(),
1313 host_
->GetRoutingID());
1315 callback
.Run(SkBitmap(), READBACK_FAILED
);
1320 bitmap
.allocPixels(SkImageInfo::Make(output_width
,
1323 kPremul_SkAlphaType
));
1324 SkCanvas
canvas(bitmap
);
1326 (float)output_width
/ (float)input_size_in_pixel
.width(),
1327 (float)output_height
/ (float)input_size_in_pixel
.height());
1328 compositor
->DemandDrawSw(&canvas
);
1329 callback
.Run(bitmap
, READBACK_SUCCESS
);
1332 void RenderWidgetHostViewAndroid::OnFrameMetadataUpdated(
1333 const cc::CompositorFrameMetadata
& frame_metadata
) {
1334 bool is_mobile_optimized
= IsMobileOptimizedFrame(frame_metadata
);
1335 gesture_provider_
.SetDoubleTapSupportForPageEnabled(!is_mobile_optimized
);
1337 if (!content_view_core_
)
1340 if (overscroll_controller_
)
1341 overscroll_controller_
->OnFrameMetadataUpdated(frame_metadata
);
1343 if (selection_controller_
) {
1344 selection_controller_
->OnSelectionEditable(
1345 frame_metadata
.selection
.is_editable
);
1346 selection_controller_
->OnSelectionEmpty(
1347 frame_metadata
.selection
.is_empty_text_form_control
);
1348 selection_controller_
->OnSelectionBoundsChanged(
1349 ConvertSelectionBound(frame_metadata
.selection
.start
),
1350 ConvertSelectionBound(frame_metadata
.selection
.end
));
1353 // All offsets and sizes are in CSS pixels.
1354 content_view_core_
->UpdateFrameInfo(
1355 frame_metadata
.root_scroll_offset
,
1356 frame_metadata
.page_scale_factor
,
1357 gfx::Vector2dF(frame_metadata
.min_page_scale_factor
,
1358 frame_metadata
.max_page_scale_factor
),
1359 frame_metadata
.root_layer_size
,
1360 frame_metadata
.scrollable_viewport_size
,
1361 frame_metadata
.location_bar_offset
,
1362 frame_metadata
.location_bar_content_translation
,
1363 is_mobile_optimized
);
1364 #if defined(VIDEO_HOLE)
1365 if (host_
&& host_
->IsRenderView()) {
1366 RenderViewHostImpl
* rvhi
= static_cast<RenderViewHostImpl
*>(
1367 RenderViewHost::From(host_
));
1368 WebContentsImpl
* web_contents_impl
=
1369 static_cast<WebContentsImpl
*>(WebContents::FromRenderViewHost(rvhi
));
1370 if (web_contents_impl
)
1371 web_contents_impl
->media_web_contents_observer()->OnFrameInfoUpdated();
1373 #endif // defined(VIDEO_HOLE)
1376 void RenderWidgetHostViewAndroid::AcceleratedSurfaceInitialized(int route_id
) {
1377 // TODO: remove need for the surface id here
1378 accelerated_surface_route_id_
= route_id
;
1381 void RenderWidgetHostViewAndroid::ShowInternal() {
1382 DCHECK(is_showing_
);
1383 if (!host_
|| !host_
->is_hidden())
1387 layer_
->SetHideLayerAndSubtree(false);
1389 frame_evictor_
->SetVisible(true);
1391 if (overscroll_controller_
)
1392 overscroll_controller_
->Enable();
1394 host_
->WasShown(ui::LatencyInfo());
1396 if (content_view_core_
) {
1397 StartObservingRootWindow();
1398 RequestVSyncUpdate(BEGIN_FRAME
);
1402 void RenderWidgetHostViewAndroid::HideInternal(
1403 bool hide_frontbuffer
,
1404 bool stop_observing_root_window
) {
1405 if (hide_frontbuffer
) {
1406 if (layer_
.get() && locks_on_frame_count_
== 0)
1407 layer_
->SetHideLayerAndSubtree(true);
1409 frame_evictor_
->SetVisible(false);
1412 if (stop_observing_root_window
)
1413 StopObservingRootWindow();
1415 if (!host_
|| host_
->is_hidden())
1418 if (overscroll_controller_
)
1419 overscroll_controller_
->Disable();
1421 RunAckCallbacks(cc::SurfaceDrawStatus::DRAW_SKIPPED
);
1423 // Inform the renderer that we are being hidden so it can reduce its resource
1428 void RenderWidgetHostViewAndroid::AttachLayers() {
1429 if (!content_view_core_
)
1434 content_view_core_
->AttachLayer(layer_
);
1435 if (overscroll_controller_
)
1436 overscroll_controller_
->Enable();
1437 layer_
->SetHideLayerAndSubtree(!is_showing_
);
1440 void RenderWidgetHostViewAndroid::RemoveLayers() {
1441 if (!content_view_core_
)
1447 content_view_core_
->RemoveLayer(layer_
);
1448 if (overscroll_controller_
)
1449 overscroll_controller_
->Disable();
1452 void RenderWidgetHostViewAndroid::RequestVSyncUpdate(uint32 requests
) {
1453 bool should_request_vsync
= !outstanding_vsync_requests_
&& requests
;
1454 outstanding_vsync_requests_
|= requests
;
1456 // If the host has been hidden, defer vsync requests until it is shown
1457 // again via |Show()|.
1458 if (!host_
|| host_
->is_hidden())
1461 // Note that if we're not currently observing the root window, outstanding
1462 // vsync requests will be pushed if/when we resume observing in
1463 // |StartObservingRootWindow()|.
1464 if (observing_root_window_
&& should_request_vsync
)
1465 content_view_core_window_android_
->RequestVSyncUpdate();
1468 void RenderWidgetHostViewAndroid::StartObservingRootWindow() {
1469 DCHECK(content_view_core_window_android_
);
1470 DCHECK(is_showing_
);
1471 if (observing_root_window_
)
1474 observing_root_window_
= true;
1475 content_view_core_window_android_
->AddObserver(this);
1477 // Clear existing vsync requests to allow a request to the new window.
1478 uint32 outstanding_vsync_requests
= outstanding_vsync_requests_
;
1479 outstanding_vsync_requests_
= 0;
1480 RequestVSyncUpdate(outstanding_vsync_requests
);
1483 void RenderWidgetHostViewAndroid::StopObservingRootWindow() {
1484 if (!content_view_core_window_android_
) {
1485 DCHECK(!observing_root_window_
);
1489 if (!observing_root_window_
)
1492 observing_root_window_
= false;
1493 content_view_core_window_android_
->RemoveObserver(this);
1496 void RenderWidgetHostViewAndroid::SendBeginFrame(base::TimeTicks frame_time
,
1497 base::TimeDelta vsync_period
) {
1498 TRACE_EVENT1("cc", "RenderWidgetHostViewAndroid::SendBeginFrame",
1499 "frame_time_us", frame_time
.ToInternalValue());
1501 if (using_browser_compositor_
) {
1502 // TODO(brianderson): Replace this hardcoded deadline after Android
1503 // switches to Surfaces and the Browser's commit isn't in the critcal path.
1504 base::TimeTicks deadline
= frame_time
+ (vsync_period
* 0.6);
1506 host_
->Send(new ViewMsg_BeginFrame(
1507 host_
->GetRoutingID(),
1508 cc::BeginFrameArgs::Create(BEGINFRAME_FROM_HERE
, frame_time
, deadline
,
1509 vsync_period
, cc::BeginFrameArgs::NORMAL
)));
1511 SynchronousCompositorImpl
* compositor
= SynchronousCompositorImpl::FromID(
1512 host_
->GetProcess()->GetID(), host_
->GetRoutingID());
1514 // The synchronous compositor synchronously does it's work in this call.
1515 // It does not use a deadline.
1516 compositor
->BeginFrame(cc::BeginFrameArgs::Create(
1517 BEGINFRAME_FROM_HERE
, frame_time
, base::TimeTicks(), vsync_period
,
1518 cc::BeginFrameArgs::NORMAL
));
1523 bool RenderWidgetHostViewAndroid::Animate(base::TimeTicks frame_time
) {
1524 bool needs_animate
= false;
1525 if (overscroll_controller_
) {
1526 needs_animate
|= overscroll_controller_
->Animate(
1527 frame_time
, content_view_core_
->GetLayer().get());
1529 if (selection_controller_
)
1530 needs_animate
|= selection_controller_
->Animate(frame_time
);
1531 return needs_animate
;
1534 void RenderWidgetHostViewAndroid::RequestDisallowInterceptTouchEvent() {
1535 if (content_view_core_
)
1536 content_view_core_
->RequestDisallowInterceptTouchEvent();
1539 void RenderWidgetHostViewAndroid::EvictDelegatedFrame() {
1541 DestroyDelegatedContent();
1542 frame_evictor_
->DiscardedFrame();
1545 bool RenderWidgetHostViewAndroid::HasAcceleratedSurface(
1546 const gfx::Size
& desired_size
) {
1551 void RenderWidgetHostViewAndroid::GetScreenInfo(blink::WebScreenInfo
* result
) {
1552 // ScreenInfo isn't tied to the widget on Android. Always return the default.
1553 RenderWidgetHostViewBase::GetDefaultScreenInfo(result
);
1556 // TODO(jrg): Find out the implications and answer correctly here,
1557 // as we are returning the WebView and not root window bounds.
1558 gfx::Rect
RenderWidgetHostViewAndroid::GetBoundsInRootWindow() {
1559 return GetViewBounds();
1562 gfx::GLSurfaceHandle
RenderWidgetHostViewAndroid::GetCompositingSurface() {
1563 gfx::GLSurfaceHandle handle
=
1564 gfx::GLSurfaceHandle(gfx::kNullPluginWindow
, gfx::NULL_TRANSPORT
);
1565 if (using_browser_compositor_
) {
1566 handle
.parent_client_id
=
1567 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId();
1572 void RenderWidgetHostViewAndroid::ProcessAckedTouchEvent(
1573 const TouchEventWithLatencyInfo
& touch
, InputEventAckState ack_result
) {
1574 const bool event_consumed
= ack_result
== INPUT_EVENT_ACK_STATE_CONSUMED
;
1575 gesture_provider_
.OnTouchEventAck(touch
.event
.uniqueTouchEventId
,
1579 void RenderWidgetHostViewAndroid::GestureEventAck(
1580 const blink::WebGestureEvent
& event
,
1581 InputEventAckState ack_result
) {
1582 if (overscroll_controller_
)
1583 overscroll_controller_
->OnGestureEventAck(event
, ack_result
);
1585 if (content_view_core_
)
1586 content_view_core_
->OnGestureEventAck(event
, ack_result
);
1589 InputEventAckState
RenderWidgetHostViewAndroid::FilterInputEvent(
1590 const blink::WebInputEvent
& input_event
) {
1591 if (selection_controller_
&&
1592 blink::WebInputEvent::isGestureEventType(input_event
.type
)) {
1593 const blink::WebGestureEvent
& gesture_event
=
1594 static_cast<const blink::WebGestureEvent
&>(input_event
);
1595 switch (gesture_event
.type
) {
1596 case blink::WebInputEvent::GestureLongPress
:
1597 if (selection_controller_
->WillHandleLongPressEvent(
1599 base::TimeDelta::FromSecondsD(input_event
.timeStampSeconds
),
1600 gfx::PointF(gesture_event
.x
, gesture_event
.y
))) {
1601 return INPUT_EVENT_ACK_STATE_CONSUMED
;
1605 case blink::WebInputEvent::GestureTap
:
1606 if (selection_controller_
->WillHandleTapEvent(
1607 gfx::PointF(gesture_event
.x
, gesture_event
.y
))) {
1608 return INPUT_EVENT_ACK_STATE_CONSUMED
;
1617 if (overscroll_controller_
&&
1618 blink::WebInputEvent::isGestureEventType(input_event
.type
) &&
1619 overscroll_controller_
->WillHandleGestureEvent(
1620 static_cast<const blink::WebGestureEvent
&>(input_event
))) {
1621 return INPUT_EVENT_ACK_STATE_CONSUMED
;
1624 if (content_view_core_
&& content_view_core_
->FilterInputEvent(input_event
))
1625 return INPUT_EVENT_ACK_STATE_CONSUMED
;
1628 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED
;
1630 if (input_event
.type
== blink::WebInputEvent::GestureTapDown
||
1631 input_event
.type
== blink::WebInputEvent::TouchStart
) {
1632 GpuDataManagerImpl
* gpu_data
= GpuDataManagerImpl::GetInstance();
1633 GpuProcessHostUIShim
* shim
= GpuProcessHostUIShim::GetOneInstance();
1634 if (shim
&& gpu_data
&& accelerated_surface_route_id_
&&
1635 gpu_data
->IsDriverBugWorkaroundActive(gpu::WAKE_UP_GPU_BEFORE_DRAWING
))
1637 new AcceleratedSurfaceMsg_WakeUpGpu(accelerated_surface_route_id_
));
1640 SynchronousCompositorImpl
* compositor
=
1641 SynchronousCompositorImpl::FromID(host_
->GetProcess()->GetID(),
1642 host_
->GetRoutingID());
1644 return compositor
->HandleInputEvent(input_event
);
1645 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED
;
1648 void RenderWidgetHostViewAndroid::OnSetNeedsFlushInput() {
1649 TRACE_EVENT0("input", "RenderWidgetHostViewAndroid::OnSetNeedsFlushInput");
1650 RequestVSyncUpdate(FLUSH_INPUT
);
1653 BrowserAccessibilityManager
*
1654 RenderWidgetHostViewAndroid::CreateBrowserAccessibilityManager(
1655 BrowserAccessibilityDelegate
* delegate
) {
1656 // TODO(dmazzoni): Currently there can only be one
1657 // BrowserAccessibilityManager per ContentViewCore, so return NULL
1658 // if there's already a BrowserAccessibilityManager for the main
1659 // frame. Eventually, in order to support cross-process iframes on
1660 // Android we'll need to add support for a
1661 // BrowserAccessibilityManager for a child frame.
1662 // http://crbug.com/423846
1663 if (!host_
|| host_
->GetRootBrowserAccessibilityManager())
1666 base::android::ScopedJavaLocalRef
<jobject
> obj
;
1667 if (content_view_core_
)
1668 obj
= content_view_core_
->GetJavaObject();
1669 return new BrowserAccessibilityManagerAndroid(
1671 BrowserAccessibilityManagerAndroid::GetEmptyDocument(),
1675 bool RenderWidgetHostViewAndroid::LockMouse() {
1680 void RenderWidgetHostViewAndroid::UnlockMouse() {
1684 // Methods called from the host to the render
1686 void RenderWidgetHostViewAndroid::SendKeyEvent(
1687 const NativeWebKeyboardEvent
& event
) {
1689 host_
->ForwardKeyboardEvent(event
);
1692 void RenderWidgetHostViewAndroid::SendMouseEvent(
1693 const blink::WebMouseEvent
& event
) {
1695 host_
->ForwardMouseEvent(event
);
1698 void RenderWidgetHostViewAndroid::SendMouseWheelEvent(
1699 const blink::WebMouseWheelEvent
& event
) {
1701 host_
->ForwardWheelEvent(event
);
1704 void RenderWidgetHostViewAndroid::SendGestureEvent(
1705 const blink::WebGestureEvent
& event
) {
1706 // Sending a gesture that may trigger overscroll should resume the effect.
1707 if (overscroll_controller_
)
1708 overscroll_controller_
->Enable();
1711 host_
->ForwardGestureEventWithLatencyInfo(event
, ui::LatencyInfo());
1714 void RenderWidgetHostViewAndroid::MoveCaret(const gfx::Point
& point
) {
1716 host_
->MoveCaret(point
);
1719 void RenderWidgetHostViewAndroid::DismissTextHandles() {
1720 if (selection_controller_
)
1721 selection_controller_
->HideAndDisallowShowingAutomatically();
1724 void RenderWidgetHostViewAndroid::SetTextHandlesTemporarilyHidden(bool hidden
) {
1725 if (selection_controller_
)
1726 selection_controller_
->SetTemporarilyHidden(hidden
);
1729 void RenderWidgetHostViewAndroid::OnShowingPastePopup(
1730 const gfx::PointF
& point
) {
1731 if (!selection_controller_
)
1734 // As the paste popup may be triggered *before* the bounds and editability
1735 // of the region have been updated, explicitly set the properties now.
1736 // TODO(jdduke): Remove this workaround when auxiliary paste popup
1737 // notifications are no longer required, crbug.com/398170.
1738 ui::SelectionBound insertion_bound
;
1739 insertion_bound
.set_type(ui::SelectionBound::CENTER
);
1740 insertion_bound
.set_visible(true);
1741 insertion_bound
.SetEdge(point
, point
);
1742 selection_controller_
->HideAndDisallowShowingAutomatically();
1743 selection_controller_
->OnSelectionEditable(true);
1744 selection_controller_
->OnSelectionEmpty(true);
1745 selection_controller_
->OnSelectionBoundsChanged(insertion_bound
,
1747 selection_controller_
->AllowShowingFromCurrentSelection();
1750 SkColor
RenderWidgetHostViewAndroid::GetCachedBackgroundColor() const {
1751 return cached_background_color_
;
1754 void RenderWidgetHostViewAndroid::DidOverscroll(
1755 const DidOverscrollParams
& params
) {
1756 if (!content_view_core_
|| !layer_
.get() || !is_showing_
)
1759 if (overscroll_controller_
)
1760 overscroll_controller_
->OnOverscrolled(params
);
1763 void RenderWidgetHostViewAndroid::DidStopFlinging() {
1764 if (content_view_core_
)
1765 content_view_core_
->DidStopFlinging();
1768 uint32_t RenderWidgetHostViewAndroid::GetSurfaceIdNamespace() {
1770 return id_allocator_
->id_namespace();
1774 void RenderWidgetHostViewAndroid::SetContentViewCore(
1775 ContentViewCoreImpl
* content_view_core
) {
1777 StopObservingRootWindow();
1779 bool resize
= false;
1780 if (content_view_core
!= content_view_core_
) {
1781 overscroll_controller_
.reset();
1782 selection_controller_
.reset();
1783 ReleaseLocksOnSurface();
1787 content_view_core_
= content_view_core
;
1788 content_view_core_window_android_
=
1789 content_view_core_
? content_view_core_
->GetWindowAndroid() : nullptr;
1790 DCHECK_EQ(!!content_view_core_
, !!content_view_core_window_android_
);
1792 BrowserAccessibilityManager
* manager
= NULL
;
1794 manager
= host_
->GetRootBrowserAccessibilityManager();
1796 base::android::ScopedJavaLocalRef
<jobject
> obj
;
1797 if (content_view_core_
)
1798 obj
= content_view_core_
->GetJavaObject();
1799 manager
->ToBrowserAccessibilityManagerAndroid()->SetContentViewCore(obj
);
1804 if (!content_view_core_
)
1808 StartObservingRootWindow();
1813 if (!selection_controller_
)
1814 selection_controller_
= CreateSelectionController(this, content_view_core_
);
1816 if (!overscroll_controller_
&&
1817 content_view_core_window_android_
->GetCompositor()) {
1818 overscroll_controller_
= CreateOverscrollController(content_view_core_
);
1822 void RenderWidgetHostViewAndroid::RunAckCallbacks(
1823 cc::SurfaceDrawStatus status
) {
1824 while (!ack_callbacks_
.empty()) {
1825 ack_callbacks_
.front().Run();
1826 ack_callbacks_
.pop();
1830 void RenderWidgetHostViewAndroid::OnGestureEvent(
1831 const ui::GestureEventData
& gesture
) {
1832 blink::WebGestureEvent web_gesture
=
1833 ui::CreateWebGestureEventFromGestureEventData(gesture
);
1834 // TODO(jdduke): Remove this workaround after Android fixes UiAutomator to
1835 // stop providing shift meta values to synthetic MotionEvents. This prevents
1836 // unintended shift+click interpretation of all accessibility clicks.
1837 // See crbug.com/443247.
1838 if (web_gesture
.type
== blink::WebInputEvent::GestureTap
&&
1839 web_gesture
.modifiers
== blink::WebInputEvent::ShiftKey
) {
1840 web_gesture
.modifiers
= 0;
1842 SendGestureEvent(web_gesture
);
1845 void RenderWidgetHostViewAndroid::OnCompositingDidCommit() {
1846 RunAckCallbacks(cc::SurfaceDrawStatus::DRAWN
);
1849 void RenderWidgetHostViewAndroid::OnRootWindowVisibilityChanged(bool visible
) {
1850 DCHECK(is_showing_
);
1854 bool hide_frontbuffer
= true;
1855 bool stop_observing_root_window
= false;
1856 HideInternal(hide_frontbuffer
, stop_observing_root_window
);
1860 void RenderWidgetHostViewAndroid::OnAttachCompositor() {
1861 DCHECK(content_view_core_
);
1862 if (!overscroll_controller_
)
1863 overscroll_controller_
= CreateOverscrollController(content_view_core_
);
1866 void RenderWidgetHostViewAndroid::OnDetachCompositor() {
1867 DCHECK(content_view_core_
);
1868 DCHECK(using_browser_compositor_
);
1869 RunAckCallbacks(cc::SurfaceDrawStatus::DRAW_SKIPPED
);
1870 overscroll_controller_
.reset();
1873 void RenderWidgetHostViewAndroid::OnVSync(base::TimeTicks frame_time
,
1874 base::TimeDelta vsync_period
) {
1875 TRACE_EVENT0("cc,benchmark", "RenderWidgetHostViewAndroid::OnVSync");
1876 if (!host_
|| host_
->is_hidden())
1879 if (outstanding_vsync_requests_
& FLUSH_INPUT
) {
1880 outstanding_vsync_requests_
&= ~FLUSH_INPUT
;
1881 host_
->FlushInput();
1884 if (outstanding_vsync_requests_
& BEGIN_FRAME
||
1885 outstanding_vsync_requests_
& PERSISTENT_BEGIN_FRAME
) {
1886 outstanding_vsync_requests_
&= ~BEGIN_FRAME
;
1887 SendBeginFrame(frame_time
, vsync_period
);
1890 // This allows for SendBeginFrame and FlushInput to modify
1891 // outstanding_vsync_requests.
1892 uint32 outstanding_vsync_requests
= outstanding_vsync_requests_
;
1893 outstanding_vsync_requests_
= 0;
1894 RequestVSyncUpdate(outstanding_vsync_requests
);
1897 void RenderWidgetHostViewAndroid::OnAnimate(base::TimeTicks begin_frame_time
) {
1898 if (Animate(begin_frame_time
))
1902 void RenderWidgetHostViewAndroid::OnActivityStopped() {
1903 TRACE_EVENT0("browser", "RenderWidgetHostViewAndroid::OnActivityStopped");
1904 DCHECK(is_showing_
);
1905 bool hide_frontbuffer
= false;
1906 bool stop_observing_root_window
= false;
1907 HideInternal(hide_frontbuffer
, stop_observing_root_window
);
1910 void RenderWidgetHostViewAndroid::OnActivityStarted() {
1911 TRACE_EVENT0("browser", "RenderWidgetHostViewAndroid::OnActivityStarted");
1912 DCHECK(is_showing_
);
1916 void RenderWidgetHostViewAndroid::OnLostResources() {
1917 ReleaseLocksOnSurface();
1919 DestroyDelegatedContent();
1920 DCHECK(ack_callbacks_
.empty());
1924 void RenderWidgetHostViewAndroid::
1925 PrepareTextureCopyOutputResultForDelegatedReadback(
1926 const gfx::Size
& dst_size_in_pixel
,
1927 SkColorType color_type
,
1928 const base::TimeTicks
& start_time
,
1929 scoped_refptr
<cc::Layer
> readback_layer
,
1930 ReadbackRequestCallback
& callback
,
1931 scoped_ptr
<cc::CopyOutputResult
> result
) {
1932 readback_layer
->RemoveFromParent();
1933 PrepareTextureCopyOutputResult(
1934 dst_size_in_pixel
, color_type
, start_time
, callback
, result
.Pass());
1938 void RenderWidgetHostViewAndroid::PrepareTextureCopyOutputResult(
1939 const gfx::Size
& dst_size_in_pixel
,
1940 SkColorType color_type
,
1941 const base::TimeTicks
& start_time
,
1942 ReadbackRequestCallback
& callback
,
1943 scoped_ptr
<cc::CopyOutputResult
> result
) {
1944 base::ScopedClosureRunner
scoped_callback_runner(
1945 base::Bind(callback
, SkBitmap(), READBACK_FAILED
));
1947 "RenderWidgetHostViewAndroid::PrepareTextureCopyOutputResult");
1949 if (!result
->HasTexture() || result
->IsEmpty() || result
->size().IsEmpty())
1952 gfx::Size output_size_in_pixel
;
1953 if (dst_size_in_pixel
.IsEmpty())
1954 output_size_in_pixel
= result
->size();
1956 output_size_in_pixel
= dst_size_in_pixel
;
1958 GLHelper
* gl_helper
= GetPostReadbackGLHelper();
1961 if (!gl_helper
->IsReadbackConfigSupported(color_type
))
1962 color_type
= kRGBA_8888_SkColorType
;
1963 scoped_ptr
<SkBitmap
> bitmap(new SkBitmap
);
1964 if (!bitmap
->tryAllocPixels(SkImageInfo::Make(output_size_in_pixel
.width(),
1965 output_size_in_pixel
.height(),
1967 kOpaque_SkAlphaType
))) {
1968 scoped_callback_runner
.Reset(
1969 base::Bind(callback
, SkBitmap(), READBACK_BITMAP_ALLOCATION_FAILURE
));
1974 scoped_ptr
<SkAutoLockPixels
> bitmap_pixels_lock(
1975 new SkAutoLockPixels(*bitmap
));
1976 uint8
* pixels
= static_cast<uint8
*>(bitmap
->getPixels());
1978 cc::TextureMailbox texture_mailbox
;
1979 scoped_ptr
<cc::SingleReleaseCallback
> release_callback
;
1980 result
->TakeTexture(&texture_mailbox
, &release_callback
);
1981 DCHECK(texture_mailbox
.IsTexture());
1982 if (!texture_mailbox
.IsTexture())
1985 ignore_result(scoped_callback_runner
.Release());
1987 gl_helper
->CropScaleReadbackAndCleanMailbox(
1988 texture_mailbox
.mailbox(),
1989 texture_mailbox
.sync_point(),
1991 gfx::Rect(result
->size()),
1992 output_size_in_pixel
,
1995 base::Bind(&CopyFromCompositingSurfaceFinished
,
1997 base::Passed(&release_callback
),
1998 base::Passed(&bitmap
),
2000 base::Passed(&bitmap_pixels_lock
)),
2001 GLHelper::SCALER_QUALITY_GOOD
);
2004 void RenderWidgetHostViewAndroid::OnStylusSelectBegin(float x0
,
2008 SelectBetweenCoordinates(gfx::PointF(x0
, y0
), gfx::PointF(x1
, y1
));
2011 void RenderWidgetHostViewAndroid::OnStylusSelectUpdate(float x
, float y
) {
2012 MoveRangeSelectionExtent(gfx::PointF(x
, y
));
2015 void RenderWidgetHostViewAndroid::OnStylusSelectEnd() {
2016 if (selection_controller_
)
2017 selection_controller_
->AllowShowingFromCurrentSelection();
2020 void RenderWidgetHostViewAndroid::OnStylusSelectTap(base::TimeTicks time
,
2023 // Treat the stylus tap as a long press, activating either a word selection or
2024 // context menu depending on the targetted content.
2025 blink::WebGestureEvent long_press
= WebGestureEventBuilder::Build(
2026 blink::WebInputEvent::GestureLongPress
,
2027 (time
- base::TimeTicks()).InSecondsF(), x
, y
);
2028 SendGestureEvent(long_press
);
2032 void RenderWidgetHostViewBase::GetDefaultScreenInfo(
2033 blink::WebScreenInfo
* results
) {
2034 const gfx::Display
& display
=
2035 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
2036 results
->rect
= display
.bounds();
2037 // TODO(husky): Remove any system controls from availableRect.
2038 results
->availableRect
= display
.work_area();
2039 results
->deviceScaleFactor
= display
.device_scale_factor();
2040 results
->orientationAngle
= display
.RotationAsDegree();
2041 results
->orientationType
=
2042 RenderWidgetHostViewBase::GetOrientationTypeForMobile(display
);
2043 gfx::DeviceDisplayInfo info
;
2044 results
->depth
= info
.GetBitsPerPixel();
2045 results
->depthPerComponent
= info
.GetBitsPerComponent();
2046 results
->isMonochrome
= (results
->depthPerComponent
== 0);
2049 } // namespace content