Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(930)

Side by Side Diff: src/effects/SkGaussianEdgeShader.cpp

Issue 2319003003: Update SampleAndroidShadows to use algorithm closer to Android OpenGL (Closed)
Patch Set: Address comment Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « samplecode/SampleAndroidShadows.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkGaussianEdgeShader.h" 8 #include "SkGaussianEdgeShader.h"
9 #include "SkReadBuffer.h" 9 #include "SkReadBuffer.h"
10 #include "SkWriteBuffer.h" 10 #include "SkWriteBuffer.h"
11 11
12 /** \class SkGaussianEdgeShaderImpl 12 /** \class SkGaussianEdgeShaderImpl
13 This subclass of shader applies a Gaussian to shadow edge 13 This subclass of shader applies a Gaussian to shadow edge
14 14
15 If largerBlur is false:
15 The radius of the Gaussian blur is specified by the g value of the color, in 6. 2 fixed point. 16 The radius of the Gaussian blur is specified by the g value of the color, in 6. 2 fixed point.
16 For spot shadows, we increase the stroke width to set the shadow against the sh ape. This pad 17 For spot shadows, we increase the stroke width to set the shadow against the sh ape. This pad
17 is specified by b, also in 6.2 fixed point. The r value represents the max fina l alpha. 18 is specified by b, also in 6.2 fixed point. The r value represents the max fina l alpha.
18 The incoming alpha should be 1. 19 The incoming alpha should be 1.
20
21 If largerBlur is true:
22 The radius of the Gaussian blur is specified by the r & g values of the color i n 14.2 fixed point.
23 For spot shadows, we increase the stroke width to set the shadow against the sh ape. This pad
24 is specified by b, also in 6.2 fixed point. The a value represents the max fina l alpha.
25
26 LargerBlur will be removed once Android is migrated to the updated shader.
19 */ 27 */
20 class SkGaussianEdgeShaderImpl : public SkShader { 28 class SkGaussianEdgeShaderImpl : public SkShader {
21 public: 29 public:
22 SkGaussianEdgeShaderImpl() {} 30 SkGaussianEdgeShaderImpl()
31 : fLargerBlur(false) {}
32
33 SkGaussianEdgeShaderImpl(bool largerBlur)
34 : fLargerBlur(largerBlur) {}
23 35
24 bool isOpaque() const override; 36 bool isOpaque() const override;
25 37
26 #if SK_SUPPORT_GPU 38 #if SK_SUPPORT_GPU
27 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de; 39 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de;
28 #endif 40 #endif
29 41
30 SK_TO_STRING_OVERRIDE() 42 SK_TO_STRING_OVERRIDE()
31 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl ) 43 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl )
32 44
33 protected: 45 protected:
34 void flatten(SkWriteBuffer&) const override; 46 void flatten(SkWriteBuffer&) const override;
35 47
36 private: 48 private:
37 friend class SkGaussianEdgeShader; 49 friend class SkGaussianEdgeShader;
50 bool fLargerBlur;
38 51
39 typedef SkShader INHERITED; 52 typedef SkShader INHERITED;
40 }; 53 };
41 54
42 //////////////////////////////////////////////////////////////////////////// 55 ////////////////////////////////////////////////////////////////////////////
43 56
44 #if SK_SUPPORT_GPU 57 #if SK_SUPPORT_GPU
45 58
46 #include "GrCoordTransform.h" 59 #include "GrCoordTransform.h"
47 #include "GrFragmentProcessor.h" 60 #include "GrFragmentProcessor.h"
48 #include "GrInvariantOutput.h" 61 #include "GrInvariantOutput.h"
49 #include "glsl/GrGLSLFragmentProcessor.h" 62 #include "glsl/GrGLSLFragmentProcessor.h"
50 #include "glsl/GrGLSLFragmentShaderBuilder.h" 63 #include "glsl/GrGLSLFragmentShaderBuilder.h"
51 #include "glsl/GrGLSLProgramDataManager.h" 64 #include "glsl/GrGLSLProgramDataManager.h"
52 #include "glsl/GrGLSLUniformHandler.h" 65 #include "glsl/GrGLSLUniformHandler.h"
53 #include "SkGr.h" 66 #include "SkGr.h"
54 #include "SkGrPriv.h" 67 #include "SkGrPriv.h"
55 68
56 class GaussianEdgeFP : public GrFragmentProcessor { 69 class GaussianEdgeFP : public GrFragmentProcessor {
57 public: 70 public:
58 GaussianEdgeFP() { 71 GaussianEdgeFP(bool largerBlur) : fLargerBlur(largerBlur) {
59 this->initClassID<GaussianEdgeFP>(); 72 this->initClassID<GaussianEdgeFP>();
60 73
61 // enable output of distance information for shape 74 // enable output of distance information for shape
62 fUsesDistanceVectorField = true; 75 fUsesDistanceVectorField = true;
63 } 76 }
64 77
65 class GLSLGaussianEdgeFP : public GrGLSLFragmentProcessor { 78 class GLSLGaussianEdgeFP : public GrGLSLFragmentProcessor {
66 public: 79 public:
67 GLSLGaussianEdgeFP() {} 80 GLSLGaussianEdgeFP(bool largerBlur) : fLargerBlur(largerBlur) {}
68 81
69 void emitCode(EmitArgs& args) override { 82 void emitCode(EmitArgs& args) override {
70 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 83 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
71 84
72 if (!args.fGpImplementsDistanceVector) { 85 if (!args.fGpImplementsDistanceVector) {
73 fragBuilder->codeAppendf("// GP does not implement fsDistanceVec tor - " 86 fragBuilder->codeAppendf("// GP does not implement fsDistanceVec tor - "
74 " returning grey in GLSLGaussianEdgeFP\ n"); 87 " returning grey in GLSLGaussianEdgeFP\ n");
75 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); 88 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor);
76 fragBuilder->codeAppendf("%s = vec4(0, 0, 0, color.r);", args.fO utputColor); 89 fragBuilder->codeAppendf("%s = vec4(0, 0, 0, color.r);", args.fO utputColor);
90 } else if (fLargerBlur) {
91 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor);
92 fragBuilder->codeAppend("float radius = color.r*256*64 + color.g *64;");
93 fragBuilder->codeAppend("float pad = color.b*64;");
94
95 fragBuilder->codeAppendf("float factor = 1 - clamp((%s.z - pad)/ radius, 0, 1);",
96 fragBuilder->distanceVectorName());
97 fragBuilder->codeAppend("factor = exp(-factor * factor * 4) - 0. 018;");
98 fragBuilder->codeAppendf("%s = factor*vec4(0, 0, 0, color.a);",
99 args.fOutputColor);
77 } else { 100 } else {
78 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); 101 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor);
79 fragBuilder->codeAppend("float radius = color.g*64.0;"); 102 fragBuilder->codeAppend("float radius = color.g*64;");
80 fragBuilder->codeAppend("float pad = color.b*64.0;"); 103 fragBuilder->codeAppend("float pad = color.b*64;");
81 104
82 fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad )/radius," 105 fragBuilder->codeAppendf("float factor = 1 - clamp((%s.z - pad)/ radius, 0, 1);",
83 "0.0, 1.0);" ,
84 fragBuilder->distanceVectorName()); 106 fragBuilder->distanceVectorName());
85 fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;"); 107 fragBuilder->codeAppend("factor = exp(-factor * factor * 4) - 0. 018;");
86 fragBuilder->codeAppendf("%s = factor*vec4(0.0, 0.0, 0.0, color. r);", 108 fragBuilder->codeAppendf("%s = factor*vec4(0, 0, 0, color.r);",
87 args.fOutputColor); 109 args.fOutputColor);
88 } 110 }
89 } 111 }
90 112
91 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, 113 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
92 GrProcessorKeyBuilder* b) { 114 GrProcessorKeyBuilder* b) {
93 // only one shader generated currently 115 const GaussianEdgeFP& gefp = proc.cast<GaussianEdgeFP>();
94 b->add32(0x0); 116 b->add32(gefp.fLargerBlur ? 0x1 : 0x0);
95 } 117 }
96 118
97 protected: 119 protected:
98 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {} 120 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {}
121
122 bool fLargerBlur;
99 }; 123 };
100 124
101 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { 125 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
102 GLSLGaussianEdgeFP::GenKey(*this, caps, b); 126 GLSLGaussianEdgeFP::GenKey(*this, caps, b);
103 } 127 }
104 128
105 const char* name() const override { return "GaussianEdgeFP"; } 129 const char* name() const override { return "GaussianEdgeFP"; }
106 130
107 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { 131 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
108 inout->mulByUnknownFourComponents(); 132 inout->mulByUnknownFourComponents();
109 } 133 }
110 134
111 private: 135 private:
112 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLGaussianEdgeFP; } 136 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
137 return new GLSLGaussianEdgeFP(fLargerBlur);
138 }
113 139
114 bool onIsEqual(const GrFragmentProcessor& proc) const override { return true ; } 140 bool onIsEqual(const GrFragmentProcessor& proc) const override { return true ; }
141
142 bool fLargerBlur;
115 }; 143 };
116 144
117 //////////////////////////////////////////////////////////////////////////// 145 ////////////////////////////////////////////////////////////////////////////
118 146
119 sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const A sFPArgs& args) const { 147 sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const A sFPArgs& args) const {
120 return sk_make_sp<GaussianEdgeFP>(); 148 return sk_make_sp<GaussianEdgeFP>(fLargerBlur);
121 } 149 }
122 150
123 #endif 151 #endif
124 152
125 //////////////////////////////////////////////////////////////////////////// 153 ////////////////////////////////////////////////////////////////////////////
126 154
127 bool SkGaussianEdgeShaderImpl::isOpaque() const { 155 bool SkGaussianEdgeShaderImpl::isOpaque() const {
128 return false; 156 return false;
129 } 157 }
130 158
131 //////////////////////////////////////////////////////////////////////////// 159 ////////////////////////////////////////////////////////////////////////////
132 160
133 #ifndef SK_IGNORE_TO_STRING 161 #ifndef SK_IGNORE_TO_STRING
134 void SkGaussianEdgeShaderImpl::toString(SkString* str) const { 162 void SkGaussianEdgeShaderImpl::toString(SkString* str) const {
135 str->appendf("GaussianEdgeShader: ()"); 163 str->appendf("GaussianEdgeShader: ()");
136 } 164 }
137 #endif 165 #endif
138 166
139 sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) { 167 sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) {
140 return sk_make_sp<SkGaussianEdgeShaderImpl>(); 168 return sk_make_sp<SkGaussianEdgeShaderImpl>();
141 } 169 }
142 170
143 void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const { 171 void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const {
144 } 172 }
145 173
146 /////////////////////////////////////////////////////////////////////////////// 174 ///////////////////////////////////////////////////////////////////////////////
147 175
148 sk_sp<SkShader> SkGaussianEdgeShader::Make() { 176 sk_sp<SkShader> SkGaussianEdgeShader::Make(bool largerBlur) {
149 return sk_make_sp<SkGaussianEdgeShaderImpl>(); 177 return sk_make_sp<SkGaussianEdgeShaderImpl>(largerBlur);
150 } 178 }
151 179
152 /////////////////////////////////////////////////////////////////////////////// 180 ///////////////////////////////////////////////////////////////////////////////
153 181
154 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader) 182 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader)
155 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl) 183 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl)
156 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 184 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
157 185
158 /////////////////////////////////////////////////////////////////////////////// 186 ///////////////////////////////////////////////////////////////////////////////
OLDNEW
« no previous file with comments | « samplecode/SampleAndroidShadows.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698