Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Unified Diff: ui/gfx/geometry/rect.h

Issue 2384063007: Fix scrollbar overflow with ScaleToEnclosingRectSafe (Closed)
Patch Set: With comment Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/layers/painted_scrollbar_layer.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/rect.h
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h
index 17266fe03fb7c3f385df26746ff660e466087820..b749ac2c300833d8714eb24846f3a051884d187c 100644
--- a/ui/gfx/geometry/rect.h
+++ b/ui/gfx/geometry/rect.h
@@ -254,6 +254,9 @@ GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
// contained within the rect, because they will appear on one of these edges.
GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
+// Scales the rect and returns the enclosing rect. Use this only the inputs are
+// known to not overflow. Use ScaleToEnclosingRectSafe if the inputs are
+// unknown and need to use saturated math.
inline Rect ScaleToEnclosingRect(const Rect& rect,
float x_scale,
float y_scale) {
@@ -284,6 +287,24 @@ inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
return ScaleToEnclosingRect(rect, scale, scale);
}
+// ScaleToEnclosingRect but clamping instead of asserting if the resulting rect
+// would overflow.
+inline Rect ScaleToEnclosingRectSafe(const Rect& rect,
+ float x_scale,
+ float y_scale) {
+ if (x_scale == 1.f && y_scale == 1.f)
+ return rect;
+ int x = base::saturated_cast<int>(std::floor(rect.x() * x_scale));
+ int y = base::saturated_cast<int>(std::floor(rect.y() * y_scale));
+ int w = base::saturated_cast<int>(std::ceil(rect.width() * x_scale));
+ int h = base::saturated_cast<int>(std::ceil(rect.height() * y_scale));
+ return Rect(x, y, w, h);
+}
+
+inline Rect ScaleToEnclosingRectSafe(const Rect& rect, float scale) {
+ return ScaleToEnclosingRectSafe(rect, scale, scale);
+}
+
inline Rect ScaleToEnclosedRect(const Rect& rect,
float x_scale,
float y_scale) {
« no previous file with comments | « cc/layers/painted_scrollbar_layer.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698