simplify GrPathRenderer interface

Review URL: http://codereview.appspot.com/5706053/



git-svn-id: http://skia.googlecode.com/svn/trunk@3312 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrAAHairLinePathRenderer.cpp b/src/gpu/GrAAHairLinePathRenderer.cpp
index e11c12b..3ed4488 100644
--- a/src/gpu/GrAAHairLinePathRenderer.cpp
+++ b/src/gpu/GrAAHairLinePathRenderer.cpp
@@ -99,7 +99,6 @@
     linesIndexBuffer->ref();
     fQuadsIndexBuffer = quadsIndexBuffer;
     quadsIndexBuffer->ref();
-    this->resetGeom();
 }
 
 GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
@@ -107,33 +106,6 @@
     fQuadsIndexBuffer->unref();
 }
 
-bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget::Caps& targetCaps,
-                                           const SkPath& path,
-                                           GrPathFill fill,
-                                           bool antiAlias) const {
-    static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
-                                          SkPath::kQuad_SegmentMask;
-    return (kHairLine_PathFill == fill &&
-            antiAlias &&
-            (targetCaps.fShaderDerivativeSupport ||
-             !(gReqDerivMask & path.getSegmentMasks())));
-}
-
-void GrAAHairLinePathRenderer::pathWillClear() {
-    this->resetGeom();
-}
-
-void GrAAHairLinePathRenderer::resetGeom() {
-    fPreviousStages = ~0;
-    fPreviousRTHeight = ~0;
-    fPreviousViewMatrix = GrMatrix::InvalidMatrix();
-    fLineSegmentCnt = 0;
-    fQuadCnt = 0; 
-    if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) {
-        fTarget->resetVertexSource();
-    }
-}
-
 namespace {
 
 typedef SkTArray<SkPoint, true> PtArray;
@@ -528,28 +500,23 @@
 
 }
 
-bool GrAAHairLinePathRenderer::createGeom(GrDrawState::StageMask stageMask) {
-    const GrDrawState& drawState = fTarget->getDrawState();
+bool GrAAHairLinePathRenderer::createGeom(const SkPath& path,
+                                          const GrVec* translate,
+                                          GrDrawTarget* target,
+                                          GrDrawState::StageMask stageMask,
+                                          int* lineCnt,
+                                          int* quadCnt) {
+    const GrDrawState& drawState = target->getDrawState();
     int rtHeight = drawState.getRenderTarget()->height();
 
     GrIRect clip;
-    if (fTarget->getClip().hasConservativeBounds()) {
-        GrRect clipRect =  fTarget->getClip().getConservativeBounds();
+    if (target->getClip().hasConservativeBounds()) {
+        GrRect clipRect =  target->getClip().getConservativeBounds();
         clipRect.roundOut(&clip);
     } else {
         clip.setLargest();
     }
 
-    // If none of the inputs that affect generation of path geometry have
-    // have changed since last previous path draw then we can reuse the
-    // previous geoemtry.
-    if (stageMask == fPreviousStages &&
-        fPreviousViewMatrix == drawState.getViewMatrix() &&
-        fPreviousTranslate == fTranslate &&
-        rtHeight == fPreviousRTHeight &&
-        fClipRect == clip) {
-        return true;
-    }
 
     GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
     for (int s = 0; s < GrDrawState::kNumStages; ++s) {
@@ -563,16 +530,20 @@
     PREALLOC_PTARRAY(128) lines;
     PREALLOC_PTARRAY(128) quads;
     IntArray qSubdivs;
-    fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
+    static const GrVec gZeroVec = {0, 0};
+    if (NULL == translate) {
+        translate = &gZeroVec;
+    }
+    *quadCnt = generate_lines_and_quads(path, viewM, *translate, clip,
                                         &lines, &quads, &qSubdivs);
 
-    fLineSegmentCnt = lines.count() / 2;
-    int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
+    *lineCnt = lines.count() / 2;
+    int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt;
 
     GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
 
     Vertex* verts;
-    if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
+    if (!target->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
         return false;
     }
 
@@ -587,7 +558,7 @@
         }
     }
 
-    for (int i = 0; i < fLineSegmentCnt; ++i) {
+    for (int i = 0; i < *lineCnt; ++i) {
         add_line(&lines[2*i], rtHeight, toSrc, &verts);
     }
 
@@ -597,25 +568,50 @@
         add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
     }
 
-    fPreviousStages = stageMask;
-    fPreviousViewMatrix = drawState.getViewMatrix();
-    fPreviousRTHeight = rtHeight;
-    fClipRect = clip;
-    fPreviousTranslate = fTranslate;
     return true;
 }
 
-void GrAAHairLinePathRenderer::drawPath(GrDrawState::StageMask stageMask) {
-
-    if (!this->createGeom(stageMask)) {
-        return;
+bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
+                                           GrPathFill fill,
+                                           const GrDrawTarget* target,
+                                           bool antiAlias) const {
+    if (fill != kHairLine_PathFill || !antiAlias) {
+        return false;
     }
 
-    GrDrawState* drawState = fTarget->drawState();
+    static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
+                                          SkPath::kQuad_SegmentMask;
+    if (!target->getCaps().fShaderDerivativeSupport &&
+        (gReqDerivMask & path.getSegmentMasks())) {
+        return false;
+    }
+    return true;
+}
+
+bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
+                                          GrPathFill fill,
+                                          const GrVec* translate,
+                                          GrDrawTarget* target,
+                                          GrDrawState::StageMask stageMask,
+                                          bool antiAlias) {
+
+    int lineCnt;
+    int quadCnt;
+
+    if (!this->createGeom(path,
+                          translate,
+                          target,
+                          stageMask,
+                          &lineCnt,
+                          &quadCnt)) {
+        return false;
+    }
+
+    GrDrawState* drawState = target->drawState();
 
     GrDrawTarget::AutoStateRestore asr;
     if (!drawState->getViewMatrix().hasPerspective()) {
-        asr.set(fTarget);
+        asr.set(target);
         GrMatrix ivm;
         if (drawState->getViewInverse(&ivm)) {
             drawState->preConcatSamplerMatrices(stageMask, ivm);
@@ -625,32 +621,32 @@
 
     // TODO: See whether rendering lines as degenerate quads improves perf
     // when we have a mix
-    fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
+    target->setIndexSourceToBuffer(fLinesIndexBuffer);
     int lines = 0;
     int nBufLines = fLinesIndexBuffer->maxQuads();
-    while (lines < fLineSegmentCnt) {
-        int n = GrMin(fLineSegmentCnt-lines, nBufLines);
+    while (lines < lineCnt) {
+        int n = GrMin(lineCnt - lines, nBufLines);
         drawState->setVertexEdgeType(GrDrawState::kHairLine_EdgeType);
-        fTarget->drawIndexed(kTriangles_PrimitiveType,
-                             kVertsPerLineSeg*lines,    // startV
-                             0,                         // startI
-                             kVertsPerLineSeg*n,        // vCount
-                             kIdxsPerLineSeg*n);        // iCount
+        target->drawIndexed(kTriangles_PrimitiveType,
+                            kVertsPerLineSeg*lines,    // startV
+                            0,                         // startI
+                            kVertsPerLineSeg*n,        // vCount
+                            kIdxsPerLineSeg*n);        // iCount
         lines += n;
     }
 
-    fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
+    target->setIndexSourceToBuffer(fQuadsIndexBuffer);
     int quads = 0;
-    while (quads < fQuadCnt) {
-        int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
+    while (quads < quadCnt) {
+        int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
         drawState->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType);
-        fTarget->drawIndexed(kTriangles_PrimitiveType,
-                             4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
-                             0,                                       // startI
-                             kVertsPerQuad*n,                         // vCount
-                             kIdxsPerQuad*n);                         // iCount
+        target->drawIndexed(kTriangles_PrimitiveType,
+                            4 * lineCnt + kVertsPerQuad*quads, // startV
+                            0,                                 // startI
+                            kVertsPerQuad*n,                   // vCount
+                            kIdxsPerQuad*n);                   // iCount
         quads += n;
     }
-
+    return true;
 }