michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkTwoPointConicalGradient.h" michael@0: michael@0: static int valid_divide(float numer, float denom, float* ratio) { michael@0: SkASSERT(ratio); michael@0: if (0 == denom) { michael@0: return 0; michael@0: } michael@0: *ratio = numer / denom; michael@0: return 1; michael@0: } michael@0: michael@0: // Return the number of distinct real roots, and write them into roots[] in michael@0: // ascending order michael@0: static int find_quad_roots(float A, float B, float C, float roots[2]) { michael@0: SkASSERT(roots); michael@0: michael@0: if (A == 0) { michael@0: return valid_divide(-C, B, roots); michael@0: } michael@0: michael@0: float R = B*B - 4*A*C; michael@0: if (R < 0) { michael@0: return 0; michael@0: } michael@0: R = sk_float_sqrt(R); michael@0: michael@0: #if 1 michael@0: float Q = B; michael@0: if (Q < 0) { michael@0: Q -= R; michael@0: } else { michael@0: Q += R; michael@0: } michael@0: #else michael@0: // on 10.6 this was much slower than the above branch :( michael@0: float Q = B + copysignf(R, B); michael@0: #endif michael@0: Q *= -0.5f; michael@0: if (0 == Q) { michael@0: roots[0] = 0; michael@0: return 1; michael@0: } michael@0: michael@0: float r0 = Q / A; michael@0: float r1 = C / Q; michael@0: roots[0] = r0 < r1 ? r0 : r1; michael@0: roots[1] = r0 > r1 ? r0 : r1; michael@0: return 2; michael@0: } michael@0: michael@0: static float lerp(float x, float dx, float t) { michael@0: return x + t * dx; michael@0: } michael@0: michael@0: static float sqr(float x) { return x * x; } michael@0: michael@0: void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0, michael@0: const SkPoint& center1, SkScalar rad1) { michael@0: fCenterX = SkScalarToFloat(center0.fX); michael@0: fCenterY = SkScalarToFloat(center0.fY); michael@0: fDCenterX = SkScalarToFloat(center1.fX) - fCenterX; michael@0: fDCenterY = SkScalarToFloat(center1.fY) - fCenterY; michael@0: fRadius = SkScalarToFloat(rad0); michael@0: fDRadius = SkScalarToFloat(rad1) - fRadius; michael@0: michael@0: fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius); michael@0: fRadius2 = sqr(fRadius); michael@0: fRDR = fRadius * fDRadius; michael@0: } michael@0: michael@0: void TwoPtRadial::setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy) { michael@0: fRelX = SkScalarToFloat(fx) - fCenterX; michael@0: fRelY = SkScalarToFloat(fy) - fCenterY; michael@0: fIncX = SkScalarToFloat(dfx); michael@0: fIncY = SkScalarToFloat(dfy); michael@0: fB = -2 * (fDCenterX * fRelX + fDCenterY * fRelY + fRDR); michael@0: fDB = -2 * (fDCenterX * fIncX + fDCenterY * fIncY); michael@0: } michael@0: michael@0: SkFixed TwoPtRadial::nextT() { michael@0: float roots[2]; michael@0: michael@0: float C = sqr(fRelX) + sqr(fRelY) - fRadius2; michael@0: int countRoots = find_quad_roots(fA, fB, C, roots); michael@0: michael@0: fRelX += fIncX; michael@0: fRelY += fIncY; michael@0: fB += fDB; michael@0: michael@0: if (0 == countRoots) { michael@0: return kDontDrawT; michael@0: } michael@0: michael@0: // Prefer the bigger t value if both give a radius(t) > 0 michael@0: // find_quad_roots returns the values sorted, so we start with the last michael@0: float t = roots[countRoots - 1]; michael@0: float r = lerp(fRadius, fDRadius, t); michael@0: if (r <= 0) { michael@0: t = roots[0]; // might be the same as roots[countRoots-1] michael@0: r = lerp(fRadius, fDRadius, t); michael@0: if (r <= 0) { michael@0: return kDontDrawT; michael@0: } michael@0: } michael@0: return SkFloatToFixed(t); michael@0: } michael@0: michael@0: typedef void (*TwoPointConicalProc)(TwoPtRadial* rec, SkPMColor* dstC, michael@0: const SkPMColor* cache, int toggle, int count); michael@0: michael@0: static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, michael@0: const SkPMColor* SK_RESTRICT cache, int toggle, michael@0: int count) { michael@0: for (; count > 0; --count) { michael@0: SkFixed t = rec->nextT(); michael@0: if (TwoPtRadial::DontDrawT(t)) { michael@0: *dstC++ = 0; michael@0: } else { michael@0: SkFixed index = SkClampMax(t, 0xFFFF); michael@0: SkASSERT(index <= 0xFFFF); michael@0: *dstC++ = cache[toggle + michael@0: (index >> SkGradientShaderBase::kCache32Shift)]; michael@0: } michael@0: toggle = next_dither_toggle(toggle); michael@0: } michael@0: } michael@0: michael@0: static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, michael@0: const SkPMColor* SK_RESTRICT cache, int toggle, michael@0: int count) { michael@0: for (; count > 0; --count) { michael@0: SkFixed t = rec->nextT(); michael@0: if (TwoPtRadial::DontDrawT(t)) { michael@0: *dstC++ = 0; michael@0: } else { michael@0: SkFixed index = repeat_tileproc(t); michael@0: SkASSERT(index <= 0xFFFF); michael@0: *dstC++ = cache[toggle + michael@0: (index >> SkGradientShaderBase::kCache32Shift)]; michael@0: } michael@0: toggle = next_dither_toggle(toggle); michael@0: } michael@0: } michael@0: michael@0: static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, michael@0: const SkPMColor* SK_RESTRICT cache, int toggle, michael@0: int count) { michael@0: for (; count > 0; --count) { michael@0: SkFixed t = rec->nextT(); michael@0: if (TwoPtRadial::DontDrawT(t)) { michael@0: *dstC++ = 0; michael@0: } else { michael@0: SkFixed index = mirror_tileproc(t); michael@0: SkASSERT(index <= 0xFFFF); michael@0: *dstC++ = cache[toggle + michael@0: (index >> SkGradientShaderBase::kCache32Shift)]; michael@0: } michael@0: toggle = next_dither_toggle(toggle); michael@0: } michael@0: } michael@0: michael@0: void SkTwoPointConicalGradient::init() { michael@0: fRec.init(fCenter1, fRadius1, fCenter2, fRadius2); michael@0: fPtsToUnit.reset(); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: SkTwoPointConicalGradient::SkTwoPointConicalGradient( michael@0: const SkPoint& start, SkScalar startRadius, michael@0: const SkPoint& end, SkScalar endRadius, michael@0: const Descriptor& desc) michael@0: : SkGradientShaderBase(desc), michael@0: fCenter1(start), michael@0: fCenter2(end), michael@0: fRadius1(startRadius), michael@0: fRadius2(endRadius) { michael@0: // this is degenerate, and should be caught by our caller michael@0: SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2); michael@0: this->init(); michael@0: } michael@0: michael@0: bool SkTwoPointConicalGradient::isOpaque() const { michael@0: // Because areas outside the cone are left untouched, we cannot treat the michael@0: // shader as opaque even if the gradient itself is opaque. michael@0: // TODO(junov): Compute whether the cone fills the plane crbug.com/222380 michael@0: return false; michael@0: } michael@0: michael@0: void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam, michael@0: int count) { michael@0: int toggle = init_dither_toggle(x, y); michael@0: michael@0: SkASSERT(count > 0); michael@0: michael@0: SkPMColor* SK_RESTRICT dstC = dstCParam; michael@0: michael@0: SkMatrix::MapXYProc dstProc = fDstToIndexProc; michael@0: michael@0: const SkPMColor* SK_RESTRICT cache = this->getCache32(); michael@0: michael@0: TwoPointConicalProc shadeProc = twopoint_repeat; michael@0: if (SkShader::kClamp_TileMode == fTileMode) { michael@0: shadeProc = twopoint_clamp; michael@0: } else if (SkShader::kMirror_TileMode == fTileMode) { michael@0: shadeProc = twopoint_mirror; michael@0: } else { michael@0: SkASSERT(SkShader::kRepeat_TileMode == fTileMode); michael@0: } michael@0: michael@0: if (fDstToIndexClass != kPerspective_MatrixClass) { michael@0: SkPoint srcPt; michael@0: dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: SkScalar dx, fx = srcPt.fX; michael@0: SkScalar dy, fy = srcPt.fY; michael@0: michael@0: if (fDstToIndexClass == kFixedStepInX_MatrixClass) { michael@0: SkFixed fixedX, fixedY; michael@0: (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); michael@0: dx = SkFixedToScalar(fixedX); michael@0: dy = SkFixedToScalar(fixedY); michael@0: } else { michael@0: SkASSERT(fDstToIndexClass == kLinear_MatrixClass); michael@0: dx = fDstToIndex.getScaleX(); michael@0: dy = fDstToIndex.getSkewY(); michael@0: } michael@0: michael@0: fRec.setup(fx, fy, dx, dy); michael@0: (*shadeProc)(&fRec, dstC, cache, toggle, count); michael@0: } else { // perspective case michael@0: SkScalar dstX = SkIntToScalar(x) + SK_ScalarHalf; michael@0: SkScalar dstY = SkIntToScalar(y) + SK_ScalarHalf; michael@0: for (; count > 0; --count) { michael@0: SkPoint srcPt; michael@0: dstProc(fDstToIndex, dstX, dstY, &srcPt); michael@0: fRec.setup(srcPt.fX, srcPt.fY, 0, 0); michael@0: (*shadeProc)(&fRec, dstC, cache, toggle, 1); michael@0: michael@0: dstX += SK_Scalar1; michael@0: toggle = next_dither_toggle(toggle); michael@0: dstC += 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkTwoPointConicalGradient::setContext(const SkBitmap& device, michael@0: const SkPaint& paint, michael@0: const SkMatrix& matrix) { michael@0: if (!this->INHERITED::setContext(device, paint, matrix)) { michael@0: return false; michael@0: } michael@0: michael@0: // we don't have a span16 proc michael@0: fFlags &= ~kHasSpan16_Flag; michael@0: michael@0: // in general, we might discard based on computed-radius, so clear michael@0: // this flag (todo: sometimes we can detect that we never discard...) michael@0: fFlags &= ~kOpaqueAlpha_Flag; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: SkShader::BitmapType SkTwoPointConicalGradient::asABitmap( michael@0: SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const { michael@0: SkPoint diff = fCenter2 - fCenter1; michael@0: SkScalar diffLen = 0; michael@0: michael@0: if (bitmap) { michael@0: this->getGradientTableBitmap(bitmap); michael@0: } michael@0: if (matrix) { michael@0: diffLen = diff.length(); michael@0: } michael@0: if (matrix) { michael@0: if (diffLen) { michael@0: SkScalar invDiffLen = SkScalarInvert(diffLen); michael@0: // rotate to align circle centers with the x-axis michael@0: matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY), michael@0: SkScalarMul(invDiffLen, diff.fX)); michael@0: } else { michael@0: matrix->reset(); michael@0: } michael@0: matrix->preTranslate(-fCenter1.fX, -fCenter1.fY); michael@0: } michael@0: if (xy) { michael@0: xy[0] = fTileMode; michael@0: xy[1] = kClamp_TileMode; michael@0: } michael@0: return kTwoPointConical_BitmapType; michael@0: } michael@0: michael@0: SkShader::GradientType SkTwoPointConicalGradient::asAGradient( michael@0: GradientInfo* info) const { michael@0: if (info) { michael@0: commonAsAGradient(info); michael@0: info->fPoint[0] = fCenter1; michael@0: info->fPoint[1] = fCenter2; michael@0: info->fRadius[0] = fRadius1; michael@0: info->fRadius[1] = fRadius2; michael@0: } michael@0: return kConical_GradientType; michael@0: } michael@0: michael@0: SkTwoPointConicalGradient::SkTwoPointConicalGradient( michael@0: SkReadBuffer& buffer) michael@0: : INHERITED(buffer), michael@0: fCenter1(buffer.readPoint()), michael@0: fCenter2(buffer.readPoint()), michael@0: fRadius1(buffer.readScalar()), michael@0: fRadius2(buffer.readScalar()) { michael@0: this->init(); michael@0: }; michael@0: michael@0: void SkTwoPointConicalGradient::flatten( michael@0: SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.writePoint(fCenter1); michael@0: buffer.writePoint(fCenter2); michael@0: buffer.writeScalar(fRadius1); michael@0: buffer.writeScalar(fRadius2); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: #if SK_SUPPORT_GPU michael@0: michael@0: #include "GrTBackendEffectFactory.h" michael@0: michael@0: // For brevity michael@0: typedef GrGLUniformManager::UniformHandle UniformHandle; michael@0: michael@0: class GrGLConical2Gradient : public GrGLGradientEffect { michael@0: public: michael@0: michael@0: GrGLConical2Gradient(const GrBackendEffectFactory& factory, const GrDrawEffect&); michael@0: virtual ~GrGLConical2Gradient() { } michael@0: michael@0: virtual void emitCode(GrGLShaderBuilder*, michael@0: const GrDrawEffect&, michael@0: EffectKey, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray&) SK_OVERRIDE; michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; michael@0: michael@0: static EffectKey GenKey(const GrDrawEffect&, const GrGLCaps& caps); michael@0: michael@0: protected: michael@0: michael@0: UniformHandle fParamUni; michael@0: michael@0: const char* fVSVaryingName; michael@0: const char* fFSVaryingName; michael@0: michael@0: bool fIsDegenerate; michael@0: michael@0: // @{ michael@0: /// Values last uploaded as uniforms michael@0: michael@0: SkScalar fCachedCenter; michael@0: SkScalar fCachedRadius; michael@0: SkScalar fCachedDiffRadius; michael@0: michael@0: // @} michael@0: michael@0: private: michael@0: michael@0: typedef GrGLGradientEffect INHERITED; michael@0: michael@0: }; michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: class GrConical2Gradient : public GrGradientEffect { michael@0: public: michael@0: michael@0: static GrEffectRef* Create(GrContext* ctx, michael@0: const SkTwoPointConicalGradient& shader, michael@0: const SkMatrix& matrix, michael@0: SkShader::TileMode tm) { michael@0: AutoEffectUnref effect(SkNEW_ARGS(GrConical2Gradient, (ctx, shader, matrix, tm))); michael@0: return CreateEffectRef(effect); michael@0: } michael@0: michael@0: virtual ~GrConical2Gradient() { } michael@0: michael@0: static const char* Name() { return "Two-Point Conical Gradient"; } michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: // The radial gradient parameters can collapse to a linear (instead of quadratic) equation. michael@0: bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); } michael@0: SkScalar center() const { return fCenterX1; } michael@0: SkScalar diffRadius() const { return fDiffRadius; } michael@0: SkScalar radius() const { return fRadius0; } michael@0: michael@0: typedef GrGLConical2Gradient GLEffect; michael@0: michael@0: private: michael@0: virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { michael@0: const GrConical2Gradient& s = CastEffect(sBase); michael@0: return (INHERITED::onIsEqual(sBase) && michael@0: this->fCenterX1 == s.fCenterX1 && michael@0: this->fRadius0 == s.fRadius0 && michael@0: this->fDiffRadius == s.fDiffRadius); michael@0: } michael@0: michael@0: GrConical2Gradient(GrContext* ctx, michael@0: const SkTwoPointConicalGradient& shader, michael@0: const SkMatrix& matrix, michael@0: SkShader::TileMode tm) michael@0: : INHERITED(ctx, shader, matrix, tm) michael@0: , fCenterX1(shader.getCenterX1()) michael@0: , fRadius0(shader.getStartRadius()) michael@0: , fDiffRadius(shader.getDiffRadius()) { michael@0: // We pass the linear part of the quadratic as a varying. michael@0: // float b = -2.0 * (fCenterX1 * x + fRadius0 * fDiffRadius * z) michael@0: fBTransform = this->getCoordTransform(); michael@0: SkMatrix& bMatrix = *fBTransform.accessMatrix(); michael@0: SkScalar r0dr = SkScalarMul(fRadius0, fDiffRadius); michael@0: bMatrix[SkMatrix::kMScaleX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMScaleX]) + michael@0: SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp0])); michael@0: bMatrix[SkMatrix::kMSkewX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMSkewX]) + michael@0: SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp1])); michael@0: bMatrix[SkMatrix::kMTransX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMTransX]) + michael@0: SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp2])); michael@0: this->addCoordTransform(&fBTransform); michael@0: } michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: // @{ michael@0: // Cache of values - these can change arbitrarily, EXCEPT michael@0: // we shouldn't change between degenerate and non-degenerate?! michael@0: michael@0: GrCoordTransform fBTransform; michael@0: SkScalar fCenterX1; michael@0: SkScalar fRadius0; michael@0: SkScalar fDiffRadius; michael@0: michael@0: // @} michael@0: michael@0: typedef GrGradientEffect INHERITED; michael@0: }; michael@0: michael@0: GR_DEFINE_EFFECT_TEST(GrConical2Gradient); michael@0: michael@0: GrEffectRef* GrConical2Gradient::TestCreate(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture**) { michael@0: SkPoint center1 = {random->nextUScalar1(), random->nextUScalar1()}; michael@0: SkScalar radius1 = random->nextUScalar1(); michael@0: SkPoint center2; michael@0: SkScalar radius2; michael@0: do { michael@0: center2.set(random->nextUScalar1(), random->nextUScalar1()); michael@0: radius2 = random->nextUScalar1 (); michael@0: // If the circles are identical the factory will give us an empty shader. michael@0: } while (radius1 == radius2 && center1 == center2); michael@0: michael@0: SkColor colors[kMaxRandomGradientColors]; michael@0: SkScalar stopsArray[kMaxRandomGradientColors]; michael@0: SkScalar* stops = stopsArray; michael@0: SkShader::TileMode tm; michael@0: int colorCount = RandomGradientParams(random, colors, &stops, &tm); michael@0: SkAutoTUnref shader(SkGradientShader::CreateTwoPointConical(center1, radius1, michael@0: center2, radius2, michael@0: colors, stops, colorCount, michael@0: tm)); michael@0: SkPaint paint; michael@0: return shader->asNewEffect(context, paint); michael@0: } michael@0: michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: GrGLConical2Gradient::GrGLConical2Gradient(const GrBackendEffectFactory& factory, michael@0: const GrDrawEffect& drawEffect) michael@0: : INHERITED(factory) michael@0: , fVSVaryingName(NULL) michael@0: , fFSVaryingName(NULL) michael@0: , fCachedCenter(SK_ScalarMax) michael@0: , fCachedRadius(-SK_ScalarMax) michael@0: , fCachedDiffRadius(-SK_ScalarMax) { michael@0: michael@0: const GrConical2Gradient& data = drawEffect.castEffect(); michael@0: fIsDegenerate = data.isDegenerate(); michael@0: } michael@0: michael@0: void GrGLConical2Gradient::emitCode(GrGLShaderBuilder* builder, michael@0: const GrDrawEffect&, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray& coords, michael@0: const TextureSamplerArray& samplers) { michael@0: this->emitUniforms(builder, key); michael@0: fParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, michael@0: kFloat_GrSLType, "Conical2FSParams", 6); michael@0: michael@0: SkString cName("c"); michael@0: SkString ac4Name("ac4"); michael@0: SkString dName("d"); michael@0: SkString qName("q"); michael@0: SkString r0Name("r0"); michael@0: SkString r1Name("r1"); michael@0: SkString tName("t"); michael@0: SkString p0; // 4a michael@0: SkString p1; // 1/a michael@0: SkString p2; // distance between centers michael@0: SkString p3; // start radius michael@0: SkString p4; // start radius squared michael@0: SkString p5; // difference in radii (r1 - r0) michael@0: michael@0: builder->getUniformVariable(fParamUni).appendArrayAccess(0, &p0); michael@0: builder->getUniformVariable(fParamUni).appendArrayAccess(1, &p1); michael@0: builder->getUniformVariable(fParamUni).appendArrayAccess(2, &p2); michael@0: builder->getUniformVariable(fParamUni).appendArrayAccess(3, &p3); michael@0: builder->getUniformVariable(fParamUni).appendArrayAccess(4, &p4); michael@0: builder->getUniformVariable(fParamUni).appendArrayAccess(5, &p5); michael@0: michael@0: // We interpolate the linear component in coords[1]. michael@0: SkASSERT(coords[0].type() == coords[1].type()); michael@0: const char* coords2D; michael@0: SkString bVar; michael@0: if (kVec3f_GrSLType == coords[0].type()) { michael@0: builder->fsCodeAppendf("\tvec3 interpolants = vec3(%s.xy, %s.x) / %s.z;\n", michael@0: coords[0].c_str(), coords[1].c_str(), coords[0].c_str()); michael@0: coords2D = "interpolants.xy"; michael@0: bVar = "interpolants.z"; michael@0: } else { michael@0: coords2D = coords[0].c_str(); michael@0: bVar.printf("%s.x", coords[1].c_str()); michael@0: } michael@0: michael@0: // output will default to transparent black (we simply won't write anything michael@0: // else to it if invalid, instead of discarding or returning prematurely) michael@0: builder->fsCodeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor); michael@0: michael@0: // c = (x^2)+(y^2) - params[4] michael@0: builder->fsCodeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", michael@0: cName.c_str(), coords2D, coords2D, p4.c_str()); michael@0: michael@0: // Non-degenerate case (quadratic) michael@0: if (!fIsDegenerate) { michael@0: michael@0: // ac4 = params[0] * c michael@0: builder->fsCodeAppendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(), michael@0: cName.c_str()); michael@0: michael@0: // d = b^2 - ac4 michael@0: builder->fsCodeAppendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(), michael@0: bVar.c_str(), bVar.c_str(), ac4Name.c_str()); michael@0: michael@0: // only proceed if discriminant is >= 0 michael@0: builder->fsCodeAppendf("\tif (%s >= 0.0) {\n", dName.c_str()); michael@0: michael@0: // intermediate value we'll use to compute the roots michael@0: // q = -0.5 * (b +/- sqrt(d)) michael@0: builder->fsCodeAppendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)" michael@0: " * sqrt(%s));\n", qName.c_str(), bVar.c_str(), michael@0: bVar.c_str(), dName.c_str()); michael@0: michael@0: // compute both roots michael@0: // r0 = q * params[1] michael@0: builder->fsCodeAppendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(), michael@0: qName.c_str(), p1.c_str()); michael@0: // r1 = c / q michael@0: builder->fsCodeAppendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(), michael@0: cName.c_str(), qName.c_str()); michael@0: michael@0: // Note: If there are two roots that both generate radius(t) > 0, the michael@0: // Canvas spec says to choose the larger t. michael@0: michael@0: // so we'll look at the larger one first: michael@0: builder->fsCodeAppendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(), michael@0: r0Name.c_str(), r1Name.c_str()); michael@0: michael@0: // if r(t) > 0, then we're done; t will be our x coordinate michael@0: builder->fsCodeAppendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(), michael@0: p5.c_str(), p3.c_str()); michael@0: michael@0: builder->fsCodeAppend("\t\t"); michael@0: this->emitColor(builder, tName.c_str(), key, outputColor, inputColor, samplers); michael@0: michael@0: // otherwise, if r(t) for the larger root was <= 0, try the other root michael@0: builder->fsCodeAppend("\t\t} else {\n"); michael@0: builder->fsCodeAppendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(), michael@0: r0Name.c_str(), r1Name.c_str()); michael@0: michael@0: // if r(t) > 0 for the smaller root, then t will be our x coordinate michael@0: builder->fsCodeAppendf("\t\t\tif (%s * %s + %s > 0.0) {\n", michael@0: tName.c_str(), p5.c_str(), p3.c_str()); michael@0: michael@0: builder->fsCodeAppend("\t\t\t"); michael@0: this->emitColor(builder, tName.c_str(), key, outputColor, inputColor, samplers); michael@0: michael@0: // end if (r(t) > 0) for smaller root michael@0: builder->fsCodeAppend("\t\t\t}\n"); michael@0: // end if (r(t) > 0), else, for larger root michael@0: builder->fsCodeAppend("\t\t}\n"); michael@0: // end if (discriminant >= 0) michael@0: builder->fsCodeAppend("\t}\n"); michael@0: } else { michael@0: michael@0: // linear case: t = -c/b michael@0: builder->fsCodeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), michael@0: cName.c_str(), bVar.c_str()); michael@0: michael@0: // if r(t) > 0, then t will be the x coordinate michael@0: builder->fsCodeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(), michael@0: p5.c_str(), p3.c_str()); michael@0: builder->fsCodeAppend("\t"); michael@0: this->emitColor(builder, tName.c_str(), key, outputColor, inputColor, samplers); michael@0: builder->fsCodeAppend("\t}\n"); michael@0: } michael@0: } michael@0: michael@0: void GrGLConical2Gradient::setData(const GrGLUniformManager& uman, michael@0: const GrDrawEffect& drawEffect) { michael@0: INHERITED::setData(uman, drawEffect); michael@0: const GrConical2Gradient& data = drawEffect.castEffect(); michael@0: SkASSERT(data.isDegenerate() == fIsDegenerate); michael@0: SkScalar centerX1 = data.center(); michael@0: SkScalar radius0 = data.radius(); michael@0: SkScalar diffRadius = data.diffRadius(); michael@0: michael@0: if (fCachedCenter != centerX1 || michael@0: fCachedRadius != radius0 || michael@0: fCachedDiffRadius != diffRadius) { michael@0: michael@0: SkScalar a = SkScalarMul(centerX1, centerX1) - diffRadius * diffRadius; michael@0: michael@0: // When we're in the degenerate (linear) case, the second michael@0: // value will be INF but the program doesn't read it. (We michael@0: // use the same 6 uniforms even though we don't need them michael@0: // all in the linear case just to keep the code complexity michael@0: // down). michael@0: float values[6] = { michael@0: SkScalarToFloat(a * 4), michael@0: 1.f / (SkScalarToFloat(a)), michael@0: SkScalarToFloat(centerX1), michael@0: SkScalarToFloat(radius0), michael@0: SkScalarToFloat(SkScalarMul(radius0, radius0)), michael@0: SkScalarToFloat(diffRadius) michael@0: }; michael@0: michael@0: uman.set1fv(fParamUni, 6, values); michael@0: fCachedCenter = centerX1; michael@0: fCachedRadius = radius0; michael@0: fCachedDiffRadius = diffRadius; michael@0: } michael@0: } michael@0: michael@0: GrGLEffect::EffectKey GrGLConical2Gradient::GenKey(const GrDrawEffect& drawEffect, michael@0: const GrGLCaps&) { michael@0: enum { michael@0: kIsDegenerate = 1 << kBaseKeyBitCnt, michael@0: }; michael@0: michael@0: EffectKey key = GenBaseGradientKey(drawEffect); michael@0: if (drawEffect.castEffect().isDegenerate()) { michael@0: key |= kIsDegenerate; michael@0: } michael@0: return key; michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&) const { michael@0: SkASSERT(NULL != context); michael@0: SkASSERT(fPtsToUnit.isIdentity()); michael@0: // invert the localM, translate to center1, rotate so center2 is on x axis. michael@0: SkMatrix matrix; michael@0: if (!this->getLocalMatrix().invert(&matrix)) { michael@0: return NULL; michael@0: } michael@0: matrix.postTranslate(-fCenter1.fX, -fCenter1.fY); michael@0: michael@0: SkPoint diff = fCenter2 - fCenter1; michael@0: SkScalar diffLen = diff.length(); michael@0: if (0 != diffLen) { michael@0: SkScalar invDiffLen = SkScalarInvert(diffLen); michael@0: SkMatrix rot; michael@0: rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY), michael@0: SkScalarMul(invDiffLen, diff.fX)); michael@0: matrix.postConcat(rot); michael@0: } michael@0: michael@0: return GrConical2Gradient::Create(context, *this, matrix, fTileMode); michael@0: } michael@0: michael@0: #else michael@0: michael@0: GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext*, const SkPaint&) const { michael@0: SkDEBUGFAIL("Should not call in GPU-less build"); michael@0: return NULL; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkTwoPointConicalGradient::toString(SkString* str) const { michael@0: str->append("SkTwoPointConicalGradient: ("); michael@0: michael@0: str->append("center1: ("); michael@0: str->appendScalar(fCenter1.fX); michael@0: str->append(", "); michael@0: str->appendScalar(fCenter1.fY); michael@0: str->append(") radius1: "); michael@0: str->appendScalar(fRadius1); michael@0: str->append(" "); michael@0: michael@0: str->append("center2: ("); michael@0: str->appendScalar(fCenter2.fX); michael@0: str->append(", "); michael@0: str->appendScalar(fCenter2.fY); michael@0: str->append(") radius2: "); michael@0: str->appendScalar(fRadius2); michael@0: str->append(" "); michael@0: michael@0: this->INHERITED::toString(str); michael@0: michael@0: str->append(")"); michael@0: } michael@0: #endif