Hide drawBatch on GrDrawTarget from test Methods

TBR=bsalomon@google.com
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1574383002

Review URL: https://codereview.chromium.org/1574383002
diff --git a/gm/beziereffects.cpp b/gm/beziereffects.cpp
index 5167b77..2511dec 100644
--- a/gm/beziereffects.cpp
+++ b/gm/beziereffects.cpp
@@ -12,6 +12,7 @@
 
 #if SK_SUPPORT_GPU
 
+#include "GrDrawContext.h"
 #include "GrContext.h"
 #include "GrPathUtils.h"
 #include "GrTest.h"
@@ -129,6 +130,11 @@
             return;
         }
 
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
+        if (!drawContext) {
+            return;
+        }
+
         struct Vertex {
             SkPoint fPosition;
             float   fKLM[4]; // The last value is ignored. The effect expects a vec4f.
@@ -155,20 +161,12 @@
             };
             for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
                 SkAutoTUnref<GrGeometryProcessor> gp;
-                {   // scope to contain GrTestTarget
-                    GrTestTarget tt;
-                    context->getTestTarget(&tt, rt);
-                    if (nullptr == tt.target()) {
-                        continue;
-                    }
-                    GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
-                    gp.reset(GrCubicEffect::Create(color, SkMatrix::I(), et,
-                                                   *tt.target()->caps()));
-                    if (!gp) {
-                        continue;
-                    }
+                GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
+                gp.reset(GrCubicEffect::Create(color, SkMatrix::I(), et,
+                                               *context->caps()));
+                if (!gp) {
+                    continue;
                 }
-
                 SkScalar x = SkScalarMul(col, w);
                 SkScalar y = SkScalarMul(row, h);
                 SkPoint controlPts[] = {
@@ -216,10 +214,6 @@
                     boundsPaint.setStyle(SkPaint::kStroke_Style);
                     canvas->drawRect(bounds, boundsPaint);
 
-                    GrTestTarget tt;
-                    context->getTestTarget(&tt, rt);
-                    SkASSERT(tt.target());
-
                     GrPipelineBuilder pipelineBuilder;
                     pipelineBuilder.setXPFactory(
                         GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
@@ -232,7 +226,7 @@
                     SkAutoTUnref<GrDrawBatch> batch(
                             BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs, klmSigns[c]));
 
-                    tt.target()->drawBatch(pipelineBuilder, batch);
+                    drawContext->internal_drawBatch(pipelineBuilder, batch);
                 }
                 ++col;
                 if (numCols == col) {
@@ -279,6 +273,11 @@
             return;
         }
 
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
+        if (!drawContext) {
+            return;
+        }
+
         struct Vertex {
             SkPoint fPosition;
             float   fKLM[4]; // The last value is ignored. The effect expects a vec4f.
@@ -305,18 +304,11 @@
             SkScalar weight = rand.nextRangeF(0.f, 2.f);
             for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
                 SkAutoTUnref<GrGeometryProcessor> gp;
-                {   // scope to contain GrTestTarget
-                    GrTestTarget tt;
-                    context->getTestTarget(&tt, rt);
-                    if (nullptr == tt.target()) {
-                        continue;
-                    }
-                    GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
-                    gp.reset(GrConicEffect::Create(color, SkMatrix::I(), et,
-                                                   *tt.target()->caps(), SkMatrix::I(), false));
-                    if (!gp) {
-                        continue;
-                    }
+                GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
+                gp.reset(GrConicEffect::Create(color, SkMatrix::I(), et,
+                                               *context->caps(), SkMatrix::I(), false));
+                if (!gp) {
+                    continue;
                 }
 
                 SkScalar x = SkScalarMul(col, w);
@@ -363,10 +355,6 @@
                     boundsPaint.setStyle(SkPaint::kStroke_Style);
                     canvas->drawRect(bounds, boundsPaint);
 
-                    GrTestTarget tt;
-                    context->getTestTarget(&tt, rt);
-                    SkASSERT(tt.target());
-
                     GrPipelineBuilder pipelineBuilder;
                     pipelineBuilder.setXPFactory(
                         GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
@@ -379,7 +367,7 @@
                     SkAutoTUnref<GrDrawBatch> batch(
                             BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs, 1.f));
 
-                    tt.target()->drawBatch(pipelineBuilder, batch);
+                    drawContext->internal_drawBatch(pipelineBuilder, batch);
                 }
                 ++col;
                 if (numCols == col) {
@@ -524,6 +512,11 @@
             return;
         }
 
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
+        if (!drawContext) {
+            return;
+        }
+
         struct Vertex {
             SkPoint fPosition;
             float   fUV[4]; // The last two values are ignored. The effect expects a vec4f.
@@ -548,18 +541,11 @@
             };
             for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
                 SkAutoTUnref<GrGeometryProcessor> gp;
-                {   // scope to contain GrTestTarget
-                    GrTestTarget tt;
-                    context->getTestTarget(&tt, rt);
-                    if (nullptr == tt.target()) {
-                        continue;
-                    }
-                    GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
-                    gp.reset(GrQuadEffect::Create(color, SkMatrix::I(), et,
-                                                  *tt.target()->caps(), SkMatrix::I(), false));
-                    if (!gp) {
-                        continue;
-                    }
+                GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
+                gp.reset(GrQuadEffect::Create(color, SkMatrix::I(), et,
+                                              *context->caps(), SkMatrix::I(), false));
+                if (!gp) {
+                    continue;
                 }
 
                 SkScalar x = SkScalarMul(col, w);
@@ -603,10 +589,6 @@
                     boundsPaint.setStyle(SkPaint::kStroke_Style);
                     canvas->drawRect(bounds, boundsPaint);
 
-                    GrTestTarget tt;
-                    context->getTestTarget(&tt, rt);
-                    SkASSERT(tt.target());
-
                     GrPipelineBuilder pipelineBuilder;
                     pipelineBuilder.setXPFactory(
                         GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
@@ -621,7 +603,7 @@
                     SkAutoTUnref<GrDrawBatch> batch(BezierQuadTestBatch::Create(gp, geometry,
                                                                                 DevToUV));
 
-                    tt.target()->drawBatch(pipelineBuilder, batch);
+                    drawContext->internal_drawBatch(pipelineBuilder, batch);
                 }
                 ++col;
                 if (numCols == col) {
diff --git a/gm/convexpolyeffect.cpp b/gm/convexpolyeffect.cpp
index fb767dd..4071159 100644
--- a/gm/convexpolyeffect.cpp
+++ b/gm/convexpolyeffect.cpp
@@ -14,6 +14,7 @@
 
 #include "GrContext.h"
 #include "GrDefaultGeoProcFactory.h"
+#include "GrDrawContext.h"
 #include "GrPathUtils.h"
 #include "GrTest.h"
 #include "SkColorPriv.h"
@@ -32,7 +33,9 @@
 public:
     DEFINE_BATCH_CLASS_ID
     struct Geometry : public GrTestBatch::Geometry {
-        SkRect fBounds;
+        SkRect fRect;
+        SkRect fBounds; // This will be == fRect, except fBounds must be sorted, whereas fRect can
+                        // be inverted
     };
 
     const char* name() const override { return "ConvexPolyTestBatch"; }
@@ -48,6 +51,7 @@
         // Make sure any artifacts around the exterior of path are visible by using overly
         // conservative bounding geometry.
         fGeometry.fBounds.outset(5.f, 5.f);
+        fGeometry.fRect.outset(5.f, 5.f);
     }
 
     Geometry* geoData(int index) override {
@@ -69,7 +73,7 @@
             return;
         }
 
-        fGeometry.fBounds.toQuad(verts);
+        fGeometry.fRect.toQuad(verts);
 
         helper.recordDraw(target);
     }
@@ -159,6 +163,11 @@
             return;
         }
 
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
+        if (!drawContext) {
+            return;
+        }
+
         Color color(0xff000000);
         Coverage coverage(Coverage::kSolid_Type);
         LocalCoords localCoords(LocalCoords::kUnused_Type);
@@ -173,12 +182,6 @@
             SkScalar x = 0;
 
             for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
-                GrTestTarget tt;
-                context->getTestTarget(&tt, rt);
-                if (nullptr == tt.target()) {
-                    SkDEBUGFAIL("Couldn't get Gr test target.");
-                    return;
-                }
                 const SkMatrix m = SkMatrix::MakeTrans(x, y);
                 SkPath p;
                 path->transform(m, &p);
@@ -197,11 +200,12 @@
 
                 ConvexPolyTestBatch::Geometry geometry;
                 geometry.fColor = color.fColor;
+                geometry.fRect = p.getBounds();
                 geometry.fBounds = p.getBounds();
 
                 SkAutoTUnref<GrDrawBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
 
-                tt.target()->drawBatch(pipelineBuilder, batch);
+                drawContext->internal_drawBatch(pipelineBuilder, batch);
 
                 x += SkScalarCeilToScalar(path->getBounds().width() + 10.f);
             }
@@ -226,12 +230,6 @@
             SkScalar x = 0;
 
             for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
-                GrTestTarget tt;
-                context->getTestTarget(&tt, rt);
-                if (nullptr == tt.target()) {
-                    SkDEBUGFAIL("Couldn't get Gr test target.");
-                    return;
-                }
                 SkRect rect = *iter.get();
                 rect.offset(x, y);
                 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
@@ -248,11 +246,13 @@
 
                 ConvexPolyTestBatch::Geometry geometry;
                 geometry.fColor = color.fColor;
+                geometry.fRect = rect;
                 geometry.fBounds = rect;
+                geometry.fBounds.sort();
 
                 SkAutoTUnref<GrDrawBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
 
-                tt.target()->drawBatch(pipelineBuilder, batch);
+                drawContext->internal_drawBatch(pipelineBuilder, batch);
 
                 x += SkScalarCeilToScalar(rect.width() + 10.f);
             }
diff --git a/include/gpu/GrDrawContext.h b/include/gpu/GrDrawContext.h
index 9a82995..9276e2b 100644
--- a/include/gpu/GrDrawContext.h
+++ b/include/gpu/GrDrawContext.h
@@ -278,6 +278,10 @@
 
     GrRenderTarget* accessRenderTarget() { return fRenderTarget; }
 
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+    // Functions intended for internal use only.
+    void internal_drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch);
+
 private:
     friend class GrAtlasTextBlob; // for access to drawBatch
     friend class GrDrawingManager; // for ctor
diff --git a/src/gpu/GrTest.cpp b/src/gpu/GrTest.cpp
index 2a2dc95..b83d67f 100644
--- a/src/gpu/GrTest.cpp
+++ b/src/gpu/GrTest.cpp
@@ -251,6 +251,24 @@
 void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
 
 ///////////////////////////////////////////////////////////////////////////////
+
+#define ASSERT_SINGLE_OWNER \
+    SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
+#define RETURN_IF_ABANDONED        if (fDrawingManager->abandoned()) { return; }
+
+void GrDrawContext::internal_drawBatch(const GrPipelineBuilder& pipelineBuilder,
+                                       GrDrawBatch* batch) {
+    ASSERT_SINGLE_OWNER
+    RETURN_IF_ABANDONED
+    SkDEBUGCODE(this->validate();)
+
+    this->getDrawTarget()->drawBatch(pipelineBuilder, batch);
+}
+
+#undef ASSERT_SINGLE_OWNER
+#undef RETURN_IF_ABANDONED
+
+///////////////////////////////////////////////////////////////////////////////
 // Code for the mock context. It's built on a mock GrGpu class that does nothing.
 ////
 
diff --git a/tests/GLProgramsTest.cpp b/tests/GLProgramsTest.cpp
index c72c315..3933e64 100644
--- a/tests/GLProgramsTest.cpp
+++ b/tests/GLProgramsTest.cpp
@@ -15,6 +15,7 @@
 #include "GrAutoLocaleSetter.h"
 #include "GrBatchTest.h"
 #include "GrContextFactory.h"
+#include "GrDrawContext.h"
 #include "GrDrawingManager.h"
 #include "GrInvariantOutput.h"
 #include "GrPipeline.h"
@@ -356,10 +357,13 @@
         set_random_state(&pipelineBuilder, &random);
         set_random_stencil(&pipelineBuilder, &random);
 
-        GrTestTarget tt;
-        context->getTestTarget(&tt, rt);
+        SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
+        if (!drawContext) {
+            SkDebugf("Could not allocate drawContext");
+            return false;
+        }
 
-        tt.target()->drawBatch(pipelineBuilder, batch);
+        drawContext->internal_drawBatch(pipelineBuilder, batch);
     }
     // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
     drawingManager->flush();
@@ -390,10 +394,13 @@
                 BlockInputFragmentProcessor::Create(fp));
             builder.addColorFragmentProcessor(blockFP);
 
-            GrTestTarget tt;
-            context->getTestTarget(&tt, rt);
+            SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
+            if (!drawContext) {
+                SkDebugf("Could not allocate a drawcontext");
+                return false;
+            }
 
-            tt.target()->drawBatch(builder, batch);
+            drawContext->internal_drawBatch(builder, batch);
             drawingManager->flush();
         }
     }