blob: f1acd551cc1578769bb350ac05a434cff448dcfc [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 "SkTwoPointConicalGradient.h"
10
11static int valid_divide(float numer, float denom, float* ratio) {
12 SkASSERT(ratio);
13 if (0 == denom) {
14 return 0;
15 }
16 *ratio = numer / denom;
17 return 1;
18}
19
20// Return the number of distinct real roots, and write them into roots[] in
21// ascending order
22static int find_quad_roots(float A, float B, float C, float roots[2]) {
23 SkASSERT(roots);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000024
rileya@google.com589708b2012-07-26 20:04:23 +000025 if (A == 0) {
26 return valid_divide(-C, B, roots);
27 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028
rileya@google.com589708b2012-07-26 20:04:23 +000029 float R = B*B - 4*A*C;
30 if (R < 0) {
31 return 0;
32 }
33 R = sk_float_sqrt(R);
34
35#if 1
36 float Q = B;
37 if (Q < 0) {
38 Q -= R;
39 } else {
40 Q += R;
41 }
42#else
43 // on 10.6 this was much slower than the above branch :(
44 float Q = B + copysignf(R, B);
45#endif
46 Q *= -0.5f;
47 if (0 == Q) {
48 roots[0] = 0;
49 return 1;
50 }
51
52 float r0 = Q / A;
53 float r1 = C / Q;
54 roots[0] = r0 < r1 ? r0 : r1;
55 roots[1] = r0 > r1 ? r0 : r1;
56 return 2;
57}
58
59static float lerp(float x, float dx, float t) {
60 return x + t * dx;
61}
62
63static float sqr(float x) { return x * x; }
64
65void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0,
66 const SkPoint& center1, SkScalar rad1) {
67 fCenterX = SkScalarToFloat(center0.fX);
68 fCenterY = SkScalarToFloat(center0.fY);
69 fDCenterX = SkScalarToFloat(center1.fX) - fCenterX;
70 fDCenterY = SkScalarToFloat(center1.fY) - fCenterY;
71 fRadius = SkScalarToFloat(rad0);
72 fDRadius = SkScalarToFloat(rad1) - fRadius;
73
74 fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius);
75 fRadius2 = sqr(fRadius);
76 fRDR = fRadius * fDRadius;
77}
78
79void TwoPtRadial::setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy) {
80 fRelX = SkScalarToFloat(fx) - fCenterX;
81 fRelY = SkScalarToFloat(fy) - fCenterY;
82 fIncX = SkScalarToFloat(dfx);
83 fIncY = SkScalarToFloat(dfy);
84 fB = -2 * (fDCenterX * fRelX + fDCenterY * fRelY + fRDR);
85 fDB = -2 * (fDCenterX * fIncX + fDCenterY * fIncY);
86}
87
88SkFixed TwoPtRadial::nextT() {
89 float roots[2];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
rileya@google.com589708b2012-07-26 20:04:23 +000091 float C = sqr(fRelX) + sqr(fRelY) - fRadius2;
92 int countRoots = find_quad_roots(fA, fB, C, roots);
93
94 fRelX += fIncX;
95 fRelY += fIncY;
96 fB += fDB;
97
98 if (0 == countRoots) {
99 return kDontDrawT;
100 }
101
102 // Prefer the bigger t value if both give a radius(t) > 0
103 // find_quad_roots returns the values sorted, so we start with the last
104 float t = roots[countRoots - 1];
105 float r = lerp(fRadius, fDRadius, t);
106 if (r <= 0) {
107 t = roots[0]; // might be the same as roots[countRoots-1]
108 r = lerp(fRadius, fDRadius, t);
109 if (r <= 0) {
110 return kDontDrawT;
111 }
112 }
113 return SkFloatToFixed(t);
114}
115
reed@google.com60040292013-02-04 18:21:23 +0000116typedef void (*TwoPointConicalProc)(TwoPtRadial* rec, SkPMColor* dstC,
117 const SkPMColor* cache, int toggle, int count);
rileya@google.com589708b2012-07-26 20:04:23 +0000118
119static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000120 const SkPMColor* SK_RESTRICT cache, int toggle,
121 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000122 for (; count > 0; --count) {
123 SkFixed t = rec->nextT();
124 if (TwoPtRadial::DontDrawT(t)) {
125 *dstC++ = 0;
126 } else {
127 SkFixed index = SkClampMax(t, 0xFFFF);
128 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000129 *dstC++ = cache[toggle +
130 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000131 }
reed@google.com60040292013-02-04 18:21:23 +0000132#ifndef SK_IGNORE_GRADIENT_DITHER_FIX
133 toggle = next_dither_toggle(toggle);
134#endif
rileya@google.com589708b2012-07-26 20:04:23 +0000135 }
136}
137
138static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000139 const SkPMColor* SK_RESTRICT cache, int toggle,
140 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000141 for (; count > 0; --count) {
142 SkFixed t = rec->nextT();
143 if (TwoPtRadial::DontDrawT(t)) {
144 *dstC++ = 0;
145 } else {
146 SkFixed index = repeat_tileproc(t);
147 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000148 *dstC++ = cache[toggle +
149 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000150 }
reed@google.com60040292013-02-04 18:21:23 +0000151#ifndef SK_IGNORE_GRADIENT_DITHER_FIX
152 toggle = next_dither_toggle(toggle);
153#endif
rileya@google.com589708b2012-07-26 20:04:23 +0000154 }
155}
156
157static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000158 const SkPMColor* SK_RESTRICT cache, int toggle,
159 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000160 for (; count > 0; --count) {
161 SkFixed t = rec->nextT();
162 if (TwoPtRadial::DontDrawT(t)) {
163 *dstC++ = 0;
164 } else {
165 SkFixed index = mirror_tileproc(t);
166 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000167 *dstC++ = cache[toggle +
168 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000169 }
reed@google.com60040292013-02-04 18:21:23 +0000170#ifndef SK_IGNORE_GRADIENT_DITHER_FIX
171 toggle = next_dither_toggle(toggle);
172#endif
rileya@google.com589708b2012-07-26 20:04:23 +0000173 }
174}
175
176void SkTwoPointConicalGradient::init() {
177 fRec.init(fCenter1, fRadius1, fCenter2, fRadius2);
178 fPtsToUnit.reset();
179}
180
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000181/////////////////////////////////////////////////////////////////////
182
rileya@google.com589708b2012-07-26 20:04:23 +0000183SkTwoPointConicalGradient::SkTwoPointConicalGradient(
184 const SkPoint& start, SkScalar startRadius,
185 const SkPoint& end, SkScalar endRadius,
186 const SkColor colors[], const SkScalar pos[],
187 int colorCount, SkShader::TileMode mode,
188 SkUnitMapper* mapper)
189 : SkGradientShaderBase(colors, pos, colorCount, mode, mapper),
190 fCenter1(start),
191 fCenter2(end),
192 fRadius1(startRadius),
193 fRadius2(endRadius) {
194 // this is degenerate, and should be caught by our caller
195 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
196 this->init();
197}
198
199void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam,
200 int count) {
reed@google.com60040292013-02-04 18:21:23 +0000201#ifndef SK_IGNORE_GRADIENT_DITHER_FIX
202 int toggle = init_dither_toggle(x, y);
203#else
204 int toggle = 0;
205#endif
206
rileya@google.com589708b2012-07-26 20:04:23 +0000207 SkASSERT(count > 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208
rileya@google.com589708b2012-07-26 20:04:23 +0000209 SkPMColor* SK_RESTRICT dstC = dstCParam;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210
rileya@google.com589708b2012-07-26 20:04:23 +0000211 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
bsalomon@google.com100abf42012-09-05 17:40:04 +0000212
rileya@google.com589708b2012-07-26 20:04:23 +0000213 const SkPMColor* SK_RESTRICT cache = this->getCache32();
214
reed@google.com60040292013-02-04 18:21:23 +0000215 TwoPointConicalProc shadeProc = twopoint_repeat;
rileya@google.com589708b2012-07-26 20:04:23 +0000216 if (SkShader::kClamp_TileMode == fTileMode) {
217 shadeProc = twopoint_clamp;
218 } else if (SkShader::kMirror_TileMode == fTileMode) {
219 shadeProc = twopoint_mirror;
220 } else {
221 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
222 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000223
rileya@google.com589708b2012-07-26 20:04:23 +0000224 if (fDstToIndexClass != kPerspective_MatrixClass) {
225 SkPoint srcPt;
226 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
227 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
228 SkScalar dx, fx = srcPt.fX;
229 SkScalar dy, fy = srcPt.fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000230
rileya@google.com589708b2012-07-26 20:04:23 +0000231 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
232 SkFixed fixedX, fixedY;
233 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
234 dx = SkFixedToScalar(fixedX);
235 dy = SkFixedToScalar(fixedY);
236 } else {
237 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
238 dx = fDstToIndex.getScaleX();
239 dy = fDstToIndex.getSkewY();
240 }
241
242 fRec.setup(fx, fy, dx, dy);
reed@google.com60040292013-02-04 18:21:23 +0000243 (*shadeProc)(&fRec, dstC, cache, toggle, count);
rileya@google.com589708b2012-07-26 20:04:23 +0000244 } else { // perspective case
245 SkScalar dstX = SkIntToScalar(x);
246 SkScalar dstY = SkIntToScalar(y);
247 for (; count > 0; --count) {
248 SkPoint srcPt;
249 dstProc(fDstToIndex, dstX, dstY, &srcPt);
250 dstX += SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000251
rileya@google.com589708b2012-07-26 20:04:23 +0000252 fRec.setup(srcPt.fX, srcPt.fY, 0, 0);
reed@google.com60040292013-02-04 18:21:23 +0000253 (*shadeProc)(&fRec, dstC, cache, toggle, 1);
254#ifndef SK_IGNORE_GRADIENT_DITHER_FIX
255 toggle = next_dither_toggle(toggle);
256#endif
rileya@google.com589708b2012-07-26 20:04:23 +0000257 }
258 }
259}
260
261bool SkTwoPointConicalGradient::setContext(const SkBitmap& device,
262 const SkPaint& paint,
263 const SkMatrix& matrix) {
264 if (!this->INHERITED::setContext(device, paint, matrix)) {
265 return false;
266 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000267
rileya@google.com589708b2012-07-26 20:04:23 +0000268 // we don't have a span16 proc
269 fFlags &= ~kHasSpan16_Flag;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000270
rileya@google.com589708b2012-07-26 20:04:23 +0000271 // in general, we might discard based on computed-radius, so clear
272 // this flag (todo: sometimes we can detect that we never discard...)
273 fFlags &= ~kOpaqueAlpha_Flag;
274
275 return true;
276}
277
278SkShader::BitmapType SkTwoPointConicalGradient::asABitmap(
279 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const {
280 SkPoint diff = fCenter2 - fCenter1;
rileya@google.com589708b2012-07-26 20:04:23 +0000281 SkScalar diffLen = 0;
282
283 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000284 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000285 }
286 if (matrix) {
287 diffLen = diff.length();
288 }
289 if (matrix) {
290 if (diffLen) {
291 SkScalar invDiffLen = SkScalarInvert(diffLen);
292 // rotate to align circle centers with the x-axis
293 matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
294 SkScalarMul(invDiffLen, diff.fX));
295 } else {
296 matrix->reset();
297 }
298 matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
299 }
300 if (xy) {
301 xy[0] = fTileMode;
302 xy[1] = kClamp_TileMode;
303 }
304 return kTwoPointConical_BitmapType;
305}
306
307SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
308 GradientInfo* info) const {
309 if (info) {
310 commonAsAGradient(info);
311 info->fPoint[0] = fCenter1;
312 info->fPoint[1] = fCenter2;
313 info->fRadius[0] = fRadius1;
314 info->fRadius[1] = fRadius2;
315 }
316 return kConical_GradientType;
317}
318
rileya@google.com589708b2012-07-26 20:04:23 +0000319SkTwoPointConicalGradient::SkTwoPointConicalGradient(
320 SkFlattenableReadBuffer& buffer)
321 : INHERITED(buffer),
322 fCenter1(buffer.readPoint()),
323 fCenter2(buffer.readPoint()),
324 fRadius1(buffer.readScalar()),
325 fRadius2(buffer.readScalar()) {
326 this->init();
327};
328
329void SkTwoPointConicalGradient::flatten(
330 SkFlattenableWriteBuffer& buffer) const {
331 this->INHERITED::flatten(buffer);
332 buffer.writePoint(fCenter1);
333 buffer.writePoint(fCenter2);
334 buffer.writeScalar(fRadius1);
335 buffer.writeScalar(fRadius2);
336}
337
rileya@google.comd7cc6512012-07-27 14:00:39 +0000338/////////////////////////////////////////////////////////////////////
339
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000340#if SK_SUPPORT_GPU
341
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000342#include "GrTBackendEffectFactory.h"
343
rileya@google.comd7cc6512012-07-27 14:00:39 +0000344// For brevity
345typedef GrGLUniformManager::UniformHandle UniformHandle;
346static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
347
bsalomon@google.com0707c292012-10-25 21:45:42 +0000348class GrGLConical2Gradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000349public:
350
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000351 GrGLConical2Gradient(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000352 const GrEffectRef&);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000353 virtual ~GrGLConical2Gradient() { }
354
bsalomon@google.comf78df332012-10-29 12:43:38 +0000355 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000356 const GrEffectStage&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000357 EffectKey,
358 const char* vertexCoords,
359 const char* outputColor,
360 const char* inputColor,
361 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000362 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000363
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000364 static EffectKey GenKey(const GrEffectStage&, const GrGLCaps& caps);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000365
366protected:
367
368 UniformHandle fVSParamUni;
369 UniformHandle fFSParamUni;
370
371 const char* fVSVaryingName;
372 const char* fFSVaryingName;
373
374 bool fIsDegenerate;
375
376 // @{
377 /// Values last uploaded as uniforms
378
bsalomon@google.com81712882012-11-01 17:12:34 +0000379 SkScalar fCachedCenter;
380 SkScalar fCachedRadius;
381 SkScalar fCachedDiffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000382
383 // @}
384
385private:
386
bsalomon@google.com0707c292012-10-25 21:45:42 +0000387 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000388
389};
390
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000391/////////////////////////////////////////////////////////////////////
392
393class GrConical2Gradient : public GrGradientEffect {
394public:
395
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000396 static GrEffectRef* Create(GrContext* ctx,
397 const SkTwoPointConicalGradient& shader,
398 const SkMatrix& matrix,
399 SkShader::TileMode tm) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000400 AutoEffectUnref effect(SkNEW_ARGS(GrConical2Gradient, (ctx, shader, matrix, tm)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000401 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000402 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000403
404 virtual ~GrConical2Gradient() { }
405
406 static const char* Name() { return "Two-Point Conical Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000407 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
408 return GrTBackendEffectFactory<GrConical2Gradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000409 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000410
411 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
412 bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000413 SkScalar center() const { return fCenterX1; }
414 SkScalar diffRadius() const { return fDiffRadius; }
415 SkScalar radius() const { return fRadius0; }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000416
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000417 typedef GrGLConical2Gradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000418
419private:
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000420 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000421 const GrConical2Gradient& s = CastEffect<GrConical2Gradient>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000422 return (INHERITED::onIsEqual(sBase) &&
423 this->fCenterX1 == s.fCenterX1 &&
424 this->fRadius0 == s.fRadius0 &&
425 this->fDiffRadius == s.fDiffRadius);
426 }
427
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000428 GrConical2Gradient(GrContext* ctx,
429 const SkTwoPointConicalGradient& shader,
430 const SkMatrix& matrix,
431 SkShader::TileMode tm)
432 : INHERITED(ctx, shader, matrix, tm)
433 , fCenterX1(shader.getCenterX1())
434 , fRadius0(shader.getStartRadius())
435 , fDiffRadius(shader.getDiffRadius()) { }
436
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000437 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000438
439 // @{
440 // Cache of values - these can change arbitrarily, EXCEPT
441 // we shouldn't change between degenerate and non-degenerate?!
442
bsalomon@google.com81712882012-11-01 17:12:34 +0000443 SkScalar fCenterX1;
444 SkScalar fRadius0;
445 SkScalar fDiffRadius;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000446
447 // @}
448
449 typedef GrGradientEffect INHERITED;
450};
451
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000452GR_DEFINE_EFFECT_TEST(GrConical2Gradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000453
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000454GrEffectRef* GrConical2Gradient::TestCreate(SkRandom* random,
455 GrContext* context,
456 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000457 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
458 SkScalar radius1 = random->nextUScalar1();
459 SkPoint center2;
460 SkScalar radius2;
461 do {
bsalomon@google.comfb883bf2012-12-11 15:32:04 +0000462 center2.set(random->nextUScalar1(), random->nextUScalar1());
bsalomon@google.comd4726202012-08-03 14:34:46 +0000463 radius2 = random->nextUScalar1 ();
464 // If the circles are identical the factory will give us an empty shader.
465 } while (radius1 == radius2 && center1 == center2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000466
bsalomon@google.comd4726202012-08-03 14:34:46 +0000467 SkColor colors[kMaxRandomGradientColors];
468 SkScalar stopsArray[kMaxRandomGradientColors];
469 SkScalar* stops = stopsArray;
470 SkShader::TileMode tm;
471 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
472 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
473 center2, radius2,
474 colors, stops, colorCount,
475 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000476 SkPaint paint;
477 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000478}
479
480
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000481/////////////////////////////////////////////////////////////////////
482
bsalomon@google.com6340a412013-01-22 19:55:59 +0000483GrGLConical2Gradient::GrGLConical2Gradient(const GrBackendEffectFactory& factory,
484 const GrEffectRef& baseData)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000485 : INHERITED(factory)
486 , fVSParamUni(kInvalidUniformHandle)
487 , fFSParamUni(kInvalidUniformHandle)
488 , fVSVaryingName(NULL)
489 , fFSVaryingName(NULL)
bsalomon@google.com81712882012-11-01 17:12:34 +0000490 , fCachedCenter(SK_ScalarMax)
491 , fCachedRadius(-SK_ScalarMax)
492 , fCachedDiffRadius(-SK_ScalarMax) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000493
bsalomon@google.com6340a412013-01-22 19:55:59 +0000494 const GrConical2Gradient& data = CastEffect<GrConical2Gradient>(baseData);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000495 fIsDegenerate = data.isDegenerate();
496}
497
bsalomon@google.comf78df332012-10-29 12:43:38 +0000498void GrGLConical2Gradient::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000499 const GrEffectStage& stage,
500 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000501 const char* vertexCoords,
502 const char* outputColor,
503 const char* inputColor,
504 const TextureSamplerArray& samplers) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000505 const char* fsCoords;
506 const char* vsCoordsVarying;
507 GrSLType coordsVaryingType;
508 this->setupMatrix(builder, key, vertexCoords, &fsCoords, &vsCoordsVarying, &coordsVaryingType);
509
bsalomon@google.comf78df332012-10-29 12:43:38 +0000510 this->emitYCoordUniform(builder);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000511 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
512 // to work around Xoom bug. Doesn't seem to cause performance decrease
513 // in test apps, but need to keep an eye on it.
514 fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
515 kFloat_GrSLType, "Conical2VSParams", 6);
516 fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
517 kFloat_GrSLType, "Conical2FSParams", 6);
518
519 // For radial gradients without perspective we can pass the linear
520 // part of the quadratic as a varying.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000521 if (kVec2f_GrSLType == coordsVaryingType) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000522 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
523 &fVSVaryingName, &fFSVaryingName);
524 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000525
bsalomon@google.comf78df332012-10-29 12:43:38 +0000526 // VS
527 {
528 SkString* code = &builder->fVSCode;
529 SkString p2; // distance between centers
530 SkString p3; // start radius
531 SkString p5; // difference in radii (r1 - r0)
532 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
533 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
534 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000535
bsalomon@google.comf78df332012-10-29 12:43:38 +0000536 // For radial gradients without perspective we can pass the linear
537 // part of the quadratic as a varying.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000538 if (kVec2f_GrSLType == coordsVaryingType) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000539 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
540 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
541 fVSVaryingName, p2.c_str(),
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000542 vsCoordsVarying, p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000543 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000544 }
545
bsalomon@google.comf78df332012-10-29 12:43:38 +0000546 // FS
547 {
548 SkString* code = &builder->fFSCode;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000549
bsalomon@google.comf78df332012-10-29 12:43:38 +0000550 SkString cName("c");
551 SkString ac4Name("ac4");
552 SkString dName("d");
553 SkString qName("q");
554 SkString r0Name("r0");
555 SkString r1Name("r1");
556 SkString tName("t");
557 SkString p0; // 4a
558 SkString p1; // 1/a
559 SkString p2; // distance between centers
560 SkString p3; // start radius
561 SkString p4; // start radius squared
562 SkString p5; // difference in radii (r1 - r0)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000563
bsalomon@google.comf78df332012-10-29 12:43:38 +0000564 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
565 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
566 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
567 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
568 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
569 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000570
bsalomon@google.comf78df332012-10-29 12:43:38 +0000571 // If we we're able to interpolate the linear component,
572 // bVar is the varying; otherwise compute it
573 SkString bVar;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000574 if (kVec2f_GrSLType == coordsVaryingType) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000575 bVar = fFSVaryingName;
576 } else {
577 bVar = "b";
578 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000579 bVar.c_str(), p2.c_str(), fsCoords,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000580 p3.c_str(), p5.c_str());
581 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000582
bsalomon@google.comf78df332012-10-29 12:43:38 +0000583 // output will default to transparent black (we simply won't write anything
584 // else to it if invalid, instead of discarding or returning prematurely)
585 code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000586
bsalomon@google.comf78df332012-10-29 12:43:38 +0000587 // c = (x^2)+(y^2) - params[4]
588 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000589 fsCoords, fsCoords,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000590 p4.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000591
bsalomon@google.comf78df332012-10-29 12:43:38 +0000592 // Non-degenerate case (quadratic)
593 if (!fIsDegenerate) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000594
bsalomon@google.comf78df332012-10-29 12:43:38 +0000595 // ac4 = params[0] * c
596 code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
597 cName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000598
bsalomon@google.comf78df332012-10-29 12:43:38 +0000599 // d = b^2 - ac4
600 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
601 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000602
bsalomon@google.comf78df332012-10-29 12:43:38 +0000603 // only proceed if discriminant is >= 0
604 code->appendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000605
bsalomon@google.comf78df332012-10-29 12:43:38 +0000606 // intermediate value we'll use to compute the roots
607 // q = -0.5 * (b +/- sqrt(d))
608 code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
609 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
610 bVar.c_str(), dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000611
bsalomon@google.comf78df332012-10-29 12:43:38 +0000612 // compute both roots
613 // r0 = q * params[1]
614 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
615 qName.c_str(), p1.c_str());
616 // r1 = c / q
617 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
618 cName.c_str(), qName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000619
bsalomon@google.comf78df332012-10-29 12:43:38 +0000620 // Note: If there are two roots that both generate radius(t) > 0, the
621 // Canvas spec says to choose the larger t.
rileya@google.comd7cc6512012-07-27 14:00:39 +0000622
bsalomon@google.comf78df332012-10-29 12:43:38 +0000623 // so we'll look at the larger one first:
624 code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
625 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000626
bsalomon@google.comf78df332012-10-29 12:43:38 +0000627 // if r(t) > 0, then we're done; t will be our x coordinate
628 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
629 p5.c_str(), p3.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000630
bsalomon@google.comf78df332012-10-29 12:43:38 +0000631 code->appendf("\t\t");
632 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000633
bsalomon@google.comf78df332012-10-29 12:43:38 +0000634 // otherwise, if r(t) for the larger root was <= 0, try the other root
635 code->appendf("\t\t} else {\n");
636 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
637 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000638
bsalomon@google.comf78df332012-10-29 12:43:38 +0000639 // if r(t) > 0 for the smaller root, then t will be our x coordinate
640 code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
641 tName.c_str(), p5.c_str(), p3.c_str());
642
643 code->appendf("\t\t\t");
644 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
645
646 // end if (r(t) > 0) for smaller root
647 code->appendf("\t\t\t}\n");
648 // end if (r(t) > 0), else, for larger root
649 code->appendf("\t\t}\n");
650 // end if (discriminant >= 0)
651 code->appendf("\t}\n");
652 } else {
653
654 // linear case: t = -c/b
655 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
656 cName.c_str(), bVar.c_str());
657
658 // if r(t) > 0, then t will be the x coordinate
659 code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
660 p5.c_str(), p3.c_str());
661 code->appendf("\t");
662 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
663 code->appendf("\t}\n");
664 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000665 }
666}
667
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000668void GrGLConical2Gradient::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
669 INHERITED::setData(uman, stage);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000670 const GrConical2Gradient& data = GetEffectFromStage<GrConical2Gradient>(stage);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000671 GrAssert(data.isDegenerate() == fIsDegenerate);
bsalomon@google.com81712882012-11-01 17:12:34 +0000672 SkScalar centerX1 = data.center();
673 SkScalar radius0 = data.radius();
674 SkScalar diffRadius = data.diffRadius();
rileya@google.comd7cc6512012-07-27 14:00:39 +0000675
676 if (fCachedCenter != centerX1 ||
677 fCachedRadius != radius0 ||
678 fCachedDiffRadius != diffRadius) {
679
bsalomon@google.com81712882012-11-01 17:12:34 +0000680 SkScalar a = SkScalarMul(centerX1, centerX1) - diffRadius * diffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000681
682 // When we're in the degenerate (linear) case, the second
683 // value will be INF but the program doesn't read it. (We
684 // use the same 6 uniforms even though we don't need them
685 // all in the linear case just to keep the code complexity
686 // down).
687 float values[6] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000688 SkScalarToFloat(a * 4),
689 1.f / (SkScalarToFloat(a)),
690 SkScalarToFloat(centerX1),
691 SkScalarToFloat(radius0),
692 SkScalarToFloat(SkScalarMul(radius0, radius0)),
693 SkScalarToFloat(diffRadius)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000694 };
695
696 uman.set1fv(fVSParamUni, 0, 6, values);
697 uman.set1fv(fFSParamUni, 0, 6, values);
698 fCachedCenter = centerX1;
699 fCachedRadius = radius0;
700 fCachedDiffRadius = diffRadius;
701 }
702}
703
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000704GrGLEffect::EffectKey GrGLConical2Gradient::GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000705 enum {
706 kIsDegenerate = 1 << kMatrixKeyBitCnt,
707 };
708
709 EffectKey key = GenMatrixKey(s);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000710 if (GetEffectFromStage<GrConical2Gradient>(s).isDegenerate()) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000711 key |= kIsDegenerate;
712 }
713 return key;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000714}
rileya@google.comd7cc6512012-07-27 14:00:39 +0000715
716/////////////////////////////////////////////////////////////////////
717
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000718GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&) const {
bsalomon@google.com00835cc2013-01-14 17:07:22 +0000719 SkASSERT(NULL != context);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000720 SkASSERT(fPtsToUnit.isIdentity());
721 // invert the localM, translate to center1, rotate so center2 is on x axis.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000722 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000723 if (!this->getLocalMatrix().invert(&matrix)) {
humper@google.com84831ac2013-01-14 22:09:54 +0000724 return NULL;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000725 }
726 matrix.postTranslate(-fCenter1.fX, -fCenter1.fY);
727
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000728 SkPoint diff = fCenter2 - fCenter1;
729 SkScalar diffLen = diff.length();
730 if (0 != diffLen) {
731 SkScalar invDiffLen = SkScalarInvert(diffLen);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000732 SkMatrix rot;
733 rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
734 SkScalarMul(invDiffLen, diff.fX));
735 matrix.postConcat(rot);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000736 }
737
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000738 return GrConical2Gradient::Create(context, *this, matrix, fTileMode);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000739}
740
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000741#else
742
bsalomon@google.com5d2cd202013-01-16 15:31:06 +0000743GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext*, const SkPaint&) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000744 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000745 return NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000746}
747
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000748#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000749
750#ifdef SK_DEVELOPER
751void SkTwoPointConicalGradient::toString(SkString* str) const {
752 str->append("SkTwoPointConicalGradient: (");
753
754 str->append("center1: (");
755 str->appendScalar(fCenter1.fX);
756 str->append(", ");
757 str->appendScalar(fCenter1.fY);
758 str->append(") radius1: ");
759 str->appendScalar(fRadius1);
760 str->append(" ");
761
762 str->append("center2: (");
763 str->appendScalar(fCenter2.fX);
764 str->append(", ");
765 str->appendScalar(fCenter2.fY);
766 str->append(") radius2: ");
767 str->appendScalar(fRadius2);
768 str->append(" ");
769
770 this->INHERITED::toString(str);
771
772 str->append(")");
773}
774#endif