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 |
| 13 | #include "SkTypes.h" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 14 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 15 | #include "GrProxyProvider.h" |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 16 | #include "GrShaderCaps.h" |
Mike Reed | 554aa6a | 2018-03-12 11:36:26 -0400 | [diff] [blame] | 17 | #include "SkBlurMask.h" |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 18 | #include "SkScalar.h" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 19 | #include "GrFragmentProcessor.h" |
| 20 | #include "GrCoordTransform.h" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 21 | class GrRectBlurEffect : public GrFragmentProcessor { |
| 22 | public: |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 23 | static sk_sp<GrTextureProxy> CreateBlurProfileTexture(GrProxyProvider* proxyProvider, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 24 | float sigma) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 25 | unsigned int profileSize = SkScalarCeilToInt(6 * sigma); |
| 26 | |
| 27 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 28 | GrUniqueKey key; |
| 29 | GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 30 | builder[0] = profileSize; |
| 31 | builder.finish(); |
| 32 | |
| 33 | sk_sp<GrTextureProxy> blurProfile( |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 34 | proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin)); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 35 | if (!blurProfile) { |
Robert Phillips | cb25659 | 2018-02-14 15:20:41 -0500 | [diff] [blame] | 36 | SkImageInfo ii = SkImageInfo::MakeA8(profileSize, 1); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 37 | |
Robert Phillips | cb25659 | 2018-02-14 15:20:41 -0500 | [diff] [blame] | 38 | SkBitmap bitmap; |
| 39 | if (!bitmap.tryAllocPixels(ii)) { |
| 40 | return nullptr; |
| 41 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 42 | |
Robert Phillips | cb25659 | 2018-02-14 15:20:41 -0500 | [diff] [blame] | 43 | SkBlurMask::ComputeBlurProfile(bitmap.getAddr8(0, 0), profileSize, sigma); |
| 44 | bitmap.setImmutable(); |
| 45 | |
| 46 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 47 | if (!image) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 51 | blurProfile = |
| 52 | proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1, |
| 53 | SkBudgeted::kYes, SkBackingFit::kExact); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 54 | if (!blurProfile) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 59 | proxyProvider->assignUniqueKeyToProxy(key, blurProfile.get()); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return blurProfile; |
| 63 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 64 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 65 | static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider* proxyProvider, |
| 66 | const GrShaderCaps& caps, const SkRect& rect, |
| 67 | float sigma) { |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 68 | if (!caps.floatIs32Bits()) { |
| 69 | // We promote the rect uniform from half to float when it has large values for |
| 70 | // precision. If we don't have full float then fail. |
| 71 | if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || |
| 72 | SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f || |
| 73 | SkScalarAbs(rect.width()) > 16000.f || SkScalarAbs(rect.height()) > 16000.f) { |
| 74 | return nullptr; |
| 75 | } |
| 76 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 77 | int doubleProfileSize = SkScalarCeilToInt(12 * sigma); |
| 78 | |
| 79 | if (doubleProfileSize >= rect.width() || doubleProfileSize >= rect.height()) { |
| 80 | // if the blur sigma is too large so the gaussian overlaps the whole |
| 81 | // rect in either direction, fall back to CPU path for now. |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 85 | sk_sp<GrTextureProxy> blurProfile(CreateBlurProfileTexture(proxyProvider, sigma)); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 86 | if (!blurProfile) { |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
Robert Phillips | 2b2936e | 2017-10-20 15:11:35 -0400 | [diff] [blame] | 90 | return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect( |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 91 | rect, sigma, std::move(blurProfile), |
Robert Phillips | 2b2936e | 2017-10-20 15:11:35 -0400 | [diff] [blame] | 92 | GrSamplerState(GrSamplerState::WrapMode::kClamp, GrSamplerState::Filter::kBilerp))); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 93 | } |
| 94 | GrRectBlurEffect(const GrRectBlurEffect& src); |
| 95 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 96 | const char* name() const override { return "RectBlurEffect"; } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame^] | 97 | SkRect rect; |
| 98 | float sigma; |
| 99 | TextureSampler blurProfile; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 100 | |
| 101 | private: |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 102 | GrRectBlurEffect(SkRect rect, float sigma, sk_sp<GrTextureProxy> blurProfile, |
| 103 | GrSamplerState samplerParams) |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 104 | : INHERITED(kGrRectBlurEffect_ClassID, |
| 105 | (OptimizationFlags)kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame^] | 106 | , rect(rect) |
| 107 | , sigma(sigma) |
| 108 | , blurProfile(std::move(blurProfile), samplerParams) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 109 | this->setTextureSamplerCnt(1); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 110 | } |
| 111 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 112 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 113 | bool onIsEqual(const GrFragmentProcessor&) const override; |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 114 | const TextureSampler& onTextureSampler(int) const override; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 115 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 116 | typedef GrFragmentProcessor INHERITED; |
| 117 | }; |
| 118 | #endif |