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