Commit 73dc6655 authored by Hui Yingst's avatar Hui Yingst Committed by Chromium LUCI CQ

Migrate CalculateBackgroundParts() to PdfViewPluginBase.

CalculateBackgroundParts() needs to be migrated to PdfViewPluginBase
since it can be used identically by both PdfViewWebPlugin and
OutOfProcessInstance.

This CL migrates `background_parts_` and `available_area_` to
PdfViewPluginBase and provides accessors and mutators for them, so that
CalculateBackgroundParts() can be migrated as well.

Bug: 1140629
Change-Id: I767468a3716bae40c96c8c36b49d6dd2bb16008d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2625787
Commit-Queue: Hui Yingst <nigi@chromium.org>
Reviewed-by: default avatarK. Moon <kmoon@chromium.org>
Reviewed-by: default avatarDaniel Hosseinian <dhoss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843804}
parent 3f41f887
......@@ -761,7 +761,7 @@ bool OutOfProcessInstance::HandleInputEvent(const pp::InputEvent& event) {
pp::Point point = mouse_event.GetPosition();
pp::Point movement = mouse_event.GetMovement();
ScalePoint(device_scale(), &point);
point.set_x(point.x() - available_area_.x());
point.set_x(point.x() - available_area().x());
ScalePoint(device_scale(), &movement);
mouse_event =
......@@ -786,7 +786,7 @@ bool OutOfProcessInstance::HandleInputEvent(const pp::InputEvent& event) {
pp::FloatPoint point = touch_point.position();
ScaleFloatPoint(device_scale(), &point);
point.set_x(point.x() - available_area_.x());
point.set_x(point.x() - available_area().x());
new_touch_event.AddTouchPoint(
PP_TOUCHLIST_TYPE_TARGETTOUCHES,
......@@ -970,8 +970,8 @@ void OutOfProcessInstance::SendAccessibilityViewportInfo() {
viewport_info.scroll.x = -plugin_offset().x();
viewport_info.scroll.y =
-top_toolbar_height_in_viewport_coords() - plugin_offset().y();
viewport_info.offset.x = available_area_.x() / (device_scale() * zoom());
viewport_info.offset.y = available_area_.y() / (device_scale() * zoom());
viewport_info.offset.x = available_area().x() / (device_scale() * zoom());
viewport_info.offset.y = available_area().y() / (device_scale() * zoom());
viewport_info.zoom = zoom();
viewport_info.scale = device_scale();
......@@ -988,8 +988,8 @@ void OutOfProcessInstance::SendAccessibilityViewportInfo() {
void OutOfProcessInstance::SelectionChanged(const gfx::Rect& left,
const gfx::Rect& right) {
pp::Point l(left.x() + available_area_.x(), left.y());
pp::Point r(right.x() + available_area_.x(), right.y());
pp::Point l(left.x() + available_area().x(), left.y());
pp::Point r(right.x() + available_area().x(), right.y());
float inverse_scale = 1.0f / device_scale();
ScalePoint(inverse_scale, &l);
......@@ -1005,7 +1005,7 @@ void OutOfProcessInstance::SelectionChanged(const gfx::Rect& left,
void OutOfProcessInstance::SetCaretPosition(const pp::FloatPoint& position) {
pp::Point new_position(position.x(), position.y());
ScalePoint(device_scale(), &new_position);
new_position.set_x(new_position.x() - available_area_.x());
new_position.set_x(new_position.x() - available_area().x());
engine()->SetCaretPosition(PointFromPPPoint(new_position));
}
......@@ -1013,7 +1013,7 @@ void OutOfProcessInstance::MoveRangeSelectionExtent(
const pp::FloatPoint& extent) {
pp::Point new_extent(extent.x(), extent.y());
ScalePoint(device_scale(), &new_extent);
new_extent.set_x(new_extent.x() - available_area_.x());
new_extent.set_x(new_extent.x() - available_area().x());
engine()->MoveRangeSelectionExtent(PointFromPPPoint(new_extent));
}
......@@ -1021,11 +1021,11 @@ void OutOfProcessInstance::SetSelectionBounds(const pp::FloatPoint& base,
const pp::FloatPoint& extent) {
pp::Point new_base_point(base.x(), base.y());
ScalePoint(device_scale(), &new_base_point);
new_base_point.set_x(new_base_point.x() - available_area_.x());
new_base_point.set_x(new_base_point.x() - available_area().x());
pp::Point new_extent_point(extent.x(), extent.y());
ScalePoint(device_scale(), &new_extent_point);
new_extent_point.set_x(new_extent_point.x() - available_area_.x());
new_extent_point.set_x(new_extent_point.x() - available_area().x());
engine()->SetSelectionBounds(PointFromPPPoint(new_base_point),
PointFromPPPoint(new_extent_point));
......@@ -1034,7 +1034,7 @@ void OutOfProcessInstance::SetSelectionBounds(const pp::FloatPoint& base,
pp::Var OutOfProcessInstance::GetLinkAtPosition(const pp::Point& point) {
pp::Point offset_point(point);
ScalePoint(device_scale(), &offset_point);
offset_point.set_x(offset_point.x() - available_area_.x());
offset_point.set_x(offset_point.x() - available_area().x());
return engine()->GetLinkAtPosition(PointFromPPPoint(offset_point));
}
......@@ -1191,19 +1191,19 @@ void OutOfProcessInstance::DoPaint(const std::vector<gfx::Rect>& paint_rects,
if (rect.IsEmpty())
continue;
gfx::Rect pdf_rect = gfx::IntersectRects(rect, available_area_);
gfx::Rect pdf_rect = gfx::IntersectRects(rect, available_area());
if (!pdf_rect.IsEmpty()) {
pdf_rect.Offset(available_area_.x() * -1, 0);
pdf_rect.Offset(available_area().x() * -1, 0);
std::vector<gfx::Rect> pdf_ready;
std::vector<gfx::Rect> pdf_pending;
engine()->Paint(pdf_rect, skia_image_data_, pdf_ready, pdf_pending);
for (auto& ready_rect : pdf_ready) {
ready_rect.Offset(available_area_.OffsetFromOrigin());
ready_rect.Offset(available_area().OffsetFromOrigin());
ready->push_back(PaintReadyRect(ready_rect, image_data_));
}
for (auto& pending_rect : pdf_pending) {
pending_rect.Offset(available_area_.OffsetFromOrigin());
pending_rect.Offset(available_area().OffsetFromOrigin());
pending->push_back(pending_rect);
}
}
......@@ -1219,7 +1219,7 @@ void OutOfProcessInstance::DoPaint(const std::vector<gfx::Rect>& paint_rects,
FillRect(region, GetBackgroundColor());
}
for (const auto& background_part : background_parts_) {
for (const auto& background_part : background_parts()) {
gfx::Rect intersection =
gfx::IntersectRects(background_part.location, rect);
if (!intersection.IsEmpty()) {
......@@ -1240,27 +1240,6 @@ void OutOfProcessInstance::DoPaint(const std::vector<gfx::Rect>& paint_rects,
}
}
void OutOfProcessInstance::CalculateBackgroundParts() {
background_parts_.clear();
int left_width = available_area_.x();
int right_start = available_area_.right();
int right_width = abs(plugin_size().width() - available_area_.right());
int bottom = std::min(available_area_.bottom(), plugin_size().height());
// Add the left, right, and bottom rectangles. Note: we assume only
// horizontal centering.
BackgroundPart part = {gfx::Rect(left_width, bottom), GetBackgroundColor()};
if (!part.location.IsEmpty())
background_parts_.push_back(part);
part.location = gfx::Rect(right_start, 0, right_width, bottom);
if (!part.location.IsEmpty())
background_parts_.push_back(part);
part.location = gfx::Rect(0, bottom, plugin_size().width(),
plugin_size().height() - bottom);
if (!part.location.IsEmpty())
background_parts_.push_back(part);
}
pp::VarArray OutOfProcessInstance::GetDocumentAttachments() {
const std::vector<DocumentAttachmentInfo>& list =
engine()->GetDocumentAttachmentInfoList();
......@@ -1328,13 +1307,13 @@ void OutOfProcessInstance::Invalidate(const gfx::Rect& rect) {
return;
}
gfx::Rect offset_rect = rect + available_area_.OffsetFromOrigin();
gfx::Rect offset_rect = rect + available_area().OffsetFromOrigin();
paint_manager().InvalidateRect(offset_rect);
}
void OutOfProcessInstance::DidScroll(const gfx::Vector2d& offset) {
if (!image_data_.is_null())
paint_manager().ScrollRect(available_area_, offset);
paint_manager().ScrollRect(available_area(), offset);
}
void OutOfProcessInstance::ScrollToX(int x_in_screen_coords) {
......@@ -2235,21 +2214,22 @@ void OutOfProcessInstance::OnGeometryChanged(double old_zoom,
if (zoom() != old_zoom || device_scale() != old_device_scale)
engine()->ZoomUpdated(zoom() * device_scale());
available_area_ = gfx::Rect(plugin_size());
mutable_available_area() = gfx::Rect(plugin_size());
int doc_width = GetDocumentPixelWidth();
if (doc_width < available_area_.width()) {
available_area_.Offset((available_area_.width() - doc_width) / 2, 0);
available_area_.set_width(doc_width);
if (doc_width < available_area().width()) {
mutable_available_area().Offset((available_area().width() - doc_width) / 2,
0);
mutable_available_area().set_width(doc_width);
}
int bottom_of_document =
GetDocumentPixelHeight() +
(top_toolbar_height_in_viewport_coords() * device_scale());
if (bottom_of_document < available_area_.height())
available_area_.set_height(bottom_of_document);
if (bottom_of_document < available_area().height())
mutable_available_area().set_height(bottom_of_document);
CalculateBackgroundParts();
engine()->PageOffsetUpdated(available_area_.OffsetFromOrigin());
engine()->PluginSizeUpdated(available_area_.size());
engine()->PageOffsetUpdated(available_area().OffsetFromOrigin());
engine()->PluginSizeUpdated(available_area().size());
if (document_size().IsEmpty())
return;
......
......@@ -211,10 +211,6 @@ class OutOfProcessInstance : public PdfViewPluginBase,
void ResetRecentlySentFindUpdate(int32_t);
// Figures out the location of any background rectangles (i.e. those that
// aren't painted by the PDF engine).
void CalculateBackgroundParts();
// Returns a VarArray of Attachments. Each Attachment is a VarDictionary
// which contains the following key/values:
// - "name" - a string Var.
......@@ -347,9 +343,6 @@ class OutOfProcessInstance : public PdfViewPluginBase,
// The current cursor.
PP_CursorType_Dev cursor_ = PP_CURSORTYPE_POINTER;
// Remaining area, in pixels, to render the pdf in after accounting for
// horizontal centering.
gfx::Rect available_area_;
// The scroll offset in CSS pixels.
pp::Point scroll_offset_;
......@@ -377,12 +370,6 @@ class OutOfProcessInstance : public PdfViewPluginBase,
// Deferred invalidates while |in_paint_| is true.
std::vector<gfx::Rect> deferred_invalidates_;
struct BackgroundPart {
gfx::Rect location;
uint32_t color;
};
std::vector<BackgroundPart> background_parts_;
struct PrintSettings {
PrintSettings() { Clear(); }
......
......@@ -4,6 +4,7 @@
#include "pdf/pdf_view_plugin_base.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <string>
......@@ -15,6 +16,7 @@
#include "base/memory/weak_ptr.h"
#include "pdf/pdfium/pdfium_engine.h"
#include "pdf/ppapi_migration/url_loader.h"
#include "ui/gfx/geometry/rect.h"
namespace chrome_pdf {
......@@ -60,6 +62,32 @@ void PdfViewPluginBase::LoadUrl(const std::string& url, bool is_print_preview) {
GetWeakPtr(), std::move(loader)));
}
void PdfViewPluginBase::CalculateBackgroundParts() {
background_parts_.clear();
int left_width = available_area_.x();
int right_start = available_area_.right();
int right_width = std::abs(plugin_size().width() - available_area_.right());
int bottom = std::min(available_area_.bottom(), plugin_size().height());
// Note: we assume the display of the PDF document is always centered
// horizontally, but not necessarily centered vertically.
// Add the left rectangle.
BackgroundPart part = {gfx::Rect(left_width, bottom), GetBackgroundColor()};
if (!part.location.IsEmpty())
background_parts_.push_back(part);
// Add the right rectangle.
part.location = gfx::Rect(right_start, 0, right_width, bottom);
if (!part.location.IsEmpty())
background_parts_.push_back(part);
// Add the bottom rectangle.
part.location = gfx::Rect(0, bottom, plugin_size().width(),
plugin_size().height() - bottom);
if (!part.location.IsEmpty())
background_parts_.push_back(part);
}
int PdfViewPluginBase::GetDocumentPixelWidth() const {
return static_cast<int>(
std::ceil(document_size_.width() * zoom() * device_scale()));
......
......@@ -15,6 +15,7 @@
#include "base/memory/weak_ptr.h"
#include "pdf/paint_manager.h"
#include "pdf/pdfium/pdfium_form_filler.h"
#include "ui/gfx/geometry/rect.h"
namespace chrome_pdf {
......@@ -38,7 +39,12 @@ class PdfViewPluginBase : public PDFEngine::Client,
std::vector<gfx::Rect>* pending) override;
protected:
// The mininum zoom level allowed.
struct BackgroundPart {
gfx::Rect location;
uint32_t color;
};
// The minimum zoom level allowed.
static constexpr double kMinZoom = 0.01;
PdfViewPluginBase();
......@@ -83,11 +89,26 @@ class PdfViewPluginBase : public PDFEngine::Client,
// background parts, and notifies the pdf engine.
virtual void OnGeometryChanged(double old_zoom, float old_device_scale) = 0;
// Figures out the location of any background rectangles (i.e. those that
// aren't painted by the PDF engine).
void CalculateBackgroundParts();
// Computes document width/height in device pixels, based on current zoom and
// device scale
int GetDocumentPixelWidth() const;
int GetDocumentPixelHeight() const;
const std::vector<BackgroundPart>& background_parts() const {
return background_parts_;
}
const gfx::Rect& available_area() const { return available_area_; }
// TODO(https://crbug.com/1140629): Remove mutable_available_area()
// once all uses of it in OnGeometryChanged() are migrated to
// PdfViewPluginBase.
gfx::Rect& mutable_available_area() { return available_area_; }
const gfx::Size& document_size() const { return document_size_; }
void set_document_size(const gfx::Size& size) { document_size_ = size; }
......@@ -129,6 +150,12 @@ class PdfViewPluginBase : public PDFEngine::Client,
std::unique_ptr<PDFiumEngine> engine_;
PaintManager paint_manager_{this};
std::vector<BackgroundPart> background_parts_;
// Remaining area, in pixels, to render the pdf in after accounting for
// horizontal centering.
gfx::Rect available_area_;
// The size of the entire document in pixels (i.e. if each page is 800 pixels
// high and there are 10 pages, the height will be 8000).
gfx::Size document_size_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment