Index: src/gpu/GrPathUtils.cpp |
diff --git a/src/gpu/GrPathUtils.cpp b/src/gpu/GrPathUtils.cpp |
index 81348ec239995a5a5645a1f553d8e1930b2dc869..b7039a9ac62a5834170307ea99efaaa8b380b567 100644 |
--- a/src/gpu/GrPathUtils.cpp |
+++ b/src/gpu/GrPathUtils.cpp |
@@ -187,8 +187,6 @@ int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, |
} |
void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) { |
- // can't make this static, no cons :( |
- SkMatrix UVpts; |
#ifndef SK_SCALAR_IS_FLOAT |
GrCrash("Expected scalar is float."); |
#endif |
@@ -197,18 +195,23 @@ void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) { |
// We know M * control_pts = [0 1/2 1] |
// [0 0 1] |
// [1 1 1] |
+ // And control_pts = [x0 x1 x2] |
+ // [y0 y1 y2] |
+ // [1 1 1 ] |
// We invert the control pt matrix and post concat to both sides to get M. |
- UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1, |
- 0, 0, SK_Scalar1, |
- SkScalarToPersp(SK_Scalar1), |
- SkScalarToPersp(SK_Scalar1), |
- SkScalarToPersp(SK_Scalar1)); |
- m.setAll(qPts[0].fX, qPts[1].fX, qPts[2].fX, |
- qPts[0].fY, qPts[1].fY, qPts[2].fY, |
- SkScalarToPersp(SK_Scalar1), |
- SkScalarToPersp(SK_Scalar1), |
- SkScalarToPersp(SK_Scalar1)); |
- if (!m.invert(&m)) { |
+ // Using the known form of the control point matrix and the result, we can |
+ // optimize and improve precision. |
+ |
+ double x0 = qPts[0].fX; |
+ double y0 = qPts[0].fY; |
+ double x1 = qPts[1].fX; |
+ double y1 = qPts[1].fY; |
+ double x2 = qPts[2].fX; |
+ double y2 = qPts[2].fY; |
+ double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2; |
+ |
+ if (!sk_float_isfinite(det) |
+ || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
// The quad is degenerate. Hopefully this is rare. Find the pts that are |
// farthest apart to compute a line (unless it is really a pt). |
SkScalar maxD = qPts[0].distanceToSqd(qPts[1]); |
@@ -247,7 +250,35 @@ void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) { |
fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
} |
} else { |
- m.postConcat(UVpts); |
+ double scale = 1.0/det; |
+ |
+ // order swapped around to minimize stalls |
mtklein
2013/12/03 20:52:28
Interesting! Does this actually generate signific
jvanverth1
2013/12/04 19:51:20
I looked into this, and it appears that on Linux,
|
+ double adjugate[9]; |
+ adjugate[6] = y0-y1; |
+ adjugate[7] = x1-x0; |
+ adjugate[8] = x0*y1-y0*x1; |
mtklein
2013/12/03 20:52:28
Just a nit. All the others have x*y except this y
|
+ |
+ adjugate[3] = y2-y0; |
+ adjugate[4] = x0-x2; |
+ adjugate[5] = x2*y0-x0*y2; |
+ |
+ adjugate[0] = y1-y2; |
+ adjugate[1] = x2-x1; |
+ adjugate[2] = x1*y2-x2*y1; |
+ |
+ // this performs the uv_pts*adjugate(control_pts) multiply, |
+ // then does the scale by 1/det afterwards to improve precision |
+ m[SkMatrix::kMSkewY] = (float)(adjugate[6]*scale); |
mtklein
2013/12/03 20:52:28
This is again a nit, but it'd help me read with an
|
+ m[SkMatrix::kMScaleY] = (float)(adjugate[7]*scale); |
+ m[SkMatrix::kMTransY] = (float)(adjugate[8]*scale); |
+ |
+ m[SkMatrix::kMScaleX] = (float)((0.5*adjugate[3] + adjugate[6])*scale); |
+ m[SkMatrix::kMSkewX] = (float)((0.5*adjugate[4] + adjugate[7])*scale); |
+ m[SkMatrix::kMTransX] = (float)((0.5*adjugate[5] + adjugate[8])*scale); |
+ |
+ m[SkMatrix::kMPersp0] = (float)((adjugate[0] + adjugate[3] + adjugate[6])*scale); |
+ m[SkMatrix::kMPersp1] = (float)((adjugate[1] + adjugate[4] + adjugate[7])*scale); |
+ m[SkMatrix::kMPersp2] = (float)((adjugate[2] + adjugate[5] + adjugate[8])*scale); |
// The matrix should not have perspective. |
SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f); |