blob: d3fb4dd4355008bf784bb68daf43d909340e3464 [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 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000133 }
134}
135
136static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000137 const SkPMColor* SK_RESTRICT cache, int toggle,
138 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000139 for (; count > 0; --count) {
140 SkFixed t = rec->nextT();
141 if (TwoPtRadial::DontDrawT(t)) {
142 *dstC++ = 0;
143 } else {
144 SkFixed index = repeat_tileproc(t);
145 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000146 *dstC++ = cache[toggle +
147 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000148 }
reed@google.com60040292013-02-04 18:21:23 +0000149 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000150 }
151}
152
153static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000154 const SkPMColor* SK_RESTRICT cache, int toggle,
155 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000156 for (; count > 0; --count) {
157 SkFixed t = rec->nextT();
158 if (TwoPtRadial::DontDrawT(t)) {
159 *dstC++ = 0;
160 } else {
161 SkFixed index = mirror_tileproc(t);
162 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000163 *dstC++ = cache[toggle +
164 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000165 }
reed@google.com60040292013-02-04 18:21:23 +0000166 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000167 }
168}
169
170void SkTwoPointConicalGradient::init() {
171 fRec.init(fCenter1, fRadius1, fCenter2, fRadius2);
172 fPtsToUnit.reset();
173}
174
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000175/////////////////////////////////////////////////////////////////////
176
rileya@google.com589708b2012-07-26 20:04:23 +0000177SkTwoPointConicalGradient::SkTwoPointConicalGradient(
178 const SkPoint& start, SkScalar startRadius,
179 const SkPoint& end, SkScalar endRadius,
180 const SkColor colors[], const SkScalar pos[],
181 int colorCount, SkShader::TileMode mode,
182 SkUnitMapper* mapper)
183 : SkGradientShaderBase(colors, pos, colorCount, mode, mapper),
184 fCenter1(start),
185 fCenter2(end),
186 fRadius1(startRadius),
187 fRadius2(endRadius) {
188 // this is degenerate, and should be caught by our caller
189 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
190 this->init();
191}
192
193void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam,
194 int count) {
reed@google.com60040292013-02-04 18:21:23 +0000195 int toggle = init_dither_toggle(x, y);
reed@google.com60040292013-02-04 18:21:23 +0000196
rileya@google.com589708b2012-07-26 20:04:23 +0000197 SkASSERT(count > 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000198
rileya@google.com589708b2012-07-26 20:04:23 +0000199 SkPMColor* SK_RESTRICT dstC = dstCParam;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
rileya@google.com589708b2012-07-26 20:04:23 +0000201 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
bsalomon@google.com100abf42012-09-05 17:40:04 +0000202
rileya@google.com589708b2012-07-26 20:04:23 +0000203 const SkPMColor* SK_RESTRICT cache = this->getCache32();
204
reed@google.com60040292013-02-04 18:21:23 +0000205 TwoPointConicalProc shadeProc = twopoint_repeat;
rileya@google.com589708b2012-07-26 20:04:23 +0000206 if (SkShader::kClamp_TileMode == fTileMode) {
207 shadeProc = twopoint_clamp;
208 } else if (SkShader::kMirror_TileMode == fTileMode) {
209 shadeProc = twopoint_mirror;
210 } else {
211 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
212 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213
rileya@google.com589708b2012-07-26 20:04:23 +0000214 if (fDstToIndexClass != kPerspective_MatrixClass) {
215 SkPoint srcPt;
216 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
217 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
218 SkScalar dx, fx = srcPt.fX;
219 SkScalar dy, fy = srcPt.fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000220
rileya@google.com589708b2012-07-26 20:04:23 +0000221 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
222 SkFixed fixedX, fixedY;
223 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
224 dx = SkFixedToScalar(fixedX);
225 dy = SkFixedToScalar(fixedY);
226 } else {
227 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
228 dx = fDstToIndex.getScaleX();
229 dy = fDstToIndex.getSkewY();
230 }
231
232 fRec.setup(fx, fy, dx, dy);
reed@google.com60040292013-02-04 18:21:23 +0000233 (*shadeProc)(&fRec, dstC, cache, toggle, count);
rileya@google.com589708b2012-07-26 20:04:23 +0000234 } else { // perspective case
235 SkScalar dstX = SkIntToScalar(x);
236 SkScalar dstY = SkIntToScalar(y);
237 for (; count > 0; --count) {
238 SkPoint srcPt;
239 dstProc(fDstToIndex, dstX, dstY, &srcPt);
240 dstX += SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000241
rileya@google.com589708b2012-07-26 20:04:23 +0000242 fRec.setup(srcPt.fX, srcPt.fY, 0, 0);
reed@google.com60040292013-02-04 18:21:23 +0000243 (*shadeProc)(&fRec, dstC, cache, toggle, 1);
reed@google.com60040292013-02-04 18:21:23 +0000244 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000245 }
246 }
247}
248
249bool SkTwoPointConicalGradient::setContext(const SkBitmap& device,
250 const SkPaint& paint,
251 const SkMatrix& matrix) {
252 if (!this->INHERITED::setContext(device, paint, matrix)) {
253 return false;
254 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000255
rileya@google.com589708b2012-07-26 20:04:23 +0000256 // we don't have a span16 proc
257 fFlags &= ~kHasSpan16_Flag;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000258
rileya@google.com589708b2012-07-26 20:04:23 +0000259 // in general, we might discard based on computed-radius, so clear
260 // this flag (todo: sometimes we can detect that we never discard...)
261 fFlags &= ~kOpaqueAlpha_Flag;
262
263 return true;
264}
265
266SkShader::BitmapType SkTwoPointConicalGradient::asABitmap(
267 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const {
268 SkPoint diff = fCenter2 - fCenter1;
rileya@google.com589708b2012-07-26 20:04:23 +0000269 SkScalar diffLen = 0;
270
271 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000272 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000273 }
274 if (matrix) {
275 diffLen = diff.length();
276 }
277 if (matrix) {
278 if (diffLen) {
279 SkScalar invDiffLen = SkScalarInvert(diffLen);
280 // rotate to align circle centers with the x-axis
281 matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
282 SkScalarMul(invDiffLen, diff.fX));
283 } else {
284 matrix->reset();
285 }
286 matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
287 }
288 if (xy) {
289 xy[0] = fTileMode;
290 xy[1] = kClamp_TileMode;
291 }
292 return kTwoPointConical_BitmapType;
293}
294
295SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
296 GradientInfo* info) const {
297 if (info) {
298 commonAsAGradient(info);
299 info->fPoint[0] = fCenter1;
300 info->fPoint[1] = fCenter2;
301 info->fRadius[0] = fRadius1;
302 info->fRadius[1] = fRadius2;
303 }
304 return kConical_GradientType;
305}
306
rileya@google.com589708b2012-07-26 20:04:23 +0000307SkTwoPointConicalGradient::SkTwoPointConicalGradient(
308 SkFlattenableReadBuffer& buffer)
309 : INHERITED(buffer),
310 fCenter1(buffer.readPoint()),
311 fCenter2(buffer.readPoint()),
312 fRadius1(buffer.readScalar()),
313 fRadius2(buffer.readScalar()) {
314 this->init();
315};
316
317void SkTwoPointConicalGradient::flatten(
318 SkFlattenableWriteBuffer& buffer) const {
319 this->INHERITED::flatten(buffer);
320 buffer.writePoint(fCenter1);
321 buffer.writePoint(fCenter2);
322 buffer.writeScalar(fRadius1);
323 buffer.writeScalar(fRadius2);
324}
325
rileya@google.comd7cc6512012-07-27 14:00:39 +0000326/////////////////////////////////////////////////////////////////////
327
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000328#if SK_SUPPORT_GPU
329
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000330#include "GrTBackendEffectFactory.h"
331
rileya@google.comd7cc6512012-07-27 14:00:39 +0000332// For brevity
333typedef GrGLUniformManager::UniformHandle UniformHandle;
334static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
335
bsalomon@google.com0707c292012-10-25 21:45:42 +0000336class GrGLConical2Gradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000337public:
338
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000339 GrGLConical2Gradient(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000340 const GrEffectRef&);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000341 virtual ~GrGLConical2Gradient() { }
342
bsalomon@google.comf78df332012-10-29 12:43:38 +0000343 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000344 const GrEffectStage&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000345 EffectKey,
346 const char* vertexCoords,
347 const char* outputColor,
348 const char* inputColor,
349 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000350 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000351
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000352 static EffectKey GenKey(const GrEffectStage&, const GrGLCaps& caps);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000353
354protected:
355
356 UniformHandle fVSParamUni;
357 UniformHandle fFSParamUni;
358
359 const char* fVSVaryingName;
360 const char* fFSVaryingName;
361
362 bool fIsDegenerate;
363
364 // @{
365 /// Values last uploaded as uniforms
366
bsalomon@google.com81712882012-11-01 17:12:34 +0000367 SkScalar fCachedCenter;
368 SkScalar fCachedRadius;
369 SkScalar fCachedDiffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000370
371 // @}
372
373private:
374
bsalomon@google.com0707c292012-10-25 21:45:42 +0000375 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000376
377};
378
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000379/////////////////////////////////////////////////////////////////////
380
381class GrConical2Gradient : public GrGradientEffect {
382public:
383
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000384 static GrEffectRef* Create(GrContext* ctx,
385 const SkTwoPointConicalGradient& shader,
386 const SkMatrix& matrix,
387 SkShader::TileMode tm) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000388 AutoEffectUnref effect(SkNEW_ARGS(GrConical2Gradient, (ctx, shader, matrix, tm)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000389 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000390 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000391
392 virtual ~GrConical2Gradient() { }
393
394 static const char* Name() { return "Two-Point Conical Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000395 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
396 return GrTBackendEffectFactory<GrConical2Gradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000397 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000398
399 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
400 bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000401 SkScalar center() const { return fCenterX1; }
402 SkScalar diffRadius() const { return fDiffRadius; }
403 SkScalar radius() const { return fRadius0; }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000404
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000405 typedef GrGLConical2Gradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000406
407private:
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000408 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000409 const GrConical2Gradient& s = CastEffect<GrConical2Gradient>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000410 return (INHERITED::onIsEqual(sBase) &&
411 this->fCenterX1 == s.fCenterX1 &&
412 this->fRadius0 == s.fRadius0 &&
413 this->fDiffRadius == s.fDiffRadius);
414 }
415
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000416 GrConical2Gradient(GrContext* ctx,
417 const SkTwoPointConicalGradient& shader,
418 const SkMatrix& matrix,
419 SkShader::TileMode tm)
420 : INHERITED(ctx, shader, matrix, tm)
421 , fCenterX1(shader.getCenterX1())
422 , fRadius0(shader.getStartRadius())
423 , fDiffRadius(shader.getDiffRadius()) { }
424
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000425 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000426
427 // @{
428 // Cache of values - these can change arbitrarily, EXCEPT
429 // we shouldn't change between degenerate and non-degenerate?!
430
bsalomon@google.com81712882012-11-01 17:12:34 +0000431 SkScalar fCenterX1;
432 SkScalar fRadius0;
433 SkScalar fDiffRadius;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000434
435 // @}
436
437 typedef GrGradientEffect INHERITED;
438};
439
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000440GR_DEFINE_EFFECT_TEST(GrConical2Gradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000441
bsalomon@google.com73a96942013-02-13 16:31:19 +0000442GrEffectRef* GrConical2Gradient::TestCreate(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000443 GrContext* context,
444 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000445 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
446 SkScalar radius1 = random->nextUScalar1();
447 SkPoint center2;
448 SkScalar radius2;
449 do {
bsalomon@google.comfb883bf2012-12-11 15:32:04 +0000450 center2.set(random->nextUScalar1(), random->nextUScalar1());
bsalomon@google.comd4726202012-08-03 14:34:46 +0000451 radius2 = random->nextUScalar1 ();
452 // If the circles are identical the factory will give us an empty shader.
453 } while (radius1 == radius2 && center1 == center2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000454
bsalomon@google.comd4726202012-08-03 14:34:46 +0000455 SkColor colors[kMaxRandomGradientColors];
456 SkScalar stopsArray[kMaxRandomGradientColors];
457 SkScalar* stops = stopsArray;
458 SkShader::TileMode tm;
459 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
460 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
461 center2, radius2,
462 colors, stops, colorCount,
463 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000464 SkPaint paint;
465 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000466}
467
468
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000469/////////////////////////////////////////////////////////////////////
470
bsalomon@google.com6340a412013-01-22 19:55:59 +0000471GrGLConical2Gradient::GrGLConical2Gradient(const GrBackendEffectFactory& factory,
472 const GrEffectRef& baseData)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000473 : INHERITED(factory)
474 , fVSParamUni(kInvalidUniformHandle)
475 , fFSParamUni(kInvalidUniformHandle)
476 , fVSVaryingName(NULL)
477 , fFSVaryingName(NULL)
bsalomon@google.com81712882012-11-01 17:12:34 +0000478 , fCachedCenter(SK_ScalarMax)
479 , fCachedRadius(-SK_ScalarMax)
480 , fCachedDiffRadius(-SK_ScalarMax) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000481
bsalomon@google.com6340a412013-01-22 19:55:59 +0000482 const GrConical2Gradient& data = CastEffect<GrConical2Gradient>(baseData);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000483 fIsDegenerate = data.isDegenerate();
484}
485
bsalomon@google.comf78df332012-10-29 12:43:38 +0000486void GrGLConical2Gradient::emitCode(GrGLShaderBuilder* builder,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000487 const GrEffectStage&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000488 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000489 const char* vertexCoords,
490 const char* outputColor,
491 const char* inputColor,
492 const TextureSamplerArray& samplers) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000493 const char* fsCoords;
494 const char* vsCoordsVarying;
495 GrSLType coordsVaryingType;
496 this->setupMatrix(builder, key, vertexCoords, &fsCoords, &vsCoordsVarying, &coordsVaryingType);
497
bsalomon@google.comf78df332012-10-29 12:43:38 +0000498 this->emitYCoordUniform(builder);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000499 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
500 // to work around Xoom bug. Doesn't seem to cause performance decrease
501 // in test apps, but need to keep an eye on it.
502 fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
503 kFloat_GrSLType, "Conical2VSParams", 6);
504 fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
505 kFloat_GrSLType, "Conical2FSParams", 6);
506
507 // For radial gradients without perspective we can pass the linear
508 // part of the quadratic as a varying.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000509 if (kVec2f_GrSLType == coordsVaryingType) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000510 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
511 &fVSVaryingName, &fFSVaryingName);
512 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000513
bsalomon@google.comf78df332012-10-29 12:43:38 +0000514 // VS
515 {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000516 SkString p2; // distance between centers
517 SkString p3; // start radius
518 SkString p5; // difference in radii (r1 - r0)
519 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
520 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
521 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000522
bsalomon@google.comf78df332012-10-29 12:43:38 +0000523 // For radial gradients without perspective we can pass the linear
524 // part of the quadratic as a varying.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000525 if (kVec2f_GrSLType == coordsVaryingType) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000526 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000527 builder->vsCodeAppendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
528 fVSVaryingName, p2.c_str(),
529 vsCoordsVarying, p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000530 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000531 }
532
bsalomon@google.comf78df332012-10-29 12:43:38 +0000533 // FS
534 {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000535
bsalomon@google.comf78df332012-10-29 12:43:38 +0000536 SkString cName("c");
537 SkString ac4Name("ac4");
538 SkString dName("d");
539 SkString qName("q");
540 SkString r0Name("r0");
541 SkString r1Name("r1");
542 SkString tName("t");
543 SkString p0; // 4a
544 SkString p1; // 1/a
545 SkString p2; // distance between centers
546 SkString p3; // start radius
547 SkString p4; // start radius squared
548 SkString p5; // difference in radii (r1 - r0)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000549
bsalomon@google.comf78df332012-10-29 12:43:38 +0000550 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
551 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
552 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
553 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
554 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
555 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000556
bsalomon@google.comf78df332012-10-29 12:43:38 +0000557 // If we we're able to interpolate the linear component,
558 // bVar is the varying; otherwise compute it
559 SkString bVar;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000560 if (kVec2f_GrSLType == coordsVaryingType) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000561 bVar = fFSVaryingName;
562 } else {
563 bVar = "b";
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000564 builder->fsCodeAppendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
565 bVar.c_str(), p2.c_str(), fsCoords,
566 p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000567 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000568
bsalomon@google.comf78df332012-10-29 12:43:38 +0000569 // output will default to transparent black (we simply won't write anything
570 // else to it if invalid, instead of discarding or returning prematurely)
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000571 builder->fsCodeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000572
bsalomon@google.comf78df332012-10-29 12:43:38 +0000573 // c = (x^2)+(y^2) - params[4]
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000574 builder->fsCodeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
575 fsCoords, fsCoords,
576 p4.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000577
bsalomon@google.comf78df332012-10-29 12:43:38 +0000578 // Non-degenerate case (quadratic)
579 if (!fIsDegenerate) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000580
bsalomon@google.comf78df332012-10-29 12:43:38 +0000581 // ac4 = params[0] * c
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000582 builder->fsCodeAppendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
583 cName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000584
bsalomon@google.comf78df332012-10-29 12:43:38 +0000585 // d = b^2 - ac4
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000586 builder->fsCodeAppendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
587 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000588
bsalomon@google.comf78df332012-10-29 12:43:38 +0000589 // only proceed if discriminant is >= 0
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000590 builder->fsCodeAppendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000591
bsalomon@google.comf78df332012-10-29 12:43:38 +0000592 // intermediate value we'll use to compute the roots
593 // q = -0.5 * (b +/- sqrt(d))
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000594 builder->fsCodeAppendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
595 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
596 bVar.c_str(), dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000597
bsalomon@google.comf78df332012-10-29 12:43:38 +0000598 // compute both roots
599 // r0 = q * params[1]
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000600 builder->fsCodeAppendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
601 qName.c_str(), p1.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000602 // r1 = c / q
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000603 builder->fsCodeAppendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
604 cName.c_str(), qName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000605
bsalomon@google.comf78df332012-10-29 12:43:38 +0000606 // Note: If there are two roots that both generate radius(t) > 0, the
607 // Canvas spec says to choose the larger t.
rileya@google.comd7cc6512012-07-27 14:00:39 +0000608
bsalomon@google.comf78df332012-10-29 12:43:38 +0000609 // so we'll look at the larger one first:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000610 builder->fsCodeAppendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
611 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000612
bsalomon@google.comf78df332012-10-29 12:43:38 +0000613 // if r(t) > 0, then we're done; t will be our x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000614 builder->fsCodeAppendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
615 p5.c_str(), p3.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000616
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000617 builder->fsCodeAppend("\t\t");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000618 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000619
bsalomon@google.comf78df332012-10-29 12:43:38 +0000620 // otherwise, if r(t) for the larger root was <= 0, try the other root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000621 builder->fsCodeAppend("\t\t} else {\n");
622 builder->fsCodeAppendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
623 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000624
bsalomon@google.comf78df332012-10-29 12:43:38 +0000625 // if r(t) > 0 for the smaller root, then t will be our x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000626 builder->fsCodeAppendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
627 tName.c_str(), p5.c_str(), p3.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000628
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000629 builder->fsCodeAppend("\t\t\t");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000630 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
631
632 // end if (r(t) > 0) for smaller root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000633 builder->fsCodeAppend("\t\t\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000634 // end if (r(t) > 0), else, for larger root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000635 builder->fsCodeAppend("\t\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000636 // end if (discriminant >= 0)
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000637 builder->fsCodeAppend("\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000638 } else {
639
640 // linear case: t = -c/b
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000641 builder->fsCodeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
642 cName.c_str(), bVar.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000643
644 // if r(t) > 0, then t will be the x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000645 builder->fsCodeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
646 p5.c_str(), p3.c_str());
647 builder->fsCodeAppend("\t");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000648 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000649 builder->fsCodeAppend("\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000650 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000651 }
652}
653
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000654void GrGLConical2Gradient::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
655 INHERITED::setData(uman, stage);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000656 const GrConical2Gradient& data = GetEffectFromStage<GrConical2Gradient>(stage);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000657 GrAssert(data.isDegenerate() == fIsDegenerate);
bsalomon@google.com81712882012-11-01 17:12:34 +0000658 SkScalar centerX1 = data.center();
659 SkScalar radius0 = data.radius();
660 SkScalar diffRadius = data.diffRadius();
rileya@google.comd7cc6512012-07-27 14:00:39 +0000661
662 if (fCachedCenter != centerX1 ||
663 fCachedRadius != radius0 ||
664 fCachedDiffRadius != diffRadius) {
665
bsalomon@google.com81712882012-11-01 17:12:34 +0000666 SkScalar a = SkScalarMul(centerX1, centerX1) - diffRadius * diffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000667
668 // When we're in the degenerate (linear) case, the second
669 // value will be INF but the program doesn't read it. (We
670 // use the same 6 uniforms even though we don't need them
671 // all in the linear case just to keep the code complexity
672 // down).
673 float values[6] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000674 SkScalarToFloat(a * 4),
675 1.f / (SkScalarToFloat(a)),
676 SkScalarToFloat(centerX1),
677 SkScalarToFloat(radius0),
678 SkScalarToFloat(SkScalarMul(radius0, radius0)),
679 SkScalarToFloat(diffRadius)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000680 };
681
682 uman.set1fv(fVSParamUni, 0, 6, values);
683 uman.set1fv(fFSParamUni, 0, 6, values);
684 fCachedCenter = centerX1;
685 fCachedRadius = radius0;
686 fCachedDiffRadius = diffRadius;
687 }
688}
689
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000690GrGLEffect::EffectKey GrGLConical2Gradient::GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000691 enum {
692 kIsDegenerate = 1 << kMatrixKeyBitCnt,
693 };
694
695 EffectKey key = GenMatrixKey(s);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000696 if (GetEffectFromStage<GrConical2Gradient>(s).isDegenerate()) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000697 key |= kIsDegenerate;
698 }
699 return key;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000700}
rileya@google.comd7cc6512012-07-27 14:00:39 +0000701
702/////////////////////////////////////////////////////////////////////
703
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000704GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&) const {
bsalomon@google.com00835cc2013-01-14 17:07:22 +0000705 SkASSERT(NULL != context);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000706 SkASSERT(fPtsToUnit.isIdentity());
707 // invert the localM, translate to center1, rotate so center2 is on x axis.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000708 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000709 if (!this->getLocalMatrix().invert(&matrix)) {
humper@google.com84831ac2013-01-14 22:09:54 +0000710 return NULL;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000711 }
712 matrix.postTranslate(-fCenter1.fX, -fCenter1.fY);
713
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000714 SkPoint diff = fCenter2 - fCenter1;
715 SkScalar diffLen = diff.length();
716 if (0 != diffLen) {
717 SkScalar invDiffLen = SkScalarInvert(diffLen);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000718 SkMatrix rot;
719 rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
720 SkScalarMul(invDiffLen, diff.fX));
721 matrix.postConcat(rot);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000722 }
723
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000724 return GrConical2Gradient::Create(context, *this, matrix, fTileMode);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000725}
726
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000727#else
728
bsalomon@google.com5d2cd202013-01-16 15:31:06 +0000729GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext*, const SkPaint&) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000730 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000731 return NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000732}
733
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000734#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000735
736#ifdef SK_DEVELOPER
737void SkTwoPointConicalGradient::toString(SkString* str) const {
738 str->append("SkTwoPointConicalGradient: (");
739
740 str->append("center1: (");
741 str->appendScalar(fCenter1.fX);
742 str->append(", ");
743 str->appendScalar(fCenter1.fY);
744 str->append(") radius1: ");
745 str->appendScalar(fRadius1);
746 str->append(" ");
747
748 str->append("center2: (");
749 str->appendScalar(fCenter2.fX);
750 str->append(", ");
751 str->appendScalar(fCenter2.fY);
752 str->append(") radius2: ");
753 str->appendScalar(fRadius2);
754 str->append(" ");
755
756 this->INHERITED::toString(str);
757
758 str->append(")");
759}
760#endif