Add comparison and assignment to SkClipStack (and fix an optimization bug).

Review URL: http://codereview.appspot.com/4423085

git-svn-id: http://skia.googlecode.com/svn/trunk@1230 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
vandebo@chromium.org 2011-05-03 16:26:09 +00:00
parent 7894b92b57
commit 1e1c36f4f8
3 changed files with 146 additions and 1 deletions

View File

@ -10,8 +10,13 @@ class SkPath;
class SK_API SkClipStack {
public:
SkClipStack();
SkClipStack(const SkClipStack& b);
~SkClipStack() {}
SkClipStack& operator=(const SkClipStack& b);
bool operator==(const SkClipStack& b) const;
bool operator!=(const SkClipStack& b) const { return !(*this == b); }
void reset();
int getSaveCount() const { return fSaveCount; }

View File

@ -27,11 +27,33 @@ struct SkClipStack::Rec {
fState = kPath_State;
}
bool operator==(const Rec& b) const {
if (fSaveCount != b.fSaveCount || fOp != b.fOp || fState != b.fState) {
return false;
}
switch (fState) {
case kEmpty_State:
return true;
case kRect_State:
return fRect == b.fRect;
case kPath_State:
return fPath == b.fPath;
}
return false; // Silence the compiler.
}
bool operator!=(const Rec& b) const {
return !(*this == b);
}
/**
* Returns true if this Rec can be intersected in place with a new clip
*/
bool canBeIntersected(int saveCount, SkRegion::Op op) const {
if (kEmpty_State == fState) {
if (kEmpty_State == fState && (
SkRegion::kDifference_Op == op ||
SkRegion::kIntersect_Op == op)) {
return true;
}
return fSaveCount == saveCount &&
@ -44,6 +66,46 @@ SkClipStack::SkClipStack() : fDeque(sizeof(Rec)) {
fSaveCount = 0;
}
SkClipStack::SkClipStack(const SkClipStack& b) : fDeque(sizeof(Rec)) {
*this = b;
}
SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
if (this == &b) {
return *this;
}
reset();
fSaveCount = b.fSaveCount;
SkDeque::F2BIter recIter(b.fDeque);
for (const Rec* rec = (const Rec*)recIter.next();
rec != NULL;
rec = (const Rec*)recIter.next()) {
new (fDeque.push_back()) Rec(*rec);
}
return *this;
}
bool SkClipStack::operator==(const SkClipStack& b) const {
if (fSaveCount != b.fSaveCount || fDeque.count() != b.fDeque.count()) {
return false;
}
SkDeque::F2BIter myIter(fDeque);
SkDeque::F2BIter bIter(b.fDeque);
const Rec* myRec = (const Rec*)myIter.next();
const Rec* bRec = (const Rec*)bIter.next();
while (myRec != NULL && bRec != NULL) {
if (*myRec != *bRec) {
return false;
}
myRec = (const Rec*)myIter.next();
bRec = (const Rec*)bIter.next();
}
return myRec == NULL && bRec == NULL;
}
void SkClipStack::reset() {
// don't have a reset() on SkDeque, so fake it here
fDeque.~SkDeque();

View File

@ -1,5 +1,81 @@
#include "Test.h"
#include "SkClipStack.h"
#include "SkPath.h"
#include "SkRect.h"
static void test_assign_and_comparison(skiatest::Reporter* reporter) {
SkClipStack s;
// Build up a clip stack with a path, an empty clip, and a rect.
s.save();
SkPath p;
p.moveTo(5, 6);
p.lineTo(7, 8);
p.lineTo(5, 9);
p.close();
s.clipDevPath(p);
s.save();
SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
s.clipDevRect(r);
r = SkRect::MakeLTRB(10, 11, 12, 13);
s.clipDevRect(r);
s.save();
r = SkRect::MakeLTRB(14, 15, 16, 17);
s.clipDevRect(r, SkRegion::kUnion_Op);
// Test that assignment works.
SkClipStack copy = s;
REPORTER_ASSERT(reporter, s == copy);
// Test that different save levels triggers not equal.
s.restore();
REPORTER_ASSERT(reporter, s != copy);
// Test that an equal, but not copied version is equal.
s.save();
r = SkRect::MakeLTRB(14, 15, 16, 17);
s.clipDevRect(r, SkRegion::kUnion_Op);
REPORTER_ASSERT(reporter, s == copy);
// Test that a different op on one level triggers not equal.
s.restore();
s.save();
r = SkRect::MakeLTRB(14, 15, 16, 17);
s.clipDevRect(r);
REPORTER_ASSERT(reporter, s != copy);
// Test that different state (clip type) triggers not equal.
s.restore();
s.save();
SkPath rp;
rp.addRect(r);
s.clipDevPath(rp, SkRegion::kUnion_Op);
REPORTER_ASSERT(reporter, s != copy);
// Test that different rects triggers not equal.
s.restore();
s.save();
r = SkRect::MakeLTRB(24, 25, 26, 27);
s.clipDevRect(r, SkRegion::kUnion_Op);
REPORTER_ASSERT(reporter, s != copy);
// Sanity check
s.restore();
copy.restore();
REPORTER_ASSERT(reporter, s == copy);
s.restore();
copy.restore();
REPORTER_ASSERT(reporter, s == copy);
// Test that different paths triggers not equal.
s.restore();
s.save();
p.addRect(r);
s.clipDevPath(p);
REPORTER_ASSERT(reporter, s != copy);
}
static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
int count) {
@ -43,6 +119,8 @@ static void TestClipStack(skiatest::Reporter* reporter) {
stack.reset();
assert_count(reporter, stack, 0);
test_assign_and_comparison(reporter);
}
#include "TestClassDef.h"