blob: a9ef994fd5f5c08a7d990891e70f388c2ab48515 [file] [log] [blame]
Chris Dalton70a0d2c2021-01-26 12:01:21 -07001/*
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 Dalton031d76b2021-06-08 16:32:00 -06008#include "src/gpu/tessellate/GrPathStencilCoverOp.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -07009
10#include "src/gpu/GrEagerVertexAllocator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060011#include "src/gpu/GrGpu.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070012#include "src/gpu/GrOpFlushState.h"
13#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton2f733ec2021-06-01 12:11:57 -060014#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070015#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060016#include "src/gpu/tessellate/GrPathCurveTessellator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060017#include "src/gpu/tessellate/GrPathWedgeTessellator.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070018#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Chris Dalton3b412782021-06-01 13:40:03 -060019#include "src/gpu/tessellate/shaders/GrPathTessellationShader.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070020
Chris Dalton917f9192021-06-08 14:32:37 -060021using PathFlags = GrTessellationPathRenderer::PathFlags;
Chris Dalton70a0d2c2021-01-26 12:01:21 -070022
Chris Dalton2f733ec2021-06-01 12:11:57 -060023namespace {
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.
28class BoundingBoxShader : public GrPathTessellationShader {
29public:
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
38private:
Chris Daltonb63711a2021-06-01 14:52:02 -060039 const char* name() const final { return "tessellate_BoundingBoxShader"; }
Chris Dalton2f733ec2021-06-01 12:11:57 -060040 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
41 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
42};
43
44GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const {
45 class Impl : public GrPathTessellationShader::Impl {
Chris Daltond2b8ba32021-06-09 00:12:59 -060046 void emitVertexCode(const GrPathTessellationShader&, GrGLSLVertexBuilder* v,
47 GrGPArgs* gpArgs) override {
Chris Dalton2f733ec2021-06-01 12:11:57 -060048 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 Phillips294723d2021-06-17 09:23:58 -040066void GrPathStencilCoverOp::visitProxies(const GrVisitProxyFunc& func) const {
Chris Dalton031d76b2021-06-08 16:32:00 -060067 if (fCoverBBoxProgram) {
Robert Phillips294723d2021-06-17 09:23:58 -040068 fCoverBBoxProgram->pipeline().visitProxies(func);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070069 } else {
Robert Phillips294723d2021-06-17 09:23:58 -040070 fProcessors.visitProxies(func);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070071 }
72}
73
Chris Dalton031d76b2021-06-08 16:32:00 -060074GrDrawOp::FixedFunctionFlags GrPathStencilCoverOp::fixedFunctionFlags() const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070075 auto flags = FixedFunctionFlags::kUsesStencil;
76 if (fAAType != GrAAType::kNone) {
77 flags |= FixedFunctionFlags::kUsesHWAA;
78 }
79 return flags;
80}
81
Chris Dalton031d76b2021-06-08 16:32:00 -060082GrProcessorSet::Analysis GrPathStencilCoverOp::finalize(const GrCaps& caps,
83 const GrAppliedClip* clip,
84 GrClampType clampType) {
Chris Dalton57ab06c2021-04-22 12:57:28 -060085 return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps,
86 clampType, &fColor);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070087}
88
Chris Dalton031d76b2021-06-08 16:32:00 -060089void GrPathStencilCoverOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args,
90 GrAppliedClip&& appliedClip) {
Chris Dalton569c01b2021-05-25 10:11:46 -060091 SkASSERT(!fTessellator);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070092 SkASSERT(!fStencilFanProgram);
93 SkASSERT(!fStencilPathProgram);
Chris Dalton031d76b2021-06-08 16:32:00 -060094 SkASSERT(!fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070095
Chris Dalton2f733ec2021-06-01 12:11:57 -060096 const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline(
Chris Dalton917f9192021-06-08 14:32:37 -060097 args, fAAType, fPathFlags, appliedClip.hardClip());
Chris Dalton2f733ec2021-06-01 12:11:57 -060098 const GrUserStencilSettings* stencilPathSettings =
Chris Daltonbaae2dd2021-06-25 14:52:49 -060099 GrPathTessellationShader::StencilPathSettings(GrFillRuleForSkPath(fPath));
Chris Dalton569c01b2021-05-25 10:11:46 -0600100
Chris Dalton917f9192021-06-08 14:32:37 -0600101 if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256 * 256) {
Chris Daltond2b8ba32021-06-09 00:12:59 -0600102 // Large complex paths do better with a dedicated triangle shader for the inner fan.
103 // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us
104 // to make sure it has an efficient middle-out topology.
Chris Dalton917f9192021-06-08 14:32:37 -0600105 auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(
106 args.fArena, fViewMatrix, SK_PMColor4fTRANSPARENT);
107 fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline,
108 stencilPathSettings);
Chris Daltond2b8ba32021-06-09 00:12:59 -0600109 fTessellator = GrPathCurveTessellator::Make(args.fArena, fViewMatrix,
110 SK_PMColor4fTRANSPARENT,
111 GrPathCurveTessellator::DrawInnerFan::kNo,
Chris Dalton198ac152021-06-09 13:49:43 -0600112 fPath.countVerbs(), *stencilPipeline,
113 *args.fCaps);
Chris Dalton917f9192021-06-08 14:32:37 -0600114 } else {
Chris Dalton2f733ec2021-06-01 12:11:57 -0600115 fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix,
Chris Daltond2b8ba32021-06-09 00:12:59 -0600116 SK_PMColor4fTRANSPARENT, fPath.countVerbs(),
Chris Dalton198ac152021-06-09 13:49:43 -0600117 *stencilPipeline, *args.fCaps);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700118 }
Chris Dalton2f733ec2021-06-01 12:11:57 -0600119 fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(),
120 stencilPipeline, stencilPathSettings);
Chris Dalton569c01b2021-05-25 10:11:46 -0600121
Chris Dalton917f9192021-06-08 14:32:37 -0600122 if (!(fPathFlags & PathFlags::kStencilOnly)) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700123 // Create a program that draws a bounding box over the path and fills its stencil coverage
124 // into the color buffer.
Chris Dalton2f733ec2021-06-01 12:11:57 -0600125 auto* bboxShader = args.fArena->make<BoundingBoxShader>(fViewMatrix, fColor);
126 auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType,
127 std::move(appliedClip),
128 std::move(fProcessors));
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600129 auto* bboxStencil =
130 GrPathTessellationShader::TestAndResetStencilSettings(fPath.isInverseFillType());
Chris Dalton031d76b2021-06-08 16:32:00 -0600131 fCoverBBoxProgram = GrTessellationShader::MakeProgram(args, bboxShader, bboxPipeline,
132 bboxStencil);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700133 }
134}
135
Chris Dalton031d76b2021-06-08 16:32:00 -0600136void GrPathStencilCoverOp::onPrePrepare(GrRecordingContext* context,
137 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
138 const GrDstProxyView& dstProxyView,
139 GrXferBarrierFlags renderPassXferBarriers,
140 GrLoadOp colorLoadOp) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700141 this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView,
142 renderPassXferBarriers, colorLoadOp, context->priv().caps()},
143 (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
144 if (fStencilFanProgram) {
145 context->priv().recordProgramInfo(fStencilFanProgram);
146 }
147 if (fStencilPathProgram) {
148 context->priv().recordProgramInfo(fStencilPathProgram);
149 }
Chris Dalton031d76b2021-06-08 16:32:00 -0600150 if (fCoverBBoxProgram) {
151 context->priv().recordProgramInfo(fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700152 }
153}
154
Chris Dalton031d76b2021-06-08 16:32:00 -0600155void GrPathStencilCoverOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700156 if (!fTessellator) {
157 this->prePreparePrograms({flushState->allocator(), flushState->writeView(),
158 &flushState->dstProxyView(), flushState->renderPassBarriers(),
159 flushState->colorLoadOp(), &flushState->caps()},
160 flushState->detachAppliedClip());
161 if (!fTessellator) {
162 return;
163 }
164 }
165
166 if (fStencilFanProgram) {
167 // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a
168 // middle-out topology.
169 GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex);
170 int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon.
Chris Dalton8731a712021-05-14 14:48:54 -0600171 GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700172 fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan(
Chris Daltondf2dbad2021-05-14 16:21:15 -0600173 &triangleVertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kTriangles,
174 fPath) * 3;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700175 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
176 vertexAlloc.unlock(fFanVertexCount);
177 }
178
Chris Dalton569c01b2021-05-25 10:11:46 -0600179 fTessellator->prepare(flushState, this->bounds(), fPath);
Chris Daltonc91dd692021-05-24 15:04:47 -0600180
Chris Dalton031d76b2021-06-08 16:32:00 -0600181 if (fCoverBBoxProgram) {
Chris Daltonc91dd692021-05-24 15:04:47 -0600182 GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer,
183 &fBBoxBaseInstance);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600184 if (fPath.isInverseFillType()) {
185 // Fill the entire backing store to make sure we clear every stencil value back to 0. If
186 // there is a scissor it will have already clipped the stencil draw.
187 auto rtBounds = flushState->writeView().asRenderTargetProxy()->backingStoreBoundsRect();
188 SkASSERT(rtBounds == fOriginalDrawBounds);
189 SkRect pathSpaceRTBounds;
190 if (SkMatrixPriv::InverseMapRect(fViewMatrix, &pathSpaceRTBounds, rtBounds)) {
191 vertexWriter.write(pathSpaceRTBounds);
192 } else {
193 vertexWriter.write(fPath.getBounds());
194 }
195 } else {
196 vertexWriter.write(fPath.getBounds());
197 }
Chris Daltonc91dd692021-05-24 15:04:47 -0600198 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700199}
200
Chris Dalton031d76b2021-06-08 16:32:00 -0600201void GrPathStencilCoverOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700202 if (!fTessellator) {
203 return;
204 }
205
206 // Stencil the inner fan, if any.
207 if (fFanVertexCount > 0) {
208 SkASSERT(fStencilFanProgram);
209 SkASSERT(fFanBuffer);
210 flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds());
211 flushState->bindBuffers(nullptr, nullptr, fFanBuffer);
212 flushState->draw(fFanVertexCount, fFanBaseVertex);
213 }
214
215 // Stencil the rest of the path.
216 SkASSERT(fStencilPathProgram);
217 flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds());
218 fTessellator->draw(flushState);
Chris Daltond9bdc322021-06-01 19:22:05 -0600219 if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) {
220 flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739
221 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700222
223 // Fill in the bounding box (if not in stencil-only mode).
Chris Dalton031d76b2021-06-08 16:32:00 -0600224 if (fCoverBBoxProgram) {
225 flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds());
226 flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr,
227 fCoverBBoxProgram->pipeline());
Chris Daltonc91dd692021-05-24 15:04:47 -0600228 flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr);
229 flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700230 }
231}