michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrAAHairLinePathRenderer.h" michael@0: michael@0: #include "GrContext.h" michael@0: #include "GrDrawState.h" michael@0: #include "GrDrawTargetCaps.h" michael@0: #include "GrEffect.h" michael@0: #include "GrGpu.h" michael@0: #include "GrIndexBuffer.h" michael@0: #include "GrPathUtils.h" michael@0: #include "GrTBackendEffectFactory.h" michael@0: #include "SkGeometry.h" michael@0: #include "SkStroke.h" michael@0: #include "SkTemplates.h" michael@0: michael@0: #include "effects/GrBezierEffect.h" michael@0: michael@0: namespace { michael@0: // quadratics are rendered as 5-sided polys in order to bound the michael@0: // AA stroke around the center-curve. See comments in push_quad_index_buffer and michael@0: // bloat_quad. Quadratics and conics share an index buffer michael@0: static const int kVertsPerQuad = 5; michael@0: static const int kIdxsPerQuad = 9; michael@0: michael@0: // lines are rendered as: michael@0: // *______________* michael@0: // |\ -_______ /| michael@0: // | \ \ / | michael@0: // | *--------* | michael@0: // | / ______/ \ | michael@0: // */_-__________\* michael@0: // For: 6 vertices and 18 indices (for 6 triangles) michael@0: static const int kVertsPerLineSeg = 6; michael@0: static const int kIdxsPerLineSeg = 18; michael@0: michael@0: static const int kNumQuadsInIdxBuffer = 256; michael@0: static const size_t kQuadIdxSBufize = kIdxsPerQuad * michael@0: sizeof(uint16_t) * michael@0: kNumQuadsInIdxBuffer; michael@0: michael@0: static const int kNumLineSegsInIdxBuffer = 256; michael@0: static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg * michael@0: sizeof(uint16_t) * michael@0: kNumLineSegsInIdxBuffer; michael@0: michael@0: static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) { michael@0: uint16_t* data = (uint16_t*) qIdxBuffer->lock(); michael@0: bool tempData = NULL == data; michael@0: if (tempData) { michael@0: data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad); michael@0: } michael@0: for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) { michael@0: michael@0: // Each quadratic is rendered as a five sided polygon. This poly bounds michael@0: // the quadratic's bounding triangle but has been expanded so that the michael@0: // 1-pixel wide area around the curve is inside the poly. michael@0: // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 michael@0: // that is rendered would look like this: michael@0: // b0 michael@0: // b michael@0: // michael@0: // a0 c0 michael@0: // a c michael@0: // a1 c1 michael@0: // Each is drawn as three triangles specified by these 9 indices: michael@0: int baseIdx = i * kIdxsPerQuad; michael@0: uint16_t baseVert = (uint16_t)(i * kVertsPerQuad); michael@0: data[0 + baseIdx] = baseVert + 0; // a0 michael@0: data[1 + baseIdx] = baseVert + 1; // a1 michael@0: data[2 + baseIdx] = baseVert + 2; // b0 michael@0: data[3 + baseIdx] = baseVert + 2; // b0 michael@0: data[4 + baseIdx] = baseVert + 4; // c1 michael@0: data[5 + baseIdx] = baseVert + 3; // c0 michael@0: data[6 + baseIdx] = baseVert + 1; // a1 michael@0: data[7 + baseIdx] = baseVert + 4; // c1 michael@0: data[8 + baseIdx] = baseVert + 2; // b0 michael@0: } michael@0: if (tempData) { michael@0: bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize); michael@0: delete[] data; michael@0: return ret; michael@0: } else { michael@0: qIdxBuffer->unlock(); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) { michael@0: uint16_t* data = (uint16_t*) lIdxBuffer->lock(); michael@0: bool tempData = NULL == data; michael@0: if (tempData) { michael@0: data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg); michael@0: } michael@0: for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) { michael@0: // Each line segment is rendered as two quads and two triangles. michael@0: // p0 and p1 have alpha = 1 while all other points have alpha = 0. michael@0: // The four external points are offset 1 pixel perpendicular to the michael@0: // line and half a pixel parallel to the line. michael@0: // michael@0: // p4 p5 michael@0: // p0 p1 michael@0: // p2 p3 michael@0: // michael@0: // Each is drawn as six triangles specified by these 18 indices: michael@0: int baseIdx = i * kIdxsPerLineSeg; michael@0: uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg); michael@0: data[0 + baseIdx] = baseVert + 0; michael@0: data[1 + baseIdx] = baseVert + 1; michael@0: data[2 + baseIdx] = baseVert + 3; michael@0: michael@0: data[3 + baseIdx] = baseVert + 0; michael@0: data[4 + baseIdx] = baseVert + 3; michael@0: data[5 + baseIdx] = baseVert + 2; michael@0: michael@0: data[6 + baseIdx] = baseVert + 0; michael@0: data[7 + baseIdx] = baseVert + 4; michael@0: data[8 + baseIdx] = baseVert + 5; michael@0: michael@0: data[9 + baseIdx] = baseVert + 0; michael@0: data[10+ baseIdx] = baseVert + 5; michael@0: data[11+ baseIdx] = baseVert + 1; michael@0: michael@0: data[12 + baseIdx] = baseVert + 0; michael@0: data[13 + baseIdx] = baseVert + 2; michael@0: data[14 + baseIdx] = baseVert + 4; michael@0: michael@0: data[15 + baseIdx] = baseVert + 1; michael@0: data[16 + baseIdx] = baseVert + 5; michael@0: data[17 + baseIdx] = baseVert + 3; michael@0: } michael@0: if (tempData) { michael@0: bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize); michael@0: delete[] data; michael@0: return ret; michael@0: } else { michael@0: lIdxBuffer->unlock(); michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) { michael@0: GrGpu* gpu = context->getGpu(); michael@0: GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); michael@0: SkAutoTUnref qIdxBuffer(qIdxBuf); michael@0: if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) { michael@0: return NULL; michael@0: } michael@0: GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false); michael@0: SkAutoTUnref lIdxBuffer(lIdxBuf); michael@0: if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) { michael@0: return NULL; michael@0: } michael@0: return SkNEW_ARGS(GrAAHairLinePathRenderer, michael@0: (context, lIdxBuf, qIdxBuf)); michael@0: } michael@0: michael@0: GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( michael@0: const GrContext* context, michael@0: const GrIndexBuffer* linesIndexBuffer, michael@0: const GrIndexBuffer* quadsIndexBuffer) { michael@0: fLinesIndexBuffer = linesIndexBuffer; michael@0: linesIndexBuffer->ref(); michael@0: fQuadsIndexBuffer = quadsIndexBuffer; michael@0: quadsIndexBuffer->ref(); michael@0: } michael@0: michael@0: GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { michael@0: fLinesIndexBuffer->unref(); michael@0: fQuadsIndexBuffer->unref(); michael@0: } michael@0: michael@0: namespace { michael@0: michael@0: #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> michael@0: michael@0: // Takes 178th time of logf on Z600 / VC2010 michael@0: int get_float_exp(float x) { michael@0: GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); michael@0: #ifdef SK_DEBUG michael@0: static bool tested; michael@0: if (!tested) { michael@0: tested = true; michael@0: SkASSERT(get_float_exp(0.25f) == -2); michael@0: SkASSERT(get_float_exp(0.3f) == -2); michael@0: SkASSERT(get_float_exp(0.5f) == -1); michael@0: SkASSERT(get_float_exp(1.f) == 0); michael@0: SkASSERT(get_float_exp(2.f) == 1); michael@0: SkASSERT(get_float_exp(2.5f) == 1); michael@0: SkASSERT(get_float_exp(8.f) == 3); michael@0: SkASSERT(get_float_exp(100.f) == 6); michael@0: SkASSERT(get_float_exp(1000.f) == 9); michael@0: SkASSERT(get_float_exp(1024.f) == 10); michael@0: SkASSERT(get_float_exp(3000000.f) == 21); michael@0: } michael@0: #endif michael@0: const int* iptr = (const int*)&x; michael@0: return (((*iptr) & 0x7f800000) >> 23) - 127; michael@0: } michael@0: michael@0: // Uses the max curvature function for quads to estimate michael@0: // where to chop the conic. If the max curvature is not michael@0: // found along the curve segment it will return 1 and michael@0: // dst[0] is the original conic. If it returns 2 the dst[0] michael@0: // and dst[1] are the two new conics. michael@0: int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { michael@0: SkScalar t = SkFindQuadMaxCurvature(src); michael@0: if (t == 0) { michael@0: if (dst) { michael@0: dst[0].set(src, weight); michael@0: } michael@0: return 1; michael@0: } else { michael@0: if (dst) { michael@0: SkConic conic; michael@0: conic.set(src, weight); michael@0: conic.chopAt(t, dst); michael@0: } michael@0: return 2; michael@0: } michael@0: } michael@0: michael@0: // Calls split_conic on the entire conic and then once more on each subsection. michael@0: // Most cases will result in either 1 conic (chop point is not within t range) michael@0: // or 3 points (split once and then one subsection is split again). michael@0: int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { michael@0: SkConic dstTemp[2]; michael@0: int conicCnt = split_conic(src, dstTemp, weight); michael@0: if (2 == conicCnt) { michael@0: int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); michael@0: conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); michael@0: } else { michael@0: dst[0] = dstTemp[0]; michael@0: } michael@0: return conicCnt; michael@0: } michael@0: michael@0: // returns 0 if quad/conic is degen or close to it michael@0: // in this case approx the path with lines michael@0: // otherwise returns 1 michael@0: int is_degen_quad_or_conic(const SkPoint p[3]) { michael@0: static const SkScalar gDegenerateToLineTol = SK_Scalar1; michael@0: static const SkScalar gDegenerateToLineTolSqd = michael@0: SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); michael@0: michael@0: if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || michael@0: p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { michael@0: return 1; michael@0: } michael@0: michael@0: SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); michael@0: if (dsqd < gDegenerateToLineTolSqd) { michael@0: return 1; michael@0: } michael@0: michael@0: if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: // we subdivide the quads to avoid huge overfill michael@0: // if it returns -1 then should be drawn as lines michael@0: int num_quad_subdivs(const SkPoint p[3]) { michael@0: static const SkScalar gDegenerateToLineTol = SK_Scalar1; michael@0: static const SkScalar gDegenerateToLineTolSqd = michael@0: SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); michael@0: michael@0: if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || michael@0: p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { michael@0: return -1; michael@0: } michael@0: michael@0: SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); michael@0: if (dsqd < gDegenerateToLineTolSqd) { michael@0: return -1; michael@0: } michael@0: michael@0: if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { michael@0: return -1; michael@0: } michael@0: michael@0: // tolerance of triangle height in pixels michael@0: // tuned on windows Quadro FX 380 / Z600 michael@0: // trade off of fill vs cpu time on verts michael@0: // maybe different when do this using gpu (geo or tess shaders) michael@0: static const SkScalar gSubdivTol = 175 * SK_Scalar1; michael@0: michael@0: if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) { michael@0: return 0; michael@0: } else { michael@0: static const int kMaxSub = 4; michael@0: // subdividing the quad reduces d by 4. so we want x = log4(d/tol) michael@0: // = log4(d*d/tol*tol)/2 michael@0: // = log2(d*d/tol*tol) michael@0: michael@0: // +1 since we're ignoring the mantissa contribution. michael@0: int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; michael@0: log = GrMin(GrMax(0, log), kMaxSub); michael@0: return log; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Generates the lines and quads to be rendered. Lines are always recorded in michael@0: * device space. We will do a device space bloat to account for the 1pixel michael@0: * thickness. michael@0: * Quads are recorded in device space unless m contains michael@0: * perspective, then in they are in src space. We do this because we will michael@0: * subdivide large quads to reduce over-fill. This subdivision has to be michael@0: * performed before applying the perspective matrix. michael@0: */ michael@0: int generate_lines_and_quads(const SkPath& path, michael@0: const SkMatrix& m, michael@0: const SkIRect& devClipBounds, michael@0: GrAAHairLinePathRenderer::PtArray* lines, michael@0: GrAAHairLinePathRenderer::PtArray* quads, michael@0: GrAAHairLinePathRenderer::PtArray* conics, michael@0: GrAAHairLinePathRenderer::IntArray* quadSubdivCnts, michael@0: GrAAHairLinePathRenderer::FloatArray* conicWeights) { michael@0: SkPath::Iter iter(path, false); michael@0: michael@0: int totalQuadCount = 0; michael@0: SkRect bounds; michael@0: SkIRect ibounds; michael@0: michael@0: bool persp = m.hasPerspective(); michael@0: michael@0: for (;;) { michael@0: GrPoint pathPts[4]; michael@0: GrPoint devPts[4]; michael@0: SkPath::Verb verb = iter.next(pathPts); michael@0: switch (verb) { michael@0: case SkPath::kConic_Verb: { michael@0: SkConic dst[4]; michael@0: // We chop the conics to create tighter clipping to hide error michael@0: // that appears near max curvature of very thin conics. Thin michael@0: // hyperbolas with high weight still show error. michael@0: int conicCnt = chop_conic(pathPts, dst, iter.conicWeight()); michael@0: for (int i = 0; i < conicCnt; ++i) { michael@0: SkPoint* chopPnts = dst[i].fPts; michael@0: m.mapPoints(devPts, chopPnts, 3); michael@0: bounds.setBounds(devPts, 3); michael@0: bounds.outset(SK_Scalar1, SK_Scalar1); michael@0: bounds.roundOut(&ibounds); michael@0: if (SkIRect::Intersects(devClipBounds, ibounds)) { michael@0: if (is_degen_quad_or_conic(devPts)) { michael@0: SkPoint* pts = lines->push_back_n(4); michael@0: pts[0] = devPts[0]; michael@0: pts[1] = devPts[1]; michael@0: pts[2] = devPts[1]; michael@0: pts[3] = devPts[2]; michael@0: } else { michael@0: // when in perspective keep conics in src space michael@0: SkPoint* cPts = persp ? chopPnts : devPts; michael@0: SkPoint* pts = conics->push_back_n(3); michael@0: pts[0] = cPts[0]; michael@0: pts[1] = cPts[1]; michael@0: pts[2] = cPts[2]; michael@0: conicWeights->push_back() = dst[i].fW; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: case SkPath::kMove_Verb: michael@0: break; michael@0: case SkPath::kLine_Verb: michael@0: m.mapPoints(devPts, pathPts, 2); michael@0: bounds.setBounds(devPts, 2); michael@0: bounds.outset(SK_Scalar1, SK_Scalar1); michael@0: bounds.roundOut(&ibounds); michael@0: if (SkIRect::Intersects(devClipBounds, ibounds)) { michael@0: SkPoint* pts = lines->push_back_n(2); michael@0: pts[0] = devPts[0]; michael@0: pts[1] = devPts[1]; michael@0: } michael@0: break; michael@0: case SkPath::kQuad_Verb: { michael@0: SkPoint choppedPts[5]; michael@0: // Chopping the quad helps when the quad is either degenerate or nearly degenerate. michael@0: // When it is degenerate it allows the approximation with lines to work since the michael@0: // chop point (if there is one) will be at the parabola's vertex. In the nearly michael@0: // degenerate the QuadUVMatrix computed for the points is almost singular which michael@0: // can cause rendering artifacts. michael@0: int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts); michael@0: for (int i = 0; i < n; ++i) { michael@0: SkPoint* quadPts = choppedPts + i * 2; michael@0: m.mapPoints(devPts, quadPts, 3); michael@0: bounds.setBounds(devPts, 3); michael@0: bounds.outset(SK_Scalar1, SK_Scalar1); michael@0: bounds.roundOut(&ibounds); michael@0: michael@0: if (SkIRect::Intersects(devClipBounds, ibounds)) { michael@0: int subdiv = num_quad_subdivs(devPts); michael@0: SkASSERT(subdiv >= -1); michael@0: if (-1 == subdiv) { michael@0: SkPoint* pts = lines->push_back_n(4); michael@0: pts[0] = devPts[0]; michael@0: pts[1] = devPts[1]; michael@0: pts[2] = devPts[1]; michael@0: pts[3] = devPts[2]; michael@0: } else { michael@0: // when in perspective keep quads in src space michael@0: SkPoint* qPts = persp ? quadPts : devPts; michael@0: SkPoint* pts = quads->push_back_n(3); michael@0: pts[0] = qPts[0]; michael@0: pts[1] = qPts[1]; michael@0: pts[2] = qPts[2]; michael@0: quadSubdivCnts->push_back() = subdiv; michael@0: totalQuadCount += 1 << subdiv; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: case SkPath::kCubic_Verb: michael@0: m.mapPoints(devPts, pathPts, 4); michael@0: bounds.setBounds(devPts, 4); michael@0: bounds.outset(SK_Scalar1, SK_Scalar1); michael@0: bounds.roundOut(&ibounds); michael@0: if (SkIRect::Intersects(devClipBounds, ibounds)) { michael@0: PREALLOC_PTARRAY(32) q; michael@0: // we don't need a direction if we aren't constraining the subdivision michael@0: static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction; michael@0: // We convert cubics to quadratics (for now). michael@0: // In perspective have to do conversion in src space. michael@0: if (persp) { michael@0: SkScalar tolScale = michael@0: GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, michael@0: path.getBounds()); michael@0: GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q); michael@0: } else { michael@0: GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q); michael@0: } michael@0: for (int i = 0; i < q.count(); i += 3) { michael@0: SkPoint* qInDevSpace; michael@0: // bounds has to be calculated in device space, but q is michael@0: // in src space when there is perspective. michael@0: if (persp) { michael@0: m.mapPoints(devPts, &q[i], 3); michael@0: bounds.setBounds(devPts, 3); michael@0: qInDevSpace = devPts; michael@0: } else { michael@0: bounds.setBounds(&q[i], 3); michael@0: qInDevSpace = &q[i]; michael@0: } michael@0: bounds.outset(SK_Scalar1, SK_Scalar1); michael@0: bounds.roundOut(&ibounds); michael@0: if (SkIRect::Intersects(devClipBounds, ibounds)) { michael@0: int subdiv = num_quad_subdivs(qInDevSpace); michael@0: SkASSERT(subdiv >= -1); michael@0: if (-1 == subdiv) { michael@0: SkPoint* pts = lines->push_back_n(4); michael@0: // lines should always be in device coords michael@0: pts[0] = qInDevSpace[0]; michael@0: pts[1] = qInDevSpace[1]; michael@0: pts[2] = qInDevSpace[1]; michael@0: pts[3] = qInDevSpace[2]; michael@0: } else { michael@0: SkPoint* pts = quads->push_back_n(3); michael@0: // q is already in src space when there is no michael@0: // perspective and dev coords otherwise. michael@0: pts[0] = q[0 + i]; michael@0: pts[1] = q[1 + i]; michael@0: pts[2] = q[2 + i]; michael@0: quadSubdivCnts->push_back() = subdiv; michael@0: totalQuadCount += 1 << subdiv; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: case SkPath::kClose_Verb: michael@0: break; michael@0: case SkPath::kDone_Verb: michael@0: return totalQuadCount; michael@0: } michael@0: } michael@0: } michael@0: michael@0: struct LineVertex { michael@0: GrPoint fPos; michael@0: GrColor fCoverage; michael@0: }; michael@0: michael@0: struct BezierVertex { michael@0: GrPoint fPos; michael@0: union { michael@0: struct { michael@0: SkScalar fK; michael@0: SkScalar fL; michael@0: SkScalar fM; michael@0: } fConic; michael@0: GrVec fQuadCoord; michael@0: struct { michael@0: SkScalar fBogus[4]; michael@0: }; michael@0: }; michael@0: }; michael@0: michael@0: GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(GrPoint)); michael@0: michael@0: void intersect_lines(const SkPoint& ptA, const SkVector& normA, michael@0: const SkPoint& ptB, const SkVector& normB, michael@0: SkPoint* result) { michael@0: michael@0: SkScalar lineAW = -normA.dot(ptA); michael@0: SkScalar lineBW = -normB.dot(ptB); michael@0: michael@0: SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - michael@0: SkScalarMul(normA.fY, normB.fX); michael@0: wInv = SkScalarInvert(wInv); michael@0: michael@0: result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); michael@0: result->fX = SkScalarMul(result->fX, wInv); michael@0: michael@0: result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); michael@0: result->fY = SkScalarMul(result->fY, wInv); michael@0: } michael@0: michael@0: void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kVertsPerQuad]) { michael@0: // this should be in the src space, not dev coords, when we have perspective michael@0: GrPathUtils::QuadUVMatrix DevToUV(qpts); michael@0: DevToUV.apply(verts); michael@0: } michael@0: michael@0: void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, michael@0: const SkMatrix* toSrc, BezierVertex verts[kVertsPerQuad], michael@0: SkRect* devBounds) { michael@0: SkASSERT(!toDevice == !toSrc); michael@0: // original quad is specified by tri a,b,c michael@0: SkPoint a = qpts[0]; michael@0: SkPoint b = qpts[1]; michael@0: SkPoint c = qpts[2]; michael@0: michael@0: if (toDevice) { michael@0: toDevice->mapPoints(&a, 1); michael@0: toDevice->mapPoints(&b, 1); michael@0: toDevice->mapPoints(&c, 1); michael@0: } michael@0: // make a new poly where we replace a and c by a 1-pixel wide edges orthog michael@0: // to edges ab and bc: michael@0: // michael@0: // before | after michael@0: // | b0 michael@0: // b | michael@0: // | michael@0: // | a0 c0 michael@0: // a c | a1 c1 michael@0: // michael@0: // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, michael@0: // respectively. michael@0: BezierVertex& a0 = verts[0]; michael@0: BezierVertex& a1 = verts[1]; michael@0: BezierVertex& b0 = verts[2]; michael@0: BezierVertex& c0 = verts[3]; michael@0: BezierVertex& c1 = verts[4]; michael@0: michael@0: SkVector ab = b; michael@0: ab -= a; michael@0: SkVector ac = c; michael@0: ac -= a; michael@0: SkVector cb = b; michael@0: cb -= c; michael@0: michael@0: // We should have already handled degenerates michael@0: SkASSERT(ab.length() > 0 && cb.length() > 0); michael@0: michael@0: ab.normalize(); michael@0: SkVector abN; michael@0: abN.setOrthog(ab, SkVector::kLeft_Side); michael@0: if (abN.dot(ac) > 0) { michael@0: abN.negate(); michael@0: } michael@0: michael@0: cb.normalize(); michael@0: SkVector cbN; michael@0: cbN.setOrthog(cb, SkVector::kLeft_Side); michael@0: if (cbN.dot(ac) < 0) { michael@0: cbN.negate(); michael@0: } michael@0: michael@0: a0.fPos = a; michael@0: a0.fPos += abN; michael@0: a1.fPos = a; michael@0: a1.fPos -= abN; michael@0: michael@0: c0.fPos = c; michael@0: c0.fPos += cbN; michael@0: c1.fPos = c; michael@0: c1.fPos -= cbN; michael@0: michael@0: intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); michael@0: devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); michael@0: michael@0: if (toSrc) { michael@0: toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); michael@0: } michael@0: } michael@0: michael@0: // Equations based off of Loop-Blinn Quadratic GPU Rendering michael@0: // Input Parametric: michael@0: // P(t) = (P0*(1-t)^2 + 2*w*P1*t*(1-t) + P2*t^2) / (1-t)^2 + 2*w*t*(1-t) + t^2) michael@0: // Output Implicit: michael@0: // f(x, y, w) = f(P) = K^2 - LM michael@0: // K = dot(k, P), L = dot(l, P), M = dot(m, P) michael@0: // k, l, m are calculated in function GrPathUtils::getConicKLM michael@0: void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad], michael@0: const SkScalar weight) { michael@0: SkScalar klm[9]; michael@0: michael@0: GrPathUtils::getConicKLM(p, weight, klm); michael@0: michael@0: for (int i = 0; i < kVertsPerQuad; ++i) { michael@0: const SkPoint pnt = verts[i].fPos; michael@0: verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2]; michael@0: verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5]; michael@0: verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8]; michael@0: } michael@0: } michael@0: michael@0: void add_conics(const SkPoint p[3], michael@0: const SkScalar weight, michael@0: const SkMatrix* toDevice, michael@0: const SkMatrix* toSrc, michael@0: BezierVertex** vert, michael@0: SkRect* devBounds) { michael@0: bloat_quad(p, toDevice, toSrc, *vert, devBounds); michael@0: set_conic_coeffs(p, *vert, weight); michael@0: *vert += kVertsPerQuad; michael@0: } michael@0: michael@0: void add_quads(const SkPoint p[3], michael@0: int subdiv, michael@0: const SkMatrix* toDevice, michael@0: const SkMatrix* toSrc, michael@0: BezierVertex** vert, michael@0: SkRect* devBounds) { michael@0: SkASSERT(subdiv >= 0); michael@0: if (subdiv) { michael@0: SkPoint newP[5]; michael@0: SkChopQuadAtHalf(p, newP); michael@0: add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds); michael@0: add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds); michael@0: } else { michael@0: bloat_quad(p, toDevice, toSrc, *vert, devBounds); michael@0: set_uv_quad(p, *vert); michael@0: *vert += kVertsPerQuad; michael@0: } michael@0: } michael@0: michael@0: void add_line(const SkPoint p[2], michael@0: const SkMatrix* toSrc, michael@0: GrColor coverage, michael@0: LineVertex** vert) { michael@0: const SkPoint& a = p[0]; michael@0: const SkPoint& b = p[1]; michael@0: michael@0: SkVector ortho, vec = b; michael@0: vec -= a; michael@0: michael@0: if (vec.setLength(SK_ScalarHalf)) { michael@0: // Create a vector orthogonal to 'vec' and of unit length michael@0: ortho.fX = 2.0f * vec.fY; michael@0: ortho.fY = -2.0f * vec.fX; michael@0: michael@0: (*vert)[0].fPos = a; michael@0: (*vert)[0].fCoverage = coverage; michael@0: (*vert)[1].fPos = b; michael@0: (*vert)[1].fCoverage = coverage; michael@0: (*vert)[2].fPos = a - vec + ortho; michael@0: (*vert)[2].fCoverage = 0; michael@0: (*vert)[3].fPos = b + vec + ortho; michael@0: (*vert)[3].fCoverage = 0; michael@0: (*vert)[4].fPos = a - vec - ortho; michael@0: (*vert)[4].fCoverage = 0; michael@0: (*vert)[5].fPos = b + vec - ortho; michael@0: (*vert)[5].fCoverage = 0; michael@0: michael@0: if (NULL != toSrc) { michael@0: toSrc->mapPointsWithStride(&(*vert)->fPos, michael@0: sizeof(LineVertex), michael@0: kVertsPerLineSeg); michael@0: } michael@0: } else { michael@0: // just make it degenerate and likely offscreen michael@0: for (int i = 0; i < kVertsPerLineSeg; ++i) { michael@0: (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); michael@0: } michael@0: } michael@0: michael@0: *vert += kVertsPerLineSeg; michael@0: } michael@0: michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: namespace { michael@0: michael@0: // position + edge michael@0: extern const GrVertexAttrib gHairlineBezierAttribs[] = { michael@0: {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, michael@0: {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} michael@0: }; michael@0: michael@0: // position + coverage michael@0: extern const GrVertexAttrib gHairlineLineAttribs[] = { michael@0: {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, michael@0: {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBinding}, michael@0: }; michael@0: michael@0: }; michael@0: michael@0: bool GrAAHairLinePathRenderer::createLineGeom(const SkPath& path, michael@0: GrDrawTarget* target, michael@0: const PtArray& lines, michael@0: int lineCnt, michael@0: GrDrawTarget::AutoReleaseGeometry* arg, michael@0: SkRect* devBounds) { michael@0: GrDrawState* drawState = target->drawState(); michael@0: michael@0: const SkMatrix& viewM = drawState->getViewMatrix(); michael@0: michael@0: int vertCnt = kVertsPerLineSeg * lineCnt; michael@0: michael@0: drawState->setVertexAttribs(SK_ARRAY_COUNT(gHairlineLineAttribs)); michael@0: SkASSERT(sizeof(LineVertex) == drawState->getVertexSize()); michael@0: michael@0: if (!arg->set(target, vertCnt, 0)) { michael@0: return false; michael@0: } michael@0: michael@0: LineVertex* verts = reinterpret_cast(arg->vertices()); michael@0: michael@0: const SkMatrix* toSrc = NULL; michael@0: SkMatrix ivm; michael@0: michael@0: if (viewM.hasPerspective()) { michael@0: if (viewM.invert(&ivm)) { michael@0: toSrc = &ivm; michael@0: } michael@0: } michael@0: devBounds->set(lines.begin(), lines.count()); michael@0: for (int i = 0; i < lineCnt; ++i) { michael@0: add_line(&lines[2*i], toSrc, drawState->getCoverageColor(), &verts); michael@0: } michael@0: // All the verts computed by add_line are within sqrt(1^2 + 0.5^2) of the end points. michael@0: static const SkScalar kSqrtOfOneAndAQuarter = 1.118f; michael@0: // Add a little extra to account for vector normalization precision. michael@0: static const SkScalar kOutset = kSqrtOfOneAndAQuarter + SK_Scalar1 / 20; michael@0: devBounds->outset(kOutset, kOutset); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool GrAAHairLinePathRenderer::createBezierGeom( michael@0: const SkPath& path, michael@0: GrDrawTarget* target, michael@0: const PtArray& quads, michael@0: int quadCnt, michael@0: const PtArray& conics, michael@0: int conicCnt, michael@0: const IntArray& qSubdivs, michael@0: const FloatArray& cWeights, michael@0: GrDrawTarget::AutoReleaseGeometry* arg, michael@0: SkRect* devBounds) { michael@0: GrDrawState* drawState = target->drawState(); michael@0: michael@0: const SkMatrix& viewM = drawState->getViewMatrix(); michael@0: michael@0: int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt; michael@0: michael@0: target->drawState()->setVertexAttribs(SK_ARRAY_COUNT(gHairlineBezierAttribs)); michael@0: SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize()); michael@0: michael@0: if (!arg->set(target, vertCnt, 0)) { michael@0: return false; michael@0: } michael@0: michael@0: BezierVertex* verts = reinterpret_cast(arg->vertices()); michael@0: michael@0: const SkMatrix* toDevice = NULL; michael@0: const SkMatrix* toSrc = NULL; michael@0: SkMatrix ivm; michael@0: michael@0: if (viewM.hasPerspective()) { michael@0: if (viewM.invert(&ivm)) { michael@0: toDevice = &viewM; michael@0: toSrc = &ivm; michael@0: } michael@0: } michael@0: michael@0: // Seed the dev bounds with some pts known to be inside. Each quad and conic grows the bounding michael@0: // box to include its vertices. michael@0: SkPoint seedPts[2]; michael@0: if (quadCnt) { michael@0: seedPts[0] = quads[0]; michael@0: seedPts[1] = quads[2]; michael@0: } else if (conicCnt) { michael@0: seedPts[0] = conics[0]; michael@0: seedPts[1] = conics[2]; michael@0: } michael@0: if (NULL != toDevice) { michael@0: toDevice->mapPoints(seedPts, 2); michael@0: } michael@0: devBounds->set(seedPts[0], seedPts[1]); michael@0: michael@0: int unsubdivQuadCnt = quads.count() / 3; michael@0: for (int i = 0; i < unsubdivQuadCnt; ++i) { michael@0: SkASSERT(qSubdivs[i] >= 0); michael@0: add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds); michael@0: } michael@0: michael@0: // Start Conics michael@0: for (int i = 0; i < conicCnt; ++i) { michael@0: add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path, michael@0: const SkStrokeRec& stroke, michael@0: const GrDrawTarget* target, michael@0: bool antiAlias) const { michael@0: if (!antiAlias) { michael@0: return false; michael@0: } michael@0: michael@0: if (!IsStrokeHairlineOrEquivalent(stroke, michael@0: target->getDrawState().getViewMatrix(), michael@0: NULL)) { michael@0: return false; michael@0: } michael@0: michael@0: if (SkPath::kLine_SegmentMask == path.getSegmentMasks() || michael@0: target->caps()->shaderDerivativeSupport()) { michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: template michael@0: bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount) michael@0: { michael@0: SkRect tolDevBounds = devBounds; michael@0: // The bounds ought to be tight, but in perspective the below code runs the verts michael@0: // through the view matrix to get back to dev coords, which can introduce imprecision. michael@0: if (drawState->getViewMatrix().hasPerspective()) { michael@0: tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000); michael@0: } else { michael@0: // Non-persp matrices cause this path renderer to draw in device space. michael@0: SkASSERT(drawState->getViewMatrix().isIdentity()); michael@0: } michael@0: SkRect actualBounds; michael@0: michael@0: VertexType* verts = reinterpret_cast(vertices); michael@0: bool first = true; michael@0: for (int i = 0; i < vCount; ++i) { michael@0: SkPoint pos = verts[i].fPos; michael@0: // This is a hack to workaround the fact that we move some degenerate segments offscreen. michael@0: if (SK_ScalarMax == pos.fX) { michael@0: continue; michael@0: } michael@0: drawState->getViewMatrix().mapPoints(&pos, 1); michael@0: if (first) { michael@0: actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY); michael@0: first = false; michael@0: } else { michael@0: actualBounds.growToInclude(pos.fX, pos.fY); michael@0: } michael@0: } michael@0: if (!first) { michael@0: return tolDevBounds.contains(actualBounds); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, michael@0: const SkStrokeRec& stroke, michael@0: GrDrawTarget* target, michael@0: bool antiAlias) { michael@0: GrDrawState* drawState = target->drawState(); michael@0: michael@0: SkScalar hairlineCoverage; michael@0: if (IsStrokeHairlineOrEquivalent(stroke, michael@0: target->getDrawState().getViewMatrix(), michael@0: &hairlineCoverage)) { michael@0: uint8_t newCoverage = SkScalarRoundToInt(hairlineCoverage * michael@0: target->getDrawState().getCoverage()); michael@0: target->drawState()->setCoverage(newCoverage); michael@0: } michael@0: michael@0: SkIRect devClipBounds; michael@0: target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds); michael@0: michael@0: int lineCnt; michael@0: int quadCnt; michael@0: int conicCnt; michael@0: PREALLOC_PTARRAY(128) lines; michael@0: PREALLOC_PTARRAY(128) quads; michael@0: PREALLOC_PTARRAY(128) conics; michael@0: IntArray qSubdivs; michael@0: FloatArray cWeights; michael@0: quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds, michael@0: &lines, &quads, &conics, &qSubdivs, &cWeights); michael@0: lineCnt = lines.count() / 2; michael@0: conicCnt = conics.count() / 3; michael@0: michael@0: // do lines first michael@0: if (lineCnt) { michael@0: GrDrawTarget::AutoReleaseGeometry arg; michael@0: SkRect devBounds; michael@0: michael@0: if (!this->createLineGeom(path, michael@0: target, michael@0: lines, michael@0: lineCnt, michael@0: &arg, michael@0: &devBounds)) { michael@0: return false; michael@0: } michael@0: michael@0: GrDrawTarget::AutoStateRestore asr; michael@0: michael@0: // createLineGeom transforms the geometry to device space when the matrix does not have michael@0: // perspective. michael@0: if (target->getDrawState().getViewMatrix().hasPerspective()) { michael@0: asr.set(target, GrDrawTarget::kPreserve_ASRInit); michael@0: } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { michael@0: return false; michael@0: } michael@0: GrDrawState* drawState = target->drawState(); michael@0: michael@0: // Check devBounds michael@0: SkASSERT(check_bounds(drawState, devBounds, arg.vertices(), michael@0: kVertsPerLineSeg * lineCnt)); michael@0: michael@0: { michael@0: GrDrawState::AutoRestoreEffects are(drawState); michael@0: target->setIndexSourceToBuffer(fLinesIndexBuffer); michael@0: int lines = 0; michael@0: while (lines < lineCnt) { michael@0: int n = GrMin(lineCnt - lines, kNumLineSegsInIdxBuffer); michael@0: target->drawIndexed(kTriangles_GrPrimitiveType, michael@0: kVertsPerLineSeg*lines, // startV michael@0: 0, // startI michael@0: kVertsPerLineSeg*n, // vCount michael@0: kIdxsPerLineSeg*n, // iCount michael@0: &devBounds); michael@0: lines += n; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // then quadratics/conics michael@0: if (quadCnt || conicCnt) { michael@0: GrDrawTarget::AutoReleaseGeometry arg; michael@0: SkRect devBounds; michael@0: michael@0: if (!this->createBezierGeom(path, michael@0: target, michael@0: quads, michael@0: quadCnt, michael@0: conics, michael@0: conicCnt, michael@0: qSubdivs, michael@0: cWeights, michael@0: &arg, michael@0: &devBounds)) { michael@0: return false; michael@0: } michael@0: michael@0: GrDrawTarget::AutoStateRestore asr; michael@0: michael@0: // createGeom transforms the geometry to device space when the matrix does not have michael@0: // perspective. michael@0: if (target->getDrawState().getViewMatrix().hasPerspective()) { michael@0: asr.set(target, GrDrawTarget::kPreserve_ASRInit); michael@0: } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { michael@0: return false; michael@0: } michael@0: GrDrawState* drawState = target->drawState(); michael@0: michael@0: static const int kEdgeAttrIndex = 1; michael@0: michael@0: // Check devBounds michael@0: SkASSERT(check_bounds(drawState, devBounds, arg.vertices(), michael@0: kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt)); michael@0: michael@0: if (quadCnt > 0) { michael@0: GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairlineAA_GrEffectEdgeType, michael@0: *target->caps()); michael@0: SkASSERT(NULL != hairQuadEffect); michael@0: GrDrawState::AutoRestoreEffects are(drawState); michael@0: target->setIndexSourceToBuffer(fQuadsIndexBuffer); michael@0: drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref(); michael@0: int quads = 0; michael@0: while (quads < quadCnt) { michael@0: int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer); michael@0: target->drawIndexed(kTriangles_GrPrimitiveType, michael@0: kVertsPerQuad*quads, // startV michael@0: 0, // startI michael@0: kVertsPerQuad*n, // vCount michael@0: kIdxsPerQuad*n, // iCount michael@0: &devBounds); michael@0: quads += n; michael@0: } michael@0: } michael@0: michael@0: if (conicCnt > 0) { michael@0: GrDrawState::AutoRestoreEffects are(drawState); michael@0: GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairlineAA_GrEffectEdgeType, michael@0: *target->caps()); michael@0: SkASSERT(NULL != hairConicEffect); michael@0: drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref(); michael@0: int conics = 0; michael@0: while (conics < conicCnt) { michael@0: int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer); michael@0: target->drawIndexed(kTriangles_GrPrimitiveType, michael@0: kVertsPerQuad*(quadCnt + conics), // startV michael@0: 0, // startI michael@0: kVertsPerQuad*n, // vCount michael@0: kIdxsPerQuad*n, // iCount michael@0: &devBounds); michael@0: conics += n; michael@0: } michael@0: } michael@0: } michael@0: michael@0: target->resetIndexSource(); michael@0: michael@0: return true; michael@0: }