diff options
-rw-r--r-- | include/core/SkShader.h | 12 | ||||
-rw-r--r-- | samplecode/SampleDegenerateTwoPtRadials.cpp | 2 | ||||
-rw-r--r-- | src/effects/SkGradientShader.cpp | 41 | ||||
-rw-r--r-- | src/gpu/SkGpuDevice.cpp | 11 | ||||
-rw-r--r-- | src/gpu/effects/GrGradientEffects.cpp | 321 | ||||
-rw-r--r-- | src/gpu/effects/GrGradientEffects.h | 38 |
6 files changed, 412 insertions, 13 deletions
diff --git a/include/core/SkShader.h b/include/core/SkShader.h index e84206258e..435643e1d3 100644 --- a/include/core/SkShader.h +++ b/include/core/SkShader.h @@ -212,8 +212,18 @@ public: // space // 2: the second radius minus the first radius // in pre-transformed space. + kTwoPointConical_BitmapType, + //<! Matrix transforms to space where (0,0) is + // the center of the starting circle. The second + // circle will be centered (x, 0) where x may be + // 0. + // Three extra parameters are returned: + // 0: x-offset of second circle center + // to first. + // 1: radius of first circle + // 2: the second radius minus the first radius - kLast_BitmapType = kTwoPointRadial_BitmapType + kLast_BitmapType = kTwoPointConical_BitmapType }; /** Optional methods for shaders that can pretend to be a bitmap/texture to play along with opengl. Default just returns kNone_BitmapType and diff --git a/samplecode/SampleDegenerateTwoPtRadials.cpp b/samplecode/SampleDegenerateTwoPtRadials.cpp index 2c08a816ce..91f1622b69 100644 --- a/samplecode/SampleDegenerateTwoPtRadials.cpp +++ b/samplecode/SampleDegenerateTwoPtRadials.cpp @@ -26,7 +26,7 @@ static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) SkPoint c1 = { l + 3 * w / 5, t + h / 2 }; SkScalar r0 = w / 5; SkScalar r1 = 2 * w / 5; - SkShader* s = SkGradientShader::CreateTwoPointRadial(c0, r0, c1, r1, colors, + SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, pos, SK_ARRAY_COUNT(pos), SkShader::kClamp_TileMode); SkPaint paint; diff --git a/src/effects/SkGradientShader.cpp b/src/effects/SkGradientShader.cpp index b0badc5127..559e711cfe 100644 --- a/src/effects/SkGradientShader.cpp +++ b/src/effects/SkGradientShader.cpp @@ -2304,6 +2304,47 @@ public: return true; } + virtual BitmapType asABitmap(SkBitmap* bitmap, + SkMatrix* matrix, + TileMode* xy, + SkScalar* twoPointRadialParams) const { + + SkPoint diff = fCenter2 - fCenter1; + SkScalar diffRadius = fRadius2 - fRadius1; + SkScalar startRadius = fRadius1; + SkScalar diffLen = 0; + + if (bitmap) { + this->commonAsABitmap(bitmap); + } + if (matrix || twoPointRadialParams) { + diffLen = diff.length(); + } + if (matrix) { + if (diffLen) { + SkScalar invDiffLen = SkScalarInvert(diffLen); + // rotate to align circle centers with the x-axis + matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY), + SkScalarMul(invDiffLen, diff.fX)); + } else { + matrix->reset(); + } + matrix->preTranslate(-fCenter1.fX, -fCenter1.fY); + } + if (xy) { + xy[0] = fTileMode; + xy[1] = kClamp_TileMode; + } + if (NULL != twoPointRadialParams) { + twoPointRadialParams[0] = diffLen; + twoPointRadialParams[1] = startRadius; + twoPointRadialParams[2] = diffRadius; + } + + return kTwoPointConical_BitmapType; + } + + SkShader::GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE { if (info) { commonAsAGradient(info); diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp index c3e495ae28..491e273216 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -411,7 +411,9 @@ SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch); SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch); SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4, shader_type_mismatch); -SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch); +SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5, + shader_type_mismatch); +SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 5, shader_type_mismatch); namespace { @@ -545,6 +547,13 @@ inline bool skPaint2GrPaintShader(SkGpuDevice* dev, twoPointParams[2] < 0))->unref(); sampler->setFilter(GrSamplerState::kBilinear_Filter); break; + case SkShader::kTwoPointConical_BitmapType: + sampler->setCustomStage(new + GrConical2Gradient(twoPointParams[0], + twoPointParams[1], + twoPointParams[2]))->unref(); + sampler->setFilter(GrSamplerState::kBilinear_Filter); + break; default: if (skPaint.isFilterBitmap()) { sampler->setFilter(GrSamplerState::kBilinear_Filter); diff --git a/src/gpu/effects/GrGradientEffects.cpp b/src/gpu/effects/GrGradientEffects.cpp index d876d62990..005002fc95 100644 --- a/src/gpu/effects/GrGradientEffects.cpp +++ b/src/gpu/effects/GrGradientEffects.cpp @@ -35,9 +35,9 @@ private: }; void GrGLRadialGradient::emitFS(GrGLShaderBuilder* state, - const char* outputColor, - const char* inputColor, - const char* samplerName) { + const char* outputColor, + const char* inputColor, + const char* samplerName) { state->fSampleCoords.printf("vec2(length(%s.xy), 0.5)", state->fSampleCoords.c_str()); state->fComplexCoord = true; @@ -161,7 +161,7 @@ void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) { } void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state, - const char* vertexCoords) { + const char* vertexCoords) { GrStringBuilder* code = &state->fVSCode; GrStringBuilder p2; GrStringBuilder p3; @@ -179,9 +179,9 @@ void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state, } void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* state, - const char* outputColor, - const char* inputColor, - const char* samplerName) { + const char* outputColor, + const char* inputColor, + const char* samplerName) { GrStringBuilder* code = &state->fFSCode; GrStringBuilder cName("c"); GrStringBuilder ac4Name("ac4"); @@ -259,9 +259,9 @@ void GrGLRadial2Gradient::initUniforms(const GrGLInterface* gl, int programID) { } void GrGLRadial2Gradient::setData(const GrGLInterface* gl, - const GrGLTexture& texture, - const GrCustomStage& baseData, - int stageNum) { + const GrGLTexture& texture, + const GrCustomStage& baseData, + int stageNum) { const GrRadial2Gradient& data = static_cast<const GrRadial2Gradient&>(baseData); GrAssert(data.isDegenerate() == fIsDegenerate); @@ -323,6 +323,307 @@ bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const { ///////////////////////////////////////////////////////////////////// +class GrGLConical2Gradient : public GrGLProgramStage { + +public: + + GrGLConical2Gradient(const GrProgramStageFactory& factory, + const GrCustomStage&); + virtual ~GrGLConical2Gradient() { } + + virtual void setupVariables(GrGLShaderBuilder* state, + int stage) SK_OVERRIDE; + virtual void emitVS(GrGLShaderBuilder* state, + const char* vertexCoords) SK_OVERRIDE; + virtual void emitFS(GrGLShaderBuilder* state, + const char* outputColor, + const char* inputColor, + const char* samplerName) SK_OVERRIDE; + virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE; + virtual void setData(const GrGLInterface*, + const GrGLTexture&, + const GrCustomStage&, + int stageNum) SK_OVERRIDE; + + static StageKey GenKey(const GrCustomStage& s) { + return (static_cast<const GrConical2Gradient&>(s).isDegenerate()); + } + +protected: + + const GrGLShaderVar* fVSParamVar; + GrGLint fVSParamLocation; + const GrGLShaderVar* fFSParamVar; + GrGLint fFSParamLocation; + + const char* fVSVaryingName; + const char* fFSVaryingName; + + bool fIsDegenerate; + + // @{ + /// Values last uploaded as uniforms + + GrScalar fCachedCenter; + GrScalar fCachedRadius; + GrScalar fCachedDiffRadius; + + // @} + +private: + + typedef GrGLProgramStage INHERITED; + +}; + +GrGLConical2Gradient::GrGLConical2Gradient( + const GrProgramStageFactory& factory, + const GrCustomStage& baseData) + : INHERITED(factory) + , fVSParamVar(NULL) + , fFSParamVar(NULL) + , fVSVaryingName(NULL) + , fFSVaryingName(NULL) + , fCachedCenter(GR_ScalarMax) + , fCachedRadius(-GR_ScalarMax) + , fCachedDiffRadius(-GR_ScalarMax) { + + const GrConical2Gradient& data = + static_cast<const GrConical2Gradient&>(baseData); + fIsDegenerate = data.isDegenerate(); +} + +void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) { + // 2 copies of uniform array, 1 for each of vertex & fragment shader, + // to work around Xoom bug. Doesn't seem to cause performance decrease + // in test apps, but need to keep an eye on it. + fVSParamVar = &state->addUniform( + GrGLShaderBuilder::kVertex_VariableLifetime, + kFloat_GrSLType, "uConical2VSParams", stage, 6); + fFSParamVar = &state->addUniform( + GrGLShaderBuilder::kFragment_VariableLifetime, + kFloat_GrSLType, "uConical2FSParams", stage, 6); + + fVSParamLocation = GrGLProgramStage::kUseUniform; + fFSParamLocation = GrGLProgramStage::kUseUniform; + + // For radial gradients without perspective we can pass the linear + // part of the quadratic as a varying. + if (state->fVaryingDims == state->fCoordDims) { + state->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage, + &fVSVaryingName, &fFSVaryingName); + } +} + +void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* state, + const char* vertexCoords) { + GrStringBuilder* code = &state->fVSCode; + GrStringBuilder p2; // distance between centers + GrStringBuilder p3; // start radius + GrStringBuilder p5; // difference in radii (r1 - r0) + fVSParamVar->appendArrayAccess(2, &p2); + fVSParamVar->appendArrayAccess(3, &p3); + fVSParamVar->appendArrayAccess(5, &p5); + + // For radial gradients without perspective we can pass the linear + // part of the quadratic as a varying. + if (state->fVaryingDims == state->fCoordDims) { + // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5]) + code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n", + fVSVaryingName, p2.c_str(), + vertexCoords, p3.c_str(), p5.c_str()); + } +} + +void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* state, + const char* outputColor, + const char* inputColor, + const char* samplerName) { + GrStringBuilder* code = &state->fFSCode; + + GrStringBuilder cName("c"); + GrStringBuilder ac4Name("ac4"); + GrStringBuilder dName("d"); + GrStringBuilder qName("q"); + GrStringBuilder r0Name("r0"); + GrStringBuilder r1Name("r1"); + GrStringBuilder tName("t"); + GrStringBuilder p0; // 4a + GrStringBuilder p1; // 1/(2a) + GrStringBuilder p2; // distance between centers + GrStringBuilder p3; // start radius + GrStringBuilder p4; // start radius squared + GrStringBuilder p5; // difference in radii (r1 - r0) + fFSParamVar->appendArrayAccess(0, &p0); + fFSParamVar->appendArrayAccess(1, &p1); + fFSParamVar->appendArrayAccess(2, &p2); + fFSParamVar->appendArrayAccess(3, &p3); + fFSParamVar->appendArrayAccess(4, &p4); + fFSParamVar->appendArrayAccess(5, &p5); + + // If we we're able to interpolate the linear component, + // bVar is the varying; otherwise compute it + GrStringBuilder bVar; + if (state->fCoordDims == state->fVaryingDims) { + bVar = fFSVaryingName; + GrAssert(2 == state->fVaryingDims); + } else { + GrAssert(3 == state->fVaryingDims); + bVar = "b"; + code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n", + bVar.c_str(), p2.c_str(), state->fSampleCoords.c_str(), + p3.c_str(), p5.c_str()); + } + + // c = (x^2)+(y^2) - params[4] + code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(), + state->fSampleCoords.c_str(), state->fSampleCoords.c_str(), + p4.c_str()); + + // Non-degenerate case (quadratic) + if (!fIsDegenerate) { + + // ac4 = params[0] * c + code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(), + cName.c_str()); + + // d = b^2 - ac4 + code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(), + bVar.c_str(), bVar.c_str(), ac4Name.c_str()); + + // if discriminant is < 0, set to transparent black and return + code->appendf("\tif (%s < 0.0) {\n\t\tgl_FragColor = vec4(0.0,0.0,0.0," + "0.0);\n\t\treturn;\n\t}\n", dName.c_str()); + + // intermediate value we'll use to compute the roots + // q = -0.5 * (b +/- sqrt(d)) + code->appendf("\tfloat %s = -0.5f * (%s + (%s < 0.0 ? -1.0 : 1.0)" + " * sqrt(%s));\n", qName.c_str(), bVar.c_str(), + bVar.c_str(), dName.c_str()); + + // compute both roots + // r0 = q * params[1] + code->appendf("\tfloat %s = %s * %s;\n", r0Name.c_str(), qName.c_str(), + p1.c_str()); + // r1 = c / q + code->appendf("\tfloat %s = %s / %s;\n", r1Name.c_str(), cName.c_str(), + qName.c_str()); + + // Note: If there are two roots that both generate radius(t) > 0, the + // Canvas spec says to choose the larger t. + + // so we'll look at the larger one first: + code->appendf("\tfloat %s = max(%s, %s);\n", tName.c_str(), + r0Name.c_str(), r1Name.c_str()); + + // if r(t) for the larger root is <= 0, try the other one + code->appendf("\tif (%s * %s + %s <= 0.0) {\n", tName.c_str(), + p5.c_str(), p3.c_str()); + code->appendf("\t\t%s = min(%s, %s);\n", tName.c_str(), + r0Name.c_str(), r1Name.c_str()); + + // if r(t) for the smaller root is also <= 0, set the fragment to + // transparent black and return + code->appendf("\t\tif (%s * %s + %s <= 0.0) {\n\t\t\tgl_FragColor = " + "vec4(0.0,0.0,0.0,0.0);\n\t\t\treturn;\n\t\t}\n", + tName.c_str(), p5.c_str(), p3.c_str()); + + code->appendf("\t}\n"); + } else { + + // linear case: t = -c/b + code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), + cName.c_str(), bVar.c_str()); + + // set the fragment to transparent black and return if r(t) is <= 0 + code->appendf("\tif (%s * %s + %s <= 0.0) {\n\t\tgl_FragColor = vec4(" + "0.0,0.0,0.0,0.0);\n\t\treturn;\n\t}\n", tName.c_str(), + p5.c_str(), p3.c_str()); + } + state->fComplexCoord = true; + + // x coord is: t + // y coord is 0.5 (texture is effectively 1D) + state->fSampleCoords.printf("\tvec2(%s, 0.5)", tName.c_str()); + + state->emitDefaultFetch(outputColor, samplerName); +} + +void GrGLConical2Gradient::initUniforms(const GrGLInterface* gl, int programID) { + GR_GL_CALL_RET(gl, fVSParamLocation, + GetUniformLocation(programID, fVSParamVar->getName().c_str())); + GR_GL_CALL_RET(gl, fFSParamLocation, + GetUniformLocation(programID, fFSParamVar->getName().c_str())); +} + +void GrGLConical2Gradient::setData(const GrGLInterface* gl, + const GrGLTexture& texture, + const GrCustomStage& baseData, + int stageNum) { + const GrConical2Gradient& data = + static_cast<const GrConical2Gradient&>(baseData); + GrAssert(data.isDegenerate() == fIsDegenerate); + GrScalar centerX1 = data.center(); + GrScalar radius0 = data.radius(); + GrScalar diffRadius = data.diffRadius(); + + if (fCachedCenter != centerX1 || + fCachedRadius != radius0 || + fCachedDiffRadius != diffRadius) { + + GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius; + + // When we're in the degenerate (linear) case, the second + // value will be INF but the program doesn't read it. (We + // use the same 6 uniforms even though we don't need them + // all in the linear case just to keep the code complexity + // down). + float values[6] = { + GrScalarToFloat(a * 4), + 1.f / (GrScalarToFloat(a)), + GrScalarToFloat(centerX1), + GrScalarToFloat(radius0), + GrScalarToFloat(SkScalarMul(radius0, radius0)), + GrScalarToFloat(diffRadius) + }; + + GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values)); + GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values)); + fCachedCenter = centerX1; + fCachedRadius = radius0; + fCachedDiffRadius = diffRadius; + } +} + + +///////////////////////////////////////////////////////////////////// + +GrConical2Gradient::GrConical2Gradient(GrScalar center, + GrScalar radius, + GrScalar diffRadius) + : fCenterX1 (center) + , fRadius0 (radius) + , fDiffRadius (diffRadius) { + +} + +GrConical2Gradient::~GrConical2Gradient() { + +} + + +const GrProgramStageFactory& GrConical2Gradient::getFactory() const { + return GrTProgramStageFactory<GrConical2Gradient>::getInstance(); +} + +bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const { + const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase); + return (this->isDegenerate() == s.isDegenerate()); +} + +///////////////////////////////////////////////////////////////////// + + class GrGLSweepGradient : public GrGLProgramStage { public: diff --git a/src/gpu/effects/GrGradientEffects.h b/src/gpu/effects/GrGradientEffects.h index 9bcf9dd772..cf6827f251 100644 --- a/src/gpu/effects/GrGradientEffects.h +++ b/src/gpu/effects/GrGradientEffects.h @@ -92,6 +92,44 @@ private: typedef GrCustomStage INHERITED; }; +class GrGLConical2Gradient; + +class GrConical2Gradient : public GrCustomStage { + +public: + + GrConical2Gradient(GrScalar center, GrScalar radius, GrScalar diffRadius); + virtual ~GrConical2Gradient(); + + static const char* Name() { return "Two-Point Conical Gradient"; } + virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE; + virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE; + + // The radial gradient parameters can collapse to a linear (instead + // of quadratic) equation. + bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == + SkScalarAbs(fCenterX1); } + GrScalar center() const { return fCenterX1; } + GrScalar diffRadius() const { return fDiffRadius; } + GrScalar radius() const { return fRadius0; } + + typedef GrGLConical2Gradient GLProgramStage; + +private: + + // @{ + // Cache of values - these can change arbitrarily, EXCEPT + // we shouldn't change between degenerate and non-degenerate?! + + GrScalar fCenterX1; + GrScalar fRadius0; + GrScalar fDiffRadius; + + // @} + + typedef GrCustomStage INHERITED; +}; + class GrGLSweepGradient; class GrSweepGradient : public GrCustomStage { |