Chromium Code Reviews| Index: ui/compositor/paint_context.cc |
| diff --git a/ui/compositor/paint_context.cc b/ui/compositor/paint_context.cc |
| index b2b4fac54b39a4c19adb6b398cd1d3674d3a5e58..8da8991a1d46b287026ef018c4bc5ab05a703a9c 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_, scale_type)), |
| + offset_(other.offset_ + pixel_bounds_.OffsetFromOrigin()), |
| + scale_type_(scale_type) { |
| #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,70 @@ 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 (float)pixel_bounds_.width() / (float)bounds_.width(); |
|
oshima
2017/06/15 22:59:12
static_cast<float>
same for the other places.
malaykeshav
2017/06/16 20:41:57
Done
|
| +} |
| + |
| +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 (float)pixel_bounds_.height() / (float)bounds_.height(); |
| +} |
| + |
| +gfx::Size PaintContext::ScaleToEffectivePixelSize(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::ScaleToEffectivePixelBounds( |
| + const gfx::Rect& bounds) 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 bounds; |
| + return ScaleToCornerScaledRect(bounds, effective_scale_factor_x(), |
| + effective_scale_factor_y()); |
| +} |
| + |
| +gfx::Rect PaintContext::GetSnappedPixelBounds(const gfx::Rect& child_bounds, |
| + int scale_type) const { |
| + if (!IsPixelCanvas()) |
| + return child_bounds; |
| + |
| + if (scale_type == SCALE_TO_SCALE_FACTOR) { |
| + return ScaleToCornerScaledRect(child_bounds, device_scale_factor_); |
| + } |
| + |
| + 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 |