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 |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 13 | |
Mike Reed | 46f5c5f | 2020-02-20 15:42:29 -0500 | [diff] [blame] | 14 | #include "include/core/SkM44.h" |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 15 | #include "include/core/SkTypes.h" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 16 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 17 | #include <cmath> |
| 18 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkScalar.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 20 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/core/SkBlurMask.h" |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 22 | #include "src/core/SkMathPriv.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrProxyProvider.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/GrShaderCaps.h" |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 27 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/GrFragmentProcessor.h" |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 30 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 31 | class GrRectBlurEffect : public GrFragmentProcessor { |
| 32 | public: |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 33 | static std::unique_ptr<GrFragmentProcessor> MakeIntegralFP(GrRecordingContext* context, |
| 34 | float sixSigma) { |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 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. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 41 | int width = std::max(SkNextPow2(minWidth), 32); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 42 | |
| 43 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 44 | GrUniqueKey key; |
| 45 | GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 46 | builder[0] = width; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 47 | builder.finish(); |
| 48 | |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 49 | SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f); |
| 50 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 51 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 52 | if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) { |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 53 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
| 54 | GrColorType::kAlpha_8); |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 55 | GrSurfaceProxyView view{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle}; |
| 56 | return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 57 | GrSamplerState::Filter::kLinear); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 58 | } |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 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 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 75 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame^] | 76 | auto view = maker.view(GrMipmapped::kNo); |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 77 | if (!view) { |
| 78 | return {}; |
| 79 | } |
| 80 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
| 81 | proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy()); |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 82 | return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 83 | GrSamplerState::Filter::kLinear); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 84 | } |
| 85 | |
John Stiles | e7b4d73 | 2020-06-10 12:46:40 -0400 | [diff] [blame] | 86 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 87 | GrRecordingContext* context, |
Brian Osman | ff0717f | 2020-03-27 11:08:38 -0400 | [diff] [blame] | 88 | const GrShaderCaps& caps, |
| 89 | const SkRect& rect, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 90 | float sigma) { |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 91 | SkASSERT(rect.isSorted()); |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 92 | if (!caps.floatIs32Bits()) { |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 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. |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 96 | if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 97 | SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) { |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 98 | return nullptr; |
| 99 | } |
| 100 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 101 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 102 | const float sixSigma = 6 * sigma; |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 103 | std::unique_ptr<GrFragmentProcessor> integral = MakeIntegralFP(context, sixSigma); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 104 | if (!integral) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 105 | return nullptr; |
| 106 | } |
| 107 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 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(); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 121 | return std::unique_ptr<GrFragmentProcessor>( |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 122 | new GrRectBlurEffect(std::move(inputFP), insetRect, std::move(integral), isFast, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 123 | GrSamplerState::Filter::kLinear)); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 124 | } |
| 125 | GrRectBlurEffect(const GrRectBlurEffect& src); |
| 126 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 127 | const char* name() const override { return "RectBlurEffect"; } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 128 | SkRect rect; |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 129 | bool isFast; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 130 | |
| 131 | private: |
John Stiles | e7b4d73 | 2020-06-10 12:46:40 -0400 | [diff] [blame] | 132 | GrRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 133 | SkRect rect, |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 134 | std::unique_ptr<GrFragmentProcessor> integral, |
Brian Osman | ff0717f | 2020-03-27 11:08:38 -0400 | [diff] [blame] | 135 | bool isFast, |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 136 | GrSamplerState samplerParams) |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 137 | : INHERITED(kGrRectBlurEffect_ClassID, |
John Stiles | e7b4d73 | 2020-06-10 12:46:40 -0400 | [diff] [blame] | 138 | (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get()) |
| 139 | : kAll_OptimizationFlags) & |
| 140 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 141 | , rect(rect) |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 142 | , isFast(isFast) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 143 | this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough()); |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 144 | SkASSERT(integral); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 145 | this->registerChild(std::move(integral), SkSL::SampleUsage::Explicit()); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 146 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 147 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 148 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 149 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 150 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 151 | typedef GrFragmentProcessor INHERITED; |
| 152 | }; |
| 153 | #endif |