From ecd1a69fbf88dc704709416f37179c1177954b13 Mon Sep 17 00:00:00 2001 From: joshualitt Date: Mon, 10 Aug 2015 10:08:26 -0700 Subject: Create GrRectBatchFactory BUG=skia: Review URL: https://codereview.chromium.org/1287433003 --- src/gpu/GrDrawTarget.cpp | 6 +- src/gpu/batches/GrRectBatch.cpp | 318 ++++++++++++--------------------- src/gpu/batches/GrRectBatch.h | 75 ++++++-- src/gpu/batches/GrRectBatchFactory.cpp | 41 +++++ src/gpu/batches/GrRectBatchFactory.h | 31 ++++ 5 files changed, 251 insertions(+), 220 deletions(-) create mode 100644 src/gpu/batches/GrRectBatchFactory.cpp create mode 100644 src/gpu/batches/GrRectBatchFactory.h (limited to 'src') diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp index 21a80deb31..c2a9eb0aa7 100644 --- a/src/gpu/GrDrawTarget.cpp +++ b/src/gpu/GrDrawTarget.cpp @@ -22,7 +22,7 @@ #include "GrVertexBuffer.h" #include "batches/GrBatch.h" -#include "batches/GrRectBatch.h" +#include "batches/GrRectBatchFactory.h" #include "SkStrokeRec.h" @@ -294,8 +294,8 @@ void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder, const SkRect& rect, const SkRect* localRect, const SkMatrix* localMatrix) { - SkAutoTUnref batch(GrRectBatch::Create(color, viewMatrix, rect, localRect, - localMatrix)); + SkAutoTUnref batch(GrRectBatchFactory::Create(color, viewMatrix, rect, localRect, + localMatrix)); this->drawBatch(pipelineBuilder, batch); } diff --git a/src/gpu/batches/GrRectBatch.cpp b/src/gpu/batches/GrRectBatch.cpp index 1cc948652b..0995156b4e 100644 --- a/src/gpu/batches/GrRectBatch.cpp +++ b/src/gpu/batches/GrRectBatch.cpp @@ -7,262 +7,168 @@ #include "GrRectBatch.h" -#include "GrBatch.h" #include "GrBatchTarget.h" -#include "GrBatchTest.h" #include "GrDefaultGeoProcFactory.h" #include "GrPrimitiveProcessor.h" -class RectBatch : public GrBatch { -public: - struct Geometry { - SkMatrix fViewMatrix; - SkRect fRect; - SkRect fLocalRect; - SkMatrix fLocalMatrix; - GrColor fColor; - bool fHasLocalRect; - bool fHasLocalMatrix; - }; - - static GrBatch* Create(const Geometry& geometry) { - return SkNEW_ARGS(RectBatch, (geometry)); +void GrRectBatch::initBatchTracker(const GrPipelineInfo& init) { + // Handle any color overrides + if (!init.readsColor()) { + fGeoData[0].fColor = GrColor_ILLEGAL; } + init.getOverrideColorIfSet(&fGeoData[0].fColor); - const char* name() const override { return "RectBatch"; } + // setup batch properties + fBatch.fColorIgnored = !init.readsColor(); + fBatch.fColor = fGeoData[0].fColor; + fBatch.fUsesLocalCoords = init.readsLocalCoords(); + fBatch.fCoverageIgnored = !init.readsCoverage(); +} - void getInvariantOutputColor(GrInitInvariantOutput* out) const override { - // When this is called on a batch, there is only one geometry bundle - out->setKnownFourComponents(fGeoData[0].fColor); +void GrRectBatch::generateGeometry(GrBatchTarget* batchTarget) { + SkAutoTUnref gp(this->createRectGP()); + if (!gp) { + SkDebugf("Could not create GrGeometryProcessor\n"); + return; } - void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { - out->setKnownSingleComponent(0xff); - } + batchTarget->initDraw(gp, this->pipeline()); - void initBatchTracker(const GrPipelineInfo& init) override { - // Handle any color overrides - if (!init.readsColor()) { - fGeoData[0].fColor = GrColor_ILLEGAL; - } - init.getOverrideColorIfSet(&fGeoData[0].fColor); + int instanceCount = fGeoData.count(); + size_t vertexStride = gp->getVertexStride(); + SkASSERT(this->hasLocalRect() ? + vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : + vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); + QuadHelper helper; + void* vertices = helper.init(batchTarget, vertexStride, instanceCount); - // setup batch properties - fBatch.fColorIgnored = !init.readsColor(); - fBatch.fColor = fGeoData[0].fColor; - fBatch.fUsesLocalCoords = init.readsLocalCoords(); - fBatch.fCoverageIgnored = !init.readsCoverage(); + if (!vertices) { + return; } - void generateGeometry(GrBatchTarget* batchTarget) override { - SkAutoTUnref gp(this->createRectGP()); - if (!gp) { - SkDebugf("Could not create GrGeometryProcessor\n"); - return; + for (int i = 0; i < instanceCount; i++) { + const Geometry& geom = fGeoData[i]; + + intptr_t offset = reinterpret_cast(vertices) + + kVerticesPerQuad * i * vertexStride; + SkPoint* positions = reinterpret_cast(offset); + + positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop, + geom.fRect.fRight, geom.fRect.fBottom, vertexStride); + geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerticesPerQuad); + + // TODO we should only do this if local coords are being read + if (geom.fHasLocalRect) { + static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); + SkPoint* coords = reinterpret_cast(offset + kLocalOffset); + coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop, + geom.fLocalRect.fRight, geom.fLocalRect.fBottom, + vertexStride); + if (geom.fHasLocalMatrix) { + geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride, kVerticesPerQuad); + } } - batchTarget->initDraw(gp, this->pipeline()); - - int instanceCount = fGeoData.count(); - size_t vertexStride = gp->getVertexStride(); - SkASSERT(this->hasLocalRect() ? - vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : - vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); - QuadHelper helper; - void* vertices = helper.init(batchTarget, vertexStride, instanceCount); - - if (!vertices) { - return; + static const int kColorOffset = sizeof(SkPoint); + GrColor* vertColor = reinterpret_cast(offset + kColorOffset); + for (int j = 0; j < 4; ++j) { + *vertColor = geom.fColor; + vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); } + } - for (int i = 0; i < instanceCount; i++) { - const Geometry& geom = fGeoData[i]; - - intptr_t offset = reinterpret_cast(vertices) + - kVerticesPerQuad * i * vertexStride; - SkPoint* positions = reinterpret_cast(offset); - - positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop, - geom.fRect.fRight, geom.fRect.fBottom, vertexStride); - geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerticesPerQuad); - - // TODO we should only do this if local coords are being read - if (geom.fHasLocalRect) { - static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); - SkPoint* coords = reinterpret_cast(offset + kLocalOffset); - coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop, - geom.fLocalRect.fRight, geom.fLocalRect.fBottom, - vertexStride); - if (geom.fHasLocalMatrix) { - geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride, kVerticesPerQuad); - } - } - - static const int kColorOffset = sizeof(SkPoint); - GrColor* vertColor = reinterpret_cast(offset + kColorOffset); - for (int j = 0; j < 4; ++j) { - *vertColor = geom.fColor; - vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); - } - } + helper.issueDraw(batchTarget); +} - helper.issueDraw(batchTarget); +bool GrRectBatch::onCombineIfPossible(GrBatch* t) { + if (!this->pipeline()->isEqual(*t->pipeline())) { + return false; } - SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } + GrRectBatch* that = t->cast(); -private: - RectBatch(const Geometry& geometry) { - this->initClassID(); - fGeoData.push_back(geometry); - - fBounds = geometry.fRect; - geometry.fViewMatrix.mapRect(&fBounds); + if (this->hasLocalRect() != that->hasLocalRect()) { + return false; } - GrColor color() const { return fBatch.fColor; } - bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } - bool colorIgnored() const { return fBatch.fColorIgnored; } - const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } - const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; } - bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; } - bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; } - bool coverageIgnored() const { return fBatch.fCoverageIgnored; } - - bool onCombineIfPossible(GrBatch* t) override { - if (!this->pipeline()->isEqual(*t->pipeline())) { + SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); + if (!this->hasLocalRect() && this->usesLocalCoords()) { + if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { return false; } - RectBatch* that = t->cast(); - - if (this->hasLocalRect() != that->hasLocalRect()) { + if (this->hasLocalMatrix() != that->hasLocalMatrix()) { return false; } - SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); - if (!this->hasLocalRect() && this->usesLocalCoords()) { - if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { - return false; - } - - if (this->hasLocalMatrix() != that->hasLocalMatrix()) { - return false; - } - - if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that->localMatrix())) { - return false; - } - } - - if (this->color() != that->color()) { - fBatch.fColor = GrColor_ILLEGAL; + if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that->localMatrix())) { + return false; } - fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); - this->joinBounds(that->bounds()); - return true; } - /** We always use per-vertex colors so that rects can be batched across color changes. Sometimes - we have explicit local coords and sometimes not. We *could* always provide explicit local - coords and just duplicate the positions when the caller hasn't provided a local coord rect, - but we haven't seen a use case which frequently switches between local rect and no local - rect draws. - - The color param is used to determine whether the opaque hint can be set on the draw state. - The caller must populate the vertex colors itself. - - The vertex attrib order is always pos, color, [local coords]. - */ - const GrGeometryProcessor* createRectGP() { - using namespace GrDefaultGeoProcFactory; - Color color(Color::kAttribute_Type); - Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Coverage::kSolid_Type); - - // if we have a local rect, then we apply the localMatrix directly to the localRect to - // generate vertex local coords - if (this->hasLocalRect()) { - LocalCoords localCoords(LocalCoords::kHasExplicit_Type); - return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); - } else { - LocalCoords localCoords(LocalCoords::kUsePosition_Type, - this->hasLocalMatrix() ? &this->localMatrix() : NULL); - return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords, - this->viewMatrix()); - } + if (this->color() != that->color()) { + fBatch.fColor = GrColor_ILLEGAL; } + fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); + this->joinBounds(that->bounds()); + return true; +} +/** We always use per-vertex colors so that rects can be batched across color changes. Sometimes + we have explicit local coords and sometimes not. We *could* always provide explicit local + coords and just duplicate the positions when the caller hasn't provided a local coord rect, + but we haven't seen a use case which frequently switches between local rect and no local + rect draws. - struct BatchTracker { - GrColor fColor; - bool fUsesLocalCoords; - bool fColorIgnored; - bool fCoverageIgnored; - }; - - BatchTracker fBatch; - SkSTArray<1, Geometry, true> fGeoData; -}; - -namespace GrRectBatch { - -GrBatch* Create(GrColor color, - const SkMatrix& viewMatrix, - const SkRect& rect, - const SkRect* localRect, - const SkMatrix* localMatrix) { - RectBatch::Geometry geometry; - geometry.fColor = color; - geometry.fViewMatrix = viewMatrix; - geometry.fRect = rect; - - if (localRect) { - geometry.fHasLocalRect = true; - geometry.fLocalRect = *localRect; - } else { - geometry.fHasLocalRect = false; - } + The color param is used to determine whether the opaque hint can be set on the draw state. + The caller must populate the vertex colors itself. - if (localMatrix) { - geometry.fHasLocalMatrix = true; - geometry.fLocalMatrix = *localMatrix; + The vertex attrib order is always pos, color, [local coords]. + */ +const GrGeometryProcessor* GrRectBatch::createRectGP() { + using namespace GrDefaultGeoProcFactory; + Color color(Color::kAttribute_Type); + Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Coverage::kSolid_Type); + + // if we have a local rect, then we apply the localMatrix directly to the localRect to + // generate vertex local coords + if (this->hasLocalRect()) { + LocalCoords localCoords(LocalCoords::kHasExplicit_Type); + return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); } else { - geometry.fHasLocalMatrix = false; + LocalCoords localCoords(LocalCoords::kUsePosition_Type, + this->hasLocalMatrix() ? &this->localMatrix() : NULL); + return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords, + this->viewMatrix()); } - - return RectBatch::Create(geometry); } -}; - /////////////////////////////////////////////////////////////////////////////////////////////////// #ifdef GR_TEST_UTILS +#include "GrBatchTest.h" + BATCH_TEST_DEFINE(RectBatch) { - GrColor color = GrRandomColor(random); - - SkRect rect = GrTest::TestRect(random); - SkRect localRect; - bool hasLocalRect = random->nextBool(); - bool hasLocalMatrix = random->nextBool(); - - SkMatrix viewMatrix; - SkMatrix localMatrix; - if (hasLocalRect) { - viewMatrix = GrTest::TestMatrixInvertible(random); - localRect = GrTest::TestRect(random); + GrRectBatch::Geometry geometry; + geometry.fColor = GrRandomColor(random); + + geometry.fRect = GrTest::TestRect(random); + geometry.fHasLocalRect = random->nextBool(); + + if (geometry.fHasLocalRect) { + geometry.fViewMatrix = GrTest::TestMatrixInvertible(random); + geometry.fLocalRect = GrTest::TestRect(random); } else { - viewMatrix = GrTest::TestMatrix(random); + geometry.fViewMatrix = GrTest::TestMatrix(random); } - if (hasLocalMatrix) { - localMatrix = GrTest::TestMatrix(random); + geometry.fHasLocalMatrix = random->nextBool(); + if (geometry.fHasLocalMatrix) { + geometry.fLocalMatrix = GrTest::TestMatrix(random); } - return GrRectBatch::Create(color, viewMatrix, rect, - hasLocalRect ? &localRect : NULL, - hasLocalMatrix ? &localMatrix : NULL); + return GrRectBatch::Create(geometry); } #endif diff --git a/src/gpu/batches/GrRectBatch.h b/src/gpu/batches/GrRectBatch.h index 5844cc3b01..b8c57424c7 100644 --- a/src/gpu/batches/GrRectBatch.h +++ b/src/gpu/batches/GrRectBatch.h @@ -8,24 +8,77 @@ #ifndef GrRectBatch_DEFINED #define GrRectBatch_DEFINED +#include "GrBatch.h" #include "GrColor.h" -class GrBatch; +class GrBatchTarget; class SkMatrix; struct SkRect; -/* - * A factory for returning batches which can draw rectangles. Right now this only handles non-AA - * rects - */ -namespace GrRectBatch { +class GrRectBatch : public GrBatch { +public: + struct Geometry { + SkMatrix fViewMatrix; + SkRect fRect; + SkRect fLocalRect; + SkMatrix fLocalMatrix; + GrColor fColor; + bool fHasLocalRect; + bool fHasLocalMatrix; + }; + + static GrBatch* Create(const Geometry& geometry) { + return SkNEW_ARGS(GrRectBatch, (geometry)); + } + + const char* name() const override { return "RectBatch"; } + + void getInvariantOutputColor(GrInitInvariantOutput* out) const override { + // When this is called on a batch, there is only one geometry bundle + out->setKnownFourComponents(fGeoData[0].fColor); + } + + void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { + out->setKnownSingleComponent(0xff); + } + + void initBatchTracker(const GrPipelineInfo& init) override; + + void generateGeometry(GrBatchTarget* batchTarget) override; + + SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } + +private: + GrRectBatch(const Geometry& geometry) { + this->initClassID(); + fGeoData.push_back(geometry); + + fBounds = geometry.fRect; + geometry.fViewMatrix.mapRect(&fBounds); + } + + GrColor color() const { return fBatch.fColor; } + bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } + bool colorIgnored() const { return fBatch.fColorIgnored; } + const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } + const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; } + bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; } + bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; } + bool coverageIgnored() const { return fBatch.fCoverageIgnored; } + + bool onCombineIfPossible(GrBatch* t) override; + + const GrGeometryProcessor* createRectGP(); -GrBatch* Create(GrColor color, - const SkMatrix& viewMatrix, - const SkRect& rect, - const SkRect* localRect, - const SkMatrix* localMatrix); + struct BatchTracker { + GrColor fColor; + bool fUsesLocalCoords; + bool fColorIgnored; + bool fCoverageIgnored; + }; + BatchTracker fBatch; + SkSTArray<1, Geometry, true> fGeoData; }; #endif diff --git a/src/gpu/batches/GrRectBatchFactory.cpp b/src/gpu/batches/GrRectBatchFactory.cpp new file mode 100644 index 0000000000..b41561209e --- /dev/null +++ b/src/gpu/batches/GrRectBatchFactory.cpp @@ -0,0 +1,41 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "GrRectBatchFactory.h" + +#include "GrRectBatch.h" + +namespace GrRectBatchFactory { + +GrBatch* Create(GrColor color, + const SkMatrix& viewMatrix, + const SkRect& rect, + const SkRect* localRect, + const SkMatrix* localMatrix) { + GrRectBatch::Geometry geometry; + geometry.fColor = color; + geometry.fViewMatrix = viewMatrix; + geometry.fRect = rect; + + if (localRect) { + geometry.fHasLocalRect = true; + geometry.fLocalRect = *localRect; + } else { + geometry.fHasLocalRect = false; + } + + if (localMatrix) { + geometry.fHasLocalMatrix = true; + geometry.fLocalMatrix = *localMatrix; + } else { + geometry.fHasLocalMatrix = false; + } + + return GrRectBatch::Create(geometry); +} + +}; diff --git a/src/gpu/batches/GrRectBatchFactory.h b/src/gpu/batches/GrRectBatchFactory.h new file mode 100644 index 0000000000..33ecf917d0 --- /dev/null +++ b/src/gpu/batches/GrRectBatchFactory.h @@ -0,0 +1,31 @@ +/* + * 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 GrRectBatchFactory_DEFINED +#define GrRectBatchFactory_DEFINED + +#include "GrColor.h" + +class GrBatch; +class SkMatrix; +struct SkRect; + +/* + * A factory for returning batches which can draw rectangles. Right now this only handles non-AA + * rects + */ +namespace GrRectBatchFactory { + +GrBatch* Create(GrColor color, + const SkMatrix& viewMatrix, + const SkRect& rect, + const SkRect* localRect, + const SkMatrix* localMatrix); + +}; + +#endif -- cgit v1.2.3