diff options
author | bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-10-17 15:00:32 +0000 |
---|---|---|
committer | bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-10-17 15:00:32 +0000 |
commit | b2ad101313cfba9c06a3a4dc06531766bcbec73b (patch) | |
tree | 04891e3b660c5e2a533eb834b1903b51132f0592 /src | |
parent | 67e78c9e47c38a51816412a24a10f4fe2db142a3 (diff) |
Make table color filter use asNewCustomStage.
R=robertphillips@google.com
Review URL: https://codereview.appspot.com/6713051
git-svn-id: http://skia.googlecode.com/svn/trunk@5976 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkColorFilter.cpp | 2 | ||||
-rw-r--r-- | src/effects/SkTableColorFilter.cpp | 174 | ||||
-rw-r--r-- | src/gpu/SkGpuDevice.cpp | 11 | ||||
-rw-r--r-- | src/gpu/effects/GrColorTableEffect.cpp | 122 | ||||
-rw-r--r-- | src/gpu/effects/GrColorTableEffect.h | 42 |
5 files changed, 169 insertions, 182 deletions
diff --git a/src/core/SkColorFilter.cpp b/src/core/SkColorFilter.cpp index 3651d7e618..7daaf81a3d 100644 --- a/src/core/SkColorFilter.cpp +++ b/src/core/SkColorFilter.cpp @@ -21,7 +21,7 @@ bool SkColorFilter::asColorMatrix(SkScalar matrix[20]) { return false; } -bool SkColorFilter::asComponentTable(SkBitmap*) { +bool SkColorFilter::asComponentTable(SkBitmap*) const { return false; } diff --git a/src/effects/SkTableColorFilter.cpp b/src/effects/SkTableColorFilter.cpp index fcfb444a3f..83049f29c6 100644 --- a/src/effects/SkTableColorFilter.cpp +++ b/src/effects/SkTableColorFilter.cpp @@ -38,7 +38,11 @@ public: SkDELETE(fBitmap); } - virtual bool asComponentTable(SkBitmap* table) SK_OVERRIDE; + virtual bool asComponentTable(SkBitmap* table) const SK_OVERRIDE; + +#if SK_SUPPORT_GPU + virtual GrCustomStage* asNewCustomStage(GrContext* context) const SK_OVERRIDE; +#endif virtual void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) SK_OVERRIDE; @@ -50,7 +54,7 @@ protected: virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; private: - SkBitmap* fBitmap; + mutable const SkBitmap* fBitmap; // lazily allocated enum { kA_Flag = 1 << 0, @@ -184,13 +188,13 @@ SkTable_ColorFilter::SkTable_ColorFilter(SkFlattenableReadBuffer& buffer) : INHE SkASSERT(raw == count * 256); } -bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) { +bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const { if (table) { if (NULL == fBitmap) { - fBitmap = SkNEW(SkBitmap); - fBitmap->setConfig(SkBitmap::kA8_Config, 256, 4, 256); - fBitmap->allocPixels(); - uint8_t* bitmapPixels = fBitmap->getAddr8(0, 0); + SkBitmap* bmp = SkNEW(SkBitmap); + bmp->setConfig(SkBitmap::kA8_Config, 256, 4, 256); + bmp->allocPixels(); + uint8_t* bitmapPixels = bmp->getAddr8(0, 0); int offset = 0; static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag }; @@ -203,12 +207,168 @@ bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) { } bitmapPixels += 256; } + fBitmap = bmp; } *table = *fBitmap; } return true; } +#if SK_SUPPORT_GPU + +#include "GrCustomStage.h" +#include "gl/GrGLProgramStage.h" +#include "SkGr.h" + +class GLColorTableEffect; + +class ColorTableEffect : public GrCustomStage { +public: + + explicit ColorTableEffect(GrTexture* texture); + virtual ~ColorTableEffect(); + + static const char* Name() { return "ColorTable"; } + virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE; + virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE; + + virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE; + + typedef GLColorTableEffect GLProgramStage; + +private: + GR_DECLARE_CUSTOM_STAGE_TEST; + + GrTextureAccess fTextureAccess; + + typedef GrCustomStage INHERITED; +}; + +class GLColorTableEffect : public GrGLProgramStage { +public: + GLColorTableEffect(const GrProgramStageFactory& factory, + const GrCustomStage& stage); + + virtual void setupVariables(GrGLShaderBuilder* state) SK_OVERRIDE {} + virtual void emitVS(GrGLShaderBuilder* state, + const char* vertexCoords) SK_OVERRIDE {} + virtual void emitFS(GrGLShaderBuilder* state, + const char* outputColor, + const char* inputColor, + const TextureSamplerArray&) SK_OVERRIDE; + + virtual void setData(const GrGLUniformManager&, + const GrCustomStage&, + const GrRenderTarget*, + int stageNum) SK_OVERRIDE {} + + static StageKey GenKey(const GrCustomStage&, const GrGLCaps&); + +private: + + typedef GrGLProgramStage INHERITED; +}; + +GLColorTableEffect::GLColorTableEffect( + const GrProgramStageFactory& factory, const GrCustomStage& stage) + : INHERITED(factory) { + } + +void GLColorTableEffect::emitFS(GrGLShaderBuilder* builder, + const char* outputColor, + const char* inputColor, + const TextureSamplerArray& samplers) { + static const float kColorScaleFactor = 255.0f / 256.0f; + static const float kColorOffsetFactor = 1.0f / 512.0f; + SkString* code = &builder->fFSCode; + if (NULL == inputColor) { + // the input color is solid white (all ones). + static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor; + code->appendf("\t\tvec4 coord = vec4(%f, %f, %f, %f);\n", + kMaxValue, kMaxValue, kMaxValue, kMaxValue); + + } else { + code->appendf("\t\tfloat nonZeroAlpha = max(%s.a, .0001);\n", inputColor); + code->appendf("\t\tvec4 coord = vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n", inputColor); + code->appendf("\t\tcoord = coord * %f + vec4(%f, %f, %f, %f);\n", + kColorScaleFactor, + kColorOffsetFactor, kColorOffsetFactor, + kColorOffsetFactor, kColorOffsetFactor); + } + + code->appendf("\t\t%s.a = ", outputColor); + builder->appendTextureLookup(code, samplers[0], "vec2(coord.a, 0.125)"); + code->append(";\n"); + + code->appendf("\t\t%s.r = ", outputColor); + builder->appendTextureLookup(code, samplers[0], "vec2(coord.r, 0.375)"); + code->append(";\n"); + + code->appendf("\t\t%s.g = ", outputColor); + builder->appendTextureLookup(code, samplers[0], "vec2(coord.g, 0.625)"); + code->append(";\n"); + + code->appendf("\t\t%s.b = ", outputColor); + builder->appendTextureLookup(code, samplers[0], "vec2(coord.b, 0.875)"); + code->append(";\n"); + + code->appendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); +} + +GrGLProgramStage::StageKey GLColorTableEffect::GenKey(const GrCustomStage& s, + const GrGLCaps& caps) { + return 0; +} + +/////////////////////////////////////////////////////////////////////////////// + +ColorTableEffect::ColorTableEffect(GrTexture* texture) + : INHERITED(1) + , fTextureAccess(texture, "a") { +} + +ColorTableEffect::~ColorTableEffect() { +} + +const GrProgramStageFactory& ColorTableEffect::getFactory() const { + return GrTProgramStageFactory<ColorTableEffect>::getInstance(); +} + +bool ColorTableEffect::isEqual(const GrCustomStage& sBase) const { + return INHERITED::isEqual(sBase); +} + +const GrTextureAccess& ColorTableEffect::textureAccess(int index) const { + GrAssert(0 == index); + return fTextureAccess; +} + +/////////////////////////////////////////////////////////////////////////////// + +GR_DEFINE_CUSTOM_STAGE_TEST(ColorTableEffect); + +GrCustomStage* ColorTableEffect::TestCreate(SkRandom* random, + GrContext* context, + GrTexture* textures[]) { + return SkNEW_ARGS(ColorTableEffect, (textures[GrCustomStageUnitTest::kAlphaTextureIdx])); +} + +GrCustomStage* SkTable_ColorFilter::asNewCustomStage(GrContext* context) const { + SkBitmap bitmap; + this->asComponentTable(&bitmap); + // passing NULL because this custom effect does no tiling or filtering. + GrTexture* texture = GrLockCachedBitmapTexture(context, bitmap, NULL); + GrCustomStage* stage = SkNEW_ARGS(ColorTableEffect, (texture)); + + // Unlock immediately, this is not great, but we don't have a way of + // knowing when else to unlock it currently. TODO: Remove this when + // unref becomes the unlock replacement for all types of textures. + GrUnlockCachedBitmapTexture(texture); + return stage; +} + +#endif // SK_SUPPORT_GPU + /////////////////////////////////////////////////////////////////////////////// #ifdef SK_CPU_BENDIAN diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp index 7ea1e756f5..5506d1ab99 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -7,7 +7,6 @@ #include "SkGpuDevice.h" -#include "effects/GrColorTableEffect.h" #include "effects/GrTextureDomainEffect.h" #include "GrContext.h" @@ -513,19 +512,11 @@ inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev, if (NULL != stage.get()) { grPaint->colorSampler(kColorFilterTextureIdx)->setCustomStage(stage); } else { - // TODO: rewrite these using asNewCustomStage() + // TODO: rewrite this using asNewCustomStage() SkColor color; SkXfermode::Mode filterMode; - SkBitmap colorTransformTable; if (colorFilter->asColorMode(&color, &filterMode)) { grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color)); - } else if (colorFilter != NULL && - colorFilter->asComponentTable(&colorTransformTable)) { - // pass NULL because the color table effect doesn't use tiling or filtering. - GrTexture* texture = act->set(dev, colorTransformTable, NULL); - GrSamplerState* colorSampler = grPaint->colorSampler(kColorFilterTextureIdx); - colorSampler->reset(); - colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref(); } } } diff --git a/src/gpu/effects/GrColorTableEffect.cpp b/src/gpu/effects/GrColorTableEffect.cpp deleted file mode 100644 index 165cd60443..0000000000 --- a/src/gpu/effects/GrColorTableEffect.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - #include "GrColorTableEffect.h" - #include "gl/GrGLProgramStage.h" - #include "GrProgramStageFactory.h" - #include "SkString.h" - -/////////////////////////////////////////////////////////////////////////////// - -class GrGLColorTableEffect : public GrGLProgramStage { -public: - GrGLColorTableEffect(const GrProgramStageFactory& factory, - const GrCustomStage& stage); - - virtual void setupVariables(GrGLShaderBuilder* state) SK_OVERRIDE {} - virtual void emitVS(GrGLShaderBuilder* state, - const char* vertexCoords) SK_OVERRIDE {} - virtual void emitFS(GrGLShaderBuilder* state, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray&) SK_OVERRIDE; - - virtual void setData(const GrGLUniformManager&, - const GrCustomStage&, - const GrRenderTarget*, - int stageNum) SK_OVERRIDE {} - - static StageKey GenKey(const GrCustomStage&, const GrGLCaps&); - -private: - - typedef GrGLProgramStage INHERITED; -}; - -GrGLColorTableEffect::GrGLColorTableEffect( - const GrProgramStageFactory& factory, const GrCustomStage& stage) - : INHERITED(factory) { - } - -void GrGLColorTableEffect::emitFS(GrGLShaderBuilder* builder, - const char* outputColor, - const char* inputColor, - const TextureSamplerArray& samplers) { - static const float kColorScaleFactor = 255.0f / 256.0f; - static const float kColorOffsetFactor = 1.0f / 512.0f; - SkString* code = &builder->fFSCode; - if (NULL == inputColor) { - // the input color is solid white (all ones). - static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor; - code->appendf("\t\tvec4 coord = vec4(%f, %f, %f, %f);\n", - kMaxValue, kMaxValue, kMaxValue, kMaxValue); - - } else { - code->appendf("\t\tvec4 coord = vec4(%s.rgb / %s.a, %s.a);\n", - inputColor, inputColor, inputColor); - code->appendf("\t\tcoord = coord * %f + vec4(%f, %f, %f, %f);\n", - kColorScaleFactor, - kColorOffsetFactor, kColorOffsetFactor, - kColorOffsetFactor, kColorOffsetFactor); - } - - code->appendf("\t\t%s.a = ", outputColor); - builder->appendTextureLookup(code, samplers[0], "vec2(coord.a, 0.125)"); - code->append(";\n"); - - code->appendf("\t\t%s.r = ", outputColor); - builder->appendTextureLookup(code, samplers[0], "vec2(coord.r, 0.375)"); - code->append(";\n"); - - code->appendf("\t\t%s.g = ", outputColor); - builder->appendTextureLookup(code, samplers[0], "vec2(coord.g, 0.625)"); - code->append(";\n"); - - code->appendf("\t\t%s.b = ", outputColor); - builder->appendTextureLookup(code, samplers[0], "vec2(coord.b, 0.875)"); - code->append(";\n"); - - code->appendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); -} - -GrGLProgramStage::StageKey GrGLColorTableEffect::GenKey(const GrCustomStage& s, - const GrGLCaps& caps) { - return 0; -} - -/////////////////////////////////////////////////////////////////////////////// - -GrColorTableEffect::GrColorTableEffect(GrTexture* texture) - : INHERITED(1) - , fTextureAccess(texture, "a") { -} - -GrColorTableEffect::~GrColorTableEffect() { -} - -const GrProgramStageFactory& GrColorTableEffect::getFactory() const { - return GrTProgramStageFactory<GrColorTableEffect>::getInstance(); -} - -bool GrColorTableEffect::isEqual(const GrCustomStage& sBase) const { - return INHERITED::isEqual(sBase); -} - -const GrTextureAccess& GrColorTableEffect::textureAccess(int index) const { - GrAssert(0 == index); - return fTextureAccess; -} - -/////////////////////////////////////////////////////////////////////////////// - -GR_DEFINE_CUSTOM_STAGE_TEST(GrColorTableEffect); - -GrCustomStage* GrColorTableEffect::TestCreate(SkRandom* random, - GrContext* context, - GrTexture* textures[]) { - return SkNEW_ARGS(GrColorTableEffect, (textures[GrCustomStageUnitTest::kAlphaTextureIdx])); -} diff --git a/src/gpu/effects/GrColorTableEffect.h b/src/gpu/effects/GrColorTableEffect.h deleted file mode 100644 index c064600c26..0000000000 --- a/src/gpu/effects/GrColorTableEffect.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef GrColorTableEffect_DEFINED -#define GrColorTableEffect_DEFINED - -#include "GrSingleTextureEffect.h" -#include "GrTexture.h" - -class GrGLColorTableEffect; - -/** - * LUT-based color transformation effect. This class implements the Gr - * counterpart to the SkTable_ColorFilter effect. A 256 * 4 (single-channel) - * LUT is used to transform the input colors of the image. - */ -class GrColorTableEffect : public GrCustomStage { -public: - - explicit GrColorTableEffect(GrTexture* texture); - virtual ~GrColorTableEffect(); - - static const char* Name() { return "ColorTable"; } - virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE; - virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE; - - virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE; - - typedef GrGLColorTableEffect GLProgramStage; - -private: - GR_DECLARE_CUSTOM_STAGE_TEST; - - GrTextureAccess fTextureAccess; - - typedef GrCustomStage INHERITED; -}; -#endif |