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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
* 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 SkGradientShaderPriv_DEFINED
#define SkGradientShaderPriv_DEFINED
#include "SkGradientShader.h"
#include "SkClampRange.h"
#include "SkColorPriv.h"
#include "SkFlattenableBuffers.h"
#include "SkMallocPixelRef.h"
#include "SkUnitMapper.h"
#include "SkUtils.h"
#include "SkTemplates.h"
#include "SkBitmapCache.h"
#include "SkShader.h"
static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
int count) {
if (count > 0) {
if (v0 == v1) {
sk_memset32(dst, v0, count);
} else {
int pairs = count >> 1;
for (int i = 0; i < pairs; i++) {
*dst++ = v0;
*dst++ = v1;
}
if (count & 1) {
*dst = v0;
}
}
}
}
// Clamp
static inline SkFixed clamp_tileproc(SkFixed x) {
return SkClampMax(x, 0xFFFF);
}
// Repeat
static inline SkFixed repeat_tileproc(SkFixed x) {
return x & 0xFFFF;
}
// Mirror
// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
// See http://code.google.com/p/skia/issues/detail?id=472
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
#pragma optimize("", off)
#endif
static inline SkFixed mirror_tileproc(SkFixed x) {
int s = x << 15 >> 31;
return (x ^ s) & 0xFFFF;
}
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
#pragma optimize("", on)
#endif
///////////////////////////////////////////////////////////////////////////////
typedef SkFixed (*TileProc)(SkFixed);
///////////////////////////////////////////////////////////////////////////////
static const TileProc gTileProcs[] = {
clamp_tileproc,
repeat_tileproc,
mirror_tileproc
};
///////////////////////////////////////////////////////////////////////////////
class SkGradientShaderBase : public SkShader {
public:
SkGradientShaderBase(const SkColor colors[], const SkScalar pos[],
int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper);
virtual ~SkGradientShaderBase();
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
virtual bool isOpaque() const SK_OVERRIDE;
void getGradientTableBitmap(SkBitmap*) const;
enum {
/// Seems like enough for visual accuracy. TODO: if pos[] deserves
/// it, use a larger cache.
kCache16Bits = 8,
kCache16Count = (1 << kCache16Bits),
kCache16Shift = 16 - kCache16Bits,
kSqrt16Shift = 8 - kCache16Bits,
/// Seems like enough for visual accuracy. TODO: if pos[] deserves
/// it, use a larger cache.
kCache32Bits = 8,
kCache32Count = (1 << kCache32Bits),
kCache32Shift = 16 - kCache32Bits,
kSqrt32Shift = 8 - kCache32Bits,
/// This value is used to *read* the dither cache; it may be 0
/// if dithering is disabled.
kDitherStride32 = kCache32Count,
kDitherStride16 = kCache16Count,
};
protected:
SkGradientShaderBase(SkFlattenableReadBuffer& );
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
SK_DEVELOPER_TO_STRING()
SkUnitMapper* fMapper;
SkMatrix fPtsToUnit; // set by subclass
SkMatrix fDstToIndex;
SkMatrix::MapXYProc fDstToIndexProc;
TileMode fTileMode;
TileProc fTileProc;
int fColorCount;
uint8_t fDstToIndexClass;
uint8_t fFlags;
struct Rec {
SkFixed fPos; // 0...1
uint32_t fScale; // (1 << 24) / range
};
Rec* fRecs;
const uint16_t* getCache16() const;
const SkPMColor* getCache32() const;
void commonAsAGradient(GradientInfo*) const;
private:
enum {
kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
};
SkColor fStorage[(kStorageSize + 3) >> 2];
SkColor* fOrigColors; // original colors, before modulation by paint in setContext
bool fColorsAreOpaque;
mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
mutable SkMallocPixelRef* fCache32PixelRef;
mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
U8CPU alpha);
void setCacheAlpha(U8CPU alpha) const;
void initCommon();
typedef SkShader INHERITED;
};
static inline int init_dither_toggle(int x, int y) {
x &= 1;
y = (y & 1) << 1;
return (x | y) * SkGradientShaderBase::kDitherStride32;
}
static inline int next_dither_toggle(int toggle) {
return toggle ^ SkGradientShaderBase::kDitherStride32;
}
static inline int init_dither_toggle16(int x, int y) {
return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16;
}
static inline int next_dither_toggle16(int toggle) {
return toggle ^ SkGradientShaderBase::kDitherStride16;
}
///////////////////////////////////////////////////////////////////////////////
#if SK_SUPPORT_GPU
#include "gl/GrGLEffect.h"
#include "gl/GrGLEffectMatrix.h"
class GrEffectStage;
class GrBackendEffectFactory;
/*
* The interpretation 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.
*/
class GrTextureStripAtlas;
// Base class for Gr gradient effects
class GrGradientEffect : public GrEffect {
public:
GrGradientEffect(GrContext* ctx,
const SkGradientShaderBase& shader,
const SkMatrix& matrix,
SkShader::TileMode tileMode);
virtual ~GrGradientEffect();
bool useAtlas() const { return SkToBool(-1 != fRow); }
SkScalar getYCoord() const { return fYCoord; };
const SkMatrix& getMatrix() const { return fMatrix;}
virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
protected:
/** Populates a pair of arrays with colors and stop info to construct a random gradient.
The function decides whether stop values should be used or not. The return value indicates
the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
passed to the gradient factory rather than the array.
*/
static const int kMaxRandomGradientColors = 4;
static int RandomGradientParams(SkMWCRandom* r,
SkColor colors[kMaxRandomGradientColors],
SkScalar** stops,
SkShader::TileMode* tm);
virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE;
private:
GrTextureAccess fTextureAccess;
SkScalar fYCoord;
GrTextureStripAtlas* fAtlas;
int fRow;
SkMatrix fMatrix;
bool fIsOpaque;
typedef GrEffect INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
// Base class for GL gradient effects
class GrGLGradientEffect : public GrGLEffect {
public:
GrGLGradientEffect(const GrBackendEffectFactory& factory);
virtual ~GrGLGradientEffect();
virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
protected:
/**
* Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by
* GrGLGradientEffect.
*/
enum {
kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits,
kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1,
};
/**
* Subclasses must call this. It will return a value restricted to the lower kMatrixKeyBitCnt
* bits.
*/
static EffectKey GenMatrixKey(const GrDrawEffect&);
/**
* Inserts code to implement the GrGradientEffect's matrix. This should be called before a
* subclass emits its own code. The name of the 2D coords is output via fsCoordName and already
* incorporates any perspective division. The caller can also optionally retrieve the name of
* the varying inserted in the VS and its type, which may be either vec2f or vec3f depending
* upon whether the matrix has perspective or not. It is not necessary to mask the key before
* calling.
*/
void setupMatrix(GrGLShaderBuilder* builder,
EffectKey key,
const char** fsCoordName,
const char** vsVaryingName = NULL,
GrSLType* vsVaryingType = NULL);
// Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
// should call this method from their emitCode().
void emitYCoordUniform(GrGLShaderBuilder* builder);
// emit code that gets a fragment's color from an expression for t; for now this always uses the
// texture, but for simpler cases we'll be able to lerp. Subclasses should call this method from
// their emitCode().
void emitColorLookup(GrGLShaderBuilder* builder,
const char* gradientTValue,
const char* outputColor,
const char* inputColor,
const GrGLShaderBuilder::TextureSampler&);
private:
static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType;
SkScalar fCachedYCoord;
GrGLUniformManager::UniformHandle fFSYUni;
GrGLEffectMatrix fEffectMatrix;
typedef GrGLEffect INHERITED;
};
#endif
#endif
|