blob: 574091a8b60c9834c065f9dc5ee009777e8d1bd2 [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 Dalton2ed22fa2021-05-06 16:08:30 -06008#include "src/gpu/tessellate/GrPathStencilFillOp.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -07009
10#include "src/gpu/GrEagerVertexAllocator.h"
11#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton2f733ec2021-06-01 12:11:57 -060013#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070014#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
Chris Dalton2f733ec2021-06-01 12:11:57 -060015#include "src/gpu/tessellate/GrPathTessellationShader.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070016#include "src/gpu/tessellate/GrPathTessellator.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070017#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
18
19using OpFlags = GrTessellationPathRenderer::OpFlags;
20
Chris Dalton2f733ec2021-06-01 12:11:57 -060021namespace {
22
23// Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme
24// edges of the path.
25// NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix.
26class BoundingBoxShader : public GrPathTessellationShader {
27public:
28 BoundingBoxShader(const SkMatrix& viewMatrix, SkPMColor4f color)
29 : GrPathTessellationShader(kTessellate_BoundingBoxShader_ClassID,
30 GrPrimitiveType::kTriangleStrip, 0, viewMatrix, color) {
31 constexpr static Attribute kPathBoundsAttrib = {"pathBounds", kFloat4_GrVertexAttribType,
32 kFloat4_GrSLType};
33 this->setInstanceAttributes(&kPathBoundsAttrib, 1);
34 }
35
36private:
37 const char* name() const final { return "BoundingBoxShader"; }
38 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
39 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
40};
41
42GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const {
43 class Impl : public GrPathTessellationShader::Impl {
44 void emitVertexCode(GrGLSLVertexBuilder* v, GrGPArgs* gpArgs) override {
45 v->codeAppend(R"(
46 // Bloat the bounding box by 1/4px to avoid potential T-junctions at the edges.
47 float2x2 M_ = inverse(AFFINE_MATRIX);
48 float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25;
49
50 // Find the vertex position.
51 float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1);
52 float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, T);
53 float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;)");
54 gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord");
55 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos");
56 }
57 };
58 return new Impl;
59}
60
61} // namespace
62
Chris Dalton2ed22fa2021-05-06 16:08:30 -060063void GrPathStencilFillOp::visitProxies(const VisitProxyFunc& fn) const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070064 if (fFillBBoxProgram) {
65 fFillBBoxProgram->pipeline().visitProxies(fn);
66 } else {
67 fProcessors.visitProxies(fn);
68 }
69}
70
Chris Dalton2ed22fa2021-05-06 16:08:30 -060071GrDrawOp::FixedFunctionFlags GrPathStencilFillOp::fixedFunctionFlags() const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070072 auto flags = FixedFunctionFlags::kUsesStencil;
73 if (fAAType != GrAAType::kNone) {
74 flags |= FixedFunctionFlags::kUsesHWAA;
75 }
76 return flags;
77}
78
Chris Dalton2ed22fa2021-05-06 16:08:30 -060079GrProcessorSet::Analysis GrPathStencilFillOp::finalize(const GrCaps& caps,
80 const GrAppliedClip* clip,
81 GrClampType clampType) {
Chris Dalton57ab06c2021-04-22 12:57:28 -060082 return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps,
83 clampType, &fColor);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070084}
85
Chris Dalton2f733ec2021-06-01 12:11:57 -060086void GrPathStencilFillOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args,
Chris Dalton2ed22fa2021-05-06 16:08:30 -060087 GrAppliedClip&& appliedClip) {
Chris Dalton569c01b2021-05-25 10:11:46 -060088 SkASSERT(!fTessellator);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070089 SkASSERT(!fStencilFanProgram);
90 SkASSERT(!fStencilPathProgram);
91 SkASSERT(!fFillBBoxProgram);
92
Chris Dalton569c01b2021-05-25 10:11:46 -060093 if (fPath.countVerbs() <= 0) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070094 return;
95 }
96
Chris Dalton2f733ec2021-06-01 12:11:57 -060097 const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline(
Chris Dalton70a0d2c2021-01-26 12:01:21 -070098 args, fAAType, fOpFlags, appliedClip.hardClip());
Chris Dalton2f733ec2021-06-01 12:11:57 -060099 const GrUserStencilSettings* stencilPathSettings =
100 GrPathTessellationShader::StencilPathSettings(fPath.getFillType());
Chris Dalton569c01b2021-05-25 10:11:46 -0600101
102 if ((fOpFlags & OpFlags::kPreferWedges) && args.fCaps->shaderCaps()->tessellationSupport()) {
103 // The path is an atlas with relatively small contours, or something else that does best
104 // with wedges.
Chris Dalton2f733ec2021-06-01 12:11:57 -0600105 fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix,
106 SK_PMColor4fTRANSPARENT);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700107 } else {
Chris Dalton569c01b2021-05-25 10:11:46 -0600108 auto drawFanWithTessellator = GrPathTessellator::DrawInnerFan::kYes;
109 if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256*256) {
110 // Large complex paths do better with a dedicated triangle shader for the inner fan.
111 // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us
112 // to make sure it has an efficient middle-out topology.
Chris Dalton2f733ec2021-06-01 12:11:57 -0600113 auto shader = args.fArena->make<GrTriangleShader>(fViewMatrix, SK_PMColor4fTRANSPARENT);
114 fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline,
115 stencilPathSettings);
Chris Dalton569c01b2021-05-25 10:11:46 -0600116 drawFanWithTessellator = GrPathTessellator::DrawInnerFan::kNo;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700117 }
Chris Dalton2f733ec2021-06-01 12:11:57 -0600118 fTessellator = GrPathTessellator::Make(args.fArena, fPath, fViewMatrix,
119 SK_PMColor4fTRANSPARENT, drawFanWithTessellator,
120 *args.fCaps);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700121 }
122
Chris Dalton2f733ec2021-06-01 12:11:57 -0600123 fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(),
124 stencilPipeline, stencilPathSettings);
Chris Dalton569c01b2021-05-25 10:11:46 -0600125
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700126 if (!(fOpFlags & OpFlags::kStencilOnly)) {
127 // Create a program that draws a bounding box over the path and fills its stencil coverage
128 // into the color buffer.
Chris Dalton2f733ec2021-06-01 12:11:57 -0600129 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();
134 fFillBBoxProgram = GrTessellationShader::MakeProgram(args, bboxShader, bboxPipeline,
135 bboxStencil);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700136 }
137}
138
Chris Dalton2ed22fa2021-05-06 16:08:30 -0600139void GrPathStencilFillOp::onPrePrepare(GrRecordingContext* context,
140 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
141 const GrXferProcessor::DstProxyView& dstProxyView,
142 GrXferBarrierFlags renderPassXferBarriers,
143 GrLoadOp colorLoadOp) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700144 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 }
153 if (fFillBBoxProgram) {
154 context->priv().recordProgramInfo(fFillBBoxProgram);
155 }
156}
157
Chris Dalton2ed22fa2021-05-06 16:08:30 -0600158void GrPathStencilFillOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700159 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 Dalton8731a712021-05-14 14:48:54 -0600174 GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700175 fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan(
Chris Daltondf2dbad2021-05-14 16:21:15 -0600176 &triangleVertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kTriangles,
177 fPath) * 3;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700178 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
179 vertexAlloc.unlock(fFanVertexCount);
180 }
181
Chris Dalton569c01b2021-05-25 10:11:46 -0600182 fTessellator->prepare(flushState, this->bounds(), fPath);
Chris Daltonc91dd692021-05-24 15:04:47 -0600183
184 if (fFillBBoxProgram) {
185 GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer,
186 &fBBoxBaseInstance);
187 vertexWriter.write(fPath.getBounds());
188 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700189}
190
Chris Dalton2ed22fa2021-05-06 16:08:30 -0600191void GrPathStencilFillOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700192 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);
209
210 // Fill in the bounding box (if not in stencil-only mode).
211 if (fFillBBoxProgram) {
212 flushState->bindPipelineAndScissorClip(*fFillBBoxProgram, this->bounds());
Robert Phillips787fd9d2021-03-22 14:48:09 -0400213 flushState->bindTextures(fFillBBoxProgram->geomProc(), nullptr,
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700214 fFillBBoxProgram->pipeline());
Chris Daltonc91dd692021-05-24 15:04:47 -0600215 flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr);
216 flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700217 }
218}