зеркало из https://github.com/mozilla/moz-skia.git
add fFx1 field, returning where to start the iterator for fCount1 range
add unittest git-svn-id: http://skia.googlecode.com/svn/trunk@1145 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
Родитель
b692a14984
Коммит
63c1ad82fc
|
@ -31,11 +31,12 @@ struct SkClampRange {
|
|||
int fCount0; // count for fV0
|
||||
int fCount1; // count for interpolating (fV0...fV1)
|
||||
int fCount2; // count for fV1
|
||||
SkFixed fFx1; // initial fx value for the fCount1 range.
|
||||
// only valid if fCount1 > 0
|
||||
int fV0, fV1;
|
||||
bool fOverflowed; // true if we had to clamp due to numerical overflow
|
||||
|
||||
void init(SkFixed fx, SkFixed dx, int count, int v0, int v1);
|
||||
|
||||
static void UnitTest();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -42,12 +42,24 @@ void SkClampRange::init(SkFixed fx, SkFixed dx, int count, int v0, int v1) {
|
|||
fV0 = v0;
|
||||
fV1 = v1;
|
||||
|
||||
// check for over/underflow
|
||||
{
|
||||
int64_t eex = (int64_t)fx + count * (int64_t)dx;
|
||||
if (eex > SK_FixedMax) {
|
||||
|
||||
} else if (eex < -SK_FixedMax) {
|
||||
}
|
||||
}
|
||||
|
||||
// remember our original fx
|
||||
const SkFixed fx0 = fx;
|
||||
// start with ex equal to the last computed value
|
||||
SkFixed ex = fx + (count - 1) * dx;
|
||||
|
||||
if ((unsigned)(fx | ex) <= 0xFFFF) {
|
||||
fCount0 = fCount2 = 0;
|
||||
fCount1 = count;
|
||||
fFx1 = fx;
|
||||
return;
|
||||
}
|
||||
if (fx <= 0 && ex <= 0) {
|
||||
|
@ -96,83 +108,11 @@ void SkClampRange::init(SkFixed fx, SkFixed dx, int count, int v0, int v1) {
|
|||
if (doSwap) {
|
||||
SkTSwap(fCount0, fCount2);
|
||||
SkTSwap(fV0, fV1);
|
||||
dx = -dx;
|
||||
}
|
||||
|
||||
if (fCount1 > 0) {
|
||||
fFx1 = fx0 + fCount0 * dx;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////
|
||||
#include "SkRandom.h"
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
static bool in_range(int x, int target, int slop) {
|
||||
SkASSERT(slop >= 0);
|
||||
return SkAbs32(x - target) <= slop;
|
||||
}
|
||||
|
||||
static int classify_value(SkFixed fx, int v0, int v1) {
|
||||
if (fx <= 0) {
|
||||
return v0;
|
||||
}
|
||||
if (fx >= 0xFFFF) {
|
||||
return v1;
|
||||
}
|
||||
SkASSERT(!"bad fx");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define V0 -42
|
||||
#define V1 1024
|
||||
|
||||
static void slow_check(const SkClampRange& range,
|
||||
SkFixed fx, SkFixed dx, int count) {
|
||||
SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < range.fCount0; i++) {
|
||||
int v = classify_value(fx, V0, V1);
|
||||
SkASSERT(v == range.fV0);
|
||||
fx += dx;
|
||||
}
|
||||
for (i = 0; i < range.fCount1; i++) {
|
||||
SkASSERT(fx >= 0 && fx <= 0xFFFF);
|
||||
fx += dx;
|
||||
}
|
||||
for (i = 0; i < range.fCount2; i++) {
|
||||
int v = classify_value(fx, V0, V1);
|
||||
SkASSERT(v == range.fV1);
|
||||
fx += dx;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_range(SkFixed fx, SkFixed dx, int count) {
|
||||
SkClampRange range;
|
||||
range.init(fx, dx, count, V0, V1);
|
||||
slow_check(range, fx, dx, count);
|
||||
}
|
||||
|
||||
#define ff(x) SkIntToFixed(x)
|
||||
|
||||
void SkClampRange::UnitTest() {
|
||||
test_range(0, 0, 20);
|
||||
test_range(0xFFFF, 0, 20);
|
||||
test_range(-ff(2), 0, 20);
|
||||
test_range( ff(2), 0, 20);
|
||||
|
||||
test_range(-10, 1, 20);
|
||||
test_range(10, -1, 20);
|
||||
test_range(-10, 3, 20);
|
||||
test_range(10, -3, 20);
|
||||
|
||||
SkRandom rand;
|
||||
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
SkFixed fx = rand.nextS() >> 1;
|
||||
SkFixed sx = rand.nextS() >> 1;
|
||||
int count = rand.nextU() % 1000 + 1;
|
||||
SkFixed dx = (sx - fx) / count;
|
||||
test_range(fx, dx, count);
|
||||
}
|
||||
}
|
||||
#else
|
||||
void SkClampRange::UnitTest() {}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
#include "Test.h"
|
||||
#include "SkClampRange.h"
|
||||
#include "SkRandom.h"
|
||||
|
||||
static skiatest::Reporter* gReporter;
|
||||
|
||||
static int classify_value(SkFixed fx, int v0, int v1) {
|
||||
if (fx <= 0) {
|
||||
return v0;
|
||||
}
|
||||
if (fx >= 0xFFFF) {
|
||||
return v1;
|
||||
}
|
||||
REPORTER_ASSERT(gReporter, false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define V0 -42
|
||||
#define V1 1024
|
||||
|
||||
static void slow_check(const SkClampRange& range,
|
||||
SkFixed fx, SkFixed dx, int count) {
|
||||
SkASSERT(range.fCount0 + range.fCount1 + range.fCount2 == count);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < range.fCount0; i++) {
|
||||
int v = classify_value(fx, V0, V1);
|
||||
REPORTER_ASSERT(gReporter, v == range.fV0);
|
||||
fx += dx;
|
||||
}
|
||||
REPORTER_ASSERT(gReporter, range.fCount1 == 0 || fx == range.fFx1);
|
||||
for (i = 0; i < range.fCount1; i++) {
|
||||
REPORTER_ASSERT(gReporter, fx >= 0 && fx <= 0xFFFF);
|
||||
fx += dx;
|
||||
}
|
||||
for (i = 0; i < range.fCount2; i++) {
|
||||
int v = classify_value(fx, V0, V1);
|
||||
REPORTER_ASSERT(gReporter, v == range.fV1);
|
||||
fx += dx;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_range(SkFixed fx, SkFixed dx, int count) {
|
||||
SkClampRange range;
|
||||
range.init(fx, dx, count, V0, V1);
|
||||
slow_check(range, fx, dx, count);
|
||||
}
|
||||
|
||||
#define ff(x) SkIntToFixed(x)
|
||||
|
||||
static void TestClampRange(skiatest::Reporter* reporter) {
|
||||
gReporter = reporter;
|
||||
|
||||
test_range(0, 0, 20);
|
||||
test_range(0xFFFF, 0, 20);
|
||||
test_range(-ff(2), 0, 20);
|
||||
test_range( ff(2), 0, 20);
|
||||
|
||||
test_range(-10, 1, 20);
|
||||
test_range(10, -1, 20);
|
||||
test_range(-10, 3, 20);
|
||||
test_range(10, -3, 20);
|
||||
|
||||
SkRandom rand;
|
||||
|
||||
// test non-overflow cases
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
SkFixed fx = rand.nextS() >> 1;
|
||||
SkFixed sx = rand.nextS() >> 1;
|
||||
int count = rand.nextU() % 1000 + 1;
|
||||
SkFixed dx = (sx - fx) / count;
|
||||
test_range(fx, dx, count);
|
||||
}
|
||||
|
||||
// test overflow cases
|
||||
for (int i = 0; i < 0*1000000; i++) {
|
||||
SkFixed fx = rand.nextS();
|
||||
SkFixed sx = rand.nextS();
|
||||
int count = rand.nextU() % 1000 + 1;
|
||||
SkFixed dx = (sx - fx) / count;
|
||||
test_range(fx, dx, count);
|
||||
}
|
||||
}
|
||||
|
||||
#include "TestClassDef.h"
|
||||
DEFINE_TESTCLASS("ClampRange", ClampRangeClass, TestClampRange)
|
|
@ -2,6 +2,7 @@ SOURCE := \
|
|||
BitmapCopyTest.cpp \
|
||||
BitmapGetColorTest.cpp \
|
||||
BlitRowTest.cpp \
|
||||
ClampRangeTest.cpp \
|
||||
ClipCubicTest.cpp \
|
||||
ClipStackTest.cpp \
|
||||
ClipperTest.cpp \
|
||||
|
|
Загрузка…
Ссылка в новой задаче