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
diff --git a/src/effects/SkXfermodeImageFilter.cpp b/src/effects/SkXfermodeImageFilter.cpp
new file mode 100644
index 0000000..5f2d368
--- /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