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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkScalar.h" |
| 16 | #include "src/core/SkBlurMask.h" |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 17 | #include "src/core/SkMathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrProxyProvider.h" |
| 19 | #include "src/gpu/GrShaderCaps.h" |
| 20 | |
| 21 | #include "src/gpu/GrCoordTransform.h" |
| 22 | #include "src/gpu/GrFragmentProcessor.h" |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 23 | class GrRectBlurEffect : public GrFragmentProcessor { |
| 24 | public: |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 25 | static sk_sp<GrTextureProxy> CreateBlurProfileTexture(GrProxyProvider* proxyProvider, |
| 26 | float sigma) { |
| 27 | // The "profile" we are calculating is the integral of a Gaussian with 'sigma' and a half |
| 28 | // plane. All such profiles are just scales of each other. So all we really care about is |
| 29 | // having enough resolution so that the linear interpolation done in texture lookup doesn't |
| 30 | // introduce noticeable artifacts. SkBlurMask::ComputeBlurProfile() produces profiles with |
| 31 | // ceil(6 * sigma) entries. We conservatively choose to have 2 texels for each dst pixel. |
| 32 | int minProfileWidth = 2 * sk_float_ceil2int(6 * sigma); |
| 33 | // Bin by powers of 2 with a minimum so we get good profile reuse (remember we can just |
| 34 | // scale the texture coords to span the larger profile over a 6 sigma distance). |
| 35 | int profileWidth = SkTMax(SkNextPow2(minProfileWidth), 32); |
| 36 | |
| 37 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 38 | GrUniqueKey key; |
| 39 | GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); |
| 40 | builder[0] = profileWidth; |
| 41 | builder.finish(); |
| 42 | |
| 43 | sk_sp<GrTextureProxy> blurProfile(proxyProvider->findOrCreateProxyByUniqueKey( |
| 44 | key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin)); |
| 45 | if (!blurProfile) { |
| 46 | SkBitmap bitmap; |
| 47 | if (!bitmap.tryAllocPixels(SkImageInfo::MakeA8(profileWidth, 1))) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | SkBlurMask::ComputeBlurProfile(bitmap.getAddr8(0, 0), profileWidth, profileWidth / 6.f); |
| 51 | bitmap.setImmutable(); |
| 52 | blurProfile = proxyProvider->createProxyFromBitmap(bitmap, GrMipMapped::kNo); |
| 53 | if (!blurProfile) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin); |
| 57 | proxyProvider->assignUniqueKeyToProxy(key, blurProfile.get()); |
| 58 | } |
| 59 | |
| 60 | return blurProfile; |
| 61 | } |
| 62 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 63 | static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider* proxyProvider, |
| 64 | const GrShaderCaps& caps, const SkRect& rect, |
| 65 | float sigma) { |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 66 | float doubleProfileSize = (12 * sigma); |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 67 | if (!caps.floatIs32Bits()) { |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 68 | // We promote the math that gets us into the Gaussian space to full float when the rect |
| 69 | // coords are large. If we don't have full float then fail. We could probably clip the |
| 70 | // rect to an outset device bounds instead. |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 71 | if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 72 | SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) { |
Brian Salomon | ff6a7cd | 2018-05-10 09:42:27 -0400 | [diff] [blame] | 73 | return nullptr; |
| 74 | } |
| 75 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 76 | |
Brian Salomon | 2c59659 | 2019-08-13 20:05:04 -0400 | [diff] [blame] | 77 | if (doubleProfileSize >= (float)rect.width() || doubleProfileSize >= (float)rect.height()) { |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 78 | // if the blur sigma is too large so the gaussian overlaps the whole |
| 79 | // rect in either direction, fall back to CPU path for now. |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 83 | auto profile = CreateBlurProfileTexture(proxyProvider, sigma); |
| 84 | if (!profile) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | // The profile is calculated such that the midpoint is at the rect's edge. To simplify |
| 88 | // calculating texture coords in the shader, we inset the rect such that the profile |
| 89 | // can be used with one end point aligned to the edges of the rect uniform. The texture |
| 90 | // coords should be scaled such that the profile is sampled over a 6 sigma range so inset |
| 91 | // by 3 sigma. |
| 92 | float halfW = 3.f * sigma; |
| 93 | auto insetR = rect.makeInset(halfW, halfW); |
| 94 | // inverse of the width over which the profile texture should be interpolated outward from |
| 95 | // the inset rect. |
| 96 | float invWidth = 1.f / (2 * halfW); |
| 97 | return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect( |
| 98 | insetR, std::move(profile), invWidth, GrSamplerState::ClampBilerp())); |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 99 | } |
| 100 | GrRectBlurEffect(const GrRectBlurEffect& src); |
| 101 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 102 | const char* name() const override { return "RectBlurEffect"; } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 103 | SkRect rect; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 104 | TextureSampler blurProfile; |
| 105 | float invProfileWidth; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 106 | |
| 107 | private: |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 108 | GrRectBlurEffect(SkRect rect, sk_sp<GrTextureProxy> blurProfile, float invProfileWidth, |
| 109 | GrSamplerState samplerParams) |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 110 | : INHERITED(kGrRectBlurEffect_ClassID, |
| 111 | (OptimizationFlags)kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 112 | , rect(rect) |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 113 | , blurProfile(std::move(blurProfile), samplerParams) |
| 114 | , invProfileWidth(invProfileWidth) { |
| 115 | this->setTextureSamplerCnt(1); |
| 116 | } |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 117 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 118 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 119 | bool onIsEqual(const GrFragmentProcessor&) const override; |
Brian Salomon | e736684 | 2019-09-04 11:20:45 -0400 | [diff] [blame^] | 120 | const TextureSampler& onTextureSampler(int) const override; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 121 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 122 | typedef GrFragmentProcessor INHERITED; |
| 123 | }; |
| 124 | #endif |