#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"
}
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.
*/
return SkISize::Make(800, 800);
}
-
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
if (NULL == rt) {
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) {
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) {
};
//////////////////////////////////////////////////////////////////////////////
+
+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.
*/
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) {
#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.
*/
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();
}
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);
}
}
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);
}
--- /dev/null
+/*
+ * 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