Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrPathStencilFillOp.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
| 11 | #include "src/gpu/GrOpFlushState.h" |
| 12 | #include "src/gpu/GrRecordingContextPriv.h" |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 14 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
| 15 | #include "src/gpu/tessellate/GrPathTessellator.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame^] | 17 | #include "src/gpu/tessellate/shaders/GrPathTessellationShader.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 18 | |
| 19 | using OpFlags = GrTessellationPathRenderer::OpFlags; |
| 20 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme |
| 24 | // edges of the path. |
| 25 | // NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix. |
| 26 | class BoundingBoxShader : public GrPathTessellationShader { |
| 27 | public: |
| 28 | BoundingBoxShader(const SkMatrix& viewMatrix, SkPMColor4f color) |
| 29 | : GrPathTessellationShader(kTessellate_BoundingBoxShader_ClassID, |
| 30 | GrPrimitiveType::kTriangleStrip, 0, viewMatrix, color) { |
| 31 | constexpr static Attribute kPathBoundsAttrib = {"pathBounds", kFloat4_GrVertexAttribType, |
| 32 | kFloat4_GrSLType}; |
| 33 | this->setInstanceAttributes(&kPathBoundsAttrib, 1); |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | const char* name() const final { return "BoundingBoxShader"; } |
| 38 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
| 39 | GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 40 | }; |
| 41 | |
| 42 | GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const { |
| 43 | class Impl : public GrPathTessellationShader::Impl { |
| 44 | void emitVertexCode(GrGLSLVertexBuilder* v, GrGPArgs* gpArgs) override { |
| 45 | v->codeAppend(R"( |
| 46 | // Bloat the bounding box by 1/4px to avoid potential T-junctions at the edges. |
| 47 | float2x2 M_ = inverse(AFFINE_MATRIX); |
| 48 | float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25; |
| 49 | |
| 50 | // Find the vertex position. |
| 51 | float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1); |
| 52 | float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, T); |
| 53 | float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;)"); |
| 54 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
| 55 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
| 56 | } |
| 57 | }; |
| 58 | return new Impl; |
| 59 | } |
| 60 | |
| 61 | } // namespace |
| 62 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 63 | void GrPathStencilFillOp::visitProxies(const VisitProxyFunc& fn) const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 64 | if (fFillBBoxProgram) { |
| 65 | fFillBBoxProgram->pipeline().visitProxies(fn); |
| 66 | } else { |
| 67 | fProcessors.visitProxies(fn); |
| 68 | } |
| 69 | } |
| 70 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 71 | GrDrawOp::FixedFunctionFlags GrPathStencilFillOp::fixedFunctionFlags() const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 72 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 73 | if (fAAType != GrAAType::kNone) { |
| 74 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 75 | } |
| 76 | return flags; |
| 77 | } |
| 78 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 79 | GrProcessorSet::Analysis GrPathStencilFillOp::finalize(const GrCaps& caps, |
| 80 | const GrAppliedClip* clip, |
| 81 | GrClampType clampType) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 82 | return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps, |
| 83 | clampType, &fColor); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 86 | void GrPathStencilFillOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args, |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 87 | GrAppliedClip&& appliedClip) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 88 | SkASSERT(!fTessellator); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 89 | SkASSERT(!fStencilFanProgram); |
| 90 | SkASSERT(!fStencilPathProgram); |
| 91 | SkASSERT(!fFillBBoxProgram); |
| 92 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 93 | if (fPath.countVerbs() <= 0) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 97 | const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline( |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 98 | args, fAAType, fOpFlags, appliedClip.hardClip()); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 99 | const GrUserStencilSettings* stencilPathSettings = |
| 100 | GrPathTessellationShader::StencilPathSettings(fPath.getFillType()); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 101 | |
| 102 | if ((fOpFlags & OpFlags::kPreferWedges) && args.fCaps->shaderCaps()->tessellationSupport()) { |
| 103 | // The path is an atlas with relatively small contours, or something else that does best |
| 104 | // with wedges. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 105 | fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix, |
| 106 | SK_PMColor4fTRANSPARENT); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 107 | } else { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 108 | auto drawFanWithTessellator = GrPathTessellator::DrawInnerFan::kYes; |
| 109 | if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256*256) { |
| 110 | // Large complex paths do better with a dedicated triangle shader for the inner fan. |
| 111 | // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us |
| 112 | // to make sure it has an efficient middle-out topology. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 113 | auto shader = args.fArena->make<GrTriangleShader>(fViewMatrix, SK_PMColor4fTRANSPARENT); |
| 114 | fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline, |
| 115 | stencilPathSettings); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 116 | drawFanWithTessellator = GrPathTessellator::DrawInnerFan::kNo; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 117 | } |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 118 | fTessellator = GrPathTessellator::Make(args.fArena, fPath, fViewMatrix, |
| 119 | SK_PMColor4fTRANSPARENT, drawFanWithTessellator, |
| 120 | *args.fCaps); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 123 | fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(), |
| 124 | stencilPipeline, stencilPathSettings); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 125 | |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 126 | if (!(fOpFlags & OpFlags::kStencilOnly)) { |
| 127 | // Create a program that draws a bounding box over the path and fills its stencil coverage |
| 128 | // into the color buffer. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 129 | auto* bboxShader = args.fArena->make<BoundingBoxShader>(fViewMatrix, fColor); |
| 130 | auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType, |
| 131 | std::move(appliedClip), |
| 132 | std::move(fProcessors)); |
| 133 | auto* bboxStencil = GrPathTessellationShader::TestAndResetStencilSettings(); |
| 134 | fFillBBoxProgram = GrTessellationShader::MakeProgram(args, bboxShader, bboxPipeline, |
| 135 | bboxStencil); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 139 | void GrPathStencilFillOp::onPrePrepare(GrRecordingContext* context, |
| 140 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 141 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 142 | GrXferBarrierFlags renderPassXferBarriers, |
| 143 | GrLoadOp colorLoadOp) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 144 | this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 145 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 146 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 147 | if (fStencilFanProgram) { |
| 148 | context->priv().recordProgramInfo(fStencilFanProgram); |
| 149 | } |
| 150 | if (fStencilPathProgram) { |
| 151 | context->priv().recordProgramInfo(fStencilPathProgram); |
| 152 | } |
| 153 | if (fFillBBoxProgram) { |
| 154 | context->priv().recordProgramInfo(fFillBBoxProgram); |
| 155 | } |
| 156 | } |
| 157 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 158 | void GrPathStencilFillOp::onPrepare(GrOpFlushState* flushState) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 159 | if (!fTessellator) { |
| 160 | this->prePreparePrograms({flushState->allocator(), flushState->writeView(), |
| 161 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 162 | flushState->colorLoadOp(), &flushState->caps()}, |
| 163 | flushState->detachAppliedClip()); |
| 164 | if (!fTessellator) { |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (fStencilFanProgram) { |
| 170 | // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a |
| 171 | // middle-out topology. |
| 172 | GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex); |
| 173 | int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon. |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 174 | GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 175 | fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan( |
Chris Dalton | df2dbad | 2021-05-14 16:21:15 -0600 | [diff] [blame] | 176 | &triangleVertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kTriangles, |
| 177 | fPath) * 3; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 178 | SkASSERT(fFanVertexCount <= maxFanTriangles * 3); |
| 179 | vertexAlloc.unlock(fFanVertexCount); |
| 180 | } |
| 181 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 182 | fTessellator->prepare(flushState, this->bounds(), fPath); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 183 | |
| 184 | if (fFillBBoxProgram) { |
| 185 | GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer, |
| 186 | &fBBoxBaseInstance); |
| 187 | vertexWriter.write(fPath.getBounds()); |
| 188 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 191 | void GrPathStencilFillOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 192 | if (!fTessellator) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Stencil the inner fan, if any. |
| 197 | if (fFanVertexCount > 0) { |
| 198 | SkASSERT(fStencilFanProgram); |
| 199 | SkASSERT(fFanBuffer); |
| 200 | flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds()); |
| 201 | flushState->bindBuffers(nullptr, nullptr, fFanBuffer); |
| 202 | flushState->draw(fFanVertexCount, fFanBaseVertex); |
| 203 | } |
| 204 | |
| 205 | // Stencil the rest of the path. |
| 206 | SkASSERT(fStencilPathProgram); |
| 207 | flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds()); |
| 208 | fTessellator->draw(flushState); |
| 209 | |
| 210 | // Fill in the bounding box (if not in stencil-only mode). |
| 211 | if (fFillBBoxProgram) { |
| 212 | flushState->bindPipelineAndScissorClip(*fFillBBoxProgram, this->bounds()); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 213 | flushState->bindTextures(fFillBBoxProgram->geomProc(), nullptr, |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 214 | fFillBBoxProgram->pipeline()); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 215 | flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr); |
| 216 | flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 217 | } |
| 218 | } |