Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Unified Diff: src/effects/gradients/SkGradientShader.cpp

Issue 22854005: GPU Gradients (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Moved mode toggling back to base class, fixed EffectKey masking. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/effects/gradients/SkGradientShader.cpp
diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index de43b69a2f70b78c1b4e3f667f9c357f2cf52b28..d943bb5e0fad4a344fabb8950bb93d22b7fb35ff 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -832,28 +832,71 @@ 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 (is2Color(key)) { // 2 Color case
+ fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+ kVec4f_GrSLType, "GradientStartColor");
+ fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+ kVec4f_GrSLType, "GradientEndColor");
+
+ } else if (is3Color(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 (is2Color(e)){
+ uman.set4f(fColorStartUni, 1.f, 1.f, 1.f, 1.f);
+ uman.set4f(fColorEndUni, 0.f, 0.f, 0.f, 1.f);
+
+ } else if (is3Color(e)){
+ 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) {
bsalomon 2013/08/13 14:28:37 Since this is no longer just doing the matrix it s
dierk 2013/08/13 19:50:17 Done.
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.numColors > 3){
+ texture = e.texture(0);
+ }
+
+ EffectKey key = GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsType, texture);
bsalomon 2013/08/13 14:28:37 Should probably only call GrGLEffectMatrix::GenKey
dierk 2013/08/13 19:50:17 Why is that? I thought I needed the matrix key to
+
+ if (is2Color(e)) {
+ key = key | 0x4000;
+ } else if (is3Color(e)){
+ key = key | 0x6000;
+ }
+
+ return key;
}
void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder,
@@ -868,21 +911,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 (is2Color(key)){
+ builder->fsCodeAppendf("\t%s = mix(%s, %s, %s);\n",
+ outputColor,
+ builder->getUniformVariable(fColorStartUni).c_str(),
+ builder->getUniformVariable(fColorEndUni).c_str(),
+ gradientTValue);
+ } else if (is3Color(key)){
+ //same as 2 color for now... causes bug from unused UNI
+ 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 +949,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 +957,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, &params);
- 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 (info.fColorCount == 2) { // if 2 color
bsalomon 2013/08/13 14:28:37 Style nit: 2 == info.fColorCount
dierk 2013/08/13 19:50:17 Done.
+ fRow = -1;
+ numColors = 2;
+
+ } else if (info.fColorCount == 3 && (abs(pos[1] - 0.5) < 0.01)) { // if 3 color symmetric
bsalomon 2013/08/13 14:28:37 I think there is an abs for SkScalar.
dierk 2013/08/13 19:50:17 Done.
+ fRow = -1;
+ numColors = 3;
+
+ } else {
+ numColors = 4;
bsalomon 2013/08/13 14:28:37 ? This at least deserves a comment since the numbe
dierk 2013/08/13 19:50:17 Done.
+
+ 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, &params);
+ 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 +1023,8 @@ bool GrGradientEffect::onIsEqual(const GrEffect& effect) const {
s.fTextureAccess.getParams().getTileModeX() &&
this->useAtlas() == s.useAtlas() &&
bsalomon 2013/08/13 14:28:37 Is useAtlas() equivalent to NULL == fTextureAccess
dierk 2013/08/13 19:50:17 I'm not sure. It was already there. The useAtlas(
bsalomon 2013/08/13 21:07:52 It should be. When you update the color comparison
fYCoord == s.getYCoord() &&
- fMatrix.cheapEqualTo(s.getMatrix());
+ fMatrix.cheapEqualTo(s.getMatrix())
+ && this->numColors == s.numColors; // do I need this check?
bsalomon 2013/08/13 14:28:37 Yes, you probably need to compare the colors too f
dierk 2013/08/13 19:50:17 Will add once colors are piped in. On 2013/08/13
}
void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {

Powered by Google App Engine
This is Rietveld 408576698