SkGradientShader.h revision 39123f4c0b4a02b7d94bac4f0348d6ec468b4382
1/*
2 * Copyright 2006 The Android Open Source Project
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 SkGradientShader_DEFINED
9#define SkGradientShader_DEFINED
10
11#include "SkShader.h"
12
13#define SK_SUPPORT_LEGACY_GRADIENT_FACTORIES
14
15/** \class SkGradientShader
16
17    SkGradientShader hosts factories for creating subclasses of SkShader that
18    render linear and radial gradients.
19*/
20class SK_API SkGradientShader {
21public:
22    enum Flags {
23        /** By default gradients will interpolate their colors in unpremul space
24         *  and then premultiply each of the results. By setting this flag, the
25         *  gradients will premultiply their colors first, and then interpolate
26         *  between them.
27         */
28        kInterpolateColorsInPremul_Flag = 1 << 0,
29    };
30
31    /** Returns a shader that generates a linear gradient between the two
32        specified points.
33        <p />
34        CreateLinear returns a shader with a reference count of 1.
35        The caller should decrement the shader's reference count when done with the shader.
36        It is an error for count to be < 2.
37        @param  pts The start and end points for the gradient.
38        @param  colors  The array[count] of colors, to be distributed between the two points
39        @param  pos     May be NULL. array[count] of SkScalars, or NULL, of the relative position of
40                        each corresponding color in the colors array. If this is NULL,
41                        the the colors are distributed evenly between the start and end point.
42                        If this is not null, the values must begin with 0, end with 1.0, and
43                        intermediate values must be strictly increasing.
44        @param  count   Must be >=2. The number of colors (and pos if not NULL) entries.
45        @param  mode    The tiling mode
46    */
47    static SkShader* CreateLinear(const SkPoint pts[2],
48                                  const SkColor colors[], const SkScalar pos[], int count,
49                                  SkShader::TileMode mode,
50                                  uint32_t flags, const SkMatrix* localMatrix);
51
52    static SkShader* CreateLinear(const SkPoint pts[2],
53                                  const SkColor colors[], const SkScalar pos[], int count,
54                                  SkShader::TileMode mode) {
55        return CreateLinear(pts, colors, pos, count, mode, 0, NULL);
56    }
57
58#ifdef SK_SUPPORT_LEGACY_GRADIENT_FACTORIES
59    static SkShader* CreateLinear(const SkPoint pts[2],
60                                  const SkColor colors[], const SkScalar pos[], int count,
61                                  SkShader::TileMode mode, void* ignored,
62                                  uint32_t flags, const SkMatrix* localMatrix) {
63        return CreateLinear(pts, colors, pos, count, mode, flags, localMatrix);
64    }
65#endif
66
67    /** Returns a shader that generates a radial gradient given the center and radius.
68        <p />
69        CreateRadial returns a shader with a reference count of 1.
70        The caller should decrement the shader's reference count when done with the shader.
71        It is an error for colorCount to be < 2, or for radius to be <= 0.
72        @param  center  The center of the circle for this gradient
73        @param  radius  Must be positive. The radius of the circle for this gradient
74        @param  colors  The array[count] of colors, to be distributed between the center and edge of the circle
75        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
76                        each corresponding color in the colors array. If this is NULL,
77                        the the colors are distributed evenly between the center and edge of the circle.
78                        If this is not null, the values must begin with 0, end with 1.0, and
79                        intermediate values must be strictly increasing.
80        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
81        @param  mode    The tiling mode
82    */
83    static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
84                                  const SkColor colors[], const SkScalar pos[], int count,
85                                  SkShader::TileMode mode,
86                                  uint32_t flags, const SkMatrix* localMatrix);
87
88    static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
89                                  const SkColor colors[], const SkScalar pos[], int count,
90                                  SkShader::TileMode mode) {
91        return CreateRadial(center, radius, colors, pos, count, mode, 0, NULL);
92    }
93
94#ifdef SK_SUPPORT_LEGACY_GRADIENT_FACTORIES
95    static SkShader* CreateRadial(const SkPoint& center, SkScalar radius,
96                                  const SkColor colors[], const SkScalar pos[], int count,
97                                  SkShader::TileMode mode, void* ignored,
98                                  uint32_t flags, const SkMatrix* localMatrix) {
99        return CreateRadial(center, radius, colors, pos, count, mode, flags, localMatrix);
100    }
101#endif
102
103    /** Returns a shader that generates a radial gradient given the start position, start radius, end position and end radius.
104        <p />
105        CreateTwoPointRadial returns a shader with a reference count of 1.
106        The caller should decrement the shader's reference count when done with the shader.
107        It is an error for colorCount to be < 2, for startRadius or endRadius to be < 0, or for
108        startRadius to be equal to endRadius.
109        @param  start   The center of the start circle for this gradient
110        @param  startRadius  Must be positive.  The radius of the start circle for this gradient.
111        @param  end     The center of the end circle for this gradient
112        @param  endRadius  Must be positive. The radius of the end circle for this gradient.
113        @param  colors  The array[count] of colors, to be distributed between the center and edge of the circle
114        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
115                        each corresponding color in the colors array. If this is NULL,
116                        the the colors are distributed evenly between the center and edge of the circle.
117                        If this is not null, the values must begin with 0, end with 1.0, and
118                        intermediate values must be strictly increasing.
119        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
120        @param  mode    The tiling mode
121    */
122    static SkShader* CreateTwoPointRadial(const SkPoint& start, SkScalar startRadius,
123                                          const SkPoint& end, SkScalar endRadius,
124                                          const SkColor colors[], const SkScalar pos[], int count,
125                                          SkShader::TileMode mode,
126                                          uint32_t flags, const SkMatrix* localMatrix);
127
128    static SkShader* CreateTwoPointRadial(const SkPoint& start, SkScalar startRadius,
129                                          const SkPoint& end, SkScalar endRadius,
130                                          const SkColor colors[], const SkScalar pos[], int count,
131                                          SkShader::TileMode mode) {
132        return CreateTwoPointRadial(start, startRadius, end, endRadius, colors, pos, count, mode,
133                                    0, NULL);
134    }
135
136#ifdef SK_SUPPORT_LEGACY_GRADIENT_FACTORIES
137    static SkShader* CreateTwoPointRadial(const SkPoint& start, SkScalar startRadius,
138                                          const SkPoint& end, SkScalar endRadius,
139                                          const SkColor colors[], const SkScalar pos[], int count,
140                                          SkShader::TileMode mode, void* ignored,
141                                          uint32_t flags, const SkMatrix* localMatrix) {
142        return CreateTwoPointRadial(start, startRadius, end, endRadius, colors, pos, count, mode,
143                                    flags, localMatrix);
144    }
145#endif
146
147    /**
148     *  Returns a shader that generates a conical gradient given two circles, or
149     *  returns NULL if the inputs are invalid. The gradient interprets the
150     *  two circles according to the following HTML spec.
151     *  http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
152     */
153    static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius,
154                                           const SkPoint& end, SkScalar endRadius,
155                                           const SkColor colors[], const SkScalar pos[], int count,
156                                           SkShader::TileMode mode,
157                                           uint32_t flags, const SkMatrix* localMatrix);
158
159    static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius,
160                                           const SkPoint& end, SkScalar endRadius,
161                                           const SkColor colors[], const SkScalar pos[], int count,
162                                           SkShader::TileMode mode) {
163        return CreateTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode,
164                                     0, NULL);
165    }
166
167#ifdef SK_SUPPORT_LEGACY_GRADIENT_FACTORIES
168    static SkShader* CreateTwoPointConical(const SkPoint& start, SkScalar startRadius,
169                                           const SkPoint& end, SkScalar endRadius,
170                                           const SkColor colors[], const SkScalar pos[], int count,
171                                           SkShader::TileMode mode, void* ignored,
172                                           uint32_t flags, const SkMatrix* localMatrix) {
173        return CreateTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode,
174                                    flags, localMatrix);
175    }
176#endif
177
178    /** Returns a shader that generates a sweep gradient given a center.
179        <p />
180        CreateSweep returns a shader with a reference count of 1.
181        The caller should decrement the shader's reference count when done with the shader.
182        It is an error for colorCount to be < 2.
183        @param  cx      The X coordinate of the center of the sweep
184        @param  cx      The Y coordinate of the center of the sweep
185        @param  colors  The array[count] of colors, to be distributed around the center.
186        @param  pos     May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
187                        each corresponding color in the colors array. If this is NULL,
188                        the the colors are distributed evenly between the center and edge of the circle.
189                        If this is not null, the values must begin with 0, end with 1.0, and
190                        intermediate values must be strictly increasing.
191        @param  count   Must be >= 2. The number of colors (and pos if not NULL) entries
192    */
193    static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
194                                 const SkColor colors[], const SkScalar pos[], int count,
195                                 uint32_t flags, const SkMatrix* localMatrix);
196
197    static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
198                                 const SkColor colors[], const SkScalar pos[], int count) {
199        return CreateSweep(cx, cy, colors, pos, count, 0, NULL);
200    }
201
202#ifdef SK_SUPPORT_LEGACY_GRADIENT_FACTORIES
203    static SkShader* CreateSweep(SkScalar cx, SkScalar cy,
204                                 const SkColor colors[], const SkScalar pos[], int count,
205                                 void* ignored,
206                                 uint32_t flags, const SkMatrix* localMatrix) {
207        return CreateSweep(cx, cy, colors, pos, count, flags, localMatrix);
208    }
209#endif
210
211    SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
212};
213
214#endif
215