1/*
2 * Copyright 2017 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#ifndef GrBlurredEdgeFragmentProcessor_DEFINED
9#define GrBlurredEdgeFragmentProcessor_DEFINED
10
11#include "GrFragmentProcessor.h"
12
13/**
14 * Shader for managing a blurred edge for a shadow.
15 *
16 * There are two blurring modes supported: Gaussian blur function and smoothstep function.
17 *
18 * If the primitive supports an implicit distance to the edge, the radius of the blur is specified
19 * by r & g values of the color in 14.2 fixed point. For spot shadows, we increase the stroke width
20 * to set the shadow against the shape. This pad is specified by b, also in 6.2 fixed point.
21 * The a value represents the max final alpha.
22 *
23 * When not using implicit distance, then b in the input color represents the input to the
24 * blur function, and r the max final alpha.
25 *
26 */
27class GrBlurredEdgeFP : public GrFragmentProcessor {
28public:
29    enum Mode {
30        kGaussian_Mode,
31        kSmoothstep_Mode,
32
33        kLastMode = kSmoothstep_Mode
34    };
35    static const int kModeCnt = kLastMode + 1;
36
37    static sk_sp<GrFragmentProcessor> Make(Mode mode = kGaussian_Mode) {
38        return sk_sp<GrFragmentProcessor>(new GrBlurredEdgeFP(mode));
39    }
40
41    const char* name() const override { return "BlurredEdge"; }
42
43    Mode mode() const { return fMode; }
44
45private:
46    GrBlurredEdgeFP(Mode mode)
47        : INHERITED(kNone_OptimizationFlags)
48        , fMode(mode) {
49        // enable output of distance information for shape
50        this->setWillUseDistanceVectorField();
51
52        this->initClassID<GrBlurredEdgeFP>();
53    }
54
55    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
56
57    void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
58
59    bool onIsEqual(const GrFragmentProcessor&) const override;
60
61    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
62
63    Mode   fMode;
64
65    typedef GrFragmentProcessor INHERITED;
66};
67
68#endif
69