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 | |
Robert Phillips | e453fa0 | 2021-08-19 14:57:05 -0400 | [diff] [blame^] | 8 | #include "src/gpu/ops/PathStencilCoverOp.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" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrResourceProvider.h" |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLVarying.h" |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 17 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Robert Phillips | 06273bc | 2021-08-11 15:43:50 -0400 | [diff] [blame] | 18 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrPathCurveTessellator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrPathWedgeTessellator.h" |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame] | 22 | #include "src/gpu/tessellate/shaders/GrPathTessellationShader.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 23 | |
Robert Phillips | 832f3fb | 2021-08-18 13:05:15 -0400 | [diff] [blame] | 24 | using PathFlags = GrTessellationPathFlags; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 25 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | // Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme |
| 29 | // edges of the path. |
| 30 | // NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix. |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 31 | class BoundingBoxShader : public GrGeometryProcessor { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 32 | public: |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 33 | BoundingBoxShader(SkPMColor4f color, const GrShaderCaps& shaderCaps) |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 34 | : GrGeometryProcessor(kTessellate_BoundingBoxShader_ClassID) |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 35 | , fColor(color) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 36 | if (!shaderCaps.vertexIDSupport()) { |
| 37 | constexpr static Attribute kUnitCoordAttrib("unitCoord", kFloat2_GrVertexAttribType, |
| 38 | kFloat2_GrSLType); |
| 39 | this->setVertexAttributes(&kUnitCoordAttrib, 1); |
| 40 | } |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 41 | constexpr static Attribute kInstanceAttribs[] = { |
| 42 | {"matrix2d", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 43 | {"translate", kFloat2_GrVertexAttribType, kFloat2_GrSLType}, |
| 44 | {"pathBounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType} |
| 45 | }; |
| 46 | this->setInstanceAttributes(kInstanceAttribs, SK_ARRAY_COUNT(kInstanceAttribs)); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | private: |
Chris Dalton | b63711a | 2021-06-01 14:52:02 -0600 | [diff] [blame] | 50 | const char* name() const final { return "tessellate_BoundingBoxShader"; } |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 51 | void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 52 | std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final; |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 53 | |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 54 | const SkPMColor4f fColor; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 57 | std::unique_ptr<GrGeometryProcessor::ProgramImpl> BoundingBoxShader::makeProgramImpl( |
| 58 | const GrShaderCaps&) const { |
| 59 | class Impl : public ProgramImpl { |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 60 | public: |
| 61 | void setData(const GrGLSLProgramDataManager& pdman, |
| 62 | const GrShaderCaps&, |
| 63 | const GrGeometryProcessor& gp) override { |
| 64 | const SkPMColor4f& color = gp.cast<BoundingBoxShader>().fColor; |
| 65 | pdman.set4f(fColorUniform, color.fR, color.fG, color.fB, color.fA); |
| 66 | } |
| 67 | |
| 68 | private: |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 69 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 70 | args.fVaryingHandler->emitAttributes(args.fGeomProc); |
| 71 | |
| 72 | // Vertex shader. |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 73 | if (args.fShaderCaps->vertexIDSupport()) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 74 | // If we don't have sk_VertexID support then "unitCoord" already came in as a vertex |
| 75 | // attrib. |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 76 | args.fVertBuilder->codeAppend(R"( |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 77 | float2 unitCoord = float2(sk_VertexID & 1, sk_VertexID >> 1);)"); |
| 78 | } |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 79 | args.fVertBuilder->codeAppend(R"( |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 80 | // Bloat the bounding box by 1/4px to be certain we will reset every stencil value. |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 81 | float2x2 M_ = inverse(float2x2(matrix2d)); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 82 | float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25; |
| 83 | |
| 84 | // Find the vertex position. |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 85 | float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, unitCoord); |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 86 | float2 vertexpos = float2x2(matrix2d) * localcoord + translate;)"); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 87 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
| 88 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 89 | |
| 90 | // Fragment shader. |
| 91 | const char* color; |
| 92 | fColorUniform = args.fUniformHandler->addUniform(nullptr, kFragment_GrShaderFlag, |
| 93 | kHalf4_GrSLType, "color", &color); |
| 94 | args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor, color); |
| 95 | args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 96 | } |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 97 | |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 98 | GrGLSLUniformHandler::UniformHandle fColorUniform; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 99 | }; |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 100 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 101 | return std::make_unique<Impl>(); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace |
| 105 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 106 | void GrPathStencilCoverOp::visitProxies(const GrVisitProxyFunc& func) const { |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 107 | if (fCoverBBoxProgram) { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 108 | fCoverBBoxProgram->pipeline().visitProxies(func); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 109 | } else { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 110 | fProcessors.visitProxies(func); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 114 | GrDrawOp::FixedFunctionFlags GrPathStencilCoverOp::fixedFunctionFlags() const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 115 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 116 | if (fAAType != GrAAType::kNone) { |
| 117 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 118 | } |
| 119 | return flags; |
| 120 | } |
| 121 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 122 | GrProcessorSet::Analysis GrPathStencilCoverOp::finalize(const GrCaps& caps, |
| 123 | const GrAppliedClip* clip, |
| 124 | GrClampType clampType) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 125 | return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps, |
| 126 | clampType, &fColor); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 129 | void GrPathStencilCoverOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args, |
| 130 | GrAppliedClip&& appliedClip) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 131 | SkASSERT(!fTessellator); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 132 | SkASSERT(!fStencilFanProgram); |
| 133 | SkASSERT(!fStencilPathProgram); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 134 | SkASSERT(!fCoverBBoxProgram); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 135 | |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 136 | // We transform paths on the CPU. This allows for better batching. |
| 137 | const SkMatrix& shaderMatrix = SkMatrix::I(); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 138 | const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline( |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 139 | args, fAAType, fPathFlags, appliedClip.hardClip()); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 140 | const GrUserStencilSettings* stencilSettings = GrPathTessellationShader::StencilPathSettings( |
| 141 | GrFillRuleForPathFillType(this->pathFillType())); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 142 | |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 143 | if (fTotalCombinedPathVerbCnt > 50 && |
| 144 | this->bounds().height() * this->bounds().width() > 256 * 256) { |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 145 | // Large complex paths do better with a dedicated triangle shader for the inner fan. |
| 146 | // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us |
| 147 | // to make sure it has an efficient middle-out topology. |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 148 | auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(args.fArena, |
| 149 | shaderMatrix, |
| 150 | SK_PMColor4fTRANSPARENT); |
| 151 | fStencilFanProgram = GrTessellationShader::MakeProgram(args, |
| 152 | shader, |
| 153 | stencilPipeline, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 154 | stencilSettings); |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 155 | fTessellator = GrPathCurveTessellator::Make(args.fArena, |
| 156 | shaderMatrix, |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 157 | SK_PMColor4fTRANSPARENT, |
| 158 | GrPathCurveTessellator::DrawInnerFan::kNo, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 159 | fTotalCombinedPathVerbCnt, |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 160 | *stencilPipeline, |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 161 | *args.fCaps); |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 162 | } else { |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 163 | fTessellator = GrPathWedgeTessellator::Make(args.fArena, |
| 164 | shaderMatrix, |
| 165 | SK_PMColor4fTRANSPARENT, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 166 | fTotalCombinedPathVerbCnt, |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 167 | *stencilPipeline, |
| 168 | *args.fCaps); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 169 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 170 | fStencilPathProgram = GrTessellationShader::MakeProgram(args, |
| 171 | fTessellator->shader(), |
| 172 | stencilPipeline, |
| 173 | stencilSettings); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 174 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 175 | if (!(fPathFlags & PathFlags::kStencilOnly)) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 176 | // Create a program that draws a bounding box over the path and fills its stencil coverage |
| 177 | // into the color buffer. |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 178 | auto* bboxShader = args.fArena->make<BoundingBoxShader>(fColor, *args.fCaps->shaderCaps()); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 179 | auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType, |
| 180 | std::move(appliedClip), |
| 181 | std::move(fProcessors)); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 182 | auto* bboxStencil = GrPathTessellationShader::TestAndResetStencilSettings( |
| 183 | SkPathFillType_IsInverse(this->pathFillType())); |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 184 | fCoverBBoxProgram = GrSimpleMeshDrawOpHelper::CreateProgramInfo( |
| 185 | args.fArena, |
| 186 | bboxPipeline, |
| 187 | args.fWriteView, |
| 188 | bboxShader, |
| 189 | GrPrimitiveType::kTriangleStrip, |
| 190 | args.fXferBarrierFlags, |
| 191 | args.fColorLoadOp, |
| 192 | bboxStencil); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 196 | void GrPathStencilCoverOp::onPrePrepare(GrRecordingContext* context, |
| 197 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 198 | const GrDstProxyView& dstProxyView, |
| 199 | GrXferBarrierFlags renderPassXferBarriers, |
| 200 | GrLoadOp colorLoadOp) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 201 | this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 202 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 203 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 204 | if (fStencilFanProgram) { |
| 205 | context->priv().recordProgramInfo(fStencilFanProgram); |
| 206 | } |
| 207 | if (fStencilPathProgram) { |
| 208 | context->priv().recordProgramInfo(fStencilPathProgram); |
| 209 | } |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 210 | if (fCoverBBoxProgram) { |
| 211 | context->priv().recordProgramInfo(fCoverBBoxProgram); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 215 | GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 216 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 217 | void GrPathStencilCoverOp::onPrepare(GrOpFlushState* flushState) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 218 | if (!fTessellator) { |
| 219 | this->prePreparePrograms({flushState->allocator(), flushState->writeView(), |
| 220 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 221 | flushState->colorLoadOp(), &flushState->caps()}, |
| 222 | flushState->detachAppliedClip()); |
| 223 | if (!fTessellator) { |
| 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (fStencilFanProgram) { |
| 229 | // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a |
| 230 | // middle-out topology. |
| 231 | GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 232 | int maxCombinedFanEdges = |
| 233 | GrPathTessellator::MaxCombinedFanEdgesInPathDrawList(fTotalCombinedPathVerbCnt); |
| 234 | // A single n-sided polygon is fanned by n-2 triangles. Multiple polygons with a combined |
| 235 | // edge count of n are fanned by strictly fewer triangles. |
| 236 | int maxTrianglesInFans = std::max(maxCombinedFanEdges - 2, 0); |
| 237 | GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxTrianglesInFans * 3); |
| 238 | int fanTriangleCount = 0; |
| 239 | for (auto [pathMatrix, path] : *fPathDrawList) { |
| 240 | int numTrianglesWritten; |
| 241 | triangleVertexWriter = GrMiddleOutPolygonTriangulator::WritePathInnerFan( |
| 242 | std::move(triangleVertexWriter), |
| 243 | 0, |
| 244 | 0, |
| 245 | pathMatrix, |
| 246 | path, |
| 247 | &numTrianglesWritten); |
| 248 | fanTriangleCount += numTrianglesWritten; |
| 249 | } |
| 250 | SkASSERT(fanTriangleCount <= maxTrianglesInFans); |
| 251 | fFanVertexCount = fanTriangleCount * 3; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 252 | vertexAlloc.unlock(fFanVertexCount); |
| 253 | } |
| 254 | |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 255 | fTessellator->prepare(flushState, this->bounds(), *fPathDrawList, fTotalCombinedPathVerbCnt); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 256 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 257 | if (fCoverBBoxProgram) { |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 258 | size_t instanceStride = fCoverBBoxProgram->geomProc().instanceStride(); |
| 259 | GrVertexWriter vertexWriter = flushState->makeVertexSpace( |
| 260 | instanceStride, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 261 | fPathCount, |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 262 | &fBBoxBuffer, |
| 263 | &fBBoxBaseInstance); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 264 | SkDEBUGCODE(int pathCount = 0;) |
| 265 | for (auto [pathMatrix, path] : *fPathDrawList) { |
| 266 | SkDEBUGCODE(auto end = vertexWriter.makeOffset(instanceStride)); |
| 267 | vertexWriter.write(pathMatrix.getScaleX(), |
| 268 | pathMatrix.getSkewY(), |
| 269 | pathMatrix.getSkewX(), |
| 270 | pathMatrix.getScaleY(), |
| 271 | pathMatrix.getTranslateX(), |
| 272 | pathMatrix.getTranslateY()); |
| 273 | if (path.isInverseFillType()) { |
| 274 | // Fill the entire backing store to make sure we clear every stencil value back to |
| 275 | // 0. If there is a scissor it will have already clipped the stencil draw. |
| 276 | auto rtBounds = |
| 277 | flushState->writeView().asRenderTargetProxy()->backingStoreBoundsRect(); |
| 278 | SkASSERT(rtBounds == fOriginalDrawBounds); |
| 279 | SkRect pathSpaceRTBounds; |
| 280 | if (SkMatrixPriv::InverseMapRect(pathMatrix, &pathSpaceRTBounds, rtBounds)) { |
| 281 | vertexWriter.write(pathSpaceRTBounds); |
| 282 | } else { |
| 283 | vertexWriter.write(path.getBounds()); |
| 284 | } |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 285 | } else { |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 286 | vertexWriter.write(path.getBounds()); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 287 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 288 | SkASSERT(vertexWriter == end); |
| 289 | SkDEBUGCODE(++pathCount;) |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 290 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 291 | SkASSERT(pathCount == fPathCount); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 292 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 293 | |
| 294 | if (!flushState->caps().shaderCaps()->vertexIDSupport()) { |
| 295 | constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}}; |
| 296 | |
| 297 | GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 298 | |
| 299 | fBBoxVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer( |
| 300 | GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey); |
| 301 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 304 | void GrPathStencilCoverOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 305 | if (!fTessellator) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Stencil the inner fan, if any. |
| 310 | if (fFanVertexCount > 0) { |
| 311 | SkASSERT(fStencilFanProgram); |
| 312 | SkASSERT(fFanBuffer); |
| 313 | flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds()); |
| 314 | flushState->bindBuffers(nullptr, nullptr, fFanBuffer); |
| 315 | flushState->draw(fFanVertexCount, fFanBaseVertex); |
| 316 | } |
| 317 | |
| 318 | // Stencil the rest of the path. |
| 319 | SkASSERT(fStencilPathProgram); |
| 320 | flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds()); |
| 321 | fTessellator->draw(flushState); |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 322 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 323 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 324 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 325 | |
| 326 | // Fill in the bounding box (if not in stencil-only mode). |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 327 | if (fCoverBBoxProgram) { |
| 328 | flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds()); |
| 329 | flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr, |
| 330 | fCoverBBoxProgram->pipeline()); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 331 | flushState->bindBuffers(nullptr, fBBoxBuffer, fBBoxVertexBufferIfNoIDSupport); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 332 | flushState->drawInstanced(fPathCount, fBBoxBaseInstance, 4, 0); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 333 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 334 | } |