| OLD | NEW |
| 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 If largerBlur is false: |
| 16 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. |
| 17 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 |
| 18 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. |
| 19 The incoming alpha should be 1. | 19 The incoming alpha should be 1. |
| 20 | 20 |
| 21 If largerBlur is true: | 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. | 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 | 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. | 24 is specified by b, also in 6.2 fixed point. The a value represents the max fina
l alpha. |
| 25 | 25 |
| 26 LargerBlur will be removed once Android is migrated to the updated shader. | 26 LargerBlur will be removed once Android is migrated to the updated shader. |
| 27 */ | 27 */ |
| 28 class SkGaussianEdgeShaderImpl : public SkShader { | 28 class SkGaussianEdgeShaderImpl : public SkShader { |
| 29 public: | 29 public: |
| 30 SkGaussianEdgeShaderImpl() | 30 SkGaussianEdgeShaderImpl() {} |
| 31 : fLargerBlur(false) {} | |
| 32 | |
| 33 SkGaussianEdgeShaderImpl(bool largerBlur) | |
| 34 : fLargerBlur(largerBlur) {} | |
| 35 | 31 |
| 36 bool isOpaque() const override; | 32 bool isOpaque() const override; |
| 37 | 33 |
| 38 #if SK_SUPPORT_GPU | 34 #if SK_SUPPORT_GPU |
| 39 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri
de; | 35 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri
de; |
| 40 #endif | 36 #endif |
| 41 | 37 |
| 42 SK_TO_STRING_OVERRIDE() | 38 SK_TO_STRING_OVERRIDE() |
| 43 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl
) | 39 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl
) |
| 44 | 40 |
| 45 protected: | 41 protected: |
| 46 void flatten(SkWriteBuffer&) const override; | 42 void flatten(SkWriteBuffer&) const override; |
| 47 | 43 |
| 48 private: | 44 private: |
| 49 friend class SkGaussianEdgeShader; | 45 friend class SkGaussianEdgeShader; |
| 50 bool fLargerBlur; | |
| 51 | 46 |
| 52 typedef SkShader INHERITED; | 47 typedef SkShader INHERITED; |
| 53 }; | 48 }; |
| 54 | 49 |
| 55 //////////////////////////////////////////////////////////////////////////// | 50 //////////////////////////////////////////////////////////////////////////// |
| 56 | 51 |
| 57 #if SK_SUPPORT_GPU | 52 #if SK_SUPPORT_GPU |
| 58 | 53 |
| 59 #include "GrCoordTransform.h" | 54 #include "GrCoordTransform.h" |
| 60 #include "GrFragmentProcessor.h" | 55 #include "GrFragmentProcessor.h" |
| 61 #include "GrInvariantOutput.h" | 56 #include "GrInvariantOutput.h" |
| 62 #include "glsl/GrGLSLFragmentProcessor.h" | 57 #include "glsl/GrGLSLFragmentProcessor.h" |
| 63 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 58 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 64 #include "glsl/GrGLSLProgramDataManager.h" | 59 #include "glsl/GrGLSLProgramDataManager.h" |
| 65 #include "glsl/GrGLSLUniformHandler.h" | 60 #include "glsl/GrGLSLUniformHandler.h" |
| 66 #include "SkGr.h" | 61 #include "SkGr.h" |
| 67 #include "SkGrPriv.h" | 62 #include "SkGrPriv.h" |
| 68 | 63 |
| 69 class GaussianEdgeFP : public GrFragmentProcessor { | 64 class GaussianEdgeFP : public GrFragmentProcessor { |
| 70 public: | 65 public: |
| 71 GaussianEdgeFP(bool largerBlur) : fLargerBlur(largerBlur) { | 66 GaussianEdgeFP() { |
| 72 this->initClassID<GaussianEdgeFP>(); | 67 this->initClassID<GaussianEdgeFP>(); |
| 73 | 68 |
| 74 // enable output of distance information for shape | 69 // enable output of distance information for shape |
| 75 fUsesDistanceVectorField = true; | 70 fUsesDistanceVectorField = true; |
| 76 } | 71 } |
| 77 | 72 |
| 78 class GLSLGaussianEdgeFP : public GrGLSLFragmentProcessor { | 73 class GLSLGaussianEdgeFP : public GrGLSLFragmentProcessor { |
| 79 public: | 74 public: |
| 80 GLSLGaussianEdgeFP(bool largerBlur) : fLargerBlur(largerBlur) {} | 75 GLSLGaussianEdgeFP() {} |
| 81 | 76 |
| 82 void emitCode(EmitArgs& args) override { | 77 void emitCode(EmitArgs& args) override { |
| 83 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; | 78 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 84 | 79 |
| 85 if (!args.fGpImplementsDistanceVector) { | 80 if (!args.fGpImplementsDistanceVector) { |
| 86 fragBuilder->codeAppendf("// GP does not implement fsDistanceVec
tor - " | 81 fragBuilder->codeAppendf("// GP does not implement fsDistanceVec
tor - " |
| 87 " returning grey in GLSLGaussianEdgeFP\
n"); | 82 " returning grey in GLSLGaussianEdgeFP\
n"); |
| 88 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); | 83 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); |
| 89 fragBuilder->codeAppendf("%s = vec4(0.0, 0.0, 0.0, color.r);", a
rgs.fOutputColor); | 84 fragBuilder->codeAppendf("%s = vec4(0.0, 0.0, 0.0, color.r);", a
rgs.fOutputColor); |
| 90 } else if (fLargerBlur) { | 85 } else { |
| 91 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); | 86 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); |
| 92 fragBuilder->codeAppend("float radius = color.r*256.0*64.0 + col
or.g*64.0;"); | 87 fragBuilder->codeAppend("float radius = color.r*256.0*64.0 + col
or.g*64.0;"); |
| 93 fragBuilder->codeAppend("float pad = color.b*64.0;"); | 88 fragBuilder->codeAppend("float pad = color.b*64.0;"); |
| 94 | 89 |
| 95 fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad
)/radius, 0.0, 1.0);", | 90 fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad
)/radius, 0.0, 1.0);", |
| 96 fragBuilder->distanceVectorName()); | 91 fragBuilder->distanceVectorName()); |
| 97 fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) -
0.018;"); | 92 fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) -
0.018;"); |
| 98 fragBuilder->codeAppendf("%s = factor*vec4(0.0, 0.0, 0.0, color.
a);", | 93 fragBuilder->codeAppendf("%s = factor*vec4(0.0, 0.0, 0.0, color.
a);", |
| 99 args.fOutputColor); | 94 args.fOutputColor); |
| 100 } else { | |
| 101 fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor); | |
| 102 fragBuilder->codeAppend("float radius = color.g*64.0;"); | |
| 103 fragBuilder->codeAppend("float pad = color.b*64.0;"); | |
| 104 | |
| 105 fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad
)/radius, 0.0, 1.0);", | |
| 106 fragBuilder->distanceVectorName()); | |
| 107 fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) -
0.018;"); | |
| 108 fragBuilder->codeAppendf("%s = factor*vec4(0.0, 0.0, 0.0, color.
r);", | |
| 109 args.fOutputColor); | |
| 110 } | 95 } |
| 111 } | 96 } |
| 112 | 97 |
| 113 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 98 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 114 GrProcessorKeyBuilder* b) { | 99 GrProcessorKeyBuilder* b) { |
| 115 const GaussianEdgeFP& gefp = proc.cast<GaussianEdgeFP>(); | 100 // only one shader generated currently |
| 116 b->add32(gefp.fLargerBlur ? 0x1 : 0x0); | 101 b->add32(0x0); |
| 117 } | 102 } |
| 118 | 103 |
| 119 protected: | 104 protected: |
| 120 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override {} | 105 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override {} |
| 121 | 106 |
| 122 bool fLargerBlur; | 107 bool fLargerBlur; |
| 123 }; | 108 }; |
| 124 | 109 |
| 125 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { | 110 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { |
| 126 GLSLGaussianEdgeFP::GenKey(*this, caps, b); | 111 GLSLGaussianEdgeFP::GenKey(*this, caps, b); |
| 127 } | 112 } |
| 128 | 113 |
| 129 const char* name() const override { return "GaussianEdgeFP"; } | 114 const char* name() const override { return "GaussianEdgeFP"; } |
| 130 | 115 |
| 131 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 116 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 132 inout->mulByUnknownFourComponents(); | 117 inout->mulByUnknownFourComponents(); |
| 133 } | 118 } |
| 134 | 119 |
| 135 private: | 120 private: |
| 136 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { | 121 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 137 return new GLSLGaussianEdgeFP(fLargerBlur); | 122 return new GLSLGaussianEdgeFP(); |
| 138 } | 123 } |
| 139 | 124 |
| 140 bool onIsEqual(const GrFragmentProcessor& proc) const override { return true
; } | 125 bool onIsEqual(const GrFragmentProcessor& proc) const override { return true
; } |
| 141 | |
| 142 bool fLargerBlur; | |
| 143 }; | 126 }; |
| 144 | 127 |
| 145 //////////////////////////////////////////////////////////////////////////// | 128 //////////////////////////////////////////////////////////////////////////// |
| 146 | 129 |
| 147 sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const A
sFPArgs& args) const { | 130 sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const A
sFPArgs&) const { |
| 148 return sk_make_sp<GaussianEdgeFP>(fLargerBlur); | 131 return sk_make_sp<GaussianEdgeFP>(); |
| 149 } | 132 } |
| 150 | 133 |
| 151 #endif | 134 #endif |
| 152 | 135 |
| 153 //////////////////////////////////////////////////////////////////////////// | 136 //////////////////////////////////////////////////////////////////////////// |
| 154 | 137 |
| 155 bool SkGaussianEdgeShaderImpl::isOpaque() const { | 138 bool SkGaussianEdgeShaderImpl::isOpaque() const { |
| 156 return false; | 139 return false; |
| 157 } | 140 } |
| 158 | 141 |
| 159 //////////////////////////////////////////////////////////////////////////// | 142 //////////////////////////////////////////////////////////////////////////// |
| 160 | 143 |
| 161 #ifndef SK_IGNORE_TO_STRING | 144 #ifndef SK_IGNORE_TO_STRING |
| 162 void SkGaussianEdgeShaderImpl::toString(SkString* str) const { | 145 void SkGaussianEdgeShaderImpl::toString(SkString* str) const { |
| 163 str->appendf("GaussianEdgeShader: ()"); | 146 str->appendf("GaussianEdgeShader: ()"); |
| 164 } | 147 } |
| 165 #endif | 148 #endif |
| 166 | 149 |
| 167 sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) { | 150 sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) { |
| 168 return sk_make_sp<SkGaussianEdgeShaderImpl>(); | 151 return sk_make_sp<SkGaussianEdgeShaderImpl>(); |
| 169 } | 152 } |
| 170 | 153 |
| 171 void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const { | 154 void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const { |
| 172 } | 155 } |
| 173 | 156 |
| 174 /////////////////////////////////////////////////////////////////////////////// | 157 /////////////////////////////////////////////////////////////////////////////// |
| 175 | 158 |
| 176 sk_sp<SkShader> SkGaussianEdgeShader::Make(bool largerBlur) { | 159 sk_sp<SkShader> SkGaussianEdgeShader::Make() { |
| 177 return sk_make_sp<SkGaussianEdgeShaderImpl>(largerBlur); | 160 return sk_make_sp<SkGaussianEdgeShaderImpl>(); |
| 178 } | 161 } |
| 179 | 162 |
| 180 /////////////////////////////////////////////////////////////////////////////// | 163 /////////////////////////////////////////////////////////////////////////////// |
| 181 | 164 |
| 182 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader) | 165 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader) |
| 183 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl) | 166 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl) |
| 184 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 167 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 185 | 168 |
| 186 /////////////////////////////////////////////////////////////////////////////// | 169 /////////////////////////////////////////////////////////////////////////////// |
| OLD | NEW |