OLD | NEW |
(Empty) | |
| 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 SkGaussianEdgeShaderImpl |
| 13 This subclass of shader applies a Gaussian to shadow edge |
| 14 */ |
| 15 class SkGaussianEdgeShaderImpl : public SkShader { |
| 16 public: |
| 17 SkGaussianEdgeShaderImpl() {} |
| 18 |
| 19 bool isOpaque() const override; |
| 20 |
| 21 #if SK_SUPPORT_GPU |
| 22 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri
de; |
| 23 #endif |
| 24 |
| 25 SK_TO_STRING_OVERRIDE() |
| 26 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl
) |
| 27 |
| 28 protected: |
| 29 void flatten(SkWriteBuffer&) const override; |
| 30 |
| 31 private: |
| 32 friend class SkGaussianEdgeShader; |
| 33 |
| 34 typedef SkShader INHERITED; |
| 35 }; |
| 36 |
| 37 //////////////////////////////////////////////////////////////////////////// |
| 38 |
| 39 #if SK_SUPPORT_GPU |
| 40 |
| 41 #include "GrCoordTransform.h" |
| 42 #include "GrFragmentProcessor.h" |
| 43 #include "GrInvariantOutput.h" |
| 44 #include "glsl/GrGLSLFragmentProcessor.h" |
| 45 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 46 #include "glsl/GrGLSLProgramDataManager.h" |
| 47 #include "glsl/GrGLSLUniformHandler.h" |
| 48 #include "SkGr.h" |
| 49 #include "SkGrPriv.h" |
| 50 |
| 51 class GaussianEdgeFP : public GrFragmentProcessor { |
| 52 public: |
| 53 GaussianEdgeFP() { |
| 54 this->initClassID<GaussianEdgeFP>(); |
| 55 |
| 56 // enable output of distance information for shape |
| 57 fUsesDistanceVectorField = true; |
| 58 } |
| 59 |
| 60 class GLSLGaussianEdgeFP : public GrGLSLFragmentProcessor { |
| 61 public: |
| 62 GLSLGaussianEdgeFP() {} |
| 63 |
| 64 void emitCode(EmitArgs& args) override { |
| 65 |
| 66 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 67 |
| 68 fragBuilder->codeAppendf("vec4 output = %s;", args.fInputColor); |
| 69 // outside the outer edge |
| 70 fragBuilder->codeAppendf("if (%s.z <= 0) {", fragBuilder->distanceVe
ctorName()); |
| 71 fragBuilder->codeAppend("output *= 0.0;"); |
| 72 // inside the stroke |
| 73 fragBuilder->codeAppendf("} else if (%s.w > 0) {", fragBuilder->dist
anceVectorName()); |
| 74 fragBuilder->codeAppendf("float factor = %s.w/(%s.z + %s.w);", |
| 75 fragBuilder->distanceVectorName(), |
| 76 fragBuilder->distanceVectorName(), |
| 77 fragBuilder->distanceVectorName()); |
| 78 fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.01
8;"); |
| 79 fragBuilder->codeAppend("output *= factor;"); |
| 80 fragBuilder->codeAppend("}"); |
| 81 fragBuilder->codeAppendf("%s = output;", args.fOutputColor); |
| 82 } |
| 83 |
| 84 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 85 GrProcessorKeyBuilder* b) { |
| 86 // only one shader generated currently |
| 87 b->add32(0x0); |
| 88 } |
| 89 |
| 90 protected: |
| 91 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override {} |
| 92 }; |
| 93 |
| 94 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { |
| 95 GLSLGaussianEdgeFP::GenKey(*this, caps, b); |
| 96 } |
| 97 |
| 98 const char* name() const override { return "GaussianEdgeFP"; } |
| 99 |
| 100 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 101 inout->mulByUnknownFourComponents(); |
| 102 } |
| 103 |
| 104 private: |
| 105 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLGaussianEdgeFP; } |
| 106 |
| 107 bool onIsEqual(const GrFragmentProcessor& proc) const override { return true
; } |
| 108 }; |
| 109 |
| 110 //////////////////////////////////////////////////////////////////////////// |
| 111 |
| 112 sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const A
sFPArgs& args) const { |
| 113 return sk_make_sp<GaussianEdgeFP>(); |
| 114 } |
| 115 |
| 116 #endif |
| 117 |
| 118 //////////////////////////////////////////////////////////////////////////// |
| 119 |
| 120 bool SkGaussianEdgeShaderImpl::isOpaque() const { |
| 121 return false; |
| 122 } |
| 123 |
| 124 //////////////////////////////////////////////////////////////////////////// |
| 125 |
| 126 #ifndef SK_IGNORE_TO_STRING |
| 127 void SkGaussianEdgeShaderImpl::toString(SkString* str) const { |
| 128 str->appendf("GaussianEdgeShader: ()"); |
| 129 } |
| 130 #endif |
| 131 |
| 132 sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) { |
| 133 return sk_make_sp<SkGaussianEdgeShaderImpl>(); |
| 134 } |
| 135 |
| 136 void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const { |
| 137 this->INHERITED::flatten(buf); |
| 138 } |
| 139 |
| 140 /////////////////////////////////////////////////////////////////////////////// |
| 141 |
| 142 sk_sp<SkShader> SkGaussianEdgeShader::Make() { |
| 143 return sk_make_sp<SkGaussianEdgeShaderImpl>(); |
| 144 } |
| 145 |
| 146 /////////////////////////////////////////////////////////////////////////////// |
| 147 |
| 148 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader) |
| 149 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl) |
| 150 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 151 |
| 152 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |