blob: 7f504948fe9f39f5afe43e54a0b1e741968a03e7 [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkSweepGradient.h"
10
11SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, const SkColor colors[],
12 const SkScalar pos[], int count, SkUnitMapper* mapper)
13: SkGradientShaderBase(colors, pos, count, SkShader::kClamp_TileMode, mapper),
14 fCenter(SkPoint::Make(cx, cy))
15{
16 fPtsToUnit.setTranslate(-cx, -cy);
17}
18
19SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap,
20 SkMatrix* matrix, SkShader::TileMode* xy) const {
21 if (bitmap) {
22 this->commonAsABitmap(bitmap);
23 }
24 if (matrix) {
25 *matrix = fPtsToUnit;
26 }
27 if (xy) {
28 xy[0] = fTileMode;
29 xy[1] = kClamp_TileMode;
30 }
31 return kSweep_BitmapType;
32}
33
34SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const {
35 if (info) {
36 commonAsAGradient(info);
37 info->fPoint[0] = fCenter;
38 }
39 return kSweep_GradientType;
40}
41
42GrCustomStage* SkSweepGradient::asNewCustomStage(GrContext* context,
43 GrSamplerState* sampler) const {
44 sampler->matrix()->preConcat(fPtsToUnit);
45 sampler->textureParams()->setTileModeX(fTileMode);
46 sampler->textureParams()->setTileModeY(kClamp_TileMode);
47 sampler->textureParams()->setBilerp(true);
48 return SkNEW_ARGS(GrSweepGradient, (context, *this, sampler));
49}
50
51SkSweepGradient::SkSweepGradient(SkFlattenableReadBuffer& buffer)
52 : INHERITED(buffer),
53 fCenter(buffer.readPoint()) {
54}
55
56void SkSweepGradient::flatten(SkFlattenableWriteBuffer& buffer) const {
57 this->INHERITED::flatten(buffer);
58 buffer.writePoint(fCenter);
59}
60
61#ifndef SK_SCALAR_IS_FLOAT
62#ifdef COMPUTE_SWEEP_TABLE
63#define PI 3.14159265
64static bool gSweepTableReady;
65static uint8_t gSweepTable[65];
66
67/* Our table stores precomputed values for atan: [0...1] -> [0..PI/4]
68 We scale the results to [0..32]
69*/
70static const uint8_t* build_sweep_table() {
71 if (!gSweepTableReady) {
72 const int N = 65;
73 const double DENOM = N - 1;
74
75 for (int i = 0; i < N; i++)
76 {
77 double arg = i / DENOM;
78 double v = atan(arg);
79 int iv = (int)round(v * DENOM * 2 / PI);
80// printf("[%d] atan(%g) = %g %d\n", i, arg, v, iv);
81 printf("%d, ", iv);
82 gSweepTable[i] = iv;
83 }
84 gSweepTableReady = true;
85 }
86 return gSweepTable;
87}
88#else
89static const uint8_t gSweepTable[] = {
90 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9,
91 10, 11, 11, 12, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18,
92 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26,
93 26, 27, 27, 27, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32,
94 32
95};
96static const uint8_t* build_sweep_table() { return gSweepTable; }
97#endif
98#endif
99
100// divide numer/denom, with a bias of 6bits. Assumes numer <= denom
101// and denom != 0. Since our table is 6bits big (+1), this is a nice fit.
102// Same as (but faster than) SkFixedDiv(numer, denom) >> 10
103
104//unsigned div_64(int numer, int denom);
105#ifndef SK_SCALAR_IS_FLOAT
106static unsigned div_64(int numer, int denom) {
107 SkASSERT(numer <= denom);
108 SkASSERT(numer > 0);
109 SkASSERT(denom > 0);
110
111 int nbits = SkCLZ(numer);
112 int dbits = SkCLZ(denom);
113 int bits = 6 - nbits + dbits;
114 SkASSERT(bits <= 6);
115
116 if (bits < 0) { // detect underflow
117 return 0;
118 }
119
120 denom <<= dbits - 1;
121 numer <<= nbits - 1;
122
123 unsigned result = 0;
124
125 // do the first one
126 if ((numer -= denom) >= 0) {
127 result = 1;
128 } else {
129 numer += denom;
130 }
131
132 // Now fall into our switch statement if there are more bits to compute
133 if (bits > 0) {
134 // make room for the rest of the answer bits
135 result <<= bits;
136 switch (bits) {
137 case 6:
138 if ((numer = (numer << 1) - denom) >= 0)
139 result |= 32;
140 else
141 numer += denom;
142 case 5:
143 if ((numer = (numer << 1) - denom) >= 0)
144 result |= 16;
145 else
146 numer += denom;
147 case 4:
148 if ((numer = (numer << 1) - denom) >= 0)
149 result |= 8;
150 else
151 numer += denom;
152 case 3:
153 if ((numer = (numer << 1) - denom) >= 0)
154 result |= 4;
155 else
156 numer += denom;
157 case 2:
158 if ((numer = (numer << 1) - denom) >= 0)
159 result |= 2;
160 else
161 numer += denom;
162 case 1:
163 default: // not strictly need, but makes GCC make better ARM code
164 if ((numer = (numer << 1) - denom) >= 0)
165 result |= 1;
166 else
167 numer += denom;
168 }
169 }
170 return result;
171}
172#endif
173
174// Given x,y in the first quadrant, return 0..63 for the angle [0..90]
175#ifndef SK_SCALAR_IS_FLOAT
176static unsigned atan_0_90(SkFixed y, SkFixed x) {
177#ifdef SK_DEBUG
178 {
179 static bool gOnce;
180 if (!gOnce) {
181 gOnce = true;
182 SkASSERT(div_64(55, 55) == 64);
183 SkASSERT(div_64(128, 256) == 32);
184 SkASSERT(div_64(2326528, 4685824) == 31);
185 SkASSERT(div_64(753664, 5210112) == 9);
186 SkASSERT(div_64(229376, 4882432) == 3);
187 SkASSERT(div_64(2, 64) == 2);
188 SkASSERT(div_64(1, 64) == 1);
189 // test that we handle underflow correctly
190 SkASSERT(div_64(12345, 0x54321234) == 0);
191 }
192 }
193#endif
194
195 SkASSERT(y > 0 && x > 0);
196 const uint8_t* table = build_sweep_table();
197
198 unsigned result;
199 bool swap = (x < y);
200 if (swap) {
201 // first part of the atan(v) = PI/2 - atan(1/v) identity
202 // since our div_64 and table want v <= 1, where v = y/x
203 SkTSwap<SkFixed>(x, y);
204 }
205
206 result = div_64(y, x);
207
208#ifdef SK_DEBUG
209 {
210 unsigned result2 = SkDivBits(y, x, 6);
211 SkASSERT(result2 == result ||
212 (result == 1 && result2 == 0));
213 }
214#endif
215
216 SkASSERT(result < SK_ARRAY_COUNT(gSweepTable));
217 result = table[result];
218
219 if (swap) {
220 // complete the atan(v) = PI/2 - atan(1/v) identity
221 result = 64 - result;
222 // pin to 63
223 result -= result >> 6;
224 }
225
226 SkASSERT(result <= 63);
227 return result;
228}
229#endif
230
231// returns angle in a circle [0..2PI) -> [0..255]
232#ifdef SK_SCALAR_IS_FLOAT
233static unsigned SkATan2_255(float y, float x) {
234 // static const float g255Over2PI = 255 / (2 * SK_ScalarPI);
235 static const float g255Over2PI = 40.584510488433314f;
236
237 float result = sk_float_atan2(y, x);
238 if (result < 0) {
239 result += 2 * SK_ScalarPI;
240 }
241 SkASSERT(result >= 0);
242 // since our value is always >= 0, we can cast to int, which is faster than
243 // calling floorf()
244 int ir = (int)(result * g255Over2PI);
245 SkASSERT(ir >= 0 && ir <= 255);
246 return ir;
247}
248#else
249static unsigned SkATan2_255(SkFixed y, SkFixed x) {
250 if (x == 0) {
251 if (y == 0) {
252 return 0;
253 }
254 return y < 0 ? 192 : 64;
255 }
256 if (y == 0) {
257 return x < 0 ? 128 : 0;
258 }
259
260 /* Find the right quadrant for x,y
261 Since atan_0_90 only handles the first quadrant, we rotate x,y
262 appropriately before calling it, and then add the right amount
263 to account for the real quadrant.
264 quadrant 0 : add 0 | x > 0 && y > 0
265 quadrant 1 : add 64 (90 degrees) | x < 0 && y > 0
266 quadrant 2 : add 128 (180 degrees) | x < 0 && y < 0
267 quadrant 3 : add 192 (270 degrees) | x > 0 && y < 0
268
269 map x<0 to (1 << 6)
270 map y<0 to (3 << 6)
271 add = map_x ^ map_y
272 */
273 int xsign = x >> 31;
274 int ysign = y >> 31;
275 int add = ((-xsign) ^ (ysign & 3)) << 6;
276
277#ifdef SK_DEBUG
278 if (0 == add)
279 SkASSERT(x > 0 && y > 0);
280 else if (64 == add)
281 SkASSERT(x < 0 && y > 0);
282 else if (128 == add)
283 SkASSERT(x < 0 && y < 0);
284 else if (192 == add)
285 SkASSERT(x > 0 && y < 0);
286 else
287 SkDEBUGFAIL("bad value for add");
288#endif
289
290 /* This ^ trick makes x, y positive, and the swap<> handles quadrants
291 where we need to rotate x,y by 90 or -90
292 */
293 x = (x ^ xsign) - xsign;
294 y = (y ^ ysign) - ysign;
295 if (add & 64) { // quads 1 or 3 need to swap x,y
296 SkTSwap<SkFixed>(x, y);
297 }
298
299 unsigned result = add + atan_0_90(y, x);
300 SkASSERT(result < 256);
301 return result;
302}
303#endif
304
305void SkSweepGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
306 int count) {
307 SkMatrix::MapXYProc proc = fDstToIndexProc;
308 const SkMatrix& matrix = fDstToIndex;
309 const SkPMColor* SK_RESTRICT cache = this->getCache32();
310 SkPoint srcPt;
311
312 if (fDstToIndexClass != kPerspective_MatrixClass) {
313 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
314 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
315 SkScalar dx, fx = srcPt.fX;
316 SkScalar dy, fy = srcPt.fY;
317
318 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
319 SkFixed storage[2];
320 (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf,
321 &storage[0], &storage[1]);
322 dx = SkFixedToScalar(storage[0]);
323 dy = SkFixedToScalar(storage[1]);
324 } else {
325 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
326 dx = matrix.getScaleX();
327 dy = matrix.getSkewY();
328 }
329
330 for (; count > 0; --count) {
331 *dstC++ = cache[SkATan2_255(fy, fx)];
332 fx += dx;
333 fy += dy;
334 }
335 } else { // perspective case
336 for (int stop = x + count; x < stop; x++) {
337 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
338 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
339 *dstC++ = cache[SkATan2_255(srcPt.fY, srcPt.fX)];
340 }
341 }
342}
343
344void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC,
345 int count) {
346 SkMatrix::MapXYProc proc = fDstToIndexProc;
347 const SkMatrix& matrix = fDstToIndex;
348 const uint16_t* SK_RESTRICT cache = this->getCache16();
349 int toggle = ((x ^ y) & 1) * kDitherStride16;
350 SkPoint srcPt;
351
352 if (fDstToIndexClass != kPerspective_MatrixClass) {
353 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
354 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
355 SkScalar dx, fx = srcPt.fX;
356 SkScalar dy, fy = srcPt.fY;
357
358 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
359 SkFixed storage[2];
360 (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf,
361 &storage[0], &storage[1]);
362 dx = SkFixedToScalar(storage[0]);
363 dy = SkFixedToScalar(storage[1]);
364 } else {
365 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
366 dx = matrix.getScaleX();
367 dy = matrix.getSkewY();
368 }
369
370 for (; count > 0; --count) {
371 int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits);
372 *dstC++ = cache[toggle + index];
373 toggle ^= kDitherStride16;
374 fx += dx;
375 fy += dy;
376 }
377 } else { // perspective case
378 for (int stop = x + count; x < stop; x++) {
379 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
380 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
381
382 int index = SkATan2_255(srcPt.fY, srcPt.fX);
383 index >>= (8 - kCache16Bits);
384 *dstC++ = cache[toggle + index];
385 toggle ^= kDitherStride16;
386 }
387 }
388}
389
rileya@google.comd7cc6512012-07-27 14:00:39 +0000390/////////////////////////////////////////////////////////////////////
391
392class GrGLSweepGradient : public GrGLGradientStage {
393
394public:
395
396 GrGLSweepGradient(const GrProgramStageFactory& factory,
397 const GrCustomStage&) : INHERITED (factory) { }
398 virtual ~GrGLSweepGradient() { }
399
400 virtual void emitVS(GrGLShaderBuilder* builder,
401 const char* vertexCoords) SK_OVERRIDE { }
402 virtual void emitFS(GrGLShaderBuilder* builder,
403 const char* outputColor,
404 const char* inputColor,
405 const char* samplerName) SK_OVERRIDE;
406
407 static StageKey GenKey(const GrCustomStage& s) { return 0; }
408
409private:
410
411 typedef GrGLGradientStage INHERITED;
412
413};
414
415void GrGLSweepGradient::emitFS(GrGLShaderBuilder* builder,
416 const char* outputColor,
417 const char* inputColor,
418 const char* samplerName) {
419 SkString t;
420 t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5",
421 builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str());
422 this->emitColorLookup(builder, t.c_str(), outputColor, samplerName);
423}
424
425/////////////////////////////////////////////////////////////////////
426
427GrSweepGradient::GrSweepGradient(GrTexture* texture)
428 : INHERITED(texture) {
429
430}
431
432GrSweepGradient::GrSweepGradient(GrContext* ctx, const SkShader& shader,
433 GrSamplerState* sampler)
434 : INHERITED(ctx, shader, sampler) {
435}
436
437GrSweepGradient::~GrSweepGradient() {
438
439}
440
441const GrProgramStageFactory& GrSweepGradient::getFactory() const {
442 return GrTProgramStageFactory<GrSweepGradient>::getInstance();
443}
444