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