static inline bool is_cubic_nearly_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3, const Sk2f& tan0, const Sk2f& tan1, Sk2f* c) { Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0); Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan1, p3); *c = (c1 + c2) * .5f; // Hopefully optimized out if not used? return ((c1 - c2).abs() <= 1).allTrue(); }
/** * Called on a background thread. Here we can only modify fBackPaths. */ void runAnimationTask(double t, double dt, int w, int h) override { const float tsec = static_cast<float>(t); this->INHERITED::runAnimationTask(t, 0.5 * dt, w, h); for (int i = 0; i < kNumPaths; ++i) { const Glyph& glyph = fGlyphs[i]; const SkMatrix& backMatrix = fBackMatrices[i]; const Sk2f matrix[3] = { Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()), Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()), Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY()) }; SkPath* backpath = &fBackPaths[i]; backpath->reset(); backpath->setFillType(SkPath::kEvenOdd_FillType); SkPath::RawIter iter(glyph.fPath); SkPath::Verb verb; SkPoint pts[4]; while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { switch (verb) { case SkPath::kMove_Verb: { SkPoint pt = fWaves.apply(tsec, matrix, pts[0]); backpath->moveTo(pt.x(), pt.y()); break; } case SkPath::kLine_Verb: { SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]); backpath->lineTo(endpt.x(), endpt.y()); break; } case SkPath::kQuad_Verb: { SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]); SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]); backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y()); break; } case SkPath::kClose_Verb: { backpath->close(); break; } case SkPath::kCubic_Verb: case SkPath::kConic_Verb: case SkPath::kDone_Verb: SK_ABORT("Unexpected path verb"); break; } } } }
void GrCCFillGeometry::conicTo(const SkPoint P[3], float w) { SkASSERT(fBuildingContour); SkASSERT(P[0] == fPoints.back()); Sk2f p0 = Sk2f::Load(P); Sk2f p1 = Sk2f::Load(P+1); Sk2f p2 = Sk2f::Load(P+2); Sk2f tan0 = p1 - p0; Sk2f tan1 = p2 - p1; if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) { // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't // necessary if we are only interested in a vector in the same *direction* as a given // tangent line. Since the denominator scales dx and dy uniformly, we can throw it out // completely after evaluating the derivative with the standard quotient rule. This leaves // us with a simpler quadratic function that we use to find the midtangent. float midT = find_midtangent(tan0, tan1, (w - 1) * (p2 - p0), (p2 - p0) - 2*w*(p1 - p0), w*(p1 - p0)); // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we // cull near-linear conics above. And while w=0 is flat, it's not a line and has valid // midtangents.) if (!(midT > 0 && midT < 1)) { // The conic is flat. Otherwise there would be a real midtangent inside T=0..1. this->appendLine(p0, p2); return; } // Chop the conic at midtangent to produce two monotonic segments. Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0); Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w; Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0); Sk4f midT4 = midT; Sk4f p3d01 = lerp(p3d0, p3d1, midT4); Sk4f p3d12 = lerp(p3d1, p3d2, midT4); Sk4f p3d012 = lerp(p3d01, p3d12, midT4); Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2]; Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt(); this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]); this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]); return; } this->appendMonotonicConic(p0, p1, p2, w); }
// Will the given round rect look good if we use HW derivatives? static bool can_use_hw_derivatives_with_coverage( const GrShaderCaps& shaderCaps, const SkMatrix& viewMatrix, const SkRRect& rrect) { if (!shaderCaps.shaderDerivativeSupport()) { return false; } Sk2f x = Sk2f(viewMatrix.getScaleX(), viewMatrix.getSkewX()); Sk2f y = Sk2f(viewMatrix.getSkewY(), viewMatrix.getScaleY()); Sk2f devScale = (x*x + y*y).sqrt(); switch (rrect.getType()) { case SkRRect::kEmpty_Type: case SkRRect::kRect_Type: return true; case SkRRect::kOval_Type: case SkRRect::kSimple_Type: return can_use_hw_derivatives_with_coverage(devScale, rrect.getSimpleRadii()); case SkRRect::kNinePatch_Type: { Sk2f r0 = Sk2f::Load(SkRRectPriv::GetRadiiArray(rrect)); Sk2f r1 = Sk2f::Load(SkRRectPriv::GetRadiiArray(rrect) + 2); Sk2f minRadii = Sk2f::Min(r0, r1); Sk2f maxRadii = Sk2f::Max(r0, r1); return can_use_hw_derivatives_with_coverage(devScale, Sk2f(minRadii[0], maxRadii[1])) && can_use_hw_derivatives_with_coverage(devScale, Sk2f(maxRadii[0], minRadii[1])); } case SkRRect::kComplex_Type: { for (int i = 0; i < 4; ++i) { auto corner = static_cast<SkRRect::Corner>(i); if (!can_use_hw_derivatives_with_coverage(devScale, rrect.radii(corner))) { return false; } } return true; } } SK_ABORT("Invalid round rect type."); return false; // Add this return to keep GCC happy. }
GrCCPathCache::MaskTransform::MaskTransform(const SkMatrix& m, SkIVector* shift) : fMatrix2x2{m.getScaleX(), m.getSkewX(), m.getSkewY(), m.getScaleY()} { SkASSERT(!m.hasPerspective()); Sk2f translate = Sk2f(m.getTranslateX(), m.getTranslateY()); Sk2f transFloor; #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK // On Android framework we pre-round view matrix translates to integers for better caching. transFloor = translate; #else transFloor = translate.floor(); (translate - transFloor).store(fSubpixelTranslate); #endif shift->set((int)transFloor[0], (int)transFloor[1]); SkASSERT((float)shift->fX == transFloor[0]); // Make sure transFloor had integer values. SkASSERT((float)shift->fY == transFloor[1]); }
void ValueTraits<ShapeValue>::Lerp(const ShapeValue& v0, const ShapeValue& v1, float t, ShapeValue* result) { SkASSERT(v0.fVertices.size() == v1.fVertices.size()); SkASSERT(v0.fClosed == v1.fClosed); result->fClosed = v0.fClosed; result->fVolatile = true; // interpolated values are volatile const auto t2f = Sk2f(t); result->fVertices.resize(v0.fVertices.size()); for (size_t i = 0; i < v0.fVertices.size(); ++i) { result->fVertices[i] = BezierVertex({ lerp_point(v0.fVertices[i].fInPoint , v1.fVertices[i].fInPoint , t2f), lerp_point(v0.fVertices[i].fOutPoint, v1.fVertices[i].fOutPoint, t2f), lerp_point(v0.fVertices[i].fVertex , v1.fVertices[i].fVertex , t2f) }); } }
bool GrCCFiller::prepareToDraw(GrOnFlushResourceProvider* onFlushRP) { using Verb = GrCCFillGeometry::Verb; SkASSERT(!fInstanceBuffer); SkASSERT(fBatches.back().fEndNonScissorIndices == // Call closeCurrentBatch(). fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled]); SkASSERT(fBatches.back().fEndScissorSubBatchIdx == fScissorSubBatches.count()); // Here we build a single instance buffer to share with every internal batch. // // CCPR processs 3 different types of primitives: triangles, quadratics, cubics. Each primitive // type is further divided into instances that require a scissor and those that don't. This // leaves us with 3*2 = 6 independent instance arrays to build for the GPU. // // Rather than place each instance array in its own GPU buffer, we allocate a single // megabuffer and lay them all out side-by-side. We can offset the "baseInstance" parameter in // our draw calls to direct the GPU to the applicable elements within a given array. // // We already know how big to make each of the 6 arrays from fTotalPrimitiveCounts, so layout is // straightforward. Start with triangles and quadratics. They both view the instance buffer as // an array of TriPointInstance[], so we can begin at zero and lay them out one after the other. fBaseInstances[0].fTriangles = 0; fBaseInstances[1].fTriangles = fBaseInstances[0].fTriangles + fTotalPrimitiveCounts[0].fTriangles; fBaseInstances[0].fQuadratics = fBaseInstances[1].fTriangles + fTotalPrimitiveCounts[1].fTriangles; fBaseInstances[1].fQuadratics = fBaseInstances[0].fQuadratics + fTotalPrimitiveCounts[0].fQuadratics; int triEndIdx = fBaseInstances[1].fQuadratics + fTotalPrimitiveCounts[1].fQuadratics; // Wound triangles and cubics both view the same instance buffer as an array of // QuadPointInstance[]. So, reinterpreting the instance data as QuadPointInstance[], we start // them on the first index that will not overwrite previous TriPointInstance data. int quadBaseIdx = GR_CT_DIV_ROUND_UP(triEndIdx * sizeof(TriPointInstance), sizeof(QuadPointInstance)); fBaseInstances[0].fWeightedTriangles = quadBaseIdx; fBaseInstances[1].fWeightedTriangles = fBaseInstances[0].fWeightedTriangles + fTotalPrimitiveCounts[0].fWeightedTriangles; fBaseInstances[0].fCubics = fBaseInstances[1].fWeightedTriangles + fTotalPrimitiveCounts[1].fWeightedTriangles; fBaseInstances[1].fCubics = fBaseInstances[0].fCubics + fTotalPrimitiveCounts[0].fCubics; fBaseInstances[0].fConics = fBaseInstances[1].fCubics + fTotalPrimitiveCounts[1].fCubics; fBaseInstances[1].fConics = fBaseInstances[0].fConics + fTotalPrimitiveCounts[0].fConics; int quadEndIdx = fBaseInstances[1].fConics + fTotalPrimitiveCounts[1].fConics; fInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType, quadEndIdx * sizeof(QuadPointInstance)); if (!fInstanceBuffer) { SkDebugf("WARNING: failed to allocate CCPR fill instance buffer.\n"); return false; } TriPointInstance* triPointInstanceData = static_cast<TriPointInstance*>(fInstanceBuffer->map()); QuadPointInstance* quadPointInstanceData = reinterpret_cast<QuadPointInstance*>(triPointInstanceData); SkASSERT(quadPointInstanceData); PathInfo* nextPathInfo = fPathInfos.begin(); Sk2f devToAtlasOffset; PrimitiveTallies instanceIndices[2] = {fBaseInstances[0], fBaseInstances[1]}; PrimitiveTallies* currIndices = nullptr; SkSTArray<256, int32_t, true> currFan; bool currFanIsTessellated = false; const SkTArray<SkPoint, true>& pts = fGeometry.points(); int ptsIdx = -1; int nextConicWeightIdx = 0; // Expand the ccpr verbs into GPU instance buffers. for (Verb verb : fGeometry.verbs()) { switch (verb) { case Verb::kBeginPath: SkASSERT(currFan.empty()); currIndices = &instanceIndices[(int)nextPathInfo->scissorTest()]; devToAtlasOffset = Sk2f(static_cast<float>(nextPathInfo->devToAtlasOffset().fX), static_cast<float>(nextPathInfo->devToAtlasOffset().fY)); currFanIsTessellated = nextPathInfo->hasFanTessellation(); if (currFanIsTessellated) { emit_tessellated_fan(nextPathInfo->fanTessellation(), nextPathInfo->fanTessellationCount(), devToAtlasOffset, triPointInstanceData, quadPointInstanceData, currIndices); } ++nextPathInfo; continue; case Verb::kBeginContour: SkASSERT(currFan.empty()); ++ptsIdx; if (!currFanIsTessellated) { currFan.push_back(ptsIdx); } continue; case Verb::kLineTo: ++ptsIdx; if (!currFanIsTessellated) { SkASSERT(!currFan.empty()); currFan.push_back(ptsIdx); } continue; case Verb::kMonotonicQuadraticTo: triPointInstanceData[currIndices->fQuadratics++].set(&pts[ptsIdx], devToAtlasOffset); ptsIdx += 2; if (!currFanIsTessellated) { SkASSERT(!currFan.empty()); currFan.push_back(ptsIdx); } continue; case Verb::kMonotonicCubicTo: quadPointInstanceData[currIndices->fCubics++].set(&pts[ptsIdx], devToAtlasOffset[0], devToAtlasOffset[1]); ptsIdx += 3; if (!currFanIsTessellated) { SkASSERT(!currFan.empty()); currFan.push_back(ptsIdx); } continue; case Verb::kMonotonicConicTo: quadPointInstanceData[currIndices->fConics++].setW( &pts[ptsIdx], devToAtlasOffset, fGeometry.getConicWeight(nextConicWeightIdx)); ptsIdx += 2; ++nextConicWeightIdx; if (!currFanIsTessellated) { SkASSERT(!currFan.empty()); currFan.push_back(ptsIdx); } continue; case Verb::kEndClosedContour: // endPt == startPt. if (!currFanIsTessellated) { SkASSERT(!currFan.empty()); currFan.pop_back(); } // fallthru. case Verb::kEndOpenContour: // endPt != startPt. SkASSERT(!currFanIsTessellated || currFan.empty()); if (!currFanIsTessellated && currFan.count() >= 3) { int fanSize = currFan.count(); // Reserve space for emit_recursive_fan. Technically this can grow to // fanSize + log3(fanSize), but we approximate with log2. currFan.push_back_n(SkNextLog2(fanSize)); SkDEBUGCODE(TriPointInstance* end =) emit_recursive_fan(pts, currFan, 0, fanSize, devToAtlasOffset, triPointInstanceData + currIndices->fTriangles); currIndices->fTriangles += fanSize - 2; SkASSERT(triPointInstanceData + currIndices->fTriangles == end); } currFan.reset(); continue; } }
void GrCCFillGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) { SkASSERT(fBuildingContour); SkASSERT(P[0] == fPoints.back()); // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small). // Flat curves can break the math below. if (are_collinear(P)) { Sk2f p0 = Sk2f::Load(P); Sk2f p3 = Sk2f::Load(P+3); this->appendLine(p0, p3); return; } Sk2f p0 = Sk2f::Load(P); Sk2f p1 = Sk2f::Load(P+1); Sk2f p2 = Sk2f::Load(P+2); Sk2f p3 = Sk2f::Load(P+3); // Also detect near-quadratics ahead of time. Sk2f tan0, tan1, c; get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1); if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) { this->appendQuadratics(p0, c, p3); return; } double tt[2], ss[2], D[4]; fCurrCubicType = SkClassifyCubic(P, tt, ss, D); SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1])); Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1])); ExcludedTerm skipTerm = (std::abs(D[2]) > std::abs(D[1])) ? ExcludedTerm::kQuadraticTerm : ExcludedTerm::kLinearTerm; Sk2f C0 = SkNx_fma(Sk2f(3), p1 - p2, p3 - p0); Sk2f C1 = (ExcludedTerm::kLinearTerm == skipTerm ? SkNx_fma(Sk2f(-2), p1, p0 + p2) : p1 - p0) * 3; Sk2f C0x1 = C0 * SkNx_shuffle<1,0>(C1); float Cdet = C0x1[0] - C0x1[1]; SkSTArray<4, float> chops; if (SkCubicType::kLoop != fCurrCubicType) { find_chops_around_inflection_points(inflectPad, t, s, C0, C1, skipTerm, Cdet, &chops); } else { find_chops_around_loop_intersection(loopIntersectPad, t, s, C0, C1, skipTerm, Cdet, &chops); } if (4 == chops.count() && chops[1] >= chops[2]) { // This just the means the KLM roots are so close that their paddings overlap. We will // approximate the entire middle section, but still have it chopped midway. For loops this // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at // least almost) a cusp and the chop makes sure we get a sharp point. Sk2f ts = t * SkNx_shuffle<1,0>(s); chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]); } #ifdef SK_DEBUG for (int i = 1; i < chops.count(); ++i) { SkASSERT(chops[i] >= chops[i - 1]); } #endif this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count()); }
// Finds where to chop a non-loop around its intersection point. The resulting cubic segments will // be chopped such that a box of radius 'padRadius', centered at any point along the curve segment, // is guaranteed to not cross the tangent lines at the intersection point (a.k.a lines L & M). // // 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be // drawn with quadratic splines instead of cubics. // // A loop intersection falls at two different T values, so this method takes Sk2f and computes the // padding for both in SIMD. static inline void find_chops_around_loop_intersection(float padRadius, Sk2f t2, Sk2f s2, const Sk2f& C0, const Sk2f& C1, ExcludedTerm skipTerm, float Cdet, SkSTArray<4, float>* chops) { SkASSERT(chops->empty()); SkASSERT(padRadius >= 0); padRadius /= std::abs(Cdet); // Scale this single value rather than all of C^-1 later on. // The parametric functions for distance from lines L & M are: // // l(T) = (T - Td)^2 * (T - Te) // m(T) = (T - Td) * (T - Te)^2 // // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware", // 4.3 Finding klmn: // // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf Sk2f T2 = t2/s2; // T2 is the double root of l(T). Sk2f T1 = SkNx_shuffle<1,0>(T2); // T1 is the other root of l(T). // Convert l(T), m(T) to power-basis form: // // | 1 1 | // |l(T) m(T)| = |T^3 T^2 T 1| * | l2 m2 | // | l1 m1 | // | l0 m0 | // // From here on we use Sk2f with "L" names, but the second lane will be for line M. Sk2f l2 = SkNx_fma(Sk2f(-2), T2, -T1); Sk2f l1 = T2 * SkNx_fma(Sk2f(2), T1, T2); Sk2f l0 = -T2*T2*T1; // The equation for line L can be found as follows: // // L = C^-1 * (l excluding skipTerm) // // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.) // We are only interested in the normal to L, so only need the upper 2x2 of C^-1. And rather // than divide by determinant(C) here, we have already performed this divide on padRadius. Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? l2 : l1; Sk2f Lx = -C0[1]*l2or1 + C1[1]; // l3 is always 1. Sk2f Ly = C0[0]*l2or1 - C1[0]; // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan // with of L. (See rationale in are_collinear.) Sk2f Lwidth = Lx.abs() + Ly.abs(); Sk2f pad = Lwidth * padRadius; // Is l(T=0) outside the padding around line L? Sk2f lT0 = l0; // l(T=0) = |0 0 0 1| dot |1 l2 l1 l0| = l0 Sk2f outsideT0 = lT0.abs() - pad; // Is l(T=1) outside the padding around line L? Sk2f lT1 = (Sk2f(1) + l2 + l1 + l0).abs(); // l(T=1) = |1 1 1 1| dot |1 l2 l1 l0| Sk2f outsideT1 = lT1.abs() - pad; // Values for solving the cubic. Sk2f p, q, qqq, discr, numRoots, D; bool hasDiscr = false; // Values for calculating one root (rarely needed). Sk2f R, QQ; bool hasOneRootVals = false; // Values for calculating three roots. Sk2f P, cosTheta3; bool hasThreeRootVals = false; // Solve for the T values where l(T) = +pad and m(T) = -pad. for (int i = 0; i < 2; ++i) { float T = T2[i]; // T is the point we are chopping around. if ((T < 0 && outsideT0[i] >= 0) || (T > 1 && outsideT1[i] >= 0)) { // The padding around T is completely out of range. No point solving for it. continue; } if (!hasDiscr) { p = Sk2f(+.5f, -.5f) * pad; q = (1.f/3) * (T2 - T1); qqq = q*q*q; discr = qqq*p*2 + p*p; numRoots = (discr < 0).thenElse(3, 1); D = T2 - q; hasDiscr = true; } if (1 == numRoots[i]) { if (!hasOneRootVals) { Sk2f r = qqq + p; Sk2f s = r.abs() + discr.sqrt(); R = (r > 0).thenElse(-s, s); QQ = q*q; hasOneRootVals = true; } float A = cbrtf(R[i]); float B = A != 0 ? QQ[i]/A : 0; // When there is only one root, ine L chops from root..1, line M chops from 0..root. if (1 == i) { chops->push_back(0); } chops->push_back(A + B + D[i]); if (0 == i) { chops->push_back(1); } continue; } if (!hasThreeRootVals) { P = q.abs() * -2; cosTheta3 = (q >= 0).thenElse(1, -1) + p / qqq.abs(); hasThreeRootVals = true; } static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3; float theta = std::acos(cosTheta3[i]) * (1.f/3); float roots[3] = {P[i] * std::cos(theta) + D[i], P[i] * std::cos(theta + k2PiOver3) + D[i], P[i] * std::cos(theta - k2PiOver3) + D[i]}; // Sort the three roots. swap_if_greater(roots[0], roots[1]); swap_if_greater(roots[1], roots[2]); swap_if_greater(roots[0], roots[1]); // Line L chops around the first 2 roots, line M chops around the second 2. chops->push_back_n(2, &roots[i]); } }
// Finds where to chop a non-loop around its inflection points. The resulting cubic segments will be // chopped such that a box of radius 'padRadius', centered at any point along the curve segment, is // guaranteed to not cross the tangent lines at the inflection points (a.k.a lines L & M). // // 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be // drawn with flat lines instead of cubics. // // A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding // for both in SIMD. static inline void find_chops_around_inflection_points(float padRadius, Sk2f tl, Sk2f sl, const Sk2f& C0, const Sk2f& C1, ExcludedTerm skipTerm, float Cdet, SkSTArray<4, float>* chops) { SkASSERT(chops->empty()); SkASSERT(padRadius >= 0); padRadius /= std::abs(Cdet); // Scale this single value rather than all of C^-1 later on. // The homogeneous parametric functions for distance from lines L & M are: // // l(t,s) = (t*sl - s*tl)^3 // m(t,s) = (t*sm - s*tm)^3 // // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware", // 4.3 Finding klmn: // // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf // // From here on we use Sk2f with "L" names, but the second lane will be for line M. tl = (sl > 0).thenElse(tl, -tl); // Tl=tl/sl is the triple root of l(t,s). Normalize so s >= 0. sl = sl.abs(); // Convert l(t,s), m(t,s) to power-basis form: // // | l3 m3 | // |l(t,s) m(t,s)| = |t^3 t^2*s t*s^2 s^3| * | l2 m2 | // | l1 m1 | // | l0 m0 | // Sk2f l3 = sl*sl*sl; Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? sl*sl*tl*-3 : sl*tl*tl*3; // The equation for line L can be found as follows: // // L = C^-1 * (l excluding skipTerm) // // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.) // We are only interested in the normal to L, so only need the upper 2x2 of C^-1. And rather // than divide by determinant(C) here, we have already performed this divide on padRadius. Sk2f Lx = C1[1]*l3 - C0[1]*l2or1; Sk2f Ly = -C1[0]*l3 + C0[0]*l2or1; // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan // with of L. (See rationale in are_collinear.) Sk2f Lwidth = Lx.abs() + Ly.abs(); Sk2f pad = Lwidth * padRadius; // Will T=(t + cbrt(pad))/s be greater than 0? No need to solve roots outside T=0..1. Sk2f insideLeftPad = pad + tl*tl*tl; // Will T=(t - cbrt(pad))/s be less than 1? No need to solve roots outside T=0..1. Sk2f tms = tl - sl; Sk2f insideRightPad = pad - tms*tms*tms; // Solve for the T values where abs(l(T)) = pad. if (insideLeftPad[0] > 0 && insideRightPad[0] > 0) { float padT = cbrtf(pad[0]); Sk2f pts = (tl[0] + Sk2f(-padT, +padT)) / sl[0]; pts.store(chops->push_back_n(2)); } // Solve for the T values where abs(m(T)) = pad. if (insideLeftPad[1] > 0 && insideRightPad[1] > 0) { float padT = cbrtf(pad[1]); Sk2f pts = (tl[1] + Sk2f(-padT, +padT)) / sl[1]; pts.store(chops->push_back_n(2)); } }