aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrGradientEffects.h
blob: fdd5d523535b17b742d5b16dfbcdae2c9f71415b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGradientEffects_DEFINED
#define GrGradientEffects_DEFINED

#include "GrSingleTextureEffect.h"
#include "GrTypes.h"
#include "GrScalar.h"
#include "SkShader.h"

/*
 * The intepretation of the texture matrix depends on the sample mode. The
 * texture matrix is applied both when the texture coordinates are explicit
 * and  when vertex positions are used as texture  coordinates. In the latter
 * case the texture matrix is applied to the pre-view-matrix position 
 * values.
 *
 * Normal SampleMode
 *  The post-matrix texture coordinates are in normalize space with (0,0) at
 *  the top-left and (1,1) at the bottom right.
 * RadialGradient
 *  The matrix specifies the radial gradient parameters.
 *  (0,0) in the post-matrix space is center of the radial gradient.
 * Radial2Gradient
 *   Matrix transforms to space where first circle is centered at the
 *   origin. The second circle will be centered (x, 0) where x may be 
 *   0 and is provided by setRadial2Params. The post-matrix space is 
 *   normalized such that 1 is the second radius - first radius.
 * SweepGradient
 *  The angle from the origin of texture coordinates in post-matrix space
 *  determines the gradient value.
 */

// Base class for Gr gradient effects
class GrGradientEffect : public GrCustomStage {
public:

    GrGradientEffect(GrTexture* texture);
    GrGradientEffect(GrContext* ctx, const SkShader& shader);

    virtual ~GrGradientEffect();

    unsigned int numTextures() const;
    GrTexture* texture(unsigned int index) const;

    bool useTexture() const { return fUseTexture; }

private:

    GrTexture* fTexture;
    bool fUseTexture;

    typedef GrCustomStage INHERITED;

};

class GrGLLinearGradient;

class GrLinearGradient : public GrGradientEffect {

public:

    GrLinearGradient(GrTexture* texture);
    GrLinearGradient(GrContext* ctx, const SkShader& shader);
    virtual ~GrLinearGradient();

    static const char* Name() { return "Linear Gradient"; }
    virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;

    typedef GrGLLinearGradient GLProgramStage;

private:

    typedef GrGradientEffect INHERITED;
};

class GrGLRadialGradient;

class GrRadialGradient : public GrGradientEffect {

public:

    GrRadialGradient(GrTexture* texture);
    GrRadialGradient(GrContext* ctx, const SkShader& shader);
    virtual ~GrRadialGradient();

    static const char* Name() { return "Radial Gradient"; }
    virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;

    typedef GrGLRadialGradient GLProgramStage;

private:

    typedef GrGradientEffect INHERITED;
};

class GrGLRadial2Gradient;

class GrRadial2Gradient : public GrGradientEffect {

public:

    GrRadial2Gradient(GrTexture* texture, GrScalar center, GrScalar radius, bool posRoot);
    GrRadial2Gradient(GrContext* ctx, const SkShader& shader);
    virtual ~GrRadial2Gradient();

    static const char* Name() { return "Two-Point Radial Gradient"; }
    virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
    virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;

    // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
    bool isDegenerate() const { return GR_Scalar1 == fCenterX1; }
    GrScalar center() const { return fCenterX1; }
    GrScalar radius() const { return fRadius0; }
    bool isPosRoot() const { return SkToBool(fPosRoot); }

    typedef GrGLRadial2Gradient GLProgramStage;

private:

    // @{
    // Cache of values - these can change arbitrarily, EXCEPT
    // we shouldn't change between degenerate and non-degenerate?!

    GrScalar fCenterX1;
    GrScalar fRadius0;
    SkBool8  fPosRoot;

    // @}

    typedef GrGradientEffect INHERITED;
};

class GrGLConical2Gradient;

class GrConical2Gradient : public GrGradientEffect {

public:

    GrConical2Gradient(GrTexture* texture, GrScalar center, GrScalar radius, GrScalar diffRadius);
    GrConical2Gradient(GrContext* ctx, const SkShader& shader);
    virtual ~GrConical2Gradient();

    static const char* Name() { return "Two-Point Conical Gradient"; }
    virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
    virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;

    // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
    bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
    GrScalar center() const { return fCenterX1; }
    GrScalar diffRadius() const { return fDiffRadius; }
    GrScalar radius() const { return fRadius0; }

    typedef GrGLConical2Gradient GLProgramStage;

private:

    // @{
    // Cache of values - these can change arbitrarily, EXCEPT
    // we shouldn't change between degenerate and non-degenerate?!

    GrScalar fCenterX1;
    GrScalar fRadius0;
    GrScalar fDiffRadius;

    // @}

    typedef GrGradientEffect INHERITED;
};

class GrGLSweepGradient;

class GrSweepGradient : public GrGradientEffect {

public:

    GrSweepGradient(GrTexture* texture);
    GrSweepGradient(GrContext* ctx, const SkShader& shader);
    virtual ~GrSweepGradient();

    static const char* Name() { return "Sweep Gradient"; }
    virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;

    typedef GrGLSweepGradient GLProgramStage;

protected:

    typedef GrGradientEffect INHERITED;
};

#endif