1 /*
2  * Copyright 2017 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 /**************************************************************************************************
9  *** This file was autogenerated from GrCircleEffect.fp; do not modify.
10  **************************************************************************************************/
11 #include "GrCircleEffect.h"
12 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "glsl/GrGLSLProgramBuilder.h"
15 #include "GrTexture.h"
16 #include "SkSLCPP.h"
17 #include "SkSLUtil.h"
18 class GrGLSLCircleEffect : public GrGLSLFragmentProcessor {
19 public:
GrGLSLCircleEffect()20     GrGLSLCircleEffect() {}
emitCode(EmitArgs & args)21     void emitCode(EmitArgs& args) override {
22         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
23         const GrCircleEffect& _outer = args.fFp.cast<GrCircleEffect>();
24         (void)_outer;
25         auto edgeType = _outer.edgeType();
26         (void)edgeType;
27         auto center = _outer.center();
28         (void)center;
29         auto radius = _outer.radius();
30         (void)radius;
31         prevRadius = -1.0;
32         fCircleVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
33                                                       kDefault_GrSLPrecision, "circle");
34         fragBuilder->codeAppendf(
35                 "half2 prevCenter;\nhalf prevRadius = %f;\nhalf d;\n@if (%d == 2 || %d == 3) {\n   "
36                 " d = (float(length((%s.xy - half2(sk_FragCoord.xy)) * %s.w)) - 1.0) * %s.z;\n} "
37                 "else {\n    d = half((1.0 - float(length((%s.xy - half2(sk_FragCoord.xy)) * "
38                 "%s.w))) * float(%s.z));\n}\n@if ((%d == 1 || %d == 3) || %d == 4) {\n    d = "
39                 "half(clamp(float(d), 0.0, 1.0));\n} else {\n    d = half(float(d) > 0.5 ? 1.0 : "
40                 "0.0);\n}\n%s = %s * d;\n",
41                 prevRadius, (int)_outer.edgeType(), (int)_outer.edgeType(),
42                 args.fUniformHandler->getUniformCStr(fCircleVar),
43                 args.fUniformHandler->getUniformCStr(fCircleVar),
44                 args.fUniformHandler->getUniformCStr(fCircleVar),
45                 args.fUniformHandler->getUniformCStr(fCircleVar),
46                 args.fUniformHandler->getUniformCStr(fCircleVar),
47                 args.fUniformHandler->getUniformCStr(fCircleVar), (int)_outer.edgeType(),
48                 (int)_outer.edgeType(), (int)_outer.edgeType(), args.fOutputColor,
49                 args.fInputColor);
50     }
51 
52 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)53     void onSetData(const GrGLSLProgramDataManager& pdman,
54                    const GrFragmentProcessor& _proc) override {
55         const GrCircleEffect& _outer = _proc.cast<GrCircleEffect>();
56         auto edgeType = _outer.edgeType();
57         (void)edgeType;
58         auto center = _outer.center();
59         (void)center;
60         auto radius = _outer.radius();
61         (void)radius;
62         UniformHandle& circle = fCircleVar;
63         (void)circle;
64 
65         if (radius != prevRadius || center != prevCenter) {
66             SkScalar effectiveRadius = radius;
67             if (GrProcessorEdgeTypeIsInverseFill((GrClipEdgeType)edgeType)) {
68                 effectiveRadius -= 0.5f;
69                 // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the
70                 // shader.
71                 effectiveRadius = SkTMax(0.001f, effectiveRadius);
72             } else {
73                 effectiveRadius += 0.5f;
74             }
75             pdman.set4f(circle, center.fX, center.fY, effectiveRadius,
76                         SkScalarInvert(effectiveRadius));
77             prevCenter = center;
78             prevRadius = radius;
79         }
80     }
81     SkPoint prevCenter = half2(0);
82     float prevRadius = 0;
83     UniformHandle fCircleVar;
84 };
onCreateGLSLInstance() const85 GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const {
86     return new GrGLSLCircleEffect();
87 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const88 void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
89                                            GrProcessorKeyBuilder* b) const {
90     b->add32((int32_t)fEdgeType);
91 }
onIsEqual(const GrFragmentProcessor & other) const92 bool GrCircleEffect::onIsEqual(const GrFragmentProcessor& other) const {
93     const GrCircleEffect& that = other.cast<GrCircleEffect>();
94     (void)that;
95     if (fEdgeType != that.fEdgeType) return false;
96     if (fCenter != that.fCenter) return false;
97     if (fRadius != that.fRadius) return false;
98     return true;
99 }
GrCircleEffect(const GrCircleEffect & src)100 GrCircleEffect::GrCircleEffect(const GrCircleEffect& src)
101         : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags())
102         , fEdgeType(src.fEdgeType)
103         , fCenter(src.fCenter)
104         , fRadius(src.fRadius) {}
clone() const105 std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const {
106     return std::unique_ptr<GrFragmentProcessor>(new GrCircleEffect(*this));
107 }
108 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect);
109 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * testData)110 std::unique_ptr<GrFragmentProcessor> GrCircleEffect::TestCreate(GrProcessorTestData* testData) {
111     SkPoint center;
112     center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f);
113     center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f);
114     SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f);
115     GrClipEdgeType et;
116     do {
117         et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
118     } while (GrClipEdgeType::kHairlineAA == et);
119     return GrCircleEffect::Make(et, center, radius);
120 }
121 #endif
122