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 | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrPathStencilCoverOp.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 11 | #include "src/gpu/GrGpu.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 12 | #include "src/gpu/GrOpFlushState.h" |
| 13 | #include "src/gpu/GrRecordingContextPriv.h" |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrPathCurveTessellator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrPathWedgeTessellator.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame] | 19 | #include "src/gpu/tessellate/shaders/GrPathTessellationShader.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 20 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 21 | using PathFlags = GrTessellationPathRenderer::PathFlags; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 22 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme |
| 26 | // edges of the path. |
| 27 | // NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix. |
| 28 | class BoundingBoxShader : public GrPathTessellationShader { |
| 29 | public: |
| 30 | BoundingBoxShader(const SkMatrix& viewMatrix, SkPMColor4f color) |
| 31 | : GrPathTessellationShader(kTessellate_BoundingBoxShader_ClassID, |
| 32 | GrPrimitiveType::kTriangleStrip, 0, viewMatrix, color) { |
| 33 | constexpr static Attribute kPathBoundsAttrib = {"pathBounds", kFloat4_GrVertexAttribType, |
| 34 | kFloat4_GrSLType}; |
| 35 | this->setInstanceAttributes(&kPathBoundsAttrib, 1); |
| 36 | } |
| 37 | |
| 38 | private: |
Chris Dalton | b63711a | 2021-06-01 14:52:02 -0600 | [diff] [blame] | 39 | const char* name() const final { return "tessellate_BoundingBoxShader"; } |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 40 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
| 41 | GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 42 | }; |
| 43 | |
| 44 | GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const { |
| 45 | class Impl : public GrPathTessellationShader::Impl { |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 46 | void emitVertexCode(const GrPathTessellationShader&, GrGLSLVertexBuilder* v, |
| 47 | GrGPArgs* gpArgs) override { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 48 | v->codeAppend(R"( |
| 49 | // Bloat the bounding box by 1/4px to avoid potential T-junctions at the edges. |
| 50 | float2x2 M_ = inverse(AFFINE_MATRIX); |
| 51 | float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25; |
| 52 | |
| 53 | // Find the vertex position. |
| 54 | float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1); |
| 55 | float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, T); |
| 56 | float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;)"); |
| 57 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
| 58 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
| 59 | } |
| 60 | }; |
| 61 | return new Impl; |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 66 | void GrPathStencilCoverOp::visitProxies(const GrVisitProxyFunc& func) const { |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 67 | if (fCoverBBoxProgram) { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 68 | fCoverBBoxProgram->pipeline().visitProxies(func); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 69 | } else { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 70 | fProcessors.visitProxies(func); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 74 | GrDrawOp::FixedFunctionFlags GrPathStencilCoverOp::fixedFunctionFlags() const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 75 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 76 | if (fAAType != GrAAType::kNone) { |
| 77 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 78 | } |
| 79 | return flags; |
| 80 | } |
| 81 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 82 | GrProcessorSet::Analysis GrPathStencilCoverOp::finalize(const GrCaps& caps, |
| 83 | const GrAppliedClip* clip, |
| 84 | GrClampType clampType) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 85 | return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps, |
| 86 | clampType, &fColor); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 89 | void GrPathStencilCoverOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args, |
| 90 | GrAppliedClip&& appliedClip) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 91 | SkASSERT(!fTessellator); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 92 | SkASSERT(!fStencilFanProgram); |
| 93 | SkASSERT(!fStencilPathProgram); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 94 | SkASSERT(!fCoverBBoxProgram); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 95 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 96 | if (fPath.countVerbs() <= 0) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 100 | const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline( |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 101 | args, fAAType, fPathFlags, appliedClip.hardClip()); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 102 | const GrUserStencilSettings* stencilPathSettings = |
| 103 | GrPathTessellationShader::StencilPathSettings(fPath.getFillType()); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 104 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 105 | if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256 * 256) { |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 106 | // Large complex paths do better with a dedicated triangle shader for the inner fan. |
| 107 | // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us |
| 108 | // to make sure it has an efficient middle-out topology. |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 109 | auto shader = GrPathTessellationShader::MakeSimpleTriangleShader( |
| 110 | args.fArena, fViewMatrix, SK_PMColor4fTRANSPARENT); |
| 111 | fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline, |
| 112 | stencilPathSettings); |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 113 | fTessellator = GrPathCurveTessellator::Make(args.fArena, fViewMatrix, |
| 114 | SK_PMColor4fTRANSPARENT, |
| 115 | GrPathCurveTessellator::DrawInnerFan::kNo, |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 116 | fPath.countVerbs(), *stencilPipeline, |
| 117 | *args.fCaps); |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 118 | } else { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 119 | fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix, |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 120 | SK_PMColor4fTRANSPARENT, fPath.countVerbs(), |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 121 | *stencilPipeline, *args.fCaps); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 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 | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 126 | if (!(fPathFlags & PathFlags::kStencilOnly)) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 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(); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 134 | fCoverBBoxProgram = GrTessellationShader::MakeProgram(args, bboxShader, bboxPipeline, |
| 135 | bboxStencil); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 139 | void GrPathStencilCoverOp::onPrePrepare(GrRecordingContext* context, |
| 140 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 141 | const GrDstProxyView& 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 | } |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 153 | if (fCoverBBoxProgram) { |
| 154 | context->priv().recordProgramInfo(fCoverBBoxProgram); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 158 | void GrPathStencilCoverOp::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 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 184 | if (fCoverBBoxProgram) { |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 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 | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 191 | void GrPathStencilCoverOp::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); |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 209 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 210 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 211 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 212 | |
| 213 | // Fill in the bounding box (if not in stencil-only mode). |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 214 | if (fCoverBBoxProgram) { |
| 215 | flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds()); |
| 216 | flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr, |
| 217 | fCoverBBoxProgram->pipeline()); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 218 | flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr); |
| 219 | flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 220 | } |
| 221 | } |