diff --git a/src/gpu/GrAARectRenderer.cpp b/src/gpu/GrAARectRenderer.cpp index 420c9e7c5a..dbebd75471 100644 --- a/src/gpu/GrAARectRenderer.cpp +++ b/src/gpu/GrAARectRenderer.cpp @@ -109,16 +109,15 @@ public: bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); SkMatrix localMatrix; - if (!this->viewMatrix().invert(&localMatrix)) { + if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) { SkDebugf("Cannot invert\n"); return; } - const GrGeometryProcessor* gp = create_fill_rect_gp(canTweakAlphaForCoverage, - localMatrix); + SkAutoTUnref gp(create_fill_rect_gp(canTweakAlphaForCoverage, + localMatrix)); batchTarget->initDraw(gp, pipeline); - gp->unref(); // 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 @@ -142,7 +141,7 @@ public: const GrVertexBuffer* vertexBuffer; int firstVertex; - void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, + void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex); @@ -150,13 +149,13 @@ public: 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); + i * kVertsPerAAFillRect * vertexStride, + vertexStride, + args.fColor, + args.fViewMatrix, + args.fRect, + args.fDevRect, + canTweakAlphaForCoverage); } GrDrawTarget::DrawInfo drawInfo; @@ -200,15 +199,10 @@ private: bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { AAFillRectBatch* that = t->cast(); - if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { - return false; - } - if (this->colorIgnored() != that->colorIgnored()) { - return false; - } - - SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); + SkASSERT(this->canTweakAlphaForCoverage() == that->canTweakAlphaForCoverage() && + this->usesLocalCoords() == that->usesLocalCoords() && + this->colorIgnored() == that->colorIgnored()); // 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 @@ -546,25 +540,318 @@ void GrAARectRenderer::strokeAARect(GrDrawTarget* target, devOutsideAssist, devInside, miterStroke); } -static const GrGeometryProcessor* create_rect_gp(const GrPipelineBuilder& pipelneBuilder, - GrColor color, - CoverageAttribType* type, - const SkMatrix& localMatrix) { - uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; - const GrGeometryProcessor* gp; - if (pipelneBuilder.canTweakAlphaForCoverage()) { - gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localMatrix); - SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); - *type = kUseColor_CoverageAttribType; - } else { - flags |= GrDefaultGeoProcFactory::kCoverage_GPType; - gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localMatrix, - GrColorIsOpaque(color)); - SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); - *type = kUseCoverage_CoverageAttribType; +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, + const GrIndexBuffer* indexBuffer) { + return SkNEW_ARGS(AAStrokeRectBatch, (geometry, viewMatrix, indexBuffer)); } - return gp; -} + + const char* name() const SK_OVERRIDE { return "AAStrokeRect"; } + + void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE { + // When this is called on a batch, there is only one geometry bundle + if (!this->canTweakAlphaForCoverage() && GrColorIsOpaque(fGeoData[0].fColor)) { + out->setUnknownOpaqueFourComponents(); + } else { + out->setUnknownFourComponents(); + } + } + + void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE { + if (this->canTweakAlphaForCoverage()) { + // uniform coverage + out->setKnownSingleComponent(0xff); + } else { + out->setUnknownSingleComponent(); + } + } + + void initBatchOpt(const GrBatchOpt& batchOpt) { + fBatchOpt = batchOpt; + } + + void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE { + // Handle any color overrides + if (init.fColorIgnored) { + fGeoData[0].fColor = GrColor_ILLEGAL; + } else if (GrColor_ILLEGAL != init.fOverrideColor) { + fGeoData[0].fColor = init.fOverrideColor; + } + + // setup batch properties + fBatch.fColorIgnored = init.fColorIgnored; + fBatch.fColor = fGeoData[0].fColor; + fBatch.fUsesLocalCoords = init.fUsesLocalCoords; + fBatch.fCoverageIgnored = init.fCoverageIgnored; + fBatch.fMiterStroke = fGeoData[0].fMiterStroke; + } + + void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE { + bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); + + // Local matrix is ignored if we don't have local coords. If we have localcoords we only + // batch with identical view matrices + SkMatrix localMatrix; + if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) { + SkDebugf("Cannot invert\n"); + return; + } + + SkAutoTUnrefgp(create_fill_rect_gp(canTweakAlphaForCoverage, + localMatrix)); + + batchTarget->initDraw(gp, 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 = this->usesLocalCoords(); + gp->initBatchTracker(batchTarget->currentBatchTracker(), init); + + 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 totalVertexNum = (outerVertexNum + innerVertexNum) * 2; + + int instanceCount = fGeoData.count(); + int vertexCount = totalVertexNum * instanceCount; + + const GrVertexBuffer* vertexBuffer; + int firstVertex; + + void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, + vertexCount, + &vertexBuffer, + &firstVertex); + + for (int i = 0; i < instanceCount; i++) { + const Geometry& args = fGeoData[i]; + this->generateAAStrokeRectGeometry(vertices, + i * totalVertexNum * vertexStride, + vertexStride, + outerVertexNum, + innerVertexNum, + args.fColor, + args.fDevOutside, + args.fDevOutsideAssist, + args.fDevInside, + args.fMiterStroke, + canTweakAlphaForCoverage); + } + + GrDrawTarget::DrawInfo drawInfo; + drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); + drawInfo.setStartVertex(0); + drawInfo.setStartIndex(0); + drawInfo.setVerticesPerInstance(totalVertexNum); + drawInfo.setIndicesPerInstance(aa_stroke_rect_index_count(this->miterStroke())); + drawInfo.adjustStartVertex(firstVertex); + drawInfo.setVertexBuffer(vertexBuffer); + drawInfo.setIndexBuffer(fIndexBuffer); + + int maxInstancesPerDraw = kNumBevelStrokeRectsInIndexBuffer; + + while (instanceCount) { + drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)); + drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance()); + drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance()); + + batchTarget->draw(drawInfo); + + drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount()); + instanceCount -= drawInfo.instanceCount(); + } + } + + SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } + +private: + AAStrokeRectBatch(const Geometry& geometry, const SkMatrix& viewMatrix, + const GrIndexBuffer* indexBuffer) + : fIndexBuffer(indexBuffer) { + this->initClassID(); + fBatch.fViewMatrix = viewMatrix; + fGeoData.push_back(geometry); + } + + GrColor color() const { return fBatch.fColor; } + bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } + bool canTweakAlphaForCoverage() const { return fBatchOpt.fCanTweakAlphaForCoverage; } + bool colorIgnored() const { return fBatch.fColorIgnored; } + const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } + bool miterStroke() const { return fBatch.fMiterStroke; } + + bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { + AAStrokeRectBatch* that = t->cast(); + + // TODO batch across miterstroke changes + if (this->miterStroke() != that->miterStroke()) { + return false; + } + + SkASSERT(this->canTweakAlphaForCoverage() == that->canTweakAlphaForCoverage() && + this->usesLocalCoords() == that->usesLocalCoords() && + this->colorIgnored() == that->colorIgnored()); + // 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; + } + fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); + 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(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(verts); + SkPoint* fan1Pos = reinterpret_cast(verts + outerVertexNum * vertexStride); + SkPoint* fan2Pos = reinterpret_cast(verts + 2 * outerVertexNum * vertexStride); + SkPoint* fan3Pos = reinterpret_cast(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(verts + 4 * vertexStride); + SkPoint* fan1AssistPos = reinterpret_cast(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(verts + i * vertexStride) = 0; + } else { + *reinterpret_cast(verts + i * vertexStride) = color; + *reinterpret_cast(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(verts + i * vertexStride) = scaledColor; + } else { + *reinterpret_cast(verts + i * vertexStride) = color; + *reinterpret_cast(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(verts + i * vertexStride) = 0; + } else { + *reinterpret_cast(verts + i * vertexStride) = color; + *reinterpret_cast(verts + i * vertexStride + sizeof(GrColor)) = 0; + } + } + } + + struct BatchTracker { + SkMatrix fViewMatrix; + GrColor fColor; + bool fUsesLocalCoords; + bool fColorIgnored; + bool fCoverageIgnored; + bool fMiterStroke; + }; + + GrBatchOpt fBatchOpt; + BatchTracker fBatch; + const GrIndexBuffer* fIndexBuffer; + SkSTArray<1, Geometry, true> fGeoData; +}; void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, @@ -575,134 +862,21 @@ void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, const SkRect& devOutsideAssist, const SkRect& devInside, bool miterStroke) { - SkMatrix localMatrix; - if (!viewMatrix.invert(&localMatrix)) { - SkDebugf("Cannot invert\n"); - return; - } - - CoverageAttribType type; - SkAutoTUnref gp(create_rect_gp(*pipelineBuilder, color, &type, - localMatrix)); - - int innerVertexNum = 4; - int outerVertexNum = miterStroke ? 4 : 8; - int totalVertexNum = (outerVertexNum + innerVertexNum) * 2; - - size_t vstride = gp->getVertexStride(); - GrDrawTarget::AutoReleaseGeometry geo(target, totalVertexNum, vstride, 0); - if (!geo.succeeded()) { - SkDebugf("Failed to get space for vertices!\n"); - return; - } GrIndexBuffer* indexBuffer = this->aaStrokeRectIndexBuffer(miterStroke); if (NULL == indexBuffer) { SkDebugf("Failed to create index buffer!\n"); return; } - intptr_t verts = reinterpret_cast(geo.vertices()); + AAStrokeRectBatch::Geometry geometry; + geometry.fColor = color; + geometry.fDevOutside = devOutside; + geometry.fDevOutsideAssist = devOutsideAssist; + geometry.fDevInside = devInside; + geometry.fMiterStroke = miterStroke; - // 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(verts); - SkPoint* fan1Pos = reinterpret_cast(verts + outerVertexNum * vstride); - SkPoint* fan2Pos = reinterpret_cast(verts + 2 * outerVertexNum * vstride); - SkPoint* fan3Pos = reinterpret_cast(verts + (2 * outerVertexNum + innerVertexNum) * vstride); - -#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, vstride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); - // inner two - set_inset_fan(fan1Pos, vstride, devOutside, inset, inset); - set_inset_fan(fan2Pos, vstride, devInside, -inset, -inset); - // innermost - set_inset_fan(fan3Pos, vstride, devInside, SK_ScalarHalf, SK_ScalarHalf); - } else { - SkPoint* fan0AssistPos = reinterpret_cast(verts + 4 * vstride); - SkPoint* fan1AssistPos = reinterpret_cast(verts + (outerVertexNum + 4) * vstride); - // outermost - set_inset_fan(fan0Pos, vstride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); - set_inset_fan(fan0AssistPos, vstride, devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf); - // outer one of the inner two - set_inset_fan(fan1Pos, vstride, devOutside, inset, inset); - set_inset_fan(fan1AssistPos, vstride, devOutsideAssist, inset, inset); - // inner one of the inner two - set_inset_fan(fan2Pos, vstride, devInside, -inset, -inset); - // innermost - set_inset_fan(fan3Pos, vstride, 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 (kUseCoverage_CoverageAttribType == type) { - *reinterpret_cast(verts + i * vstride) = color; - *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = 0; - } else { - *reinterpret_cast(verts + i * vstride) = 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 * vstride; - for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) { - if (kUseCoverage_CoverageAttribType == type) { - *reinterpret_cast(verts + i * vstride) = color; - *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = innerCoverage; - } else { - *reinterpret_cast(verts + i * vstride) = scaledColor; - } - } - - // The innermost rect has 0 coverage - verts += (outerVertexNum + innerVertexNum) * vstride; - for (int i = 0; i < innerVertexNum; ++i) { - if (kUseCoverage_CoverageAttribType == type) { - *reinterpret_cast(verts + i * vstride) = color; - *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = 0; - } else { - *reinterpret_cast(verts + i * vstride) = 0; - } - } - - target->setIndexSourceToBuffer(indexBuffer); - target->drawIndexedInstances(pipelineBuilder, - gp, - kTriangles_GrPrimitiveType, - 1, - totalVertexNum, - aa_stroke_rect_index_count(miterStroke)); - target->resetIndexSource(); + SkAutoTUnref batch(AAStrokeRectBatch::Create(geometry, viewMatrix, indexBuffer)); + target->drawBatch(pipelineBuilder, batch); } void GrAARectRenderer::fillAANestedRects(GrDrawTarget* target,