static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside, bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect, SkScalar strokeWidth, bool miterStroke) { SkRect devRect; viewMatrix.mapRect(&devRect, rect); SkVector devStrokeSize; if (strokeWidth > 0) { devStrokeSize.set(strokeWidth, strokeWidth); viewMatrix.mapVectors(&devStrokeSize, 1); devStrokeSize.setAbs(devStrokeSize); } else { devStrokeSize.set(SK_Scalar1, SK_Scalar1); } const SkScalar dx = devStrokeSize.fX; const SkScalar dy = devStrokeSize.fY; const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); *devOutside = devRect; *devOutsideAssist = devRect; *devInside = devRect; devOutside->outset(rx, ry); devInside->inset(rx, ry); // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we // make a degenerate inside rect to avoid double hitting. We will also jam all of the points // together when we render these rects. SkScalar spare; { SkScalar w = devRect.width() - dx; SkScalar h = devRect.height() - dy; spare = SkTMin(w, h); } *isDegenerate = spare <= 0; if (*isDegenerate) { devInside->fLeft = devInside->fRight = devRect.centerX(); devInside->fTop = devInside->fBottom = devRect.centerY(); } // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) // to draw the outside of the octagon. Because there are 8 vertices on the outer // edge, while vertex number of inner edge is 4, the same as miter-stroke. if (!miterStroke) { devOutside->inset(0, ry); devOutsideAssist->outset(0, ry); } }
static void draw_45(SkCanvas* canvas, SkRRect::Corner corner, SkScalar dist, const SkPoint& center) { SkRRect::Corner left = SkRRect::kUpperLeft_Corner, right = SkRRect::kUpperLeft_Corner; SkVector dir = { 0, 0 }; constexpr SkScalar kSize = 64.0f / SK_ScalarSqrt2; switch (corner) { case SkRRect::kUpperLeft_Corner: left = SkRRect::kUpperRight_Corner; right = SkRRect::kLowerLeft_Corner; dir.set(-SK_ScalarRoot2Over2, -SK_ScalarRoot2Over2); break; case SkRRect::kUpperRight_Corner: left = SkRRect::kUpperLeft_Corner; right = SkRRect::kLowerRight_Corner; dir.set(SK_ScalarRoot2Over2, -SK_ScalarRoot2Over2); break; case SkRRect::kLowerRight_Corner: left = SkRRect::kLowerLeft_Corner; right = SkRRect::kUpperRight_Corner; dir.set(SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); break; case SkRRect::kLowerLeft_Corner: left = SkRRect::kLowerRight_Corner; right = SkRRect::kUpperLeft_Corner; dir.set(-SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); break; default: SkFAIL("Invalid shape."); } SkRect r = SkRect::MakeWH(kSize, kSize); // UL, UR, LR, LL SkVector radii[4] = { { 0.0f, 0.0f }, { 0.0f, 0.0f }, { 0.0f, 0.0f }, { 0.0f, 0.0f } }; radii[left] = SkVector::Make(kSize, kSize); radii[right] = SkVector::Make(kSize, kSize); SkRRect rr; rr.setRectRadii( offset_center_to(r.roundOut(), center.fX + dist*dir.fX, center.fY + dist*dir.fY), radii); SkRRect occRR; dist -= 10.0f; occRR.setRectRadii( offset_center_to(r.roundOut(), center.fX + dist*dir.fX, center.fY + dist*dir.fY), radii); draw_rrect(canvas, rr, occRR); }
static void morphpoints(SkPoint dst[], const SkPoint src[], int count, SkPathMeasure& meas, const SkMatrix& matrix) { SkMatrix::MapXYProc proc = matrix.getMapXYProc(); for (int i = 0; i < count; i++) { SkPoint pos; SkVector tangent; proc(matrix, src[i].fX, src[i].fY, &pos); SkScalar sx = pos.fX; SkScalar sy = pos.fY; if (!meas.getPosTan(sx, &pos, &tangent)) { // set to 0 if the measure failed, so that we just set dst == pos tangent.set(0, 0); } /* This is the old way (that explains our approach but is way too slow SkMatrix matrix; SkPoint pt; pt.set(sx, sy); matrix.setSinCos(tangent.fY, tangent.fX); matrix.preTranslate(-sx, 0); matrix.postTranslate(pos.fX, pos.fY); matrix.mapPoints(&dst[i], &pt, 1); */ dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy), pos.fY + SkScalarMul(tangent.fX, sy)); } }
void TextArt::EnvelopeWarp::morphpoints(SkPoint dst[], const SkPoint src[], int count, SkPathMeasure& meas, const SkMatrix& matrix) { SkMatrix::MapXYProc proc = matrix.getMapXYProc(); for (int i = 0; i < count; i++) { SkPoint pos; SkVector tangent; SkPoint iSrc = src[i]; iSrc.fX = k1_ * iSrc.fX; proc(matrix, iSrc.fX, iSrc.fY, &pos); SkScalar sx = pos.fX; SkScalar sy = pos.fY; if (xWeightingMode_ & XWeightingMode_Linearly) { //in Linearly mode adjust Top text by TopLength/BottomLength relation if (isTop) { //move text below the Top skeleton sy -= boundsRect_.fTop; } } if (!meas.getPosTan(sx, &pos, &tangent)) { // set to 0 if the measure failed, so that we just set dst == pos tangent.set(0, 0); } /* This is the old way (that explains our approach but is way too slow SkMatrix matrix; SkPoint pt; pt.set(sx, sy); matrix.setSinCos(tangent.fY, tangent.fX); matrix.preTranslate(-sx, 0); matrix.postTranslate(pos.fX, pos.fY); matrix.mapPoints(&dst[i], &pt, 1); */ if (isNormalRotated_) { dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy), pos.fY + SkScalarMul(tangent.fX, sy)); } else { dst[i].set(pos.fX, pos.fY + sy); } } }
GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path, const SkVector* offset) { if (kHairlineAA_GrProcessorEdgeType == type) { return NULL; } if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) { return NULL; } if (path.countPoints() > kMaxEdges) { return NULL; } SkPoint pts[kMaxEdges]; SkScalar edges[3 * kMaxEdges]; SkPath::Direction dir; SkAssertResult(path.cheapComputeDirection(&dir)); SkVector t; if (NULL == offset) { t.set(0, 0); } else { t = *offset; } int count = path.getPoints(pts, kMaxEdges); int n = 0; for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) { if (pts[lastPt] != pts[i]) { SkVector v = pts[i] - pts[lastPt]; v.normalize(); if (SkPath::kCCW_Direction == dir) { edges[3 * n] = v.fY; edges[3 * n + 1] = -v.fX; } else { edges[3 * n] = -v.fY; edges[3 * n + 1] = v.fX; } SkPoint p = pts[i] + t; edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY); ++n; } } if (path.isInverseFillType()) { type = GrInvertProcessorEdgeType(type); } return Create(type, n, edges); }
void GrAARectRenderer::strokeAARect(GrDrawTarget* target, GrDrawState* drawState, GrColor color, const SkRect& rect, const SkMatrix& combinedMatrix, const SkRect& devRect, const SkStrokeRec& stroke) { SkVector devStrokeSize; SkScalar width = stroke.getWidth(); if (width > 0) { devStrokeSize.set(width, width); combinedMatrix.mapVectors(&devStrokeSize, 1); devStrokeSize.setAbs(devStrokeSize); } else { devStrokeSize.set(SK_Scalar1, SK_Scalar1); } const SkScalar dx = devStrokeSize.fX; const SkScalar dy = devStrokeSize.fY; const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); // Temporarily #if'ed out. We don't want to pass in the devRect but // right now it is computed in GrContext::apply_aa_to_rect and we don't // want to throw away the work #if 0 SkRect devRect; combinedMatrix.mapRect(&devRect, rect); #endif SkScalar spare; { SkScalar w = devRect.width() - dx; SkScalar h = devRect.height() - dy; spare = SkTMin(w, h); } SkRect devOutside(devRect); devOutside.outset(rx, ry); bool miterStroke = true; // For hairlines, make bevel and round joins appear the same as mitered ones. // small miter limit means right angles show bevel... if ((width > 0) && (stroke.getJoin() != SkPaint::kMiter_Join || stroke.getMiter() < SK_ScalarSqrt2)) { miterStroke = false; } if (spare <= 0 && miterStroke) { this->fillAARect(target, drawState, color, devOutside, SkMatrix::I(), devOutside); return; } SkRect devInside(devRect); devInside.inset(rx, ry); SkRect devOutsideAssist(devRect); // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) // to draw the outer of the rect. Because there are 8 vertices on the outer // edge, while vertex number of inner edge is 4, the same as miter-stroke. if (!miterStroke) { devOutside.inset(0, ry); devOutsideAssist.outset(0, ry); } this->geometryStrokeAARect(target, drawState, color, devOutside, devOutsideAssist, devInside, miterStroke); }
static void MiterJoiner(SkPath* outer, SkPath* inner, const SkVector& beforeUnitNormal, const SkPoint& pivot, const SkVector& afterUnitNormal, SkScalar radius, SkScalar invMiterLimit, bool prevIsLine, bool currIsLine) { // negate the dot since we're using normals instead of tangents SkScalar dotProd = SkPoint::DotProduct(beforeUnitNormal, afterUnitNormal); AngleType angleType = Dot2AngleType(dotProd); SkVector before = beforeUnitNormal; SkVector after = afterUnitNormal; SkVector mid; SkScalar sinHalfAngle; bool ccw; if (angleType == kNearlyLine_AngleType) return; if (angleType == kNearly180_AngleType) { currIsLine = false; goto DO_BLUNT; } ccw = !is_clockwise(before, after); if (ccw) { SkTSwap<SkPath*>(outer, inner); before.negate(); after.negate(); } /* Before we enter the world of square-roots and divides, check if we're trying to join an upright right angle (common case for stroking rectangles). If so, special case that (for speed an accuracy). Note: we only need to check one normal if dot==0 */ if (0 == dotProd && invMiterLimit <= kOneOverSqrt2) { mid.set(SkScalarMul(before.fX + after.fX, radius), SkScalarMul(before.fY + after.fY, radius)); goto DO_MITER; } /* midLength = radius / sinHalfAngle if (midLength > miterLimit * radius) abort if (radius / sinHalf > miterLimit * radius) abort if (1 / sinHalf > miterLimit) abort if (1 / miterLimit > sinHalf) abort My dotProd is opposite sign, since it is built from normals and not tangents hence 1 + dot instead of 1 - dot in the formula */ sinHalfAngle = SkScalarSqrt(SkScalarHalf(SK_Scalar1 + dotProd)); if (sinHalfAngle < invMiterLimit) { currIsLine = false; goto DO_BLUNT; } // choose the most accurate way to form the initial mid-vector if (angleType == kSharp_AngleType) { mid.set(after.fY - before.fY, before.fX - after.fX); if (ccw) mid.negate(); } else mid.set(before.fX + after.fX, before.fY + after.fY); mid.setLength(SkScalarDiv(radius, sinHalfAngle)); DO_MITER: if (prevIsLine) outer->setLastPt(pivot.fX + mid.fX, pivot.fY + mid.fY); else outer->lineTo(pivot.fX + mid.fX, pivot.fY + mid.fY); DO_BLUNT: after.scale(radius); if (!currIsLine) outer->lineTo(pivot.fX + after.fX, pivot.fY + after.fY); HandleInnerJoin(inner, pivot, after); }
GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path, const SkVector* offset) { if (kHairlineAA_GrProcessorEdgeType == type) { return nullptr; } if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) { return nullptr; } SkPathPriv::FirstDirection dir; // The only way this should fail is if the clip is effectively a infinitely thin line. In that // case nothing is inside the clip. It'd be nice to detect this at a higher level and either // skip the draw or omit the clip element. if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) { if (GrProcessorEdgeTypeIsInverseFill(type)) { return GrConstColorProcessor::Create(0xFFFFFFFF, GrConstColorProcessor::kModulateRGBA_InputMode); } return GrConstColorProcessor::Create(0, GrConstColorProcessor::kIgnore_InputMode); } SkVector t; if (nullptr == offset) { t.set(0, 0); } else { t = *offset; } SkScalar edges[3 * kMaxEdges]; SkPoint pts[4]; SkPath::Verb verb; SkPath::Iter iter(path, true); // SkPath considers itself convex so long as there is a convex contour within it, // regardless of any degenerate contours such as a string of moveTos before it. // Iterate here to consume any degenerate contours and only process the points // on the actual convex contour. int n = 0; while ((verb = iter.next(pts, true, true)) != SkPath::kDone_Verb) { switch (verb) { case SkPath::kMove_Verb: SkASSERT(n == 0); case SkPath::kClose_Verb: break; case SkPath::kLine_Verb: { if (n >= kMaxEdges) { return nullptr; } SkVector v = pts[1] - pts[0]; v.normalize(); if (SkPathPriv::kCCW_FirstDirection == dir) { edges[3 * n] = v.fY; edges[3 * n + 1] = -v.fX; } else { edges[3 * n] = -v.fY; edges[3 * n + 1] = v.fX; } SkPoint p = pts[1] + t; edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY); ++n; break; } default: return nullptr; } } if (path.isInverseFillType()) { type = GrInvertProcessorEdgeType(type); } return Create(type, n, edges); }
bool GrStrokePathRenderer::onDrawPath(const SkPath& origPath, const SkStrokeRec& stroke, GrDrawTarget* target, bool antiAlias) { if (origPath.isEmpty()) { return true; } SkScalar width = stroke.getWidth(); if (width <= 0) { return false; } // Get the join type SkPaint::Join join = stroke.getJoin(); SkScalar miterLimit = stroke.getMiter(); SkScalar sqMiterLimit = SkScalarMul(miterLimit, miterLimit); if ((join == SkPaint::kMiter_Join) && (miterLimit <= SK_Scalar1)) { // If the miter limit is small, treat it as a bevel join join = SkPaint::kBevel_Join; } const bool isMiter = (join == SkPaint::kMiter_Join); const bool isBevel = (join == SkPaint::kBevel_Join); SkScalar invMiterLimit = isMiter ? SK_Scalar1 / miterLimit : 0; SkScalar invMiterLimitSq = SkScalarMul(invMiterLimit, invMiterLimit); // Allocate vertices const int nbQuads = origPath.countPoints() + 1; // Could be "-1" if path is not closed const int extraVerts = isMiter || isBevel ? 1 : 0; const int maxVertexCount = nbQuads * (4 + extraVerts); const int maxIndexCount = nbQuads * (6 + extraVerts * 3); // Each extra vert adds a triangle target->drawState()->setDefaultVertexAttribs(); GrDrawTarget::AutoReleaseGeometry arg(target, maxVertexCount, maxIndexCount); if (!arg.succeeded()) { return false; } SkPoint* verts = reinterpret_cast<SkPoint*>(arg.vertices()); uint16_t* idxs = reinterpret_cast<uint16_t*>(arg.indices()); int vCount = 0, iCount = 0; // Transform the path into a list of triangles SkPath::Iter iter(origPath, false); SkPoint pts[4]; const SkScalar radius = SkScalarMul(width, 0.5f); SkPoint *firstPt = verts, *lastPt = NULL; SkVector firstDir, dir; firstDir.set(0, 0); dir.set(0, 0); bool isOpen = true; for(SkPath::Verb v = iter.next(pts); v != SkPath::kDone_Verb; v = iter.next(pts)) { switch(v) { case SkPath::kMove_Verb: // This will already be handled as pts[0] of the 1st line break; case SkPath::kClose_Verb: isOpen = (lastPt == NULL); break; case SkPath::kLine_Verb: { SkVector v0 = dir; dir = pts[1] - pts[0]; if (dir.setLength(radius)) { SkVector dirT; dirT.set(dir.fY, -dir.fX); // Get perpendicular direction SkPoint l1a = pts[0]+dirT, l1b = pts[1]+dirT, l2a = pts[0]-dirT, l2b = pts[1]-dirT; SkPoint miterPt[2]; bool useMiterPoint = false; int idx0(-1), idx1(-1); if (NULL == lastPt) { firstDir = dir; } else { SkVector v1 = dir; if (v0.normalize() && v1.normalize()) { SkScalar dotProd = v0.dot(v1); // No need for bevel or miter join if the angle // is either 0 or 180 degrees if (!SkScalarNearlyZero(dotProd + SK_Scalar1) && !SkScalarNearlyZero(dotProd - SK_Scalar1)) { bool ccw = !is_clockwise(v0, v1); int offset = ccw ? 1 : 0; idx0 = vCount-2+offset; idx1 = vCount+offset; const SkPoint* pt0 = &(lastPt[offset]); const SkPoint* pt1 = ccw ? &l2a : &l1a; switch(join) { case SkPaint::kMiter_Join: { // *Note : Logic is from MiterJoiner // FIXME : Special case if we have a right angle ? // if (SkScalarNearlyZero(dotProd)) {...} SkScalar sinHalfAngleSq = SkScalarHalf(SK_Scalar1 + dotProd); if (sinHalfAngleSq >= invMiterLimitSq) { // Find the miter point (or points if it is further // than the miter limit) const SkPoint pt2 = *pt0+v0, pt3 = *pt1+v1; if (intersection(*pt0, pt2, *pt1, pt3, miterPt[0]) != kNone_IntersectionType) { SkPoint miterPt0 = miterPt[0] - *pt0; SkPoint miterPt1 = miterPt[0] - *pt1; SkScalar sqDist0 = miterPt0.dot(miterPt0); SkScalar sqDist1 = miterPt1.dot(miterPt1); const SkScalar rSq = SkScalarDiv(SkScalarMul(radius, radius), sinHalfAngleSq); const SkScalar sqRLimit = SkScalarMul(sqMiterLimit, rSq); if (sqDist0 > sqRLimit || sqDist1 > sqRLimit) { if (sqDist1 > sqRLimit) { v1.setLength(SkScalarSqrt(sqRLimit)); miterPt[1] = *pt1+v1; } else { miterPt[1] = miterPt[0]; } if (sqDist0 > sqRLimit) { v0.setLength(SkScalarSqrt(sqRLimit)); miterPt[0] = *pt0+v0; } } else { miterPt[1] = miterPt[0]; } useMiterPoint = true; } } if (useMiterPoint && (miterPt[1] == miterPt[0])) { break; } } default: case SkPaint::kBevel_Join: { // Note : This currently causes some overdraw where both // lines initially intersect. We'd need to add // another line intersection check here if the // overdraw becomes an issue instead of using the // current point directly. // Add center point *verts++ = pts[0]; // Use current point directly // This idx is passed the current point so increment it ++idx1; // Add center triangle *idxs++ = idx0; *idxs++ = vCount; *idxs++ = idx1; vCount++; iCount += 3; } break; } } } } *verts++ = l1a; *verts++ = l2a; lastPt = verts; *verts++ = l1b; *verts++ = l2b; if (useMiterPoint && (idx0 >= 0) && (idx1 >= 0)) { firstPt[idx0] = miterPt[0]; firstPt[idx1] = miterPt[1]; } // 1st triangle *idxs++ = vCount+0; *idxs++ = vCount+2; *idxs++ = vCount+1; // 2nd triangle *idxs++ = vCount+1; *idxs++ = vCount+2; *idxs++ = vCount+3; vCount += 4; iCount += 6; } } break; case SkPath::kQuad_Verb: case SkPath::kCubic_Verb: SkDEBUGFAIL("Curves not supported!"); default: // Unhandled cases SkASSERT(false); } } if (isOpen) { // Add caps switch (stroke.getCap()) { case SkPaint::kSquare_Cap: firstPt[0] -= firstDir; firstPt[1] -= firstDir; lastPt [0] += dir; lastPt [1] += dir; break; case SkPaint::kRound_Cap: SkDEBUGFAIL("Round caps not supported!"); default: // No cap break; } } SkASSERT(vCount <= maxVertexCount); SkASSERT(iCount <= maxIndexCount); if (vCount > 0) { target->drawIndexed(kTriangles_GrPrimitiveType, 0, // start vertex 0, // start index vCount, iCount); } return true; }
void GrAARectRenderer::StrokeAARect(GrDrawTarget* target, const GrPipelineBuilder& pipelineBuilder, GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, const SkRect& devRect, const SkStrokeRec& stroke) { SkVector devStrokeSize; SkScalar width = stroke.getWidth(); if (width > 0) { devStrokeSize.set(width, width); viewMatrix.mapVectors(&devStrokeSize, 1); devStrokeSize.setAbs(devStrokeSize); } else { devStrokeSize.set(SK_Scalar1, SK_Scalar1); } const SkScalar dx = devStrokeSize.fX; const SkScalar dy = devStrokeSize.fY; const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); SkScalar spare; { SkScalar w = devRect.width() - dx; SkScalar h = devRect.height() - dy; spare = SkTMin(w, h); } SkRect devOutside(devRect); devOutside.outset(rx, ry); bool miterStroke = true; // For hairlines, make bevel and round joins appear the same as mitered ones. // small miter limit means right angles show bevel... if ((width > 0) && (stroke.getJoin() != SkPaint::kMiter_Join || stroke.getMiter() < SK_ScalarSqrt2)) { miterStroke = false; } if (spare <= 0 && miterStroke) { FillAARect(target, pipelineBuilder, color, viewMatrix, devOutside, devOutside); return; } SkRect devInside(devRect); devInside.inset(rx, ry); SkRect devOutsideAssist(devRect); // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) // to draw the outer of the rect. Because there are 8 vertices on the outer // edge, while vertex number of inner edge is 4, the same as miter-stroke. if (!miterStroke) { devOutside.inset(0, ry); devOutsideAssist.outset(0, ry); } GeometryStrokeAARect(target, pipelineBuilder, color, viewMatrix, devOutside, devOutsideAssist, devInside, miterStroke); }