diff options
author | senorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-05-31 17:49:12 +0000 |
---|---|---|
committer | senorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-05-31 17:49:12 +0000 |
commit | 86fc266eda887920e3dd104bee8121ae19729cf5 (patch) | |
tree | ca43dd64735b058926a9efaf6e3c9a7cf65e2fd4 /src/effects | |
parent | 70fdc128377bac81dac987fdc996a1b4e269e1fb (diff) |
Implement SkXfermode image filter. This required changing the signature of SkXfermode::asNewEffectOrCoeffs(), to add an optional background texture.
For the raster path, we do a straightforward 2-pass method: draw background, then composite the foreground over it.
For the GPU path, if the xfermode can be expressed as an effect, we build an effect with the background texture incorporated, then do a single-pass draw fetching both foreground and background textures, and compositing to the result. If the xfermode is expressed as src/dst coefficients, we do a 2-pass draw as in the raster path and use fixed-function blending.
R=bsalomon@google.com, reed@google.com
Review URL: https://codereview.chromium.org/16125008
git-svn-id: http://skia.googlecode.com/svn/trunk@9373 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/effects')
-rw-r--r-- | src/effects/SkArithmeticMode.cpp | 76 | ||||
-rw-r--r-- | src/effects/SkXfermodeImageFilter.cpp | 119 |
2 files changed, 180 insertions, 15 deletions
diff --git a/src/effects/SkArithmeticMode.cpp b/src/effects/SkArithmeticMode.cpp index 1d06264877..e9dc7824bc 100644 --- a/src/effects/SkArithmeticMode.cpp +++ b/src/effects/SkArithmeticMode.cpp @@ -13,6 +13,7 @@ #if SK_SUPPORT_GPU #include "GrContext.h" #include "gl/GrGLEffect.h" +#include "gl/GrGLEffectMatrix.h" #include "GrTBackendEffectFactory.h" #include "SkImageFilterUtils.h" #endif @@ -33,7 +34,7 @@ public: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkArithmeticMode_scalar) #if SK_SUPPORT_GPU - virtual bool asNewEffectOrCoeff(GrContext*, GrEffectRef** effect, Coeff*, Coeff*) const SK_OVERRIDE; + virtual bool asNewEffectOrCoeff(GrContext*, GrEffectRef** effect, Coeff*, Coeff*, GrTexture* background) const SK_OVERRIDE; #endif private: @@ -240,7 +241,7 @@ public: private: static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; - + GrGLEffectMatrix fBackgroundEffectMatrix; GrGLUniformManager::UniformHandle fKUni; typedef GrGLEffect INHERITED; @@ -250,8 +251,8 @@ private: class GrArithmeticEffect : public GrEffect { public: - static GrEffectRef* Create(float k1, float k2, float k3, float k4) { - AutoEffectUnref effect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4))); + static GrEffectRef* Create(float k1, float k2, float k3, float k4, GrTexture* background) { + AutoEffectUnref effect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4, background))); return CreateEffectRef(effect); } @@ -261,6 +262,7 @@ public: typedef GrGLArithmeticEffect GLEffect; static const char* Name() { return "Arithmetic"; } + GrTexture* backgroundTexture() const { return fBackgroundAccess.getTexture(); } virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; @@ -272,8 +274,9 @@ public: private: virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; - GrArithmeticEffect(float k1, float k2, float k3, float k4); + GrArithmeticEffect(float k1, float k2, float k3, float k4, GrTexture* background); float fK1, fK2, fK3, fK4; + GrTextureAccess fBackgroundAccess; GR_DECLARE_EFFECT_TEST; typedef GrEffect INHERITED; @@ -282,9 +285,15 @@ private: /////////////////////////////////////////////////////////////////////////////// -GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4) +GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4, + GrTexture* background) : fK1(k1), fK2(k2), fK3(k3), fK4(k4) { - this->setWillReadDstColor(); + if (background) { + fBackgroundAccess.reset(background); + this->addTextureAccess(&fBackgroundAccess); + } else { + this->setWillReadDstColor(); + } } GrArithmeticEffect::~GrArithmeticEffect() { @@ -295,7 +304,8 @@ bool GrArithmeticEffect::onIsEqual(const GrEffect& sBase) const { return fK1 == s.fK1 && fK2 == s.fK2 && fK3 == s.fK3 && - fK4 == s.fK4; + fK4 == s.fK4 && + backgroundTexture() == s.backgroundTexture(); } const GrBackendEffectFactory& GrArithmeticEffect::getFactory() const { @@ -310,19 +320,37 @@ void GrArithmeticEffect::getConstantColorComponents(GrColor* color, uint32_t* va /////////////////////////////////////////////////////////////////////////////// GrGLArithmeticEffect::GrGLArithmeticEffect(const GrBackendEffectFactory& factory, - const GrDrawEffect& drawEffect) : INHERITED(factory) { + const GrDrawEffect& drawEffect) + : INHERITED(factory) + , fBackgroundEffectMatrix(kCoordsType) { } GrGLArithmeticEffect::~GrGLArithmeticEffect() { } void GrGLArithmeticEffect::emitCode(GrGLShaderBuilder* builder, - const GrDrawEffect&, + const GrDrawEffect& drawEffect, EffectKey key, const char* outputColor, const char* inputColor, const TextureSamplerArray& samplers) { - const char* dstColor = builder->dstColor(); + + GrTexture* backgroundTex = drawEffect.castEffect<GrArithmeticEffect>().backgroundTexture(); + const char* dstColor; + if (backgroundTex) { + const char* bgCoords; + GrSLType bgCoordsType = fBackgroundEffectMatrix.emitCode(builder, key, &bgCoords, NULL, "BG"); + builder->fsCodeAppend("\t\tvec4 bgColor = "); + builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, + samplers[0], + bgCoords, + bgCoordsType); + builder->fsCodeAppendf(";\n"); + dstColor = "bgColor"; + } else { + dstColor = builder->dstColor(); + } + GrAssert(NULL != dstColor); fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, kVec4f_GrSLType, "k"); @@ -347,10 +375,26 @@ void GrGLArithmeticEffect::emitCode(GrGLShaderBuilder* builder, void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(); uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); + GrTexture* bgTex = arith.backgroundTexture(); + if (bgTex) { + fBackgroundEffectMatrix.setData(uman, + GrEffect::MakeDivByTextureWHMatrix(bgTex), + drawEffect, + bgTex); + } } GrGLEffect::EffectKey GrGLArithmeticEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { - return 0; + const GrArithmeticEffect& effect = drawEffect.castEffect<GrArithmeticEffect>(); + GrTexture* bgTex = effect.backgroundTexture(); + EffectKey bgKey = 0; + if (bgTex) { + bgKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMatrix(bgTex), + drawEffect, + GrGLArithmeticEffect::kCoordsType, + bgTex); + } + return bgKey; } GrEffectRef* GrArithmeticEffect::TestCreate(SkMWCRandom* rand, @@ -362,7 +406,7 @@ GrEffectRef* GrArithmeticEffect::TestCreate(SkMWCRandom* rand, float k3 = rand->nextF(); float k4 = rand->nextF(); - static AutoEffectUnref gEffect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4))); + static AutoEffectUnref gEffect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4, NULL))); return CreateEffectRef(gEffect); } @@ -371,12 +415,14 @@ GR_DEFINE_EFFECT_TEST(GrArithmeticEffect); bool SkArithmeticMode_scalar::asNewEffectOrCoeff(GrContext*, GrEffectRef** effect, Coeff*, - Coeff*) const { + Coeff*, + GrTexture* background) const { if (effect) { *effect = GrArithmeticEffect::Create(SkScalarToFloat(fK[0]), SkScalarToFloat(fK[1]), SkScalarToFloat(fK[2]), - SkScalarToFloat(fK[3])); + SkScalarToFloat(fK[3]), + background); } return true; } diff --git a/src/effects/SkXfermodeImageFilter.cpp b/src/effects/SkXfermodeImageFilter.cpp new file mode 100644 index 0000000000..5f2d3689e8 --- /dev/null +++ b/src/effects/SkXfermodeImageFilter.cpp @@ -0,0 +1,119 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkXfermodeImageFilter.h" +#include "SkCanvas.h" +#include "SkColorPriv.h" +#include "SkFlattenableBuffers.h" +#include "SkXfermode.h" +#if SK_SUPPORT_GPU +#include "GrContext.h" +#include "effects/GrSimpleTextureEffect.h" +#include "SkGr.h" +#include "SkImageFilterUtils.h" +#endif + +/////////////////////////////////////////////////////////////////////////////// + +SkXfermodeImageFilter::SkXfermodeImageFilter(SkXfermode* mode, SkImageFilter* background, SkImageFilter* foreground) + : INHERITED(background, foreground), fMode(mode) { + SkSafeRef(fMode); +} + +SkXfermodeImageFilter::~SkXfermodeImageFilter() { + SkSafeUnref(fMode); +} + +SkXfermodeImageFilter::SkXfermodeImageFilter(SkFlattenableReadBuffer& buffer) + : INHERITED(buffer) { + fMode = buffer.readFlattenableT<SkXfermode>(); +} + +void SkXfermodeImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { + this->INHERITED::flatten(buffer); + buffer.writeFlattenable(fMode); +} + +bool SkXfermodeImageFilter::onFilterImage(Proxy* proxy, + const SkBitmap& src, + const SkMatrix& ctm, + SkBitmap* dst, + SkIPoint* offset) { + SkBitmap background = src, foreground = src; + SkImageFilter* backgroundInput = getInput(0); + SkImageFilter* foregroundInput = getInput(1); + if (backgroundInput && !backgroundInput->filterImage(proxy, src, ctm, &background, offset)) { + return false; + } + if (foregroundInput && !foregroundInput->filterImage(proxy, src, ctm, &foreground, offset)) { + return false; + } + dst->setConfig(background.config(), background.width(), background.height()); + dst->allocPixels(); + SkCanvas canvas(*dst); + SkPaint paint; + paint.setXfermodeMode(SkXfermode::kSrc_Mode); + canvas.drawBitmap(background, 0, 0, &paint); + paint.setXfermode(fMode); + canvas.drawBitmap(foreground, 0, 0, &paint); + return true; +} + +#if SK_SUPPORT_GPU + +bool SkXfermodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, SkBitmap* result) { + SkBitmap background; + if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, &background)) { + return false; + } + GrTexture* backgroundTex = (GrTexture*) background.getTexture(); + SkBitmap foreground; + if (!SkImageFilterUtils::GetInputResultGPU(getInput(1), proxy, src, &foreground)) { + return false; + } + GrTexture* foregroundTex = (GrTexture*) foreground.getTexture(); + GrContext* context = foregroundTex->getContext(); + + GrEffectRef* xferEffect = NULL; + + GrTextureDesc desc; + desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; + desc.fWidth = src.width(); + desc.fHeight = src.height(); + desc.fConfig = kSkia8888_GrPixelConfig; + + GrAutoScratchTexture ast(context, desc); + SkAutoTUnref<GrTexture> dst(ast.detach()); + + GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); + + SkXfermode::Coeff sm, dm; + if (!SkXfermode::AsNewEffectOrCoeff(fMode, context, &xferEffect, &sm, &dm, backgroundTex)) { + return false; + } + + GrPaint paint; + SkRect srcRect; + src.getBounds(&srcRect); + if (NULL != xferEffect) { + paint.colorStage(0)->setEffect( + GrSimpleTextureEffect::Create(foregroundTex, GrEffect::MakeDivByTextureWHMatrix(foregroundTex)))->unref(); + paint.colorStage(1)->setEffect(xferEffect); + context->drawRect(paint, srcRect); + } else { + paint.colorStage(0)->setEffect( + GrSimpleTextureEffect::Create(backgroundTex, GrEffect::MakeDivByTextureWHMatrix(backgroundTex)))->unref(); + context->drawRect(paint, srcRect); + paint.setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm)); + paint.colorStage(0)->setEffect( + GrSimpleTextureEffect::Create(foregroundTex, GrEffect::MakeDivByTextureWHMatrix(foregroundTex)))->unref(); + context->drawRect(paint, srcRect); + } + return SkImageFilterUtils::WrapTexture(dst, src.width(), src.height(), result); +} + +#endif |