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