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