1 /* 2 * Copyright 2018 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 GrRRectBlurEffect.fp; do not modify. 10 **************************************************************************************************/ 11 #include "GrRRectBlurEffect.h" 12 13 std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::Make(GrContext* context, float sigma, 14 float xformedSigma, 15 const SkRRect& srcRRect, 16 const SkRRect& devRRect) { 17 SkASSERT(!SkRRectPriv::IsCircle(devRRect) && 18 !devRRect.isRect()); // Should've been caught up-stream 19 20 // TODO: loosen this up 21 if (!SkRRectPriv::IsSimpleCircular(devRRect)) { 22 return nullptr; 23 } 24 25 // Make sure we can successfully ninepatch this rrect -- the blur sigma has to be 26 // sufficiently small relative to both the size of the corner radius and the 27 // width (and height) of the rrect. 28 SkRRect rrectToDraw; 29 SkISize size; 30 SkScalar ignored[kSkBlurRRectMaxDivisions]; 31 int ignoredSize; 32 uint32_t ignored32; 33 34 bool ninePatchable = SkComputeBlurredRRectParams( 35 srcRRect, devRRect, SkRect::MakeEmpty(), sigma, xformedSigma, &rrectToDraw, &size, 36 ignored, ignored, ignored, ignored, &ignoredSize, &ignoredSize, &ignored32); 37 if (!ninePatchable) { 38 return nullptr; 39 } 40 41 sk_sp<GrTextureProxy> mask( 42 find_or_create_rrect_blur_mask(context, rrectToDraw, size, xformedSigma)); 43 if (!mask) { 44 return nullptr; 45 } 46 47 return std::unique_ptr<GrFragmentProcessor>( 48 new GrRRectBlurEffect(xformedSigma, devRRect.getBounds(), 49 SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(mask))); 50 } 51 #include "glsl/GrGLSLFragmentProcessor.h" 52 #include "glsl/GrGLSLFragmentShaderBuilder.h" 53 #include "glsl/GrGLSLProgramBuilder.h" 54 #include "GrTexture.h" 55 #include "SkSLCPP.h" 56 #include "SkSLUtil.h" 57 class GrGLSLRRectBlurEffect : public GrGLSLFragmentProcessor { 58 public: 59 GrGLSLRRectBlurEffect() {} 60 void emitCode(EmitArgs& args) override { 61 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 62 const GrRRectBlurEffect& _outer = args.fFp.cast<GrRRectBlurEffect>(); 63 (void)_outer; 64 auto sigma = _outer.sigma(); 65 (void)sigma; 66 auto rect = _outer.rect(); 67 (void)rect; 68 auto cornerRadius = _outer.cornerRadius(); 69 (void)cornerRadius; 70 fCornerRadiusVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat_GrSLType, 71 kDefault_GrSLPrecision, "cornerRadius"); 72 fProxyRectVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, 73 kDefault_GrSLPrecision, "proxyRect"); 74 fBlurRadiusVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, 75 kDefault_GrSLPrecision, "blurRadius"); 76 fragBuilder->codeAppendf( 77 "\nhalf2 translatedFragPos = half2(sk_FragCoord.xy - %s.xy);\nhalf threshold = " 78 "half(%s + 2.0 * float(%s));\nhalf2 middle = half2((%s.zw - %s.xy) - 2.0 * " 79 "float(threshold));\nif (translatedFragPos.x >= threshold && translatedFragPos.x < " 80 "middle.x + threshold) {\n translatedFragPos.x = threshold;\n} else if " 81 "(translatedFragPos.x >= middle.x + threshold) {\n translatedFragPos.x -= " 82 "float(middle.x) - 1.0;\n}\nif (translatedFragPos.y > threshold && " 83 "translatedFragPos.y < middle.y + threshold) {\n translatedFr", 84 args.fUniformHandler->getUniformCStr(fProxyRectVar), 85 args.fUniformHandler->getUniformCStr(fCornerRadiusVar), 86 args.fUniformHandler->getUniformCStr(fBlurRadiusVar), 87 args.fUniformHandler->getUniformCStr(fProxyRectVar), 88 args.fUniformHandler->getUniformCStr(fProxyRectVar)); 89 fragBuilder->codeAppendf( 90 "agPos.y = threshold;\n} else if (translatedFragPos.y >= middle.y + threshold) {\n " 91 " translatedFragPos.y -= float(middle.y) - 1.0;\n}\nhalf2 proxyDims = " 92 "half2(half(2.0 * float(threshold) + 1.0));\nhalf2 texCoord = translatedFragPos / " 93 "proxyDims;\n%s = %s * texture(%s, float2(texCoord)).%s;\n", 94 args.fOutputColor, args.fInputColor, 95 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), 96 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str()); 97 } 98 99 private: 100 void onSetData(const GrGLSLProgramDataManager& pdman, 101 const GrFragmentProcessor& _proc) override { 102 const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>(); 103 { pdman.set1f(fCornerRadiusVar, (_outer.cornerRadius())); } 104 auto sigma = _outer.sigma(); 105 (void)sigma; 106 auto rect = _outer.rect(); 107 (void)rect; 108 UniformHandle& cornerRadius = fCornerRadiusVar; 109 (void)cornerRadius; 110 GrSurfaceProxy& ninePatchSamplerProxy = *_outer.textureSampler(0).proxy(); 111 GrTexture& ninePatchSampler = *ninePatchSamplerProxy.peekTexture(); 112 (void)ninePatchSampler; 113 UniformHandle& proxyRect = fProxyRectVar; 114 (void)proxyRect; 115 UniformHandle& blurRadius = fBlurRadiusVar; 116 (void)blurRadius; 117 118 float blurRadiusValue = 3.f * SkScalarCeilToScalar(sigma - 1 / 6.0f); 119 pdman.set1f(blurRadius, blurRadiusValue); 120 121 SkRect outset = rect; 122 outset.outset(blurRadiusValue, blurRadiusValue); 123 pdman.set4f(proxyRect, outset.fLeft, outset.fTop, outset.fRight, outset.fBottom); 124 } 125 UniformHandle fProxyRectVar; 126 UniformHandle fBlurRadiusVar; 127 UniformHandle fCornerRadiusVar; 128 }; 129 GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const { 130 return new GrGLSLRRectBlurEffect(); 131 } 132 void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 133 GrProcessorKeyBuilder* b) const {} 134 bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const { 135 const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>(); 136 (void)that; 137 if (fSigma != that.fSigma) return false; 138 if (fRect != that.fRect) return false; 139 if (fCornerRadius != that.fCornerRadius) return false; 140 if (fNinePatchSampler != that.fNinePatchSampler) return false; 141 return true; 142 } 143 GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src) 144 : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags()) 145 , fSigma(src.fSigma) 146 , fRect(src.fRect) 147 , fCornerRadius(src.fCornerRadius) 148 , fNinePatchSampler(src.fNinePatchSampler) { 149 this->setTextureSamplerCnt(1); 150 } 151 std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const { 152 return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this)); 153 } 154 const GrFragmentProcessor::TextureSampler& GrRRectBlurEffect::onTextureSampler(int index) const { 155 return IthTextureSampler(index, fNinePatchSampler); 156 } 157 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect); 158 #if GR_TEST_UTILS 159 std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) { 160 SkScalar w = d->fRandom->nextRangeScalar(100.f, 1000.f); 161 SkScalar h = d->fRandom->nextRangeScalar(100.f, 1000.f); 162 SkScalar r = d->fRandom->nextRangeF(1.f, 9.f); 163 SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f); 164 SkRRect rrect; 165 rrect.setRectXY(SkRect::MakeWH(w, h), r, r); 166 return GrRRectBlurEffect::Make(d->context(), sigma, sigma, rrect, rrect); 167 } 168 #endif 169