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 | |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 13 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::Make( |
| 14 | std::unique_ptr<GrFragmentProcessor> inputFP, |
| 15 | GrRecordingContext* context, |
| 16 | float sigma, |
| 17 | float xformedSigma, |
| 18 | const SkRRect& srcRRect, |
| 19 | const SkRRect& devRRect) { |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 20 | SkASSERT(!SkRRectPriv::IsCircle(devRRect) && |
| 21 | !devRRect.isRect()); // Should've been caught up-stream |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 22 | |
| 23 | // TODO: loosen this up |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 24 | if (!SkRRectPriv::IsSimpleCircular(devRRect)) { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 25 | return nullptr; |
| 26 | } |
| 27 | |
| 28 | // Make sure we can successfully ninepatch this rrect -- the blur sigma has to be |
| 29 | // sufficiently small relative to both the size of the corner radius and the |
| 30 | // width (and height) of the rrect. |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 31 | SkRRect rrectToDraw; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 32 | SkISize dimensions; |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 33 | SkScalar ignored[kSkBlurRRectMaxDivisions]; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 34 | int ignoredSize; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 35 | uint32_t ignored32; |
| 36 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 37 | bool ninePatchable = SkComputeBlurredRRectParams( |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 38 | srcRRect, devRRect, SkRect::MakeEmpty(), sigma, xformedSigma, &rrectToDraw, &dimensions, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 39 | ignored, ignored, ignored, ignored, &ignoredSize, &ignoredSize, &ignored32); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 40 | if (!ninePatchable) { |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 44 | std::unique_ptr<GrFragmentProcessor> maskFP = |
| 45 | find_or_create_rrect_blur_mask_fp(context, rrectToDraw, dimensions, xformedSigma); |
| 46 | if (!maskFP) { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 47 | return nullptr; |
| 48 | } |
| 49 | |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 50 | return std::unique_ptr<GrFragmentProcessor>( |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 51 | new GrRRectBlurEffect(std::move(inputFP), xformedSigma, devRRect.getBounds(), |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 52 | SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(maskFP))); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 53 | } |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 54 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 55 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 56 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 57 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 58 | #include "src/sksl/SkSLCPP.h" |
| 59 | #include "src/sksl/SkSLUtil.h" |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 60 | class GrGLSLRRectBlurEffect : public GrGLSLFragmentProcessor { |
| 61 | public: |
| 62 | GrGLSLRRectBlurEffect() {} |
| 63 | void emitCode(EmitArgs& args) override { |
| 64 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 65 | const GrRRectBlurEffect& _outer = args.fFp.cast<GrRRectBlurEffect>(); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 66 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 67 | auto sigma = _outer.sigma; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 68 | (void)sigma; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 69 | auto rect = _outer.rect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 70 | (void)rect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 71 | auto cornerRadius = _outer.cornerRadius; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 72 | (void)cornerRadius; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 73 | cornerRadiusVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 74 | kHalf_GrSLType, "cornerRadius"); |
| 75 | proxyRectVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 76 | kFloat4_GrSLType, "proxyRect"); |
| 77 | blurRadiusVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 78 | kHalf_GrSLType, "blurRadius"); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 79 | fragBuilder->codeAppendf( |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 80 | "half2 translatedFragPos = half2(sk_FragCoord.xy - %s.xy);\nhalf2 proxyCenter = " |
| 81 | "half2((%s.zw - %s.xy) * 0.5);\nhalf edgeSize = (2.0 * %s + %s) + " |
| 82 | "0.5;\ntranslatedFragPos -= proxyCenter;\nhalf2 fragDirection = " |
| 83 | "sign(translatedFragPos);\ntranslatedFragPos = " |
| 84 | "abs(translatedFragPos);\ntranslatedFragPos -= proxyCenter - " |
| 85 | "edgeSize;\ntranslatedFragPos = max(translatedFragPos, 0.0);\ntranslatedFragPos *= " |
| 86 | "fragDirection;\ntranslatedFragPos += half2(edgeSize);\nhalf2 proxyDims = " |
| 87 | "half2(2.0 * edgeSize);\nhalf2 texCoord = tra", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 88 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 89 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
| 90 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 91 | args.fUniformHandler->getUniformCStr(blurRadiusVar), |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 92 | args.fUniformHandler->getUniformCStr(cornerRadiusVar)); |
| 93 | fragBuilder->codeAppendf("nslatedFragPos / proxyDims;"); |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 94 | SkString _input9604 = SkStringPrintf("%s", args.fInputColor); |
| 95 | SkString _sample9604; |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 96 | if (_outer.inputFP_index >= 0) { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 97 | _sample9604 = this->invokeChild(_outer.inputFP_index, _input9604.c_str(), args); |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 98 | } else { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 99 | _sample9604 = _input9604; |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 100 | } |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 101 | fragBuilder->codeAppendf("\nhalf4 inputColor = %s;", _sample9604.c_str()); |
| 102 | SkString _sample9664; |
| 103 | SkString _coords9664("float2(texCoord)"); |
| 104 | _sample9664 = this->invokeChild(_outer.ninePatchFP_index, args, _coords9664.c_str()); |
| 105 | fragBuilder->codeAppendf("\n%s = inputColor * %s;\n", args.fOutputColor, |
| 106 | _sample9664.c_str()); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private: |
| 110 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 111 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 112 | const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>(); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 113 | { pdman.set1f(cornerRadiusVar, (_outer.cornerRadius)); } |
| 114 | auto sigma = _outer.sigma; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 115 | (void)sigma; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 116 | auto rect = _outer.rect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 117 | (void)rect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 118 | UniformHandle& cornerRadius = cornerRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 119 | (void)cornerRadius; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 120 | UniformHandle& proxyRect = proxyRectVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 121 | (void)proxyRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 122 | UniformHandle& blurRadius = blurRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 123 | (void)blurRadius; |
| 124 | |
| 125 | float blurRadiusValue = 3.f * SkScalarCeilToScalar(sigma - 1 / 6.0f); |
| 126 | pdman.set1f(blurRadius, blurRadiusValue); |
| 127 | |
| 128 | SkRect outset = rect; |
| 129 | outset.outset(blurRadiusValue, blurRadiusValue); |
| 130 | pdman.set4f(proxyRect, outset.fLeft, outset.fTop, outset.fRight, outset.fBottom); |
| 131 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 132 | UniformHandle proxyRectVar; |
| 133 | UniformHandle blurRadiusVar; |
| 134 | UniformHandle cornerRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 135 | }; |
| 136 | GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const { |
| 137 | return new GrGLSLRRectBlurEffect(); |
| 138 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 139 | void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 140 | GrProcessorKeyBuilder* b) const {} |
| 141 | bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 142 | const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>(); |
| 143 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 144 | if (sigma != that.sigma) return false; |
| 145 | if (rect != that.rect) return false; |
| 146 | if (cornerRadius != that.cornerRadius) return false; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src) |
| 150 | : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 151 | , sigma(src.sigma) |
| 152 | , rect(src.rect) |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 153 | , cornerRadius(src.cornerRadius) { |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 154 | if (src.inputFP_index >= 0) { |
John Stiles | 3779f44 | 2020-06-15 10:48:49 -0400 | [diff] [blame] | 155 | inputFP_index = this->cloneAndRegisterChildProcessor(src.childProcessor(src.inputFP_index)); |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 156 | } |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 157 | { |
| 158 | ninePatchFP_index = |
| 159 | this->cloneAndRegisterChildProcessor(src.childProcessor(src.ninePatchFP_index)); |
| 160 | } |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 161 | } |
| 162 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const { |
| 163 | return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this)); |
| 164 | } |
| 165 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect); |
| 166 | #if GR_TEST_UTILS |
| 167 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 168 | SkScalar w = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 169 | SkScalar h = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 170 | SkScalar r = d->fRandom->nextRangeF(1.f, 9.f); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 171 | SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 172 | SkRRect rrect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 173 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 174 | return GrRRectBlurEffect::Make(/*inputFP=*/nullptr, d->context(), sigma, sigma, rrect, rrect); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 175 | } |
| 176 | #endif |