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 | |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 44 | GrSurfaceProxyView mask = |
| 45 | find_or_create_rrect_blur_mask(context, rrectToDraw, dimensions, xformedSigma); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 46 | if (!mask) { |
| 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(), |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 52 | SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(mask))); |
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;"); |
| 94 | SkString _input8931 = SkStringPrintf("%s", args.fInputColor); |
| 95 | SkString _sample8931; |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 96 | if (_outer.inputFP_index >= 0) { |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 97 | _sample8931 = this->invokeChild(_outer.inputFP_index, _input8931.c_str(), args); |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 98 | } else { |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 99 | _sample8931 = _input8931; |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 100 | } |
| 101 | fragBuilder->codeAppendf( |
| 102 | "\nhalf4 inputColor = %s;\n%s = inputColor * sample(%s, float2(texCoord)).%s;\n", |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 103 | _sample8931.c_str(), args.fOutputColor, |
Stephen White | d523a06 | 2019-06-19 13:12:46 -0400 | [diff] [blame] | 104 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]), |
Greg Daniel | 369ee6b | 2019-12-02 15:30:02 -0500 | [diff] [blame] | 105 | fragBuilder->getProgramBuilder() |
| 106 | ->samplerSwizzle(args.fTexSamplers[0]) |
| 107 | .asString() |
| 108 | .c_str()); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private: |
| 112 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 113 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 114 | const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>(); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 115 | { pdman.set1f(cornerRadiusVar, (_outer.cornerRadius)); } |
| 116 | auto sigma = _outer.sigma; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 117 | (void)sigma; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 118 | auto rect = _outer.rect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 119 | (void)rect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 120 | UniformHandle& cornerRadius = cornerRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 121 | (void)cornerRadius; |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 122 | const GrSurfaceProxyView& ninePatchSamplerView = _outer.textureSampler(0).view(); |
| 123 | GrTexture& ninePatchSampler = *ninePatchSamplerView.proxy()->peekTexture(); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 124 | (void)ninePatchSampler; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 125 | UniformHandle& proxyRect = proxyRectVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 126 | (void)proxyRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 127 | UniformHandle& blurRadius = blurRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 128 | (void)blurRadius; |
| 129 | |
| 130 | float blurRadiusValue = 3.f * SkScalarCeilToScalar(sigma - 1 / 6.0f); |
| 131 | pdman.set1f(blurRadius, blurRadiusValue); |
| 132 | |
| 133 | SkRect outset = rect; |
| 134 | outset.outset(blurRadiusValue, blurRadiusValue); |
| 135 | pdman.set4f(proxyRect, outset.fLeft, outset.fTop, outset.fRight, outset.fBottom); |
| 136 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 137 | UniformHandle proxyRectVar; |
| 138 | UniformHandle blurRadiusVar; |
| 139 | UniformHandle cornerRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 140 | }; |
| 141 | GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const { |
| 142 | return new GrGLSLRRectBlurEffect(); |
| 143 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 144 | void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 145 | GrProcessorKeyBuilder* b) const {} |
| 146 | bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 147 | const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>(); |
| 148 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 149 | if (sigma != that.sigma) return false; |
| 150 | if (rect != that.rect) return false; |
| 151 | if (cornerRadius != that.cornerRadius) return false; |
| 152 | if (ninePatchSampler != that.ninePatchSampler) return false; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src) |
| 156 | : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 157 | , sigma(src.sigma) |
| 158 | , rect(src.rect) |
| 159 | , cornerRadius(src.cornerRadius) |
| 160 | , ninePatchSampler(src.ninePatchSampler) { |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 161 | if (src.inputFP_index >= 0) { |
John Stiles | 3779f44 | 2020-06-15 10:48:49 -0400 | [diff] [blame] | 162 | inputFP_index = this->cloneAndRegisterChildProcessor(src.childProcessor(src.inputFP_index)); |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 163 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 164 | this->setTextureSamplerCnt(1); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 165 | } |
| 166 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const { |
| 167 | return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this)); |
| 168 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 169 | const GrFragmentProcessor::TextureSampler& GrRRectBlurEffect::onTextureSampler(int index) const { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 170 | return IthTextureSampler(index, ninePatchSampler); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 171 | } |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 172 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect); |
| 173 | #if GR_TEST_UTILS |
| 174 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 175 | SkScalar w = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 176 | SkScalar h = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 177 | SkScalar r = d->fRandom->nextRangeF(1.f, 9.f); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 178 | SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 179 | SkRRect rrect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 180 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 181 | return GrRRectBlurEffect::Make(/*inputFP=*/nullptr, d->context(), sigma, sigma, rrect, rrect); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 182 | } |
| 183 | #endif |