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/core/SkPathPriv.h" |
9 | #include "src/gpu/effects/GrConvexPolyEffect.h" |
10 | #include "src/gpu/effects/generated/GrAARectEffect.h" |
11 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
14 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
15 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
16 | |
17 | ////////////////////////////////////////////////////////////////////////////// |
18 | |
19 | class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor { |
20 | public: |
21 | GrGLConvexPolyEffect() { |
22 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPrevEdges); ++i) { |
23 | fPrevEdges[i] = SK_ScalarNaN; |
24 | } |
25 | } |
26 | |
27 | void emitCode(EmitArgs&) override; |
28 | |
29 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
30 | |
31 | protected: |
32 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
33 | |
34 | private: |
35 | GrGLSLProgramDataManager::UniformHandle fEdgeUniform; |
36 | SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges]; |
37 | typedef GrGLSLFragmentProcessor INHERITED; |
38 | }; |
39 | |
40 | void GrGLConvexPolyEffect::emitCode(EmitArgs& args) { |
41 | const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>(); |
42 | |
43 | const char *edgeArrayName; |
44 | fEdgeUniform = args.fUniformHandler->addUniformArray(&cpe, |
45 | kFragment_GrShaderFlag, |
46 | kHalf3_GrSLType, |
47 | "edges" , |
48 | cpe.getEdgeCount(), |
49 | &edgeArrayName); |
50 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
51 | fragBuilder->codeAppend("\t\thalf alpha = 1.0;\n" ); |
52 | fragBuilder->codeAppend("\t\thalf edge;\n" ); |
53 | for (int i = 0; i < cpe.getEdgeCount(); ++i) { |
54 | fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], half3(half(sk_FragCoord.x), " |
55 | "half(sk_FragCoord.y), " |
56 | "1));\n" , |
57 | edgeArrayName, i); |
58 | if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { |
59 | fragBuilder->codeAppend("\t\tedge = saturate(edge);\n" ); |
60 | } else { |
61 | fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n" ); |
62 | } |
63 | fragBuilder->codeAppend("\t\talpha *= edge;\n" ); |
64 | } |
65 | |
66 | if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) { |
67 | fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n" ); |
68 | } |
69 | fragBuilder->codeAppendf("\t%s = %s * alpha;\n" , args.fOutputColor, args.fInputColor); |
70 | } |
71 | |
72 | void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
73 | const GrFragmentProcessor& effect) { |
74 | const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>(); |
75 | size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); |
76 | if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { |
77 | pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); |
78 | memcpy(fPrevEdges, cpe.getEdges(), byteSize); |
79 | } |
80 | } |
81 | |
82 | void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
83 | GrProcessorKeyBuilder* b) { |
84 | const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>(); |
85 | static_assert(kGrClipEdgeTypeCnt <= 8); |
86 | uint32_t key = (cpe.getEdgeCount() << 3) | (int) cpe.getEdgeType(); |
87 | b->add32(key); |
88 | } |
89 | |
90 | ////////////////////////////////////////////////////////////////////////////// |
91 | |
92 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(GrClipEdgeType type, |
93 | const SkPath& path) { |
94 | if (GrClipEdgeType::kHairlineAA == type) { |
95 | return nullptr; |
96 | } |
97 | if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || |
98 | !path.isConvex()) { |
99 | return nullptr; |
100 | } |
101 | |
102 | SkPathPriv::FirstDirection dir; |
103 | // The only way this should fail is if the clip is effectively a infinitely thin line. In that |
104 | // case nothing is inside the clip. It'd be nice to detect this at a higher level and either |
105 | // skip the draw or omit the clip element. |
106 | if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) { |
107 | if (GrProcessorEdgeTypeIsInverseFill(type)) { |
108 | return GrConstColorProcessor::Make(SK_PMColor4fWHITE, |
109 | GrConstColorProcessor::InputMode::kModulateRGBA); |
110 | } |
111 | // This could use kIgnore instead of kModulateRGBA but it would trigger a debug print |
112 | // about a coverage processor not being compatible with the alpha-as-coverage optimization. |
113 | // We don't really care about this unlikely case so we just use kModulateRGBA to suppress |
114 | // the print. |
115 | return GrConstColorProcessor::Make(SK_PMColor4fTRANSPARENT, |
116 | GrConstColorProcessor::InputMode::kModulateRGBA); |
117 | } |
118 | |
119 | SkScalar edges[3 * kMaxEdges]; |
120 | SkPoint pts[4]; |
121 | SkPath::Verb verb; |
122 | SkPath::Iter iter(path, true); |
123 | |
124 | // SkPath considers itself convex so long as there is a convex contour within it, |
125 | // regardless of any degenerate contours such as a string of moveTos before it. |
126 | // Iterate here to consume any degenerate contours and only process the points |
127 | // on the actual convex contour. |
128 | int n = 0; |
129 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
130 | switch (verb) { |
131 | case SkPath::kMove_Verb: |
132 | SkASSERT(n == 0); |
133 | case SkPath::kClose_Verb: |
134 | break; |
135 | case SkPath::kLine_Verb: { |
136 | if (n >= kMaxEdges) { |
137 | return nullptr; |
138 | } |
139 | if (pts[0] != pts[1]) { |
140 | SkVector v = pts[1] - pts[0]; |
141 | v.normalize(); |
142 | if (SkPathPriv::kCCW_FirstDirection == dir) { |
143 | edges[3 * n] = v.fY; |
144 | edges[3 * n + 1] = -v.fX; |
145 | } else { |
146 | edges[3 * n] = -v.fY; |
147 | edges[3 * n + 1] = v.fX; |
148 | } |
149 | edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY); |
150 | ++n; |
151 | } |
152 | break; |
153 | } |
154 | default: |
155 | return nullptr; |
156 | } |
157 | } |
158 | |
159 | if (path.isInverseFillType()) { |
160 | type = GrInvertProcessorEdgeType(type); |
161 | } |
162 | return Make(type, n, edges); |
163 | } |
164 | |
165 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(GrClipEdgeType edgeType, |
166 | const SkRect& rect) { |
167 | if (GrClipEdgeType::kHairlineAA == edgeType){ |
168 | return nullptr; |
169 | } |
170 | return GrAARectEffect::Make(edgeType, rect); |
171 | } |
172 | |
173 | GrConvexPolyEffect::~GrConvexPolyEffect() {} |
174 | |
175 | void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
176 | GrProcessorKeyBuilder* b) const { |
177 | GrGLConvexPolyEffect::GenKey(*this, caps, b); |
178 | } |
179 | |
180 | GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const { |
181 | return new GrGLConvexPolyEffect; |
182 | } |
183 | |
184 | GrConvexPolyEffect::GrConvexPolyEffect(GrClipEdgeType edgeType, int n, const SkScalar edges[]) |
185 | : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
186 | , fEdgeType(edgeType) |
187 | , fEdgeCount(n) { |
188 | // Factory function should have already ensured this. |
189 | SkASSERT(n <= kMaxEdges); |
190 | memcpy(fEdges, edges, 3 * n * sizeof(SkScalar)); |
191 | // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case |
192 | // and 100% covered in the non-AA case. |
193 | for (int i = 0; i < n; ++i) { |
194 | fEdges[3 * i + 2] += SK_ScalarHalf; |
195 | } |
196 | } |
197 | |
198 | GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that) |
199 | : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
200 | , fEdgeType(that.fEdgeType) |
201 | , fEdgeCount(that.fEdgeCount) { |
202 | memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar)); |
203 | } |
204 | |
205 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const { |
206 | return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this)); |
207 | } |
208 | |
209 | bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const { |
210 | const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>(); |
211 | // ignore the fact that 0 == -0 and just use memcmp. |
212 | return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount && |
213 | 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar))); |
214 | } |
215 | |
216 | ////////////////////////////////////////////////////////////////////////////// |
217 | |
218 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect); |
219 | |
220 | #if GR_TEST_UTILS |
221 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) { |
222 | int count = d->fRandom->nextULessThan(kMaxEdges) + 1; |
223 | SkScalar edges[kMaxEdges * 3]; |
224 | for (int i = 0; i < 3 * count; ++i) { |
225 | edges[i] = d->fRandom->nextSScalar1(); |
226 | } |
227 | |
228 | std::unique_ptr<GrFragmentProcessor> fp; |
229 | do { |
230 | GrClipEdgeType edgeType = static_cast<GrClipEdgeType>( |
231 | d->fRandom->nextULessThan(kGrClipEdgeTypeCnt)); |
232 | fp = GrConvexPolyEffect::Make(edgeType, count, edges); |
233 | } while (nullptr == fp); |
234 | return fp; |
235 | } |
236 | #endif |
237 | |