blob: c3f4ef68c8ca1787b57ab95ad49f22babcd4b6b4 [file] [log] [blame]
Chris Daltonb832ce62020-01-06 19:49:37 -07001/*
2 * Copyright 2019 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/GrTessellatePathOp.h"
9
10#include "src/gpu/GrGpu.h"
11#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/GrOpsRenderPass.h"
13#include "src/gpu/GrProgramInfo.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070014#include "src/gpu/tessellate/GrCoverShader.h"
Chris Dalton40a1cee2020-01-16 01:29:37 -070015#include "src/gpu/tessellate/GrPathParser.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070016#include "src/gpu/tessellate/GrTessellateWedgeShader.h"
17
18GrTessellatePathOp::FixedFunctionFlags GrTessellatePathOp::fixedFunctionFlags() const {
19 auto flags = FixedFunctionFlags::kUsesStencil;
20 if (GrAAType::kNone != fAAType) {
21 flags |= FixedFunctionFlags::kUsesHWAA;
22 }
23 return flags;
24}
25
26void GrTessellatePathOp::onPrepare(GrOpFlushState* state) {
Chris Dalton40a1cee2020-01-16 01:29:37 -070027 int maxVertexCount = GrPathParser::MaxPossibleWedgeVertices(fPath);
28 if (auto* wedgeData = (SkPoint*)state->makeVertexSpace(
29 sizeof(SkPoint), maxVertexCount, &fWedgeBuffer, &fBaseWedgeVertex)) {
30 fWedgeVertexCount = GrPathParser::EmitCenterWedges(fPath, wedgeData);
31 state->putBackVertices(maxVertexCount - fWedgeVertexCount, sizeof(SkPoint));
Chris Daltonb832ce62020-01-06 19:49:37 -070032 }
33}
34
35void GrTessellatePathOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
36 if (!fWedgeBuffer) {
37 return;
38 }
39
40 GrAppliedClip clip = state->detachAppliedClip();
41 GrPipeline::FixedDynamicState fixedDynamicState;
42 if (clip.scissorState().enabled()) {
43 fixedDynamicState.fScissorRect = clip.scissorState().rect();
44 }
45
46 this->drawStencilPass(state, clip.hardClip(), &fixedDynamicState);
47 if (!(Flags::kStencilOnly & fFlags)) {
48 this->drawCoverPass(state, std::move(clip), &fixedDynamicState);
49 }
50}
51
52void GrTessellatePathOp::drawStencilPass(GrOpFlushState* state, const GrAppliedHardClip& hardClip,
53 const GrPipeline::FixedDynamicState* fixedDynamicState) {
54 // Increments clockwise triangles and decrements counterclockwise. Used for "winding" fill.
55 constexpr static GrUserStencilSettings kIncrDecrStencil(
56 GrUserStencilSettings::StaticInitSeparate<
57 0x0000, 0x0000,
58 GrUserStencilTest::kAlwaysIfInClip, GrUserStencilTest::kAlwaysIfInClip,
59 0xffff, 0xffff,
60 GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap,
61 GrUserStencilOp::kKeep, GrUserStencilOp::kKeep,
62 0xffff, 0xffff>());
63
64 // Inverts the bottom stencil bit. Used for "even/odd" fill.
65 constexpr static GrUserStencilSettings kInvertStencil(
66 GrUserStencilSettings::StaticInit<
67 0x0000,
68 GrUserStencilTest::kAlwaysIfInClip,
69 0xffff,
70 GrUserStencilOp::kInvert,
71 GrUserStencilOp::kKeep,
72 0x0001>());
73
74 GrPipeline::InitArgs initArgs;
75 if (GrAAType::kNone != fAAType) {
76 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
77 }
78 if (state->caps().wireframeSupport() && (Flags::kWireframe & fFlags)) {
79 initArgs.fInputFlags |= GrPipeline::InputFlags::kWireframe;
80 }
81 SkASSERT(SkPathFillType::kWinding == fPath.getFillType() ||
82 SkPathFillType::kEvenOdd == fPath.getFillType());
83 initArgs.fUserStencil = (SkPathFillType::kWinding == fPath.getFillType()) ?
84 &kIncrDecrStencil : &kInvertStencil;
85 initArgs.fCaps = &state->caps();
86
87 GrPipeline pipeline(initArgs, GrDisableColorXPFactory::MakeXferProcessor(), hardClip);
88 GrTessellateWedgeShader shader(fViewMatrix);
89 GrProgramInfo programInfo(state->proxy()->numSamples(), state->proxy()->numStencilSamples(),
90 state->proxy()->backendFormat(), state->view()->origin(), &pipeline,
91 &shader, fixedDynamicState, nullptr, 0,
92 GrPrimitiveType::kPatches, 5);
93
94 GrMesh mesh(GrPrimitiveType::kPatches, 5);
Chris Dalton40a1cee2020-01-16 01:29:37 -070095 mesh.setNonIndexedNonInstanced(fWedgeVertexCount);
Chris Daltonb832ce62020-01-06 19:49:37 -070096 mesh.setVertexData(fWedgeBuffer, fBaseWedgeVertex);
97
98 state->opsRenderPass()->draw(programInfo, &mesh, 1, this->bounds());
99
100 // http://skbug.com/9739
101 if (state->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) {
102 state->gpu()->insertManualFramebufferBarrier();
103 }
104}
105
106void GrTessellatePathOp::drawCoverPass(GrOpFlushState* state, GrAppliedClip&& clip,
107 const GrPipeline::FixedDynamicState* fixedDynamicState) {
108 // Allows non-zero stencil values to pass and write a color, and resets the stencil value back
109 // to zero; discards immediately on stencil values of zero.
110 // NOTE: It's ok to not check the clip here because the previous stencil pass only wrote to
111 // samples already inside the clip.
112 constexpr static GrUserStencilSettings kTestAndResetStencil(
113 GrUserStencilSettings::StaticInit<
114 0x0000,
115 GrUserStencilTest::kNotEqual,
116 0xffff,
117 GrUserStencilOp::kZero,
118 GrUserStencilOp::kKeep,
119 0xffff>());
120
121 GrPipeline::InitArgs initArgs;
122 if (GrAAType::kNone != fAAType) {
123 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
124 if (1 == state->proxy()->numSamples()) {
125 SkASSERT(GrAAType::kCoverage == fAAType);
126 // We are mixed sampled. Use conservative raster to make the sample coverage mask 100%
127 // at every fragment. This way we will still get a double hit on shared edges, but
128 // whichever side comes first will cover every sample and will clear the stencil. The
129 // other side will then be discarded and not cause a double blend.
130 initArgs.fInputFlags |= GrPipeline::InputFlags::kConservativeRaster;
131 }
132 }
133 initArgs.fUserStencil = &kTestAndResetStencil;
134 initArgs.fCaps = &state->caps();
135 initArgs.fDstProxyView = state->drawOpArgs().dstProxyView();
136 initArgs.fOutputSwizzle = state->drawOpArgs().outputSwizzle();
137
138 GrPipeline pipeline(initArgs, std::move(fProcessors), std::move(clip));
139 GrCoverShader shader(fViewMatrix, fPath.getBounds(), fColor);
140 GrProgramInfo programInfo(state->proxy()->numSamples(), state->proxy()->numStencilSamples(),
141 state->proxy()->backendFormat(), state->view()->origin(), &pipeline,
142 &shader, fixedDynamicState, nullptr, 0,
143 GrPrimitiveType::kTriangleStrip);
144
145 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
146 mesh.setNonIndexedNonInstanced(4);
147
148 state->opsRenderPass()->draw(programInfo, &mesh, 1, this->bounds());
149}