| 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | |
| 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrRRectBlurEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
| 11 | #ifndef GrRRectBlurEffect_DEFINED |
| 12 | #define GrRRectBlurEffect_DEFINED |
| 13 | |
| 14 | #include "include/core/SkM44.h" |
| 15 | #include "include/core/SkTypes.h" |
| 16 | |
| 17 | #include "include/gpu/GrRecordingContext.h" |
| 18 | #include "src/core/SkBlurPriv.h" |
| 19 | #include "src/core/SkGpuBlurUtils.h" |
| 20 | #include "src/core/SkRRectPriv.h" |
| 21 | #include "src/gpu/GrCaps.h" |
| 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" |
| 27 | #include "src/gpu/effects/GrTextureEffect.h" |
| 28 | |
| 29 | #include "src/gpu/GrFragmentProcessor.h" |
| 30 | |
| 31 | class GrRRectBlurEffect : public GrFragmentProcessor { |
| 32 | public: |
| 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) { |
| 38 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 39 | GrUniqueKey key; |
| 40 | GrUniqueKey::Builder builder(&key, kDomain, 9, "RoundRect Blur Mask" ); |
| 41 | builder[0] = SkScalarCeilToInt(xformedSigma - 1 / 6.0f); |
| 42 | |
| 43 | int index = 1; |
| 44 | for (auto c : {SkRRect::kUpperLeft_Corner, SkRRect::kUpperRight_Corner, |
| 45 | SkRRect::kLowerRight_Corner, SkRRect::kLowerLeft_Corner}) { |
| 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 | |
| 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()); |
| 60 | static constexpr auto kMaskOrigin = kBottomLeft_GrSurfaceOrigin; |
| 61 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 62 | |
| 63 | if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback( |
| 64 | key, kMaskOrigin, GrColorType::kAlpha_8, 1)) { |
| 65 | return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); |
| 66 | } |
| 67 | |
| 68 | auto rtc = GrRenderTargetContext::MakeWithFallback( |
| 69 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1, |
| 70 | GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin); |
| 71 | if (!rtc) { |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | GrPaint paint; |
| 76 | |
| 77 | rtc->clear(SK_PMColor4fTRANSPARENT); |
| 78 | rtc->drawRRect(nullptr, std::move(paint), GrAA::kYes, SkMatrix::I(), rrectToDraw, |
| 79 | GrStyle::SimpleFill()); |
| 80 | |
| 81 | GrSurfaceProxyView srcView = rtc->readSurfaceView(); |
| 82 | if (!srcView) { |
| 83 | return nullptr; |
| 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) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | GrSurfaceProxyView mask = rtc2->readSurfaceView(); |
| 102 | if (!mask) { |
| 103 | return nullptr; |
| 104 | } |
| 105 | SkASSERT(mask.asTextureProxy()); |
| 106 | SkASSERT(mask.origin() == kMaskOrigin); |
| 107 | proxyProvider->assignUniqueKeyToProxy(key, mask.asTextureProxy()); |
| 108 | return GrTextureEffect::Make(std::move(mask), kPremul_SkAlphaType, m); |
| 109 | } |
| 110 | |
| 111 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 112 | GrRecordingContext* context, |
| 113 | float sigma, |
| 114 | float xformedSigma, |
| 115 | const SkRRect& srcRRect, |
| 116 | const SkRRect& devRRect); |
| 117 | GrRRectBlurEffect(const GrRRectBlurEffect& src); |
| 118 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
| 119 | const char* name() const override { return "RRectBlurEffect" ; } |
| 120 | float sigma; |
| 121 | SkRect rect; |
| 122 | float cornerRadius; |
| 123 | |
| 124 | private: |
| 125 | GrRRectBlurEffect(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 126 | float sigma, |
| 127 | SkRect rect, |
| 128 | float cornerRadius, |
| 129 | std::unique_ptr<GrFragmentProcessor> ninePatchFP) |
| 130 | : INHERITED(kGrRRectBlurEffect_ClassID, |
| 131 | (OptimizationFlags)(inputFP ? ProcessorOptimizationFlags(inputFP.get()) |
| 132 | : kAll_OptimizationFlags) & |
| 133 | kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
| 134 | , sigma(sigma) |
| 135 | , rect(rect) |
| 136 | , cornerRadius(cornerRadius) { |
| 137 | this->registerChild(std::move(inputFP), SkSL::SampleUsage::PassThrough()); |
| 138 | SkASSERT(ninePatchFP); |
| 139 | this->registerChild(std::move(ninePatchFP), SkSL::SampleUsage::Explicit()); |
| 140 | } |
| 141 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 142 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 143 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 144 | #if GR_TEST_UTILS |
| 145 | SkString onDumpInfo() const override; |
| 146 | #endif |
| 147 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| 148 | typedef GrFragmentProcessor INHERITED; |
| 149 | }; |
| 150 | #endif |
| 151 | |