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