blob: 1034edeea7d8febfe91c4376dee68282eca7968d [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
John Stiles4ca88842020-06-08 17:20:01 -040013std::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 Reed242135a2018-02-22 13:41:39 -050020 SkASSERT(!SkRRectPriv::IsCircle(devRRect) &&
21 !devRRect.isRect()); // Should've been caught up-stream
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050022
23 // TODO: loosen this up
Mike Reed242135a2018-02-22 13:41:39 -050024 if (!SkRRectPriv::IsSimpleCircular(devRRect)) {
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050025 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 Kleind6ab77a2019-03-21 08:18:24 -050031 SkRRect rrectToDraw;
Brian Salomon9f2b86c2019-10-22 10:37:46 -040032 SkISize dimensions;
Mike Reed28d47732018-03-05 16:56:52 -050033 SkScalar ignored[kSkBlurRRectMaxDivisions];
Mike Kleind6ab77a2019-03-21 08:18:24 -050034 int ignoredSize;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050035 uint32_t ignored32;
36
Mike Kleind6ab77a2019-03-21 08:18:24 -050037 bool ninePatchable = SkComputeBlurredRRectParams(
Brian Salomon9f2b86c2019-10-22 10:37:46 -040038 srcRRect, devRRect, SkRect::MakeEmpty(), sigma, xformedSigma, &rrectToDraw, &dimensions,
Mike Kleind6ab77a2019-03-21 08:18:24 -050039 ignored, ignored, ignored, ignored, &ignoredSize, &ignoredSize, &ignored32);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050040 if (!ninePatchable) {
41 return nullptr;
42 }
43
Greg Daniel43956122020-02-11 15:49:27 -050044 GrSurfaceProxyView mask =
45 find_or_create_rrect_blur_mask(context, rrectToDraw, dimensions, xformedSigma);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050046 if (!mask) {
47 return nullptr;
48 }
49
Mike Reed242135a2018-02-22 13:41:39 -050050 return std::unique_ptr<GrFragmentProcessor>(
John Stiles4ca88842020-06-08 17:20:01 -040051 new GrRRectBlurEffect(std::move(inputFP), xformedSigma, devRRect.getBounds(),
Mike Kleind6ab77a2019-03-21 08:18:24 -050052 SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(mask)));
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050053}
Greg Daniel456f9b52020-03-05 19:14:18 +000054#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055#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 Nicholas297d6ef2017-12-20 12:00:11 -050060class GrGLSLRRectBlurEffect : public GrGLSLFragmentProcessor {
61public:
62 GrGLSLRRectBlurEffect() {}
63 void emitCode(EmitArgs& args) override {
64 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050065 const GrRRectBlurEffect& _outer = args.fFp.cast<GrRRectBlurEffect>();
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050066 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040067 auto sigma = _outer.sigma;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050068 (void)sigma;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040069 auto rect = _outer.rect;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050070 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040071 auto cornerRadius = _outer.cornerRadius;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -050072 (void)cornerRadius;
Ethan Nicholas16464c32020-04-06 13:53:05 -040073 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 Nicholas297d6ef2017-12-20 12:00:11 -050079 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050080 "\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 Nicholasbcd51e82019-04-09 10:40:41 -040087 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 Nicholas297d6ef2017-12-20 12:00:11 -050092 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050093 "\n} else if (translatedFragPos.y >= middle.y + threshold) {\n "
94 "translatedFragPos.y -= middle.y - 1.0;\n}\nhalf2 proxyDims = half2(2.0 * "
John Stiles4ca88842020-06-08 17:20:01 -040095 "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 Whited523a062019-06-19 13:12:46 -0400106 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Greg Daniel369ee6b2019-12-02 15:30:02 -0500107 fragBuilder->getProgramBuilder()
108 ->samplerSwizzle(args.fTexSamplers[0])
109 .asString()
110 .c_str());
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500111 }
112
113private:
114 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500115 const GrFragmentProcessor& _proc) override {
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500116 const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>();
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400117 { pdman.set1f(cornerRadiusVar, (_outer.cornerRadius)); }
118 auto sigma = _outer.sigma;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500119 (void)sigma;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400120 auto rect = _outer.rect;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500121 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400122 UniformHandle& cornerRadius = cornerRadiusVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500123 (void)cornerRadius;
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000124 const GrSurfaceProxyView& ninePatchSamplerView = _outer.textureSampler(0).view();
125 GrTexture& ninePatchSampler = *ninePatchSamplerView.proxy()->peekTexture();
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500126 (void)ninePatchSampler;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400127 UniformHandle& proxyRect = proxyRectVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500128 (void)proxyRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400129 UniformHandle& blurRadius = blurRadiusVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500130 (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 Nicholasbcd51e82019-04-09 10:40:41 -0400139 UniformHandle proxyRectVar;
140 UniformHandle blurRadiusVar;
141 UniformHandle cornerRadiusVar;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500142};
143GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const {
144 return new GrGLSLRRectBlurEffect();
145}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500146void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500147 GrProcessorKeyBuilder* b) const {}
148bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
149 const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>();
150 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400151 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 Nicholas297d6ef2017-12-20 12:00:11 -0500155 return true;
156}
157GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src)
158 : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400159 , sigma(src.sigma)
160 , rect(src.rect)
161 , cornerRadius(src.cornerRadius)
162 , ninePatchSampler(src.ninePatchSampler) {
John Stiles88183902020-06-10 16:40:38 -0400163 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 Stiles4ca88842020-06-08 17:20:01 -0400167 }
John Stiles88183902020-06-10 16:40:38 -0400168 inputFP_index = this->registerChildProcessor(std::move(inputFP_clone));
John Stiles4ca88842020-06-08 17:20:01 -0400169 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400170 this->setTextureSamplerCnt(1);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500171}
172std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const {
173 return std::unique_ptr<GrFragmentProcessor>(new GrRRectBlurEffect(*this));
174}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400175const GrFragmentProcessor::TextureSampler& GrRRectBlurEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400176 return IthTextureSampler(index, ninePatchSampler);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400177}
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500178GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect);
179#if GR_TEST_UTILS
180std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500181 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 Nicholas297d6ef2017-12-20 12:00:11 -0500184 SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500185 SkRRect rrect;
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500186 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
John Stiles4ca88842020-06-08 17:20:01 -0400187 return GrRRectBlurEffect::Make(/*inputFP=*/nullptr, d->context(), sigma, sigma, rrect, rrect);
Ethan Nicholas297d6ef2017-12-20 12:00:11 -0500188}
189#endif