Create GLSLUniformHandler class for gpu backend
[platform/upstream/libSkiaSharp.git] / src / gpu / effects / GrConvexPolyEffect.cpp
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 "GrInvariantOutput.h"
10 #include "SkPathPriv.h"
11 #include "glsl/GrGLSLFragmentProcessor.h"
12 #include "glsl/GrGLSLFragmentShaderBuilder.h"
13 #include "glsl/GrGLSLProgramDataManager.h"
14 #include "glsl/GrGLSLUniformHandler.h"
15
16 //////////////////////////////////////////////////////////////////////////////
17 class AARectEffect : public GrFragmentProcessor {
18 public:
19     const SkRect& getRect() const { return fRect; }
20
21     static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
22         return new AARectEffect(edgeType, rect);
23     }
24
25     GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
26
27     const char* name() const override { return "AARect"; }
28
29     void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
30
31 private:
32     AARectEffect(GrPrimitiveEdgeType edgeType, const SkRect& rect)
33         : fRect(rect), fEdgeType(edgeType) {
34         this->initClassID<AARectEffect>();
35         this->setWillReadFragmentPosition();
36     }
37
38     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
39
40     bool onIsEqual(const GrFragmentProcessor& other) const override {
41         const AARectEffect& aare = other.cast<AARectEffect>();
42         return fRect == aare.fRect;
43     }
44
45     void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
46         if (fRect.isEmpty()) {
47             // An empty rect will have no coverage anywhere.
48             inout->mulByKnownSingleComponent(0);
49         } else {
50             inout->mulByUnknownSingleComponent();
51         }
52     }
53
54     SkRect              fRect;
55     GrPrimitiveEdgeType fEdgeType;
56
57     typedef GrFragmentProcessor INHERITED;
58
59     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
60
61 };
62
63 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect);
64
65 const GrFragmentProcessor* AARectEffect::TestCreate(GrProcessorTestData* d) {
66     SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
67                                    d->fRandom->nextSScalar1(),
68                                    d->fRandom->nextSScalar1(),
69                                    d->fRandom->nextSScalar1());
70     GrFragmentProcessor* fp;
71     do {
72         GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
73                 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
74
75         fp = AARectEffect::Create(edgeType, rect);
76     } while (nullptr == fp);
77     return fp;
78 }
79
80 //////////////////////////////////////////////////////////////////////////////
81
82 class GLAARectEffect : public GrGLSLFragmentProcessor {
83 public:
84     GLAARectEffect(const GrProcessor&);
85
86     virtual void emitCode(EmitArgs&) override;
87
88     static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
89
90 protected:
91     void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
92
93 private:
94     GrGLSLProgramDataManager::UniformHandle fRectUniform;
95     SkRect                                fPrevRect;
96     typedef GrGLSLFragmentProcessor INHERITED;
97 };
98
99 GLAARectEffect::GLAARectEffect(const GrProcessor& effect) {
100     fPrevRect.fLeft = SK_ScalarNaN;
101 }
102
103 void GLAARectEffect::emitCode(EmitArgs& args) {
104     const AARectEffect& aare = args.fFp.cast<AARectEffect>();
105     const char *rectName;
106     // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
107     // respectively.
108     fRectUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
109                                                     kVec4f_GrSLType,
110                                                     kDefault_GrSLPrecision,
111                                                     "rect",
112                                                     &rectName);
113
114     GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
115     const char* fragmentPos = fragBuilder->fragmentPosition();
116     if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
117         // The amount of coverage removed in x and y by the edges is computed as a pair of negative
118         // numbers, xSub and ySub.
119         fragBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
120         fragBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
121         fragBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
122         fragBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
123         fragBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
124         // Now compute coverage in x and y and multiply them to get the fraction of the pixel
125         // covered.
126         fragBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
127     } else {
128         fragBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
129         fragBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
130         fragBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
131         fragBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
132         fragBuilder->codeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
133     }
134
135     if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
136         fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
137     }
138     fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
139                              (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
140 }
141
142 void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
143                                const GrProcessor& processor) {
144     const AARectEffect& aare = processor.cast<AARectEffect>();
145     const SkRect& rect = aare.getRect();
146     if (rect != fPrevRect) {
147         pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
148                    rect.fRight - 0.5f, rect.fBottom - 0.5f);
149         fPrevRect = rect;
150     }
151 }
152
153 void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
154                             GrProcessorKeyBuilder* b) {
155     const AARectEffect& aare = processor.cast<AARectEffect>();
156     b->add32(aare.getEdgeType());
157 }
158
159 void AARectEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
160     GLAARectEffect::GenKey(*this, caps, b);
161 }
162
163 GrGLSLFragmentProcessor* AARectEffect::onCreateGLSLInstance() const  {
164     return new GLAARectEffect(*this);
165 }
166
167 //////////////////////////////////////////////////////////////////////////////
168
169 class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
170 public:
171     GrGLConvexPolyEffect(const GrProcessor&);
172
173     virtual void emitCode(EmitArgs&) override;
174
175     static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
176
177 protected:
178     void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
179
180 private:
181     GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
182     SkScalar                              fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
183     typedef GrGLSLFragmentProcessor INHERITED;
184 };
185
186 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) {
187     fPrevEdges[0] = SK_ScalarNaN;
188 }
189
190 void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
191     const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
192
193     const char *edgeArrayName;
194     fEdgeUniform = args.fUniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment_Visibility,
195                                                          kVec3f_GrSLType,
196                                                          kDefault_GrSLPrecision,
197                                                          "edges",
198                                                          cpe.getEdgeCount(),
199                                                          &edgeArrayName);
200     GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
201     fragBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
202     fragBuilder->codeAppend("\t\tfloat edge;\n");
203     const char* fragmentPos = fragBuilder->fragmentPosition();
204     for (int i = 0; i < cpe.getEdgeCount(); ++i) {
205         fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
206                                  edgeArrayName, i, fragmentPos, fragmentPos);
207         if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
208             fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
209         } else {
210             fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
211         }
212         fragBuilder->codeAppend("\t\talpha *= edge;\n");
213     }
214
215     if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
216         fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
217     }
218     fragBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
219                              (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
220 }
221
222 void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
223                                      const GrProcessor& effect) {
224     const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
225     size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
226     if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
227         pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
228         memcpy(fPrevEdges, cpe.getEdges(), byteSize);
229     }
230 }
231
232 void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
233                                   GrProcessorKeyBuilder* b) {
234     const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
235     GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
236     uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
237     b->add32(key);
238 }
239
240 //////////////////////////////////////////////////////////////////////////////
241
242 GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
243                                                 const SkVector* offset) {
244     if (kHairlineAA_GrProcessorEdgeType == type) {
245         return nullptr;
246     }
247     if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
248         !path.isConvex()) {
249         return nullptr;
250     }
251
252     if (path.countPoints() > kMaxEdges) {
253         return nullptr;
254     }
255
256     SkPoint pts[kMaxEdges];
257     SkScalar edges[3 * kMaxEdges];
258
259     SkPathPriv::FirstDirection dir;
260     SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
261
262     SkVector t;
263     if (nullptr == offset) {
264         t.set(0, 0);
265     } else {
266         t = *offset;
267     }
268
269     int count = path.getPoints(pts, kMaxEdges);
270     int n = 0;
271     for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
272         if (pts[lastPt] != pts[i]) {
273             SkVector v = pts[i] - pts[lastPt];
274             v.normalize();
275             if (SkPathPriv::kCCW_FirstDirection == dir) {
276                 edges[3 * n] = v.fY;
277                 edges[3 * n + 1] = -v.fX;
278             } else {
279                 edges[3 * n] = -v.fY;
280                 edges[3 * n + 1] = v.fX;
281             }
282             SkPoint p = pts[i] + t;
283             edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
284             ++n;
285         }
286     }
287     if (path.isInverseFillType()) {
288         type = GrInvertProcessorEdgeType(type);
289     }
290     return Create(type, n, edges);
291 }
292
293 GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
294     if (kHairlineAA_GrProcessorEdgeType == edgeType){
295         return nullptr;
296     }
297     return AARectEffect::Create(edgeType, rect);
298 }
299
300 GrConvexPolyEffect::~GrConvexPolyEffect() {}
301
302 void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
303     inout->mulByUnknownSingleComponent();
304 }
305
306 void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
307                                                GrProcessorKeyBuilder* b) const {
308     GrGLConvexPolyEffect::GenKey(*this, caps, b);
309 }
310
311 GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const  {
312     return new GrGLConvexPolyEffect(*this);
313 }
314
315 GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
316     : fEdgeType(edgeType)
317     , fEdgeCount(n) {
318     this->initClassID<GrConvexPolyEffect>();
319     // Factory function should have already ensured this.
320     SkASSERT(n <= kMaxEdges);
321     memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
322     // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
323     // and 100% covered in the non-AA case.
324     for (int i = 0; i < n; ++i) {
325         fEdges[3 * i + 2] += SK_ScalarHalf;
326     }
327     this->setWillReadFragmentPosition();
328 }
329
330 bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
331     const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
332     // ignore the fact that 0 == -0 and just use memcmp.
333     return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
334             0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
335 }
336
337 //////////////////////////////////////////////////////////////////////////////
338
339 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
340
341 const GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
342     int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
343     SkScalar edges[kMaxEdges * 3];
344     for (int i = 0; i < 3 * count; ++i) {
345         edges[i] = d->fRandom->nextSScalar1();
346     }
347
348     GrFragmentProcessor* fp;
349     do {
350         GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
351                 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
352         fp = GrConvexPolyEffect::Create(edgeType, count, edges);
353     } while (nullptr == fp);
354     return fp;
355 }