blob: 528291bdc1d5b79237bb7c1ab32896604bd6afd8 [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);
24
25 if (A == 0) {
26 return valid_divide(-C, B, roots);
27 }
28
29 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];
90
91 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
116typedef void (*TwoPointRadialProc)(TwoPtRadial* rec, SkPMColor* dstC,
117 const SkPMColor* cache, int count);
118
119static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
120 const SkPMColor* SK_RESTRICT cache, int count) {
121 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);
128 *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift];
129 }
130 }
131}
132
133static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
134 const SkPMColor* SK_RESTRICT cache, int count) {
135 for (; count > 0; --count) {
136 SkFixed t = rec->nextT();
137 if (TwoPtRadial::DontDrawT(t)) {
138 *dstC++ = 0;
139 } else {
140 SkFixed index = repeat_tileproc(t);
141 SkASSERT(index <= 0xFFFF);
142 *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift];
143 }
144 }
145}
146
147static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC,
148 const SkPMColor* SK_RESTRICT cache, int count) {
149 for (; count > 0; --count) {
150 SkFixed t = rec->nextT();
151 if (TwoPtRadial::DontDrawT(t)) {
152 *dstC++ = 0;
153 } else {
154 SkFixed index = mirror_tileproc(t);
155 SkASSERT(index <= 0xFFFF);
156 *dstC++ = cache[index >> SkGradientShaderBase::kCache32Shift];
157 }
158 }
159}
160
161void SkTwoPointConicalGradient::init() {
162 fRec.init(fCenter1, fRadius1, fCenter2, fRadius2);
163 fPtsToUnit.reset();
164}
165
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000166/////////////////////////////////////////////////////////////////////
167
rileya@google.com589708b2012-07-26 20:04:23 +0000168SkTwoPointConicalGradient::SkTwoPointConicalGradient(
169 const SkPoint& start, SkScalar startRadius,
170 const SkPoint& end, SkScalar endRadius,
171 const SkColor colors[], const SkScalar pos[],
172 int colorCount, SkShader::TileMode mode,
173 SkUnitMapper* mapper)
174 : SkGradientShaderBase(colors, pos, colorCount, mode, mapper),
175 fCenter1(start),
176 fCenter2(end),
177 fRadius1(startRadius),
178 fRadius2(endRadius) {
179 // this is degenerate, and should be caught by our caller
180 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
181 this->init();
182}
183
184void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam,
185 int count) {
186 SkASSERT(count > 0);
187
188 SkPMColor* SK_RESTRICT dstC = dstCParam;
189
190 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
191 TileProc proc = fTileProc;
192 const SkPMColor* SK_RESTRICT cache = this->getCache32();
193
194 TwoPointRadialProc shadeProc = twopoint_repeat;
195 if (SkShader::kClamp_TileMode == fTileMode) {
196 shadeProc = twopoint_clamp;
197 } else if (SkShader::kMirror_TileMode == fTileMode) {
198 shadeProc = twopoint_mirror;
199 } else {
200 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
201 }
202
203 if (fDstToIndexClass != kPerspective_MatrixClass) {
204 SkPoint srcPt;
205 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
206 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
207 SkScalar dx, fx = srcPt.fX;
208 SkScalar dy, fy = srcPt.fY;
209
210 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
211 SkFixed fixedX, fixedY;
212 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY);
213 dx = SkFixedToScalar(fixedX);
214 dy = SkFixedToScalar(fixedY);
215 } else {
216 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
217 dx = fDstToIndex.getScaleX();
218 dy = fDstToIndex.getSkewY();
219 }
220
221 fRec.setup(fx, fy, dx, dy);
222 (*shadeProc)(&fRec, dstC, cache, count);
223 } else { // perspective case
224 SkScalar dstX = SkIntToScalar(x);
225 SkScalar dstY = SkIntToScalar(y);
226 for (; count > 0; --count) {
227 SkPoint srcPt;
228 dstProc(fDstToIndex, dstX, dstY, &srcPt);
229 dstX += SK_Scalar1;
230
231 fRec.setup(srcPt.fX, srcPt.fY, 0, 0);
232 (*shadeProc)(&fRec, dstC, cache, 1);
233 }
234 }
235}
236
237bool SkTwoPointConicalGradient::setContext(const SkBitmap& device,
238 const SkPaint& paint,
239 const SkMatrix& matrix) {
240 if (!this->INHERITED::setContext(device, paint, matrix)) {
241 return false;
242 }
243
244 // we don't have a span16 proc
245 fFlags &= ~kHasSpan16_Flag;
246
247 // in general, we might discard based on computed-radius, so clear
248 // this flag (todo: sometimes we can detect that we never discard...)
249 fFlags &= ~kOpaqueAlpha_Flag;
250
251 return true;
252}
253
254SkShader::BitmapType SkTwoPointConicalGradient::asABitmap(
255 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const {
256 SkPoint diff = fCenter2 - fCenter1;
257 SkScalar diffRadius = fRadius2 - fRadius1;
258 SkScalar startRadius = fRadius1;
259 SkScalar diffLen = 0;
260
261 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000262 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000263 }
264 if (matrix) {
265 diffLen = diff.length();
266 }
267 if (matrix) {
268 if (diffLen) {
269 SkScalar invDiffLen = SkScalarInvert(diffLen);
270 // rotate to align circle centers with the x-axis
271 matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
272 SkScalarMul(invDiffLen, diff.fX));
273 } else {
274 matrix->reset();
275 }
276 matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
277 }
278 if (xy) {
279 xy[0] = fTileMode;
280 xy[1] = kClamp_TileMode;
281 }
282 return kTwoPointConical_BitmapType;
283}
284
285SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
286 GradientInfo* info) const {
287 if (info) {
288 commonAsAGradient(info);
289 info->fPoint[0] = fCenter1;
290 info->fPoint[1] = fCenter2;
291 info->fRadius[0] = fRadius1;
292 info->fRadius[1] = fRadius2;
293 }
294 return kConical_GradientType;
295}
296
rileya@google.com589708b2012-07-26 20:04:23 +0000297SkTwoPointConicalGradient::SkTwoPointConicalGradient(
298 SkFlattenableReadBuffer& buffer)
299 : INHERITED(buffer),
300 fCenter1(buffer.readPoint()),
301 fCenter2(buffer.readPoint()),
302 fRadius1(buffer.readScalar()),
303 fRadius2(buffer.readScalar()) {
304 this->init();
305};
306
307void SkTwoPointConicalGradient::flatten(
308 SkFlattenableWriteBuffer& buffer) const {
309 this->INHERITED::flatten(buffer);
310 buffer.writePoint(fCenter1);
311 buffer.writePoint(fCenter2);
312 buffer.writeScalar(fRadius1);
313 buffer.writeScalar(fRadius2);
314}
315
rileya@google.comd7cc6512012-07-27 14:00:39 +0000316/////////////////////////////////////////////////////////////////////
317
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000318#if SK_SUPPORT_GPU
319
rileya@google.comd7cc6512012-07-27 14:00:39 +0000320// For brevity
321typedef GrGLUniformManager::UniformHandle UniformHandle;
322static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
323
324class GrGLConical2Gradient : public GrGLGradientStage {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000325public:
326
327 GrGLConical2Gradient(const GrProgramStageFactory& factory,
328 const GrCustomStage&);
329 virtual ~GrGLConical2Gradient() { }
330
331 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
332 virtual void emitVS(GrGLShaderBuilder* builder,
333 const char* vertexCoords) SK_OVERRIDE;
334 virtual void emitFS(GrGLShaderBuilder* builder,
335 const char* outputColor,
336 const char* inputColor,
337 const char* samplerName) SK_OVERRIDE;
338 virtual void setData(const GrGLUniformManager&,
339 const GrCustomStage&,
340 const GrRenderTarget*,
341 int stageNum) SK_OVERRIDE;
342
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000343 static StageKey GenKey(const GrCustomStage& s, const GrGLCaps& caps);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000344
345protected:
346
347 UniformHandle fVSParamUni;
348 UniformHandle fFSParamUni;
349
350 const char* fVSVaryingName;
351 const char* fFSVaryingName;
352
353 bool fIsDegenerate;
354
355 // @{
356 /// Values last uploaded as uniforms
357
358 GrScalar fCachedCenter;
359 GrScalar fCachedRadius;
360 GrScalar fCachedDiffRadius;
361
362 // @}
363
364private:
365
366 typedef GrGLGradientStage INHERITED;
367
368};
369
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000370/////////////////////////////////////////////////////////////////////
371
372class GrConical2Gradient : public GrGradientEffect {
373public:
374
375 GrConical2Gradient(GrContext* ctx, const SkTwoPointConicalGradient& shader,
376 GrSamplerState* sampler)
377 : INHERITED(ctx, shader, sampler)
378 , fCenterX1(shader.getCenterX1())
379 , fRadius0(shader.getStartRadius())
380 , fDiffRadius(shader.getDiffRadius()) { }
381
382 virtual ~GrConical2Gradient() { }
383
384 static const char* Name() { return "Two-Point Conical Gradient"; }
385 virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE {
386 return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
387 }
388 virtual bool isEqual(const GrCustomStage& sBase) const SK_OVERRIDE {
389 const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
390 return (INHERITED::isEqual(sBase) &&
391 this->fCenterX1 == s.fCenterX1 &&
392 this->fRadius0 == s.fRadius0 &&
393 this->fDiffRadius == s.fDiffRadius);
394 }
395
396 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
397 bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
398 GrScalar center() const { return fCenterX1; }
399 GrScalar diffRadius() const { return fDiffRadius; }
400 GrScalar radius() const { return fRadius0; }
401
402 typedef GrGLConical2Gradient GLProgramStage;
403
404private:
bsalomon@google.comd4726202012-08-03 14:34:46 +0000405 GR_DECLARE_CUSTOM_STAGE_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000406
407 // @{
408 // Cache of values - these can change arbitrarily, EXCEPT
409 // we shouldn't change between degenerate and non-degenerate?!
410
411 GrScalar fCenterX1;
412 GrScalar fRadius0;
413 GrScalar fDiffRadius;
414
415 // @}
416
417 typedef GrGradientEffect INHERITED;
418};
419
bsalomon@google.comd4726202012-08-03 14:34:46 +0000420GR_DEFINE_CUSTOM_STAGE_TEST(GrConical2Gradient);
421
422GrCustomStage* GrConical2Gradient::TestCreate(SkRandom* random,
423 GrContext* context,
424 GrTexture**) {
425 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
426 SkScalar radius1 = random->nextUScalar1();
427 SkPoint center2;
428 SkScalar radius2;
429 do {
430 center1.set(random->nextUScalar1(), random->nextUScalar1());
431 radius2 = random->nextUScalar1 ();
432 // If the circles are identical the factory will give us an empty shader.
433 } while (radius1 == radius2 && center1 == center2);
434
435 SkColor colors[kMaxRandomGradientColors];
436 SkScalar stopsArray[kMaxRandomGradientColors];
437 SkScalar* stops = stopsArray;
438 SkShader::TileMode tm;
439 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
440 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
441 center2, radius2,
442 colors, stops, colorCount,
443 tm));
444 GrSamplerState sampler;
445 GrCustomStage* stage = shader->asNewCustomStage(context, &sampler);
446 GrAssert(NULL != stage);
447 return stage;
448}
449
450
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000451/////////////////////////////////////////////////////////////////////
452
rileya@google.comd7cc6512012-07-27 14:00:39 +0000453GrGLConical2Gradient::GrGLConical2Gradient(
454 const GrProgramStageFactory& factory,
455 const GrCustomStage& baseData)
456 : INHERITED(factory)
457 , fVSParamUni(kInvalidUniformHandle)
458 , fFSParamUni(kInvalidUniformHandle)
459 , fVSVaryingName(NULL)
460 , fFSVaryingName(NULL)
461 , fCachedCenter(GR_ScalarMax)
462 , fCachedRadius(-GR_ScalarMax)
463 , fCachedDiffRadius(-GR_ScalarMax) {
464
465 const GrConical2Gradient& data =
466 static_cast<const GrConical2Gradient&>(baseData);
467 fIsDegenerate = data.isDegenerate();
468}
469
470void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder) {
471 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
472 // to work around Xoom bug. Doesn't seem to cause performance decrease
473 // in test apps, but need to keep an eye on it.
474 fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
475 kFloat_GrSLType, "Conical2VSParams", 6);
476 fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
477 kFloat_GrSLType, "Conical2FSParams", 6);
478
479 // For radial gradients without perspective we can pass the linear
480 // part of the quadratic as a varying.
481 if (builder->fVaryingDims == builder->fCoordDims) {
482 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
483 &fVSVaryingName, &fFSVaryingName);
484 }
485}
486
487void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* builder,
488 const char* vertexCoords) {
489 SkString* code = &builder->fVSCode;
490 SkString p2; // distance between centers
491 SkString p3; // start radius
492 SkString p5; // difference in radii (r1 - r0)
493 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
494 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
495 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
496
497 // For radial gradients without perspective we can pass the linear
498 // part of the quadratic as a varying.
499 if (builder->fVaryingDims == builder->fCoordDims) {
500 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
501 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
502 fVSVaryingName, p2.c_str(),
503 vertexCoords, p3.c_str(), p5.c_str());
504 }
505}
506
507void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* builder,
508 const char* outputColor,
509 const char* inputColor,
510 const char* samplerName) {
511 SkString* code = &builder->fFSCode;
512
513 SkString cName("c");
514 SkString ac4Name("ac4");
515 SkString dName("d");
516 SkString qName("q");
517 SkString r0Name("r0");
518 SkString r1Name("r1");
519 SkString tName("t");
520 SkString p0; // 4a
521 SkString p1; // 1/a
522 SkString p2; // distance between centers
523 SkString p3; // start radius
524 SkString p4; // start radius squared
525 SkString p5; // difference in radii (r1 - r0)
526
527 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
528 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
529 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
530 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
531 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
532 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
533
534 // If we we're able to interpolate the linear component,
535 // bVar is the varying; otherwise compute it
536 SkString bVar;
537 if (builder->fCoordDims == builder->fVaryingDims) {
538 bVar = fFSVaryingName;
539 GrAssert(2 == builder->fVaryingDims);
540 } else {
541 GrAssert(3 == builder->fVaryingDims);
542 bVar = "b";
543 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
544 bVar.c_str(), p2.c_str(), builder->fSampleCoords.c_str(),
545 p3.c_str(), p5.c_str());
546 }
547
548 // output will default to transparent black (we simply won't write anything
549 // else to it if invalid, instead of discarding or returning prematurely)
550 code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
551
552 // c = (x^2)+(y^2) - params[4]
553 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
554 builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str(),
555 p4.c_str());
556
557 // Non-degenerate case (quadratic)
558 if (!fIsDegenerate) {
559
560 // ac4 = params[0] * c
561 code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
562 cName.c_str());
563
564 // d = b^2 - ac4
565 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
566 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
567
568 // only proceed if discriminant is >= 0
569 code->appendf("\tif (%s >= 0.0) {\n", dName.c_str());
570
571 // intermediate value we'll use to compute the roots
572 // q = -0.5 * (b +/- sqrt(d))
573 code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
574 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
575 bVar.c_str(), dName.c_str());
576
577 // compute both roots
578 // r0 = q * params[1]
579 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
580 qName.c_str(), p1.c_str());
581 // r1 = c / q
582 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
583 cName.c_str(), qName.c_str());
584
585 // Note: If there are two roots that both generate radius(t) > 0, the
586 // Canvas spec says to choose the larger t.
587
588 // so we'll look at the larger one first:
589 code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
590 r0Name.c_str(), r1Name.c_str());
591
592 // if r(t) > 0, then we're done; t will be our x coordinate
593 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
594 p5.c_str(), p3.c_str());
595
596 code->appendf("\t\t");
597 this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName);
598
599 // otherwise, if r(t) for the larger root was <= 0, try the other root
600 code->appendf("\t\t} else {\n");
601 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
602 r0Name.c_str(), r1Name.c_str());
603
604 // if r(t) > 0 for the smaller root, then t will be our x coordinate
605 code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
606 tName.c_str(), p5.c_str(), p3.c_str());
607
608 code->appendf("\t\t\t");
609 this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName);
610
611 // end if (r(t) > 0) for smaller root
612 code->appendf("\t\t\t}\n");
613 // end if (r(t) > 0), else, for larger root
614 code->appendf("\t\t}\n");
615 // end if (discriminant >= 0)
616 code->appendf("\t}\n");
617 } else {
618
619 // linear case: t = -c/b
620 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
621 cName.c_str(), bVar.c_str());
622
623 // if r(t) > 0, then t will be the x coordinate
624 code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
625 p5.c_str(), p3.c_str());
626 code->appendf("\t");
627 this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName);
628 code->appendf("\t}\n");
629 }
630}
631
632void GrGLConical2Gradient::setData(const GrGLUniformManager& uman,
633 const GrCustomStage& baseData,
634 const GrRenderTarget*,
635 int stageNum) {
636 const GrConical2Gradient& data =
637 static_cast<const GrConical2Gradient&>(baseData);
638 GrAssert(data.isDegenerate() == fIsDegenerate);
639 GrScalar centerX1 = data.center();
640 GrScalar radius0 = data.radius();
641 GrScalar diffRadius = data.diffRadius();
642
643 if (fCachedCenter != centerX1 ||
644 fCachedRadius != radius0 ||
645 fCachedDiffRadius != diffRadius) {
646
647 GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
648
649 // When we're in the degenerate (linear) case, the second
650 // value will be INF but the program doesn't read it. (We
651 // use the same 6 uniforms even though we don't need them
652 // all in the linear case just to keep the code complexity
653 // down).
654 float values[6] = {
655 GrScalarToFloat(a * 4),
656 1.f / (GrScalarToFloat(a)),
657 GrScalarToFloat(centerX1),
658 GrScalarToFloat(radius0),
659 GrScalarToFloat(SkScalarMul(radius0, radius0)),
660 GrScalarToFloat(diffRadius)
661 };
662
663 uman.set1fv(fVSParamUni, 0, 6, values);
664 uman.set1fv(fFSParamUni, 0, 6, values);
665 fCachedCenter = centerX1;
666 fCachedRadius = radius0;
667 fCachedDiffRadius = diffRadius;
668 }
669}
670
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000671GrCustomStage::StageKey GrGLConical2Gradient::GenKey(const GrCustomStage& s, const GrGLCaps& caps) {
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000672 return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
673}
rileya@google.comd7cc6512012-07-27 14:00:39 +0000674
675/////////////////////////////////////////////////////////////////////
676
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000677GrCustomStage* SkTwoPointConicalGradient::asNewCustomStage(
678 GrContext* context, GrSamplerState* sampler) const {
679 SkASSERT(NULL != context && NULL != sampler);
680 SkPoint diff = fCenter2 - fCenter1;
681 SkScalar diffLen = diff.length();
682 if (0 != diffLen) {
683 SkScalar invDiffLen = SkScalarInvert(diffLen);
684 sampler->matrix()->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
685 SkScalarMul(invDiffLen, diff.fX));
686 } else {
687 sampler->matrix()->reset();
688 }
689 sampler->matrix()->preTranslate(-fCenter1.fX, -fCenter1.fY);
690 sampler->textureParams()->setTileModeX(fTileMode);
691 sampler->textureParams()->setTileModeY(kClamp_TileMode);
692 sampler->textureParams()->setBilerp(true);
693 return SkNEW_ARGS(GrConical2Gradient, (context, *this, sampler));
rileya@google.comd7cc6512012-07-27 14:00:39 +0000694}
695
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000696#else
697
698GrCustomStage* SkTwoPointConicalGradient::asNewCustomStage(
699 GrContext* context, GrSamplerState* sampler) const {
700 SkDEBUGFAIL("Should not call in GPU-less build");
701 return NULL;
702}
703
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000704#endif