Index: ui/compositor/paint_context.cc |
diff --git a/ui/compositor/paint_context.cc b/ui/compositor/paint_context.cc |
index b2b4fac54b39a4c19adb6b398cd1d3674d3a5e58..7b3a85c8fc0a313448b69910be332cc656084717 100644 |
--- a/ui/compositor/paint_context.cc |
+++ b/ui/compositor/paint_context.cc |
@@ -7,13 +7,40 @@ |
#include "ui/gfx/canvas.h" |
namespace ui { |
+namespace { |
+ |
+// Scales the given |rect| by scaling its corner points. |
+gfx::Rect ScaleToCornerScaledRect(gfx::Rect rect, |
+ float x_scale, |
+ float y_scale = -1) { |
+ y_scale = y_scale < 0 ? x_scale : y_scale; |
+ const gfx::Vector2d& origin = rect.OffsetFromOrigin(); |
+ |
+ int right = origin.x() + rect.width(); |
+ int bottom = origin.y() + rect.height(); |
+ |
+ int new_x = std::round(origin.x() * x_scale); |
+ int new_y = std::round(origin.y() * y_scale); |
+ |
+ return gfx::Rect(new_x, new_y, std::round(right * x_scale) - new_x, |
+ std::round(bottom * y_scale) - new_y); |
+} |
+ |
+} // namespace |
PaintContext::PaintContext(cc::DisplayItemList* list, |
float device_scale_factor, |
- const gfx::Rect& invalidation) |
+ const gfx::Rect& invalidation, |
+ const gfx::Size& size) |
: list_(list), |
device_scale_factor_(device_scale_factor), |
- invalidation_(invalidation) { |
+ invalidation_(gfx::ScaleToEnclosingRect( |
+ invalidation, |
+ IsPixelCanvas() ? device_scale_factor : 1.f)), |
+ bounds_(size), |
+ pixel_bounds_(ScaleToCornerScaledRect( |
+ bounds_, |
+ IsPixelCanvas() ? device_scale_factor : 1.f)) { |
#if DCHECK_IS_ON() |
root_visited_ = nullptr; |
inside_paint_recorder_ = false; |
@@ -21,11 +48,15 @@ PaintContext::PaintContext(cc::DisplayItemList* list, |
} |
PaintContext::PaintContext(const PaintContext& other, |
- const gfx::Vector2d& offset) |
+ const gfx::Rect& bounds, |
+ int scale_type) |
: list_(other.list_), |
device_scale_factor_(other.device_scale_factor_), |
invalidation_(other.invalidation_), |
- offset_(other.offset_ + offset) { |
+ bounds_(bounds), |
+ pixel_bounds_(other.GetSnappedPixelBounds(bounds_)), |
+ offset_(other.offset_ + pixel_bounds_.OffsetFromOrigin()), |
+ scale_type_(scale_type) { |
oshima
2017/06/17 00:55:47
I wasn't clear, sorry. Scale type shouldn't be a p
|
#if DCHECK_IS_ON() |
root_visited_ = other.root_visited_; |
inside_paint_recorder_ = other.inside_paint_recorder_; |
@@ -37,6 +68,8 @@ PaintContext::PaintContext(const PaintContext& other, |
: list_(other.list_), |
device_scale_factor_(other.device_scale_factor_), |
invalidation_(), |
+ bounds_(other.bounds_), |
+ pixel_bounds_(other.pixel_bounds_), |
offset_(other.offset_) { |
#if DCHECK_IS_ON() |
root_visited_ = other.root_visited_; |
@@ -52,8 +85,58 @@ gfx::Rect PaintContext::ToLayerSpaceBounds( |
return gfx::Rect(size_in_context) + offset_; |
} |
-gfx::Rect PaintContext::ToLayerSpaceRect(const gfx::Rect& rect) const { |
- return rect + offset_; |
+float PaintContext::effective_scale_factor_x() const { |
+ if (scale_type_ == SCALE_TO_SCALE_FACTOR || pixel_bounds_.width() == 0) |
+ return IsPixelCanvas() ? device_scale_factor_ : 1.f; |
+ return static_cast<float>(pixel_bounds_.width()) / |
+ static_cast<float>(bounds_.width()); |
+} |
+ |
+float PaintContext::effective_scale_factor_y() const { |
+ if (scale_type_ == SCALE_TO_SCALE_FACTOR || pixel_bounds_.height() == 0) |
+ return IsPixelCanvas() ? device_scale_factor_ : 1.f; |
+ return static_cast<float>(pixel_bounds_.height()) / |
+ static_cast<float>(bounds_.height()); |
+} |
+ |
+gfx::Size PaintContext::ScaleToCorneredPixelSize(const gfx::Size& size) const { |
+ // effective_scale_factor_x/y() would return 1.f value if pixel canvas is |
+ // disabled. This early return is only an optimization. |
+ if (!IsPixelCanvas()) |
+ return size; |
+ return ScaleToCornerScaledRect(gfx::Rect(size), effective_scale_factor_x(), |
+ effective_scale_factor_y()) |
+ .size(); |
+} |
+ |
+gfx::Rect PaintContext::GetSnappedPixelBounds( |
+ const gfx::Rect& child_bounds) const { |
+ if (!IsPixelCanvas()) |
+ return child_bounds; |
+ |
+ const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin(); |
+ |
+ int right = child_origin.x() + child_bounds.width(); |
+ int bottom = child_origin.y() + child_bounds.height(); |
+ |
+ int new_x = std::round(child_origin.x() * device_scale_factor_); |
+ int new_y = std::round(child_origin.y() * device_scale_factor_); |
+ |
+ int new_right; |
+ int new_bottom; |
+ |
+ if (right == bounds_.width()) { |
+ new_right = pixel_bounds_.width(); |
+ } else { |
+ new_right = std::round(right * device_scale_factor_); |
+ } |
+ |
+ if (bottom == bounds_.height()) { |
+ new_bottom = pixel_bounds_.height(); |
+ } else { |
+ new_bottom = std::round(bottom * device_scale_factor_); |
+ } |
+ return gfx::Rect(new_x, new_y, new_right - new_x, new_bottom - new_y); |
} |
} // namespace ui |