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 #include "SkTypes.h"
14 
15 #include "GrCaps.h"
16 #include "GrClip.h"
17 #include "GrContext.h"
18 #include "GrPaint.h"
19 #include "GrProxyProvider.h"
20 #include "GrRecordingContext.h"
21 #include "GrRecordingContextPriv.h"
22 #include "GrRenderTargetContext.h"
23 #include "GrStyle.h"
24 #include "SkBlurMaskFilter.h"
25 #include "SkBlurPriv.h"
26 #include "SkGpuBlurUtils.h"
27 #include "SkRRectPriv.h"
28 #include "GrFragmentProcessor.h"
29 #include "GrCoordTransform.h"
30 class GrRRectBlurEffect : public GrFragmentProcessor {
31 public:
find_or_create_rrect_blur_mask(GrRecordingContext * context,const SkRRect & rrectToDraw,const SkISize & size,float xformedSigma)32     static sk_sp<GrTextureProxy> find_or_create_rrect_blur_mask(GrRecordingContext* context,
33                                                                 const SkRRect& rrectToDraw,
34                                                                 const SkISize& size,
35                                                                 float xformedSigma) {
36         static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
37         GrUniqueKey key;
38         GrUniqueKey::Builder builder(&key, kDomain, 9, "RoundRect Blur Mask");
39         builder[0] = SkScalarCeilToInt(xformedSigma - 1 / 6.0f);
40 
41         int index = 1;
42         for (auto c : {SkRRect::kUpperLeft_Corner, SkRRect::kUpperRight_Corner,
43                        SkRRect::kLowerRight_Corner, SkRRect::kLowerLeft_Corner}) {
44             SkASSERT(SkScalarIsInt(rrectToDraw.radii(c).fX) &&
45                      SkScalarIsInt(rrectToDraw.radii(c).fY));
46             builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fX);
47             builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fY);
48         }
49         builder.finish();
50 
51         GrProxyProvider* proxyProvider = context->priv().proxyProvider();
52 
53         sk_sp<GrTextureProxy> mask(
54                 proxyProvider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin));
55         if (!mask) {
56             GrBackendFormat format =
57                     context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
58             // TODO: this could be approx but the texture coords will need to be updated
59             sk_sp<GrRenderTargetContext> rtc(
60                     context->priv().makeDeferredRenderTargetContextWithFallback(
61                             format, SkBackingFit::kExact, size.fWidth, size.fHeight,
62                             kAlpha_8_GrPixelConfig, nullptr));
63             if (!rtc) {
64                 return nullptr;
65             }
66 
67             GrPaint paint;
68 
69             rtc->clear(nullptr, SK_PMColor4fTRANSPARENT,
70                        GrRenderTargetContext::CanClearFullscreen::kYes);
71             rtc->drawRRect(GrNoClip(), std::move(paint), GrAA::kYes, SkMatrix::I(), rrectToDraw,
72                            GrStyle::SimpleFill());
73 
74             sk_sp<GrTextureProxy> srcProxy(rtc->asTextureProxyRef());
75             if (!srcProxy) {
76                 return nullptr;
77             }
78             sk_sp<GrRenderTargetContext> rtc2(
79                     SkGpuBlurUtils::GaussianBlur(context,
80                                                  std::move(srcProxy),
81                                                  nullptr,
82                                                  SkIRect::MakeWH(size.fWidth, size.fHeight),
83                                                  SkIRect::EmptyIRect(),
84                                                  xformedSigma,
85                                                  xformedSigma,
86                                                  GrTextureDomain::kIgnore_Mode,
87                                                  kPremul_SkAlphaType,
88                                                  SkBackingFit::kExact));
89             if (!rtc2) {
90                 return nullptr;
91             }
92 
93             mask = rtc2->asTextureProxyRef();
94             if (!mask) {
95                 return nullptr;
96             }
97             SkASSERT(mask->origin() == kBottomLeft_GrSurfaceOrigin);
98             proxyProvider->assignUniqueKeyToProxy(key, mask.get());
99         }
100 
101         return mask;
102     }
sigma()103     float sigma() const { return fSigma; }
rect()104     const SkRect& rect() const { return fRect; }
cornerRadius()105     float cornerRadius() const { return fCornerRadius; }
106 
107     static std::unique_ptr<GrFragmentProcessor> Make(GrRecordingContext* context,
108                                                      float sigma,
109                                                      float xformedSigma,
110                                                      const SkRRect& srcRRect,
111                                                      const SkRRect& devRRect);
112     GrRRectBlurEffect(const GrRRectBlurEffect& src);
113     std::unique_ptr<GrFragmentProcessor> clone() const override;
name()114     const char* name() const override { return "RRectBlurEffect"; }
115 
116 private:
GrRRectBlurEffect(float sigma,SkRect rect,float cornerRadius,sk_sp<GrTextureProxy> ninePatchSampler)117     GrRRectBlurEffect(float sigma, SkRect rect, float cornerRadius,
118                       sk_sp<GrTextureProxy> ninePatchSampler)
119             : INHERITED(kGrRRectBlurEffect_ClassID,
120                         (OptimizationFlags)kCompatibleWithCoverageAsAlpha_OptimizationFlag)
121             , fSigma(sigma)
122             , fRect(rect)
123             , fCornerRadius(cornerRadius)
124             , fNinePatchSampler(std::move(ninePatchSampler)) {
125         this->setTextureSamplerCnt(1);
126     }
127     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
128     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
129     bool onIsEqual(const GrFragmentProcessor&) const override;
130     const TextureSampler& onTextureSampler(int) const override;
131     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
132     float fSigma;
133     SkRect fRect;
134     float fCornerRadius;
135     TextureSampler fNinePatchSampler;
136     typedef GrFragmentProcessor INHERITED;
137 };
138 #endif
139