Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [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 GrRRectBlurEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 11 | #ifndef GrRRectBlurEffect_DEFINED |
| 12 | #define GrRRectBlurEffect_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 | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 16 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 17 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/core/SkBlurPriv.h" |
| 19 | #include "src/core/SkGpuBlurUtils.h" |
| 20 | #include "src/core/SkRRectPriv.h" |
| 21 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrPaint.h" |
| 23 | #include "src/gpu/GrProxyProvider.h" |
| 24 | #include "src/gpu/GrRecordingContextPriv.h" |
| 25 | #include "src/gpu/GrRenderTargetContext.h" |
| 26 | #include "src/gpu/GrStyle.h" |
Brian Salomon | 99b0470 | 2020-06-18 10:12:47 -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 | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 31 | class GrRRectBlurEffect : public GrFragmentProcessor { |
| 32 | public: |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 33 | static std::unique_ptr<GrFragmentProcessor> find_or_create_rrect_blur_mask_fp( |
| 34 | GrRecordingContext* context, |
| 35 | const SkRRect& rrectToDraw, |
| 36 | const SkISize& dimensions, |
| 37 | float xformedSigma) { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 38 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 39 | GrUniqueKey key; |
| 40 | GrUniqueKey::Builder builder(&key, kDomain, 9, "RoundRect Blur Mask"); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 41 | builder[0] = SkScalarCeilToInt(xformedSigma - 1 / 6.0f); |
| 42 | |
| 43 | int index = 1; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 44 | for (auto c : {SkRRect::kUpperLeft_Corner, SkRRect::kUpperRight_Corner, |
| 45 | SkRRect::kLowerRight_Corner, SkRRect::kLowerLeft_Corner}) { |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 46 | SkASSERT(SkScalarIsInt(rrectToDraw.radii(c).fX) && |
| 47 | SkScalarIsInt(rrectToDraw.radii(c).fY)); |
| 48 | builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fX); |
| 49 | builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fY); |
| 50 | } |
| 51 | builder.finish(); |
| 52 | |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 53 | // It seems like we could omit this matrix and modify the shader code to not normalize |
| 54 | // the coords used to sample the texture effect. However, the "proxyDims" value in the |
| 55 | // shader is not always the actual the proxy dimensions. This is because 'dimensions' here |
| 56 | // was computed using integer corner radii as determined in |
| 57 | // SkComputeBlurredRRectParams whereas the shader code uses the float radius to compute |
| 58 | // 'proxyDims'. Why it draws correctly with these unequal values is a mystery for the ages. |
| 59 | auto m = SkMatrix::Scale(dimensions.width(), dimensions.height()); |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 60 | static constexpr auto kMaskOrigin = kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 61 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 62 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 63 | if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback( |
Brian Salomon | 0029db0 | 2020-04-03 10:41:24 -0400 | [diff] [blame] | 64 | key, kMaskOrigin, GrColorType::kAlpha_8, 1)) { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 65 | return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 68 | auto rtc = GrRenderTargetContext::MakeWithFallback( |
| 69 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame^] | 70 | GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin); |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 71 | if (!rtc) { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 72 | return nullptr; |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | GrPaint paint; |
| 76 | |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 77 | rtc->clear(SK_PMColor4fTRANSPARENT); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 78 | rtc->drawRRect(nullptr, std::move(paint), GrAA::kYes, SkMatrix::I(), rrectToDraw, |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 79 | GrStyle::SimpleFill()); |
| 80 | |
| 81 | GrSurfaceProxyView srcView = rtc->readSurfaceView(); |
| 82 | if (!srcView) { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 83 | return nullptr; |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 84 | } |
| 85 | SkASSERT(srcView.asTextureProxy()); |
| 86 | auto rtc2 = SkGpuBlurUtils::GaussianBlur(context, |
| 87 | std::move(srcView), |
| 88 | rtc->colorInfo().colorType(), |
| 89 | rtc->colorInfo().alphaType(), |
| 90 | nullptr, |
| 91 | SkIRect::MakeSize(dimensions), |
| 92 | SkIRect::MakeSize(dimensions), |
| 93 | xformedSigma, |
| 94 | xformedSigma, |
| 95 | SkTileMode::kClamp, |
| 96 | SkBackingFit::kExact); |
| 97 | if (!rtc2) { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 98 | return nullptr; |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | GrSurfaceProxyView mask = rtc2->readSurfaceView(); |
| 102 | if (!mask) { |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 103 | return nullptr; |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 104 | } |
| 105 | SkASSERT(mask.asTextureProxy()); |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 106 | SkASSERT(mask.origin() == kMaskOrigin); |
Greg Daniel | 4395612 | 2020-02-11 15:49:27 -0500 | [diff] [blame] | 107 | proxyProvider->assignUniqueKeyToProxy(key, mask.asTextureProxy()); |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 108 | return GrTextureEffect::Make(std::move(mask), kPremul_SkAlphaType, m); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 109 | } |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 110 | |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 111 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 112 | GrRecordingContext* context, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 113 | float sigma, |
| 114 | float xformedSigma, |
| 115 | const SkRRect& srcRRect, |
| 116 | const SkRRect& devRRect); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 117 | GrRRectBlurEffect(const GrRRectBlurEffect& src); |
| 118 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 119 | const char* name() const override { return "RRectBlurEffect"; } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 120 | float sigma; |
| 121 | SkRect rect; |
| 122 | float cornerRadius; |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 123 | |
| 124 | private: |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 125 | GrRRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 126 | float sigma, |
Brian Osman | ff0717f | 2020-03-27 11:08:38 -0400 | [diff] [blame] | 127 | SkRect rect, |
| 128 | float cornerRadius, |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 129 | std::unique_ptr<GrFragmentProcessor> ninePatchFP) |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 130 | : INHERITED(kGrRRectBlurEffect_ClassID, |
John Stiles | 4ca8884 | 2020-06-08 17:20:01 -0400 | [diff] [blame] | 131 | (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get()) |
| 132 | : kAll_OptimizationFlags) & |
| 133 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 134 | , sigma(sigma) |
| 135 | , rect(rect) |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 136 | , cornerRadius(cornerRadius) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 137 | this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough()); |
Brian Salomon | 71f6cfd | 2020-06-17 17:14:12 -0400 | [diff] [blame] | 138 | SkASSERT(ninePatchFP); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 139 | this->registerChild(std::move(ninePatchFP), SkSL::SampleUsage::Explicit()); |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 140 | } |
| 141 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 142 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 143 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 144 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | 297d6ef | 2017-12-20 12:00:11 -0500 | [diff] [blame] | 145 | typedef GrFragmentProcessor INHERITED; |
| 146 | }; |
| 147 | #endif |