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 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 12 | class SkArenaAlloc; |
| 13 | |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 14 | /** \class SkGaussianEdgeShaderImpl |
| 15 | This subclass of shader applies a Gaussian to shadow edge |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 16 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 17 | If the primitive supports an implicit distance to the edge, the radius of the blur is specified |
| 18 | by r & g values of the color in 14.2 fixed point. For spot shadows, we increase the stroke width |
| 19 | to set the shadow against the shape. This pad is specified by b, also in 6.2 fixed point. |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 20 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 21 | When not using implicit distance, then b in the input color represents the input to the |
| 22 | blur function. |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 23 | */ |
| 24 | class SkGaussianEdgeShaderImpl : public SkShader { |
| 25 | public: |
jvanverth | a8370b2 | 2016-09-16 09:13:15 -0700 | [diff] [blame] | 26 | SkGaussianEdgeShaderImpl() {} |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 27 | |
| 28 | bool isOpaque() const override; |
| 29 | |
| 30 | #if SK_SUPPORT_GPU |
| 31 | sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override; |
| 32 | #endif |
| 33 | |
| 34 | SK_TO_STRING_OVERRIDE() |
| 35 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianEdgeShaderImpl) |
| 36 | |
| 37 | protected: |
| 38 | void flatten(SkWriteBuffer&) const override; |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 39 | Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* storage) const override { |
| 40 | return nullptr; |
| 41 | } |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 42 | private: |
| 43 | friend class SkGaussianEdgeShader; |
| 44 | |
| 45 | typedef SkShader INHERITED; |
| 46 | }; |
| 47 | |
| 48 | //////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | #if SK_SUPPORT_GPU |
| 51 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 52 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 53 | |
| 54 | //////////////////////////////////////////////////////////////////////////// |
| 55 | |
jvanverth | a8370b2 | 2016-09-16 09:13:15 -0700 | [diff] [blame] | 56 | sk_sp<GrFragmentProcessor> SkGaussianEdgeShaderImpl::asFragmentProcessor(const AsFPArgs&) const { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 57 | return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | #endif |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | bool SkGaussianEdgeShaderImpl::isOpaque() const { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | //////////////////////////////////////////////////////////////////////////// |
| 69 | |
| 70 | #ifndef SK_IGNORE_TO_STRING |
| 71 | void SkGaussianEdgeShaderImpl::toString(SkString* str) const { |
| 72 | str->appendf("GaussianEdgeShader: ()"); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) { |
| 77 | return sk_make_sp<SkGaussianEdgeShaderImpl>(); |
| 78 | } |
| 79 | |
| 80 | void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const { |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
jvanverth | a8370b2 | 2016-09-16 09:13:15 -0700 | [diff] [blame] | 85 | sk_sp<SkShader> SkGaussianEdgeShader::Make() { |
| 86 | return sk_make_sp<SkGaussianEdgeShaderImpl>(); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGaussianEdgeShader) |
| 92 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkGaussianEdgeShaderImpl) |
| 93 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |