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 | d6ab77a | 2019-03-21 08:18:24 -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 | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 29 | SkRRect rrectToDraw; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 30 | SkISize dimensions; |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 31 | SkScalar ignored[kSkBlurRRectMaxDivisions]; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -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 | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 35 | bool ninePatchable = SkComputeBlurredRRectParams( |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 36 | srcRRect, devRRect, SkRect::MakeEmpty(), sigma, xformedSigma, &rrectToDraw, &dimensions, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 37 | ignored, ignored, ignored, ignored, &ignoredSize, &ignoredSize, &ignored32); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 38 | if (!ninePatchable) { |
| 39 | return nullptr; |
| 40 | } |
| 41 | |
| 42 | sk_sp<GrTextureProxy> mask( |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 43 | find_or_create_rrect_blur_mask(context, rrectToDraw, dimensions, xformedSigma)); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 44 | if (!mask) { |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 48 | return std::unique_ptr<GrFragmentProcessor>( |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 49 | new GrRRectBlurEffect(xformedSigma, devRRect.getBounds(), |
| 50 | SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(mask))); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 51 | } |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 52 | #include "include/gpu/GrTexture.h" |
| 53 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 54 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 55 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 56 | #include "src/sksl/SkSLCPP.h" |
| 57 | #include "src/sksl/SkSLUtil.h" |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 58 | class GrGLSLRRectBlurEffect : public GrGLSLFragmentProcessor { |
| 59 | public: |
| 60 | GrGLSLRRectBlurEffect() {} |
| 61 | void emitCode(EmitArgs& args) override { |
| 62 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 63 | const GrRRectBlurEffect& _outer = args.fFp.cast<GrRRectBlurEffect>(); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 64 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 65 | auto sigma = _outer.sigma; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 66 | (void)sigma; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 67 | auto rect = _outer.rect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 68 | (void)rect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 69 | auto cornerRadius = _outer.cornerRadius; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 70 | (void)cornerRadius; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 71 | cornerRadiusVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, |
| 72 | "cornerRadius"); |
| 73 | proxyRectVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, |
| 74 | "proxyRect"); |
| 75 | blurRadiusVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, |
| 76 | "blurRadius"); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 77 | fragBuilder->codeAppendf( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 78 | "\nhalf2 translatedFragPos = half2(sk_FragCoord.xy - %s.xy);\nhalf threshold = %s " |
| 79 | "+ 2.0 * %s;\nhalf2 middle = half2((%s.zw - %s.xy) - float(2.0 * threshold));\nif " |
| 80 | "(translatedFragPos.x >= threshold && translatedFragPos.x < middle.x + threshold) " |
| 81 | "{\n translatedFragPos.x = threshold;\n} else if (translatedFragPos.x >= " |
| 82 | "middle.x + threshold) {\n translatedFragPos.x -= middle.x - 1.0;\n}\nif " |
| 83 | "(translatedFragPos.y > threshold && translatedFragPos.y < middle.y + threshold) " |
| 84 | "{\n translatedFragPos.y = threshold;", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 85 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
| 86 | args.fUniformHandler->getUniformCStr(cornerRadiusVar), |
| 87 | args.fUniformHandler->getUniformCStr(blurRadiusVar), |
| 88 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
| 89 | args.fUniformHandler->getUniformCStr(proxyRectVar)); |
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 | "\n} else if (translatedFragPos.y >= middle.y + threshold) {\n " |
| 92 | "translatedFragPos.y -= middle.y - 1.0;\n}\nhalf2 proxyDims = half2(2.0 * " |
| 93 | "threshold + 1.0);\nhalf2 texCoord = translatedFragPos / proxyDims;\n%s = %s * " |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 94 | "sample(%s, float2(texCoord)).%s;\n", |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 95 | args.fOutputColor, args.fInputColor, |
Stephen White | d523a06 | 2019-06-19 13:12:46 -0400 | [diff] [blame] | 96 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]), |
Greg Daniel | 369ee6b | 2019-12-02 15:30:02 -0500 | [diff] [blame] | 97 | fragBuilder->getProgramBuilder() |
| 98 | ->samplerSwizzle(args.fTexSamplers[0]) |
| 99 | .asString() |
| 100 | .c_str()); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 105 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 106 | const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>(); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 107 | { pdman.set1f(cornerRadiusVar, (_outer.cornerRadius)); } |
| 108 | auto sigma = _outer.sigma; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 109 | (void)sigma; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 110 | auto rect = _outer.rect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 111 | (void)rect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 112 | UniformHandle& cornerRadius = cornerRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 113 | (void)cornerRadius; |
Robert Phillips | c5c0247 | 2019-12-12 13:24:36 +0000 | [diff] [blame^] | 114 | GrSurfaceProxy& ninePatchSamplerProxy = *_outer.textureSampler(0).proxy(); |
| 115 | GrTexture& ninePatchSampler = *ninePatchSamplerProxy.peekTexture(); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 116 | (void)ninePatchSampler; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 117 | UniformHandle& proxyRect = proxyRectVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 118 | (void)proxyRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 119 | UniformHandle& blurRadius = blurRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 120 | (void)blurRadius; |
| 121 | |
| 122 | float blurRadiusValue = 3.f * SkScalarCeilToScalar(sigma - 1 / 6.0f); |
| 123 | pdman.set1f(blurRadius, blurRadiusValue); |
| 124 | |
| 125 | SkRect outset = rect; |
| 126 | outset.outset(blurRadiusValue, blurRadiusValue); |
| 127 | pdman.set4f(proxyRect, outset.fLeft, outset.fTop, outset.fRight, outset.fBottom); |
| 128 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 129 | UniformHandle proxyRectVar; |
| 130 | UniformHandle blurRadiusVar; |
| 131 | UniformHandle cornerRadiusVar; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 132 | }; |
| 133 | GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const { |
| 134 | return new GrGLSLRRectBlurEffect(); |
| 135 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 136 | void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 137 | GrProcessorKeyBuilder* b) const {} |
| 138 | bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 139 | const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>(); |
| 140 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 141 | if (sigma != that.sigma) return false; |
| 142 | if (rect != that.rect) return false; |
| 143 | if (cornerRadius != that.cornerRadius) return false; |
| 144 | if (ninePatchSampler != that.ninePatchSampler) return false; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src) |
| 148 | : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 149 | , sigma(src.sigma) |
| 150 | , rect(src.rect) |
| 151 | , cornerRadius(src.cornerRadius) |
| 152 | , ninePatchSampler(src.ninePatchSampler) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 153 | this->setTextureSamplerCnt(1); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 154 | } |
| 155 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const { |
| 156 | return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this)); |
| 157 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 158 | const GrFragmentProcessor::TextureSampler& GrRRectBlurEffect::onTextureSampler(int index) const { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 159 | return IthTextureSampler(index, ninePatchSampler); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 160 | } |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 161 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect); |
| 162 | #if GR_TEST_UTILS |
| 163 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 164 | SkScalar w = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 165 | SkScalar h = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 166 | SkScalar r = d->fRandom->nextRangeF(1.f, 9.f); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 167 | SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 168 | SkRRect rrect; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 169 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
| 170 | return GrRRectBlurEffect::Make(d->context(), sigma, sigma, rrect, rrect); |
| 171 | } |
| 172 | #endif |