Reland "Add an implementation and log2 variants for Wang's formula"
This is a reland of e278e1c1c7
Original change's description:
> Add an implementation and log2 variants for Wang's formula
>
> Wang's formulas for cubics and quadratics (1985) tell us how many line
> segments a curve must be chopped into when tessellating. This CL adds
> an implementation along with optimized log2 variants, as well as tests
> and a benchmark.
>
> Change-Id: I3f777b8d0312c57c3a1cc24307de5945c70be287
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/288321
> Reviewed-by: Brian Salomon <bsalomon@google.com>
> Commit-Queue: Chris Dalton <csmartdalton@google.com>
TBR=bsalomon@google.com
Change-Id: Ie3822c62439fc579a59ea8adb49583224de41aa5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/289680
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
This commit is contained in:
parent
e253a7c183
commit
f6bf516926
@ -7,9 +7,11 @@
|
||||
|
||||
#include "bench/Benchmark.h"
|
||||
#include "include/gpu/GrContext.h"
|
||||
#include "src/core/SkPathPriv.h"
|
||||
#include "src/gpu/GrContextPriv.h"
|
||||
#include "src/gpu/GrOpFlushState.h"
|
||||
#include "src/gpu/tessellate/GrTessellatePathOp.h"
|
||||
#include "src/gpu/tessellate/GrWangsFormula.h"
|
||||
#include "tools/ToolUtils.h"
|
||||
|
||||
// This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.)
|
||||
@ -96,6 +98,7 @@ public:
|
||||
class MiddleOutInnerTrianglesBench;
|
||||
class OuterCubicsBench;
|
||||
class CubicWedgesBench;
|
||||
class WangsFormulaBench;
|
||||
|
||||
private:
|
||||
void onDraw(int loops, SkCanvas*) final {
|
||||
@ -132,7 +135,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
DEF_BENCH( return new GrTessellatePathOp::TestingOnly_Benchmark::MiddleOutInnerTrianglesBench(););
|
||||
DEF_BENCH( return new GrTessellatePathOp::TestingOnly_Benchmark::MiddleOutInnerTrianglesBench(); );
|
||||
|
||||
class GrTessellatePathOp::TestingOnly_Benchmark::OuterCubicsBench
|
||||
: public GrTessellatePathOp::TestingOnly_Benchmark {
|
||||
@ -146,7 +149,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
DEF_BENCH( return new GrTessellatePathOp::TestingOnly_Benchmark::OuterCubicsBench(););
|
||||
DEF_BENCH( return new GrTessellatePathOp::TestingOnly_Benchmark::OuterCubicsBench(); );
|
||||
|
||||
class GrTessellatePathOp::TestingOnly_Benchmark::CubicWedgesBench
|
||||
: public GrTessellatePathOp::TestingOnly_Benchmark {
|
||||
@ -160,3 +163,40 @@ public:
|
||||
};
|
||||
|
||||
DEF_BENCH( return new GrTessellatePathOp::TestingOnly_Benchmark::CubicWedgesBench(););
|
||||
|
||||
class GrTessellatePathOp::TestingOnly_Benchmark::WangsFormulaBench
|
||||
: public GrTessellatePathOp::TestingOnly_Benchmark {
|
||||
public:
|
||||
WangsFormulaBench(const char* suffix, const SkMatrix& matrix)
|
||||
: TestingOnly_Benchmark(SkStringPrintf("wangs_formula_cubic_log2%s", suffix).c_str(),
|
||||
make_cubic_path(), SkMatrix::I())
|
||||
, fMatrix(matrix) {
|
||||
}
|
||||
void runBench(GrMeshDrawOp::Target*, GrTessellatePathOp* op) override {
|
||||
int sum = 0;
|
||||
GrVectorXform xform(fMatrix);
|
||||
for (auto [verb, pts, w] : SkPathPriv::Iterate(op->fPath)) {
|
||||
if (verb == SkPathVerb::kCubic) {
|
||||
sum += GrWangsFormula::cubic_log2(4, pts, xform);
|
||||
}
|
||||
}
|
||||
// Don't let the compiler optimize away GrWangsFormula::cubic_log2.
|
||||
if (sum <= 0) {
|
||||
SK_ABORT("sum should be > 0.");
|
||||
}
|
||||
}
|
||||
private:
|
||||
SkMatrix fMatrix;
|
||||
};
|
||||
|
||||
DEF_BENCH(
|
||||
return new GrTessellatePathOp::TestingOnly_Benchmark::WangsFormulaBench("", SkMatrix::I());
|
||||
);
|
||||
DEF_BENCH(
|
||||
return new GrTessellatePathOp::TestingOnly_Benchmark::WangsFormulaBench(
|
||||
"_scale", SkMatrix::MakeScale(1.1f, 0.9f));
|
||||
);
|
||||
DEF_BENCH(
|
||||
return new GrTessellatePathOp::TestingOnly_Benchmark::WangsFormulaBench(
|
||||
"_affine", SkMatrix::MakeAll(.9f,0.9f,0, 1.1f,1.1f,0, 0,0,1));
|
||||
);
|
||||
|
@ -445,6 +445,8 @@ skia_gpu_sources = [
|
||||
"$_src/gpu/tessellate/GrTessellatePathOp.h",
|
||||
"$_src/gpu/tessellate/GrTessellationPathRenderer.cpp",
|
||||
"$_src/gpu/tessellate/GrTessellationPathRenderer.h",
|
||||
"$_src/gpu/tessellate/GrVectorXform.h",
|
||||
"$_src/gpu/tessellate/GrWangsFormula.h",
|
||||
|
||||
# text
|
||||
"$_src/gpu/text/GrAtlasManager.cpp",
|
||||
|
@ -398,4 +398,5 @@ pathops_tests_sources = [
|
||||
"$_tests/PathOpsTigerTest.cpp",
|
||||
"$_tests/PathOpsTightBoundsTest.cpp",
|
||||
"$_tests/PathOpsTypesTest.cpp",
|
||||
"$_tests/WangsFormulaTest.cpp",
|
||||
]
|
||||
|
73
src/gpu/tessellate/GrVectorXform.h
Normal file
73
src/gpu/tessellate/GrVectorXform.h
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef GrVectorXform_DEFINED
|
||||
#define GrVectorXform_DEFINED
|
||||
|
||||
#include "include/core/SkMatrix.h"
|
||||
#include "include/private/SkNx.h"
|
||||
|
||||
// We enclose this class in the anonymous namespace so it can have Sk2f/Sk4f members.
|
||||
namespace { // NOLINT(google-build-namespaces)
|
||||
|
||||
// Represents the upper-left 2x2 matrix of an affine transform for applying to vectors:
|
||||
//
|
||||
// VectorXform(p1 - p0) == M * float3(p1, 1) - M * float3(p0, 1)
|
||||
//
|
||||
class GrVectorXform {
|
||||
public:
|
||||
explicit GrVectorXform() : fType(Type::kIdentity) {}
|
||||
explicit GrVectorXform(const SkMatrix& m) {
|
||||
SkASSERT(!m.hasPerspective());
|
||||
if (m.getType() & SkMatrix::kAffine_Mask) {
|
||||
fType = Type::kAffine;
|
||||
fScaleXSkewY = {m.getScaleX(), m.getSkewY()};
|
||||
fSkewXScaleY = {m.getSkewX(), m.getScaleY()};
|
||||
fScaleXYXY = {m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()};
|
||||
fSkewXYXY = {m.getSkewX(), m.getSkewY(), m.getSkewX(), m.getSkewY()};
|
||||
} else if (m.getType() & SkMatrix::kScale_Mask) {
|
||||
fType = Type::kScale;
|
||||
fScaleXY = {m.getScaleX(), m.getScaleY()};
|
||||
fScaleXYXY = {m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()};
|
||||
} else {
|
||||
SkASSERT(!(m.getType() & ~SkMatrix::kTranslate_Mask));
|
||||
fType = Type::kIdentity;
|
||||
}
|
||||
}
|
||||
Sk2f operator()(const Sk2f& vector) const {
|
||||
switch (fType) {
|
||||
case Type::kIdentity:
|
||||
return vector;
|
||||
case Type::kScale:
|
||||
return fScaleXY * vector;
|
||||
case Type::kAffine:
|
||||
return fScaleXSkewY * vector[0] + fSkewXScaleY * vector[1];
|
||||
}
|
||||
SkUNREACHABLE;
|
||||
}
|
||||
Sk4f operator()(const Sk4f& vectors) const {
|
||||
switch (fType) {
|
||||
case Type::kIdentity:
|
||||
return vectors;
|
||||
case Type::kScale:
|
||||
return vectors * fScaleXYXY;
|
||||
case Type::kAffine:
|
||||
return fScaleXYXY * vectors + fSkewXYXY * SkNx_shuffle<1,0,3,2>(vectors);
|
||||
}
|
||||
SkUNREACHABLE;
|
||||
}
|
||||
private:
|
||||
enum class Type { kIdentity, kScale, kAffine } fType;
|
||||
union { Sk2f fScaleXY, fScaleXSkewY; };
|
||||
Sk2f fSkewXScaleY;
|
||||
Sk4f fScaleXYXY;
|
||||
Sk4f fSkewXYXY;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
101
src/gpu/tessellate/GrWangsFormula.h
Normal file
101
src/gpu/tessellate/GrWangsFormula.h
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef GrWangsFormula_DEFINED
|
||||
#define GrWangsFormula_DEFINED
|
||||
|
||||
#include "include/core/SkPoint.h"
|
||||
#include "include/private/SkNx.h"
|
||||
#include "src/gpu/tessellate/GrVectorXform.h"
|
||||
|
||||
// Wang's formulas for cubics and quadratics (1985) give us the minimum number of evenly spaced (in
|
||||
// the parametric sense) line segments that a curve must be chopped into in order to guarantee all
|
||||
// lines stay within a distance of "1/intolerance" pixels from the true curve.
|
||||
namespace GrWangsFormula {
|
||||
|
||||
SK_ALWAYS_INLINE static float length(const Sk2f& n) {
|
||||
Sk2f nn = n*n;
|
||||
return std::sqrt(nn[0] + nn[1]);
|
||||
}
|
||||
|
||||
// Returns the minimum number of evenly spaced (in the parametric sense) line segments that the
|
||||
// quadratic must be chopped into in order to guarantee all lines stay within a distance of
|
||||
// "1/intolerance" pixels from the true curve.
|
||||
SK_ALWAYS_INLINE static float quadratic(float intolerance, const SkPoint pts[]) {
|
||||
Sk2f p0 = Sk2f::Load(pts);
|
||||
Sk2f p1 = Sk2f::Load(pts + 1);
|
||||
Sk2f p2 = Sk2f::Load(pts + 2);
|
||||
float k = intolerance * .25f;
|
||||
return SkScalarSqrt(k * length(p0 - p1*2 + p2));
|
||||
}
|
||||
|
||||
// Returns the minimum number of evenly spaced (in the parametric sense) line segments that the
|
||||
// cubic must be chopped into in order to guarantee all lines stay within a distance of
|
||||
// "1/intolerance" pixels from the true curve.
|
||||
SK_ALWAYS_INLINE static float cubic(float intolerance, const SkPoint pts[]) {
|
||||
Sk2f p0 = Sk2f::Load(pts);
|
||||
Sk2f p1 = Sk2f::Load(pts + 1);
|
||||
Sk2f p2 = Sk2f::Load(pts + 2);
|
||||
Sk2f p3 = Sk2f::Load(pts + 3);
|
||||
float k = intolerance * .75f;
|
||||
return SkScalarSqrt(k * length(Sk2f::Max((p0 - p1*2 + p2).abs(),
|
||||
(p1 - p2*2 + p3).abs())));
|
||||
}
|
||||
|
||||
// Returns the log2 of the provided value, were that value to be rounded up to the next power of 2.
|
||||
// Returns 0 if value <= 0:
|
||||
// Never returns a negative number, even if value is NaN.
|
||||
//
|
||||
// nextlog2((-inf..1]) -> 0
|
||||
// nextlog2((1..2]) -> 1
|
||||
// nextlog2((2..4]) -> 2
|
||||
// nextlog2((4..8]) -> 3
|
||||
// ...
|
||||
SK_ALWAYS_INLINE static int nextlog2(float value) {
|
||||
uint32_t bits;
|
||||
memcpy(&bits, &value, 4);
|
||||
bits += (1u << 23) - 1u; // Increment the exponent for non-powers-of-2.
|
||||
int exp = ((int32_t)bits >> 23) - 127;
|
||||
return exp & ~(exp >> 31); // Return 0 for negative or denormalized floats, and exponents < 0.
|
||||
}
|
||||
|
||||
// Returns the minimum log2 number of evenly spaced (in the parametric sense) line segments that the
|
||||
// transformed quadratic must be chopped into in order to guarantee all lines stay within a distance
|
||||
// of "1/intolerance" pixels from the true curve.
|
||||
SK_ALWAYS_INLINE static int quadratic_log2(float intolerance, const SkPoint pts[],
|
||||
const GrVectorXform& vectorXform = GrVectorXform()) {
|
||||
Sk2f p0 = Sk2f::Load(pts);
|
||||
Sk2f p1 = Sk2f::Load(pts + 1);
|
||||
Sk2f p2 = Sk2f::Load(pts + 2);
|
||||
Sk2f v = p0 + p1*-2 + p2;
|
||||
v = vectorXform(v);
|
||||
Sk2f vv = v*v;
|
||||
float k = intolerance * .25f;
|
||||
float f = k*k * (vv[0] + vv[1]);
|
||||
return (nextlog2(f) + 3) >> 2; // ceil(log2(sqrt(sqrt(f))))
|
||||
}
|
||||
|
||||
// Returns the minimum log2 number of evenly spaced (in the parametric sense) line segments that the
|
||||
// transformed cubic must be chopped into in order to guarantee all lines stay within a distance of
|
||||
// "1/intolerance" pixels from the true curve.
|
||||
SK_ALWAYS_INLINE static int cubic_log2(float intolerance, const SkPoint pts[],
|
||||
const GrVectorXform& vectorXform = GrVectorXform()) {
|
||||
Sk4f p01 = Sk4f::Load(pts);
|
||||
Sk4f p12 = Sk4f::Load(pts + 1);
|
||||
Sk4f p23 = Sk4f::Load(pts + 2);
|
||||
Sk4f v = p01 + p12*-2 + p23;
|
||||
v = vectorXform(v);
|
||||
Sk4f vv = v*v;
|
||||
vv = Sk4f::Max(vv, SkNx_shuffle<2,3,0,1>(vv));
|
||||
float k = intolerance * .75f;
|
||||
float f = k*k * (vv[0] + vv[1]);
|
||||
return (nextlog2(f) + 3) >> 2; // ceil(log2(sqrt(sqrt(f))))
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
278
tests/WangsFormulaTest.cpp
Normal file
278
tests/WangsFormulaTest.cpp
Normal file
@ -0,0 +1,278 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "include/utils/SkRandom.h"
|
||||
#include "src/core/SkGeometry.h"
|
||||
#include "src/gpu/tessellate/GrWangsFormula.h"
|
||||
#include "tests/Test.h"
|
||||
|
||||
constexpr static int kIntolerance = 4; // 1/4 pixel max error.
|
||||
|
||||
const SkPoint kSerp[4] = {
|
||||
{285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}};
|
||||
|
||||
const SkPoint kLoop[4] = {
|
||||
{635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}};
|
||||
|
||||
const SkPoint kQuad[4] = {
|
||||
{460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}};
|
||||
|
||||
DEF_TEST(WangsFormula_nextlog2, r) {
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(-std::numeric_limits<float>::infinity()) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(-std::numeric_limits<float>::max()) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(-1000.0f) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(-0.1f) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(-std::numeric_limits<float>::min()) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(-std::numeric_limits<float>::denorm_min()) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(0.0f) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(std::numeric_limits<float>::denorm_min()) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(std::numeric_limits<float>::min()) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(0.1f) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(1.0f) == 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(1.1f) == 1);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(2.0f) == 1);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(2.1f) == 2);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(3.0f) == 2);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(3.1f) == 2);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(4.0f) == 2);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(4.1f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(5.0f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(5.1f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(6.0f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(6.1f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(7.0f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(7.1f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(8.0f) == 3);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(8.1f) == 4);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(9.0f) == 4);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(9.1f) == 4);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(std::numeric_limits<float>::max()) == 128);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(std::numeric_limits<float>::infinity()) > 0);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(std::numeric_limits<float>::quiet_NaN()) >= 0);
|
||||
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
float pow2 = std::ldexp(1, i);
|
||||
float epsilon = std::ldexp(SK_ScalarNearlyZero, i);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(pow2) == i);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(pow2 + epsilon) == i + 1);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::nextlog2(pow2 - epsilon) == i);
|
||||
}
|
||||
}
|
||||
|
||||
void for_random_matrices(SkRandom* rand, std::function<void(const SkMatrix&)> f) {
|
||||
SkMatrix m;
|
||||
m.setIdentity();
|
||||
f(m);
|
||||
|
||||
for (int i = -10; i <= 30; ++i) {
|
||||
for (int j = -10; j <= 30; ++j) {
|
||||
m.setScaleX(std::ldexp(1 + rand->nextF(), i));
|
||||
m.setSkewX(0);
|
||||
m.setSkewY(0);
|
||||
m.setScaleY(std::ldexp(1 + rand->nextF(), j));
|
||||
f(m);
|
||||
|
||||
m.setScaleX(std::ldexp(1 + rand->nextF(), i));
|
||||
m.setSkewX(std::ldexp(1 + rand->nextF(), (j + i) / 2));
|
||||
m.setSkewY(std::ldexp(1 + rand->nextF(), (j + i) / 2));
|
||||
m.setScaleY(std::ldexp(1 + rand->nextF(), j));
|
||||
f(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void for_random_beziers(int numPoints, SkRandom* rand, std::function<void(const SkPoint[])> f) {
|
||||
SkASSERT(numPoints <= 4);
|
||||
SkPoint pts[4];
|
||||
for (int i = -10; i <= 30; ++i) {
|
||||
for (int j = 0; j < numPoints; ++j) {
|
||||
pts[j].set(std::ldexp(1 + rand->nextF(), i), std::ldexp(1 + rand->nextF(), i));
|
||||
}
|
||||
f(pts);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the optimized "*_log2" versions return the same value as ceil(std::log2(f)).
|
||||
DEF_TEST(WangsFormula_log2, r) {
|
||||
// Constructs a cubic such that the 'length' term in wang's formula == term.
|
||||
//
|
||||
// f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
|
||||
// abs(p1 - p2*2 + p3))));
|
||||
auto setupCubicLengthTerm = [](int seed, SkPoint pts[], float term) {
|
||||
memset(pts, 0, sizeof(SkPoint) * 4);
|
||||
|
||||
SkPoint term2d = (seed & 1) ?
|
||||
SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
|
||||
seed >>= 1;
|
||||
|
||||
if (seed & 1) {
|
||||
term2d.fX = -term2d.fX;
|
||||
}
|
||||
seed >>= 1;
|
||||
|
||||
if (seed & 1) {
|
||||
std::swap(term2d.fX, term2d.fY);
|
||||
}
|
||||
seed >>= 1;
|
||||
|
||||
switch (seed % 4) {
|
||||
case 0:
|
||||
pts[0] = term2d;
|
||||
pts[3] = term2d * .75f;
|
||||
return;
|
||||
case 1:
|
||||
pts[1] = term2d * -.5f;
|
||||
return;
|
||||
case 2:
|
||||
pts[1] = term2d * -.5f;
|
||||
return;
|
||||
case 3:
|
||||
pts[3] = term2d;
|
||||
pts[0] = term2d * .75f;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Constructs a quadratic such that the 'length' term in wang's formula == term.
|
||||
//
|
||||
// f = sqrt(k * length(p0 - p1*2 + p2));
|
||||
auto setupQuadraticLengthTerm = [](int seed, SkPoint pts[], float term) {
|
||||
memset(pts, 0, sizeof(SkPoint) * 3);
|
||||
|
||||
SkPoint term2d = (seed & 1) ?
|
||||
SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
|
||||
seed >>= 1;
|
||||
|
||||
if (seed & 1) {
|
||||
term2d.fX = -term2d.fX;
|
||||
}
|
||||
seed >>= 1;
|
||||
|
||||
if (seed & 1) {
|
||||
std::swap(term2d.fX, term2d.fY);
|
||||
}
|
||||
seed >>= 1;
|
||||
|
||||
switch (seed % 3) {
|
||||
case 0:
|
||||
pts[0] = term2d;
|
||||
return;
|
||||
case 1:
|
||||
pts[1] = term2d * -.5f;
|
||||
return;
|
||||
case 2:
|
||||
pts[2] = term2d;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
for (int level = 0; level < 30; ++level) {
|
||||
float epsilon = std::ldexp(SK_ScalarNearlyZero, level * 2);
|
||||
SkPoint pts[4];
|
||||
|
||||
{
|
||||
// Test cubic boundaries.
|
||||
// f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
|
||||
// abs(p1 - p2*2 + p3))));
|
||||
constexpr static float k = (3 * 2) / (8 * (1.f/kIntolerance));
|
||||
float x = std::ldexp(1, level * 2) / k;
|
||||
setupCubicLengthTerm(level << 1, pts, x - epsilon);
|
||||
REPORTER_ASSERT(r,
|
||||
std::ceil(std::log2(GrWangsFormula::cubic(kIntolerance, pts))) == level);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level);
|
||||
setupCubicLengthTerm(level << 1, pts, x + epsilon);
|
||||
REPORTER_ASSERT(r,
|
||||
std::ceil(std::log2(GrWangsFormula::cubic(kIntolerance, pts))) == level + 1);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level + 1);
|
||||
}
|
||||
|
||||
{
|
||||
// Test quadratic boundaries.
|
||||
// f = std::sqrt(k * Length(p0 - p1*2 + p2));
|
||||
constexpr static float k = 2 / (8 * (1.f/kIntolerance));
|
||||
float x = std::ldexp(1, level * 2) / k;
|
||||
setupQuadraticLengthTerm(level << 1, pts, x - epsilon);
|
||||
REPORTER_ASSERT(r,
|
||||
std::ceil(std::log2(GrWangsFormula::quadratic(kIntolerance, pts))) == level);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level);
|
||||
setupQuadraticLengthTerm(level << 1, pts, x + epsilon);
|
||||
REPORTER_ASSERT(r,
|
||||
std::ceil(std::log2(GrWangsFormula::quadratic(kIntolerance, pts))) == level+1);
|
||||
REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level + 1);
|
||||
}
|
||||
}
|
||||
|
||||
auto check_cubic_log2 = [&](const SkPoint* pts) {
|
||||
float f = std::max(1.f, GrWangsFormula::cubic(kIntolerance, pts));
|
||||
int f_log2 = GrWangsFormula::cubic_log2(kIntolerance, pts);
|
||||
REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
|
||||
};
|
||||
|
||||
auto check_quadratic_log2 = [&](const SkPoint* pts) {
|
||||
float f = std::max(1.f, GrWangsFormula::quadratic(kIntolerance, pts));
|
||||
int f_log2 = GrWangsFormula::quadratic_log2(kIntolerance, pts);
|
||||
REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
|
||||
};
|
||||
|
||||
SkRandom rand;
|
||||
|
||||
for_random_matrices(&rand, [&](const SkMatrix& m) {
|
||||
SkPoint pts[4];
|
||||
m.mapPoints(pts, kSerp, 4);
|
||||
check_cubic_log2(pts);
|
||||
|
||||
m.mapPoints(pts, kLoop, 4);
|
||||
check_cubic_log2(pts);
|
||||
|
||||
m.mapPoints(pts, kQuad, 3);
|
||||
check_quadratic_log2(pts);
|
||||
});
|
||||
|
||||
for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
|
||||
check_cubic_log2(pts);
|
||||
});
|
||||
|
||||
for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
|
||||
check_quadratic_log2(pts);
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure using transformations gives the same result as pre-transforming all points.
|
||||
DEF_TEST(WangsFormula_vectorXforms, r) {
|
||||
auto check_cubic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m){
|
||||
SkPoint ptsXformed[4];
|
||||
m.mapPoints(ptsXformed, pts, 4);
|
||||
int expected = GrWangsFormula::cubic_log2(kIntolerance, ptsXformed);
|
||||
int actual = GrWangsFormula::cubic_log2(kIntolerance, pts, GrVectorXform(m));
|
||||
REPORTER_ASSERT(r, actual == expected);
|
||||
};
|
||||
|
||||
auto check_quadratic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m) {
|
||||
SkPoint ptsXformed[3];
|
||||
m.mapPoints(ptsXformed, pts, 3);
|
||||
int expected = GrWangsFormula::quadratic_log2(kIntolerance, ptsXformed);
|
||||
int actual = GrWangsFormula::quadratic_log2(kIntolerance, pts, GrVectorXform(m));
|
||||
REPORTER_ASSERT(r, actual == expected);
|
||||
};
|
||||
|
||||
SkRandom rand;
|
||||
|
||||
for_random_matrices(&rand, [&](const SkMatrix& m) {
|
||||
check_cubic_log2_with_transform(kSerp, m);
|
||||
check_cubic_log2_with_transform(kLoop, m);
|
||||
check_quadratic_log2_with_transform(kQuad, m);
|
||||
|
||||
for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
|
||||
check_cubic_log2_with_transform(pts, m);
|
||||
});
|
||||
|
||||
for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
|
||||
check_quadratic_log2_with_transform(pts, m);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user