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