blob: b0c86bda1c9312085e4e64d91ec94cbfa6db559b [file] [log] [blame]
Ethan Nicholas82399462017-10-16 12:35:44 -04001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas82399462017-10-16 12:35:44 -04003 *
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 GrRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas82399462017-10-16 12:35:44 -040011#ifndef GrRectBlurEffect_DEFINED
12#define GrRectBlurEffect_DEFINED
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkTypes.h"
Ethan Nicholas82399462017-10-16 12:35:44 -040014
Brian Salomonb2d5d402019-09-10 10:11:52 -040015#include <cmath>
16#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkScalar.h"
18#include "src/core/SkBlurMask.h"
Brian Salomone7366842019-09-04 11:20:45 -040019#include "src/core/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrProxyProvider.h"
21#include "src/gpu/GrShaderCaps.h"
22
23#include "src/gpu/GrCoordTransform.h"
24#include "src/gpu/GrFragmentProcessor.h"
Ethan Nicholas82399462017-10-16 12:35:44 -040025class GrRectBlurEffect : public GrFragmentProcessor {
26public:
Brian Salomonb2d5d402019-09-10 10:11:52 -040027 static sk_sp<GrTextureProxy> CreateIntegralTexture(GrProxyProvider* proxyProvider,
28 float sixSigma) {
29 // The texture we're producing represents the integral of a normal distribution over a
30 // six-sigma range centered at zero. We want enough resolution so that the linear
31 // interpolation done in texture lookup doesn't introduce noticeable artifacts. We
32 // conservatively choose to have 2 texels for each dst pixel.
33 int minWidth = 2 * sk_float_ceil2int(sixSigma);
34 // Bin by powers of 2 with a minimum so we get good profile reuse.
35 int width = SkTMax(SkNextPow2(minWidth), 32);
Brian Salomone7366842019-09-04 11:20:45 -040036
37 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
38 GrUniqueKey key;
39 GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask");
Brian Salomonb2d5d402019-09-10 10:11:52 -040040 builder[0] = width;
Brian Salomone7366842019-09-04 11:20:45 -040041 builder.finish();
42
Brian Salomonb2d5d402019-09-10 10:11:52 -040043 sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
Brian Salomone7366842019-09-04 11:20:45 -040044 key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
Brian Salomonb2d5d402019-09-10 10:11:52 -040045 if (!proxy) {
Brian Salomone7366842019-09-04 11:20:45 -040046 SkBitmap bitmap;
Brian Salomonb2d5d402019-09-10 10:11:52 -040047 if (!bitmap.tryAllocPixels(SkImageInfo::MakeA8(width, 1))) {
Brian Salomone7366842019-09-04 11:20:45 -040048 return nullptr;
49 }
Brian Salomonb2d5d402019-09-10 10:11:52 -040050 *bitmap.getAddr8(0, 0) = 255;
51 const float invWidth = 1.f / width;
52 for (int i = 1; i < width - 1; ++i) {
53 float x = (i + 0.5f) * invWidth;
54 x = (-6 * x + 3) * SK_ScalarRoot2Over2;
55 float integral = 0.5f * (std::erf(x) + 1.f);
56 *bitmap.getAddr8(i, 0) = SkToU8(sk_float_round2int(255.f * integral));
57 }
58 *bitmap.getAddr8(width - 1, 0) = 0;
Brian Salomone7366842019-09-04 11:20:45 -040059 bitmap.setImmutable();
Brian Salomonb2d5d402019-09-10 10:11:52 -040060 proxy = proxyProvider->createProxyFromBitmap(bitmap, GrMipMapped::kNo);
61 if (!proxy) {
Brian Salomone7366842019-09-04 11:20:45 -040062 return nullptr;
63 }
Brian Salomonb2d5d402019-09-10 10:11:52 -040064 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
65 proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
Brian Salomone7366842019-09-04 11:20:45 -040066 }
Brian Salomonb2d5d402019-09-10 10:11:52 -040067 return proxy;
Brian Salomone7366842019-09-04 11:20:45 -040068 }
69
Mike Kleind6ab77a2019-03-21 08:18:24 -050070 static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider* proxyProvider,
71 const GrShaderCaps& caps, const SkRect& rect,
72 float sigma) {
Brian Salomonb2d5d402019-09-10 10:11:52 -040073 SkASSERT(rect.isSorted());
Brian Salomonff6a7cd2018-05-10 09:42:27 -040074 if (!caps.floatIs32Bits()) {
Brian Salomon2c596592019-08-13 20:05:04 -040075 // We promote the math that gets us into the Gaussian space to full float when the rect
76 // coords are large. If we don't have full float then fail. We could probably clip the
77 // rect to an outset device bounds instead.
Brian Salomonff6a7cd2018-05-10 09:42:27 -040078 if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f ||
Brian Salomon2c596592019-08-13 20:05:04 -040079 SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) {
Brian Salomonff6a7cd2018-05-10 09:42:27 -040080 return nullptr;
81 }
82 }
Ethan Nicholas82399462017-10-16 12:35:44 -040083
Brian Salomonb2d5d402019-09-10 10:11:52 -040084 const float sixSigma = 6 * sigma;
85 auto integral = CreateIntegralTexture(proxyProvider, sixSigma);
86 if (!integral) {
Ethan Nicholas82399462017-10-16 12:35:44 -040087 return nullptr;
88 }
89
Brian Salomonb2d5d402019-09-10 10:11:52 -040090 // In the fast variant we think of the midpoint of the integral texture as aligning
91 // with the closest rect edge both in x and y. To simplify texture coord calculation we
92 // inset the rect so that the edge of the inset rect corresponds to t = 0 in the texture.
93 // It actually simplifies things a bit in the !isFast case, too.
94 float threeSigma = sixSigma / 2;
95 SkRect insetRect = {rect.fLeft + threeSigma, rect.fTop + threeSigma,
96 rect.fRight - threeSigma, rect.fBottom - threeSigma};
97
98 // In our fast variant we find the nearest horizontal and vertical edges and for each
99 // do a lookup in the integral texture for each and multiply them. When the rect is
100 // less than 6 sigma wide then things aren't so simple and we have to consider both the
101 // left and right edge of the rectangle (and similar in y).
102 bool isFast = insetRect.isSorted();
103 // 1 / (6 * sigma) is the domain of the integral texture. We use the inverse to produce
104 // normalized texture coords from frag coord distances.
105 float invSixSigma = 1.f / sixSigma;
106 return std::unique_ptr<GrFragmentProcessor>(
107 new GrRectBlurEffect(insetRect, std::move(integral), invSixSigma, isFast,
108 GrSamplerState::ClampBilerp()));
Ethan Nicholas82399462017-10-16 12:35:44 -0400109 }
110 GrRectBlurEffect(const GrRectBlurEffect& src);
111 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500112 const char* name() const override { return "RectBlurEffect"; }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400113 SkRect rect;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400114 TextureSampler integral;
115 float invSixSigma;
116 bool isFast;
Ethan Nicholas82399462017-10-16 12:35:44 -0400117
118private:
Brian Salomonb2d5d402019-09-10 10:11:52 -0400119 GrRectBlurEffect(SkRect rect, sk_sp<GrTextureProxy> integral, float invSixSigma, bool isFast,
Brian Salomone7366842019-09-04 11:20:45 -0400120 GrSamplerState samplerParams)
Ethan Nicholas82399462017-10-16 12:35:44 -0400121 : INHERITED(kGrRectBlurEffect_ClassID,
122 (OptimizationFlags)kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400123 , rect(rect)
Brian Salomonb2d5d402019-09-10 10:11:52 -0400124 , integral(std::move(integral), samplerParams)
125 , invSixSigma(invSixSigma)
126 , isFast(isFast) {
Brian Salomone7366842019-09-04 11:20:45 -0400127 this->setTextureSamplerCnt(1);
128 }
Ethan Nicholas82399462017-10-16 12:35:44 -0400129 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
130 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
131 bool onIsEqual(const GrFragmentProcessor&) const override;
Brian Salomone7366842019-09-04 11:20:45 -0400132 const TextureSampler& onTextureSampler(int) const override;
Ethan Nicholas82399462017-10-16 12:35:44 -0400133 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Ethan Nicholas82399462017-10-16 12:35:44 -0400134 typedef GrFragmentProcessor INHERITED;
135};
136#endif