Remove setFromPaint from GrPipelineBuilder

TBR=bsalomon@google.com
BUG=skia:

Review URL: https://codereview.chromium.org/1227153003
diff --git a/gm/constcolorprocessor.cpp b/gm/constcolorprocessor.cpp
index cea7986..661a5ea 100644
--- a/gm/constcolorprocessor.cpp
+++ b/gm/constcolorprocessor.cpp
@@ -111,9 +111,8 @@
                     GrColor color = kColors[procColor];
                     SkAutoTUnref<GrFragmentProcessor> fp(GrConstColorProcessor::Create(color, mode));
 
-                    GrPipelineBuilder pipelineBuilder;
                     GrClip clip;
-                    pipelineBuilder.setFromPaint(grPaint, rt, clip);
+                    GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
                     pipelineBuilder.addColorProcessor(fp);
 
                     tt.target()->drawSimpleRect(&pipelineBuilder,
diff --git a/include/gpu/GrDrawContext.h b/include/gpu/GrDrawContext.h
index 2a1d016..4d59b8c 100644
--- a/include/gpu/GrDrawContext.h
+++ b/include/gpu/GrDrawContext.h
@@ -244,15 +244,9 @@
 
     GrDrawContext(GrContext*, GrDrawTarget*, const SkSurfaceProps&);
 
-    // Sets the paint. Returns true on success; false on failure.
-    bool prepareToDraw(GrPipelineBuilder*,
-                       GrRenderTarget* rt,
-                       const GrClip&,
-                       const GrPaint* paint);
     GrTextContext* createTextContext(GrRenderTarget*, const SkSurfaceProps&);
 
-    // A simpler version of the above which just returns true on success; false on failure.
-    // Clip is *NOT* set
+    // Checks if the context has been abandoned and if the rendertarget is owned by this context
     bool prepareToDraw(GrRenderTarget* rt);
 
     void internalDrawPath(GrDrawTarget*,
diff --git a/src/gpu/GrAtlasTextContext.cpp b/src/gpu/GrAtlasTextContext.cpp
index e0d4529..f5bcefb 100644
--- a/src/gpu/GrAtlasTextContext.cpp
+++ b/src/gpu/GrAtlasTextContext.cpp
@@ -2167,8 +2167,7 @@
                                SkScalar transX, SkScalar transY) {
     // We loop through the runs of the blob, flushing each.  If any run is too large, then we flush
     // it as paths
-    GrPipelineBuilder pipelineBuilder;
-    pipelineBuilder.setFromPaint(grPaint, rt, clip);
+    GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
 
     GrColor color = grPaint.getColor();
 
@@ -2194,8 +2193,7 @@
                                const GrPaint& grPaint,
                                const GrClip& clip,
                                const SkIRect& clipBounds) {
-    GrPipelineBuilder pipelineBuilder;
-    pipelineBuilder.setFromPaint(grPaint, rt, clip);
+    GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
 
     GrColor color = grPaint.getColor();
     for (int run = 0; run < cacheBlob->fRunCount; run++) {
diff --git a/src/gpu/GrDrawContext.cpp b/src/gpu/GrDrawContext.cpp
index b957b91..5e950dc 100644
--- a/src/gpu/GrDrawContext.cpp
+++ b/src/gpu/GrDrawContext.cpp
@@ -187,11 +187,11 @@
         }
 
         AutoCheckFlush acf(fContext);
-        GrPipelineBuilder pipelineBuilder;
-        if (!this->prepareToDraw(&pipelineBuilder, rt, clip, paint)) {
+        if (!this->prepareToDraw(rt)) {
             return;
         }
 
+        GrPipelineBuilder pipelineBuilder(*paint, rt, clip);
         fDrawTarget->drawBWRect(&pipelineBuilder,
                                 paint->getColor(),
                                 SkMatrix::I(),
@@ -435,11 +435,12 @@
     }
 
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
+
     SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getWidth();
 
     // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
@@ -536,11 +537,11 @@
                                         const SkMatrix* localMatrix) {
     RETURN_IF_ABANDONED
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
     fDrawTarget->drawBWRect(&pipelineBuilder,
                             paint.getColor(),
                             viewMatrix,
@@ -832,12 +833,12 @@
                                  int indexCount) {
     RETURN_IF_ABANDONED
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
+
     // TODO clients should give us bounds
     SkRect bounds;
     if (!bounds.setBoundsCheck(positions, vertexCount)) {
@@ -885,11 +886,11 @@
     }
 
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
     GrColor color = paint.getColor();
     if (!GrOvalRenderer::DrawRRect(fDrawTarget,
                                    &pipelineBuilder,
@@ -920,11 +921,11 @@
     }
 
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
     GrColor color = paint.getColor();
     if (!GrOvalRenderer::DrawDRRect(fDrawTarget,
                                     &pipelineBuilder,
@@ -967,11 +968,11 @@
     }
 
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
     GrColor color = paint.getColor();
     if (!GrOvalRenderer::DrawOval(fDrawTarget,
                                   &pipelineBuilder,
@@ -1063,11 +1064,11 @@
     // the writePixels that uploads to the scratch will perform a flush so we're
     // OK.
     AutoCheckFlush acf(fContext);
-    GrPipelineBuilder pipelineBuilder;
-    if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) {
+    if (!this->prepareToDraw(rt)) {
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, rt, clip);
     if (!strokeInfo.isDashed()) {
         bool useCoverageAA = paint.isAntiAlias() &&
                 !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
@@ -1182,18 +1183,6 @@
     pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeInfoPtr, useCoverageAA);
 }
 
-bool GrDrawContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
-                                  GrRenderTarget* rt,
-                                  const GrClip& clip,
-                                  const GrPaint* paint) {
-    RETURN_FALSE_IF_ABANDONED
-
-    ASSERT_OWNED_RESOURCE(rt);
-    SkASSERT(rt && paint);
-    pipelineBuilder->setFromPaint(*paint, rt, clip);
-    return true;
-}
-
 bool GrDrawContext::prepareToDraw(GrRenderTarget* rt) {
     RETURN_FALSE_IF_ABANDONED
 
diff --git a/src/gpu/GrPipelineBuilder.cpp b/src/gpu/GrPipelineBuilder.cpp
index b31d105..59d0556 100644
--- a/src/gpu/GrPipelineBuilder.cpp
+++ b/src/gpu/GrPipelineBuilder.cpp
@@ -48,8 +48,8 @@
     return *this;
 }
 
-void GrPipelineBuilder::setFromPaint(const GrPaint& paint, GrRenderTarget* rt, const GrClip& clip) {
-    SkASSERT(0 == fBlockEffectRemovalCnt || 0 == this->numFragmentStages());
+GrPipelineBuilder::GrPipelineBuilder(const GrPaint& paint, GrRenderTarget* rt, const GrClip& clip) {
+    SkDEBUGCODE(fBlockEffectRemovalCnt = 0;)
 
     fColorStages.reset();
     fCoverageStages.reset();
diff --git a/src/gpu/GrPipelineBuilder.h b/src/gpu/GrPipelineBuilder.h
index 4f9016b..22ad865 100644
--- a/src/gpu/GrPipelineBuilder.h
+++ b/src/gpu/GrPipelineBuilder.h
@@ -38,15 +38,15 @@
         *this = pipelineBuilder;
     }
 
-    virtual ~GrPipelineBuilder();
-
     /**
-     * Initializes the GrPipelineBuilder based on a GrPaint, view matrix and render target. Note
+     * Initializes the GrPipelineBuilder based on a GrPaint, render target, and clip. Note
      * that GrPipelineBuilder encompasses more than GrPaint. Aspects of GrPipelineBuilder that have
      * no GrPaint equivalents are set to default values with the exception of vertex attribute state
      * which is unmodified by this function and clipping which will be enabled.
      */
-    void setFromPaint(const GrPaint&, GrRenderTarget*, const GrClip&);
+    GrPipelineBuilder(const GrPaint&, GrRenderTarget*, const GrClip&);
+
+    virtual ~GrPipelineBuilder();
 
     ///////////////////////////////////////////////////////////////////////////
     /// @name Fragment Processors
diff --git a/src/gpu/GrStencilAndCoverTextContext.cpp b/src/gpu/GrStencilAndCoverTextContext.cpp
index b07a546..980a494 100644
--- a/src/gpu/GrStencilAndCoverTextContext.cpp
+++ b/src/gpu/GrStencilAndCoverTextContext.cpp
@@ -378,24 +378,6 @@
                                     &fGlyphCache->getDescriptor(), fStroke);
     }
 
-    fStateRestore.set(&fPipelineBuilder);
-
-    fPipelineBuilder.setFromPaint(fPaint, fRenderTarget, fClip);
-    SkASSERT(fRenderTarget->isStencilBufferMultisampled() || !fPaint.isAntiAlias());
-    fPipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, fPaint.isAntiAlias());
-
-    GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
-                                 kZero_StencilOp,
-                                 kZero_StencilOp,
-                                 kNotEqual_StencilFunc,
-                                 0xffff,
-                                 0x0000,
-                                 0xffff);
-
-    *fPipelineBuilder.stencil() = kStencilPass;
-
-    SkASSERT(0 == fQueuedGlyphCount);
-    SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
 }
 
 bool GrStencilAndCoverTextContext::mapToFallbackContext(SkMatrix* inverse) {
@@ -445,7 +427,26 @@
                                                                  fViewMatrix,
                                                                  fLocalMatrix));
 
-        fDrawContext->drawPaths(&fPipelineBuilder, pp, fGlyphs,
+        // We should only be flushing about once every run.  However, if this impacts performance
+        // we could move the creation of the GrPipelineBuilder earlier.
+        GrPipelineBuilder pipelineBuilder(fPaint, fRenderTarget, fClip);
+        SkASSERT(fRenderTarget->isStencilBufferMultisampled() || !fPaint.isAntiAlias());
+        pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, fPaint.isAntiAlias());
+
+        GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
+                                     kZero_StencilOp,
+                                     kZero_StencilOp,
+                                     kNotEqual_StencilFunc,
+                                     0xffff,
+                                     0x0000,
+                                     0xffff);
+
+        *pipelineBuilder.stencil() = kStencilPass;
+
+        SkASSERT(0 == fQueuedGlyphCount);
+        SkASSERT(kGlyphBufferSize == fFallbackGlyphsIdx);
+
+        fDrawContext->drawPaths(&pipelineBuilder, pp, fGlyphs,
                                 fGlyphIndices, GrPathRange::kU16_PathIndexType,
                                 get_xy_scalar_array(fGlyphPositions),
                                 GrPathRendering::kTranslate_PathTransformType,
@@ -490,8 +491,6 @@
     SkGlyphCache::AttachCache(fGlyphCache);
     fGlyphCache = NULL;
 
-    fPipelineBuilder.stencil()->setDisabled();
-    fStateRestore.set(NULL);
     fViewMatrix = fContextInitialMatrix;
 }
 
diff --git a/src/gpu/GrStencilAndCoverTextContext.h b/src/gpu/GrStencilAndCoverTextContext.h
index 02d1d3f..40591c8 100644
--- a/src/gpu/GrStencilAndCoverTextContext.h
+++ b/src/gpu/GrStencilAndCoverTextContext.h
@@ -53,8 +53,6 @@
         kMaxPerformance_RenderMode,
     };
 
-    GrPipelineBuilder                                   fPipelineBuilder;
-    GrPipelineBuilder::AutoRestoreFragmentProcessors    fStateRestore;
     SkScalar                                            fTextRatio;
     float                                               fTextInverseRatio;
     SkGlyphCache*                                       fGlyphCache;