| 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/core/SkUtils.h" |
| 14 | #include "src/gpu/GrTexture.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 18 | #include "src/sksl/SkSLCPP.h" |
| 19 | #include "src/sksl/SkSLUtil.h" |
| 20 | class GrGLSLCircleEffect : public GrGLSLFragmentProcessor { |
| 21 | public: |
| 22 | GrGLSLCircleEffect() {} |
| 23 | void emitCode(EmitArgs& args) override { |
| 24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 25 | const GrCircleEffect& _outer = args.fFp.cast<GrCircleEffect>(); |
| 26 | (void)_outer; |
| 27 | auto edgeType = _outer.edgeType; |
| 28 | (void)edgeType; |
| 29 | auto center = _outer.center; |
| 30 | (void)center; |
| 31 | auto radius = _outer.radius; |
| 32 | (void)radius; |
| 33 | prevRadius = -1.0; |
| 34 | circleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 35 | kFloat4_GrSLType, "circle" ); |
| 36 | fragBuilder->codeAppendf( |
| 37 | R"SkSL(float2 prevCenter; |
| 38 | float prevRadius = %f; |
| 39 | half d; |
| 40 | @if (%d == 2 || %d == 3) { |
| 41 | d = half((length((%s.xy - sk_FragCoord.xy) * %s.w) - 1.0) * %s.z); |
| 42 | } else { |
| 43 | d = half((1.0 - length((%s.xy - sk_FragCoord.xy) * %s.w)) * %s.z); |
| 44 | })SkSL" , |
| 45 | prevRadius, (int)_outer.edgeType, (int)_outer.edgeType, |
| 46 | args.fUniformHandler->getUniformCStr(circleVar), |
| 47 | args.fUniformHandler->getUniformCStr(circleVar), |
| 48 | args.fUniformHandler->getUniformCStr(circleVar), |
| 49 | args.fUniformHandler->getUniformCStr(circleVar), |
| 50 | args.fUniformHandler->getUniformCStr(circleVar), |
| 51 | args.fUniformHandler->getUniformCStr(circleVar)); |
| 52 | SkString _sample2509 = this->invokeChild(0, args); |
| 53 | fragBuilder->codeAppendf( |
| 54 | R"SkSL( |
| 55 | half4 inputColor = %s; |
| 56 | @if (%d == 1 || %d == 3) { |
| 57 | %s = inputColor * clamp(d, 0.0, 1.0); |
| 58 | } else { |
| 59 | %s = d > 0.5 ? inputColor : half4(0.0); |
| 60 | } |
| 61 | )SkSL" , |
| 62 | _sample2509.c_str(), (int)_outer.edgeType, (int)_outer.edgeType, args.fOutputColor, |
| 63 | args.fOutputColor); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 68 | const GrFragmentProcessor& _proc) override { |
| 69 | const GrCircleEffect& _outer = _proc.cast<GrCircleEffect>(); |
| 70 | auto edgeType = _outer.edgeType; |
| 71 | (void)edgeType; |
| 72 | auto center = _outer.center; |
| 73 | (void)center; |
| 74 | auto radius = _outer.radius; |
| 75 | (void)radius; |
| 76 | UniformHandle& circle = circleVar; |
| 77 | (void)circle; |
| 78 | |
| 79 | if (radius != prevRadius || center != prevCenter) { |
| 80 | SkScalar effectiveRadius = radius; |
| 81 | if (GrProcessorEdgeTypeIsInverseFill((GrClipEdgeType)edgeType)) { |
| 82 | effectiveRadius -= 0.5f; |
| 83 | // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the |
| 84 | // shader. |
| 85 | effectiveRadius = std::max(0.001f, effectiveRadius); |
| 86 | } else { |
| 87 | effectiveRadius += 0.5f; |
| 88 | } |
| 89 | pdman.set4f(circle, center.fX, center.fY, effectiveRadius, |
| 90 | SkScalarInvert(effectiveRadius)); |
| 91 | prevCenter = center; |
| 92 | prevRadius = radius; |
| 93 | } |
| 94 | } |
| 95 | SkPoint prevCenter = float2(0); |
| 96 | float prevRadius = 0; |
| 97 | UniformHandle circleVar; |
| 98 | }; |
| 99 | GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const { |
| 100 | return new GrGLSLCircleEffect(); |
| 101 | } |
| 102 | void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 103 | GrProcessorKeyBuilder* b) const { |
| 104 | b->add32((uint32_t)edgeType); |
| 105 | } |
| 106 | bool GrCircleEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 107 | const GrCircleEffect& that = other.cast<GrCircleEffect>(); |
| 108 | (void)that; |
| 109 | if (edgeType != that.edgeType) return false; |
| 110 | if (center != that.center) return false; |
| 111 | if (radius != that.radius) return false; |
| 112 | return true; |
| 113 | } |
| 114 | GrCircleEffect::GrCircleEffect(const GrCircleEffect& src) |
| 115 | : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags()) |
| 116 | , edgeType(src.edgeType) |
| 117 | , center(src.center) |
| 118 | , radius(src.radius) { |
| 119 | this->cloneAndRegisterAllChildProcessors(src); |
| 120 | } |
| 121 | std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const { |
| 122 | return std::make_unique<GrCircleEffect>(*this); |
| 123 | } |
| 124 | #if GR_TEST_UTILS |
| 125 | SkString GrCircleEffect::onDumpInfo() const { |
| 126 | return SkStringPrintf("(edgeType=%d, center=float2(%f, %f), radius=%f)" , (int)edgeType, |
| 127 | center.fX, center.fY, radius); |
| 128 | } |
| 129 | #endif |
| 130 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect); |
| 131 | #if GR_TEST_UTILS |
| 132 | std::unique_ptr<GrFragmentProcessor> GrCircleEffect::TestCreate(GrProcessorTestData* testData) { |
| 133 | SkPoint center; |
| 134 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 135 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 136 | SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f); |
| 137 | bool success; |
| 138 | std::unique_ptr<GrFragmentProcessor> fp = testData->inputFP(); |
| 139 | do { |
| 140 | GrClipEdgeType et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 141 | std::tie(success, fp) = GrCircleEffect::Make(std::move(fp), et, center, radius); |
| 142 | } while (!success); |
| 143 | return fp; |
| 144 | } |
| 145 | #endif |
| 146 | |