diff --git a/gfx/2d/BaseRect.h b/gfx/2d/BaseRect.h index 593e7468ce2..c0719112a61 100644 --- a/gfx/2d/BaseRect.h +++ b/gfx/2d/BaseRect.h @@ -226,20 +226,6 @@ struct BaseRect { } void Inflate(const SizeT& aSize) { Inflate(aSize.width, aSize.height); } - void InflateToMultiple(const SizeT& aMultiple) - { - T xMost = XMost(); - T yMost = YMost(); - - x = static_cast(floor(x / aMultiple.width)) * aMultiple.width; - y = static_cast(floor(y / aMultiple.height)) * aMultiple.height; - xMost = static_cast(ceil(x / aMultiple.width)) * aMultiple.width; - yMost = static_cast(ceil(y / aMultiple.height)) * aMultiple.height; - - width = xMost - x; - height = yMost - y; - } - void Deflate(T aD) { Deflate(aD, aD); } void Deflate(T aDx, T aDy) { diff --git a/gfx/2d/NumericTools.h b/gfx/2d/NumericTools.h new file mode 100644 index 00000000000..a1004e17bc3 --- /dev/null +++ b/gfx/2d/NumericTools.h @@ -0,0 +1,37 @@ +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef MOZILLA_GFX_NUMERICTOOLS_H_ +#define MOZILLA_GFX_NUMERICTOOLS_H_ + +// Returns the largest multiple of aMultiplied that's <= x. +// Same as int32_t(floor(double(x) / aMultiplier)) * aMultiplier, +// but faster. +static int32_t +RoundDownToMultiple(int32_t x, int32_t aMultiplier) +{ + // We don't use float division + floor because that's hard for the compiler + // to optimize. + int mod = x % aMultiplier; + if (x > 0) { + return x - mod; + } + return mod ? x - aMultiplier - mod : x; +} + +// Returns the smallest multiple of aMultiplied that's >= x. +// Same as int32_t(ceil(double(x) / aMultiplier)) * aMultiplier, +// but faster. +static int32_t +RoundUpToMultiple(int32_t x, int32_t aMultiplier) +{ + int mod = x % aMultiplier; + if (x > 0) { + return mod ? x + aMultiplier - mod : x; + } + return x - mod; +} + +#endif /* MOZILLA_GFX_NUMERICTOOLS_H_ */ diff --git a/gfx/2d/moz.build b/gfx/2d/moz.build index 48d90b25f3d..54fcafad87e 100644 --- a/gfx/2d/moz.build +++ b/gfx/2d/moz.build @@ -26,6 +26,7 @@ EXPORTS.mozilla.gfx += [ 'Helpers.h', 'Logging.h', 'Matrix.h', + 'NumericTools.h', 'PathHelpers.h', 'PatternHelpers.h', 'Point.h', diff --git a/gfx/src/nsRect.h b/gfx/src/nsRect.h index e02750ae1f9..e424cefd1da 100644 --- a/gfx/src/nsRect.h +++ b/gfx/src/nsRect.h @@ -14,6 +14,7 @@ #include "gfxCore.h" // for NS_GFX #include "mozilla/Likely.h" // for MOZ_UNLIKELY #include "mozilla/gfx/BaseRect.h" // for BaseRect +#include "mozilla/gfx/NumericTools.h" // for RoundUpToMultiple, RoundDownToMultiple #include "nsCoord.h" // for nscoord, etc #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc #include "nsPoint.h" // for nsIntPoint, nsPoint @@ -207,6 +208,20 @@ struct NS_GFX nsIntRect : return r; } + void InflateToMultiple(const nsIntSize& aTileSize) + { + int32_t xMost = XMost(); + int32_t yMost = YMost(); + + x = RoundDownToMultiple(x, aTileSize.width); + y = RoundDownToMultiple(y, aTileSize.height); + xMost = RoundUpToMultiple(xMost, aTileSize.width); + yMost = RoundUpToMultiple(yMost, aTileSize.height); + + width = xMost - x; + height = yMost - y; + } + // This is here only to keep IPDL-generated code happy. DO NOT USE. bool operator==(const nsIntRect& aRect) const {