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