Create GrRectBatchFactory

BUG=skia:

Review URL: https://codereview.chromium.org/1287433003
This commit is contained in:
joshualitt 2015-08-10 10:08:26 -07:00 committed by Commit bot
parent 9d144ac15f
commit ecd1a69fbf
6 changed files with 282 additions and 249 deletions

View File

@ -233,6 +233,8 @@
'<(skia_src_path)/gpu/batches/GrDrawVerticesBatch.h',
'<(skia_src_path)/gpu/batches/GrRectBatch.h',
'<(skia_src_path)/gpu/batches/GrRectBatch.cpp',
'<(skia_src_path)/gpu/batches/GrRectBatchFactory.h',
'<(skia_src_path)/gpu/batches/GrRectBatchFactory.cpp',
'<(skia_src_path)/gpu/batches/GrStrokeRectBatch.cpp',
'<(skia_src_path)/gpu/batches/GrStrokeRectBatch.h',

View File

@ -22,7 +22,7 @@
#include "GrVertexBuffer.h"
#include "batches/GrBatch.h"
#include "batches/GrRectBatch.h"
#include "batches/GrRectBatchFactory.h"
#include "SkStrokeRec.h"
@ -294,7 +294,7 @@ void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder,
const SkRect& rect,
const SkRect* localRect,
const SkMatrix* localMatrix) {
SkAutoTUnref<GrBatch> batch(GrRectBatch::Create(color, viewMatrix, rect, localRect,
SkAutoTUnref<GrBatch> batch(GrRectBatchFactory::Create(color, viewMatrix, rect, localRect,
localMatrix));
this->drawBatch(pipelineBuilder, batch);
}

View File

@ -7,40 +7,11 @@
#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));
}
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 GrRectBatch::initBatchTracker(const GrPipelineInfo& init) {
// Handle any color overrides
if (!init.readsColor()) {
fGeoData[0].fColor = GrColor_ILLEGAL;
@ -52,9 +23,9 @@ public:
fBatch.fColor = fGeoData[0].fColor;
fBatch.fUsesLocalCoords = init.readsLocalCoords();
fBatch.fCoverageIgnored = !init.readsCoverage();
}
}
void generateGeometry(GrBatchTarget* batchTarget) override {
void GrRectBatch::generateGeometry(GrBatchTarget* batchTarget) {
SkAutoTUnref<const GrGeometryProcessor> gp(this->createRectGP());
if (!gp) {
SkDebugf("Could not create GrGeometryProcessor\n");
@ -107,34 +78,14 @@ public:
}
helper.issueDraw(batchTarget);
}
}
SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
private:
RectBatch(const Geometry& geometry) {
this->initClassID<RectBatch>();
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 {
bool GrRectBatch::onCombineIfPossible(GrBatch* t) {
if (!this->pipeline()->isEqual(*t->pipeline())) {
return false;
}
RectBatch* that = t->cast<RectBatch>();
GrRectBatch* that = t->cast<GrRectBatch>();
if (this->hasLocalRect() != that->hasLocalRect()) {
return false;
@ -161,9 +112,9 @@ private:
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 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
@ -174,7 +125,7 @@ private:
The vertex attrib order is always pos, color, [local coords].
*/
const GrGeometryProcessor* createRectGP() {
const GrGeometryProcessor* GrRectBatch::createRectGP() {
using namespace GrDefaultGeoProcFactory;
Color color(Color::kAttribute_Type);
Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Coverage::kSolid_Type);
@ -190,79 +141,34 @@ private:
return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords,
this->viewMatrix());
}
}
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;
}
if (localMatrix) {
geometry.fHasLocalMatrix = true;
geometry.fLocalMatrix = *localMatrix;
} else {
geometry.fHasLocalMatrix = false;
}
return RectBatch::Create(geometry);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef GR_TEST_UTILS
#include "GrBatchTest.h"
BATCH_TEST_DEFINE(RectBatch) {
GrColor color = GrRandomColor(random);
GrRectBatch::Geometry geometry;
geometry.fColor = GrRandomColor(random);
SkRect rect = GrTest::TestRect(random);
SkRect localRect;
bool hasLocalRect = random->nextBool();
bool hasLocalMatrix = random->nextBool();
geometry.fRect = GrTest::TestRect(random);
geometry.fHasLocalRect = random->nextBool();
SkMatrix viewMatrix;
SkMatrix localMatrix;
if (hasLocalRect) {
viewMatrix = GrTest::TestMatrixInvertible(random);
localRect = GrTest::TestRect(random);
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

View File

@ -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;
};
GrBatch* Create(GrColor color,
const SkMatrix& viewMatrix,
const SkRect& rect,
const SkRect* localRect,
const SkMatrix* localMatrix);
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<GrRectBatch>();
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();
struct BatchTracker {
GrColor fColor;
bool fUsesLocalCoords;
bool fColorIgnored;
bool fCoverageIgnored;
};
BatchTracker fBatch;
SkSTArray<1, Geometry, true> fGeoData;
};
#endif

View File

@ -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);
}
};

View File

@ -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