Skip to content

Commit

Permalink
Move content offset to ViewAndroid
Browse files Browse the repository at this point in the history
Content offset is specific to a given web contents/render widget.
This CL moves the offset info from WindwoAndroid to ViewAndroid,
and it is used to get the correct bounds for snapshot API.

BUG=617750

Review-Url: https://codereview.chromium.org/2219823002
Cr-Commit-Position: refs/heads/master@{#442442}
  • Loading branch information
JinsukKim authored and Commit bot committed Jan 10, 2017
1 parent 0d32da5 commit 8f74b8e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
3 changes: 0 additions & 3 deletions content/browser/android/content_view_core_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,6 @@ void ContentViewCoreImpl::UpdateFrameInfo(
if (obj.is_null() || !GetWindowAndroid())
return;

GetWindowAndroid()->set_content_offset(
gfx::Vector2dF(0.0f, top_controls_height * top_controls_shown_ratio));

page_scale_ = page_scale_factor;

// The CursorAnchorInfo API in Android only supports zero width selection
Expand Down
14 changes: 12 additions & 2 deletions content/browser/renderer_host/render_widget_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
#include "ui/gfx/skbitmap_operations.h"
#include "ui/snapshot/snapshot.h"

#if defined(OS_ANDROID)
#include "ui/android/view_android.h"
#endif

#if defined(OS_MACOSX)
#include "device/power_save_blocker/power_save_blocker.h"
#include "ui/accelerated_widget_mac/window_resize_helper_mac.h"
Expand Down Expand Up @@ -2353,8 +2357,14 @@ void RenderWidgetHostImpl::DidReceiveRendererFrame() {
void RenderWidgetHostImpl::WindowSnapshotReachedScreen(int snapshot_id) {
DCHECK(base::MessageLoopForUI::IsCurrent());

gfx::Rect view_bounds = GetView()->GetViewBounds();
gfx::Rect snapshot_bounds(view_bounds.size());
#if defined(OS_ANDROID)
// On Android, call sites should pass in the bounds with correct offset
// to capture the intended content area.
gfx::Rect snapshot_bounds(GetView()->GetViewBounds());
snapshot_bounds.Offset(0, GetView()->GetNativeView()->content_offset().y());
#else
gfx::Rect snapshot_bounds(GetView()->GetViewBounds().size());
#endif

std::vector<unsigned char> png;
if (ui::GrabViewSnapshot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,10 @@ void RenderWidgetHostViewAndroid::OnFrameMetadataUpdated(
UpdateBackgroundColor(is_transparent ? SK_ColorTRANSPARENT
: frame_metadata.root_background_color);

view_.set_content_offset(gfx::Vector2dF(0.0f,
frame_metadata.top_controls_height *
frame_metadata.top_controls_shown_ratio));

// All offsets and sizes are in CSS pixels.
content_view_core_->UpdateFrameInfo(
frame_metadata.root_scroll_offset,
Expand Down
5 changes: 1 addition & 4 deletions ui/android/view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor,
->GetDisplayNearestWindow(this)
.device_scale_factor();
int left_margin = std::round(bounds.x() * scale);
// TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's
// specific to a given web contents/render widget.
int top_margin = std::round(
(GetWindowAndroid()->content_offset().y() + bounds.y()) * scale);
int top_margin = std::round((content_offset().y() + bounds.y()) * scale);
JNIEnv* env = base::android::AttachCurrentThread();
Java_ViewAndroidDelegate_setViewPosition(
env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(),
Expand Down
11 changes: 11 additions & 0 deletions ui/android/view_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ class UI_ANDROID_EXPORT ViewAndroid {
ViewAndroid();
virtual ~ViewAndroid();

// The content offset is in CSS pixels, and is used to translate
// snapshots to the correct part of the view.
void set_content_offset(const gfx::Vector2dF& content_offset) {
content_offset_ = content_offset;
}

gfx::Vector2dF content_offset() const {
return content_offset_;
}

// Returns the window at the root of this hierarchy, or |null|
// if disconnected.
virtual WindowAndroid* GetWindowAndroid() const;
Expand Down Expand Up @@ -126,6 +136,7 @@ class UI_ANDROID_EXPORT ViewAndroid {

int physical_width_pix_;
int physical_height_pix_;
gfx::Vector2dF content_offset_; // in CSS pixel

DISALLOW_COPY_AND_ASSIGN(ViewAndroid);
};
Expand Down
11 changes: 0 additions & 11 deletions ui/android/window_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ class UI_ANDROID_EXPORT WindowAndroid : public ViewAndroid {

static bool RegisterWindowAndroid(JNIEnv* env);

// The content offset in CSS pixels. It is used together with device scale
// factor to translate snapshots to the correct part of the window.
void set_content_offset(const gfx::Vector2dF& content_offset) {
content_offset_ = content_offset;
}

gfx::Vector2dF content_offset() const {
return content_offset_;
}

// Compositor callback relay.
void OnCompositingDidCommit();

Expand Down Expand Up @@ -113,7 +103,6 @@ class UI_ANDROID_EXPORT WindowAndroid : public ViewAndroid {

base::android::ScopedJavaGlobalRef<jobject> java_window_;
const int display_id_;
gfx::Vector2dF content_offset_;
WindowAndroidCompositor* compositor_;

base::ObserverList<WindowAndroidObserver> observer_list_;
Expand Down
26 changes: 9 additions & 17 deletions ui/snapshot/snapshot_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace ui {

// Sync versions are not supported in Android. Callers should fall back
// to the async version.
bool GrabViewSnapshot(gfx::NativeView view,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds) {
Expand All @@ -31,7 +33,6 @@ bool GrabViewSnapshot(gfx::NativeView view,
bool GrabWindowSnapshot(gfx::NativeWindow window,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds) {
// Not supported in Android. Callers should fall back to the async version.
return false;
}

Expand All @@ -44,20 +45,8 @@ static void MakeAsyncCopyRequest(

const display::Display& display =
display::Screen::GetScreen()->GetDisplayNearestWindow(window);
float device_scale_factor = display.device_scale_factor();
gfx::Rect source_rect_in_pixel =
gfx::ScaleToEnclosingRect(source_rect, device_scale_factor);

// Account for the toolbar offset.
gfx::Vector2dF offset_in_pixel =
gfx::ScaleVector2d(window->content_offset(), device_scale_factor);
gfx::Rect adjusted_source_rect(
gfx::ToRoundedPoint(
gfx::PointF(source_rect_in_pixel.x() + offset_in_pixel.x(),
source_rect_in_pixel.y() + offset_in_pixel.y())),
source_rect_in_pixel.size());

request->set_area(adjusted_source_rect);
float scale = display.device_scale_factor();
request->set_area(gfx::ScaleToEnclosingRect(source_rect, scale));
window->GetCompositor()->RequestCopyOfOutputOnRootLayer(std::move(request));
}

Expand Down Expand Up @@ -92,8 +81,11 @@ void GrabViewSnapshotAsync(
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback) {
GrabWindowSnapshotAsync(
view->GetWindowAndroid(), source_rect, background_task_runner, callback);
MakeAsyncCopyRequest(view->GetWindowAndroid(),
source_rect,
base::Bind(&SnapshotAsync::EncodeCopyOutputResult,
callback,
background_task_runner));
}

} // namespace ui

0 comments on commit 8f74b8e

Please sign in to comment.