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
13#include "src/gpu/GrTexture.h"
14#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
17#include "src/sksl/SkSLCPP.h"
18#include "src/sksl/SkSLUtil.h"
19class GrGLSLCircleEffect : public GrGLSLFragmentProcessor {
20public:
21 GrGLSLCircleEffect() {}
22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrCircleEffect& _outer = args.fFp.cast<GrCircleEffect>();
25 (void)_outer;
26 auto edgeType = _outer.edgeType;
27 (void)edgeType;
28 auto center = _outer.center;
29 (void)center;
30 auto radius = _outer.radius;
31 (void)radius;
32 prevRadius = -1.0;
33 circleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
34 kFloat4_GrSLType, "circle");
35 fragBuilder->codeAppendf(
36 "float2 prevCenter;\nfloat prevRadius = %f;\nhalf d;\n@if (%d == 2 || %d == 3) {\n "
37 " d = half((length((%s.xy - sk_FragCoord.xy) * %s.w) - 1.0) * %s.z);\n} else {\n "
38 " d = half((1.0 - length((%s.xy - sk_FragCoord.xy) * %s.w)) * %s.z);\n}\n@if "
39 "((%d == 1 || %d == 3) || %d == 4) {\n %s = %s * clamp(d, 0.0, 1.0);\n} else "
40 "{\n %s = d > 0.5 ? %s : half4(0.0);\n}\n",
41 prevRadius, (int)_outer.edgeType, (int)_outer.edgeType,
42 args.fUniformHandler->getUniformCStr(circleVar),
43 args.fUniformHandler->getUniformCStr(circleVar),
44 args.fUniformHandler->getUniformCStr(circleVar),
45 args.fUniformHandler->getUniformCStr(circleVar),
46 args.fUniformHandler->getUniformCStr(circleVar),
47 args.fUniformHandler->getUniformCStr(circleVar), (int)_outer.edgeType,
48 (int)_outer.edgeType, (int)_outer.edgeType, args.fOutputColor, args.fInputColor,
49 args.fOutputColor, args.fInputColor);
50 }
51
52private:
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 = circleVar;
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 = std::max(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 = float2(0);
82 float prevRadius = 0;
83 UniformHandle circleVar;
84};
85GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const {
86 return new GrGLSLCircleEffect();
87}
88void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
89 GrProcessorKeyBuilder* b) const {
90 b->add32((int32_t)edgeType);
91}
92bool GrCircleEffect::onIsEqual(const GrFragmentProcessor& other) const {
93 const GrCircleEffect& that = other.cast<GrCircleEffect>();
94 (void)that;
95 if (edgeType != that.edgeType) return false;
96 if (center != that.center) return false;
97 if (radius != that.radius) return false;
98 return true;
99}
100GrCircleEffect::GrCircleEffect(const GrCircleEffect& src)
101 : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags())
102 , edgeType(src.edgeType)
103 , center(src.center)
104 , radius(src.radius) {}
105std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const {
106 return std::unique_ptr<GrFragmentProcessor>(new GrCircleEffect(*this));
107}
108GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect);
109#if GR_TEST_UTILS
110std::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