blob: 04cf1ebfe56b26018f6572a11493428636e800e7 [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
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000193bool SkTwoPointConicalGradient::isOpaque() const {
194 // Because areas outside the cone are left untouched, we cannot treat the
195 // shader as opaque even if the gradient itself is opaque.
junov@google.com723dd792013-03-20 16:54:30 +0000196 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000197 return false;
198}
199
rileya@google.com589708b2012-07-26 20:04:23 +0000200void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam,
201 int count) {
reed@google.com60040292013-02-04 18:21:23 +0000202 int toggle = init_dither_toggle(x, y);
reed@google.com60040292013-02-04 18:21:23 +0000203
rileya@google.com589708b2012-07-26 20:04:23 +0000204 SkASSERT(count > 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000205
rileya@google.com589708b2012-07-26 20:04:23 +0000206 SkPMColor* SK_RESTRICT dstC = dstCParam;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000207
rileya@google.com589708b2012-07-26 20:04:23 +0000208 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
bsalomon@google.com100abf42012-09-05 17:40:04 +0000209
rileya@google.com589708b2012-07-26 20:04:23 +0000210 const SkPMColor* SK_RESTRICT cache = this->getCache32();
211
reed@google.com60040292013-02-04 18:21:23 +0000212 TwoPointConicalProc shadeProc = twopoint_repeat;
rileya@google.com589708b2012-07-26 20:04:23 +0000213 if (SkShader::kClamp_TileMode == fTileMode) {
214 shadeProc = twopoint_clamp;
215 } else if (SkShader::kMirror_TileMode == fTileMode) {
216 shadeProc = twopoint_mirror;
217 } else {
218 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
219 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000220
rileya@google.com589708b2012-07-26 20:04:23 +0000221 if (fDstToIndexClass != kPerspective_MatrixClass) {
222 SkPoint srcPt;
223 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
224 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
225 SkScalar dx, fx = srcPt.fX;
226 SkScalar dy, fy = srcPt.fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000227
rileya@google.com589708b2012-07-26 20:04:23 +0000228 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
229 SkFixed fixedX, fixedY;
230 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
231 dx = SkFixedToScalar(fixedX);
232 dy = SkFixedToScalar(fixedY);
233 } else {
234 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
235 dx = fDstToIndex.getScaleX();
236 dy = fDstToIndex.getSkewY();
237 }
238
239 fRec.setup(fx, fy, dx, dy);
reed@google.com60040292013-02-04 18:21:23 +0000240 (*shadeProc)(&fRec, dstC, cache, toggle, count);
rileya@google.com589708b2012-07-26 20:04:23 +0000241 } else { // perspective case
242 SkScalar dstX = SkIntToScalar(x);
243 SkScalar dstY = SkIntToScalar(y);
244 for (; count > 0; --count) {
245 SkPoint srcPt;
246 dstProc(fDstToIndex, dstX, dstY, &srcPt);
247 dstX += SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000248
rileya@google.com589708b2012-07-26 20:04:23 +0000249 fRec.setup(srcPt.fX, srcPt.fY, 0, 0);
reed@google.com60040292013-02-04 18:21:23 +0000250 (*shadeProc)(&fRec, dstC, cache, toggle, 1);
reed@google.com60040292013-02-04 18:21:23 +0000251 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000252 }
253 }
254}
255
256bool SkTwoPointConicalGradient::setContext(const SkBitmap& device,
257 const SkPaint& paint,
258 const SkMatrix& matrix) {
259 if (!this->INHERITED::setContext(device, paint, matrix)) {
260 return false;
261 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000262
rileya@google.com589708b2012-07-26 20:04:23 +0000263 // we don't have a span16 proc
264 fFlags &= ~kHasSpan16_Flag;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265
rileya@google.com589708b2012-07-26 20:04:23 +0000266 // in general, we might discard based on computed-radius, so clear
267 // this flag (todo: sometimes we can detect that we never discard...)
268 fFlags &= ~kOpaqueAlpha_Flag;
269
270 return true;
271}
272
273SkShader::BitmapType SkTwoPointConicalGradient::asABitmap(
274 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const {
275 SkPoint diff = fCenter2 - fCenter1;
rileya@google.com589708b2012-07-26 20:04:23 +0000276 SkScalar diffLen = 0;
277
278 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000279 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000280 }
281 if (matrix) {
282 diffLen = diff.length();
283 }
284 if (matrix) {
285 if (diffLen) {
286 SkScalar invDiffLen = SkScalarInvert(diffLen);
287 // rotate to align circle centers with the x-axis
288 matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
289 SkScalarMul(invDiffLen, diff.fX));
290 } else {
291 matrix->reset();
292 }
293 matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
294 }
295 if (xy) {
296 xy[0] = fTileMode;
297 xy[1] = kClamp_TileMode;
298 }
299 return kTwoPointConical_BitmapType;
300}
301
302SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
303 GradientInfo* info) const {
304 if (info) {
305 commonAsAGradient(info);
306 info->fPoint[0] = fCenter1;
307 info->fPoint[1] = fCenter2;
308 info->fRadius[0] = fRadius1;
309 info->fRadius[1] = fRadius2;
310 }
311 return kConical_GradientType;
312}
313
rileya@google.com589708b2012-07-26 20:04:23 +0000314SkTwoPointConicalGradient::SkTwoPointConicalGradient(
315 SkFlattenableReadBuffer& buffer)
316 : INHERITED(buffer),
317 fCenter1(buffer.readPoint()),
318 fCenter2(buffer.readPoint()),
319 fRadius1(buffer.readScalar()),
320 fRadius2(buffer.readScalar()) {
321 this->init();
322};
323
324void SkTwoPointConicalGradient::flatten(
325 SkFlattenableWriteBuffer& buffer) const {
326 this->INHERITED::flatten(buffer);
327 buffer.writePoint(fCenter1);
328 buffer.writePoint(fCenter2);
329 buffer.writeScalar(fRadius1);
330 buffer.writeScalar(fRadius2);
331}
332
rileya@google.comd7cc6512012-07-27 14:00:39 +0000333/////////////////////////////////////////////////////////////////////
334
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000335#if SK_SUPPORT_GPU
336
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000337#include "GrTBackendEffectFactory.h"
338
rileya@google.comd7cc6512012-07-27 14:00:39 +0000339// For brevity
340typedef GrGLUniformManager::UniformHandle UniformHandle;
341static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
342
bsalomon@google.com0707c292012-10-25 21:45:42 +0000343class GrGLConical2Gradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000344public:
345
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000346 GrGLConical2Gradient(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000347 const GrEffectRef&);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000348 virtual ~GrGLConical2Gradient() { }
349
bsalomon@google.comf78df332012-10-29 12:43:38 +0000350 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000351 const GrEffectStage&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000352 EffectKey,
353 const char* vertexCoords,
354 const char* outputColor,
355 const char* inputColor,
356 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000357 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000358
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000359 static EffectKey GenKey(const GrEffectStage&, const GrGLCaps& caps);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000360
361protected:
362
363 UniformHandle fVSParamUni;
364 UniformHandle fFSParamUni;
365
366 const char* fVSVaryingName;
367 const char* fFSVaryingName;
368
369 bool fIsDegenerate;
370
371 // @{
372 /// Values last uploaded as uniforms
373
bsalomon@google.com81712882012-11-01 17:12:34 +0000374 SkScalar fCachedCenter;
375 SkScalar fCachedRadius;
376 SkScalar fCachedDiffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000377
378 // @}
379
380private:
381
bsalomon@google.com0707c292012-10-25 21:45:42 +0000382 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000383
384};
385
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000386/////////////////////////////////////////////////////////////////////
387
388class GrConical2Gradient : public GrGradientEffect {
389public:
390
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000391 static GrEffectRef* Create(GrContext* ctx,
392 const SkTwoPointConicalGradient& shader,
393 const SkMatrix& matrix,
394 SkShader::TileMode tm) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000395 AutoEffectUnref effect(SkNEW_ARGS(GrConical2Gradient, (ctx, shader, matrix, tm)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000396 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000397 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000398
399 virtual ~GrConical2Gradient() { }
400
401 static const char* Name() { return "Two-Point Conical Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000402 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
403 return GrTBackendEffectFactory<GrConical2Gradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000404 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000405
406 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
407 bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000408 SkScalar center() const { return fCenterX1; }
409 SkScalar diffRadius() const { return fDiffRadius; }
410 SkScalar radius() const { return fRadius0; }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000411
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000412 typedef GrGLConical2Gradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000413
414private:
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000415 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000416 const GrConical2Gradient& s = CastEffect<GrConical2Gradient>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000417 return (INHERITED::onIsEqual(sBase) &&
418 this->fCenterX1 == s.fCenterX1 &&
419 this->fRadius0 == s.fRadius0 &&
420 this->fDiffRadius == s.fDiffRadius);
421 }
422
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000423 GrConical2Gradient(GrContext* ctx,
424 const SkTwoPointConicalGradient& shader,
425 const SkMatrix& matrix,
426 SkShader::TileMode tm)
427 : INHERITED(ctx, shader, matrix, tm)
428 , fCenterX1(shader.getCenterX1())
429 , fRadius0(shader.getStartRadius())
430 , fDiffRadius(shader.getDiffRadius()) { }
431
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000432 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000433
434 // @{
435 // Cache of values - these can change arbitrarily, EXCEPT
436 // we shouldn't change between degenerate and non-degenerate?!
437
bsalomon@google.com81712882012-11-01 17:12:34 +0000438 SkScalar fCenterX1;
439 SkScalar fRadius0;
440 SkScalar fDiffRadius;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000441
442 // @}
443
444 typedef GrGradientEffect INHERITED;
445};
446
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000447GR_DEFINE_EFFECT_TEST(GrConical2Gradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000448
bsalomon@google.com73a96942013-02-13 16:31:19 +0000449GrEffectRef* GrConical2Gradient::TestCreate(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000450 GrContext* context,
451 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000452 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
453 SkScalar radius1 = random->nextUScalar1();
454 SkPoint center2;
455 SkScalar radius2;
456 do {
bsalomon@google.comfb883bf2012-12-11 15:32:04 +0000457 center2.set(random->nextUScalar1(), random->nextUScalar1());
bsalomon@google.comd4726202012-08-03 14:34:46 +0000458 radius2 = random->nextUScalar1 ();
459 // If the circles are identical the factory will give us an empty shader.
460 } while (radius1 == radius2 && center1 == center2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000461
bsalomon@google.comd4726202012-08-03 14:34:46 +0000462 SkColor colors[kMaxRandomGradientColors];
463 SkScalar stopsArray[kMaxRandomGradientColors];
464 SkScalar* stops = stopsArray;
465 SkShader::TileMode tm;
466 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
467 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
468 center2, radius2,
469 colors, stops, colorCount,
470 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000471 SkPaint paint;
472 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000473}
474
475
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000476/////////////////////////////////////////////////////////////////////
477
bsalomon@google.com6340a412013-01-22 19:55:59 +0000478GrGLConical2Gradient::GrGLConical2Gradient(const GrBackendEffectFactory& factory,
479 const GrEffectRef& baseData)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000480 : INHERITED(factory)
481 , fVSParamUni(kInvalidUniformHandle)
482 , fFSParamUni(kInvalidUniformHandle)
483 , fVSVaryingName(NULL)
484 , fFSVaryingName(NULL)
bsalomon@google.com81712882012-11-01 17:12:34 +0000485 , fCachedCenter(SK_ScalarMax)
486 , fCachedRadius(-SK_ScalarMax)
487 , fCachedDiffRadius(-SK_ScalarMax) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000488
bsalomon@google.com6340a412013-01-22 19:55:59 +0000489 const GrConical2Gradient& data = CastEffect<GrConical2Gradient>(baseData);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000490 fIsDegenerate = data.isDegenerate();
491}
492
bsalomon@google.comf78df332012-10-29 12:43:38 +0000493void GrGLConical2Gradient::emitCode(GrGLShaderBuilder* builder,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000494 const GrEffectStage&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000495 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000496 const char* vertexCoords,
497 const char* outputColor,
498 const char* inputColor,
499 const TextureSamplerArray& samplers) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000500 const char* fsCoords;
501 const char* vsCoordsVarying;
502 GrSLType coordsVaryingType;
503 this->setupMatrix(builder, key, vertexCoords, &fsCoords, &vsCoordsVarying, &coordsVaryingType);
504
bsalomon@google.comf78df332012-10-29 12:43:38 +0000505 this->emitYCoordUniform(builder);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000506 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
507 // to work around Xoom bug. Doesn't seem to cause performance decrease
508 // in test apps, but need to keep an eye on it.
509 fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
510 kFloat_GrSLType, "Conical2VSParams", 6);
511 fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
512 kFloat_GrSLType, "Conical2FSParams", 6);
513
514 // For radial gradients without perspective we can pass the linear
515 // part of the quadratic as a varying.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000516 if (kVec2f_GrSLType == coordsVaryingType) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000517 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
518 &fVSVaryingName, &fFSVaryingName);
519 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000520
bsalomon@google.comf78df332012-10-29 12:43:38 +0000521 // VS
522 {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000523 SkString p2; // distance between centers
524 SkString p3; // start radius
525 SkString p5; // difference in radii (r1 - r0)
526 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
527 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
528 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000529
bsalomon@google.comf78df332012-10-29 12:43:38 +0000530 // For radial gradients without perspective we can pass the linear
531 // part of the quadratic as a varying.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000532 if (kVec2f_GrSLType == coordsVaryingType) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000533 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000534 builder->vsCodeAppendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
535 fVSVaryingName, p2.c_str(),
536 vsCoordsVarying, p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000537 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000538 }
539
bsalomon@google.comf78df332012-10-29 12:43:38 +0000540 // FS
541 {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000542
bsalomon@google.comf78df332012-10-29 12:43:38 +0000543 SkString cName("c");
544 SkString ac4Name("ac4");
545 SkString dName("d");
546 SkString qName("q");
547 SkString r0Name("r0");
548 SkString r1Name("r1");
549 SkString tName("t");
550 SkString p0; // 4a
551 SkString p1; // 1/a
552 SkString p2; // distance between centers
553 SkString p3; // start radius
554 SkString p4; // start radius squared
555 SkString p5; // difference in radii (r1 - r0)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000556
bsalomon@google.comf78df332012-10-29 12:43:38 +0000557 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
558 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
559 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
560 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
561 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
562 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000563
bsalomon@google.comf78df332012-10-29 12:43:38 +0000564 // If we we're able to interpolate the linear component,
565 // bVar is the varying; otherwise compute it
566 SkString bVar;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000567 if (kVec2f_GrSLType == coordsVaryingType) {
bsalomon@google.comf78df332012-10-29 12:43:38 +0000568 bVar = fFSVaryingName;
569 } else {
570 bVar = "b";
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000571 builder->fsCodeAppendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
572 bVar.c_str(), p2.c_str(), fsCoords,
573 p3.c_str(), p5.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000574 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000575
bsalomon@google.comf78df332012-10-29 12:43:38 +0000576 // output will default to transparent black (we simply won't write anything
577 // else to it if invalid, instead of discarding or returning prematurely)
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000578 builder->fsCodeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000579
bsalomon@google.comf78df332012-10-29 12:43:38 +0000580 // c = (x^2)+(y^2) - params[4]
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000581 builder->fsCodeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
582 fsCoords, fsCoords,
583 p4.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000584
bsalomon@google.comf78df332012-10-29 12:43:38 +0000585 // Non-degenerate case (quadratic)
586 if (!fIsDegenerate) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000587
bsalomon@google.comf78df332012-10-29 12:43:38 +0000588 // ac4 = params[0] * c
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000589 builder->fsCodeAppendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
590 cName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000591
bsalomon@google.comf78df332012-10-29 12:43:38 +0000592 // d = b^2 - ac4
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000593 builder->fsCodeAppendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
594 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000595
bsalomon@google.comf78df332012-10-29 12:43:38 +0000596 // only proceed if discriminant is >= 0
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000597 builder->fsCodeAppendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000598
bsalomon@google.comf78df332012-10-29 12:43:38 +0000599 // intermediate value we'll use to compute the roots
600 // q = -0.5 * (b +/- sqrt(d))
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000601 builder->fsCodeAppendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
602 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
603 bVar.c_str(), dName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000604
bsalomon@google.comf78df332012-10-29 12:43:38 +0000605 // compute both roots
606 // r0 = q * params[1]
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000607 builder->fsCodeAppendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
608 qName.c_str(), p1.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000609 // r1 = c / q
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000610 builder->fsCodeAppendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
611 cName.c_str(), qName.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000612
bsalomon@google.comf78df332012-10-29 12:43:38 +0000613 // Note: If there are two roots that both generate radius(t) > 0, the
614 // Canvas spec says to choose the larger t.
rileya@google.comd7cc6512012-07-27 14:00:39 +0000615
bsalomon@google.comf78df332012-10-29 12:43:38 +0000616 // so we'll look at the larger one first:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000617 builder->fsCodeAppendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
618 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000619
bsalomon@google.comf78df332012-10-29 12:43:38 +0000620 // if r(t) > 0, then we're done; t will be our x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000621 builder->fsCodeAppendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
622 p5.c_str(), p3.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000623
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000624 builder->fsCodeAppend("\t\t");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000625 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000626
bsalomon@google.comf78df332012-10-29 12:43:38 +0000627 // otherwise, if r(t) for the larger root was <= 0, try the other root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000628 builder->fsCodeAppend("\t\t} else {\n");
629 builder->fsCodeAppendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
630 r0Name.c_str(), r1Name.c_str());
rileya@google.comd7cc6512012-07-27 14:00:39 +0000631
bsalomon@google.comf78df332012-10-29 12:43:38 +0000632 // if r(t) > 0 for the smaller root, then t will be our x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000633 builder->fsCodeAppendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
634 tName.c_str(), p5.c_str(), p3.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000635
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000636 builder->fsCodeAppend("\t\t\t");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000637 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
638
639 // end if (r(t) > 0) for smaller root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000640 builder->fsCodeAppend("\t\t\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000641 // end if (r(t) > 0), else, for larger root
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000642 builder->fsCodeAppend("\t\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000643 // end if (discriminant >= 0)
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000644 builder->fsCodeAppend("\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000645 } else {
646
647 // linear case: t = -c/b
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000648 builder->fsCodeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
649 cName.c_str(), bVar.c_str());
bsalomon@google.comf78df332012-10-29 12:43:38 +0000650
651 // if r(t) > 0, then t will be the x coordinate
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000652 builder->fsCodeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
653 p5.c_str(), p3.c_str());
654 builder->fsCodeAppend("\t");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000655 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000656 builder->fsCodeAppend("\t}\n");
bsalomon@google.comf78df332012-10-29 12:43:38 +0000657 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000658 }
659}
660
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000661void GrGLConical2Gradient::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
662 INHERITED::setData(uman, stage);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000663 const GrConical2Gradient& data = GetEffectFromStage<GrConical2Gradient>(stage);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000664 GrAssert(data.isDegenerate() == fIsDegenerate);
bsalomon@google.com81712882012-11-01 17:12:34 +0000665 SkScalar centerX1 = data.center();
666 SkScalar radius0 = data.radius();
667 SkScalar diffRadius = data.diffRadius();
rileya@google.comd7cc6512012-07-27 14:00:39 +0000668
669 if (fCachedCenter != centerX1 ||
670 fCachedRadius != radius0 ||
671 fCachedDiffRadius != diffRadius) {
672
bsalomon@google.com81712882012-11-01 17:12:34 +0000673 SkScalar a = SkScalarMul(centerX1, centerX1) - diffRadius * diffRadius;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000674
675 // When we're in the degenerate (linear) case, the second
676 // value will be INF but the program doesn't read it. (We
677 // use the same 6 uniforms even though we don't need them
678 // all in the linear case just to keep the code complexity
679 // down).
680 float values[6] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000681 SkScalarToFloat(a * 4),
682 1.f / (SkScalarToFloat(a)),
683 SkScalarToFloat(centerX1),
684 SkScalarToFloat(radius0),
685 SkScalarToFloat(SkScalarMul(radius0, radius0)),
686 SkScalarToFloat(diffRadius)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000687 };
688
689 uman.set1fv(fVSParamUni, 0, 6, values);
690 uman.set1fv(fFSParamUni, 0, 6, values);
691 fCachedCenter = centerX1;
692 fCachedRadius = radius0;
693 fCachedDiffRadius = diffRadius;
694 }
695}
696
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000697GrGLEffect::EffectKey GrGLConical2Gradient::GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000698 enum {
699 kIsDegenerate = 1 << kMatrixKeyBitCnt,
700 };
701
702 EffectKey key = GenMatrixKey(s);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000703 if (GetEffectFromStage<GrConical2Gradient>(s).isDegenerate()) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000704 key |= kIsDegenerate;
705 }
706 return key;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000707}
rileya@google.comd7cc6512012-07-27 14:00:39 +0000708
709/////////////////////////////////////////////////////////////////////
710
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000711GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&) const {
bsalomon@google.com00835cc2013-01-14 17:07:22 +0000712 SkASSERT(NULL != context);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000713 SkASSERT(fPtsToUnit.isIdentity());
714 // invert the localM, translate to center1, rotate so center2 is on x axis.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000715 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000716 if (!this->getLocalMatrix().invert(&matrix)) {
humper@google.com84831ac2013-01-14 22:09:54 +0000717 return NULL;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000718 }
719 matrix.postTranslate(-fCenter1.fX, -fCenter1.fY);
720
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000721 SkPoint diff = fCenter2 - fCenter1;
722 SkScalar diffLen = diff.length();
723 if (0 != diffLen) {
724 SkScalar invDiffLen = SkScalarInvert(diffLen);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000725 SkMatrix rot;
726 rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
727 SkScalarMul(invDiffLen, diff.fX));
728 matrix.postConcat(rot);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000729 }
730
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000731 return GrConical2Gradient::Create(context, *this, matrix, fTileMode);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000732}
733
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000734#else
735
bsalomon@google.com5d2cd202013-01-16 15:31:06 +0000736GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext*, const SkPaint&) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000737 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000738 return NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000739}
740
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000741#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000742
743#ifdef SK_DEVELOPER
744void SkTwoPointConicalGradient::toString(SkString* str) const {
745 str->append("SkTwoPointConicalGradient: (");
746
747 str->append("center1: (");
748 str->appendScalar(fCenter1.fX);
749 str->append(", ");
750 str->appendScalar(fCenter1.fY);
751 str->append(") radius1: ");
752 str->appendScalar(fRadius1);
753 str->append(" ");
754
755 str->append("center2: (");
756 str->appendScalar(fCenter2.fX);
757 str->append(", ");
758 str->appendScalar(fCenter2.fY);
759 str->append(") radius2: ");
760 str->appendScalar(fRadius2);
761 str->append(" ");
762
763 this->INHERITED::toString(str);
764
765 str->append(")");
766}
767#endif