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" |
Robert Phillips | 4ef21ed | 2020-09-02 11:11:29 -0400 | [diff] [blame] | 22 | #include "src/core/SkBlurPriv.h" |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 23 | #include "src/core/SkMathPriv.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrProxyProvider.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 26 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrShaderCaps.h" |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 28 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/GrFragmentProcessor.h" |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 31 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 32 | class GrRectBlurEffect : public GrFragmentProcessor { |
| 33 | public: |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 34 | static std::unique_ptr<GrFragmentProcessor> MakeIntegralFP(GrRecordingContext* context, |
| 35 | float sixSigma) { |
Robert Phillips | 4ef21ed | 2020-09-02 11:11:29 -0400 | [diff] [blame] | 36 | int width = SkCreateIntegralTable(sixSigma, nullptr); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 37 | |
| 38 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 39 | GrUniqueKey key; |
| 40 | GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 41 | builder[0] = width; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 42 | builder.finish(); |
| 43 | |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 44 | SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f); |
| 45 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 46 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 47 | if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) { |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 48 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
| 49 | GrColorType::kAlpha_8); |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 50 | GrSurfaceProxyView view{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle}; |
| 51 | return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 52 | GrSamplerState::Filter::kLinear); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 53 | } |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 54 | |
| 55 | SkBitmap bitmap; |
Robert Phillips | 4ef21ed | 2020-09-02 11:11:29 -0400 | [diff] [blame] | 56 | if (!SkCreateIntegralTable(sixSigma, &bitmap)) { |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 57 | return {}; |
| 58 | } |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 59 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 60 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 61 | auto view = maker.view(GrMipmapped::kNo); |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 62 | if (!view) { |
| 63 | return {}; |
| 64 | } |
| 65 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
| 66 | proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy()); |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 67 | return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 68 | GrSamplerState::Filter::kLinear); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 69 | } |
| 70 | |
John Stiles | e7b4d73 | 2020-06-10 12:46:40 -0400 | [diff] [blame] | 71 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 72 | GrRecordingContext* context, |
Brian Osman | ff0717f | 2020-03-27 11:08:38 -0400 | [diff] [blame] | 73 | const GrShaderCaps& caps, |
| 74 | const SkRect& rect, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 75 | float sigma) { |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 76 | SkASSERT(rect.isSorted()); |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 77 | if (!caps.floatIs32Bits()) { |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 78 | // We promote the math that gets us into the Gaussian space to full float when the rect |
| 79 | // coords are large. If we don't have full float then fail. We could probably clip the |
| 80 | // rect to an outset device bounds instead. |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 81 | if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 82 | SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) { |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 83 | return nullptr; |
| 84 | } |
| 85 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 86 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 87 | const float sixSigma = 6 * sigma; |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 88 | std::unique_ptr<GrFragmentProcessor> integral = MakeIntegralFP(context, sixSigma); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 89 | if (!integral) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 90 | return nullptr; |
| 91 | } |
| 92 | |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 93 | // In the fast variant we think of the midpoint of the integral texture as aligning |
| 94 | // with the closest rect edge both in x and y. To simplify texture coord calculation we |
| 95 | // inset the rect so that the edge of the inset rect corresponds to t = 0 in the texture. |
| 96 | // It actually simplifies things a bit in the !isFast case, too. |
| 97 | float threeSigma = sixSigma / 2; |
| 98 | SkRect insetRect = {rect.fLeft + threeSigma, rect.fTop + threeSigma, |
| 99 | rect.fRight - threeSigma, rect.fBottom - threeSigma}; |
| 100 | |
| 101 | // In our fast variant we find the nearest horizontal and vertical edges and for each |
| 102 | // do a lookup in the integral texture for each and multiply them. When the rect is |
| 103 | // less than 6 sigma wide then things aren't so simple and we have to consider both the |
| 104 | // left and right edge of the rectangle (and similar in y). |
| 105 | bool isFast = insetRect.isSorted(); |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 106 | return std::unique_ptr<GrFragmentProcessor>( |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 107 | new GrRectBlurEffect(std::move(inputFP), insetRect, std::move(integral), isFast, |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 108 | GrSamplerState::Filter::kLinear)); |
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"; } |
John Stiles | 735a5a7 | 2020-08-26 10:21:10 -0400 | [diff] [blame] | 113 | bool usesExplicitReturn() const override; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 114 | SkRect rect; |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 115 | bool isFast; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 116 | |
| 117 | private: |
John Stiles | e7b4d73 | 2020-06-10 12:46:40 -0400 | [diff] [blame] | 118 | GrRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 119 | SkRect rect, |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 120 | std::unique_ptr<GrFragmentProcessor> integral, |
Brian Osman | ff0717f | 2020-03-27 11:08:38 -0400 | [diff] [blame] | 121 | bool isFast, |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 122 | GrSamplerState samplerParams) |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 123 | : INHERITED(kGrRectBlurEffect_ClassID, |
John Stiles | e7b4d73 | 2020-06-10 12:46:40 -0400 | [diff] [blame] | 124 | (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get()) |
| 125 | : kAll_OptimizationFlags) & |
| 126 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 127 | , rect(rect) |
Brian Salomon | b2d5d40 | 2019-09-10 10:11:52 -0400 | [diff] [blame] | 128 | , isFast(isFast) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 129 | this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough()); |
Brian Salomon | 86b2f39 | 2020-06-16 16:01:07 -0400 | [diff] [blame] | 130 | SkASSERT(integral); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 131 | this->registerChild(std::move(integral), SkSL::SampleUsage::Explicit()); |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame] | 132 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 133 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 134 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 135 | bool onIsEqual(const GrFragmentProcessor&) const override; |
John Stiles | 776c284 | 2020-08-13 09:16:49 -0400 | [diff] [blame] | 136 | #if GR_TEST_UTILS |
| 137 | SkString onDumpInfo() const override; |
| 138 | #endif |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 139 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 140 | typedef GrFragmentProcessor INHERITED; |
| 141 | }; |
| 142 | #endif |