diff options
-rw-r--r-- | gyp/gpu.gypi | 4 | ||||
-rw-r--r-- | src/gpu/GrAARectRenderer.cpp | 726 | ||||
-rw-r--r-- | src/gpu/batches/GrAAFillRectBatch.cpp | 259 | ||||
-rw-r--r-- | src/gpu/batches/GrAAFillRectBatch.h | 91 | ||||
-rw-r--r-- | src/gpu/batches/GrAAStrokeRectBatch.cpp | 380 | ||||
-rw-r--r-- | src/gpu/batches/GrAAStrokeRectBatch.h | 110 | ||||
-rw-r--r-- | src/gpu/batches/GrDrawVerticesBatch.h | 1 |
7 files changed, 850 insertions, 721 deletions
diff --git a/gyp/gpu.gypi b/gyp/gpu.gypi index e8abc25a0a..66ff11673e 100644 --- a/gyp/gpu.gypi +++ b/gyp/gpu.gypi @@ -221,6 +221,10 @@ '<(skia_src_path)/gpu/GrXferProcessor.cpp', # Batches + '<(skia_src_path)/gpu/batches/GrAAFillRectBatch.cpp', + '<(skia_src_path)/gpu/batches/GrAAFillRectBatch.h', + '<(skia_src_path)/gpu/batches/GrAAStrokeRectBatch.cpp', + '<(skia_src_path)/gpu/batches/GrAAStrokeRectBatch.h', '<(skia_src_path)/gpu/batches/GrBatch.cpp', '<(skia_src_path)/gpu/batches/GrBatch.h', '<(skia_src_path)/gpu/batches/GrDrawAtlasBatch.cpp', diff --git a/src/gpu/GrAARectRenderer.cpp b/src/gpu/GrAARectRenderer.cpp index f1f4fd70af..939542f0d9 100644 --- a/src/gpu/GrAARectRenderer.cpp +++ b/src/gpu/GrAARectRenderer.cpp @@ -9,331 +9,34 @@ #include "GrBatchTarget.h" #include "GrBatchTest.h" #include "GrContext.h" -#include "GrDefaultGeoProcFactory.h" #include "GrDrawTarget.h" #include "GrGeometryProcessor.h" #include "GrInvariantOutput.h" -#include "GrResourceKey.h" -#include "GrResourceProvider.h" #include "GrTestUtils.h" #include "GrVertexBuffer.h" #include "SkColorPriv.h" -#include "batches/GrBatch.h" +#include "batches/GrAAFillRectBatch.h" +#include "batches/GrAAStrokeRectBatch.h" #include "gl/GrGLProcessor.h" #include "gl/GrGLGeometryProcessor.h" #include "gl/builders/GrGLProgramBuilder.h" /////////////////////////////////////////////////////////////////////////////// -static void set_inset_fan(SkPoint* pts, size_t stride, - const SkRect& r, SkScalar dx, SkScalar dy) { - pts->setRectFan(r.fLeft + dx, r.fTop + dy, - r.fRight - dx, r.fBottom - dy, stride); -} - -static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage, - const SkMatrix& viewMatrix, - bool usesLocalCoords, - bool coverageIgnored) { - using namespace GrDefaultGeoProcFactory; - - Color color(Color::kAttribute_Type); - Coverage::Type coverageType; - // TODO remove coverage if coverage is ignored - /*if (coverageIgnored) { - coverageType = Coverage::kNone_Type; - } else*/ if (tweakAlphaForCoverage) { - coverageType = Coverage::kSolid_Type; - } else { - coverageType = Coverage::kAttribute_Type; - } - Coverage coverage(coverageType); - LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : - LocalCoords::kUnused_Type); - return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); -} - -GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); - -class AAFillRectBatch : public GrBatch { -public: - struct Geometry { - GrColor fColor; - SkMatrix fViewMatrix; - SkRect fRect; - SkRect fDevRect; - }; - - static GrBatch* Create(const Geometry& geometry) { - return SkNEW_ARGS(AAFillRectBatch, (geometry)); - } - - const char* name() const override { return "AAFillRectBatch"; } - - 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->setUnknownSingleComponent(); - } - - void initBatchTracker(const GrPipelineInfo& init) override { - // Handle any color overrides - if (!init.readsColor()) { - fGeoData[0].fColor = GrColor_ILLEGAL; - } - init.getOverrideColorIfSet(&fGeoData[0].fColor); - - // setup batch properties - fBatch.fColorIgnored = !init.readsColor(); - fBatch.fColor = fGeoData[0].fColor; - fBatch.fUsesLocalCoords = init.readsLocalCoords(); - fBatch.fCoverageIgnored = !init.readsCoverage(); - fBatch.fCanTweakAlphaForCoverage = init.canTweakAlphaForCoverage(); - } - - void generateGeometry(GrBatchTarget* batchTarget) override { - bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); - - SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_rect_gp(canTweakAlphaForCoverage, - this->viewMatrix(), - this->usesLocalCoords(), - this->coverageIgnored())); - if (!gp) { - SkDebugf("Couldn't create GrGeometryProcessor\n"); - return; - } - - batchTarget->initDraw(gp, this->pipeline()); - - size_t vertexStride = gp->getVertexStride(); - SkASSERT(canTweakAlphaForCoverage ? - vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : - vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); - int instanceCount = fGeoData.count(); - - SkAutoTUnref<const GrIndexBuffer> indexBuffer(this->getIndexBuffer( - batchTarget->resourceProvider())); - InstancedHelper helper; - void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, - indexBuffer, kVertsPerAAFillRect, kIndicesPerAAFillRect, - instanceCount); - if (!vertices || !indexBuffer) { - SkDebugf("Could not allocate vertices\n"); - return; - } - - for (int i = 0; i < instanceCount; i++) { - const Geometry& args = fGeoData[i]; - this->generateAAFillRectGeometry(vertices, - i * kVertsPerAAFillRect * vertexStride, - vertexStride, - args.fColor, - args.fViewMatrix, - args.fRect, - args.fDevRect, - canTweakAlphaForCoverage); - } - - helper.issueDraw(batchTarget); - } - - SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } - -private: - AAFillRectBatch(const Geometry& geometry) { - this->initClassID<AAFillRectBatch>(); - fGeoData.push_back(geometry); - - this->setBounds(geometry.fDevRect); - } - - static const int kNumAAFillRectsInIndexBuffer = 256; - static const int kVertsPerAAFillRect = 8; - static const int kIndicesPerAAFillRect = 30; - - const GrIndexBuffer* getIndexBuffer(GrResourceProvider* resourceProvider) { - GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); - - static const uint16_t gFillAARectIdx[] = { - 0, 1, 5, 5, 4, 0, - 1, 2, 6, 6, 5, 1, - 2, 3, 7, 7, 6, 2, - 3, 0, 4, 4, 7, 3, - 4, 5, 6, 6, 7, 4, - }; - GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect); - return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx, - kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFillRect, - gAAFillRectIndexBufferKey); - } - - GrColor color() const { return fBatch.fColor; } - bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } - bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } - bool colorIgnored() const { return fBatch.fColorIgnored; } - const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } - bool coverageIgnored() const { return fBatch.fCoverageIgnored; } - - bool onCombineIfPossible(GrBatch* t) override { - if (!this->pipeline()->isEqual(*t->pipeline())) { - return false; - } - - AAFillRectBatch* that = t->cast<AAFillRectBatch>(); - - SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); - // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses - // local coords then we won't be able to batch. We could actually upload the viewmatrix - // using vertex attributes in these cases, but haven't investigated that - if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { - return false; - } - - if (this->color() != that->color()) { - fBatch.fColor = GrColor_ILLEGAL; - } - - // In the event of two batches, one who can tweak, one who cannot, we just fall back to - // not tweaking - if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { - fBatch.fCanTweakAlphaForCoverage = false; - } - - fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); - this->joinBounds(that->bounds()); - return true; - } - - void generateAAFillRectGeometry(void* vertices, - size_t offset, - size_t vertexStride, - GrColor color, - const SkMatrix& viewMatrix, - const SkRect& rect, - const SkRect& devRect, - bool tweakAlphaForCoverage) const { - intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; - - SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); - SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); - - SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); - inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); - - if (viewMatrix.rectStaysRect()) { - set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); - set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); - } else { - // compute transformed (1, 0) and (0, 1) vectors - SkVector vec[2] = { - { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, - { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } - }; - - vec[0].normalize(); - vec[0].scale(SK_ScalarHalf); - vec[1].normalize(); - vec[1].scale(SK_ScalarHalf); - - // create the rotated rect - fan0Pos->setRectFan(rect.fLeft, rect.fTop, - rect.fRight, rect.fBottom, vertexStride); - viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); - - // Now create the inset points and then outset the original - // rotated points - - // TL - *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = - *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; - // BL - *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = - *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; - // BR - *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = - *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; - // TR - *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = - *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; - } - - // Make verts point to vertex color and then set all the color and coverage vertex attrs - // values. - verts += sizeof(SkPoint); - for (int i = 0; i < 4; ++i) { - if (tweakAlphaForCoverage) { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; - } else { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; - *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; - } - } - - int scale; - if (inset < SK_ScalarHalf) { - scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); - SkASSERT(scale >= 0 && scale <= 255); - } else { - scale = 0xff; - } - - verts += 4 * vertexStride; - - float innerCoverage = GrNormalizeByteToFloat(scale); - GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); - - for (int i = 0; i < 4; ++i) { - if (tweakAlphaForCoverage) { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; - } else { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; - *reinterpret_cast<float*>(verts + i * vertexStride + - sizeof(GrColor)) = innerCoverage; - } - } - } - - struct BatchTracker { - GrColor fColor; - bool fUsesLocalCoords; - bool fColorIgnored; - bool fCoverageIgnored; - bool fCanTweakAlphaForCoverage; - }; - - BatchTracker fBatch; - SkSTArray<1, Geometry, true> fGeoData; -}; - -namespace { -// Should the coverage be multiplied into the color attrib or use a separate attrib. -enum CoverageAttribType { - kUseColor_CoverageAttribType, - kUseCoverage_CoverageAttribType, -}; -} - void GrAARectRenderer::GeometryFillAARect(GrDrawTarget* target, const GrPipelineBuilder& pipelineBuilder, GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, const SkRect& devRect) { - AAFillRectBatch::Geometry geometry; + GrAAFillRectBatch::Geometry geometry; geometry.fRect = rect; geometry.fViewMatrix = viewMatrix; geometry.fDevRect = devRect; geometry.fColor = color; - SkAutoTUnref<GrBatch> batch(AAFillRectBatch::Create(geometry)); + SkAutoTUnref<GrBatch> batch(GrAAFillRectBatch::Create(geometry)); target->drawBatch(pipelineBuilder, batch); } @@ -399,386 +102,6 @@ void GrAARectRenderer::StrokeAARect(GrDrawTarget* target, devOutsideAssist, devInside, miterStroke); } -GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); -GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); - -class AAStrokeRectBatch : public GrBatch { -public: - // TODO support AA rotated stroke rects by copying around view matrices - struct Geometry { - GrColor fColor; - SkRect fDevOutside; - SkRect fDevOutsideAssist; - SkRect fDevInside; - bool fMiterStroke; - }; - - static GrBatch* Create(const Geometry& geometry, const SkMatrix& viewMatrix) { - return SkNEW_ARGS(AAStrokeRectBatch, (geometry, viewMatrix)); - } - - const char* name() const override { return "AAStrokeRect"; } - - 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->setUnknownSingleComponent(); - } - - void initBatchTracker(const GrPipelineInfo& init) override { - // Handle any color overrides - if (!init.readsColor()) { - fGeoData[0].fColor = GrColor_ILLEGAL; - } - init.getOverrideColorIfSet(&fGeoData[0].fColor); - - // setup batch properties - fBatch.fColorIgnored = !init.readsColor(); - fBatch.fColor = fGeoData[0].fColor; - fBatch.fUsesLocalCoords = init.readsLocalCoords(); - fBatch.fCoverageIgnored = !init.readsCoverage(); - fBatch.fMiterStroke = fGeoData[0].fMiterStroke; - fBatch.fCanTweakAlphaForCoverage = init.canTweakAlphaForCoverage(); - } - - void generateGeometry(GrBatchTarget* batchTarget) override { - bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); - - SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_rect_gp(canTweakAlphaForCoverage, - this->viewMatrix(), - this->usesLocalCoords(), - this->coverageIgnored())); - if (!gp) { - SkDebugf("Couldn't create GrGeometryProcessor\n"); - return; - } - - batchTarget->initDraw(gp, this->pipeline()); - - size_t vertexStride = gp->getVertexStride(); - - SkASSERT(canTweakAlphaForCoverage ? - vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : - vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); - int innerVertexNum = 4; - int outerVertexNum = this->miterStroke() ? 4 : 8; - int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; - int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; - int instanceCount = fGeoData.count(); - - const SkAutoTUnref<const GrIndexBuffer> indexBuffer( - GetIndexBuffer(batchTarget->resourceProvider(), this->miterStroke())); - InstancedHelper helper; - void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, - indexBuffer, verticesPerInstance, indicesPerInstance, - instanceCount); - if (!vertices || !indexBuffer) { - SkDebugf("Could not allocate vertices\n"); - return; - } - - for (int i = 0; i < instanceCount; i++) { - const Geometry& args = fGeoData[i]; - this->generateAAStrokeRectGeometry(vertices, - i * verticesPerInstance * vertexStride, - vertexStride, - outerVertexNum, - innerVertexNum, - args.fColor, - args.fDevOutside, - args.fDevOutsideAssist, - args.fDevInside, - args.fMiterStroke, - canTweakAlphaForCoverage); - } - helper.issueDraw(batchTarget); - } - - SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } - -private: - AAStrokeRectBatch(const Geometry& geometry, const SkMatrix& viewMatrix) { - this->initClassID<AAStrokeRectBatch>(); - fBatch.fViewMatrix = viewMatrix; - fGeoData.push_back(geometry); - - // If we have miterstroke then we inset devOutside and outset devOutsideAssist, so we need - // the join for proper bounds - fBounds = geometry.fDevOutside; - fBounds.join(geometry.fDevOutsideAssist); - } - - - static const int kMiterIndexCnt = 3 * 24; - static const int kMiterVertexCnt = 16; - static const int kNumMiterRectsInIndexBuffer = 256; - - static const int kBevelIndexCnt = 48 + 36 + 24; - static const int kBevelVertexCnt = 24; - static const int kNumBevelRectsInIndexBuffer = 256; - - static const GrIndexBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, - bool miterStroke) { - - if (miterStroke) { - static const uint16_t gMiterIndices[] = { - 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, - 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, - 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, - 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, - - 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, - 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, - 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, - 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, - - 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, - 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, - 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, - 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, - }; - GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); - GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); - return resourceProvider->findOrCreateInstancedIndexBuffer(gMiterIndices, - kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, - gMiterIndexBufferKey); - } else { - /** - * As in miter-stroke, index = a + b, and a is the current index, b is the shift - * from the first index. The index layout: - * outer AA line: 0~3, 4~7 - * outer edge: 8~11, 12~15 - * inner edge: 16~19 - * inner AA line: 20~23 - * Following comes a bevel-stroke rect and its indices: - * - * 4 7 - * ********************************* - * * ______________________________ * - * * / 12 15 \ * - * * / \ * - * 0 * |8 16_____________________19 11 | * 3 - * * | | | | * - * * | | **************** | | * - * * | | * 20 23 * | | * - * * | | * * | | * - * * | | * 21 22 * | | * - * * | | **************** | | * - * * | |____________________| | * - * 1 * |9 17 18 10| * 2 - * * \ / * - * * \13 __________________________14/ * - * * * - * ********************************** - * 5 6 - */ - static const uint16_t gBevelIndices[] = { - // Draw outer AA, from outer AA line to outer edge, shift is 0. - 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, - 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, - 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, - 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, - 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, - 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, - 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, - 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, - - // Draw the stroke, from outer edge to inner edge, shift is 8. - 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, - 1 + 8, 5 + 8, 9 + 8, - 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, - 6 + 8, 2 + 8, 10 + 8, - 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, - 3 + 8, 7 + 8, 11 + 8, - 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, - 4 + 8, 0 + 8, 8 + 8, - - // Draw the inner AA, from inner edge to inner AA line, shift is 16. - 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, - 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, - 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, - 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, - }; - GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); - - GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); - return resourceProvider->findOrCreateInstancedIndexBuffer(gBevelIndices, - kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, - gBevelIndexBufferKey); - } - } - - GrColor color() const { return fBatch.fColor; } - bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } - bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } - bool colorIgnored() const { return fBatch.fColorIgnored; } - const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } - bool miterStroke() const { return fBatch.fMiterStroke; } - bool coverageIgnored() const { return fBatch.fCoverageIgnored; } - - bool onCombineIfPossible(GrBatch* t) override { - if (!this->pipeline()->isEqual(*t->pipeline())) { - return false; - } - - AAStrokeRectBatch* that = t->cast<AAStrokeRectBatch>(); - - // TODO batch across miterstroke changes - if (this->miterStroke() != that->miterStroke()) { - return false; - } - - // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses - // local coords then we won't be able to batch. We could actually upload the viewmatrix - // using vertex attributes in these cases, but haven't investigated that - if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { - return false; - } - - // In the event of two batches, one who can tweak, one who cannot, we just fall back to - // not tweaking - if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { - fBatch.fCanTweakAlphaForCoverage = false; - } - - 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; - } - - void generateAAStrokeRectGeometry(void* vertices, - size_t offset, - size_t vertexStride, - int outerVertexNum, - int innerVertexNum, - GrColor color, - const SkRect& devOutside, - const SkRect& devOutsideAssist, - const SkRect& devInside, - bool miterStroke, - bool tweakAlphaForCoverage) const { - intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; - - // We create vertices for four nested rectangles. There are two ramps from 0 to full - // coverage, one on the exterior of the stroke and the other on the interior. - // The following pointers refer to the four rects, from outermost to innermost. - SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); - SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride); - SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride); - SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(verts + - (2 * outerVertexNum + innerVertexNum) * - vertexStride); - - #ifndef SK_IGNORE_THIN_STROKED_RECT_FIX - // TODO: this only really works if the X & Y margins are the same all around - // the rect (or if they are all >= 1.0). - SkScalar inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight); - inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft); - inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop); - if (miterStroke) { - inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom); - } else { - inset = SK_ScalarHalf * SkMinScalar(inset, devOutsideAssist.fBottom - - devInside.fBottom); - } - SkASSERT(inset >= 0); - #else - SkScalar inset = SK_ScalarHalf; - #endif - - if (miterStroke) { - // outermost - set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); - // inner two - set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset); - set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset); - // innermost - set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf); - } else { - SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); - SkPoint* fan1AssistPos = reinterpret_cast<SkPoint*>(verts + - (outerVertexNum + 4) * - vertexStride); - // outermost - set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); - set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf, - -SK_ScalarHalf); - // outer one of the inner two - set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset); - set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset); - // inner one of the inner two - set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset); - // innermost - set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf); - } - - // Make verts point to vertex color and then set all the color and coverage vertex attrs - // values. The outermost rect has 0 coverage - verts += sizeof(SkPoint); - for (int i = 0; i < outerVertexNum; ++i) { - if (tweakAlphaForCoverage) { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; - } else { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; - *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; - } - } - - // scale is the coverage for the the inner two rects. - int scale; - if (inset < SK_ScalarHalf) { - scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); - SkASSERT(scale >= 0 && scale <= 255); - } else { - scale = 0xff; - } - - float innerCoverage = GrNormalizeByteToFloat(scale); - GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); - - verts += outerVertexNum * vertexStride; - for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) { - if (tweakAlphaForCoverage) { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; - } else { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; - *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = - innerCoverage; - } - } - - // The innermost rect has 0 coverage - verts += (outerVertexNum + innerVertexNum) * vertexStride; - for (int i = 0; i < innerVertexNum; ++i) { - if (tweakAlphaForCoverage) { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; - } else { - *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; - *reinterpret_cast<GrColor*>(verts + i * vertexStride + sizeof(GrColor)) = 0; - } - } - } - - struct BatchTracker { - SkMatrix fViewMatrix; - GrColor fColor; - bool fUsesLocalCoords; - bool fColorIgnored; - bool fCoverageIgnored; - bool fMiterStroke; - bool fCanTweakAlphaForCoverage; - }; - - BatchTracker fBatch; - SkSTArray<1, Geometry, true> fGeoData; -}; - void GrAARectRenderer::GeometryStrokeAARect(GrDrawTarget* target, const GrPipelineBuilder& pipelineBuilder, GrColor color, @@ -787,14 +110,14 @@ void GrAARectRenderer::GeometryStrokeAARect(GrDrawTarget* target, const SkRect& devOutsideAssist, const SkRect& devInside, bool miterStroke) { - AAStrokeRectBatch::Geometry geometry; + GrAAStrokeRectBatch::Geometry geometry; geometry.fColor = color; geometry.fDevOutside = devOutside; geometry.fDevOutsideAssist = devOutsideAssist; geometry.fDevInside = devInside; geometry.fMiterStroke = miterStroke; - SkAutoTUnref<GrBatch> batch(AAStrokeRectBatch::Create(geometry, viewMatrix)); + SkAutoTUnref<GrBatch> batch(GrAAStrokeRectBatch::Create(geometry, viewMatrix)); target->drawBatch(pipelineBuilder, batch); } @@ -818,40 +141,3 @@ void GrAARectRenderer::FillAANestedRects(GrDrawTarget* target, GeometryStrokeAARect(target, pipelineBuilder, color, viewMatrix, devOutside, devOutside, devInside, true); } - -/////////////////////////////////////////////////////////////////////////////////////////////////// - -#ifdef GR_TEST_UTILS - -BATCH_TEST_DEFINE(AAFillRectBatch) { - AAFillRectBatch::Geometry geo; - geo.fColor = GrRandomColor(random); - geo.fViewMatrix = GrTest::TestMatrix(random); - geo.fRect = GrTest::TestRect(random); - geo.fDevRect = GrTest::TestRect(random); - return AAFillRectBatch::Create(geo); -} - -BATCH_TEST_DEFINE(AAStrokeRectBatch) { - bool miterStroke = random->nextBool(); - - // Create mock stroke rect - SkRect outside = GrTest::TestRect(random); - SkScalar minDim = SkMinScalar(outside.width(), outside.height()); - SkScalar strokeWidth = minDim * 0.1f; - SkRect outsideAssist = outside; - outsideAssist.outset(strokeWidth, strokeWidth); - SkRect inside = outside; - inside.inset(strokeWidth, strokeWidth); - - AAStrokeRectBatch::Geometry geo; - geo.fColor = GrRandomColor(random); - geo.fDevOutside = outside; - geo.fDevOutsideAssist = outsideAssist; - geo.fDevInside = inside; - geo.fMiterStroke = miterStroke; - - return AAStrokeRectBatch::Create(geo, GrTest::TestMatrix(random)); -} - -#endif diff --git a/src/gpu/batches/GrAAFillRectBatch.cpp b/src/gpu/batches/GrAAFillRectBatch.cpp new file mode 100644 index 0000000000..4ea4908d03 --- /dev/null +++ b/src/gpu/batches/GrAAFillRectBatch.cpp @@ -0,0 +1,259 @@ +/* + * 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 "GrAAFillRectBatch.h" + +#include "GrDefaultGeoProcFactory.h" +#include "GrResourceKey.h" +#include "GrResourceProvider.h" + +GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); + +static void set_inset_fan(SkPoint* pts, size_t stride, + const SkRect& r, SkScalar dx, SkScalar dy) { + pts->setRectFan(r.fLeft + dx, r.fTop + dy, + r.fRight - dx, r.fBottom - dy, stride); +} + +static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage, + const SkMatrix& viewMatrix, + bool usesLocalCoords, + bool coverageIgnored) { + using namespace GrDefaultGeoProcFactory; + + Color color(Color::kAttribute_Type); + Coverage::Type coverageType; + // TODO remove coverage if coverage is ignored + /*if (coverageIgnored) { + coverageType = Coverage::kNone_Type; + } else*/ if (tweakAlphaForCoverage) { + coverageType = Coverage::kSolid_Type; + } else { + coverageType = Coverage::kAttribute_Type; + } + Coverage coverage(coverageType); + LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : + LocalCoords::kUnused_Type); + return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); +} + +void GrAAFillRectBatch::initBatchTracker(const GrPipelineInfo& init) { + // Handle any color overrides + if (!init.readsColor()) { + fGeoData[0].fColor = GrColor_ILLEGAL; + } + init.getOverrideColorIfSet(&fGeoData[0].fColor); + + // setup batch properties + fBatch.fColorIgnored = !init.readsColor(); + fBatch.fColor = fGeoData[0].fColor; + fBatch.fUsesLocalCoords = init.readsLocalCoords(); + fBatch.fCoverageIgnored = !init.readsCoverage(); + fBatch.fCanTweakAlphaForCoverage = init.canTweakAlphaForCoverage(); +} + +void GrAAFillRectBatch::generateGeometry(GrBatchTarget* batchTarget) { + bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); + + SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_rect_gp(canTweakAlphaForCoverage, + this->viewMatrix(), + this->usesLocalCoords(), + this->coverageIgnored())); + if (!gp) { + SkDebugf("Couldn't create GrGeometryProcessor\n"); + return; + } + + batchTarget->initDraw(gp, this->pipeline()); + + size_t vertexStride = gp->getVertexStride(); + SkASSERT(canTweakAlphaForCoverage ? + vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : + vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); + int instanceCount = fGeoData.count(); + + SkAutoTUnref<const GrIndexBuffer> indexBuffer(this->getIndexBuffer( + batchTarget->resourceProvider())); + InstancedHelper helper; + void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, + indexBuffer, kVertsPerAAFillRect, kIndicesPerAAFillRect, + instanceCount); + if (!vertices || !indexBuffer) { + SkDebugf("Could not allocate vertices\n"); + return; + } + + for (int i = 0; i < instanceCount; i++) { + const Geometry& args = fGeoData[i]; + this->generateAAFillRectGeometry(vertices, + i * kVertsPerAAFillRect * vertexStride, + vertexStride, + args.fColor, + args.fViewMatrix, + args.fRect, + args.fDevRect, + canTweakAlphaForCoverage); + } + + helper.issueDraw(batchTarget); +} + +const GrIndexBuffer* GrAAFillRectBatch::getIndexBuffer(GrResourceProvider* resourceProvider) { + GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); + + static const uint16_t gFillAARectIdx[] = { + 0, 1, 5, 5, 4, 0, + 1, 2, 6, 6, 5, 1, + 2, 3, 7, 7, 6, 2, + 3, 0, 4, 4, 7, 3, + 4, 5, 6, 6, 7, 4, + }; + GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect); + return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx, + kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFillRect, + gAAFillRectIndexBufferKey); +} + +bool GrAAFillRectBatch::onCombineIfPossible(GrBatch* t) { + if (!this->pipeline()->isEqual(*t->pipeline())) { + return false; + } + + GrAAFillRectBatch* that = t->cast<GrAAFillRectBatch>(); + + SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); + // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses + // local coords then we won't be able to batch. We could actually upload the viewmatrix + // using vertex attributes in these cases, but haven't investigated that + if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { + return false; + } + + if (this->color() != that->color()) { + fBatch.fColor = GrColor_ILLEGAL; + } + + // In the event of two batches, one who can tweak, one who cannot, we just fall back to + // not tweaking + if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { + fBatch.fCanTweakAlphaForCoverage = false; + } + + fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); + this->joinBounds(that->bounds()); + return true; +} + +void GrAAFillRectBatch::generateAAFillRectGeometry(void* vertices, + size_t offset, + size_t vertexStride, + GrColor color, + const SkMatrix& viewMatrix, + const SkRect& rect, + const SkRect& devRect, + bool tweakAlphaForCoverage) const { + intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; + + SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); + SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); + + SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); + inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); + + if (viewMatrix.rectStaysRect()) { + set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); + set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); + } else { + // compute transformed (1, 0) and (0, 1) vectors + SkVector vec[2] = { + { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, + { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } + }; + + vec[0].normalize(); + vec[0].scale(SK_ScalarHalf); + vec[1].normalize(); + vec[1].scale(SK_ScalarHalf); + + // create the rotated rect + fan0Pos->setRectFan(rect.fLeft, rect.fTop, + rect.fRight, rect.fBottom, vertexStride); + viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); + + // Now create the inset points and then outset the original + // rotated points + + // TL + *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = + *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; + // BL + *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = + *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; + // BR + *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = + *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; + // TR + *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = + *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; + } + + // Make verts point to vertex color and then set all the color and coverage vertex attrs + // values. + verts += sizeof(SkPoint); + for (int i = 0; i < 4; ++i) { + if (tweakAlphaForCoverage) { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; + } else { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; + *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; + } + } + + int scale; + if (inset < SK_ScalarHalf) { + scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); + SkASSERT(scale >= 0 && scale <= 255); + } else { + scale = 0xff; + } + + verts += 4 * vertexStride; + + float innerCoverage = GrNormalizeByteToFloat(scale); + GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); + + for (int i = 0; i < 4; ++i) { + if (tweakAlphaForCoverage) { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; + } else { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; + *reinterpret_cast<float*>(verts + i * vertexStride + + sizeof(GrColor)) = innerCoverage; + } + } +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifdef GR_TEST_UTILS + +#include "GrBatchTest.h" + +BATCH_TEST_DEFINE(AAFillRectBatch) { + GrAAFillRectBatch::Geometry geo; + geo.fColor = GrRandomColor(random); + geo.fViewMatrix = GrTest::TestMatrix(random); + geo.fRect = GrTest::TestRect(random); + geo.fDevRect = GrTest::TestRect(random); + return GrAAFillRectBatch::Create(geo); +} + +#endif diff --git a/src/gpu/batches/GrAAFillRectBatch.h b/src/gpu/batches/GrAAFillRectBatch.h new file mode 100644 index 0000000000..c852eb1002 --- /dev/null +++ b/src/gpu/batches/GrAAFillRectBatch.h @@ -0,0 +1,91 @@ +/* + * 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 GrAAFillRectBatch_DEFINED +#define GrAAFillRectBatch_DEFINED + +#include "GrBatch.h" +#include "GrColor.h" +#include "GrTypes.h" +#include "SkMatrix.h" +#include "SkRect.h" + +class GrAAFillRectBatch : public GrBatch { +public: + struct Geometry { + GrColor fColor; + SkMatrix fViewMatrix; + SkRect fRect; + SkRect fDevRect; + }; + + static GrBatch* Create(const Geometry& geometry) { + return SkNEW_ARGS(GrAAFillRectBatch, (geometry)); + } + + const char* name() const override { return "AAFillRectBatch"; } + + 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->setUnknownSingleComponent(); + } + + void initBatchTracker(const GrPipelineInfo& init) override; + + void generateGeometry(GrBatchTarget* batchTarget) override; + + SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } + +private: + GrAAFillRectBatch(const Geometry& geometry) { + this->initClassID<GrAAFillRectBatch>(); + fGeoData.push_back(geometry); + + this->setBounds(geometry.fDevRect); + } + + static const int kNumAAFillRectsInIndexBuffer = 256; + static const int kVertsPerAAFillRect = 8; + static const int kIndicesPerAAFillRect = 30; + + const GrIndexBuffer* getIndexBuffer(GrResourceProvider* resourceProvider); + + GrColor color() const { return fBatch.fColor; } + bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } + bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } + bool colorIgnored() const { return fBatch.fColorIgnored; } + const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } + bool coverageIgnored() const { return fBatch.fCoverageIgnored; } + + bool onCombineIfPossible(GrBatch* t) override; + + void generateAAFillRectGeometry(void* vertices, + size_t offset, + size_t vertexStride, + GrColor color, + const SkMatrix& viewMatrix, + const SkRect& rect, + const SkRect& devRect, + bool tweakAlphaForCoverage) const; + + struct BatchTracker { + GrColor fColor; + bool fUsesLocalCoords; + bool fColorIgnored; + bool fCoverageIgnored; + bool fCanTweakAlphaForCoverage; + }; + + BatchTracker fBatch; + SkSTArray<1, Geometry, true> fGeoData; +}; + +#endif diff --git a/src/gpu/batches/GrAAStrokeRectBatch.cpp b/src/gpu/batches/GrAAStrokeRectBatch.cpp new file mode 100644 index 0000000000..49329b2a94 --- /dev/null +++ b/src/gpu/batches/GrAAStrokeRectBatch.cpp @@ -0,0 +1,380 @@ +/* + * 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 "GrAAStrokeRectBatch.h" + +#include "GrDefaultGeoProcFactory.h" +#include "GrResourceKey.h" +#include "GrResourceProvider.h" + +GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); +GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); + +static void set_inset_fan(SkPoint* pts, size_t stride, + const SkRect& r, SkScalar dx, SkScalar dy) { + pts->setRectFan(r.fLeft + dx, r.fTop + dy, + r.fRight - dx, r.fBottom - dy, stride); +} + +static const GrGeometryProcessor* create_stroke_rect_gp(bool tweakAlphaForCoverage, + const SkMatrix& viewMatrix, + bool usesLocalCoords, + bool coverageIgnored) { + using namespace GrDefaultGeoProcFactory; + + Color color(Color::kAttribute_Type); + Coverage::Type coverageType; + // TODO remove coverage if coverage is ignored + /*if (coverageIgnored) { + coverageType = Coverage::kNone_Type; + } else*/ if (tweakAlphaForCoverage) { + coverageType = Coverage::kSolid_Type; + } else { + coverageType = Coverage::kAttribute_Type; + } + Coverage coverage(coverageType); + LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : + LocalCoords::kUnused_Type); + return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); +} + + +void GrAAStrokeRectBatch::initBatchTracker(const GrPipelineInfo& init) { + // Handle any color overrides + if (!init.readsColor()) { + fGeoData[0].fColor = GrColor_ILLEGAL; + } + init.getOverrideColorIfSet(&fGeoData[0].fColor); + + // setup batch properties + fBatch.fColorIgnored = !init.readsColor(); + fBatch.fColor = fGeoData[0].fColor; + fBatch.fUsesLocalCoords = init.readsLocalCoords(); + fBatch.fCoverageIgnored = !init.readsCoverage(); + fBatch.fMiterStroke = fGeoData[0].fMiterStroke; + fBatch.fCanTweakAlphaForCoverage = init.canTweakAlphaForCoverage(); +} + +void GrAAStrokeRectBatch::generateGeometry(GrBatchTarget* batchTarget) { + bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); + + SkAutoTUnref<const GrGeometryProcessor> gp(create_stroke_rect_gp(canTweakAlphaForCoverage, + this->viewMatrix(), + this->usesLocalCoords(), + this->coverageIgnored())); + if (!gp) { + SkDebugf("Couldn't create GrGeometryProcessor\n"); + return; + } + + batchTarget->initDraw(gp, this->pipeline()); + + size_t vertexStride = gp->getVertexStride(); + + SkASSERT(canTweakAlphaForCoverage ? + vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : + vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); + int innerVertexNum = 4; + int outerVertexNum = this->miterStroke() ? 4 : 8; + int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; + int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; + int instanceCount = fGeoData.count(); + + const SkAutoTUnref<const GrIndexBuffer> indexBuffer( + GetIndexBuffer(batchTarget->resourceProvider(), this->miterStroke())); + InstancedHelper helper; + void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, + indexBuffer, verticesPerInstance, indicesPerInstance, + instanceCount); + if (!vertices || !indexBuffer) { + SkDebugf("Could not allocate vertices\n"); + return; + } + + for (int i = 0; i < instanceCount; i++) { + const Geometry& args = fGeoData[i]; + this->generateAAStrokeRectGeometry(vertices, + i * verticesPerInstance * vertexStride, + vertexStride, + outerVertexNum, + innerVertexNum, + args.fColor, + args.fDevOutside, + args.fDevOutsideAssist, + args.fDevInside, + args.fMiterStroke, + canTweakAlphaForCoverage); + } + helper.issueDraw(batchTarget); +} + +const GrIndexBuffer* GrAAStrokeRectBatch::GetIndexBuffer(GrResourceProvider* resourceProvider, + bool miterStroke) { + + if (miterStroke) { + static const uint16_t gMiterIndices[] = { + 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, + 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, + 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, + 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, + + 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, + 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, + 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, + 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, + + 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, + 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, + 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, + 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, + }; + GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); + GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); + return resourceProvider->findOrCreateInstancedIndexBuffer(gMiterIndices, + kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, + gMiterIndexBufferKey); + } else { + /** + * As in miter-stroke, index = a + b, and a is the current index, b is the shift + * from the first index. The index layout: + * outer AA line: 0~3, 4~7 + * outer edge: 8~11, 12~15 + * inner edge: 16~19 + * inner AA line: 20~23 + * Following comes a bevel-stroke rect and its indices: + * + * 4 7 + * ********************************* + * * ______________________________ * + * * / 12 15 \ * + * * / \ * + * 0 * |8 16_____________________19 11 | * 3 + * * | | | | * + * * | | **************** | | * + * * | | * 20 23 * | | * + * * | | * * | | * + * * | | * 21 22 * | | * + * * | | **************** | | * + * * | |____________________| | * + * 1 * |9 17 18 10| * 2 + * * \ / * + * * \13 __________________________14/ * + * * * + * ********************************** + * 5 6 + */ + static const uint16_t gBevelIndices[] = { + // Draw outer AA, from outer AA line to outer edge, shift is 0. + 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, + 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, + 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, + 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, + 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, + 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, + 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, + 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, + + // Draw the stroke, from outer edge to inner edge, shift is 8. + 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, + 1 + 8, 5 + 8, 9 + 8, + 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, + 6 + 8, 2 + 8, 10 + 8, + 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, + 3 + 8, 7 + 8, 11 + 8, + 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, + 4 + 8, 0 + 8, 8 + 8, + + // Draw the inner AA, from inner edge to inner AA line, shift is 16. + 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, + 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, + 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, + 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, + }; + GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); + + GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); + return resourceProvider->findOrCreateInstancedIndexBuffer(gBevelIndices, + kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, + gBevelIndexBufferKey); + } +} + +bool GrAAStrokeRectBatch::onCombineIfPossible(GrBatch* t) { + if (!this->pipeline()->isEqual(*t->pipeline())) { + return false; + } + + GrAAStrokeRectBatch* that = t->cast<GrAAStrokeRectBatch>(); + + // TODO batch across miterstroke changes + if (this->miterStroke() != that->miterStroke()) { + return false; + } + + // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses + // local coords then we won't be able to batch. We could actually upload the viewmatrix + // using vertex attributes in these cases, but haven't investigated that + if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { + return false; + } + + // In the event of two batches, one who can tweak, one who cannot, we just fall back to + // not tweaking + if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { + fBatch.fCanTweakAlphaForCoverage = false; + } + + 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; +} + +void GrAAStrokeRectBatch::generateAAStrokeRectGeometry(void* vertices, + size_t offset, + size_t vertexStride, + int outerVertexNum, + int innerVertexNum, + GrColor color, + const SkRect& devOutside, + const SkRect& devOutsideAssist, + const SkRect& devInside, + bool miterStroke, + bool tweakAlphaForCoverage) const { + intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; + + // We create vertices for four nested rectangles. There are two ramps from 0 to full + // coverage, one on the exterior of the stroke and the other on the interior. + // The following pointers refer to the four rects, from outermost to innermost. + SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); + SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride); + SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride); + SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(verts + + (2 * outerVertexNum + innerVertexNum) * + vertexStride); + +#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX + // TODO: this only really works if the X & Y margins are the same all around + // the rect (or if they are all >= 1.0). + SkScalar inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight); + inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft); + inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop); + if (miterStroke) { + inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom); + } else { + inset = SK_ScalarHalf * SkMinScalar(inset, devOutsideAssist.fBottom - + devInside.fBottom); + } + SkASSERT(inset >= 0); +#else + SkScalar inset = SK_ScalarHalf; +#endif + + if (miterStroke) { + // outermost + set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); + // inner two + set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset); + set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset); + // innermost + set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf); + } else { + SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); + SkPoint* fan1AssistPos = reinterpret_cast<SkPoint*>(verts + + (outerVertexNum + 4) * + vertexStride); + // outermost + set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); + set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf, + -SK_ScalarHalf); + // outer one of the inner two + set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset); + set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset); + // inner one of the inner two + set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset); + // innermost + set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf); + } + + // Make verts point to vertex color and then set all the color and coverage vertex attrs + // values. The outermost rect has 0 coverage + verts += sizeof(SkPoint); + for (int i = 0; i < outerVertexNum; ++i) { + if (tweakAlphaForCoverage) { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; + } else { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; + *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; + } + } + + // scale is the coverage for the the inner two rects. + int scale; + if (inset < SK_ScalarHalf) { + scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); + SkASSERT(scale >= 0 && scale <= 255); + } else { + scale = 0xff; + } + + float innerCoverage = GrNormalizeByteToFloat(scale); + GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); + + verts += outerVertexNum * vertexStride; + for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) { + if (tweakAlphaForCoverage) { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; + } else { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; + *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = + innerCoverage; + } + } + + // The innermost rect has 0 coverage + verts += (outerVertexNum + innerVertexNum) * vertexStride; + for (int i = 0; i < innerVertexNum; ++i) { + if (tweakAlphaForCoverage) { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; + } else { + *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; + *reinterpret_cast<GrColor*>(verts + i * vertexStride + sizeof(GrColor)) = 0; + } + } +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifdef GR_TEST_UTILS + +#include "GrBatchTest.h" + +BATCH_TEST_DEFINE(AAStrokeRectBatch) { + bool miterStroke = random->nextBool(); + + // Create mock stroke rect + SkRect outside = GrTest::TestRect(random); + SkScalar minDim = SkMinScalar(outside.width(), outside.height()); + SkScalar strokeWidth = minDim * 0.1f; + SkRect outsideAssist = outside; + outsideAssist.outset(strokeWidth, strokeWidth); + SkRect inside = outside; + inside.inset(strokeWidth, strokeWidth); + + GrAAStrokeRectBatch::Geometry geo; + geo.fColor = GrRandomColor(random); + geo.fDevOutside = outside; + geo.fDevOutsideAssist = outsideAssist; + geo.fDevInside = inside; + geo.fMiterStroke = miterStroke; + + return GrAAStrokeRectBatch::Create(geo, GrTest::TestMatrix(random)); +} + +#endif diff --git a/src/gpu/batches/GrAAStrokeRectBatch.h b/src/gpu/batches/GrAAStrokeRectBatch.h new file mode 100644 index 0000000000..1e15711f19 --- /dev/null +++ b/src/gpu/batches/GrAAStrokeRectBatch.h @@ -0,0 +1,110 @@ +/* + * 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 GrAAStrokeRectBatch_DEFINED +#define GrAAStrokeRectBatch_DEFINED + +#include "GrBatch.h" +#include "GrColor.h" +#include "GrTypes.h" +#include "SkMatrix.h" +#include "SkRect.h" + +class GrAAStrokeRectBatch : public GrBatch { +public: + // TODO support AA rotated stroke rects by copying around view matrices + struct Geometry { + GrColor fColor; + SkRect fDevOutside; + SkRect fDevOutsideAssist; + SkRect fDevInside; + bool fMiterStroke; + }; + + static GrBatch* Create(const Geometry& geometry, const SkMatrix& viewMatrix) { + return SkNEW_ARGS(GrAAStrokeRectBatch, (geometry, viewMatrix)); + } + + const char* name() const override { return "AAStrokeRect"; } + + 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->setUnknownSingleComponent(); + } + + void initBatchTracker(const GrPipelineInfo& init) override; + + void generateGeometry(GrBatchTarget* batchTarget) override; + + SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } + +private: + GrAAStrokeRectBatch(const Geometry& geometry, const SkMatrix& viewMatrix) { + this->initClassID<GrAAStrokeRectBatch>(); + fBatch.fViewMatrix = viewMatrix; + fGeoData.push_back(geometry); + + // If we have miterstroke then we inset devOutside and outset devOutsideAssist, so we need + // the join for proper bounds + fBounds = geometry.fDevOutside; + fBounds.join(geometry.fDevOutsideAssist); + } + + + static const int kMiterIndexCnt = 3 * 24; + static const int kMiterVertexCnt = 16; + static const int kNumMiterRectsInIndexBuffer = 256; + + static const int kBevelIndexCnt = 48 + 36 + 24; + static const int kBevelVertexCnt = 24; + static const int kNumBevelRectsInIndexBuffer = 256; + + static const GrIndexBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, + bool miterStroke); + + GrColor color() const { return fBatch.fColor; } + bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } + bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } + bool colorIgnored() const { return fBatch.fColorIgnored; } + const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } + bool miterStroke() const { return fBatch.fMiterStroke; } + bool coverageIgnored() const { return fBatch.fCoverageIgnored; } + + bool onCombineIfPossible(GrBatch* t) override; + + void generateAAStrokeRectGeometry(void* vertices, + size_t offset, + size_t vertexStride, + int outerVertexNum, + int innerVertexNum, + GrColor color, + const SkRect& devOutside, + const SkRect& devOutsideAssist, + const SkRect& devInside, + bool miterStroke, + bool tweakAlphaForCoverage) const; + + struct BatchTracker { + SkMatrix fViewMatrix; + GrColor fColor; + bool fUsesLocalCoords; + bool fColorIgnored; + bool fCoverageIgnored; + bool fMiterStroke; + bool fCanTweakAlphaForCoverage; + }; + + BatchTracker fBatch; + SkSTArray<1, Geometry, true> fGeoData; +}; + + +#endif diff --git a/src/gpu/batches/GrDrawVerticesBatch.h b/src/gpu/batches/GrDrawVerticesBatch.h index 86ce241116..6441162253 100644 --- a/src/gpu/batches/GrDrawVerticesBatch.h +++ b/src/gpu/batches/GrDrawVerticesBatch.h @@ -15,7 +15,6 @@ #include "SkRect.h" #include "SkTDArray.h" -class GrBatch; class GrBatchTarget; struct GrInitInvariantOutput; |