blob: 19b1228c88ed480b5ce9db4a34451c4abcac80e4 [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
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);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000187
rileya@google.com589708b2012-07-26 20:04:23 +0000188 SkPMColor* SK_RESTRICT dstC = dstCParam;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000189
rileya@google.com589708b2012-07-26 20:04:23 +0000190 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
bsalomon@google.com100abf42012-09-05 17:40:04 +0000191
rileya@google.com589708b2012-07-26 20:04:23 +0000192 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000202
rileya@google.com589708b2012-07-26 20:04:23 +0000203 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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209
rileya@google.com589708b2012-07-26 20:04:23 +0000210 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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000230
rileya@google.com589708b2012-07-26 20:04:23 +0000231 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000243
rileya@google.com589708b2012-07-26 20:04:23 +0000244 // we don't have a span16 proc
245 fFlags &= ~kHasSpan16_Flag;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000246
rileya@google.com589708b2012-07-26 20:04:23 +0000247 // 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;
rileya@google.com589708b2012-07-26 20:04:23 +0000257 SkScalar diffLen = 0;
258
259 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000260 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000261 }
262 if (matrix) {
263 diffLen = diff.length();
264 }
265 if (matrix) {
266 if (diffLen) {
267 SkScalar invDiffLen = SkScalarInvert(diffLen);
268 // rotate to align circle centers with the x-axis
269 matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
270 SkScalarMul(invDiffLen, diff.fX));
271 } else {
272 matrix->reset();
273 }
274 matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
275 }
276 if (xy) {
277 xy[0] = fTileMode;
278 xy[1] = kClamp_TileMode;
279 }
280 return kTwoPointConical_BitmapType;
281}
282
283SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
284 GradientInfo* info) const {
285 if (info) {
286 commonAsAGradient(info);
287 info->fPoint[0] = fCenter1;
288 info->fPoint[1] = fCenter2;
289 info->fRadius[0] = fRadius1;
290 info->fRadius[1] = fRadius2;
291 }
292 return kConical_GradientType;
293}
294
rileya@google.com589708b2012-07-26 20:04:23 +0000295SkTwoPointConicalGradient::SkTwoPointConicalGradient(
296 SkFlattenableReadBuffer& buffer)
297 : INHERITED(buffer),
298 fCenter1(buffer.readPoint()),
299 fCenter2(buffer.readPoint()),
300 fRadius1(buffer.readScalar()),
301 fRadius2(buffer.readScalar()) {
302 this->init();
303};
304
305void SkTwoPointConicalGradient::flatten(
306 SkFlattenableWriteBuffer& buffer) const {
307 this->INHERITED::flatten(buffer);
308 buffer.writePoint(fCenter1);
309 buffer.writePoint(fCenter2);
310 buffer.writeScalar(fRadius1);
311 buffer.writeScalar(fRadius2);
312}
313
rileya@google.comd7cc6512012-07-27 14:00:39 +0000314/////////////////////////////////////////////////////////////////////
315
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000316#if SK_SUPPORT_GPU
317
rileya@google.comd7cc6512012-07-27 14:00:39 +0000318// For brevity
319typedef GrGLUniformManager::UniformHandle UniformHandle;
320static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
321
322class GrGLConical2Gradient : public GrGLGradientStage {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000323public:
324
325 GrGLConical2Gradient(const GrProgramStageFactory& factory,
326 const GrCustomStage&);
327 virtual ~GrGLConical2Gradient() { }
328
329 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
330 virtual void emitVS(GrGLShaderBuilder* builder,
331 const char* vertexCoords) SK_OVERRIDE;
332 virtual void emitFS(GrGLShaderBuilder* builder,
333 const char* outputColor,
334 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000335 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.comd3353642012-10-22 20:36:53 +0000336 virtual void setData(const GrGLUniformManager&,
337 const GrCustomStage&,
338 const GrRenderTarget*,
339 int stageNum) SK_OVERRIDE;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000340
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000341 static StageKey GenKey(const GrCustomStage& s, const GrGLCaps& caps);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000342
343protected:
344
345 UniformHandle fVSParamUni;
346 UniformHandle fFSParamUni;
347
348 const char* fVSVaryingName;
349 const char* fFSVaryingName;
350
351 bool fIsDegenerate;
352
353 // @{
354 /// Values last uploaded as uniforms
355
356 GrScalar fCachedCenter;
357 GrScalar fCachedRadius;
358 GrScalar fCachedDiffRadius;
359
360 // @}
361
362private:
363
364 typedef GrGLGradientStage INHERITED;
365
366};
367
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000368/////////////////////////////////////////////////////////////////////
369
370class GrConical2Gradient : public GrGradientEffect {
371public:
372
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000373 GrConical2Gradient(GrContext* ctx,
374 const SkTwoPointConicalGradient& shader,
375 SkShader::TileMode tm)
376 : INHERITED(ctx, shader, tm)
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000377 , fCenterX1(shader.getCenterX1())
378 , fRadius0(shader.getStartRadius())
379 , fDiffRadius(shader.getDiffRadius()) { }
380
381 virtual ~GrConical2Gradient() { }
382
383 static const char* Name() { return "Two-Point Conical Gradient"; }
384 virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE {
385 return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
386 }
387 virtual bool isEqual(const GrCustomStage& sBase) const SK_OVERRIDE {
388 const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
389 return (INHERITED::isEqual(sBase) &&
390 this->fCenterX1 == s.fCenterX1 &&
391 this->fRadius0 == s.fRadius0 &&
392 this->fDiffRadius == s.fDiffRadius);
393 }
394
395 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
396 bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
397 GrScalar center() const { return fCenterX1; }
398 GrScalar diffRadius() const { return fDiffRadius; }
399 GrScalar radius() const { return fRadius0; }
400
401 typedef GrGLConical2Gradient GLProgramStage;
402
403private:
bsalomon@google.comd4726202012-08-03 14:34:46 +0000404 GR_DECLARE_CUSTOM_STAGE_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000405
406 // @{
407 // Cache of values - these can change arbitrarily, EXCEPT
408 // we shouldn't change between degenerate and non-degenerate?!
409
410 GrScalar fCenterX1;
411 GrScalar fRadius0;
412 GrScalar fDiffRadius;
413
414 // @}
415
416 typedef GrGradientEffect INHERITED;
417};
418
bsalomon@google.comd4726202012-08-03 14:34:46 +0000419GR_DEFINE_CUSTOM_STAGE_TEST(GrConical2Gradient);
420
421GrCustomStage* GrConical2Gradient::TestCreate(SkRandom* random,
422 GrContext* context,
423 GrTexture**) {
424 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
425 SkScalar radius1 = random->nextUScalar1();
426 SkPoint center2;
427 SkScalar radius2;
428 do {
429 center1.set(random->nextUScalar1(), random->nextUScalar1());
430 radius2 = random->nextUScalar1 ();
431 // If the circles are identical the factory will give us an empty shader.
432 } while (radius1 == radius2 && center1 == center2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000433
bsalomon@google.comd4726202012-08-03 14:34:46 +0000434 SkColor colors[kMaxRandomGradientColors];
435 SkScalar stopsArray[kMaxRandomGradientColors];
436 SkScalar* stops = stopsArray;
437 SkShader::TileMode tm;
438 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
439 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
440 center2, radius2,
441 colors, stops, colorCount,
442 tm));
443 GrSamplerState sampler;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000444 shader->asNewCustomStage(context, &sampler);
445 GrAssert(NULL != sampler.getCustomStage());
446 // const_cast and ref is a hack! Will remove when asNewCustomStage returns GrCustomStage*
447 sampler.getCustomStage()->ref();
448 return const_cast<GrCustomStage*>(sampler.getCustomStage());
bsalomon@google.comd4726202012-08-03 14:34:46 +0000449}
450
451
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000452/////////////////////////////////////////////////////////////////////
453
rileya@google.comd7cc6512012-07-27 14:00:39 +0000454GrGLConical2Gradient::GrGLConical2Gradient(
455 const GrProgramStageFactory& factory,
456 const GrCustomStage& baseData)
457 : INHERITED(factory)
458 , fVSParamUni(kInvalidUniformHandle)
459 , fFSParamUni(kInvalidUniformHandle)
460 , fVSVaryingName(NULL)
461 , fFSVaryingName(NULL)
462 , fCachedCenter(GR_ScalarMax)
463 , fCachedRadius(-GR_ScalarMax)
464 , fCachedDiffRadius(-GR_ScalarMax) {
465
466 const GrConical2Gradient& data =
467 static_cast<const GrConical2Gradient&>(baseData);
468 fIsDegenerate = data.isDegenerate();
469}
470
471void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder) {
rileya@google.comb3e50f22012-08-20 17:43:08 +0000472 INHERITED::setupVariables(builder);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000473 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
474 // to work around Xoom bug. Doesn't seem to cause performance decrease
475 // in test apps, but need to keep an eye on it.
476 fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType,
477 kFloat_GrSLType, "Conical2VSParams", 6);
478 fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
479 kFloat_GrSLType, "Conical2FSParams", 6);
480
481 // For radial gradients without perspective we can pass the linear
482 // part of the quadratic as a varying.
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000483 if (!builder->defaultTextureMatrixIsPerspective()) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000484 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff",
485 &fVSVaryingName, &fFSVaryingName);
486 }
487}
488
489void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* builder,
490 const char* vertexCoords) {
491 SkString* code = &builder->fVSCode;
492 SkString p2; // distance between centers
493 SkString p3; // start radius
494 SkString p5; // difference in radii (r1 - r0)
495 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
496 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
497 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
498
499 // For radial gradients without perspective we can pass the linear
500 // part of the quadratic as a varying.
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000501 if (!builder->defaultTextureMatrixIsPerspective()) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000502 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
503 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
504 fVSVaryingName, p2.c_str(),
505 vertexCoords, p3.c_str(), p5.c_str());
506 }
507}
508
509void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* builder,
510 const char* outputColor,
511 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000512 const TextureSamplerArray& samplers) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000513 SkString* code = &builder->fFSCode;
514
515 SkString cName("c");
516 SkString ac4Name("ac4");
517 SkString dName("d");
518 SkString qName("q");
519 SkString r0Name("r0");
520 SkString r1Name("r1");
521 SkString tName("t");
522 SkString p0; // 4a
523 SkString p1; // 1/a
524 SkString p2; // distance between centers
525 SkString p3; // start radius
526 SkString p4; // start radius squared
527 SkString p5; // difference in radii (r1 - r0)
528
529 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
530 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
531 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
532 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
533 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
534 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
535
536 // If we we're able to interpolate the linear component,
537 // bVar is the varying; otherwise compute it
538 SkString bVar;
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000539 if (!builder->defaultTextureMatrixIsPerspective()) {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000540 bVar = fFSVaryingName;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000541 } else {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000542 bVar = "b";
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000543 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000544 bVar.c_str(), p2.c_str(), builder->defaultTexCoordsName(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000545 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]
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000553 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000554 builder->defaultTexCoordsName(), builder->defaultTexCoordsName(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000555 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());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000563
rileya@google.comd7cc6512012-07-27 14:00:39 +0000564 // d = b^2 - ac4
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000565 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000566 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)"
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000574 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000575 bVar.c_str(), dName.c_str());
576
577 // compute both roots
578 // r0 = q * params[1]
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000579 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000580 qName.c_str(), p1.c_str());
581 // r1 = c / q
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000582 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000583 cName.c_str(), qName.c_str());
584
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000585 // Note: If there are two roots that both generate radius(t) > 0, the
rileya@google.comd7cc6512012-07-27 14:00:39 +0000586 // Canvas spec says to choose the larger t.
587
588 // so we'll look at the larger one first:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000589 code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000590 r0Name.c_str(), r1Name.c_str());
591
592 // if r(t) > 0, then we're done; t will be our x coordinate
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000593 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000594 p5.c_str(), p3.c_str());
595
596 code->appendf("\t\t");
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000597 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000598
599 // otherwise, if r(t) for the larger root was <= 0, try the other root
600 code->appendf("\t\t} else {\n");
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000601 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000602 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");
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000609 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000610
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000611 // end if (r(t) > 0) for smaller root
rileya@google.comd7cc6512012-07-27 14:00:39 +0000612 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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
rileya@google.comd7cc6512012-07-27 14:00:39 +0000621 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");
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000627 this->emitColorLookup(builder, tName.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000628 code->appendf("\t}\n");
629 }
630}
631
bsalomon@google.comd3353642012-10-22 20:36:53 +0000632void GrGLConical2Gradient::setData(const GrGLUniformManager& uman,
633 const GrCustomStage& baseData,
634 const GrRenderTarget* target,
635 int stageNum) {
636 INHERITED::setData(uman, baseData, target, stageNum);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000637 const GrConical2Gradient& data =
638 static_cast<const GrConical2Gradient&>(baseData);
639 GrAssert(data.isDegenerate() == fIsDegenerate);
640 GrScalar centerX1 = data.center();
641 GrScalar radius0 = data.radius();
642 GrScalar diffRadius = data.diffRadius();
643
644 if (fCachedCenter != centerX1 ||
645 fCachedRadius != radius0 ||
646 fCachedDiffRadius != diffRadius) {
647
648 GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
649
650 // When we're in the degenerate (linear) case, the second
651 // value will be INF but the program doesn't read it. (We
652 // use the same 6 uniforms even though we don't need them
653 // all in the linear case just to keep the code complexity
654 // down).
655 float values[6] = {
656 GrScalarToFloat(a * 4),
657 1.f / (GrScalarToFloat(a)),
658 GrScalarToFloat(centerX1),
659 GrScalarToFloat(radius0),
660 GrScalarToFloat(SkScalarMul(radius0, radius0)),
661 GrScalarToFloat(diffRadius)
662 };
663
664 uman.set1fv(fVSParamUni, 0, 6, values);
665 uman.set1fv(fFSParamUni, 0, 6, values);
666 fCachedCenter = centerX1;
667 fCachedRadius = radius0;
668 fCachedDiffRadius = diffRadius;
669 }
670}
671
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000672GrCustomStage::StageKey GrGLConical2Gradient::GenKey(const GrCustomStage& s, const GrGLCaps& caps) {
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000673 return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
674}
rileya@google.comd7cc6512012-07-27 14:00:39 +0000675
676/////////////////////////////////////////////////////////////////////
677
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000678bool SkTwoPointConicalGradient::asNewCustomStage(GrContext* context,
679 GrSamplerState* sampler) const {
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000680 SkASSERT(NULL != context && NULL != sampler);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000681
682 SkMatrix matrix;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000683 SkPoint diff = fCenter2 - fCenter1;
684 SkScalar diffLen = diff.length();
685 if (0 != diffLen) {
686 SkScalar invDiffLen = SkScalarInvert(diffLen);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000687 matrix.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
688 SkScalarMul(invDiffLen, diff.fX));
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000689 } else {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000690 matrix.reset();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000691 }
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000692 matrix.preTranslate(-fCenter1.fX, -fCenter1.fY);
693
694 SkMatrix localM;
695 if (this->getLocalMatrix(&localM)) {
696 if (!localM.invert(&localM)) {
697 return false;
698 }
699 matrix.preConcat(localM);
700 }
701
702 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient, (context, *this, fTileMode)), matrix)->unref();
703
704 return true;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000705}
706
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000707#else
708
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000709bool SkTwoPointConicalGradient::asNewCustomStage(GrContext*, GrSamplerState*) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000710 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000711 return false;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000712}
713
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000714#endif