3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkTwoPointConicalGradient_gpu.h"
11 #include "SkTwoPointConicalGradient.h"
14 #include "gl/builders/GrGLProgramBuilder.h"
16 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
18 static const SkScalar kErrorTol = 0.00001f;
19 static const SkScalar kEdgeErrorTol = 5.f * kErrorTol;
22 * We have three general cases for 2pt conical gradients. First we always assume that
23 * the start radius <= end radius. Our first case (kInside_) is when the start circle
24 * is completely enclosed by the end circle. The second case (kOutside_) is the case
25 * when the start circle is either completely outside the end circle or the circles
26 * overlap. The final case (kEdge_) is when the start circle is inside the end one,
27 * but the two are just barely touching at 1 point along their edges.
35 //////////////////////////////////////////////////////////////////////////////
37 static void set_matrix_edge_conical(const SkTwoPointConicalGradient& shader,
38 SkMatrix* invLMatrix) {
39 // Inverse of the current local matrix is passed in then,
40 // translate to center1, rotate so center2 is on x axis.
41 const SkPoint& center1 = shader.getStartCenter();
42 const SkPoint& center2 = shader.getEndCenter();
44 invLMatrix->postTranslate(-center1.fX, -center1.fY);
46 SkPoint diff = center2 - center1;
47 SkScalar diffLen = diff.length();
49 SkScalar invDiffLen = SkScalarInvert(diffLen);
51 rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
52 SkScalarMul(invDiffLen, diff.fX));
53 invLMatrix->postConcat(rot);
57 class Edge2PtConicalEffect : public GrGradientEffect {
60 static GrFragmentProcessor* Create(GrContext* ctx,
61 const SkTwoPointConicalGradient& shader,
62 const SkMatrix& matrix,
63 SkShader::TileMode tm) {
64 return SkNEW_ARGS(Edge2PtConicalEffect, (ctx, shader, matrix, tm));
67 virtual ~Edge2PtConicalEffect() {}
69 const char* name() const SK_OVERRIDE {
70 return "Two-Point Conical Gradient Edge Touching";
73 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
75 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
77 // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
78 SkScalar center() const { return fCenterX1; }
79 SkScalar diffRadius() const { return fDiffRadius; }
80 SkScalar radius() const { return fRadius0; }
83 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
84 const Edge2PtConicalEffect& s = sBase.cast<Edge2PtConicalEffect>();
85 return (INHERITED::onIsEqual(sBase) &&
86 this->fCenterX1 == s.fCenterX1 &&
87 this->fRadius0 == s.fRadius0 &&
88 this->fDiffRadius == s.fDiffRadius);
91 Edge2PtConicalEffect(GrContext* ctx,
92 const SkTwoPointConicalGradient& shader,
93 const SkMatrix& matrix,
94 SkShader::TileMode tm)
95 : INHERITED(ctx, shader, matrix, tm),
96 fCenterX1(shader.getCenterX1()),
97 fRadius0(shader.getStartRadius()),
98 fDiffRadius(shader.getDiffRadius()){
99 this->initClassID<Edge2PtConicalEffect>();
100 // We should only be calling this shader if we are degenerate case with touching circles
101 // When deciding if we are in edge case, we scaled by the end radius for cases when the
102 // start radius was close to zero, otherwise we scaled by the start radius. In addition
103 // Our test for the edge case in set_matrix_circle_conical has a higher tolerance so we
104 // need the sqrt value below
105 SkASSERT(SkScalarAbs(SkScalarAbs(fDiffRadius) - fCenterX1) <
106 (fRadius0 < kErrorTol ? shader.getEndRadius() * kEdgeErrorTol :
107 fRadius0 * sqrt(kEdgeErrorTol)));
109 // We pass the linear part of the quadratic as a varying.
110 // float b = -2.0 * (fCenterX1 * x + fRadius0 * fDiffRadius * z)
111 fBTransform = this->getCoordTransform();
112 SkMatrix& bMatrix = *fBTransform.accessMatrix();
113 SkScalar r0dr = SkScalarMul(fRadius0, fDiffRadius);
114 bMatrix[SkMatrix::kMScaleX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMScaleX]) +
115 SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp0]));
116 bMatrix[SkMatrix::kMSkewX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMSkewX]) +
117 SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp1]));
118 bMatrix[SkMatrix::kMTransX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMTransX]) +
119 SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp2]));
120 this->addCoordTransform(&fBTransform);
123 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
126 // Cache of values - these can change arbitrarily, EXCEPT
127 // we shouldn't change between degenerate and non-degenerate?!
129 GrCoordTransform fBTransform;
132 SkScalar fDiffRadius;
136 typedef GrGradientEffect INHERITED;
139 class GLEdge2PtConicalEffect : public GrGLGradientEffect {
141 GLEdge2PtConicalEffect(const GrProcessor&);
142 virtual ~GLEdge2PtConicalEffect() { }
144 virtual void emitCode(GrGLFPBuilder*,
145 const GrFragmentProcessor&,
146 const char* outputColor,
147 const char* inputColor,
148 const TransformedCoordsArray&,
149 const TextureSamplerArray&) SK_OVERRIDE;
150 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
152 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyBuilder* b);
155 UniformHandle fParamUni;
157 const char* fVSVaryingName;
158 const char* fFSVaryingName;
161 /// Values last uploaded as uniforms
163 SkScalar fCachedRadius;
164 SkScalar fCachedDiffRadius;
169 typedef GrGLGradientEffect INHERITED;
173 void Edge2PtConicalEffect::getGLProcessorKey(const GrGLCaps& caps,
174 GrProcessorKeyBuilder* b) const {
175 GLEdge2PtConicalEffect::GenKey(*this, caps, b);
178 GrGLFragmentProcessor* Edge2PtConicalEffect::createGLInstance() const {
179 return SkNEW_ARGS(GLEdge2PtConicalEffect, (*this));
182 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Edge2PtConicalEffect);
185 * All Two point conical gradient test create functions may occasionally create edge case shaders
187 GrFragmentProcessor* Edge2PtConicalEffect::TestCreate(SkRandom* random,
189 const GrDrawTargetCaps&,
191 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
192 SkScalar radius1 = random->nextUScalar1();
196 center2.set(random->nextUScalar1(), random->nextUScalar1());
197 // If the circles are identical the factory will give us an empty shader.
198 // This will happen if we pick identical centers
199 } while (center1 == center2);
201 // Below makes sure that circle one is contained within circle two
202 // and both circles are touching on an edge
203 SkPoint diff = center2 - center1;
204 SkScalar diffLen = diff.length();
205 radius2 = radius1 + diffLen;
207 SkColor colors[kMaxRandomGradientColors];
208 SkScalar stopsArray[kMaxRandomGradientColors];
209 SkScalar* stops = stopsArray;
210 SkShader::TileMode tm;
211 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
212 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
214 colors, stops, colorCount,
217 GrFragmentProcessor* fp;
219 SkAssertResult(shader->asFragmentProcessor(context, paint,
220 GrProcessorUnitTest::TestMatrix(random), NULL,
225 GLEdge2PtConicalEffect::GLEdge2PtConicalEffect(const GrProcessor&)
226 : fVSVaryingName(NULL)
227 , fFSVaryingName(NULL)
228 , fCachedRadius(-SK_ScalarMax)
229 , fCachedDiffRadius(-SK_ScalarMax) {}
231 void GLEdge2PtConicalEffect::emitCode(GrGLFPBuilder* builder,
232 const GrFragmentProcessor& fp,
233 const char* outputColor,
234 const char* inputColor,
235 const TransformedCoordsArray& coords,
236 const TextureSamplerArray& samplers) {
237 const Edge2PtConicalEffect& ge = fp.cast<Edge2PtConicalEffect>();
238 this->emitUniforms(builder, ge);
239 fParamUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
240 kFloat_GrSLType, kDefault_GrSLPrecision,
241 "Conical2FSParams", 3);
245 SkString p0; // start radius
246 SkString p1; // start radius squared
247 SkString p2; // difference in radii (r1 - r0)
249 builder->getUniformVariable(fParamUni).appendArrayAccess(0, &p0);
250 builder->getUniformVariable(fParamUni).appendArrayAccess(1, &p1);
251 builder->getUniformVariable(fParamUni).appendArrayAccess(2, &p2);
253 // We interpolate the linear component in coords[1].
254 SkASSERT(coords[0].getType() == coords[1].getType());
255 const char* coords2D;
257 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
258 if (kVec3f_GrSLType == coords[0].getType()) {
259 fsBuilder->codeAppendf("\tvec3 interpolants = vec3(%s.xy / %s.z, %s.x / %s.z);\n",
260 coords[0].c_str(), coords[0].c_str(), coords[1].c_str(),
262 coords2D = "interpolants.xy";
263 bVar = "interpolants.z";
265 coords2D = coords[0].c_str();
266 bVar.printf("%s.x", coords[1].c_str());
269 // output will default to transparent black (we simply won't write anything
270 // else to it if invalid, instead of discarding or returning prematurely)
271 fsBuilder->codeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
273 // c = (x^2)+(y^2) - params[1]
274 fsBuilder->codeAppendf("\tfloat %s = dot(%s, %s) - %s;\n",
275 cName.c_str(), coords2D, coords2D, p1.c_str());
277 // linear case: t = -c/b
278 fsBuilder->codeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
279 cName.c_str(), bVar.c_str());
281 // if r(t) > 0, then t will be the x coordinate
282 fsBuilder->codeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
283 p2.c_str(), p0.c_str());
284 fsBuilder->codeAppend("\t");
285 this->emitColor(builder, ge, tName.c_str(), outputColor, inputColor, samplers);
286 fsBuilder->codeAppend("\t}\n");
289 void GLEdge2PtConicalEffect::setData(const GrGLProgramDataManager& pdman,
290 const GrProcessor& processor) {
291 INHERITED::setData(pdman, processor);
292 const Edge2PtConicalEffect& data = processor.cast<Edge2PtConicalEffect>();
293 SkScalar radius0 = data.radius();
294 SkScalar diffRadius = data.diffRadius();
296 if (fCachedRadius != radius0 ||
297 fCachedDiffRadius != diffRadius) {
300 SkScalarToFloat(radius0),
301 SkScalarToFloat(SkScalarMul(radius0, radius0)),
302 SkScalarToFloat(diffRadius)
305 pdman.set1fv(fParamUni, 3, values);
306 fCachedRadius = radius0;
307 fCachedDiffRadius = diffRadius;
311 void GLEdge2PtConicalEffect::GenKey(const GrProcessor& processor,
312 const GrGLCaps&, GrProcessorKeyBuilder* b) {
313 b->add32(GenBaseGradientKey(processor));
316 //////////////////////////////////////////////////////////////////////////////
317 // Focal Conical Gradients
318 //////////////////////////////////////////////////////////////////////////////
320 static ConicalType set_matrix_focal_conical(const SkTwoPointConicalGradient& shader,
321 SkMatrix* invLMatrix, SkScalar* focalX) {
322 // Inverse of the current local matrix is passed in then,
323 // translate, scale, and rotate such that endCircle is unit circle on x-axis,
324 // and focal point is at the origin.
325 ConicalType conicalType;
326 const SkPoint& focal = shader.getStartCenter();
327 const SkPoint& centerEnd = shader.getEndCenter();
328 SkScalar radius = shader.getEndRadius();
329 SkScalar invRadius = 1.f / radius;
333 matrix.setTranslate(-centerEnd.fX, -centerEnd.fY);
334 matrix.postScale(invRadius, invRadius);
337 matrix.mapPoints(&focalTrans, &focal, 1);
338 *focalX = focalTrans.length();
340 if (0.f != *focalX) {
341 SkScalar invFocalX = SkScalarInvert(*focalX);
343 rot.setSinCos(-SkScalarMul(invFocalX, focalTrans.fY),
344 SkScalarMul(invFocalX, focalTrans.fX));
345 matrix.postConcat(rot);
348 matrix.postTranslate(-(*focalX), 0.f);
350 // If the focal point is touching the edge of the circle it will
351 // cause a degenerate case that must be handled separately
352 // kEdgeErrorTol = 5 * kErrorTol was picked after manual testing the
353 // stability trade off versus the linear approx used in the Edge Shader
354 if (SkScalarAbs(1.f - (*focalX)) < kEdgeErrorTol) {
355 return kEdge_ConicalType;
358 // Scale factor 1 / (1 - focalX * focalX)
359 SkScalar oneMinusF2 = 1.f - SkScalarMul(*focalX, *focalX);
360 SkScalar s = SkScalarDiv(1.f, oneMinusF2);
364 conicalType = kInside_ConicalType;
365 matrix.postScale(s, s * SkScalarSqrt(oneMinusF2));
367 conicalType = kOutside_ConicalType;
368 matrix.postScale(s, s);
371 invLMatrix->postConcat(matrix);
376 //////////////////////////////////////////////////////////////////////////////
378 class FocalOutside2PtConicalEffect : public GrGradientEffect {
381 static GrFragmentProcessor* Create(GrContext* ctx,
382 const SkTwoPointConicalGradient& shader,
383 const SkMatrix& matrix,
384 SkShader::TileMode tm,
386 return SkNEW_ARGS(FocalOutside2PtConicalEffect, (ctx, shader, matrix, tm, focalX));
389 virtual ~FocalOutside2PtConicalEffect() { }
391 const char* name() const SK_OVERRIDE {
392 return "Two-Point Conical Gradient Focal Outside";
395 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
397 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
399 bool isFlipped() const { return fIsFlipped; }
400 SkScalar focal() const { return fFocalX; }
403 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
404 const FocalOutside2PtConicalEffect& s = sBase.cast<FocalOutside2PtConicalEffect>();
405 return (INHERITED::onIsEqual(sBase) &&
406 this->fFocalX == s.fFocalX &&
407 this->fIsFlipped == s.fIsFlipped);
410 FocalOutside2PtConicalEffect(GrContext* ctx,
411 const SkTwoPointConicalGradient& shader,
412 const SkMatrix& matrix,
413 SkShader::TileMode tm,
415 : INHERITED(ctx, shader, matrix, tm), fFocalX(focalX), fIsFlipped(shader.isFlippedGrad()) {
416 this->initClassID<FocalOutside2PtConicalEffect>();
419 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
424 typedef GrGradientEffect INHERITED;
427 class GLFocalOutside2PtConicalEffect : public GrGLGradientEffect {
429 GLFocalOutside2PtConicalEffect(const GrProcessor&);
430 virtual ~GLFocalOutside2PtConicalEffect() { }
432 virtual void emitCode(GrGLFPBuilder*,
433 const GrFragmentProcessor&,
434 const char* outputColor,
435 const char* inputColor,
436 const TransformedCoordsArray&,
437 const TextureSamplerArray&) SK_OVERRIDE;
438 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
440 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyBuilder* b);
443 UniformHandle fParamUni;
445 const char* fVSVaryingName;
446 const char* fFSVaryingName;
451 /// Values last uploaded as uniforms
453 SkScalar fCachedFocal;
458 typedef GrGLGradientEffect INHERITED;
462 void FocalOutside2PtConicalEffect::getGLProcessorKey(const GrGLCaps& caps,
463 GrProcessorKeyBuilder* b) const {
464 GLFocalOutside2PtConicalEffect::GenKey(*this, caps, b);
467 GrGLFragmentProcessor* FocalOutside2PtConicalEffect::createGLInstance() const {
468 return SkNEW_ARGS(GLFocalOutside2PtConicalEffect, (*this));
471 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(FocalOutside2PtConicalEffect);
474 * All Two point conical gradient test create functions may occasionally create edge case shaders
476 GrFragmentProcessor* FocalOutside2PtConicalEffect::TestCreate(SkRandom* random,
478 const GrDrawTargetCaps&,
480 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
481 SkScalar radius1 = 0.f;
485 center2.set(random->nextUScalar1(), random->nextUScalar1());
486 // Need to make sure the centers are not the same or else focal point will be inside
487 } while (center1 == center2);
488 SkPoint diff = center2 - center1;
489 SkScalar diffLen = diff.length();
490 // Below makes sure that the focal point is not contained within circle two
491 radius2 = random->nextRangeF(0.f, diffLen);
493 SkColor colors[kMaxRandomGradientColors];
494 SkScalar stopsArray[kMaxRandomGradientColors];
495 SkScalar* stops = stopsArray;
496 SkShader::TileMode tm;
497 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
498 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
500 colors, stops, colorCount,
503 GrFragmentProcessor* effect;
506 SkAssertResult(shader->asFragmentProcessor(context, paint,
507 GrProcessorUnitTest::TestMatrix(random), NULL,
508 &paintColor, &effect));
512 GLFocalOutside2PtConicalEffect::GLFocalOutside2PtConicalEffect(const GrProcessor& processor)
513 : fVSVaryingName(NULL)
514 , fFSVaryingName(NULL)
515 , fCachedFocal(SK_ScalarMax) {
516 const FocalOutside2PtConicalEffect& data = processor.cast<FocalOutside2PtConicalEffect>();
517 fIsFlipped = data.isFlipped();
520 void GLFocalOutside2PtConicalEffect::emitCode(GrGLFPBuilder* builder,
521 const GrFragmentProcessor& fp,
522 const char* outputColor,
523 const char* inputColor,
524 const TransformedCoordsArray& coords,
525 const TextureSamplerArray& samplers) {
526 const FocalOutside2PtConicalEffect& ge = fp.cast<FocalOutside2PtConicalEffect>();
527 this->emitUniforms(builder, ge);
528 fParamUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
529 kFloat_GrSLType, kDefault_GrSLPrecision,
530 "Conical2FSParams", 2);
532 SkString p0; // focalX
533 SkString p1; // 1 - focalX * focalX
535 builder->getUniformVariable(fParamUni).appendArrayAccess(0, &p0);
536 builder->getUniformVariable(fParamUni).appendArrayAccess(1, &p1);
538 // if we have a vec3 from being in perspective, convert it to a vec2 first
539 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
540 SkString coords2DString = fsBuilder->ensureFSCoords2D(coords, 0);
541 const char* coords2D = coords2DString.c_str();
543 // t = p.x * focal.x +/- sqrt(p.x^2 + (1 - focal.x^2) * p.y^2)
545 // output will default to transparent black (we simply won't write anything
546 // else to it if invalid, instead of discarding or returning prematurely)
547 fsBuilder->codeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
549 fsBuilder->codeAppendf("\tfloat xs = %s.x * %s.x;\n", coords2D, coords2D);
550 fsBuilder->codeAppendf("\tfloat ys = %s.y * %s.y;\n", coords2D, coords2D);
551 fsBuilder->codeAppendf("\tfloat d = xs + %s * ys;\n", p1.c_str());
553 // Must check to see if we flipped the circle order (to make sure start radius < end radius)
554 // If so we must also flip sign on sqrt
556 fsBuilder->codeAppendf("\tfloat %s = %s.x * %s + sqrt(d);\n", tName.c_str(),
557 coords2D, p0.c_str());
559 fsBuilder->codeAppendf("\tfloat %s = %s.x * %s - sqrt(d);\n", tName.c_str(),
560 coords2D, p0.c_str());
563 fsBuilder->codeAppendf("\tif (%s >= 0.0 && d >= 0.0) {\n", tName.c_str());
564 fsBuilder->codeAppend("\t\t");
565 this->emitColor(builder, ge, tName.c_str(), outputColor, inputColor, samplers);
566 fsBuilder->codeAppend("\t}\n");
569 void GLFocalOutside2PtConicalEffect::setData(const GrGLProgramDataManager& pdman,
570 const GrProcessor& processor) {
571 INHERITED::setData(pdman, processor);
572 const FocalOutside2PtConicalEffect& data = processor.cast<FocalOutside2PtConicalEffect>();
573 SkASSERT(data.isFlipped() == fIsFlipped);
574 SkScalar focal = data.focal();
576 if (fCachedFocal != focal) {
577 SkScalar oneMinus2F = 1.f - SkScalarMul(focal, focal);
580 SkScalarToFloat(focal),
581 SkScalarToFloat(oneMinus2F),
584 pdman.set1fv(fParamUni, 2, values);
585 fCachedFocal = focal;
589 void GLFocalOutside2PtConicalEffect::GenKey(const GrProcessor& processor,
590 const GrGLCaps&, GrProcessorKeyBuilder* b) {
591 uint32_t* key = b->add32n(2);
592 key[0] = GenBaseGradientKey(processor);
593 key[1] = processor.cast<FocalOutside2PtConicalEffect>().isFlipped();
596 //////////////////////////////////////////////////////////////////////////////
598 class GLFocalInside2PtConicalEffect;
600 class FocalInside2PtConicalEffect : public GrGradientEffect {
603 static GrFragmentProcessor* Create(GrContext* ctx,
604 const SkTwoPointConicalGradient& shader,
605 const SkMatrix& matrix,
606 SkShader::TileMode tm,
608 return SkNEW_ARGS(FocalInside2PtConicalEffect, (ctx, shader, matrix, tm, focalX));
611 virtual ~FocalInside2PtConicalEffect() {}
613 const char* name() const SK_OVERRIDE {
614 return "Two-Point Conical Gradient Focal Inside";
617 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
619 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
621 SkScalar focal() const { return fFocalX; }
623 typedef GLFocalInside2PtConicalEffect GLProcessor;
626 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
627 const FocalInside2PtConicalEffect& s = sBase.cast<FocalInside2PtConicalEffect>();
628 return (INHERITED::onIsEqual(sBase) &&
629 this->fFocalX == s.fFocalX);
632 FocalInside2PtConicalEffect(GrContext* ctx,
633 const SkTwoPointConicalGradient& shader,
634 const SkMatrix& matrix,
635 SkShader::TileMode tm,
637 : INHERITED(ctx, shader, matrix, tm), fFocalX(focalX) {
638 this->initClassID<FocalInside2PtConicalEffect>();
641 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
645 typedef GrGradientEffect INHERITED;
648 class GLFocalInside2PtConicalEffect : public GrGLGradientEffect {
650 GLFocalInside2PtConicalEffect(const GrProcessor&);
651 virtual ~GLFocalInside2PtConicalEffect() {}
653 virtual void emitCode(GrGLFPBuilder*,
654 const GrFragmentProcessor&,
655 const char* outputColor,
656 const char* inputColor,
657 const TransformedCoordsArray&,
658 const TextureSamplerArray&) SK_OVERRIDE;
659 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
661 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyBuilder* b);
664 UniformHandle fFocalUni;
666 const char* fVSVaryingName;
667 const char* fFSVaryingName;
670 /// Values last uploaded as uniforms
672 SkScalar fCachedFocal;
677 typedef GrGLGradientEffect INHERITED;
681 void FocalInside2PtConicalEffect::getGLProcessorKey(const GrGLCaps& caps,
682 GrProcessorKeyBuilder* b) const {
683 GLFocalInside2PtConicalEffect::GenKey(*this, caps, b);
686 GrGLFragmentProcessor* FocalInside2PtConicalEffect::createGLInstance() const {
687 return SkNEW_ARGS(GLFocalInside2PtConicalEffect, (*this));
690 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(FocalInside2PtConicalEffect);
693 * All Two point conical gradient test create functions may occasionally create edge case shaders
695 GrFragmentProcessor* FocalInside2PtConicalEffect::TestCreate(SkRandom* random,
697 const GrDrawTargetCaps&,
699 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
700 SkScalar radius1 = 0.f;
704 center2.set(random->nextUScalar1(), random->nextUScalar1());
705 // Below makes sure radius2 is larger enouch such that the focal point
706 // is inside the end circle
707 SkScalar increase = random->nextUScalar1();
708 SkPoint diff = center2 - center1;
709 SkScalar diffLen = diff.length();
710 radius2 = diffLen + increase;
711 // If the circles are identical the factory will give us an empty shader.
712 } while (radius1 == radius2 && center1 == center2);
714 SkColor colors[kMaxRandomGradientColors];
715 SkScalar stopsArray[kMaxRandomGradientColors];
716 SkScalar* stops = stopsArray;
717 SkShader::TileMode tm;
718 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
719 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
721 colors, stops, colorCount,
725 GrFragmentProcessor* fp;
726 SkAssertResult(shader->asFragmentProcessor(context, paint,
727 GrProcessorUnitTest::TestMatrix(random), NULL,
732 GLFocalInside2PtConicalEffect::GLFocalInside2PtConicalEffect(const GrProcessor&)
733 : fVSVaryingName(NULL)
734 , fFSVaryingName(NULL)
735 , fCachedFocal(SK_ScalarMax) {}
737 void GLFocalInside2PtConicalEffect::emitCode(GrGLFPBuilder* builder,
738 const GrFragmentProcessor& fp,
739 const char* outputColor,
740 const char* inputColor,
741 const TransformedCoordsArray& coords,
742 const TextureSamplerArray& samplers) {
743 const FocalInside2PtConicalEffect& ge = fp.cast<FocalInside2PtConicalEffect>();
744 this->emitUniforms(builder, ge);
745 fFocalUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
746 kFloat_GrSLType, kDefault_GrSLPrecision,
750 // this is the distance along x-axis from the end center to focal point in
751 // transformed coordinates
752 GrGLShaderVar focal = builder->getUniformVariable(fFocalUni);
754 // if we have a vec3 from being in perspective, convert it to a vec2 first
755 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
756 SkString coords2DString = fsBuilder->ensureFSCoords2D(coords, 0);
757 const char* coords2D = coords2DString.c_str();
759 // t = p.x * focalX + length(p)
760 fsBuilder->codeAppendf("\tfloat %s = %s.x * %s + length(%s);\n", tName.c_str(),
761 coords2D, focal.c_str(), coords2D);
763 this->emitColor(builder, ge, tName.c_str(), outputColor, inputColor, samplers);
766 void GLFocalInside2PtConicalEffect::setData(const GrGLProgramDataManager& pdman,
767 const GrProcessor& processor) {
768 INHERITED::setData(pdman, processor);
769 const FocalInside2PtConicalEffect& data = processor.cast<FocalInside2PtConicalEffect>();
770 SkScalar focal = data.focal();
772 if (fCachedFocal != focal) {
773 pdman.set1f(fFocalUni, SkScalarToFloat(focal));
774 fCachedFocal = focal;
778 void GLFocalInside2PtConicalEffect::GenKey(const GrProcessor& processor,
779 const GrGLCaps&, GrProcessorKeyBuilder* b) {
780 b->add32(GenBaseGradientKey(processor));
783 //////////////////////////////////////////////////////////////////////////////
784 // Circle Conical Gradients
785 //////////////////////////////////////////////////////////////////////////////
787 struct CircleConicalInfo {
794 // Returns focal distance along x-axis in transformed coords
795 static ConicalType set_matrix_circle_conical(const SkTwoPointConicalGradient& shader,
796 SkMatrix* invLMatrix, CircleConicalInfo* info) {
797 // Inverse of the current local matrix is passed in then,
798 // translate and scale such that start circle is on the origin and has radius 1
799 const SkPoint& centerStart = shader.getStartCenter();
800 const SkPoint& centerEnd = shader.getEndCenter();
801 SkScalar radiusStart = shader.getStartRadius();
802 SkScalar radiusEnd = shader.getEndRadius();
806 matrix.setTranslate(-centerStart.fX, -centerStart.fY);
808 SkScalar invStartRad = 1.f / radiusStart;
809 matrix.postScale(invStartRad, invStartRad);
811 radiusEnd /= radiusStart;
813 SkPoint centerEndTrans;
814 matrix.mapPoints(¢erEndTrans, ¢erEnd, 1);
816 SkScalar A = centerEndTrans.fX * centerEndTrans.fX + centerEndTrans.fY * centerEndTrans.fY
817 - radiusEnd * radiusEnd + 2 * radiusEnd - 1;
819 // Check to see if start circle is inside end circle with edges touching.
820 // If touching we return that it is of kEdge_ConicalType, and leave the matrix setting
821 // to the edge shader. kEdgeErrorTol = 5 * kErrorTol was picked after manual testing
822 // so that C = 1 / A is stable, and the linear approximation used in the Edge shader is
824 if (SkScalarAbs(A) < kEdgeErrorTol) {
825 return kEdge_ConicalType;
828 SkScalar C = 1.f / A;
829 SkScalar B = (radiusEnd - 1.f) * C;
831 matrix.postScale(C, C);
833 invLMatrix->postConcat(matrix);
835 info->fCenterEnd = centerEndTrans;
840 // if A ends up being negative, the start circle is contained completely inside the end cirlce
842 return kInside_ConicalType;
844 return kOutside_ConicalType;
847 class CircleInside2PtConicalEffect : public GrGradientEffect {
850 static GrFragmentProcessor* Create(GrContext* ctx,
851 const SkTwoPointConicalGradient& shader,
852 const SkMatrix& matrix,
853 SkShader::TileMode tm,
854 const CircleConicalInfo& info) {
855 return SkNEW_ARGS(CircleInside2PtConicalEffect, (ctx, shader, matrix, tm, info));
858 virtual ~CircleInside2PtConicalEffect() {}
860 const char* name() const SK_OVERRIDE { return "Two-Point Conical Gradient Inside"; }
862 virtual void getGLProcessorKey(const GrGLCaps& caps,
863 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
865 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
867 SkScalar centerX() const { return fInfo.fCenterEnd.fX; }
868 SkScalar centerY() const { return fInfo.fCenterEnd.fY; }
869 SkScalar A() const { return fInfo.fA; }
870 SkScalar B() const { return fInfo.fB; }
871 SkScalar C() const { return fInfo.fC; }
874 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
875 const CircleInside2PtConicalEffect& s = sBase.cast<CircleInside2PtConicalEffect>();
876 return (INHERITED::onIsEqual(sBase) &&
877 this->fInfo.fCenterEnd == s.fInfo.fCenterEnd &&
878 this->fInfo.fA == s.fInfo.fA &&
879 this->fInfo.fB == s.fInfo.fB &&
880 this->fInfo.fC == s.fInfo.fC);
883 CircleInside2PtConicalEffect(GrContext* ctx,
884 const SkTwoPointConicalGradient& shader,
885 const SkMatrix& matrix,
886 SkShader::TileMode tm,
887 const CircleConicalInfo& info)
888 : INHERITED(ctx, shader, matrix, tm), fInfo(info) {
889 this->initClassID<CircleInside2PtConicalEffect>();
892 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
894 const CircleConicalInfo fInfo;
896 typedef GrGradientEffect INHERITED;
899 class GLCircleInside2PtConicalEffect : public GrGLGradientEffect {
901 GLCircleInside2PtConicalEffect(const GrProcessor&);
902 virtual ~GLCircleInside2PtConicalEffect() {}
904 virtual void emitCode(GrGLFPBuilder*,
905 const GrFragmentProcessor&,
906 const char* outputColor,
907 const char* inputColor,
908 const TransformedCoordsArray&,
909 const TextureSamplerArray&) SK_OVERRIDE;
910 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
912 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyBuilder* b);
915 UniformHandle fCenterUni;
916 UniformHandle fParamUni;
918 const char* fVSVaryingName;
919 const char* fFSVaryingName;
922 /// Values last uploaded as uniforms
924 SkScalar fCachedCenterX;
925 SkScalar fCachedCenterY;
933 typedef GrGLGradientEffect INHERITED;
937 void CircleInside2PtConicalEffect::getGLProcessorKey(const GrGLCaps& caps,
938 GrProcessorKeyBuilder* b) const {
939 GLCircleInside2PtConicalEffect::GenKey(*this, caps, b);
942 GrGLFragmentProcessor* CircleInside2PtConicalEffect::createGLInstance() const {
943 return SkNEW_ARGS(GLCircleInside2PtConicalEffect, (*this));
946 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleInside2PtConicalEffect);
949 * All Two point conical gradient test create functions may occasionally create edge case shaders
951 GrFragmentProcessor* CircleInside2PtConicalEffect::TestCreate(SkRandom* random,
953 const GrDrawTargetCaps&,
955 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
956 SkScalar radius1 = random->nextUScalar1() + 0.0001f; // make sure radius1 != 0
960 center2.set(random->nextUScalar1(), random->nextUScalar1());
961 // Below makes sure that circle one is contained within circle two
962 SkScalar increase = random->nextUScalar1();
963 SkPoint diff = center2 - center1;
964 SkScalar diffLen = diff.length();
965 radius2 = radius1 + diffLen + increase;
966 // If the circles are identical the factory will give us an empty shader.
967 } while (radius1 == radius2 && center1 == center2);
969 SkColor colors[kMaxRandomGradientColors];
970 SkScalar stopsArray[kMaxRandomGradientColors];
971 SkScalar* stops = stopsArray;
972 SkShader::TileMode tm;
973 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
974 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
976 colors, stops, colorCount,
980 GrFragmentProcessor* processor;
981 SkAssertResult(shader->asFragmentProcessor(context, paint,
982 GrProcessorUnitTest::TestMatrix(random), NULL,
983 &paintColor, &processor));
987 GLCircleInside2PtConicalEffect::GLCircleInside2PtConicalEffect(const GrProcessor& processor)
988 : fVSVaryingName(NULL)
989 , fFSVaryingName(NULL)
990 , fCachedCenterX(SK_ScalarMax)
991 , fCachedCenterY(SK_ScalarMax)
992 , fCachedA(SK_ScalarMax)
993 , fCachedB(SK_ScalarMax)
994 , fCachedC(SK_ScalarMax) {}
996 void GLCircleInside2PtConicalEffect::emitCode(GrGLFPBuilder* builder,
997 const GrFragmentProcessor& fp,
998 const char* outputColor,
999 const char* inputColor,
1000 const TransformedCoordsArray& coords,
1001 const TextureSamplerArray& samplers) {
1002 const CircleInside2PtConicalEffect& ge = fp.cast<CircleInside2PtConicalEffect>();
1003 this->emitUniforms(builder, ge);
1004 fCenterUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
1005 kVec2f_GrSLType, kDefault_GrSLPrecision,
1006 "Conical2FSCenter");
1007 fParamUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
1008 kVec3f_GrSLType, kDefault_GrSLPrecision,
1009 "Conical2FSParams");
1010 SkString tName("t");
1012 GrGLShaderVar center = builder->getUniformVariable(fCenterUni);
1016 GrGLShaderVar params = builder->getUniformVariable(fParamUni);
1018 // if we have a vec3 from being in perspective, convert it to a vec2 first
1019 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
1020 SkString coords2DString = fsBuilder->ensureFSCoords2D(coords, 0);
1021 const char* coords2D = coords2DString.c_str();
1026 // A = dot(e, e) - r^2 + 2 * r - 1
1029 // d = dot(e, p) + B
1030 // t = d +/- sqrt(d^2 - A * dot(p, p) + C)
1031 fsBuilder->codeAppendf("\tfloat pDotp = dot(%s, %s);\n", coords2D, coords2D);
1032 fsBuilder->codeAppendf("\tfloat d = dot(%s, %s) + %s.y;\n", coords2D, center.c_str(),
1034 fsBuilder->codeAppendf("\tfloat %s = d + sqrt(d * d - %s.x * pDotp + %s.z);\n",
1035 tName.c_str(), params.c_str(), params.c_str());
1037 this->emitColor(builder, ge, tName.c_str(), outputColor, inputColor, samplers);
1040 void GLCircleInside2PtConicalEffect::setData(const GrGLProgramDataManager& pdman,
1041 const GrProcessor& processor) {
1042 INHERITED::setData(pdman, processor);
1043 const CircleInside2PtConicalEffect& data = processor.cast<CircleInside2PtConicalEffect>();
1044 SkScalar centerX = data.centerX();
1045 SkScalar centerY = data.centerY();
1046 SkScalar A = data.A();
1047 SkScalar B = data.B();
1048 SkScalar C = data.C();
1050 if (fCachedCenterX != centerX || fCachedCenterY != centerY ||
1051 fCachedA != A || fCachedB != B || fCachedC != C) {
1053 pdman.set2f(fCenterUni, SkScalarToFloat(centerX), SkScalarToFloat(centerY));
1054 pdman.set3f(fParamUni, SkScalarToFloat(A), SkScalarToFloat(B), SkScalarToFloat(C));
1056 fCachedCenterX = centerX;
1057 fCachedCenterY = centerY;
1064 void GLCircleInside2PtConicalEffect::GenKey(const GrProcessor& processor,
1065 const GrGLCaps&, GrProcessorKeyBuilder* b) {
1066 b->add32(GenBaseGradientKey(processor));
1069 //////////////////////////////////////////////////////////////////////////////
1071 class CircleOutside2PtConicalEffect : public GrGradientEffect {
1074 static GrFragmentProcessor* Create(GrContext* ctx,
1075 const SkTwoPointConicalGradient& shader,
1076 const SkMatrix& matrix,
1077 SkShader::TileMode tm,
1078 const CircleConicalInfo& info) {
1079 return SkNEW_ARGS(CircleOutside2PtConicalEffect, (ctx, shader, matrix, tm, info));
1082 virtual ~CircleOutside2PtConicalEffect() {}
1084 const char* name() const SK_OVERRIDE { return "Two-Point Conical Gradient Outside"; }
1086 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
1088 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
1090 SkScalar centerX() const { return fInfo.fCenterEnd.fX; }
1091 SkScalar centerY() const { return fInfo.fCenterEnd.fY; }
1092 SkScalar A() const { return fInfo.fA; }
1093 SkScalar B() const { return fInfo.fB; }
1094 SkScalar C() const { return fInfo.fC; }
1095 SkScalar tLimit() const { return fTLimit; }
1096 bool isFlipped() const { return fIsFlipped; }
1099 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
1100 const CircleOutside2PtConicalEffect& s = sBase.cast<CircleOutside2PtConicalEffect>();
1101 return (INHERITED::onIsEqual(sBase) &&
1102 this->fInfo.fCenterEnd == s.fInfo.fCenterEnd &&
1103 this->fInfo.fA == s.fInfo.fA &&
1104 this->fInfo.fB == s.fInfo.fB &&
1105 this->fInfo.fC == s.fInfo.fC &&
1106 this->fTLimit == s.fTLimit &&
1107 this->fIsFlipped == s.fIsFlipped);
1110 CircleOutside2PtConicalEffect(GrContext* ctx,
1111 const SkTwoPointConicalGradient& shader,
1112 const SkMatrix& matrix,
1113 SkShader::TileMode tm,
1114 const CircleConicalInfo& info)
1115 : INHERITED(ctx, shader, matrix, tm), fInfo(info) {
1116 this->initClassID<CircleOutside2PtConicalEffect>();
1117 if (shader.getStartRadius() != shader.getEndRadius()) {
1118 fTLimit = SkScalarDiv(shader.getStartRadius(),
1119 (shader.getStartRadius() - shader.getEndRadius()));
1121 fTLimit = SK_ScalarMin;
1124 fIsFlipped = shader.isFlippedGrad();
1127 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
1129 const CircleConicalInfo fInfo;
1133 typedef GrGradientEffect INHERITED;
1136 class GLCircleOutside2PtConicalEffect : public GrGLGradientEffect {
1138 GLCircleOutside2PtConicalEffect(const GrProcessor&);
1139 virtual ~GLCircleOutside2PtConicalEffect() {}
1141 virtual void emitCode(GrGLFPBuilder*,
1142 const GrFragmentProcessor&,
1143 const char* outputColor,
1144 const char* inputColor,
1145 const TransformedCoordsArray&,
1146 const TextureSamplerArray&) SK_OVERRIDE;
1147 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
1149 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyBuilder* b);
1152 UniformHandle fCenterUni;
1153 UniformHandle fParamUni;
1155 const char* fVSVaryingName;
1156 const char* fFSVaryingName;
1161 /// Values last uploaded as uniforms
1163 SkScalar fCachedCenterX;
1164 SkScalar fCachedCenterY;
1168 SkScalar fCachedTLimit;
1173 typedef GrGLGradientEffect INHERITED;
1177 void CircleOutside2PtConicalEffect::getGLProcessorKey(const GrGLCaps& caps,
1178 GrProcessorKeyBuilder* b) const {
1179 GLCircleOutside2PtConicalEffect::GenKey(*this, caps, b);
1182 GrGLFragmentProcessor* CircleOutside2PtConicalEffect::createGLInstance() const {
1183 return SkNEW_ARGS(GLCircleOutside2PtConicalEffect, (*this));
1186 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleOutside2PtConicalEffect);
1189 * All Two point conical gradient test create functions may occasionally create edge case shaders
1191 GrFragmentProcessor* CircleOutside2PtConicalEffect::TestCreate(SkRandom* random,
1193 const GrDrawTargetCaps&,
1195 SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()};
1196 SkScalar radius1 = random->nextUScalar1() + 0.0001f; // make sure radius1 != 0
1201 center2.set(random->nextUScalar1(), random->nextUScalar1());
1202 // If the circles share a center than we can't be in the outside case
1203 } while (center1 == center2);
1204 SkPoint diff = center2 - center1;
1205 diffLen = diff.length();
1206 // Below makes sure that circle one is not contained within circle two
1207 // and have radius2 >= radius to match sorting on cpu side
1208 radius2 = radius1 + random->nextRangeF(0.f, diffLen);
1210 SkColor colors[kMaxRandomGradientColors];
1211 SkScalar stopsArray[kMaxRandomGradientColors];
1212 SkScalar* stops = stopsArray;
1213 SkShader::TileMode tm;
1214 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
1215 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateTwoPointConical(center1, radius1,
1217 colors, stops, colorCount,
1221 GrFragmentProcessor* processor;
1223 SkAssertResult(shader->asFragmentProcessor(context, paint,
1224 GrProcessorUnitTest::TestMatrix(random), NULL,
1225 &paintColor, &processor));
1229 GLCircleOutside2PtConicalEffect::GLCircleOutside2PtConicalEffect(const GrProcessor& processor)
1230 : fVSVaryingName(NULL)
1231 , fFSVaryingName(NULL)
1232 , fCachedCenterX(SK_ScalarMax)
1233 , fCachedCenterY(SK_ScalarMax)
1234 , fCachedA(SK_ScalarMax)
1235 , fCachedB(SK_ScalarMax)
1236 , fCachedC(SK_ScalarMax)
1237 , fCachedTLimit(SK_ScalarMax) {
1238 const CircleOutside2PtConicalEffect& data = processor.cast<CircleOutside2PtConicalEffect>();
1239 fIsFlipped = data.isFlipped();
1242 void GLCircleOutside2PtConicalEffect::emitCode(GrGLFPBuilder* builder,
1243 const GrFragmentProcessor& fp,
1244 const char* outputColor,
1245 const char* inputColor,
1246 const TransformedCoordsArray& coords,
1247 const TextureSamplerArray& samplers) {
1248 const CircleOutside2PtConicalEffect& ge = fp.cast<CircleOutside2PtConicalEffect>();
1249 this->emitUniforms(builder, ge);
1250 fCenterUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
1251 kVec2f_GrSLType, kDefault_GrSLPrecision,
1252 "Conical2FSCenter");
1253 fParamUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
1254 kVec4f_GrSLType, kDefault_GrSLPrecision,
1255 "Conical2FSParams");
1256 SkString tName("t");
1258 GrGLShaderVar center = builder->getUniformVariable(fCenterUni);
1262 GrGLShaderVar params = builder->getUniformVariable(fParamUni);
1264 // if we have a vec3 from being in perspective, convert it to a vec2 first
1265 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
1266 SkString coords2DString = fsBuilder->ensureFSCoords2D(coords, 0);
1267 const char* coords2D = coords2DString.c_str();
1269 // output will default to transparent black (we simply won't write anything
1270 // else to it if invalid, instead of discarding or returning prematurely)
1271 fsBuilder->codeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
1276 // A = dot(e, e) - r^2 + 2 * r - 1
1279 // d = dot(e, p) + B
1280 // t = d +/- sqrt(d^2 - A * dot(p, p) + C)
1282 fsBuilder->codeAppendf("\tfloat pDotp = dot(%s, %s);\n", coords2D, coords2D);
1283 fsBuilder->codeAppendf("\tfloat d = dot(%s, %s) + %s.y;\n", coords2D, center.c_str(),
1285 fsBuilder->codeAppendf("\tfloat deter = d * d - %s.x * pDotp + %s.z;\n", params.c_str(),
1288 // Must check to see if we flipped the circle order (to make sure start radius < end radius)
1289 // If so we must also flip sign on sqrt
1291 fsBuilder->codeAppendf("\tfloat %s = d + sqrt(deter);\n", tName.c_str());
1293 fsBuilder->codeAppendf("\tfloat %s = d - sqrt(deter);\n", tName.c_str());
1296 fsBuilder->codeAppendf("\tif (%s >= %s.w && deter >= 0.0) {\n", tName.c_str(), params.c_str());
1297 fsBuilder->codeAppend("\t\t");
1298 this->emitColor(builder, ge, tName.c_str(), outputColor, inputColor, samplers);
1299 fsBuilder->codeAppend("\t}\n");
1302 void GLCircleOutside2PtConicalEffect::setData(const GrGLProgramDataManager& pdman,
1303 const GrProcessor& processor) {
1304 INHERITED::setData(pdman, processor);
1305 const CircleOutside2PtConicalEffect& data = processor.cast<CircleOutside2PtConicalEffect>();
1306 SkASSERT(data.isFlipped() == fIsFlipped);
1307 SkScalar centerX = data.centerX();
1308 SkScalar centerY = data.centerY();
1309 SkScalar A = data.A();
1310 SkScalar B = data.B();
1311 SkScalar C = data.C();
1312 SkScalar tLimit = data.tLimit();
1314 if (fCachedCenterX != centerX || fCachedCenterY != centerY ||
1315 fCachedA != A || fCachedB != B || fCachedC != C || fCachedTLimit != tLimit) {
1317 pdman.set2f(fCenterUni, SkScalarToFloat(centerX), SkScalarToFloat(centerY));
1318 pdman.set4f(fParamUni, SkScalarToFloat(A), SkScalarToFloat(B), SkScalarToFloat(C),
1319 SkScalarToFloat(tLimit));
1321 fCachedCenterX = centerX;
1322 fCachedCenterY = centerY;
1326 fCachedTLimit = tLimit;
1330 void GLCircleOutside2PtConicalEffect::GenKey(const GrProcessor& processor,
1331 const GrGLCaps&, GrProcessorKeyBuilder* b) {
1332 uint32_t* key = b->add32n(2);
1333 key[0] = GenBaseGradientKey(processor);
1334 key[1] = processor.cast<CircleOutside2PtConicalEffect>().isFlipped();
1337 //////////////////////////////////////////////////////////////////////////////
1339 GrFragmentProcessor* Gr2PtConicalGradientEffect::Create(GrContext* ctx,
1340 const SkTwoPointConicalGradient& shader,
1341 SkShader::TileMode tm,
1342 const SkMatrix* localMatrix) {
1344 if (!shader.getLocalMatrix().invert(&matrix)) {
1349 if (!localMatrix->invert(&inv)) {
1352 matrix.postConcat(inv);
1355 if (shader.getStartRadius() < kErrorTol) {
1357 ConicalType type = set_matrix_focal_conical(shader, &matrix, &focalX);
1358 if (type == kInside_ConicalType) {
1359 return FocalInside2PtConicalEffect::Create(ctx, shader, matrix, tm, focalX);
1360 } else if(type == kEdge_ConicalType) {
1361 set_matrix_edge_conical(shader, &matrix);
1362 return Edge2PtConicalEffect::Create(ctx, shader, matrix, tm);
1364 return FocalOutside2PtConicalEffect::Create(ctx, shader, matrix, tm, focalX);
1368 CircleConicalInfo info;
1369 ConicalType type = set_matrix_circle_conical(shader, &matrix, &info);
1371 if (type == kInside_ConicalType) {
1372 return CircleInside2PtConicalEffect::Create(ctx, shader, matrix, tm, info);
1373 } else if (type == kEdge_ConicalType) {
1374 set_matrix_edge_conical(shader, &matrix);
1375 return Edge2PtConicalEffect::Create(ctx, shader, matrix, tm);
1377 return CircleOutside2PtConicalEffect::Create(ctx, shader, matrix, tm, info);