1/*
2 * Copyright 2018 Google Inc.
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
8/**************************************************************************************************
9 *** This file was autogenerated from GrRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
11#ifndef GrRectBlurEffect_DEFINED
12#define GrRectBlurEffect_DEFINED
13
14#include "include/core/SkM44.h"
15#include "include/core/SkTypes.h"
16
17#include <cmath>
18#include "include/core/SkRect.h"
19#include "include/core/SkScalar.h"
20#include "include/gpu/GrRecordingContext.h"
21#include "src/core/SkBlurMask.h"
22#include "src/core/SkMathPriv.h"
23#include "src/gpu/GrBitmapTextureMaker.h"
24#include "src/gpu/GrProxyProvider.h"
25#include "src/gpu/GrRecordingContextPriv.h"
26#include "src/gpu/GrShaderCaps.h"
27#include "src/gpu/effects/GrTextureEffect.h"
28
29#include "src/gpu/GrFragmentProcessor.h"
30
31class GrRectBlurEffect : public GrFragmentProcessor {
32public:
33 static std::unique_ptr<GrFragmentProcessor> MakeIntegralFP(GrRecordingContext* context,
34 float sixSigma) {
35 // The texture we're producing represents the integral of a normal distribution over a
36 // six-sigma range centered at zero. We want enough resolution so that the linear
37 // interpolation done in texture lookup doesn't introduce noticeable artifacts. We
38 // conservatively choose to have 2 texels for each dst pixel.
39 int minWidth = 2 * sk_float_ceil2int(sixSigma);
40 // Bin by powers of 2 with a minimum so we get good profile reuse.
41 int width = std::max(SkNextPow2(minWidth), 32);
42
43 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
44 GrUniqueKey key;
45 GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask");
46 builder[0] = width;
47 builder.finish();
48
49 SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f);
50
51 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
52 if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
53 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
54 GrColorType::kAlpha_8);
55 GrSurfaceProxyView view{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
56 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m,
57 GrSamplerState::Filter::kLinear);
58 }
59
60 SkBitmap bitmap;
61 if (!bitmap.tryAllocPixels(SkImageInfo::MakeA8(width, 1))) {
62 return {};
63 }
64 *bitmap.getAddr8(0, 0) = 255;
65 const float invWidth = 1.f / width;
66 for (int i = 1; i < width - 1; ++i) {
67 float x = (i + 0.5f) * invWidth;
68 x = (-6 * x + 3) * SK_ScalarRoot2Over2;
69 float integral = 0.5f * (std::erf(x) + 1.f);
70 *bitmap.getAddr8(i, 0) = SkToU8(sk_float_round2int(255.f * integral));
71 }
72 *bitmap.getAddr8(width - 1, 0) = 0;
73 bitmap.setImmutable();
74
75 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
76 auto view = maker.view(GrMipmapped::kNo);
77 if (!view) {
78 return {};
79 }
80 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
81 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
82 return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m,
83 GrSamplerState::Filter::kLinear);
84 }
85
86 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP,
87 GrRecordingContext* context,
88 const GrShaderCaps& caps,
89 const SkRect& rect,
90 float sigma) {
91 SkASSERT(rect.isSorted());
92 if (!caps.floatIs32Bits()) {
93 // We promote the math that gets us into the Gaussian space to full float when the rect
94 // coords are large. If we don't have full float then fail. We could probably clip the
95 // rect to an outset device bounds instead.
96 if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f ||
97 SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) {
98 return nullptr;
99 }
100 }
101
102 const float sixSigma = 6 * sigma;
103 std::unique_ptr<GrFragmentProcessor> integral = MakeIntegralFP(context, sixSigma);
104 if (!integral) {
105 return nullptr;
106 }
107
108 // In the fast variant we think of the midpoint of the integral texture as aligning
109 // with the closest rect edge both in x and y. To simplify texture coord calculation we
110 // inset the rect so that the edge of the inset rect corresponds to t = 0 in the texture.
111 // It actually simplifies things a bit in the !isFast case, too.
112 float threeSigma = sixSigma / 2;
113 SkRect insetRect = {rect.fLeft + threeSigma, rect.fTop + threeSigma,
114 rect.fRight - threeSigma, rect.fBottom - threeSigma};
115
116 // In our fast variant we find the nearest horizontal and vertical edges and for each
117 // do a lookup in the integral texture for each and multiply them. When the rect is
118 // less than 6 sigma wide then things aren't so simple and we have to consider both the
119 // left and right edge of the rectangle (and similar in y).
120 bool isFast = insetRect.isSorted();
121 return std::unique_ptr<GrFragmentProcessor>(
122 new GrRectBlurEffect(std::move(inputFP), insetRect, std::move(integral), isFast,
123 GrSamplerState::Filter::kLinear));
124 }
125 GrRectBlurEffect(const GrRectBlurEffect& src);
126 std::unique_ptr<GrFragmentProcessor> clone() const override;
127 const char* name() const override { return "RectBlurEffect"; }
128 SkRect rect;
129 bool isFast;
130
131private:
132 GrRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
133 SkRect rect,
134 std::unique_ptr<GrFragmentProcessor> integral,
135 bool isFast,
136 GrSamplerState samplerParams)
137 : INHERITED(kGrRectBlurEffect_ClassID,
138 (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get())
139 : kAll_OptimizationFlags) &
140 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
141 , rect(rect)
142 , isFast(isFast) {
143 this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough());
144 SkASSERT(integral);
145 this->registerChild(std::move(integral), SkSL::SampleUsage::Explicit());
146 }
147 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
148 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
149 bool onIsEqual(const GrFragmentProcessor&) const override;
150#if GR_TEST_UTILS
151 SkString onDumpInfo() const override;
152#endif
153 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
154 typedef GrFragmentProcessor INHERITED;
155};
156#endif
157