blob: 0a5e29b856a3e6c9355cf127c27297fc89e08317 [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkTwoPointConicalGradient.h"
9
10static int valid_divide(float numer, float denom, float* ratio) {
11 SkASSERT(ratio);
12 if (0 == denom) {
13 return 0;
14 }
15 *ratio = numer / denom;
16 return 1;
17}
18
19// Return the number of distinct real roots, and write them into roots[] in
20// ascending order
21static int find_quad_roots(float A, float B, float C, float roots[2]) {
22 SkASSERT(roots);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000023
rileya@google.com589708b2012-07-26 20:04:23 +000024 if (A == 0) {
25 return valid_divide(-C, B, roots);
26 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000027
rileya@google.com589708b2012-07-26 20:04:23 +000028 float R = B*B - 4*A*C;
29 if (R < 0) {
30 return 0;
31 }
32 R = sk_float_sqrt(R);
33
34#if 1
35 float Q = B;
36 if (Q < 0) {
37 Q -= R;
38 } else {
39 Q += R;
40 }
41#else
42 // on 10.6 this was much slower than the above branch :(
43 float Q = B + copysignf(R, B);
44#endif
45 Q *= -0.5f;
46 if (0 == Q) {
47 roots[0] = 0;
48 return 1;
49 }
50
51 float r0 = Q / A;
52 float r1 = C / Q;
53 roots[0] = r0 < r1 ? r0 : r1;
54 roots[1] = r0 > r1 ? r0 : r1;
55 return 2;
56}
57
58static float lerp(float x, float dx, float t) {
59 return x + t * dx;
60}
61
62static float sqr(float x) { return x * x; }
63
64void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0,
65 const SkPoint& center1, SkScalar rad1) {
66 fCenterX = SkScalarToFloat(center0.fX);
67 fCenterY = SkScalarToFloat(center0.fY);
68 fDCenterX = SkScalarToFloat(center1.fX) - fCenterX;
69 fDCenterY = SkScalarToFloat(center1.fY) - fCenterY;
70 fRadius = SkScalarToFloat(rad0);
71 fDRadius = SkScalarToFloat(rad1) - fRadius;
72
73 fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius);
74 fRadius2 = sqr(fRadius);
75 fRDR = fRadius * fDRadius;
76}
77
78void TwoPtRadial::setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy) {
79 fRelX = SkScalarToFloat(fx) - fCenterX;
80 fRelY = SkScalarToFloat(fy) - fCenterY;
81 fIncX = SkScalarToFloat(dfx);
82 fIncY = SkScalarToFloat(dfy);
83 fB = -2 * (fDCenterX * fRelX + fDCenterY * fRelY + fRDR);
84 fDB = -2 * (fDCenterX * fIncX + fDCenterY * fIncY);
85}
86
87SkFixed TwoPtRadial::nextT() {
88 float roots[2];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089
rileya@google.com589708b2012-07-26 20:04:23 +000090 float C = sqr(fRelX) + sqr(fRelY) - fRadius2;
91 int countRoots = find_quad_roots(fA, fB, C, roots);
92
93 fRelX += fIncX;
94 fRelY += fIncY;
95 fB += fDB;
96
97 if (0 == countRoots) {
98 return kDontDrawT;
99 }
100
101 // Prefer the bigger t value if both give a radius(t) > 0
102 // find_quad_roots returns the values sorted, so we start with the last
103 float t = roots[countRoots - 1];
104 float r = lerp(fRadius, fDRadius, t);
105 if (r <= 0) {
106 t = roots[0]; // might be the same as roots[countRoots-1]
107 r = lerp(fRadius, fDRadius, t);
108 if (r <= 0) {
109 return kDontDrawT;
110 }
111 }
112 return SkFloatToFixed(t);
113}
114
reed@google.com60040292013-02-04 18:21:23 +0000115typedef void (*TwoPointConicalProc)(TwoPtRadial* rec, SkPMColor* dstC,
116 const SkPMColor* cache, int toggle, int count);
rileya@google.com589708b2012-07-26 20:04:23 +0000117
118static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000119 const SkPMColor* SK_RESTRICT cache, int toggle,
120 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000121 for (; count > 0; --count) {
122 SkFixed t = rec->nextT();
123 if (TwoPtRadial::DontDrawT(t)) {
124 *dstC++ = 0;
125 } else {
126 SkFixed index = SkClampMax(t, 0xFFFF);
127 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000128 *dstC++ = cache[toggle +
129 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000130 }
reed@google.com60040292013-02-04 18:21:23 +0000131 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000132 }
133}
134
135static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000136 const SkPMColor* SK_RESTRICT cache, int toggle,
137 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000138 for (; count > 0; --count) {
139 SkFixed t = rec->nextT();
140 if (TwoPtRadial::DontDrawT(t)) {
141 *dstC++ = 0;
142 } else {
143 SkFixed index = repeat_tileproc(t);
144 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000145 *dstC++ = cache[toggle +
146 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000147 }
reed@google.com60040292013-02-04 18:21:23 +0000148 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000149 }
150}
151
152static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
reed@google.com60040292013-02-04 18:21:23 +0000153 const SkPMColor* SK_RESTRICT cache, int toggle,
154 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000155 for (; count > 0; --count) {
156 SkFixed t = rec->nextT();
157 if (TwoPtRadial::DontDrawT(t)) {
158 *dstC++ = 0;
159 } else {
160 SkFixed index = mirror_tileproc(t);
161 SkASSERT(index <= 0xFFFF);
reed@google.com60040292013-02-04 18:21:23 +0000162 *dstC++ = cache[toggle +
163 (index >> SkGradientShaderBase::kCache32Shift)];
rileya@google.com589708b2012-07-26 20:04:23 +0000164 }
reed@google.com60040292013-02-04 18:21:23 +0000165 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000166 }
167}
168
169void SkTwoPointConicalGradient::init() {
170 fRec.init(fCenter1, fRadius1, fCenter2, fRadius2);
171 fPtsToUnit.reset();
172}
173
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000174/////////////////////////////////////////////////////////////////////
175
rileya@google.com589708b2012-07-26 20:04:23 +0000176SkTwoPointConicalGradient::SkTwoPointConicalGradient(
reed@google.com3d3a8602013-05-24 14:58:44 +0000177 const SkPoint& start, SkScalar startRadius,
178 const SkPoint& end, SkScalar endRadius,
179 const Descriptor& desc)
reed@google.com437d6eb2013-05-23 19:03:05 +0000180 : SkGradientShaderBase(desc),
rileya@google.com589708b2012-07-26 20:04:23 +0000181 fCenter1(start),
182 fCenter2(end),
183 fRadius1(startRadius),
184 fRadius2(endRadius) {
185 // this is degenerate, and should be caught by our caller
186 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
187 this->init();
188}
189
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000190bool SkTwoPointConicalGradient::isOpaque() const {
robertphillips@google.comcb6d97c2013-07-09 13:50:09 +0000191 // Because areas outside the cone are left untouched, we cannot treat the
192 // shader as opaque even if the gradient itself is opaque.
193 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
194 return false;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000195}
196
rileya@google.com589708b2012-07-26 20:04:23 +0000197void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam,
198 int count) {
reed@google.com60040292013-02-04 18:21:23 +0000199 int toggle = init_dither_toggle(x, y);
reed@google.com60040292013-02-04 18:21:23 +0000200
rileya@google.com589708b2012-07-26 20:04:23 +0000201 SkASSERT(count > 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000202
rileya@google.com589708b2012-07-26 20:04:23 +0000203 SkPMColor* SK_RESTRICT dstC = dstCParam;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204
rileya@google.com589708b2012-07-26 20:04:23 +0000205 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
bsalomon@google.com100abf42012-09-05 17:40:04 +0000206
rileya@google.com589708b2012-07-26 20:04:23 +0000207 const SkPMColor* SK_RESTRICT cache = this->getCache32();
208
reed@google.com60040292013-02-04 18:21:23 +0000209 TwoPointConicalProc shadeProc = twopoint_repeat;
rileya@google.com589708b2012-07-26 20:04:23 +0000210 if (SkShader::kClamp_TileMode == fTileMode) {
211 shadeProc = twopoint_clamp;
212 } else if (SkShader::kMirror_TileMode == fTileMode) {
213 shadeProc = twopoint_mirror;
214 } else {
215 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
216 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217
rileya@google.com589708b2012-07-26 20:04:23 +0000218 if (fDstToIndexClass != kPerspective_MatrixClass) {
219 SkPoint srcPt;
220 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
221 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
222 SkScalar dx, fx = srcPt.fX;
223 SkScalar dy, fy = srcPt.fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224
rileya@google.com589708b2012-07-26 20:04:23 +0000225 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
226 SkFixed fixedX, fixedY;
227 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
228 dx = SkFixedToScalar(fixedX);
229 dy = SkFixedToScalar(fixedY);
230 } else {
231 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
232 dx = fDstToIndex.getScaleX();
233 dy = fDstToIndex.getSkewY();
234 }
235
236 fRec.setup(fx, fy, dx, dy);
reed@google.com60040292013-02-04 18:21:23 +0000237 (*shadeProc)(&fRec, dstC, cache, toggle, count);
rileya@google.com589708b2012-07-26 20:04:23 +0000238 } else { // perspective case
239 SkScalar dstX = SkIntToScalar(x);
240 SkScalar dstY = SkIntToScalar(y);
241 for (; count > 0; --count) {
242 SkPoint srcPt;
243 dstProc(fDstToIndex, dstX, dstY, &srcPt);
244 dstX += SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000245
rileya@google.com589708b2012-07-26 20:04:23 +0000246 fRec.setup(srcPt.fX, srcPt.fY, 0, 0);
reed@google.com60040292013-02-04 18:21:23 +0000247 (*shadeProc)(&fRec, dstC, cache, toggle, 1);
reed@google.com60040292013-02-04 18:21:23 +0000248 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000249 }
250 }
251}
252
253bool SkTwoPointConicalGradient::setContext(const SkBitmap& device,
254 const SkPaint& paint,
255 const SkMatrix& matrix) {
256 if (!this->INHERITED::setContext(device, paint, matrix)) {
257 return false;
258 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000259
rileya@google.com589708b2012-07-26 20:04:23 +0000260 // we don't have a span16 proc
261 fFlags &= ~kHasSpan16_Flag;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000262
rileya@google.com589708b2012-07-26 20:04:23 +0000263 // in general, we might discard based on computed-radius, so clear
264 // this flag (todo: sometimes we can detect that we never discard...)
265 fFlags &= ~kOpaqueAlpha_Flag;
266
267 return true;
268}
269
270SkShader::BitmapType SkTwoPointConicalGradient::asABitmap(
271 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const {
272 SkPoint diff = fCenter2 - fCenter1;
rileya@google.com589708b2012-07-26 20:04:23 +0000273 SkScalar diffLen = 0;
274
275 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000276 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000277 }
278 if (matrix) {
279 diffLen = diff.length();
280 }
281 if (matrix) {
282 if (diffLen) {
283 SkScalar invDiffLen = SkScalarInvert(diffLen);
284 // rotate to align circle centers with the x-axis
285 matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
286 SkScalarMul(invDiffLen, diff.fX));
287 } else {
288 matrix->reset();
289 }
290 matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
291 }
292 if (xy) {
293 xy[0] = fTileMode;
294 xy[1] = kClamp_TileMode;
295 }
296 return kTwoPointConical_BitmapType;
297}
298
299SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
300 GradientInfo* info) const {
301 if (info) {
302 commonAsAGradient(info);
303 info->fPoint[0] = fCenter1;
304 info->fPoint[1] = fCenter2;
305 info->fRadius[0] = fRadius1;
306 info->fRadius[1] = fRadius2;
307 }
308 return kConical_GradientType;
309}
310
rileya@google.com589708b2012-07-26 20:04:23 +0000311SkTwoPointConicalGradient::SkTwoPointConicalGradient(
312 SkFlattenableReadBuffer& buffer)
313 : INHERITED(buffer),
314 fCenter1(buffer.readPoint()),
315 fCenter2(buffer.readPoint()),
316 fRadius1(buffer.readScalar()),
317 fRadius2(buffer.readScalar()) {
318 this->init();
319};
320
321void SkTwoPointConicalGradient::flatten(
322 SkFlattenableWriteBuffer& buffer) const {
323 this->INHERITED::flatten(buffer);
324 buffer.writePoint(fCenter1);
325 buffer.writePoint(fCenter2);
326 buffer.writeScalar(fRadius1);
327 buffer.writeScalar(fRadius2);
328}
329
rileya@google.comd7cc6512012-07-27 14:00:39 +0000330/////////////////////////////////////////////////////////////////////
331
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000332#if SK_SUPPORT_GPU
333
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000334#include "GrTBackendEffectFactory.h"
335
rileya@google.comd7cc6512012-07-27 14:00:39 +0000336// For brevity
337typedef GrGLUniformManager::UniformHandle UniformHandle;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000338
bsalomon@google.com0707c292012-10-25 21:45:42 +0000339class GrGLConical2Gradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000340public:
341
bsalomon@google.comc7818882013-03-20 19:19:53 +0000342 GrGLConical2Gradient(const GrBackendEffectFactory& factory, const GrDrawEffect&);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000343 virtual ~GrGLConical2Gradient() { }
344
bsalomon@google.comf78df332012-10-29 12:43:38 +0000345 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000346 const GrDrawEffect&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000347 EffectKey,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000348 const char* outputColor,
349 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000350 const TransformedCoordsArray&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000351 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000352 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000353
bsalomon@google.comc7818882013-03-20 19:19:53 +0000354 static EffectKey GenKey(const GrDrawEffect&, const GrGLCaps& caps);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000355
356protected:
357
358 UniformHandle fVSParamUni;
359 UniformHandle fFSParamUni;
360
361 const char* fVSVaryingName;
362 const char* fFSVaryingName;
363
364 bool fIsDegenerate;
365
366 // @{
367 /// Values last uploaded as uniforms
368
bsalomon@google.com81712882012-11-01 17:12:34 +0000369 SkScalar fCachedCenter;
370 SkScalar fCachedRadius;
371 SkScalar fCachedDiffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000372
373 // @}
374
375private:
376
bsalomon@google.com0707c292012-10-25 21:45:42 +0000377 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000378
379};
380
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000381/////////////////////////////////////////////////////////////////////
382
383class GrConical2Gradient : public GrGradientEffect {
384public:
385
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000386 static GrEffectRef* Create(GrContext* ctx,
387 const SkTwoPointConicalGradient& shader,
388 const SkMatrix& matrix,
389 SkShader::TileMode tm) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000390 AutoEffectUnref effect(SkNEW_ARGS(GrConical2Gradient, (ctx, shader, matrix, tm)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000391 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000392 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000393
394 virtual ~GrConical2Gradient() { }
395
396 static const char* Name() { return "Two-Point Conical Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000397 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
398 return GrTBackendEffectFactory<GrConical2Gradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000399 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000400
401 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
402 bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000403 SkScalar center() const { return fCenterX1; }
404 SkScalar diffRadius() const { return fDiffRadius; }
405 SkScalar radius() const { return fRadius0; }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000406
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000407 typedef GrGLConical2Gradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000408
409private:
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000410 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000411 const GrConical2Gradient& s = CastEffect<GrConical2Gradient>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000412 return (INHERITED::onIsEqual(sBase) &&
413 this->fCenterX1 == s.fCenterX1 &&
414 this->fRadius0 == s.fRadius0 &&
415 this->fDiffRadius == s.fDiffRadius);
416 }
417
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000418 GrConical2Gradient(GrContext* ctx,
419 const SkTwoPointConicalGradient& shader,
420 const SkMatrix& matrix,
421 SkShader::TileMode tm)
422 : INHERITED(ctx, shader, matrix, tm)
423 , fCenterX1(shader.getCenterX1())
424 , fRadius0(shader.getStartRadius())
425 , fDiffRadius(shader.getDiffRadius()) { }
426
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000427 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000428
429 // @{
430 // Cache of values - these can change arbitrarily, EXCEPT
431 // we shouldn't change between degenerate and non-degenerate?!
432
bsalomon@google.com81712882012-11-01 17:12:34 +0000433 SkScalar fCenterX1;
434 SkScalar fRadius0;
435 SkScalar fDiffRadius;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000436
437 // @}
438
439 typedef GrGradientEffect INHERITED;
440};
441
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000442GR_DEFINE_EFFECT_TEST(GrConical2Gradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000443
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000444GrEffectRef* GrConical2Gradient::TestCreate(SkRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000445 GrContext* context,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000446 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000447 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000448 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
449 SkScalar radius1 = random->nextUScalar1();
450 SkPoint center2;
451 SkScalar radius2;
452 do {
bsalomon@google.comfb883bf2012-12-11 15:32:04 +0000453 center2.set(random->nextUScalar1(), random->nextUScalar1());
bsalomon@google.comd4726202012-08-03 14:34:46 +0000454 radius2 = random->nextUScalar1 ();
455 // If the circles are identical the factory will give us an empty shader.
456 } while (radius1 == radius2 && center1 == center2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000457
bsalomon@google.comd4726202012-08-03 14:34:46 +0000458 SkColor colors[kMaxRandomGradientColors];
459 SkScalar stopsArray[kMaxRandomGradientColors];
460 SkScalar* stops = stopsArray;
461 SkShader::TileMode tm;
462 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
463 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
464 center2, radius2,
465 colors, stops, colorCount,
466 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000467 SkPaint paint;
468 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000469}
470
471
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000472/////////////////////////////////////////////////////////////////////
473
bsalomon@google.com6340a412013-01-22 19:55:59 +0000474GrGLConical2Gradient::GrGLConical2Gradient(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000475 const GrDrawEffect& drawEffect)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000476 : INHERITED(factory)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000477 , fVSVaryingName(NULL)
478 , fFSVaryingName(NULL)
bsalomon@google.com81712882012-11-01 17:12:34 +0000479 , fCachedCenter(SK_ScalarMax)
480 , fCachedRadius(-SK_ScalarMax)
481 , fCachedDiffRadius(-SK_ScalarMax) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000482
bsalomon@google.comc7818882013-03-20 19:19:53 +0000483 const GrConical2Gradient& data = drawEffect.castEffect<GrConical2Gradient>();
rileya@google.comd7cc6512012-07-27 14:00:39 +0000484 fIsDegenerate = data.isDegenerate();
485}
486
bsalomon@google.comf78df332012-10-29 12:43:38 +0000487void GrGLConical2Gradient::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000488 const GrDrawEffect&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000489 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000490 const char* outputColor,
491 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000492 const TransformedCoordsArray& coords,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000493 const TextureSamplerArray& samplers) {
bsalomon@google.com82d12232013-09-09 15:36:26 +0000494 this->emitUniforms(builder, key);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000495 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
496 // to work around Xoom bug. Doesn't seem to cause performance decrease
497 // in test apps, but need to keep an eye on it.
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000498 fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_Visibility,
rileya@google.comd7cc6512012-07-27 14:00:39 +0000499 kFloat_GrSLType, "Conical2VSParams", 6);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000500 fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility,
rileya@google.comd7cc6512012-07-27 14:00:39 +0000501 kFloat_GrSLType, "Conical2FSParams", 6);
502
503 // For radial gradients without perspective we can pass the linear
504 // part of the quadratic as a varying.
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000505 GrGLShaderBuilder::VertexBuilder* vertexBuilder =
bsalomon@google.com77af6802013-10-02 13:04:56 +0000506 (kVec2f_GrSLType == coords[0].type()) ? builder->getVertexBuilder() : NULL;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000507 if (NULL != vertexBuilder) {
508 vertexBuilder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
509 &fVSVaryingName, &fFSVaryingName);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000510 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000511
bsalomon@google.comf78df332012-10-29 12:43:38 +0000512 // VS
513 {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000514 SkString p2; // distance between centers
515 SkString p3; // start radius
516 SkString p5; // difference in radii (r1 - r0)
517 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
518 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
519 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000520
bsalomon@google.comf78df332012-10-29 12:43:38 +0000521 // For radial gradients without perspective we can pass the linear
522 // part of the quadratic as a varying.
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000523 if (NULL != vertexBuilder) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000524 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000525 vertexBuilder->vsCodeAppendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
526 fVSVaryingName, p2.c_str(),
bsalomon@google.com77af6802013-10-02 13:04:56 +0000527 coords[0].getVSName().c_str(), p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000528 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000529 }
530
bsalomon@google.comf78df332012-10-29 12:43:38 +0000531 // FS
532 {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000533 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
bsalomon@google.comf78df332012-10-29 12:43:38 +0000534 SkString cName("c");
535 SkString ac4Name("ac4");
536 SkString dName("d");
537 SkString qName("q");
538 SkString r0Name("r0");
539 SkString r1Name("r1");
540 SkString tName("t");
541 SkString p0; // 4a
542 SkString p1; // 1/a
543 SkString p2; // distance between centers
544 SkString p3; // start radius
545 SkString p4; // start radius squared
546 SkString p5; // difference in radii (r1 - r0)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000547
bsalomon@google.comf78df332012-10-29 12:43:38 +0000548 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
549 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
550 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
551 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
552 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
553 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000554
bsalomon@google.comf78df332012-10-29 12:43:38 +0000555 // If we we're able to interpolate the linear component,
556 // bVar is the varying; otherwise compute it
557 SkString bVar;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000558 if (NULL != vertexBuilder) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000559 bVar = fFSVaryingName;
560 } else {
561 bVar = "b";
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000562 builder->fsCodeAppendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
bsalomon@google.com77af6802013-10-02 13:04:56 +0000563 bVar.c_str(), p2.c_str(), coords2D.c_str(),
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000564 p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000565 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000566
bsalomon@google.comf78df332012-10-29 12:43:38 +0000567 // output will default to transparent black (we simply won't write anything
568 // else to it if invalid, instead of discarding or returning prematurely)
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000569 builder->fsCodeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000570
bsalomon@google.comf78df332012-10-29 12:43:38 +0000571 // c = (x^2)+(y^2) - params[4]
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000572 builder->fsCodeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
bsalomon@google.com77af6802013-10-02 13:04:56 +0000573 coords2D.c_str(), coords2D.c_str(),
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000574 p4.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000575
bsalomon@google.comf78df332012-10-29 12:43:38 +0000576 // Non-degenerate case (quadratic)
577 if (!fIsDegenerate) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000578
bsalomon@google.comf78df332012-10-29 12:43:38 +0000579 // ac4 = params[0] * c
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000580 builder->fsCodeAppendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
581 cName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000582
bsalomon@google.comf78df332012-10-29 12:43:38 +0000583 // d = b^2 - ac4
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000584 builder->fsCodeAppendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
585 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000586
bsalomon@google.comf78df332012-10-29 12:43:38 +0000587 // only proceed if discriminant is >= 0
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000588 builder->fsCodeAppendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000589
bsalomon@google.comf78df332012-10-29 12:43:38 +0000590 // intermediate value we'll use to compute the roots
591 // q = -0.5 * (b +/- sqrt(d))
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000592 builder->fsCodeAppendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
593 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
594 bVar.c_str(), dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000595
bsalomon@google.comf78df332012-10-29 12:43:38 +0000596 // compute both roots
597 // r0 = q * params[1]
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000598 builder->fsCodeAppendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
599 qName.c_str(), p1.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000600 // r1 = c / q
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000601 builder->fsCodeAppendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
602 cName.c_str(), qName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000603
bsalomon@google.comf78df332012-10-29 12:43:38 +0000604 // Note: If there are two roots that both generate radius(t) > 0, the
605 // Canvas spec says to choose the larger t.
rileya@google.comd7cc6512012-07-27 14:00:39 +0000606
bsalomon@google.comf78df332012-10-29 12:43:38 +0000607 // so we'll look at the larger one first:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000608 builder->fsCodeAppendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
609 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000610
bsalomon@google.comf78df332012-10-29 12:43:38 +0000611 // if r(t) > 0, then we're done; t will be our x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000612 builder->fsCodeAppendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
613 p5.c_str(), p3.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000614
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000615 builder->fsCodeAppend("\t\t");
bsalomon@google.com82d12232013-09-09 15:36:26 +0000616 this->emitColor(builder, tName.c_str(), key, outputColor, inputColor, samplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000617
bsalomon@google.comf78df332012-10-29 12:43:38 +0000618 // otherwise, if r(t) for the larger root was <= 0, try the other root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000619 builder->fsCodeAppend("\t\t} else {\n");
620 builder->fsCodeAppendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
621 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000622
bsalomon@google.comf78df332012-10-29 12:43:38 +0000623 // if r(t) > 0 for the smaller root, then t will be our x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000624 builder->fsCodeAppendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
625 tName.c_str(), p5.c_str(), p3.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000626
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000627 builder->fsCodeAppend("\t\t\t");
bsalomon@google.com82d12232013-09-09 15:36:26 +0000628 this->emitColor(builder, tName.c_str(), key, outputColor, inputColor, samplers);
bsalomon@google.comf78df332012-10-29 12:43:38 +0000629
630 // end if (r(t) > 0) for smaller root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000631 builder->fsCodeAppend("\t\t\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000632 // end if (r(t) > 0), else, for larger root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000633 builder->fsCodeAppend("\t\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000634 // end if (discriminant >= 0)
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000635 builder->fsCodeAppend("\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000636 } else {
637
638 // linear case: t = -c/b
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000639 builder->fsCodeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
640 cName.c_str(), bVar.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000641
642 // if r(t) > 0, then t will be the x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000643 builder->fsCodeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
644 p5.c_str(), p3.c_str());
645 builder->fsCodeAppend("\t");
bsalomon@google.com82d12232013-09-09 15:36:26 +0000646 this->emitColor(builder, tName.c_str(), key, outputColor, inputColor, samplers);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000647 builder->fsCodeAppend("\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000648 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000649 }
650}
651
bsalomon@google.comc7818882013-03-20 19:19:53 +0000652void GrGLConical2Gradient::setData(const GrGLUniformManager& uman,
653 const GrDrawEffect& drawEffect) {
654 INHERITED::setData(uman, drawEffect);
655 const GrConical2Gradient& data = drawEffect.castEffect<GrConical2Gradient>();
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000656 SkASSERT(data.isDegenerate() == fIsDegenerate);
bsalomon@google.com81712882012-11-01 17:12:34 +0000657 SkScalar centerX1 = data.center();
658 SkScalar radius0 = data.radius();
659 SkScalar diffRadius = data.diffRadius();
rileya@google.comd7cc6512012-07-27 14:00:39 +0000660
661 if (fCachedCenter != centerX1 ||
662 fCachedRadius != radius0 ||
663 fCachedDiffRadius != diffRadius) {
664
bsalomon@google.com81712882012-11-01 17:12:34 +0000665 SkScalar a = SkScalarMul(centerX1, centerX1) - diffRadius * diffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000666
667 // When we're in the degenerate (linear) case, the second
668 // value will be INF but the program doesn't read it. (We
669 // use the same 6 uniforms even though we don't need them
670 // all in the linear case just to keep the code complexity
671 // down).
672 float values[6] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000673 SkScalarToFloat(a * 4),
674 1.f / (SkScalarToFloat(a)),
675 SkScalarToFloat(centerX1),
676 SkScalarToFloat(radius0),
677 SkScalarToFloat(SkScalarMul(radius0, radius0)),
678 SkScalarToFloat(diffRadius)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000679 };
680
681 uman.set1fv(fVSParamUni, 0, 6, values);
682 uman.set1fv(fFSParamUni, 0, 6, values);
683 fCachedCenter = centerX1;
684 fCachedRadius = radius0;
685 fCachedDiffRadius = diffRadius;
686 }
687}
688
bsalomon@google.comc7818882013-03-20 19:19:53 +0000689GrGLEffect::EffectKey GrGLConical2Gradient::GenKey(const GrDrawEffect& drawEffect,
690 const GrGLCaps&) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000691 enum {
bsalomon@google.com82d12232013-09-09 15:36:26 +0000692 kIsDegenerate = 1 << kBaseKeyBitCnt,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000693 };
694
bsalomon@google.com82d12232013-09-09 15:36:26 +0000695 EffectKey key = GenBaseGradientKey(drawEffect);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000696 if (drawEffect.castEffect<GrConical2Gradient>().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