saturate IPoint math, handle NaN in isEmpty

Bug: skia:7507
Change-Id: Ibdb40584effdea70e6499eab1bb64bb4b2260d06
Reviewed-on: https://skia-review.googlesource.com/106972
Commit-Queue: Mike Reed <reed@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
Reviewed-by: Mike Klein <mtklein@chromium.org>
This commit is contained in:
Mike Reed 2018-02-13 12:48:57 -05:00 committed by Skia Commit-Bot
parent 0c6ea6c77a
commit 655bf8f022
4 changed files with 23 additions and 16 deletions

View File

@ -10,6 +10,7 @@
#include "SkMath.h"
#include "SkScalar.h"
#include "../private/SkSafe32.h"
/** \struct SkIPoint16
SkIPoint holds two 16 bit integer coordinates
@ -115,8 +116,8 @@ struct SkIPoint {
@param v ivector to add
*/
void operator+=(const SkIVector& v) {
fX += v.fX;
fY += v.fY;
fX = Sk32_sat_add(fX, v.fX);
fY = Sk32_sat_add(fY, v.fY);
}
/** Subtracts ivector v from SkIPoint. Sets SkIPoint to: (fX - v.fX, fY - v.fY).
@ -124,8 +125,8 @@ struct SkIPoint {
@param v ivector to subtract
*/
void operator-=(const SkIVector& v) {
fX -= v.fX;
fY -= v.fY;
fX = Sk32_sat_sub(fX, v.fX);
fY = Sk32_sat_sub(fY, v.fY);
}
/** Returns true if SkIPoint is equivalent to SkIPoint constructed from (x, y).
@ -167,7 +168,7 @@ struct SkIPoint {
@return ivector from b to a
*/
friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) {
return {a.fX - b.fX, a.fY - b.fY};
return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) };
}
/** Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as: (a.fX + b.fX, a.fY + b.fY).
@ -180,7 +181,7 @@ struct SkIPoint {
@return SkIPoint equal to a offset by b
*/
friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) {
return {a.fX + b.fX, a.fY + b.fY};
return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) };
}
};

View File

@ -849,12 +849,18 @@ struct SK_API SkRect {
}
/** Returns true if fLeft is equal to or greater than fRight, or if fTop is equal
to or greater than fBottom. Call sort() to reverse rectangles with negative
width() or height().
@return true if width() or height() are zero or negative
*/
bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
* to or greater than fBottom. Call sort() to reverse rectangles with negative
* width() or height().
*
* This function also returns true if any of the values are NaN.
*
* @return true if width() or height() are zero or negative
*/
bool isEmpty() const {
// We write it as the NOT of a non-empty rect, so we will return true if any values
// are NaN.
return !(fLeft < fRight && fTop < fBottom);
}
/** Returns true if fLeft is equal to or less than fRight, or if fTop is equal
to or less than fBottom. Call sort() to reverse rectangles with negative

View File

@ -88,8 +88,8 @@ void computeDisplacement(Extractor ex, const SkVector& scale, SkBitmap* dst,
SkScalar displX = scaleForColor.fX * ex.getX(c) + scaleAdj.fX;
SkScalar displY = scaleForColor.fY * ex.getY(c) + scaleAdj.fY;
// Truncate the displacement values
const int srcX = x + SkScalarTruncToInt(displX);
const int srcY = y + SkScalarTruncToInt(displY);
const int32_t srcX = Sk32_sat_add(x, SkScalarTruncToInt(displX));
const int32_t srcY = Sk32_sat_add(y, SkScalarTruncToInt(displY));
*dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ?
0 : *(src.getAddr32(srcX, srcY));
}

View File

@ -403,8 +403,8 @@ SkIRect SkMatrixConvolutionImageFilter::onFilterNodeBounds(const SkIRect& src, c
MapDirection direction) const {
SkIRect dst = src;
int w = fKernelSize.width() - 1, h = fKernelSize.height() - 1;
dst.fRight += w;
dst.fBottom += h;
dst.fRight = Sk32_sat_add(dst.fRight, w);
dst.fBottom = Sk32_sat_add(dst.fBottom, h);
if (kReverse_MapDirection == direction) {
dst.offset(-fKernelOffset);
} else {