blob: 35cf1fec1e52591f7daf5b69572f5a14fe0e07ee [file] [log] [blame]
Ethan Nicholas297d6ef2017-12-20 12:00:11 -05001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas297d6ef2017-12-20 12:00:11 -05003 *
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 Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrRRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050011#include "GrRRectBlurEffect.h"
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050012
Robert Phillips7af8fe52019-02-14 17:27:00 -050013std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::Make(GrRecordingContext* context,
Mike Kleind6ab77a2019-03-21 08:18:24 -050014 float sigma,
15 float xformedSigma,
16 const SkRRect& srcRRect,
17 const SkRRect& devRRect) {
Mike Reed242135a2018-02-22 13:41:39 -050018 SkASSERT(!SkRRectPriv::IsCircle(devRRect) &&
19 !devRRect.isRect()); // Should've been caught up-stream
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050020
21 // TODO: loosen this up
Mike Reed242135a2018-02-22 13:41:39 -050022 if (!SkRRectPriv::IsSimpleCircular(devRRect)) {
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050023 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 Kleind6ab77a2019-03-21 08:18:24 -050029 SkRRect rrectToDraw;
Brian Salomon9f2b86c2019-10-22 10:37:46 -040030 SkISize dimensions;
Mike Reed28d47732018-03-05 16:56:52 -050031 SkScalar ignored[kSkBlurRRectMaxDivisions];
Mike Kleind6ab77a2019-03-21 08:18:24 -050032 int ignoredSize;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050033 uint32_t ignored32;
34
Mike Kleind6ab77a2019-03-21 08:18:24 -050035 bool ninePatchable = SkComputeBlurredRRectParams(
Brian Salomon9f2b86c2019-10-22 10:37:46 -040036 srcRRect, devRRect, SkRect::MakeEmpty(), sigma, xformedSigma, &rrectToDraw, &dimensions,
Mike Kleind6ab77a2019-03-21 08:18:24 -050037 ignored, ignored, ignored, ignored, &ignoredSize, &ignoredSize, &ignored32);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050038 if (!ninePatchable) {
39 return nullptr;
40 }
41
42 sk_sp<GrTextureProxy> mask(
Brian Salomon9f2b86c2019-10-22 10:37:46 -040043 find_or_create_rrect_blur_mask(context, rrectToDraw, dimensions, xformedSigma));
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050044 if (!mask) {
45 return nullptr;
46 }
47
Mike Reed242135a2018-02-22 13:41:39 -050048 return std::unique_ptr<GrFragmentProcessor>(
Mike Kleind6ab77a2019-03-21 08:18:24 -050049 new GrRRectBlurEffect(xformedSigma, devRRect.getBounds(),
50 SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(mask)));
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050051}
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#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 Nicholas297d6ef2017-12-20 12:00:11 -050058class GrGLSLRRectBlurEffect : public GrGLSLFragmentProcessor {
59public:
60 GrGLSLRRectBlurEffect() {}
61 void emitCode(EmitArgs& args) override {
62 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050063 const GrRRectBlurEffect& _outer = args.fFp.cast<GrRRectBlurEffect>();
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050064 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040065 auto sigma = _outer.sigma;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050066 (void)sigma;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040067 auto rect = _outer.rect;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050068 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040069 auto cornerRadius = _outer.cornerRadius;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050070 (void)cornerRadius;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040071 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 Nicholas297d6ef2017-12-20 12:00:11 -050077 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050078 "\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 Nicholasbcd51e82019-04-09 10:40:41 -040085 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 Nicholas297d6ef2017-12-20 12:00:11 -050090 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050091 "\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 Nicholas13863662019-07-29 13:05:15 -040094 "sample(%s, float2(texCoord)).%s;\n",
Mike Kleind6ab77a2019-03-21 08:18:24 -050095 args.fOutputColor, args.fInputColor,
Stephen Whited523a062019-06-19 13:12:46 -040096 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Greg Daniel369ee6b2019-12-02 15:30:02 -050097 fragBuilder->getProgramBuilder()
98 ->samplerSwizzle(args.fTexSamplers[0])
99 .asString()
100 .c_str());
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500101 }
102
103private:
104 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500105 const GrFragmentProcessor& _proc) override {
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500106 const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>();
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400107 { pdman.set1f(cornerRadiusVar, (_outer.cornerRadius)); }
108 auto sigma = _outer.sigma;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500109 (void)sigma;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400110 auto rect = _outer.rect;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500111 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400112 UniformHandle& cornerRadius = cornerRadiusVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500113 (void)cornerRadius;
Robert Phillipsc5c02472019-12-12 13:24:36 +0000114 GrSurfaceProxy& ninePatchSamplerProxy = *_outer.textureSampler(0).proxy();
115 GrTexture& ninePatchSampler = *ninePatchSamplerProxy.peekTexture();
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500116 (void)ninePatchSampler;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400117 UniformHandle& proxyRect = proxyRectVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500118 (void)proxyRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400119 UniformHandle& blurRadius = blurRadiusVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500120 (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 Nicholasbcd51e82019-04-09 10:40:41 -0400129 UniformHandle proxyRectVar;
130 UniformHandle blurRadiusVar;
131 UniformHandle cornerRadiusVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500132};
133GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const {
134 return new GrGLSLRRectBlurEffect();
135}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500136void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500137 GrProcessorKeyBuilder* b) const {}
138bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
139 const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>();
140 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400141 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 Nicholas297d6ef2017-12-20 12:00:11 -0500145 return true;
146}
147GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src)
148 : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400149 , sigma(src.sigma)
150 , rect(src.rect)
151 , cornerRadius(src.cornerRadius)
152 , ninePatchSampler(src.ninePatchSampler) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400153 this->setTextureSamplerCnt(1);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500154}
155std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const {
156 return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this));
157}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400158const GrFragmentProcessor::TextureSampler& GrRRectBlurEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400159 return IthTextureSampler(index, ninePatchSampler);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400160}
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500161GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect);
162#if GR_TEST_UTILS
163std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500164 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 Nicholas297d6ef2017-12-20 12:00:11 -0500167 SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500168 SkRRect rrect;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500169 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
170 return GrRRectBlurEffect::Make(d->context(), sigma, sigma, rrect, rrect);
171}
172#endif