Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrRRectBlurEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 11 | #include "GrRRectBlurEffect.h" |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 12 | |
| 13 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::Make(GrContext* context, float sigma, |
| 14 | float xformedSigma, |
| 15 | const SkRRect& srcRRect, |
| 16 | const SkRRect& devRRect) { |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 17 | SkASSERT(!SkRRectPriv::IsCircle(devRRect) && |
| 18 | !devRRect.isRect()); // Should've been caught up-stream |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 19 | |
| 20 | // TODO: loosen this up |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 21 | if (!SkRRectPriv::IsSimpleCircular(devRRect)) { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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; |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 30 | SkScalar ignored[kSkBlurRRectMaxDivisions]; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 31 | int ignoredSize; |
| 32 | uint32_t ignored32; |
| 33 | |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 34 | bool ninePatchable = SkComputeBlurredRRectParams( |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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 | |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 47 | return std::unique_ptr<GrFragmentProcessor>( |
| 48 | new GrRRectBlurEffect(xformedSigma, devRRect.getBounds(), |
| 49 | SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(mask))); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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; |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 70 | fCornerRadiusVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 77 | "\nhalf2 translatedFragPos = half2(sk_FragCoord.xy - %s.xy);\nhalf threshold = %s " |
| 78 | "+ 2.0 * %s;\nhalf2 middle = half2((%s.zw - %s.xy) - float(2.0 * threshold));\nif " |
| 79 | "(translatedFragPos.x >= threshold && translatedFragPos.x < middle.x + threshold) " |
| 80 | "{\n translatedFragPos.x = threshold;\n} else if (translatedFragPos.x >= " |
| 81 | "middle.x + threshold) {\n translatedFragPos.x -= middle.x - 1.0;\n}\nif " |
| 82 | "(translatedFragPos.y > threshold && translatedFragPos.y < middle.y + threshold) " |
| 83 | "{\n translatedFragPos.y = threshold;", |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 90 | "\n} else if (translatedFragPos.y >= middle.y + threshold) {\n " |
| 91 | "translatedFragPos.y -= middle.y - 1.0;\n}\nhalf2 proxyDims = half2(2.0 * " |
| 92 | "threshold + 1.0);\nhalf2 texCoord = translatedFragPos / proxyDims;\n%s = %s * " |
| 93 | "texture(%s, float2(texCoord)).%s;\n", |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 94 | args.fOutputColor, args.fInputColor, |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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>(); |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 103 | { pdman.set1f(fCornerRadiusVar, (_outer.cornerRadius())); } |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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(); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 111 | GrTexture& ninePatchSampler = *ninePatchSamplerProxy.peekTexture(); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 149 | this->setTextureSamplerCnt(1); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 150 | } |
| 151 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const { |
| 152 | return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this)); |
| 153 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 154 | const GrFragmentProcessor::TextureSampler& GrRRectBlurEffect::onTextureSampler(int index) const { |
| 155 | return IthTextureSampler(index, fNinePatchSampler); |
| 156 | } |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 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 |