diff --git a/src/gpu/ccpr/GrCCClipPath.cpp b/src/gpu/ccpr/GrCCClipPath.cpp index 6b8a96cad3..9b8a73752c 100644 --- a/src/gpu/ccpr/GrCCClipPath.cpp +++ b/src/gpu/ccpr/GrCCClipPath.cpp @@ -48,11 +48,12 @@ void GrCCClipPath::init(GrProxyProvider* proxyProvider, fAccessRect = accessRect; } -void GrCCClipPath::placePathInAtlas(GrCCPerFlushResources* resources, - GrOnFlushResourceProvider* onFlushRP) { +void GrCCClipPath::renderPathInAtlas(GrCCPerFlushResources* resources, + GrOnFlushResourceProvider* onFlushRP) { SkASSERT(this->isInitialized()); SkASSERT(!fHasAtlas); - fAtlas = resources->addDeviceSpacePathToAtlas(*onFlushRP->caps(), fAccessRect, fDeviceSpacePath, - fPathDevIBounds, &fAtlasOffsetX, &fAtlasOffsetY); + fAtlas = resources->renderDeviceSpacePathInAtlas(*onFlushRP->caps(), fAccessRect, + fDeviceSpacePath, fPathDevIBounds, + &fAtlasOffsetX, &fAtlasOffsetY); SkDEBUGCODE(fHasAtlas = true); } diff --git a/src/gpu/ccpr/GrCCClipPath.h b/src/gpu/ccpr/GrCCClipPath.h index 4b4ad75fc5..290ac29ee4 100644 --- a/src/gpu/ccpr/GrCCClipPath.h +++ b/src/gpu/ccpr/GrCCClipPath.h @@ -55,7 +55,7 @@ public: return fPathDevIBounds; } - void placePathInAtlas(GrCCPerFlushResources*, GrOnFlushResourceProvider*); + void renderPathInAtlas(GrCCPerFlushResources*, GrOnFlushResourceProvider*); const SkVector& atlasScale() const { SkASSERT(fHasAtlasTransform); return fAtlasScale; } const SkVector& atlasTranslate() const { SkASSERT(fHasAtlasTransform); return fAtlasTranslate; } diff --git a/src/gpu/ccpr/GrCCDrawPathsOp.cpp b/src/gpu/ccpr/GrCCDrawPathsOp.cpp index b3fd711a69..39c1b26223 100644 --- a/src/gpu/ccpr/GrCCDrawPathsOp.cpp +++ b/src/gpu/ccpr/GrCCDrawPathsOp.cpp @@ -60,8 +60,7 @@ bool GrCCDrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps& caps) { SkASSERT(!that->fOwningPerOpListPaths || that->fOwningPerOpListPaths == fOwningPerOpListPaths); SkASSERT(that->fNumDraws); - if (this->getFillType() != that->getFillType() || fSRGBFlags != that->fSRGBFlags || - fProcessors != that->fProcessors || + if (fSRGBFlags != that->fSRGBFlags || fProcessors != that->fProcessors || fViewMatrixIfUsingLocalCoords != that->fViewMatrixIfUsingLocalCoords) { return false; } @@ -95,35 +94,35 @@ void GrCCDrawPathsOp::setupResources(GrCCPerFlushResources* resources, const GrCCAtlas* currentAtlas = nullptr; SkASSERT(fNumDraws > 0); SkASSERT(-1 == fBaseInstance); - fBaseInstance = resources->pathInstanceCount(); + fBaseInstance = resources->nextPathInstanceIdx(); for (const SingleDraw& draw : fDraws) { - // addPathToAtlas gives us two tight bounding boxes: one in device space, as well as a + // renderPathInAtlas gives us two tight bounding boxes: one in device space, as well as a // second one rotated an additional 45 degrees. The path vertex shader uses these two // bounding boxes to generate an octagon that circumscribes the path. SkRect devBounds, devBounds45; int16_t atlasOffsetX, atlasOffsetY; - GrCCAtlas* atlas = resources->addPathToAtlas(*onFlushRP->caps(), draw.fClipIBounds, - draw.fMatrix, draw.fPath, &devBounds, - &devBounds45, &atlasOffsetX, &atlasOffsetY); + GrCCAtlas* atlas = resources->renderPathInAtlas(*onFlushRP->caps(), draw.fClipIBounds, + draw.fMatrix, draw.fPath, &devBounds, + &devBounds45, &atlasOffsetX, &atlasOffsetY); if (!atlas) { SkDEBUGCODE(++fNumSkippedInstances); continue; } if (currentAtlas != atlas) { if (currentAtlas) { - this->addAtlasBatch(currentAtlas, resources->pathInstanceCount()); + this->addAtlasBatch(currentAtlas, resources->nextPathInstanceIdx()); } currentAtlas = atlas; } - resources->appendDrawPathInstance() = - {devBounds, devBounds45, {{atlasOffsetX, atlasOffsetY}}, draw.fColor}; + resources->appendDrawPathInstance().set(draw.fPath.getFillType(), devBounds, devBounds45, + atlasOffsetX, atlasOffsetY, draw.fColor); } - SkASSERT(resources->pathInstanceCount() == fBaseInstance + fNumDraws - fNumSkippedInstances); + SkASSERT(resources->nextPathInstanceIdx() == fBaseInstance + fNumDraws - fNumSkippedInstances); if (currentAtlas) { - this->addAtlasBatch(currentAtlas, resources->pathInstanceCount()); + this->addAtlasBatch(currentAtlas, resources->nextPathInstanceIdx()); } } @@ -156,7 +155,7 @@ void GrCCDrawPathsOp::onExecute(GrOpFlushState* flushState) { } GrCCPathProcessor pathProc(flushState->resourceProvider(), - sk_ref_sp(batch.fAtlas->textureProxy()), this->getFillType(), + sk_ref_sp(batch.fAtlas->textureProxy()), fViewMatrixIfUsingLocalCoords); pathProc.drawPaths(flushState, pipeline, resources->indexBuffer(), resources->vertexBuffer(), resources->instanceBuffer(), diff --git a/src/gpu/ccpr/GrCCDrawPathsOp.h b/src/gpu/ccpr/GrCCDrawPathsOp.h index 5406d893f7..92ba3dd4c7 100644 --- a/src/gpu/ccpr/GrCCDrawPathsOp.h +++ b/src/gpu/ccpr/GrCCDrawPathsOp.h @@ -48,11 +48,6 @@ public: void onExecute(GrOpFlushState*) override; private: - SkPath::FillType getFillType() const { - SkASSERT(fNumDraws >= 1); - return fDraws.head().fPath.getFillType(); - } - struct AtlasBatch { const GrCCAtlas* fAtlas; int fEndInstanceIdx; diff --git a/src/gpu/ccpr/GrCCPathProcessor.cpp b/src/gpu/ccpr/GrCCPathProcessor.cpp index 588e2bcb7b..de52726cea 100644 --- a/src/gpu/ccpr/GrCCPathProcessor.cpp +++ b/src/gpu/ccpr/GrCCPathProcessor.cpp @@ -76,10 +76,9 @@ sk_sp GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvid } GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider, - sk_sp atlas, SkPath::FillType fillType, + sk_sp atlas, const SkMatrix& viewMatrixIfUsingLocalCoords) : INHERITED(kGrCCPathProcessor_ClassID) - , fFillType(fillType) , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest, GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) { this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType); @@ -113,10 +112,6 @@ GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider, } } -void GrCCPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { - b->add32((fFillType << 16) | this->atlasProxy()->origin()); -} - class GLSLPathProcessor : public GrGLSLGeometryProcessor { public: void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; @@ -173,7 +168,7 @@ void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { varyingHandler->emitAttributes(proc); - GrGLSLVarying texcoord(kFloat2_GrSLType); + GrGLSLVarying texcoord(kFloat3_GrSLType); GrGLSLVarying color(kHalf4_GrSLType); varyingHandler->addVarying("texcoord", &texcoord); varyingHandler->addPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor), @@ -193,9 +188,11 @@ void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing // out of the octagon. - v->codeAppendf("float2 refpt = (0 == sk_VertexID >> 2) ? %s.xy : %s.zw;", - proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName, + v->codeAppendf("float4 devbounds = %s;", proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName); + v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)" + "? float2(min(devbounds.x, devbounds.z), devbounds.y)" + ": float2(max(devbounds.x, devbounds.z), devbounds.w);"); v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA. // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing @@ -215,12 +212,15 @@ void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { v->codeAppendf("float2 atlascoord = octocoord + float2(%s);", proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName); if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) { - v->codeAppendf("%s = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); + v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); } else { SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()); - v->codeAppendf("%s = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);", + v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);", texcoord.vsOut(), atlasAdjust, atlasAdjust); } + // The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones. + // ("right < left" indicates even-odd fill type.) + v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut()); this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType), proc.localMatrix(), args.fFPCoordTransformHandler); @@ -228,15 +228,19 @@ void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { // Fragment shader. GrGLSLFPFragmentBuilder* f = args.fFragBuilder; - f->codeAppend ("half coverage_count = "); - f->appendTextureLookup(args.fTexSamplers[0], texcoord.fsIn(), kFloat2_GrSLType); + // Look up coverage count in the atlas. + f->codeAppend ("half coverage = "); + f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(), + kFloat2_GrSLType); f->codeAppend (".a;"); - if (SkPath::kWinding_FillType == proc.fillType()) { - f->codeAppendf("%s = half4(min(abs(coverage_count), 1));", args.fOutputCoverage); - } else { - SkASSERT(SkPath::kEvenOdd_FillType == proc.fillType()); - f->codeAppend ("half t = mod(abs(coverage_count), 2);"); - f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage); - } + // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding + // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)). + f->codeAppendf("coverage = min(abs(coverage) * %s.z, .5);", texcoord.fsIn()); + + // For negative values, this finishes the even-odd sawtooth function. Since positive (winding) + // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5. + f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);"); + + f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage); } diff --git a/src/gpu/ccpr/GrCCPathProcessor.h b/src/gpu/ccpr/GrCCPathProcessor.h index 47893266da..198bc60d20 100644 --- a/src/gpu/ccpr/GrCCPathProcessor.h +++ b/src/gpu/ccpr/GrCCPathProcessor.h @@ -38,13 +38,14 @@ public: static constexpr int kNumInstanceAttribs = 1 + (int)InstanceAttribs::kColor; struct Instance { - SkRect fDevBounds; + SkRect fDevBounds; // "right < left" indicates even-odd fill type. SkRect fDevBounds45; // Bounding box in "| 1 -1 | * devCoords" space. // | 1 1 | std::array fAtlasOffset; uint32_t fColor; - GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT); + void set(SkPath::FillType, const SkRect& devBounds, const SkRect& devBounds45, + int16_t atlasOffsetX, int16_t atlasOffsetY, uint32_t color); }; GR_STATIC_ASSERT(4 * 10 == sizeof(Instance)); @@ -52,14 +53,13 @@ public: static sk_sp FindVertexBuffer(GrOnFlushResourceProvider*); static sk_sp FindIndexBuffer(GrOnFlushResourceProvider*); - GrCCPathProcessor(GrResourceProvider*, sk_sp atlas, SkPath::FillType, + GrCCPathProcessor(GrResourceProvider*, sk_sp atlas, const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I()); const char* name() const override { return "GrCCPathProcessor"; } const GrSurfaceProxy* atlasProxy() const { return fAtlasAccess.proxy(); } const GrTexture* atlas() const { return fAtlasAccess.peekTexture(); } const SkMatrix& localMatrix() const { return fLocalMatrix; } - SkPath::FillType fillType() const { return fFillType; } const Attribute& getInstanceAttrib(InstanceAttribs attribID) const { const Attribute& attrib = this->getAttrib((int)attribID); SkASSERT(Attribute::InputRate::kPerInstance == attrib.fInputRate); @@ -72,7 +72,7 @@ public: return attrib; } - void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; + void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; void drawPaths(GrOpFlushState*, const GrPipeline&, const GrBuffer* indexBuffer, @@ -80,11 +80,25 @@ public: int endInstance, const SkRect& bounds) const; private: - const SkPath::FillType fFillType; const TextureSampler fAtlasAccess; SkMatrix fLocalMatrix; typedef GrGeometryProcessor INHERITED; }; +inline void GrCCPathProcessor::Instance::set(SkPath::FillType fillType, const SkRect& devBounds, + const SkRect& devBounds45, int16_t atlasOffsetX, + int16_t atlasOffsetY, uint32_t color) { + if (SkPath::kEvenOdd_FillType == fillType) { + // "right < left" indicates even-odd fill type. + fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom); + } else { + SkASSERT(SkPath::kWinding_FillType == fillType); + fDevBounds = devBounds; + } + fDevBounds45 = devBounds45; + fAtlasOffset = {{atlasOffsetX, atlasOffsetY}}; + fColor = color; +} + #endif diff --git a/src/gpu/ccpr/GrCCPerFlushResources.cpp b/src/gpu/ccpr/GrCCPerFlushResources.cpp index a658bf7438..3f02ac75f6 100644 --- a/src/gpu/ccpr/GrCCPerFlushResources.cpp +++ b/src/gpu/ccpr/GrCCPerFlushResources.cpp @@ -38,10 +38,10 @@ GrCCPerFlushResources::GrCCPerFlushResources(GrOnFlushResourceProvider* onFlushR SkDEBUGCODE(fPathInstanceBufferCount = numPathDraws); } -GrCCAtlas* GrCCPerFlushResources::addPathToAtlas(const GrCaps& caps, const SkIRect& clipIBounds, - const SkMatrix& m, const SkPath& path, - SkRect* devBounds, SkRect* devBounds45, - int16_t* atlasOffsetX, int16_t* atlasOffsetY) { +GrCCAtlas* GrCCPerFlushResources::renderPathInAtlas(const GrCaps& caps, const SkIRect& clipIBounds, + const SkMatrix& m, const SkPath& path, + SkRect* devBounds, SkRect* devBounds45, + int16_t* atlasOffsetX, int16_t* atlasOffsetY) { SkASSERT(this->isMapped()); SkIRect devIBounds; fPathParser->parsePath(m, path, devBounds, devBounds45); @@ -49,12 +49,12 @@ GrCCAtlas* GrCCPerFlushResources::addPathToAtlas(const GrCaps& caps, const SkIRe return this->placeParsedPathInAtlas(caps, clipIBounds, devIBounds, atlasOffsetX, atlasOffsetY); } -GrCCAtlas* GrCCPerFlushResources::addDeviceSpacePathToAtlas(const GrCaps& caps, - const SkIRect& clipIBounds, - const SkPath& devPath, - const SkIRect& devPathIBounds, - int16_t* atlasOffsetX, - int16_t* atlasOffsetY) { +GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas(const GrCaps& caps, + const SkIRect& clipIBounds, + const SkPath& devPath, + const SkIRect& devPathIBounds, + int16_t* atlasOffsetX, + int16_t* atlasOffsetY) { SkASSERT(this->isMapped()); fPathParser->parseDeviceSpacePath(devPath); return this->placeParsedPathInAtlas(caps, clipIBounds, devPathIBounds, atlasOffsetX, diff --git a/src/gpu/ccpr/GrCCPerFlushResources.h b/src/gpu/ccpr/GrCCPerFlushResources.h index d1a6d8ff21..bc6b6edff3 100644 --- a/src/gpu/ccpr/GrCCPerFlushResources.h +++ b/src/gpu/ccpr/GrCCPerFlushResources.h @@ -26,22 +26,21 @@ public: bool isMapped() const { return SkToBool(fPathInstanceData); } - GrCCAtlas* addPathToAtlas(const GrCaps&, const SkIRect& clipIBounds, const SkMatrix&, - const SkPath&, SkRect* devBounds, SkRect* devBounds45, - int16_t* offsetX, int16_t* offsetY); - GrCCAtlas* addDeviceSpacePathToAtlas(const GrCaps&, const SkIRect& clipIBounds, - const SkPath& devPath, const SkIRect& devPathIBounds, - int16_t* atlasOffsetX, int16_t* atlasOffsetY); + GrCCAtlas* renderPathInAtlas(const GrCaps&, const SkIRect& clipIBounds, const SkMatrix&, + const SkPath&, SkRect* devBounds, SkRect* devBounds45, + int16_t* offsetX, int16_t* offsetY); + GrCCAtlas* renderDeviceSpacePathInAtlas(const GrCaps&, const SkIRect& clipIBounds, + const SkPath& devPath, const SkIRect& devPathIBounds, + int16_t* atlasOffsetX, int16_t* atlasOffsetY); GrCCPathProcessor::Instance& appendDrawPathInstance() { SkASSERT(this->isMapped()); - SkASSERT(fPathInstanceCount < fPathInstanceBufferCount); - return fPathInstanceData[fPathInstanceCount++]; + SkASSERT(fNextPathInstanceIdx < fPathInstanceBufferCount); + return fPathInstanceData[fNextPathInstanceIdx++]; } - int pathInstanceCount() const { return fPathInstanceCount; } + int nextPathInstanceIdx() const { return fNextPathInstanceIdx; } - bool finalize(GrOnFlushResourceProvider*, - SkTArray>* atlasDraws); + bool finalize(GrOnFlushResourceProvider*, SkTArray>* atlasDraws); const GrBuffer* indexBuffer() const { SkASSERT(!this->isMapped()); return fIndexBuffer.get(); } const GrBuffer* vertexBuffer() const { SkASSERT(!this->isMapped()); return fVertexBuffer.get();} @@ -59,7 +58,7 @@ private: sk_sp fInstanceBuffer; GrCCPathProcessor::Instance* fPathInstanceData = nullptr; - int fPathInstanceCount = 0; + int fNextPathInstanceIdx = 0; SkDEBUGCODE(int fPathInstanceBufferCount); GrSTAllocator<4, GrCCAtlas> fAtlases; diff --git a/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp b/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp index ee0dbde799..a069f6ee74 100644 --- a/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp +++ b/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp @@ -215,10 +215,10 @@ void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlush SkDEBUGCODE(numSkippedPaths += op->numSkippedInstances_debugOnly()); } for (auto& clipsIter : flushingPaths->fClipPaths) { - clipsIter.second.placePathInAtlas(resources.get(), onFlushRP); + clipsIter.second.renderPathInAtlas(resources.get(), onFlushRP); } } - SkASSERT(resources->pathInstanceCount() == numPathDraws - numSkippedPaths); + SkASSERT(resources->nextPathInstanceIdx() == numPathDraws - numSkippedPaths); // Allocate the atlases and create instance buffers to draw them. if (!resources->finalize(onFlushRP, atlasDraws)) {