1 /*
2  * Copyright 2016 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 #include "SkGaussianEdgeShader.h"
9 #include "SkReadBuffer.h"
10 #include "SkWriteBuffer.h"
11 
12 class SkArenaAlloc;
13 
14  /** \class SkGaussianEdgeShaderImpl
15  This subclass of shader applies a Gaussian to shadow edge
16 
17  If the primitive supports an implicit distance to the edge, the radius of the blur is specified
18  by r & g values of the color in 14.2 fixed point. For spot shadows, we increase the stroke width
19  to set the shadow against the shape. This pad is specified by b, also in 6.2 fixed point.
20 
21  When not using implicit distance, then b in the input color represents the input to the
22  blur function.
23  */
24 class SkGaussianEdgeShaderImpl : public SkShader {
25 public:
SkGaussianEdgeShaderImpl()26     SkGaussianEdgeShaderImpl() {}
27 
28     bool isOpaque() const override;
29 
30 #if SK_SUPPORT_GPU
31     sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
32 #endif
33 
34     SK_TO_STRING_OVERRIDE()
35     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl)
36 
37 protected:
38     void flatten(SkWriteBuffer&) const override;
onMakeContext(const ContextRec & rec,SkArenaAlloc * storage) const39     Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* storage) const override {
40         return nullptr;
41     }
42 private:
43     friend class SkGaussianEdgeShader;
44 
45     typedef SkShader INHERITED;
46 };
47 
48 ////////////////////////////////////////////////////////////////////////////
49 
50 #if SK_SUPPORT_GPU
51 
52 #include "effects/GrBlurredEdgeFragmentProcessor.h"
53 
54 ////////////////////////////////////////////////////////////////////////////
55 
asFragmentProcessor(const AsFPArgs &) const56 sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const AsFPArgs&) const {
57     return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode);
58 }
59 
60 #endif
61 
62 ////////////////////////////////////////////////////////////////////////////
63 
isOpaque() const64 bool SkGaussianEdgeShaderImpl::isOpaque() const {
65     return false;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////
69 
70 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const71 void SkGaussianEdgeShaderImpl::toString(SkString* str) const {
72     str->appendf("GaussianEdgeShader: ()");
73 }
74 #endif
75 
CreateProc(SkReadBuffer & buf)76 sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) {
77     return sk_make_sp<SkGaussianEdgeShaderImpl>();
78 }
79 
flatten(SkWriteBuffer & buf) const80 void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const {
81 }
82 
83 ///////////////////////////////////////////////////////////////////////////////
84 
Make()85 sk_sp<SkShader> SkGaussianEdgeShader::Make() {
86     return sk_make_sp<SkGaussianEdgeShaderImpl>();
87 }
88 
89 ///////////////////////////////////////////////////////////////////////////////
90 
91 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader)
92 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl)
93 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
94 
95 ///////////////////////////////////////////////////////////////////////////////
96