blob: 21ab1f7fdc1179c0cb092c7829a2477431c3c9f4 [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
8#include "src/gpu/tessellate/GrPathStencilFillOp.h"
9
10#include "src/gpu/GrEagerVertexAllocator.h"
11#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/GrRecordingContextPriv.h"
13#include "src/gpu/tessellate/GrFillPathShader.h"
14#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
15#include "src/gpu/tessellate/GrPathTessellator.h"
16#include "src/gpu/tessellate/GrStencilPathShader.h"
17#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
18
19using OpFlags = GrTessellationPathRenderer::OpFlags;
20
21void GrPathStencilFillOp::visitProxies(const VisitProxyFunc& fn) const {
22 if (fFillBBoxProgram) {
23 fFillBBoxProgram->pipeline().visitProxies(fn);
24 } else {
25 fProcessors.visitProxies(fn);
26 }
27}
28
29GrDrawOp::FixedFunctionFlags GrPathStencilFillOp::fixedFunctionFlags() const {
30 auto flags = FixedFunctionFlags::kUsesStencil;
31 if (fAAType != GrAAType::kNone) {
32 flags |= FixedFunctionFlags::kUsesHWAA;
33 }
34 return flags;
35}
36
37GrProcessorSet::Analysis GrPathStencilFillOp::finalize(const GrCaps& caps,
38 const GrAppliedClip* clip,
39 bool hasMixedSampledCoverage,
40 GrClampType clampType) {
41 return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr,
42 hasMixedSampledCoverage, caps, clampType, &fColor);
43}
44
45void GrPathStencilFillOp::prePreparePrograms(const GrPathShader::ProgramArgs& args,
46 GrAppliedClip&& appliedClip) {
47 using DrawInnerFan = GrPathIndirectTessellator::DrawInnerFan;
48 SkASSERT(!fStencilFanProgram);
49 SkASSERT(!fStencilPathProgram);
50 SkASSERT(!fFillBBoxProgram);
51
52 int numVerbs = fPath.countVerbs();
53 if (numVerbs <= 0) {
54 return;
55 }
56
57 // When there are only a few verbs, it seems to always be fastest to make a single indirect draw
58 // that contains both the inner triangles and the outer curves, instead of using hardware
59 // tessellation. Also take this path if tessellation is not supported.
60 bool drawTrianglesAsIndirectCurveDraw = (numVerbs < 50);
61 const GrPipeline* stencilPassPipeline = GrStencilPathShader::MakeStencilPassPipeline(
62 args, fAAType, fOpFlags, appliedClip.hardClip());
63 if (drawTrianglesAsIndirectCurveDraw || (fOpFlags & OpFlags::kDisableHWTessellation)) {
64 fTessellator = args.fArena->make<GrPathIndirectTessellator>(
65 fViewMatrix, fPath, DrawInnerFan(drawTrianglesAsIndirectCurveDraw));
66 if (!drawTrianglesAsIndirectCurveDraw) {
67 fStencilFanProgram = GrStencilPathShader::MakeStencilProgram<GrStencilTriangleShader>(
68 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
69 }
70 fStencilPathProgram = GrStencilPathShader::MakeStencilProgram<GrMiddleOutCubicShader>(
71 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
72 } else {
73 // The caller should have sent Flags::kDisableHWTessellation if it was not supported.
74 SkASSERT(args.fCaps->shaderCaps()->tessellationSupport());
75 // Next see if we can split up the inner triangles and outer curves into two draw calls.
76 // This allows for a more efficient inner fan topology that can reduce the rasterizer load
77 // by a large margin on complex paths, but also causes greater CPU overhead due to the extra
78 // shader switches and draw calls.
79 // NOTE: Raster-edge work is 1-dimensional, so we sum height and width instead of
80 // multiplying.
81 SkScalar scales[2];
82 SkAssertResult(fViewMatrix.getMinMaxScales(scales)); // Will fail if perspective.
83 const SkRect& bounds = fPath.getBounds();
84 float rasterEdgeWork = (bounds.height() + bounds.width()) * scales[1] * fPath.countVerbs();
85 if (rasterEdgeWork > 300 * 300) {
86 fTessellator = args.fArena->make<GrPathOuterCurveTessellator>();
87 fStencilFanProgram = GrStencilPathShader::MakeStencilProgram<GrStencilTriangleShader>(
88 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
89 fStencilPathProgram = GrStencilPathShader::MakeStencilProgram<GrCubicTessellateShader>(
90 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
91 } else {
92 // Fastest CPU approach: emit one cubic wedge per verb, fanning out from the center.
93 fTessellator = args.fArena->make<GrPathWedgeTessellator>();
94 fStencilPathProgram = GrStencilPathShader::MakeStencilProgram<GrWedgeTessellateShader>(
95 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
96 }
97 }
98
99 if (!(fOpFlags & OpFlags::kStencilOnly)) {
100 // Create a program that draws a bounding box over the path and fills its stencil coverage
101 // into the color buffer.
102 auto* bboxShader = args.fArena->make<GrFillBoundingBoxShader>(fViewMatrix, fColor,
103 fPath.getBounds());
104 auto* bboxPipeline = GrFillPathShader::MakeFillPassPipeline(args, fAAType,
105 std::move(appliedClip),
106 std::move(fProcessors));
107 auto* bboxStencil = GrFillPathShader::TestAndResetStencilSettings();
108 fFillBBoxProgram = GrPathShader::MakeProgram(args, bboxShader, bboxPipeline, bboxStencil);
109 }
110}
111
112void GrPathStencilFillOp::onPrePrepare(GrRecordingContext* context,
113 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
114 const GrXferProcessor::DstProxyView& dstProxyView,
115 GrXferBarrierFlags renderPassXferBarriers,
116 GrLoadOp colorLoadOp) {
117 this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView,
118 renderPassXferBarriers, colorLoadOp, context->priv().caps()},
119 (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
120 if (fStencilFanProgram) {
121 context->priv().recordProgramInfo(fStencilFanProgram);
122 }
123 if (fStencilPathProgram) {
124 context->priv().recordProgramInfo(fStencilPathProgram);
125 }
126 if (fFillBBoxProgram) {
127 context->priv().recordProgramInfo(fFillBBoxProgram);
128 }
129}
130
131void GrPathStencilFillOp::onPrepare(GrOpFlushState* flushState) {
132 if (!fTessellator) {
133 this->prePreparePrograms({flushState->allocator(), flushState->writeView(),
134 &flushState->dstProxyView(), flushState->renderPassBarriers(),
135 flushState->colorLoadOp(), &flushState->caps()},
136 flushState->detachAppliedClip());
137 if (!fTessellator) {
138 return;
139 }
140 }
141
142 if (fStencilFanProgram) {
143 // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a
144 // middle-out topology.
145 GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex);
146 int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon.
147 auto* triangleVertexData = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
148 fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan(
149 triangleVertexData, 3/*perTriangleVertexAdvance*/, fPath) * 3;
150 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
151 vertexAlloc.unlock(fFanVertexCount);
152 }
153
154 fTessellator->prepare(flushState, fViewMatrix, fPath);
155}
156
157void GrPathStencilFillOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
158 if (!fTessellator) {
159 return;
160 }
161
162 // Stencil the inner fan, if any.
163 if (fFanVertexCount > 0) {
164 SkASSERT(fStencilFanProgram);
165 SkASSERT(fFanBuffer);
166 flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds());
167 flushState->bindBuffers(nullptr, nullptr, fFanBuffer);
168 flushState->draw(fFanVertexCount, fFanBaseVertex);
169 }
170
171 // Stencil the rest of the path.
172 SkASSERT(fStencilPathProgram);
173 flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds());
174 fTessellator->draw(flushState);
175
176 // Fill in the bounding box (if not in stencil-only mode).
177 if (fFillBBoxProgram) {
178 flushState->bindPipelineAndScissorClip(*fFillBBoxProgram, this->bounds());
179 flushState->bindTextures(fFillBBoxProgram->primProc(), nullptr,
180 fFillBBoxProgram->pipeline());
181 flushState->bindBuffers(nullptr, nullptr, nullptr);
182 flushState->draw(4, 0);
183 }
184}