Bug 1152838 - Fix BaseRect::InflateToMultiple and move it to nsIntRect. r=mattwoodrow, r=jrmuizel

This commit is contained in:
Markus Stange 2015-04-09 15:49:25 -04:00
parent 5c34ab4326
commit a2753b5251
4 changed files with 53 additions and 14 deletions

View File

@ -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<T>(floor(x / aMultiple.width)) * aMultiple.width;
y = static_cast<T>(floor(y / aMultiple.height)) * aMultiple.height;
xMost = static_cast<T>(ceil(x / aMultiple.width)) * aMultiple.width;
yMost = static_cast<T>(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)
{

37
gfx/2d/NumericTools.h Normal file
View File

@ -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_ */

View File

@ -26,6 +26,7 @@ EXPORTS.mozilla.gfx += [
'Helpers.h',
'Logging.h',
'Matrix.h',
'NumericTools.h',
'PathHelpers.h',
'PatternHelpers.h',
'Point.h',

View File

@ -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
{