diff options
author | Derek Sollenberger <djsollen@google.com> | 2011-06-23 17:32:30 -0400 |
---|---|---|
committer | Derek Sollenberger <djsollen@google.com> | 2011-06-23 17:32:30 -0400 |
commit | 0199fa7423f89a129da2b22a488f2c18e2e4727f (patch) | |
tree | a831057e846066083c5bab3fb014313678ad1cac /tests | |
parent | 4eb5f622ae8a529cdf2a2fe6347e6bd97b5f28cb (diff) | |
download | external_skia-0199fa7423f89a129da2b22a488f2c18e2e4727f.zip external_skia-0199fa7423f89a129da2b22a488f2c18e2e4727f.tar.gz external_skia-0199fa7423f89a129da2b22a488f2c18e2e4727f.tar.bz2 |
Skia Merge (revision 1562)
Change-Id: Ief005abc9eb741ccf5efec3d1f29fe2dfc23103d
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Android.mk | 1 | ||||
-rw-r--r-- | tests/MathTest.cpp | 2 | ||||
-rw-r--r-- | tests/Matrix44Test.cpp | 70 | ||||
-rw-r--r-- | tests/PathCoverageTest.cpp | 129 | ||||
-rw-r--r-- | tests/Reader32Test.cpp | 4 | ||||
-rw-r--r-- | tests/tests_files.mk | 1 |
6 files changed, 205 insertions, 2 deletions
diff --git a/tests/Android.mk b/tests/Android.mk index 5823b0e..4db6c75 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -24,6 +24,7 @@ LOCAL_SRC_FILES:= \ PackBitsTest.cpp \ PaintTest.cpp \ ParsePathTest.cpp \ + PathCoverageTest.cpp \ PathMeasureTest.cpp \ PathTest.cpp \ Reader32Test.cpp \ diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp index 7a9364f..efdad3a 100644 --- a/tests/MathTest.cpp +++ b/tests/MathTest.cpp @@ -188,7 +188,7 @@ static float make_zero() { static void unittest_isfinite(skiatest::Reporter* reporter) { #ifdef SK_SCALAR_IS_FLOAT - float nan = ::asin(2); + float nan = sk_float_asin(2); float inf = 1.0 / make_zero(); float big = 3.40282e+038; diff --git a/tests/Matrix44Test.cpp b/tests/Matrix44Test.cpp new file mode 100644 index 0000000..8755bd3 --- /dev/null +++ b/tests/Matrix44Test.cpp @@ -0,0 +1,70 @@ +#include "Test.h" +#include "SkMatrix44.h" + +static bool nearly_equal_scalar(SkScalar a, SkScalar b) { + // Note that we get more compounded error for multiple operations when + // SK_SCALAR_IS_FIXED. +#ifdef SK_SCALAR_IS_FLOAT + const SkScalar tolerance = SK_Scalar1 / 200000; +#else + const SkScalar tolerance = SK_Scalar1 / 1024; +#endif + + return SkScalarAbs(a - b) <= tolerance; +} + +static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) { + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + if (!nearly_equal_scalar(a.get(i, j), b.get(i, j))) { + printf("not equal %g %g\n", (float)a.get(i, j), (float)b.get(i, j)); + return false; + } + } + } + return true; +} + +static bool is_identity(const SkMatrix44& m) { + SkMatrix44 identity; + identity.reset(); + return nearly_equal(m, identity); +} + + +void TestMatrix44(skiatest::Reporter* reporter) { + SkMatrix44 mat, inverse, iden1, iden2, rot; + + mat.reset(); + mat.setTranslate(SK_Scalar1, SK_Scalar1, SK_Scalar1); + mat.invert(&inverse); + iden1.setConcat(mat, inverse); + REPORTER_ASSERT(reporter, is_identity(iden1)); + + mat.setScale(SkIntToScalar(2), SkIntToScalar(2), SkIntToScalar(2)); + mat.invert(&inverse); + iden1.setConcat(mat, inverse); + REPORTER_ASSERT(reporter, is_identity(iden1)); + + mat.setScale(SK_Scalar1/2, SK_Scalar1/2, SK_Scalar1/2); + mat.invert(&inverse); + iden1.setConcat(mat, inverse); + REPORTER_ASSERT(reporter, is_identity(iden1)); + + mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20)); + rot.setRotateDegreesAbout( + SkIntToScalar(0), + SkIntToScalar(0), + SkIntToScalar(-1), + SkIntToScalar(90)); + mat.postConcat(rot); + REPORTER_ASSERT(reporter, mat.invert(NULL)); + mat.invert(&inverse); + iden1.setConcat(mat, inverse); + REPORTER_ASSERT(reporter, is_identity(iden1)); + iden2.setConcat(inverse, mat); + REPORTER_ASSERT(reporter, is_identity(iden2)); +} + +#include "TestClassDef.h" +DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44) diff --git a/tests/PathCoverageTest.cpp b/tests/PathCoverageTest.cpp new file mode 100644 index 0000000..8676029 --- /dev/null +++ b/tests/PathCoverageTest.cpp @@ -0,0 +1,129 @@ +#include "SkPoint.h" +#include "SkScalar.h" +#include "Test.h" + +/* + Duplicates lots of code from gpu/src/GrPathUtils.cpp + It'd be nice not to do so, but that code's set up currently to only have a single implementation. +*/ + +#define MAX_COEFF_SHIFT 6 +static const uint32_t MAX_POINTS_PER_CURVE = 1 << MAX_COEFF_SHIFT; + +static inline int cheap_distance(SkScalar dx, SkScalar dy) { + int idx = SkAbs32(SkScalarRound(dx)); + int idy = SkAbs32(SkScalarRound(dy)); + if (idx > idy) { + idx += idy >> 1; + } else { + idx = idy + (idx >> 1); + } + return idx; +} + +static inline int diff_to_shift(SkScalar dx, SkScalar dy) { + int dist = cheap_distance(dx, dy); + return (32 - SkCLZ(dist)); +} + +uint32_t estimatedQuadraticPointCount(const SkPoint points[], SkScalar tol) { + int shift = diff_to_shift(points[1].fX * 2 - points[2].fX - points[0].fX, + points[1].fY * 2 - points[2].fY - points[0].fY); + SkASSERT(shift >= 0); + //SkDebugf("Quad shift %d;", shift); + // bias to more closely approximate exact value, then clamp to zero + shift -= 2; + shift &= ~(shift>>31); + + if (shift > MAX_COEFF_SHIFT) { + shift = MAX_COEFF_SHIFT; + } + uint32_t count = 1 << shift; + //SkDebugf(" biased shift %d, scale %u\n", shift, count); + return count; +} + +uint32_t computedQuadraticPointCount(const SkPoint points[], SkScalar tol) { + SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); + if (d < tol) { + return 1; + } else { + int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol))); + uint32_t count = SkMinScalar(SkNextPow2(temp), MAX_POINTS_PER_CURVE); + return count; + } +} + +// Curve from samplecode/SampleSlides.cpp +static const int gXY[] = { + 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 +}; + +static const int gSawtooth[] = { + 0, 0, 10, 10, 20, 20, 30, 10, 40, 0, 50, -10, 60, -20, 70, -10, 80, 0 +}; + +static const int gOvalish[] = { + 0, 0, 5, 15, 20, 20, 35, 15, 40, 0 +}; + +static const int gSharpSawtooth[] = { + 0, 0, 1, 10, 2, 0, 3, -10, 4, 0 +}; + +// Curve crosses back over itself around 0,10 +static const int gRibbon[] = { + -4, 0, 4, 20, 0, 25, -4, 20, 4, 0 +}; + +static bool one_d_pe(const int* array, const unsigned int count, + skiatest::Reporter* reporter) { + SkPoint path [3]; + path[1] = SkPoint::Make(SkIntToScalar(array[0]), SkIntToScalar(array[1])); + path[2] = SkPoint::Make(SkIntToScalar(array[2]), SkIntToScalar(array[3])); + int numErrors = 0; + for (unsigned i = 4; i < (count); i += 2) { + path[0] = path[1]; + path[1] = path[2]; + path[2] = SkPoint::Make(SkIntToScalar(array[i]), + SkIntToScalar(array[i+1])); + uint32_t computedCount = + computedQuadraticPointCount(path, SkIntToScalar(1)); + uint32_t estimatedCount = + estimatedQuadraticPointCount(path, SkIntToScalar(1)); + // Allow estimated to be off by a factor of two, but no more. + if ((estimatedCount > 2 * computedCount) || + (computedCount > estimatedCount * 2)) { + SkString errorDescription; + errorDescription.printf( + "Curve from %.2f %.2f through %.2f %.2f to %.2f %.2f " + "computes %d, estimates %d\n", + path[0].fX, path[0].fY, path[1].fX, path[1].fY, + path[2].fX, path[2].fY, computedCount, estimatedCount); + numErrors++; + reporter->reportFailed(errorDescription); + } + } + + if (numErrors > 0) + printf("%d curve segments differ\n", numErrors); + return (numErrors == 0); +} + + + +static void TestQuadPointCount(skiatest::Reporter* reporter) { + one_d_pe(gXY, SK_ARRAY_COUNT(gXY), reporter); + one_d_pe(gSawtooth, SK_ARRAY_COUNT(gSawtooth), reporter); + one_d_pe(gOvalish, SK_ARRAY_COUNT(gOvalish), reporter); + one_d_pe(gSharpSawtooth, SK_ARRAY_COUNT(gSharpSawtooth), reporter); + one_d_pe(gRibbon, SK_ARRAY_COUNT(gRibbon), reporter); +} + +static void TestPathCoverage(skiatest::Reporter* reporter) { + TestQuadPointCount(reporter); + +} + +#include "TestClassDef.h" +DEFINE_TESTCLASS("PathCoverage", PathCoverageTestClass, TestPathCoverage) diff --git a/tests/Reader32Test.cpp b/tests/Reader32Test.cpp index c752b0f..cad2d33 100644 --- a/tests/Reader32Test.cpp +++ b/tests/Reader32Test.cpp @@ -52,7 +52,9 @@ static void Tests(skiatest::Reporter* reporter) { const int32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; const SkScalar data2[] = { 0, SK_Scalar1, -SK_Scalar1, SK_Scalar1/2 }; - char buffer[SkMax32(sizeof(data), sizeof(data2))]; + const size_t bufsize = sizeof(data) > sizeof(data2) ? + sizeof(data) : sizeof(data2); + char buffer[bufsize]; reader.setMemory(data, sizeof(data)); for (i = 0; i < SK_ARRAY_COUNT(data); ++i) { diff --git a/tests/tests_files.mk b/tests/tests_files.mk index 9b90179..667c9b5 100644 --- a/tests/tests_files.mk +++ b/tests/tests_files.mk @@ -20,6 +20,7 @@ SOURCE := \ PackBitsTest.cpp \ PaintTest.cpp \ ParsePathTest.cpp \ + PathCoverageTest.cpp \ PathMeasureTest.cpp \ PathTest.cpp \ Reader32Test.cpp \ |