diff options
author | mfomitchev <mfomitchev@chromium.org> | 2016-02-17 12:53:39 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-17 20:54:47 +0000 |
commit | 2600fd7ce8de2bbb1ec1e02bbbb0558ff8d4782d (patch) | |
tree | 699e08896f9fb4824c4d88460b8754a11adbf4ee /content/renderer/devtools | |
parent | 49d4195f6958a6e5d503ca6f8fc08b55fdc63250 (diff) | |
download | chromium_src-2600fd7ce8de2bbb1ec1e02bbbb0558ff8d4782d.zip chromium_src-2600fd7ce8de2bbb1ec1e02bbbb0558ff8d4782d.tar.gz chromium_src-2600fd7ce8de2bbb1ec1e02bbbb0558ff8d4782d.tar.bz2 |
Reland of Decouple ScreenMetricsEmulator From RenderWidget
> Original issue's description:
> > Decouple ScreenMetricsEmulator From RenderWidget
> >
> > This is part of the ongoing effort to thin down RenderWidget to be just IPC transport.
> > Started in http://crrev.com/1599583002
> >
> > BUG=577321
TBR=dgozman@chromium.org,fsamuel@chromium.org,jam@chromium.org,pfeldman@chromium.org
BUG=577321
Review URL: https://codereview.chromium.org/1697263008
Cr-Commit-Position: refs/heads/master@{#375977}
Diffstat (limited to 'content/renderer/devtools')
3 files changed, 294 insertions, 0 deletions
diff --git a/content/renderer/devtools/render_widget_screen_metrics_emulator.cc b/content/renderer/devtools/render_widget_screen_metrics_emulator.cc new file mode 100644 index 0000000..5843379 --- /dev/null +++ b/content/renderer/devtools/render_widget_screen_metrics_emulator.cc @@ -0,0 +1,162 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/renderer/devtools/render_widget_screen_metrics_emulator.h" + +#include "content/common/resize_params.h" +#include "content/public/common/context_menu_params.h" +#include "content/renderer/devtools/render_widget_screen_metrics_emulator_delegate.h" + +namespace content { + +RenderWidgetScreenMetricsEmulator::RenderWidgetScreenMetricsEmulator( + RenderWidgetScreenMetricsEmulatorDelegate* delegate, + const blink::WebDeviceEmulationParams& params, + const ResizeParams& resize_params, + const gfx::Rect& view_screen_rect, + const gfx::Rect& window_screen_rect) + : delegate_(delegate), + emulation_params_(params), + scale_(1.f), + original_resize_params_(resize_params), + original_view_screen_rect_(view_screen_rect), + original_window_screen_rect_(window_screen_rect) { + Apply(); +} + +RenderWidgetScreenMetricsEmulator::~RenderWidgetScreenMetricsEmulator() { + delegate_->SetScreenMetricsEmulationParameters(false, emulation_params_); + delegate_->SetScreenRects(original_view_screen_rect_, + original_window_screen_rect_); + delegate_->Resize(original_resize_params_); +} + +void RenderWidgetScreenMetricsEmulator::ChangeEmulationParams( + const blink::WebDeviceEmulationParams& params) { + emulation_params_ = params; + Apply(); +} + +void RenderWidgetScreenMetricsEmulator::Apply() { + ResizeParams modified_resize_params = original_resize_params_; + applied_widget_rect_.set_size(gfx::Size(emulation_params_.viewSize)); + + if (!applied_widget_rect_.width()) + applied_widget_rect_.set_width(original_size().width()); + + if (!applied_widget_rect_.height()) + applied_widget_rect_.set_height(original_size().height()); + + if (emulation_params_.fitToView && !original_size().IsEmpty()) { + int original_width = std::max(original_size().width(), 1); + int original_height = std::max(original_size().height(), 1); + float width_ratio = + static_cast<float>(applied_widget_rect_.width()) / original_width; + float height_ratio = + static_cast<float>(applied_widget_rect_.height()) / original_height; + float ratio = std::max(1.0f, std::max(width_ratio, height_ratio)); + scale_ = 1.f / ratio; + + // Center emulated view inside available view space. + offset_.set_x( + (original_size().width() - scale_ * applied_widget_rect_.width()) / 2); + offset_.set_y( + (original_size().height() - scale_ * applied_widget_rect_.height()) / + 2); + } else { + scale_ = emulation_params_.scale; + offset_.SetPoint(emulation_params_.offset.x, emulation_params_.offset.y); + if (!emulation_params_.viewSize.width && + !emulation_params_.viewSize.height && scale_) { + applied_widget_rect_.set_size( + gfx::ScaleToRoundedSize(original_size(), 1.f / scale_)); + } + } + + gfx::Rect window_screen_rect; + if (emulation_params_.screenPosition == + blink::WebDeviceEmulationParams::Desktop) { + applied_widget_rect_.set_origin(original_view_screen_rect_.origin()); + modified_resize_params.screen_info.rect = original_screen_info().rect; + modified_resize_params.screen_info.availableRect = + original_screen_info().availableRect; + window_screen_rect = original_window_screen_rect_; + } else { + applied_widget_rect_.set_origin(emulation_params_.viewPosition); + gfx::Rect screen_rect = applied_widget_rect_; + if (!emulation_params_.screenSize.isEmpty()) { + screen_rect = gfx::Rect(0, 0, emulation_params_.screenSize.width, + emulation_params_.screenSize.height); + } + modified_resize_params.screen_info.rect = screen_rect; + modified_resize_params.screen_info.availableRect = screen_rect; + window_screen_rect = applied_widget_rect_; + } + + modified_resize_params.screen_info.deviceScaleFactor = + emulation_params_.deviceScaleFactor + ? emulation_params_.deviceScaleFactor + : original_screen_info().deviceScaleFactor; + + // Pass three emulation parameters to the blink side: + // - we keep the real device scale factor in compositor to produce sharp image + // even when emulating different scale factor; + // - in order to fit into view, WebView applies offset and scale to the + // root layer. + blink::WebDeviceEmulationParams modified_emulation_params = emulation_params_; + modified_emulation_params.deviceScaleFactor = + original_screen_info().deviceScaleFactor; + modified_emulation_params.offset = + blink::WebFloatPoint(offset_.x(), offset_.y()); + modified_emulation_params.scale = scale_; + delegate_->SetScreenMetricsEmulationParameters(true, + modified_emulation_params); + + delegate_->SetScreenRects(applied_widget_rect_, window_screen_rect); + + modified_resize_params.physical_backing_size = + gfx::ScaleToCeiledSize(original_resize_params_.new_size, + original_screen_info().deviceScaleFactor); + modified_resize_params.new_size = applied_widget_rect_.size(); + modified_resize_params.visible_viewport_size = applied_widget_rect_.size(); + modified_resize_params.needs_resize_ack = false; + delegate_->Resize(modified_resize_params); +} + +void RenderWidgetScreenMetricsEmulator::OnResize(const ResizeParams& params) { + original_resize_params_ = params; + Apply(); + + if (params.needs_resize_ack) + delegate_->Redraw(); +} + +void RenderWidgetScreenMetricsEmulator::OnUpdateScreenRects( + const gfx::Rect& view_screen_rect, + const gfx::Rect& window_screen_rect) { + original_view_screen_rect_ = view_screen_rect; + original_window_screen_rect_ = window_screen_rect; + if (emulation_params_.screenPosition == + blink::WebDeviceEmulationParams::Desktop) { + Apply(); + } +} + +void RenderWidgetScreenMetricsEmulator::OnShowContextMenu( + ContextMenuParams* params) { + params->x *= scale_; + params->x += offset_.x(); + params->y *= scale_; + params->y += offset_.y(); +} + +gfx::Rect RenderWidgetScreenMetricsEmulator::AdjustValidationMessageAnchor( + const gfx::Rect& anchor) { + gfx::Rect scaled = gfx::ScaleToEnclosedRect(anchor, scale_); + scaled.set_x(scaled.x() + offset_.x()); + scaled.set_y(scaled.y() + offset_.y()); + return scaled; +} + +} // namespace content diff --git a/content/renderer/devtools/render_widget_screen_metrics_emulator.h b/content/renderer/devtools/render_widget_screen_metrics_emulator.h new file mode 100644 index 0000000..8288dfa --- /dev/null +++ b/content/renderer/devtools/render_widget_screen_metrics_emulator.h @@ -0,0 +1,88 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_RENDERER_DEVTOOLS_RENDER_WIDGET_SCREEN_METRICS_EMULATOR_H_ +#define CONTENT_RENDERER_DEVTOOLS_RENDER_WIDGET_SCREEN_METRICS_EMULATOR_H_ + +#include "base/memory/scoped_ptr.h" +#include "content/common/resize_params.h" +#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h" + +namespace gfx { +class PointF; +class Rect; +} + +namespace content { + +class RenderWidgetScreenMetricsEmulatorDelegate; +struct ContextMenuParams; + +// RenderWidgetScreenMetricsEmulator class manages screen emulation inside a +// RenderWidget. This includes resizing, placing view on the screen at desired +// position, changing device scale factor, and scaling down the whole +// widget if required to fit into the browser window. +class RenderWidgetScreenMetricsEmulator { + public: + RenderWidgetScreenMetricsEmulator( + RenderWidgetScreenMetricsEmulatorDelegate* delegate, + const blink::WebDeviceEmulationParams& params, + const ResizeParams& resize_params, + const gfx::Rect& view_screen_rect, + const gfx::Rect& window_screen_rect); + virtual ~RenderWidgetScreenMetricsEmulator(); + + // Scale and offset used to convert between host coordinates + // and webwidget coordinates. + const gfx::Size& original_size() const { + return original_resize_params_.new_size; + } + + float scale() const { return scale_; } + const gfx::PointF& offset() const { return offset_; } + const gfx::Rect& applied_widget_rect() const { return applied_widget_rect_; } + const blink::WebScreenInfo& original_screen_info() const { + return original_resize_params_.screen_info; + } + const gfx::Rect& original_screen_rect() const { + return original_view_screen_rect_; + } + + void ChangeEmulationParams(const blink::WebDeviceEmulationParams& params); + + // The following methods alter handlers' behavior for messages related to + // widget size and position. + void OnResize(const ResizeParams& params); + void OnUpdateScreenRects(const gfx::Rect& view_screen_rect, + const gfx::Rect& window_screen_rect); + void OnShowContextMenu(ContextMenuParams* params); + gfx::Rect AdjustValidationMessageAnchor(const gfx::Rect& anchor); + + private: + // Apply original_resize_params_ + void Apply(); + + RenderWidgetScreenMetricsEmulatorDelegate* const delegate_; + + // Parameters as passed by RenderWidget::EnableScreenMetricsEmulation. + blink::WebDeviceEmulationParams emulation_params_; + + // The computed scale and offset used to fit widget into browser window. + float scale_; + gfx::PointF offset_; + + // Widget rect as passed to webwidget. + gfx::Rect applied_widget_rect_; + + // Original values to restore back after emulation ends. + ResizeParams original_resize_params_; + gfx::Rect original_view_screen_rect_; + gfx::Rect original_window_screen_rect_; + + DISALLOW_COPY_AND_ASSIGN(RenderWidgetScreenMetricsEmulator); +}; + +} // namespace content + +#endif // CONTENT_RENDERER_DEVTOOLS_RENDER_WIDGET_SCREEN_METRICS_EMULATOR_H_ diff --git a/content/renderer/devtools/render_widget_screen_metrics_emulator_delegate.h b/content/renderer/devtools/render_widget_screen_metrics_emulator_delegate.h new file mode 100644 index 0000000..af79f19 --- /dev/null +++ b/content/renderer/devtools/render_widget_screen_metrics_emulator_delegate.h @@ -0,0 +1,44 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_RENDERER_DEVTOOLS_RENDER_WIDGET_SCREEN_METRICS_EMULATOR_DELEGATE_H_ +#define CONTENT_RENDERER_DEVTOOLS_RENDER_WIDGET_SCREEN_METRICS_EMULATOR_DELEGATE_H_ + +#include "content/common/content_export.h" + +namespace blink { +struct WebDeviceEmulationParams; +} + +namespace content { + +struct ResizeParams; + +// Consumers of RenderWidgetScreenMetricsEmulatorDelegate implement this +// delegate in order to transport emulation information across processes. +class CONTENT_EXPORT RenderWidgetScreenMetricsEmulatorDelegate { + public: + // Requests a full redraw of the contents of the renderer. + virtual void Redraw() = 0; + + // Resizes the Widget with the provided |resize_params|. + virtual void Resize(const ResizeParams& resize_params) = 0; + + // Passes device emulation parameters to the delegate. + virtual void SetScreenMetricsEmulationParameters( + bool enabled, + const blink::WebDeviceEmulationParams& params) = 0; + + // Passes new view bounds and window bounds in screen coordinates to the + // delegate. + virtual void SetScreenRects(const gfx::Rect& view_screen_rect, + const gfx::Rect& window_screen_rect) = 0; + + protected: + virtual ~RenderWidgetScreenMetricsEmulatorDelegate() {} +}; + +} // namespace content + +#endif // CONTENT_RENDERER_DEVTOOLS_RENDER_WIDGET_SCREEN_METRICS_EMULATOR_DELEGATE_H_ |