Chromium Code Reviews| Index: src/effects/gradients/SkGradientShader.cpp |
| diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp |
| index de43b69a2f70b78c1b4e3f667f9c357f2cf52b28..714ef72ba43abb794226f1c8a3c1224d3dbc60aa 100644 |
| --- a/src/effects/gradients/SkGradientShader.cpp |
| +++ b/src/effects/gradients/SkGradientShader.cpp |
| @@ -832,28 +832,72 @@ GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) |
| GrGLGradientEffect::~GrGLGradientEffect() { } |
| -void GrGLGradientEffect::emitYCoordUniform(GrGLShaderBuilder* builder) { |
| - fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| - kFloat_GrSLType, "GradientYCoordFS"); |
| +void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) { |
| + |
| + if (GrGradientEffect::kTwo_ColorType == getColorType(key)) { // 2 Color case |
| + fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| + kVec4f_GrSLType, "GradientStartColor"); |
| + fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| + kVec4f_GrSLType, "GradientEndColor"); |
| + |
| + } else if (GrGradientEffect::kTwo_ColorType == getColorType(key)){ // 3 Color Case |
| + fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| + kVec4f_GrSLType, "GradientStartColor"); |
| + fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| + kVec4f_GrSLType, "GradientMidColor"); |
| + fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| + kVec4f_GrSLType, "GradientEndColor"); |
| + |
| + } else { // if not a fast case |
| + fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| + kFloat_GrSLType, "GradientYCoordFS"); |
| + } |
| } |
| void GrGLGradientEffect::setData(const GrGLUniformManager& uman, |
| const GrDrawEffect& drawEffect) { |
| + |
| const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
| - const GrTexture* texture = e.texture(0); |
| - fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture); |
| - SkScalar yCoord = e.getYCoord(); |
| - if (yCoord != fCachedYCoord) { |
| - uman.set1f(fFSYUni, yCoord); |
| - fCachedYCoord = yCoord; |
| + |
| + if (GrGradientEffect::kTwo_ColorType == e.getColorType()){ |
| + uman.set4f(fColorStartUni, 1.f, 1.f, 1.f, 1.f); |
| + uman.set4f(fColorEndUni, 0.f, 0.f, 0.f, 1.f); |
| + |
| + } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ |
| + uman.set4f(fColorStartUni, 1.f, 1.f, 1.f, 1.f); |
| + uman.set4f(fColorMidUni, 1.f, 0.f, 0.f, 0.f); |
| + uman.set4f(fColorEndUni, 1.f, 1.f, 1.f, 1.f); |
| + } else { |
| + const GrTexture* texture = e.texture(0); |
| + fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture); |
| + |
| + SkScalar yCoord = e.getYCoord(); |
| + if (yCoord != fCachedYCoord) { |
| + uman.set1f(fFSYUni, yCoord); |
| + fCachedYCoord = yCoord; |
| + } |
| } |
| } |
| -GrGLEffect::EffectKey GrGLGradientEffect::GenMatrixKey(const GrDrawEffect& drawEffect) { |
| + |
| +GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { |
| const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
| - const GrTexture* texture = e.texture(0); |
| - return GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsType, texture); |
| + const GrTexture* texture = NULL; |
| + |
| + if (e.getNumColors() > 3){ |
| + texture = e.texture(0); |
| + } |
| + |
| + EffectKey key = GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsType, texture); |
| + |
| + if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { |
| + key = key | kTwoColorKey; |
| + } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ |
| + key = key | kThreeColorKey; |
| + } |
| + |
| + return key; |
| } |
| void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, |
| @@ -868,21 +912,36 @@ void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, |
| vsVaryingType); |
| } |
| -void GrGLGradientEffect::emitColorLookup(GrGLShaderBuilder* builder, |
| - const char* gradientTValue, |
| - const char* outputColor, |
| - const char* inputColor, |
| - const GrGLShaderBuilder::TextureSampler& sampler) { |
| - |
| - builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", |
| - gradientTValue, |
| - builder->getUniformVariable(fFSYUni).c_str()); |
| - builder->fsCodeAppendf("\t%s = ", outputColor); |
| - builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType, |
| - inputColor, |
| - sampler, |
| - "coord"); |
| - builder->fsCodeAppend(";\n"); |
| +void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, |
| + const char* gradientTValue, |
| + EffectKey key, |
| + const char* outputColor, |
| + const char* inputColor, |
| + const GrGLShaderBuilder::TextureSamplerArray& samplers) { |
| + if (GrGradientEffect::kTwo_ColorType == getColorType(key)){ |
| + builder->fsCodeAppendf("\t%s = mix(%s, %s, %s);\n", |
| + outputColor, |
| + builder->getUniformVariable(fColorStartUni).c_str(), |
| + builder->getUniformVariable(fColorEndUni).c_str(), |
| + gradientTValue); |
| + } else if (GrGradientEffect::kThree_ColorType == getColorType(key)){ |
| + //same as 2 color for now... causes bug from unused UNI |
|
dierk
2013/08/13 19:50:18
How to mix for 3 colors on the GPU? Is there a way
bsalomon
2013/08/13 20:40:03
I think this works, but not sure if it's faster th
|
| + builder->fsCodeAppendf("\t%s = mix(%s, %s, %s);\n", |
| + outputColor, |
| + builder->getUniformVariable(fColorStartUni).c_str(), |
| + builder->getUniformVariable(fColorEndUni).c_str(), |
| + gradientTValue); |
| + } else { |
| + builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", |
| + gradientTValue, |
| + builder->getUniformVariable(fFSYUni).c_str()); |
| + builder->fsCodeAppendf("\t%s = ", outputColor); |
| + builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType, |
| + inputColor, |
| + samplers[0], |
| + "coord"); |
| + builder->fsCodeAppend(";\n"); |
| + } |
| } |
| ///////////////////////////////////////////////////////////////////// |
| @@ -891,10 +950,6 @@ GrGradientEffect::GrGradientEffect(GrContext* ctx, |
| const SkGradientShaderBase& shader, |
| const SkMatrix& matrix, |
| SkShader::TileMode tileMode) { |
| - // TODO: check for simple cases where we don't need a texture: |
| - //GradientInfo info; |
| - //shader.asAGradient(&info); |
| - //if (info.fColorCount == 2) { ... |
| fMatrix = matrix; |
| @@ -903,36 +958,57 @@ GrGradientEffect::GrGradientEffect(GrContext* ctx, |
| fIsOpaque = shader.isOpaque(); |
| - GrTextureStripAtlas::Desc desc; |
| - desc.fWidth = bitmap.width(); |
| - desc.fHeight = 32; |
| - desc.fRowHeight = bitmap.height(); |
| - desc.fContext = ctx; |
| - desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| - fAtlas = GrTextureStripAtlas::GetAtlas(desc); |
| - GrAssert(NULL != fAtlas); |
| - |
| - // We always filter the gradient table. Each table is one row of a texture, so always y-clamp. |
| - GrTextureParams params; |
| - params.setFilterMode(GrTextureParams::kBilerp_FilterMode); |
| - params.setTileModeX(tileMode); |
| - |
| - fRow = fAtlas->lockRow(bitmap); |
| - if (-1 != fRow) { |
| - fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * |
| - fAtlas->getVerticalScaleFactor(); |
| - fTextureAccess.reset(fAtlas->getTexture(), params); |
| - } else { |
| - GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶ms); |
| - fTextureAccess.reset(texture, params); |
| - fYCoord = SK_ScalarHalf; |
| + // TODO: check for simple cases where we don't need a texture: |
| + SkShader::GradientInfo info; |
| + SkScalar pos[3] = {0}; |
| + |
| + info.fColorCount = 3; |
| + info.fColors = NULL; |
| + info.fColorOffsets = &pos[0]; |
| + shader.asAGradient(&info); |
| - // Unlock immediately, this is not great, but we don't have a way of |
| - // knowing when else to unlock it currently, so it may get purged from |
| - // the cache, but it'll still be ref'd until it's no longer being used. |
| - GrUnlockAndUnrefCachedBitmapTexture(texture); |
| + if (2 == info.fColorCount) { // if 2 color |
| + fRow = -1; |
| + fNumColors = 2; |
| + |
| + } else if (3 == info.fColorCount && (SkScalarAbs(pos[1] - 0.5) < 0.01)) { // if 3 color symmetric |
| + fRow = -1; |
| + fNumColors = 3; |
| + |
| + } else { |
| + fNumColors = 4; // if anything else use 4 as code for use texture instead |
| + |
| + GrTextureStripAtlas::Desc desc; |
| + desc.fWidth = bitmap.width(); |
| + desc.fHeight = 32; |
| + desc.fRowHeight = bitmap.height(); |
| + desc.fContext = ctx; |
| + desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| + fAtlas = GrTextureStripAtlas::GetAtlas(desc); |
| + GrAssert(NULL != fAtlas); |
| + |
| + // We always filter the gradient table. Each table is one row of a texture, so always y-clamp. |
| + GrTextureParams params; |
| + params.setFilterMode(GrTextureParams::kBilerp_FilterMode); |
| + params.setTileModeX(tileMode); |
| + |
| + fRow = fAtlas->lockRow(bitmap); |
| + if (-1 != fRow) { |
| + fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * |
| + fAtlas->getVerticalScaleFactor(); |
| + fTextureAccess.reset(fAtlas->getTexture(), params); |
| + } else { |
| + GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶ms); |
| + fTextureAccess.reset(texture, params); |
| + fYCoord = SK_ScalarHalf; |
| + |
| + // Unlock immediately, this is not great, but we don't have a way of |
| + // knowing when else to unlock it currently, so it may get purged from |
| + // the cache, but it'll still be ref'd until it's no longer being used. |
| + GrUnlockAndUnrefCachedBitmapTexture(texture); |
| + } |
| + this->addTextureAccess(&fTextureAccess); |
| } |
| - this->addTextureAccess(&fTextureAccess); |
| } |
| GrGradientEffect::~GrGradientEffect() { |
| @@ -948,7 +1024,9 @@ bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { |
| s.fTextureAccess.getParams().getTileModeX() && |
| this->useAtlas() == s.useAtlas() && |
| fYCoord == s.getYCoord() && |
| - fMatrix.cheapEqualTo(s.getMatrix()); |
| + fMatrix.cheapEqualTo(s.getMatrix()) |
| + && this->fNumColors == s.fNumColors; |
| + // also check for colors of 2 / 3 gradients! |
| } |
| void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |