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