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