Make GrDrawContext::internalDrawPath cons up its own GrPipelineBuilder
authorrobertphillips <robertphillips@google.com>
Thu, 25 Feb 2016 20:56:50 +0000 (12:56 -0800)
committerCommit bot <commit-bot@chromium.org>
Thu, 25 Feb 2016 20:56:50 +0000 (12:56 -0800)
Hopefully, this better defines (and reduces) the lifetime and reuse of GrPipelineBuilder objects in GrDrawContext.

TBR=bsalomon@google.com

GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1730903007

Review URL: https://codereview.chromium.org/1730903007

include/gpu/GrDrawContext.h
src/gpu/GrDrawContext.cpp

index 8042ab1..67182d1 100644 (file)
@@ -100,7 +100,7 @@ public:
                   const GrPaint& paint,
                   const SkMatrix& viewMatrix,
                   const SkRect&,
-                  const GrStrokeInfo* strokeInfo = NULL);
+                  const GrStrokeInfo* strokeInfo = nullptr);
 
     /**
      * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
@@ -280,12 +280,11 @@ private:
     friend class GrAtlasTextBlob; // for access to drawBatch
     friend class GrDrawingManager; // for ctor
 
-    void internalDrawPath(GrPipelineBuilder*,
+    void internalDrawPath(const GrClip& clip,
+                          const GrPaint& paint,
                           const SkMatrix& viewMatrix,
-                          GrColor,
-                          bool useAA,
-                          const SkPath&,
-                          const GrStrokeInfo&);
+                          const SkPath& path,
+                          const GrStrokeInfo& strokeInfo);
 
     // This entry point allows the GrTextContext-derived classes to add their batches to
     // the drawTarget.
index b5c2819..9c656a6 100644 (file)
@@ -227,10 +227,10 @@ void GrDrawContext::drawPaint(const GrClip& clip,
 
         AutoCheckFlush acf(fDrawingManager);
 
-        GrPipelineBuilder pipelineBuilder(*paint, fRenderTarget, clip);
         SkAutoTUnref<GrDrawBatch> batch(
                 GrRectBatchFactory::CreateNonAAFill(paint->getColor(), SkMatrix::I(), r, nullptr,
                                                     &localMatrix));
+        GrPipelineBuilder pipelineBuilder(*paint, fRenderTarget, clip);
         this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
     }
 }
@@ -263,7 +263,7 @@ void GrDrawContext::drawRect(const GrClip& clip,
 
     AutoCheckFlush acf(fDrawingManager);
 
-    SkScalar width = nullptr == strokeInfo ? -1 : strokeInfo->getWidth();
+    SkScalar width = !strokeInfo ? -1 : strokeInfo->getWidth();
 
     // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
     // cases where the RT is fully inside a stroke.
@@ -299,15 +299,13 @@ void GrDrawContext::drawRect(const GrClip& clip,
         }
     }
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
-    GrColor color = paint.getColor();
-
+    bool snapToPixelCenters = false;
     SkAutoTUnref<GrDrawBatch> batch;
     if (should_apply_coverage_aa(paint, fRenderTarget)) {
         if (width >= 0) {
             // The stroke path needs the rect to remain axis aligned (no rotation or skew).
             if (viewMatrix.rectStaysRect()) {
-                batch.reset(GrRectBatchFactory::CreateAAStroke(color, viewMatrix, rect,
+                batch.reset(GrRectBatchFactory::CreateAAStroke(paint.getColor(), viewMatrix, rect,
                                                                *strokeInfo));
             }
         } else {
@@ -315,34 +313,42 @@ void GrDrawContext::drawRect(const GrClip& clip,
             if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
                 SkRect devBoundRect;
                 viewMatrix.mapRect(&devBoundRect, rect);
-                batch.reset(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
+                batch.reset(GrRectBatchFactory::CreateAAFill(paint.getColor(), viewMatrix, rect,
                                                              devBoundRect));
             }
         }
-        if (!batch) {
-            SkPath path;
-            path.setIsVolatile(true);
-            path.addRect(rect);
-            this->internalDrawPath(&pipelineBuilder, viewMatrix, color, true, path, *strokeInfo);
-            SkASSERT(paint.isAntiAlias());
-            return;
-        }
     } else if (width >= 0) {
         // Non-AA hairlines are snapped to pixel centers to make which pixels are hit deterministic
-        bool snapToPixelCenters = (0 == width && !fRenderTarget->isUnifiedMultisampled());
-        batch.reset(GrRectBatchFactory::CreateNonAAStroke(color, viewMatrix, rect, width,
-                                                          snapToPixelCenters));
+        snapToPixelCenters = (0 == width && !fRenderTarget->isUnifiedMultisampled());
+        batch.reset(GrRectBatchFactory::CreateNonAAStroke(paint.getColor(), viewMatrix, rect,
+                                                          width, snapToPixelCenters));
 
         // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
-        // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA
-        // is enabled because it can cause ugly artifacts.
-        pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag,
-                                 snapToPixelCenters);
+        // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when
+        // MSAA is enabled because it can cause ugly artifacts.
     } else {
         // filled BW rect
-        batch.reset(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, nullptr, nullptr));
+        batch.reset(GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rect,
+                                                        nullptr, nullptr));
     }
-    this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
+
+    if (batch) {
+        GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
+
+        if (snapToPixelCenters) {
+            pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag,
+                                     snapToPixelCenters);
+        }
+
+        this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
+        return;
+    }
+    
+    SkPath path;
+    path.setIsVolatile(true);
+    path.addRect(rect);
+    this->internalDrawPath(clip, paint, viewMatrix, path,
+                           strokeInfo ? *strokeInfo : GrStrokeInfo::FillInfo());
 }
 
 void GrDrawContext::fillRectToRect(const GrClip& clip,
@@ -357,7 +363,6 @@ void GrDrawContext::fillRectToRect(const GrClip& clip,
 
     AutoCheckFlush acf(fDrawingManager);
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
     SkAutoTUnref<GrDrawBatch> batch;
     if (should_apply_coverage_aa(paint, fRenderTarget) &&
         view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
@@ -369,6 +374,7 @@ void GrDrawContext::fillRectToRect(const GrClip& clip,
     }
 
     if (batch) {
+        GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
         this->drawBatch(&pipelineBuilder, batch);
     }
 }
@@ -385,8 +391,6 @@ void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip,
 
     AutoCheckFlush acf(fDrawingManager);
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
-
     SkAutoTUnref<GrDrawBatch> batch;
     if (should_apply_coverage_aa(paint, fRenderTarget) &&
         view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
@@ -396,6 +400,8 @@ void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip,
         batch.reset(GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rectToDraw,
                                                         nullptr, &localMatrix));
     }
+
+    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
     this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
 }
 
@@ -416,8 +422,6 @@ void GrDrawContext::drawVertices(const GrClip& clip,
 
     AutoCheckFlush acf(fDrawingManager);
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
-
     // TODO clients should give us bounds
     SkRect bounds;
     if (!bounds.setBoundsCheck(positions, vertexCount)) {
@@ -442,6 +446,7 @@ void GrDrawContext::drawVertices(const GrClip& clip,
                                                                 indexCount, colors, texCoords,
                                                                 bounds));
 
+    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
     this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
 }
 
@@ -460,14 +465,13 @@ void GrDrawContext::drawAtlas(const GrClip& clip,
     GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawAtlas");
 
     AutoCheckFlush acf(fDrawingManager);
-    
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
-    
+        
     GrDrawAtlasBatch::Geometry geometry;
     geometry.fColor = paint.getColor();
     SkAutoTUnref<GrDrawBatch> batch(GrDrawAtlasBatch::Create(geometry, viewMatrix, spriteCount,
                                                              xform, texRect, colors));
     
+    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
     this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
 }
 
@@ -491,18 +495,16 @@ void GrDrawContext::drawRRect(const GrClip& clip,
 
     AutoCheckFlush acf(fDrawingManager);
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
-    GrColor color = paint.getColor();
-
     if (should_apply_coverage_aa(paint, fRenderTarget)) {
         GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
 
-        SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateRRectBatch(color,
+        SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateRRectBatch(paint.getColor(),
                                                                          viewMatrix,
                                                                          rrect,
                                                                          strokeInfo,
                                                                          shaderCaps));
         if (batch) {
+            GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
             this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
             return;
         }
@@ -511,8 +513,7 @@ void GrDrawContext::drawRRect(const GrClip& clip,
     SkPath path;
     path.setIsVolatile(true);
     path.addRRect(rrect);
-    this->internalDrawPath(&pipelineBuilder, viewMatrix, color,
-                           paint.isAntiAlias(), path, strokeInfo);
+    this->internalDrawPath(clip, paint, viewMatrix, path, strokeInfo);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -535,17 +536,15 @@ void GrDrawContext::drawOval(const GrClip& clip,
 
     AutoCheckFlush acf(fDrawingManager);
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
-    GrColor color = paint.getColor();
-    
     if (should_apply_coverage_aa(paint, fRenderTarget)) {
         GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
-        SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(color,
+        SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(),
                                                                         viewMatrix,
                                                                         oval,
                                                                         strokeInfo,
                                                                         shaderCaps));
         if (batch) {
+            GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
             this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
             return;
         }
@@ -554,8 +553,7 @@ void GrDrawContext::drawOval(const GrClip& clip,
     SkPath path;
     path.setIsVolatile(true);
     path.addOval(oval);
-    this->internalDrawPath(&pipelineBuilder, viewMatrix, color,
-                           paint.isAntiAlias(), path, strokeInfo);
+    this->internalDrawPath(clip, paint, viewMatrix, path, strokeInfo);
 }
 
 void GrDrawContext::drawImageNine(const GrClip& clip,
@@ -673,16 +671,8 @@ void GrDrawContext::drawPath(const GrClip& clip,
        return;
     }
 
-    GrColor color = paint.getColor();
-
-    // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
-    // Scratch textures can be recycled after they are returned to the texture
-    // cache. This presents a potential hazard for buffered drawing. However,
-    // the writePixels that uploads to the scratch will perform a flush so we're
-    // OK.
     AutoCheckFlush acf(fDrawingManager);
 
-    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
     if (should_apply_coverage_aa(paint, fRenderTarget) && !strokeInfo.isDashed()) {
         if (strokeInfo.getWidth() < 0 && !path.isConvex()) {
             // Concave AA paths are expensive - try to avoid them for special cases
@@ -690,7 +680,9 @@ void GrDrawContext::drawPath(const GrClip& clip,
 
             if (is_nested_rects(viewMatrix, path, strokeInfo, rects)) {
                 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFillNestedRects(
-                    color, viewMatrix, rects));
+                    paint.getColor(), viewMatrix, rects));
+
+                GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
                 this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
                 return;
             }
@@ -700,25 +692,30 @@ void GrDrawContext::drawPath(const GrClip& clip,
 
         if (isOval && !path.isInverseFillType()) {
             GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
-            SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(color,
+            SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(),
                                                                             viewMatrix,
                                                                             ovalRect,
                                                                             strokeInfo,
                                                                             shaderCaps));
             if (batch) {
+                GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
                 this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
                 return;
             }
         }
     }
-    this->internalDrawPath(&pipelineBuilder, viewMatrix, color,
-                           paint.isAntiAlias(), path, strokeInfo);
+
+    // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
+    // Scratch textures can be recycled after they are returned to the texture
+    // cache. This presents a potential hazard for buffered drawing. However,
+    // the writePixels that uploads to the scratch will perform a flush so we're
+    // OK.
+    this->internalDrawPath(clip, paint, viewMatrix, path, strokeInfo);
 }
 
-void GrDrawContext::internalDrawPath(GrPipelineBuilder* pipelineBuilder,
+void GrDrawContext::internalDrawPath(const GrClip& clip,
+                                     const GrPaint& paint,
                                      const SkMatrix& viewMatrix,
-                                     GrColor color,
-                                     bool useAA,
                                      const SkPath& path,
                                      const GrStrokeInfo& strokeInfo) {
     ASSERT_SINGLE_OWNER
@@ -729,10 +726,9 @@ void GrDrawContext::internalDrawPath(GrPipelineBuilder* pipelineBuilder,
     // the src color (either the input alpha or in the frag shader) to implement
     // aa. If we have some future driver-mojo path AA that can do the right
     // thing WRT to the blend then we'll need some query on the PR.
-    bool useCoverageAA = useAA &&
-        !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled();
-    bool isStencilDisabled = pipelineBuilder->getStencil().isDisabled();
-    bool isStencilBufferMSAA = pipelineBuilder->getRenderTarget()->isStencilBufferMultisampled();
+    bool useCoverageAA = should_apply_coverage_aa(paint, fRenderTarget);
+    const bool isStencilDisabled = true;
+    bool isStencilBufferMSAA = fRenderTarget->isStencilBufferMultisampled();
 
     const GrPathRendererChain::DrawType type =
         useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType
@@ -805,11 +801,13 @@ void GrDrawContext::internalDrawPath(GrPipelineBuilder* pipelineBuilder,
         return;
     }
 
+    GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip);
+
     GrPathRenderer::DrawPathArgs args;
     args.fTarget = this->getDrawTarget();
     args.fResourceProvider = fDrawingManager->getContext()->resourceProvider();
-    args.fPipelineBuilder = pipelineBuilder;
-    args.fColor = color;
+    args.fPipelineBuilder = &pipelineBuilder;
+    args.fColor = paint.getColor();
     args.fViewMatrix = &viewMatrix;
     args.fPath = pathPtr;
     args.fStroke = strokeInfoPtr;