Debian Codegraph @

src/gpu/ganesh/effects/GrConvexPolyEffect.cpp

raw
1 /*
2 * Copyright 2014 Google Inc.
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/ganesh/effects/GrConvexPolyEffect.h"
9
10 #include "include/core/SkPath.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkString.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkFloatingPoint.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "src/base/SkRandom.h"
17 #include "src/core/SkColorData.h"
18 #include "src/core/SkPathEnums.h"
19 #include "src/core/SkPathPriv.h"
20 #include "src/core/SkSLTypeShared.h"
21 #include "src/gpu/KeyBuilder.h"
22 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
23 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
24 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
25
26 #include <algorithm>
27 #include <cstddef>
28 #include <cstdint>
29 #include <tuple>
30
31 struct GrShaderCaps;
32
33 //////////////////////////////////////////////////////////////////////////////
34
35 GrFPResult GrConvexPolyEffect::Make(std::unique_ptr<GrFragmentProcessor> inputFP,
36 GrClipEdgeType type, const SkPath& path) {
37 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) {
38 return GrFPFailure(std::move(inputFP));
39 }
40
41 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(path);
42 // The only way this should fail is if the clip is effectively a infinitely thin line. In that
43 // case nothing is inside the clip. It'd be nice to detect this at a higher level and either
44 // skip the draw or omit the clip element.
45 if (dir == SkPathFirstDirection::kUnknown) {
46 if (GrClipEdgeTypeIsInverseFill(type)) {
47 return GrFPSuccess(
48 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fWHITE));
49 }
50 // This could use ConstColor instead of ModulateRGBA but it would trigger a debug print
51 // about a coverage processor not being compatible with the alpha-as-coverage optimization.
52 // We don't really care about this unlikely case so we just use ModulateRGBA to suppress
53 // the print.
54 return GrFPSuccess(
55 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fTRANSPARENT));
56 }
57
58 SkScalar edges[3 * kMaxEdges];
59 SkPath::Iter iter(path, true);
60
61 // SkPath considers itself convex so long as there is a convex contour within it,
62 // regardless of any degenerate contours such as a string of moveTos before it.
63 // Iterate here to consume any degenerate contours and only process the points
64 // on the actual convex contour.
65 int n = 0;
66 while (auto rec = iter.next()) {
67 switch (rec->fVerb) {
68 case SkPathVerb::kMove:
69 case SkPathVerb::kClose:
70 break;
71 case SkPathVerb::kLine: {
72 SkSpan<const SkPoint> pts = rec->fPoints;
73 if (n >= kMaxEdges) {
74 return GrFPFailure(std::move(inputFP));
75 }
76 if (pts[0] != pts[1]) {
77 SkVector v = pts[1] - pts[0];
78 v.normalize();
79 if (SkPathFirstDirection::kCCW == dir) {
80 edges[3 * n] = v.fY;
81 edges[3 * n + 1] = -v.fX;
82 } else {
83 edges[3 * n] = -v.fY;
84 edges[3 * n + 1] = v.fX;
85 }
86 edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY);
87 ++n;
88 }
89 break;
90 }
91 default:
92 // Non-linear segment so not a polygon.
93 return GrFPFailure(std::move(inputFP));
94 }
95 }
96
97 if (path.isInverseFillType()) {
98 type = GrInvertClipEdgeType(type);
99 }
100 return GrConvexPolyEffect::Make(std::move(inputFP), type, n, edges);
101 }
102
103 GrConvexPolyEffect::~GrConvexPolyEffect() {}
104
105 void GrConvexPolyEffect::onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const {
106 static_assert(kGrClipEdgeTypeCnt <= 8);
107 uint32_t key = (fEdgeCount << 3) | static_cast<int>(fEdgeType);
108 b->add32(key);
109 }
110
111 std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrConvexPolyEffect::onMakeProgramImpl() const {
112 class Impl : public ProgramImpl {
113 public:
114 void emitCode(EmitArgs& args) override {
115 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
116
117 const char *edgeArrayName;
118 fEdgeUniform = args.fUniformHandler->addUniformArray(&cpe,
119 kFragment_GrShaderFlag,
120 SkSLType::kHalf3,
121 "edgeArray",
122 cpe.fEdgeCount,
123 &edgeArrayName);
124 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
125 fragBuilder->codeAppend("float alpha = 1.0;\n"
126 "float edge;\n");
127 for (int i = 0; i < cpe.fEdgeCount; ++i) {
128 fragBuilder->codeAppendf("edge = dot(float3(%s[%d]), sk_FragCoord.xy1);\n",
129 edgeArrayName, i);
130 if (GrClipEdgeTypeIsAA(cpe.fEdgeType)) {
131 fragBuilder->codeAppend("alpha *= saturate(edge);\n");
132 } else {
133 fragBuilder->codeAppend("alpha *= step(0.5, edge);\n");
134 }
135 }
136
137 if (GrClipEdgeTypeIsInverseFill(cpe.fEdgeType)) {
138 fragBuilder->codeAppend("alpha = 1.0 - alpha;\n");
139 }
140
141 SkString inputSample = this->invokeChild(/*childIndex=*/0, args);
142
143 fragBuilder->codeAppendf("return %s * half(alpha);\n", inputSample.c_str());
144 }
145
146 private:
147 void onSetData(const GrGLSLProgramDataManager& pdman,
148 const GrFragmentProcessor& fp) override {
149 const GrConvexPolyEffect& cpe = fp.cast<GrConvexPolyEffect>();
150 size_t n = 3*cpe.fEdgeCount;
151 if (!std::equal(fPrevEdges.begin(), fPrevEdges.begin() + n, cpe.fEdges.begin())) {
152 pdman.set3fv(fEdgeUniform, cpe.fEdgeCount, cpe.fEdges.data());
153 std::copy_n(cpe.fEdges.begin(), n, fPrevEdges.begin());
154 }
155 }
156
157 GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
158 std::array<float, 3 * GrConvexPolyEffect::kMaxEdges> fPrevEdges = {SK_FloatNaN};
159 };
160
161 return std::make_unique<Impl>();
162 }
163
164 GrConvexPolyEffect::GrConvexPolyEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
165 GrClipEdgeType edgeType,
166 int n,
167 const float edges[])
168 : INHERITED(kGrConvexPolyEffect_ClassID,
169 ProcessorOptimizationFlags(inputFP.get()) &
170 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
171 , fEdgeType(edgeType)
172 , fEdgeCount(n) {
173 // Factory function should have already ensured this.
174 SkASSERT(n <= kMaxEdges);
175 std::copy_n(edges, 3*n, fEdges.begin());
176 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
177 // and 100% covered in the non-AA case.
178 for (int i = 0; i < n; ++i) {
179 fEdges[3 * i + 2] += SK_ScalarHalf;
180 }
181
182 this->registerChild(std::move(inputFP));
183 }
184
185 GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that)
186 : INHERITED(that)
187 , fEdgeType(that.fEdgeType)
188 , fEdgeCount(that.fEdgeCount) {
189 std::copy_n(that.fEdges.begin(), 3*that.fEdgeCount, fEdges.begin());
190 }
191
192 std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const {
193 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this));
194 }
195
196 bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
197 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
198 int n = 3*cpe.fEdgeCount;
199 return cpe.fEdgeType == fEdgeType &&
200 cpe.fEdgeCount == fEdgeCount &&
201 std::equal(cpe.fEdges.begin(), cpe.fEdges.begin() + n, fEdges.begin());
202 }
203
204 //////////////////////////////////////////////////////////////////////////////
205
206 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect)
207
208 #if defined(GPU_TEST_UTILS)
209 std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
210 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
211 SkScalar edges[kMaxEdges * 3];
212 for (int i = 0; i < 3 * count; ++i) {
213 edges[i] = d->fRandom->nextSScalar1();
214 }
215
216 bool success;
217 std::unique_ptr<GrFragmentProcessor> fp = d->inputFP();
218 do {
219 GrClipEdgeType edgeType =
220 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
221 std::tie(success, fp) = GrConvexPolyEffect::Make(std::move(fp), edgeType, count, edges);
222 } while (!success);
223 return fp;
224 }
225 #endif