GMs now use batch
authorjoshualitt <joshualitt@chromium.org>
Wed, 11 Feb 2015 19:34:58 +0000 (11:34 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 11 Feb 2015 19:34:58 +0000 (11:34 -0800)
BUG=skia:

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

gm/beziereffects.cpp
gm/convexpolyeffect.cpp
gyp/gmslides.gypi
src/gpu/GrTestBatch.h [new file with mode: 0644]

index 0ddcc52b95da2de9c37acb51ec594885fb2a37ad..578a597ce51ce4f6808ea658e760d4aba456545a 100644 (file)
 
 #if SK_SUPPORT_GPU
 
+#include "GrBatchTarget.h"
+#include "GrBufferAllocPool.h"
 #include "GrContext.h"
 #include "GrPathUtils.h"
 #include "GrTest.h"
+#include "GrTestBatch.h"
 #include "SkColorPriv.h"
 #include "SkDevice.h"
 #include "SkGeometry.h"
@@ -26,6 +29,86 @@ static inline SkScalar eval_line(const SkPoint& p, const SkScalar lineEq[3], SkS
 }
 
 namespace skiagm {
+
+class BezierCubicOrConicTestBatch : public GrTestBatch {
+public:
+    struct Geometry : public GrTestBatch::Geometry {
+        SkRect fBounds;
+    };
+
+    const char* name() const SK_OVERRIDE { return "BezierCubicOrConicTestBatch"; }
+
+    static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo,
+                           const SkScalar klmEqs[9], SkScalar sign) {
+        return SkNEW_ARGS(BezierCubicOrConicTestBatch, (gp, geo, klmEqs, sign));
+    }
+
+private:
+    BezierCubicOrConicTestBatch(const GrGeometryProcessor* gp, const Geometry& geo,
+                                const SkScalar klmEqs[9], SkScalar sign)
+        : INHERITED(gp) {
+        for (int i = 0; i < 9; i++) {
+            fKlmEqs[i] = klmEqs[i];
+        }
+
+        fGeometry = geo;
+        fSign = sign;
+    }
+
+    struct Vertex {
+        SkPoint fPosition;
+        float   fKLM[4]; // The last value is ignored. The effect expects a vec4f.
+    };
+
+    Geometry* geoData(int index) SK_OVERRIDE {
+        SkASSERT(0 == index);
+        return &fGeometry;
+    }
+
+    void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
+        size_t vertexStride = this->geometryProcessor()->getVertexStride();
+
+        const GrVertexBuffer* vertexBuffer;
+        int firstVertex;
+
+        void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
+                                                              kVertsPerCubic,
+                                                              &vertexBuffer,
+                                                              &firstVertex);
+
+        SkASSERT(vertexStride == sizeof(Vertex));
+        Vertex* verts = reinterpret_cast<Vertex*>(vertices);
+
+        verts[0].fPosition.setRectFan(fGeometry.fBounds.fLeft, fGeometry.fBounds.fTop,
+                                      fGeometry.fBounds.fRight, fGeometry.fBounds.fBottom,
+                                      sizeof(Vertex));
+        for (int v = 0; v < 4; ++v) {
+            verts[v].fKLM[0] = eval_line(verts[v].fPosition, fKlmEqs + 0, fSign);
+            verts[v].fKLM[1] = eval_line(verts[v].fPosition, fKlmEqs + 3, fSign);
+            verts[v].fKLM[2] = eval_line(verts[v].fPosition, fKlmEqs + 6, 1.f);
+        }
+
+        GrDrawTarget::DrawInfo drawInfo;
+        drawInfo.setPrimitiveType(kTriangleFan_GrPrimitiveType);
+        drawInfo.setVertexBuffer(vertexBuffer);
+        drawInfo.setStartVertex(firstVertex);
+        drawInfo.setVertexCount(kVertsPerCubic);
+        drawInfo.setStartIndex(0);
+        drawInfo.setIndexCount(kIndicesPerCubic);
+        drawInfo.setIndexBuffer(batchTarget->quadIndexBuffer());
+        batchTarget->draw(drawInfo);
+    }
+
+    Geometry fGeometry;
+    SkScalar fKlmEqs[9];
+    SkScalar fSign;
+
+    static const int kVertsPerCubic = 4;
+    static const int kIndicesPerCubic = 6;
+
+    typedef GrTestBatch INHERITED;
+};
+
 /**
  * This GM directly exercises effects that draw Bezier curves in the GPU backend.
  */
@@ -44,7 +127,6 @@ protected:
         return SkISize::Make(800, 800);
     }
 
-
     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
         GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
         if (NULL == rt) {
@@ -147,25 +229,16 @@ protected:
                     SkASSERT(tt.target());
 
                     GrPipelineBuilder pipelineBuilder;
+                    pipelineBuilder.setRenderTarget(rt);
 
-                    GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0);
-                    SkASSERT(gp->getVertexStride() == sizeof(Vertex));
-                    Vertex* verts = reinterpret_cast<Vertex*>(geo.vertices());
-
-                    verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
-                                                  bounds.fRight, bounds.fBottom,
-                                                  sizeof(Vertex));
-                    for (int v = 0; v < 4; ++v) {
-                        verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, klmSigns[c]);
-                        verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, klmSigns[c]);
-                        verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f);
-                    }
+                    BezierCubicOrConicTestBatch::Geometry geometry;
+                    geometry.fColor = gp->color();
+                    geometry.fBounds = bounds;
 
-                    pipelineBuilder.setRenderTarget(rt);
+                    SkAutoTUnref<GrBatch> batch(BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs,
+                                                                             klmSigns[c]));
 
-                    tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
-                    tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType,
-                                             0, 0,4,6);
+                    tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
                 }
                 ++col;
                 if (numCols == col) {
@@ -300,25 +373,16 @@ protected:
                     SkASSERT(tt.target());
 
                     GrPipelineBuilder pipelineBuilder;
+                    pipelineBuilder.setRenderTarget(rt);
 
-                    GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0);
-                    SkASSERT(gp->getVertexStride() == sizeof(Vertex));
-                    Vertex* verts = reinterpret_cast<Vertex*>(geo.vertices());
-
-                    verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
-                                                  bounds.fRight, bounds.fBottom,
-                                                  sizeof(Vertex));
-                    for (int v = 0; v < 4; ++v) {
-                        verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, 1.f);
-                        verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, 1.f);
-                        verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f);
-                    }
+                    BezierCubicOrConicTestBatch::Geometry geometry;
+                    geometry.fColor = gp->color();
+                    geometry.fBounds = bounds;
 
-                    pipelineBuilder.setRenderTarget(rt);
+                    SkAutoTUnref<GrBatch> batch(BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs,
+                                                                             1.f));
 
-                    tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
-                    tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType,
-                                             0, 0,4,6);
+                    tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
                 }
                 ++col;
                 if (numCols == col) {
@@ -371,6 +435,79 @@ private:
 };
 
 //////////////////////////////////////////////////////////////////////////////
+
+class BezierQuadTestBatch : public GrTestBatch {
+public:
+    struct Geometry : public GrTestBatch::Geometry {
+        SkRect fBounds;
+    };
+
+    const char* name() const SK_OVERRIDE { return "BezierQuadTestBatch"; }
+
+    static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo,
+                           const GrPathUtils::QuadUVMatrix& devToUV) {
+        return SkNEW_ARGS(BezierQuadTestBatch, (gp, geo, devToUV));
+    }
+
+private:
+    BezierQuadTestBatch(const GrGeometryProcessor* gp, const Geometry& geo,
+                        const GrPathUtils::QuadUVMatrix& devToUV)
+        : INHERITED(gp)
+        , fGeometry(geo)
+        , fDevToUV(devToUV) {
+    }
+
+    struct Vertex {
+        SkPoint fPosition;
+        float   fKLM[4]; // The last value is ignored. The effect expects a vec4f.
+    };
+
+    Geometry* geoData(int index) SK_OVERRIDE {
+        SkASSERT(0 == index);
+        return &fGeometry;
+    }
+
+    void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
+        size_t vertexStride = this->geometryProcessor()->getVertexStride();
+
+        const GrVertexBuffer* vertexBuffer;
+        int firstVertex;
+
+        void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
+                                                              kVertsPerCubic,
+                                                              &vertexBuffer,
+                                                              &firstVertex);
+
+        SkASSERT(vertexStride == sizeof(Vertex));
+        Vertex* verts = reinterpret_cast<Vertex*>(vertices);
+
+        verts[0].fPosition.setRectFan(fGeometry.fBounds.fLeft, fGeometry.fBounds.fTop,
+                                      fGeometry.fBounds.fRight, fGeometry.fBounds.fBottom,
+                                      sizeof(Vertex));
+
+        fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
+
+
+        GrDrawTarget::DrawInfo drawInfo;
+        drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
+        drawInfo.setVertexBuffer(vertexBuffer);
+        drawInfo.setStartVertex(firstVertex);
+        drawInfo.setVertexCount(kVertsPerCubic);
+        drawInfo.setStartIndex(0);
+        drawInfo.setIndexCount(kIndicesPerCubic);
+        drawInfo.setIndexBuffer(batchTarget->quadIndexBuffer());
+        batchTarget->draw(drawInfo);
+    }
+
+    Geometry fGeometry;
+    GrPathUtils::QuadUVMatrix fDevToUV;
+
+    static const int kVertsPerCubic = 4;
+    static const int kIndicesPerCubic = 6;
+
+    typedef GrTestBatch INHERITED;
+};
+
 /**
  * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
  */
@@ -484,23 +621,17 @@ protected:
                     SkASSERT(tt.target());
 
                     GrPipelineBuilder pipelineBuilder;
-
-                    GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0);
-                    SkASSERT(gp->getVertexStride() == sizeof(Vertex));
-                    Vertex* verts = reinterpret_cast<Vertex*>(geo.vertices());
-
-                    verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
-                                                  bounds.fRight, bounds.fBottom,
-                                                  sizeof(Vertex));
+                    pipelineBuilder.setRenderTarget(rt);
 
                     GrPathUtils::QuadUVMatrix DevToUV(pts);
-                    DevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
 
-                    pipelineBuilder.setRenderTarget(rt);
+                    BezierQuadTestBatch::Geometry geometry;
+                    geometry.fColor = gp->color();
+                    geometry.fBounds = bounds;
+
+                    SkAutoTUnref<GrBatch> batch(BezierQuadTestBatch::Create(gp, geometry, DevToUV));
 
-                    tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
-                    tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangles_GrPrimitiveType,
-                                             0, 0, 4, 6);
+                    tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
                 }
                 ++col;
                 if (numCols == col) {
index 281ddca77e873249b97792ccb0a1bc38993a8094..0a3be28c73a81146383fb02a0bfd2d949020e944 100644 (file)
 
 #if SK_SUPPORT_GPU
 
+#include "GrBatchTarget.h"
+#include "GrBufferAllocPool.h"
 #include "GrContext.h"
 #include "GrDefaultGeoProcFactory.h"
 #include "GrPathUtils.h"
 #include "GrTest.h"
+#include "GrTestBatch.h"
 #include "SkColorPriv.h"
 #include "SkDevice.h"
 #include "SkGeometry.h"
 #include "effects/GrConvexPolyEffect.h"
 
 namespace skiagm {
+
+class ConvexPolyTestBatch : public GrTestBatch {
+public:
+    struct Geometry : public GrTestBatch::Geometry {
+        SkRect fBounds;
+    };
+
+    const char* name() const SK_OVERRIDE { return "ConvexPolyTestBatch"; }
+
+    static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo) {
+        return SkNEW_ARGS(ConvexPolyTestBatch, (gp, geo));
+    }
+
+private:
+    ConvexPolyTestBatch(const GrGeometryProcessor* gp, const Geometry& geo)
+        : INHERITED(gp)
+        , fGeometry(geo) {
+    }
+
+    Geometry* geoData(int index) SK_OVERRIDE {
+        SkASSERT(0 == index);
+        return &fGeometry;
+    }
+
+    void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
+        size_t vertexStride = this->geometryProcessor()->getVertexStride();
+
+        const GrVertexBuffer* vertexBuffer;
+        int firstVertex;
+
+        void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
+                                                              kVertsPerCubic,
+                                                              &vertexBuffer,
+                                                              &firstVertex);
+
+        SkASSERT(vertexStride == sizeof(SkPoint));
+        SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
+
+        // 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.fBounds.toQuad(verts);
+
+        GrDrawTarget::DrawInfo drawInfo;
+        drawInfo.setPrimitiveType(kTriangleFan_GrPrimitiveType);
+        drawInfo.setVertexBuffer(vertexBuffer);
+        drawInfo.setStartVertex(firstVertex);
+        drawInfo.setVertexCount(kVertsPerCubic);
+        drawInfo.setStartIndex(0);
+        drawInfo.setIndexCount(kIndicesPerCubic);
+        drawInfo.setIndexBuffer(batchTarget->quadIndexBuffer());
+        batchTarget->draw(drawInfo);
+    }
+
+    Geometry fGeometry;
+
+    static const int kVertsPerCubic = 4;
+    static const int kIndicesPerCubic = 6;
+
+    typedef GrTestBatch INHERITED;
+};
+
 /**
  * This GM directly exercises a GrProcessor that draws convex polygons.
  */
@@ -103,6 +168,10 @@ protected:
             return;
         }
 
+        SkAutoTUnref<const GrGeometryProcessor> gp(
+                GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
+                                                0xff000000));
+
         SkScalar y = 0;
         for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_IterStart);
              iter.get();
@@ -129,25 +198,16 @@ protected:
                 }
 
                 GrPipelineBuilder pipelineBuilder;
-                SkAutoTUnref<const GrGeometryProcessor> gp(
-                        GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
-                                                        0xff000000));
                 pipelineBuilder.addCoverageProcessor(fp);
                 pipelineBuilder.setRenderTarget(rt);
 
-                GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0);
-                SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
-                SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
+                ConvexPolyTestBatch::Geometry geometry;
+                geometry.fColor = gp->color();
+                geometry.fBounds = p.getBounds();
 
-                SkRect bounds = p.getBounds();
-                // Make sure any artifacts around the exterior of path are visible by using overly
-                // conservative bounding geometry.
-                bounds.outset(5.f, 5.f);
-                bounds.toQuad(verts);
+                SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
 
-                tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
-                tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType,
-                                         0, 0, 4, 6);
+                tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
 
                 x += SkScalarCeilToScalar(path->getBounds().width() + 10.f);
             }
@@ -187,23 +247,16 @@ protected:
                 }
 
                 GrPipelineBuilder pipelineBuilder;
-                SkAutoTUnref<const GrGeometryProcessor> gp(
-                        GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
-                                                        0xff000000));
                 pipelineBuilder.addCoverageProcessor(fp);
                 pipelineBuilder.setRenderTarget(rt);
 
-                GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0);
-                SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
-                SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
+                ConvexPolyTestBatch::Geometry geometry;
+                geometry.fColor = gp->color();
+                geometry.fBounds = rect;
 
-                SkRect bounds = rect;
-                bounds.outset(5.f, 5.f);
-                bounds.toQuad(verts);
+                SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
 
-                tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
-                tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType,
-                                         0, 0, 4, 6);
+                tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
 
                 x += SkScalarCeilToScalar(rect.width() + 10.f);
             }
index a285ee6aa326ba440127e86815b56b4b1314d9dd..45bd303da9aaadcf1f31c55019aa75c80a070e7f 100644 (file)
         '../gm/yuvtorgbeffect.cpp',
 
         # Files needed by particular GMs
+        '../src/gpu/GrTestBatch.h',
         '../src/utils/debugger/SkDrawCommand.h',
         '../src/utils/debugger/SkDrawCommand.cpp',
         '../src/utils/debugger/SkDebugCanvas.h',
diff --git a/src/gpu/GrTestBatch.h b/src/gpu/GrTestBatch.h
new file mode 100644 (file)
index 0000000..ab294ff
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrTestBatch_DEFINED
+#define GrTestBatch_DEFINED
+
+#include "GrBatch.h"
+
+/*
+ * A simple batch only for testing purposes which actually doesn't batch at all, but can fit into
+ * the batch pipeline and generate arbitrary geometry
+ */
+class GrTestBatch : public GrBatch {
+public:
+    struct Geometry {
+        GrColor fColor;
+    };
+
+    virtual const char* name() const SK_OVERRIDE = 0;
+
+    void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE {
+        // When this is called on a batch, there is only one geometry bundle
+        out->setUnknownFourComponents();
+    }
+
+    void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE {
+        out->setUnknownSingleComponent();
+    }
+
+    void initBatchOpt(const GrBatchOpt& batchOpt) {}
+
+    void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE {
+        // Handle any color overrides
+        if (init.fColorIgnored) {
+            this->geoData(0)->fColor = GrColor_ILLEGAL;
+        } else if (GrColor_ILLEGAL != init.fOverrideColor) {
+            this->geoData(0)->fColor = init.fOverrideColor;
+        }
+
+        // setup batch properties
+        fBatch.fColorIgnored = init.fColorIgnored;
+        fBatch.fColor = this->geoData(0)->fColor;
+        fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
+        fBatch.fCoverageIgnored = init.fCoverageIgnored;
+    }
+
+    void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
+        batchTarget->initDraw(fGeometryProcessor, pipeline);
+
+        // TODO this is hacky, but the only way we have to initialize the GP is to use the
+        // GrPipelineInfo struct so we can generate the correct shader.  Once we have GrBatch
+        // everywhere we can remove this nastiness
+        GrPipelineInfo init;
+        init.fColorIgnored = fBatch.fColorIgnored;
+        init.fOverrideColor = GrColor_ILLEGAL;
+        init.fCoverageIgnored = fBatch.fCoverageIgnored;
+        init.fUsesLocalCoords = fBatch.fUsesLocalCoords;
+        fGeometryProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init);
+
+        this->onGenerateGeometry(batchTarget, pipeline);
+    }
+
+protected:
+    GrTestBatch(const GrGeometryProcessor* gp) {
+        fGeometryProcessor.reset(SkRef(gp));
+    }
+
+    const GrGeometryProcessor* geometryProcessor() const { return fGeometryProcessor; }
+
+private:
+    virtual Geometry* geoData(int index) = 0;
+
+    bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE {
+        return false;
+    }
+
+    virtual void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) = 0;
+
+    struct BatchTracker {
+        GrColor fColor;
+        bool fUsesLocalCoords;
+        bool fColorIgnored;
+        bool fCoverageIgnored;
+    };
+
+    SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
+    BatchTracker fBatch;
+};
+
+#endif