Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [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 GrRectBlurEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 11 | #ifndef GrRectBlurEffect_DEFINED |
| 12 | #define GrRectBlurEffect_DEFINED |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkTypes.h" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 14 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 15 | #include <cmath> |
| 16 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkScalar.h" |
| 18 | #include "src/core/SkBlurMask.h" |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 19 | #include "src/core/SkMathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #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 Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 25 | class GrRectBlurEffect : public GrFragmentProcessor { |
| 26 | public: |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 27 | 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 Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 36 | |
| 37 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 38 | GrUniqueKey key; |
| 39 | GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 40 | builder[0] = width; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 41 | builder.finish(); |
| 42 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 43 | sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey( |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 44 | key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin)); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 45 | if (!proxy) { |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 46 | SkBitmap bitmap; |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 47 | if (!bitmap.tryAllocPixels(SkImageInfo::MakeA8(width, 1))) { |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 48 | return nullptr; |
| 49 | } |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 50 | *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 Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 59 | bitmap.setImmutable(); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 60 | proxy = proxyProvider->createProxyFromBitmap(bitmap, GrMipMapped::kNo); |
| 61 | if (!proxy) { |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 62 | return nullptr; |
| 63 | } |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 64 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
| 65 | proxyProvider->assignUniqueKeyToProxy(key, proxy.get()); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 66 | } |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 67 | return proxy; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 70 | static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider* proxyProvider, |
| 71 | const GrShaderCaps& caps, const SkRect& rect, |
| 72 | float sigma) { |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 73 | SkASSERT(rect.isSorted()); |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 74 | if (!caps.floatIs32Bits()) { |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 75 | // 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 Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 78 | if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 79 | SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) { |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 80 | return nullptr; |
| 81 | } |
| 82 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 83 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 84 | const float sixSigma = 6 * sigma; |
| 85 | auto integral = CreateIntegralTexture(proxyProvider, sixSigma); |
| 86 | if (!integral) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 87 | return nullptr; |
| 88 | } |
| 89 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 90 | // 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 Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 109 | } |
| 110 | GrRectBlurEffect(const GrRectBlurEffect& src); |
| 111 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 112 | const char* name() const override { return "RectBlurEffect"; } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 113 | SkRect rect; |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 114 | TextureSampler integral; |
| 115 | float invSixSigma; |
| 116 | bool isFast; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 117 | |
| 118 | private: |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 119 | GrRectBlurEffect(SkRect rect, sk_sp<GrTextureProxy> integral, float invSixSigma, bool isFast, |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 120 | GrSamplerState samplerParams) |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 121 | : INHERITED(kGrRectBlurEffect_ClassID, |
| 122 | (OptimizationFlags)kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 123 | , rect(rect) |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 124 | , integral(std::move(integral), samplerParams) |
| 125 | , invSixSigma(invSixSigma) |
| 126 | , isFast(isFast) { |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 127 | this->setTextureSamplerCnt(1); |
| 128 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 129 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 130 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 131 | bool onIsEqual(const GrFragmentProcessor&) const override; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 132 | const TextureSampler& onTextureSampler(int) const override; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 133 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 134 | typedef GrFragmentProcessor INHERITED; |
| 135 | }; |
| 136 | #endif |