Add rect subtraction utility function+tests

This operation came when updating aggregate inner and outer bounds
for Ganesh' new clip stack (particularly when accounting for the effect
of a difference operation). This geometric operation is theoretically
more general purpose so I moved it out to SkRectPriv.

Change-Id: Ibd76f9b95efc1790ecda1038779c124155031d8f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/283756
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
This commit is contained in:
Michael Ludwig 2020-04-15 15:26:05 -04:00 committed by Skia Commit-Bot
parent 583c24c665
commit 9e1e913c81
3 changed files with 178 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include "include/core/SkRect.h"
#include "include/private/SkMalloc.h"
#include "src/core/SkRectPriv.h"
bool SkIRect::intersect(const SkIRect& a, const SkIRect& b) {
SkIRect tmp = {
@ -166,3 +167,88 @@ void SkRect::dump(bool asHex) const {
}
SkDebugf("%s\n", line.c_str());
}
////////////////////////////////////////////////////////////////////////////////////////////////
template<typename R, typename C>
static bool subtract(const R& a, const R& b, R* out) {
static constexpr C kZero = C(0);
if (!R::Intersects(a, b)) {
// Either already empty, or subtracting the empty rect, or there's no intersection, so
// in all cases the answer is A.
*out = a;
return true;
}
// 4 rectangles to consider. If the edge in A is contained in B, the resulting difference can
// be represented exactly as a rectangle. Otherwise the difference is the largest subrectangle
// that is disjoint from B:
// 1. Left part of A: (A.left, A.top, B.left, A.bottom)
// 2. Right part of A: (B.right, A.top, A.right, A.bottom)
// 3. Top part of A: (A.left, A.top, A.right, B.top)
// 4. Bottom part of A: (A.left, B.bottom, A.right, A.bottom)
C height = a.height();
C width = a.width();
// Compute the areas of the 4 rects described above. Depending on how B intersects A, there
// will be 1 to 4 positive areas:
// - 4 occur when A contains B
// - 3 occur when B intersects a single edge
// - 2 occur when B intersects at a corner, or spans two opposing edges
// - 1 occurs when B spans two opposing edges and contains a 3rd, resulting in an exact rect
// - 0 occurs when B contains A, resulting in the empty rect
C leftArea = kZero, rightArea = kZero, topArea = kZero, bottomArea = kZero;
int positiveCount = 0;
if (b.fLeft > a.fLeft) {
leftArea = (b.fLeft - a.fLeft) * height;
positiveCount++;
}
if (a.fRight > b.fRight) {
rightArea = (a.fRight - b.fRight) * height;
positiveCount++;
}
if (b.fTop > a.fTop) {
topArea = (b.fTop - a.fTop) * width;
positiveCount++;
}
if (a.fBottom > b.fBottom) {
bottomArea = (a.fBottom - b.fBottom) * width;
positiveCount++;
}
if (positiveCount == 0) {
SkASSERT(b.contains(a));
*out = R::MakeEmpty();
return true;
}
*out = a;
if (leftArea > rightArea && leftArea > topArea && leftArea > bottomArea) {
// Left chunk of A, so the new right edge is B's left edge
out->fRight = b.fLeft;
} else if (rightArea > topArea && rightArea > bottomArea) {
// Right chunk of A, so the new left edge is B's right edge
out->fLeft = b.fRight;
} else if (topArea > bottomArea) {
// Top chunk of A, so the new bottom edge is B's top edge
out->fBottom = b.fTop;
} else {
// Bottom chunk of A, so the new top edge is B's bottom edge
SkASSERT(bottomArea > kZero);
out->fTop = b.fBottom;
}
// If we have 1 valid area, the disjoint shape is representable as a rectangle.
SkASSERT(!R::Intersects(*out, b));
return positiveCount == 1;
}
bool SkRectPriv::Subtract(const SkRect& a, const SkRect& b, SkRect* out) {
return subtract<SkRect, SkScalar>(a, b, out);
}
bool SkRectPriv::Subtract(const SkIRect& a, const SkIRect& b, SkIRect* out) {
return subtract<SkIRect, int>(a, b, out);
}

View File

@ -58,6 +58,25 @@ public:
return SkTFitsIn<int16_t>(r.fLeft) && SkTFitsIn<int16_t>(r.fTop) &&
SkTFitsIn<int16_t>(r.fRight) && SkTFitsIn<int16_t>(r.fBottom);
}
// Evaluate A-B. If the difference shape cannot be represented as a rectangle then false is
// returned and 'out' is set to the largest rectangle contained in said shape. If true is
// returned then A-B is representable as a rectangle, which is stored in 'out'.
static bool Subtract(const SkRect& a, const SkRect& b, SkRect* out);
static bool Subtract(const SkIRect& a, const SkIRect& b, SkIRect* out);
// Evaluate A-B, and return the largest rectangle contained in that shape (since the difference
// may not be representable as rectangle). The returned rectangle will not intersect B.
static SkRect Subtract(const SkRect& a, const SkRect& b) {
SkRect diff;
Subtract(a, b, &diff);
return diff;
}
static SkIRect Subtract(const SkIRect& a, const SkIRect& b) {
SkIRect diff;
Subtract(a, b, &diff);
return diff;
}
};

View File

@ -160,6 +160,79 @@ DEF_TEST(Rect_center, reporter) {
REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.height()));
}
DEF_TEST(Rect_subtract, reporter) {
struct Expectation {
SkIRect fA;
SkIRect fB;
SkIRect fExpected;
bool fExact;
};
SkIRect a = SkIRect::MakeLTRB(2, 3, 12, 15);
Expectation tests[] = {
// B contains A == empty rect
{a, a.makeOutset(2, 2), SkIRect::MakeEmpty(), true},
// A contains B, producing 4x12 (left), 2x12 (right), 4x10(top), and 5x10(bottom)
{a, {6, 6, 10, 10}, {2, 10, 12, 15}, false},
// A is empty, B is not == empty rect
{SkIRect::MakeEmpty(), a, SkIRect::MakeEmpty(), true},
// A is not empty, B is empty == a
{a, SkIRect::MakeEmpty(), a, true},
// A and B are empty == empty
{SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), true},
// A and B do not intersect == a
{a, {15, 17, 20, 40}, a, true},
// B cuts off left side of A, producing 6x12 (right)
{a, {0, 0, 6, 20}, {6, 3, 12, 15}, true},
// B cuts off right side of A, producing 4x12 (left)
{a, {6, 0, 20, 20}, {2, 3, 6, 15}, true},
// B cuts off top side of A, producing 10x9 (bottom)
{a, {0, 0, 20, 6}, {2, 6, 12, 15}, true},
// B cuts off bottom side of A, producing 10x7 (top)
{a, {0, 10, 20, 20}, {2, 3, 12, 10}, true},
// B splits A horizontally, producing 10x3 (top) or 10x5 (bottom)
{a, {0, 6, 20, 10}, {2, 10, 12, 15}, false},
// B splits A vertically, producing 4x12 (left) or 2x12 (right)
{a, {6, 0, 10, 20}, {2, 3, 6, 15}, false},
// B cuts top-left of A, producing 8x12 (right) or 10x11 (bottom)
{a, {0, 0, 4, 4}, {2, 4, 12, 15}, false},
// B cuts top-right of A, producing 8x12 (left) or 10x8 (bottom)
{a, {10, 0, 14, 7}, {2, 3, 10, 15}, false},
// B cuts bottom-left of A, producing 7x12 (right) or 10x9 (top)
{a, {0, 12, 5, 20}, {2, 3, 12, 12}, false},
// B cuts bottom-right of A, producing 8x12 (left) or 10x9 (top)
{a, {10, 12, 20, 20}, {2, 3, 10, 15}, false},
// B crosses the left of A, producing 4x12 (right) or 10x3 (top) or 10x5 (bottom)
{a, {0, 6, 8, 10}, {2, 10, 12, 15}, false},
// B crosses the right side of A, producing 6x12 (left) or 10x3 (top) or 10x5 (bottom)
{a, {8, 6, 20, 10}, {2, 3, 8, 15}, false},
// B crosses the top side of A, producing 4x12 (left) or 2x12 (right) or 10x8 (bottom)
{a, {6, 0, 10, 7}, {2, 7, 12, 15}, false},
// B crosses the bottom side of A, producing 1x12 (left) or 4x12 (right) or 10x3 (top)
{a, {4, 6, 8, 20}, {8, 3, 12, 15}, false}
};
for (const Expectation& e : tests) {
SkIRect difference;
bool exact = SkRectPriv::Subtract(e.fA, e.fB, &difference);
REPORTER_ASSERT(reporter, exact == e.fExact);
REPORTER_ASSERT(reporter, difference == e.fExpected);
// Generate equivalent tests for the SkRect case by moving the input rects by 0.5px
SkRect af = SkRect::Make(e.fA);
SkRect bf = SkRect::Make(e.fB);
SkRect ef = SkRect::Make(e.fExpected);
af.offset(0.5f, 0.5f);
bf.offset(0.5f, 0.5f);
ef.offset(0.5f, 0.5f);
SkRect df;
exact = SkRectPriv::Subtract(af, bf, &df);
REPORTER_ASSERT(reporter, exact == e.fExact);
REPORTER_ASSERT(reporter, (df.isEmpty() && ef.isEmpty()) || (df == ef));
}
}
#include "include/core/SkSurface.h"
// Before the fix, this sequence would trigger a release_assert in the Tiler