Implement a bicubic resampling image filter, with raster and GPU backends.
authorsenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 18 Jan 2013 17:29:15 +0000 (17:29 +0000)
committersenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 18 Jan 2013 17:29:15 +0000 (17:29 +0000)
In order to get this to work on the GPU side, I had to modify the width and height of the drawn texture in drawSprite() and drawDevice() to use the filtered texture's dimensions, instead of the source texture.  (This wasn't a problem before since all other image filters produce results the same dimensions as their input texture.)
For now, this implementation only does axis-aligned scaling (same as the Lanczos-3 implementation in Chrome).  It's also done for correctness and clarity, not speed, so there are lots of opportunities for speedups.

Review URL: https://codereview.appspot.com/7033049

git-svn-id: http://skia.googlecode.com/svn/trunk@7275 2bbb7eff-a529-9590-31e7-b0007b416f81

gm/bicubicfilter.cpp [new file with mode: 0644]
gyp/effects.gypi
gyp/gmslides.gypi
include/effects/SkBicubicImageFilter.h [new file with mode: 0644]
src/effects/SkBicubicImageFilter.cpp [new file with mode: 0644]
src/gpu/SkGpuDevice.cpp
src/ports/SkGlobalInitialization_default.cpp

diff --git a/gm/bicubicfilter.cpp b/gm/bicubicfilter.cpp
new file mode 100644 (file)
index 0000000..c3f80df
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "SkColor.h"
+#include "SkBicubicImageFilter.h"
+
+namespace skiagm {
+
+class BicubicGM : public GM {
+public:
+    BicubicGM() : fInitialized(false) {
+        this->setBGColor(0x00000000);
+    }
+
+protected:
+    virtual SkString onShortName() {
+        return SkString("bicubicfilter");
+    }
+
+    void make_checkerboard(int width, int height) {
+        SkASSERT(width % 2 == 0);
+        SkASSERT(height % 2 == 0);
+        fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, width, height);
+        fCheckerboard.allocPixels();
+        SkAutoLockPixels lock(fCheckerboard);
+        for (int y = 0; y < height; y += 2) {
+            SkPMColor* s = fCheckerboard.getAddr32(0, y);
+            for (int x = 0; x < width; x += 2) {
+                *s++ = 0xFFFFFFFF;
+                *s++ = 0xFF000000;
+            }
+            s = fCheckerboard.getAddr32(0, y + 1);
+            for (int x = 0; x < width; x += 2) {
+                *s++ = 0xFF000000;
+                *s++ = 0xFFFFFFFF;
+            }
+        }
+    }
+
+    virtual SkISize onISize() {
+        return make_isize(400, 300);
+    }
+
+    virtual void onDraw(SkCanvas* canvas) {
+        if (!fInitialized) {
+            make_checkerboard(4, 4);
+            fInitialized = true;
+        }
+        SkScalar sk32 = SkIntToScalar(32);
+        canvas->clear(0x00000000);
+        SkPaint bilinearPaint, bicubicPaint;
+        SkSize scale = SkSize::Make(sk32, sk32);
+        canvas->save();
+        canvas->scale(sk32, sk32);
+        bilinearPaint.setFilterBitmap(true);
+        canvas->drawBitmap(fCheckerboard, 0, 0, &bilinearPaint);
+        canvas->restore();
+        SkAutoTUnref<SkImageFilter> bicubic(SkBicubicImageFilter::CreateMitchell(scale));
+        bicubicPaint.setImageFilter(bicubic);
+        SkRect srcBounds;
+        fCheckerboard.getBounds(&srcBounds);
+        canvas->translate(SkIntToScalar(140), 0);
+        canvas->saveLayer(&srcBounds, &bicubicPaint);
+        canvas->drawBitmap(fCheckerboard, 0, 0);
+        canvas->restore();
+    }
+
+private:
+    typedef GM INHERITED;
+    SkBitmap fCheckerboard;
+    bool fInitialized;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static GM* MyFactory(void*) { return new BicubicGM; }
+static GMRegistry reg(MyFactory);
+
+}
index b8defb4..8c61c34 100644 (file)
@@ -11,6 +11,7 @@
     '<(skia_src_path)/effects/Sk2DPathEffect.cpp',
     '<(skia_src_path)/effects/SkAvoidXfermode.cpp',
     '<(skia_src_path)/effects/SkArithmeticMode.cpp',
+    '<(skia_src_path)/effects/SkBicubicImageFilter.cpp',
     '<(skia_src_path)/effects/SkBitmapSource.cpp',
     '<(skia_src_path)/effects/SkBlendImageFilter.cpp',
     '<(skia_src_path)/effects/SkBlurDrawLooper.cpp',
index a50d084..8d772f3 100644 (file)
@@ -4,6 +4,7 @@
     '../gm/aaclip.cpp',
     '../gm/aarectmodes.cpp',
     '../gm/arithmode.cpp',
+    '../gm/bicubicfilter.cpp',
     '../gm/bigmatrix.cpp',
     '../gm/bitmapcopy.cpp',
     '../gm/bitmapmatrix.cpp',
diff --git a/include/effects/SkBicubicImageFilter.h b/include/effects/SkBicubicImageFilter.h
new file mode 100644 (file)
index 0000000..682b460
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+
+#ifndef SkBicubicImageFilter_DEFINED
+#define SkBicubicImageFilter_DEFINED
+
+#include "SkSingleInputImageFilter.h"
+#include "SkScalar.h"
+#include "SkSize.h"
+#include "SkPoint.h"
+
+/*! \class SkBicubicImageFilter
+    Bicubic resampling image filter.  This filter does a 16-tap bicubic
+    filter using the given matrix.
+ */
+
+class SK_API SkBicubicImageFilter : public SkSingleInputImageFilter {
+public:
+    /** Construct a (scaling-only) bicubic resampling image filter.
+        @param scale        How much to scale the image.
+        @param coefficients The 16 coefficients of the bicubic matrix.
+        @param input        The input image filter.  If NULL, the src bitmap
+                            passed to filterImage() is used instead.
+    */
+
+    SkBicubicImageFilter(const SkSize& scale, const SkScalar coefficients[16], SkImageFilter* input = NULL);
+    static SkBicubicImageFilter* CreateMitchell(const SkSize& scale, SkImageFilter* input = NULL);
+    virtual ~SkBicubicImageFilter();
+
+    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBicubicImageFilter)
+
+protected:
+    SkBicubicImageFilter(SkFlattenableReadBuffer& buffer);
+    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
+
+    virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&,
+                               SkBitmap* result, SkIPoint* loc) SK_OVERRIDE;
+
+#if SK_SUPPORT_GPU
+    virtual bool canFilterImageGPU() const SK_OVERRIDE { return true; }
+    virtual GrTexture* filterImageGPU(Proxy* proxy, GrTexture* src,
+                                      const SkRect& rect) SK_OVERRIDE;
+#endif
+
+private:
+    SkSize    fScale;
+    SkScalar  fCoefficients[16];
+    typedef SkSingleInputImageFilter INHERITED;
+};
+
+#endif
diff --git a/src/effects/SkBicubicImageFilter.cpp b/src/effects/SkBicubicImageFilter.cpp
new file mode 100644 (file)
index 0000000..b02305a
--- /dev/null
@@ -0,0 +1,362 @@
+/*
+ * 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 "SkBicubicImageFilter.h"
+#include "SkBitmap.h"
+#include "SkColorPriv.h"
+#include "SkFlattenableBuffers.h"
+#include "SkRect.h"
+#include "SkUnPreMultiply.h"
+
+#if SK_SUPPORT_GPU
+#include "gl/GrGLEffectMatrix.h"
+#include "effects/GrSingleTextureEffect.h"
+#include "GrTBackendEffectFactory.h"
+#include "GrContext.h"
+#include "GrTexture.h"
+#endif
+
+SkBicubicImageFilter::SkBicubicImageFilter(const SkSize& scale, const SkScalar coefficients[16], SkImageFilter* input)
+  : INHERITED(input),
+    fScale(scale) {
+    memcpy(fCoefficients, coefficients, sizeof(fCoefficients));
+}
+
+#define DS(x) SkDoubleToScalar(x)
+
+SkBicubicImageFilter* SkBicubicImageFilter::CreateMitchell(const SkSize& scale,
+                                                           SkImageFilter* input) {
+    static const SkScalar coefficients[16] = {
+        DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), 
+        DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), 
+        DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), 
+        DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS(  7.0 / 18.0), 
+    };
+    return SkNEW_ARGS(SkBicubicImageFilter, (scale, coefficients, input));
+}
+
+SkBicubicImageFilter::SkBicubicImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
+    uint32_t readSize = buffer.readScalarArray(fCoefficients);
+    SkASSERT(readSize == 16);
+    fScale.fWidth = buffer.readScalar();
+    fScale.fHeight = buffer.readScalar();
+}
+
+void SkBicubicImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
+    this->INHERITED::flatten(buffer);
+    buffer.writeScalarArray(fCoefficients, 16);
+    buffer.writeScalar(fScale.fWidth);
+    buffer.writeScalar(fScale.fHeight);
+}
+
+SkBicubicImageFilter::~SkBicubicImageFilter() {
+}
+
+inline SkPMColor cubicBlend(const SkScalar c[16], SkScalar t, SkPMColor c0, SkPMColor c1, SkPMColor c2, SkPMColor c3) {
+    SkScalar t2 = t * t, t3 = t2 * t;
+    SkScalar cc[4];
+    // FIXME:  For the fractx case, this should be refactored out of this function.
+    cc[0] = c[0]  + SkScalarMul(c[1], t) + SkScalarMul(c[2], t2) + SkScalarMul(c[3], t3);
+    cc[1] = c[4]  + SkScalarMul(c[5], t) + SkScalarMul(c[6], t2) + SkScalarMul(c[7], t3);
+    cc[2] = c[8]  + SkScalarMul(c[9], t) + SkScalarMul(c[10], t2) + SkScalarMul(c[11], t3);
+    cc[3] = c[12] + SkScalarMul(c[13], t) + SkScalarMul(c[14], t2) + SkScalarMul(c[15], t3);
+    SkScalar a = SkScalarMul(cc[0], SkGetPackedA32(c0)) + SkScalarMul(cc[1], SkGetPackedA32(c1)) + SkScalarMul(cc[2], SkGetPackedA32(c2)) + SkScalarMul(cc[3], SkGetPackedA32(c3));
+    SkScalar r = SkScalarMul(cc[0], SkGetPackedR32(c0)) + SkScalarMul(cc[1], SkGetPackedR32(c1)) + SkScalarMul(cc[2], SkGetPackedR32(c2)) + SkScalarMul(cc[3], SkGetPackedR32(c3));
+    SkScalar g = SkScalarMul(cc[0], SkGetPackedG32(c0)) + SkScalarMul(cc[1], SkGetPackedG32(c1)) + SkScalarMul(cc[2], SkGetPackedG32(c2)) + SkScalarMul(cc[3], SkGetPackedG32(c3));
+    SkScalar b = SkScalarMul(cc[0], SkGetPackedB32(c0)) + SkScalarMul(cc[1], SkGetPackedB32(c1)) + SkScalarMul(cc[2], SkGetPackedB32(c2)) + SkScalarMul(cc[3], SkGetPackedB32(c3));
+    return SkPackARGB32(SkScalarRoundToInt(SkScalarClampMax(a, 255)), SkScalarRoundToInt(SkScalarClampMax(r, 255)), SkScalarRoundToInt(SkScalarClampMax(g, 255)), SkScalarRoundToInt(SkScalarClampMax(b, 255)));
+}
+
+bool SkBicubicImageFilter::onFilterImage(Proxy* proxy,
+                                         const SkBitmap& source,
+                                         const SkMatrix& matrix,
+                                         SkBitmap* result,
+                                         SkIPoint* loc) {
+    SkBitmap src = this->getInputResult(proxy, source, matrix, loc);
+    if (src.config() != SkBitmap::kARGB_8888_Config) {
+        return false;
+    }
+
+    SkAutoLockPixels alp(src);
+    if (!src.getPixels()) {
+        return false;
+    }
+
+    SkRect dstRect = SkRect::MakeWH(SkScalarMul(SkIntToScalar(src.width()), fScale.fWidth),
+                                    SkScalarMul(SkIntToScalar(src.height()), fScale.fHeight));
+    SkIRect dstIRect;
+    dstRect.roundOut(&dstIRect);
+    result->setConfig(src.config(), dstIRect.width(), dstIRect.height());
+    result->allocPixels();
+    if (!result->getPixels()) {
+        return false;
+    }
+
+    SkRect srcRect;
+    src.getBounds(&srcRect);
+    SkMatrix inverse;
+    inverse.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
+    inverse.postTranslate(SkFloatToScalar(-0.5f), SkFloatToScalar(-0.5f));
+
+    for (int y = dstIRect.fTop; y < dstIRect.fBottom; ++y) {
+        SkPMColor* dptr = result->getAddr32(dstIRect.fLeft, y);
+        for (int x = dstIRect.fLeft; x < dstIRect.fRight; ++x) {
+            SkPoint srcPt, dstPt = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
+            inverse.mapPoints(&srcPt, &dstPt, 1);
+            SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
+            SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
+            int sx = SkScalarFloorToInt(srcPt.fX);
+            int sy = SkScalarFloorToInt(srcPt.fY);
+            int x0 = SkClampMax(sx - 1, src.width() - 1);
+            int x1 = SkClampMax(sx    , src.width() - 1);
+            int x2 = SkClampMax(sx + 1, src.width() - 1);
+            int x3 = SkClampMax(sx + 2, src.width() - 1);
+            int y0 = SkClampMax(sy - 1, src.height() - 1);
+            int y1 = SkClampMax(sy    , src.height() - 1);
+            int y2 = SkClampMax(sy + 1, src.height() - 1);
+            int y3 = SkClampMax(sy + 2, src.height() - 1);
+            SkPMColor s00 = *src.getAddr32(x0, y0);
+            SkPMColor s10 = *src.getAddr32(x1, y0);
+            SkPMColor s20 = *src.getAddr32(x2, y0);
+            SkPMColor s30 = *src.getAddr32(x3, y0);
+            SkPMColor s0 = cubicBlend(fCoefficients, fractx, s00, s10, s20, s30);
+            SkPMColor s01 = *src.getAddr32(x0, y1);
+            SkPMColor s11 = *src.getAddr32(x1, y1);
+            SkPMColor s21 = *src.getAddr32(x2, y1);
+            SkPMColor s31 = *src.getAddr32(x3, y1);
+            SkPMColor s1 = cubicBlend(fCoefficients, fractx, s01, s11, s21, s31);
+            SkPMColor s02 = *src.getAddr32(x0, y2);
+            SkPMColor s12 = *src.getAddr32(x1, y2);
+            SkPMColor s22 = *src.getAddr32(x2, y2);
+            SkPMColor s32 = *src.getAddr32(x3, y2);
+            SkPMColor s2 = cubicBlend(fCoefficients, fractx, s02, s12, s22, s32);
+            SkPMColor s03 = *src.getAddr32(x0, y3);
+            SkPMColor s13 = *src.getAddr32(x1, y3);
+            SkPMColor s23 = *src.getAddr32(x2, y3);
+            SkPMColor s33 = *src.getAddr32(x3, y3);
+            SkPMColor s3 = cubicBlend(fCoefficients, fractx, s03, s13, s23, s33);
+            *dptr++ = cubicBlend(fCoefficients, fracty, s0, s1, s2, s3);
+        }
+    }
+    return true;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+class GrGLBicubicEffect;
+
+class GrBicubicEffect : public GrSingleTextureEffect {
+public:
+    virtual ~GrBicubicEffect();
+
+    static const char* Name() { return "Bicubic"; }
+    const float* coefficients() const { return fCoefficients; }
+
+    typedef GrGLBicubicEffect GLEffect;
+
+    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
+    virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
+    virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
+
+    static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16]) {
+        SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients)));
+        return CreateEffectRef(effect);
+    }
+
+private:
+    GrBicubicEffect(GrTexture*, const SkScalar coefficients[16]);
+    float    fCoefficients[16];
+
+    GR_DECLARE_EFFECT_TEST;
+
+    typedef GrSingleTextureEffect INHERITED;
+};
+
+class GrGLBicubicEffect : public GrGLEffect {
+public:
+    GrGLBicubicEffect(const GrBackendEffectFactory& factory,
+                                const GrEffect& effect);
+    virtual void emitCode(GrGLShaderBuilder*,
+                          const GrEffectStage&,
+                          EffectKey,
+                          const char* vertexCoords,
+                          const char* outputColor,
+                          const char* inputColor,
+                          const TextureSamplerArray&) SK_OVERRIDE;
+
+    static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
+
+    virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
+
+private:
+    typedef GrGLUniformManager::UniformHandle        UniformHandle;
+
+    UniformHandle       fCoefficientsUni;
+    UniformHandle       fImageIncrementUni;
+
+    GrGLEffectMatrix    fEffectMatrix;
+
+    typedef GrGLEffect INHERITED;
+};
+
+GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory,
+                                     const GrEffect& effect)
+    : INHERITED(factory)
+    , fCoefficientsUni(GrGLUniformManager::kInvalidUniformHandle)
+    , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) {
+    const GrBicubicEffect& m = static_cast<const GrBicubicEffect&>(effect);
+}
+
+void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
+                                 const GrEffectStage&,
+                                 EffectKey key,
+                                 const char* vertexCoords,
+                                 const char* outputColor,
+                                 const char* inputColor,
+                                 const TextureSamplerArray& samplers) {
+    const char* coords;
+    fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
+    fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                                           kMat44f_GrSLType, "Coefficients");
+    fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                                             kVec2f_GrSLType, "ImageIncrement");
+    SkString* code = &builder->fFSCode;
+
+    const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
+    const char* coeff = builder->getUniformCStr(fCoefficientsUni);
+
+    SkString cubicBlendName;
+
+    static const GrGLShaderVar gCubicBlendArgs[] = {
+        GrGLShaderVar("coefficients",  kMat44f_GrSLType),
+        GrGLShaderVar("t",             kFloat_GrSLType),
+        GrGLShaderVar("c0",            kVec4f_GrSLType),
+        GrGLShaderVar("c1",            kVec4f_GrSLType),
+        GrGLShaderVar("c2",            kVec4f_GrSLType),
+        GrGLShaderVar("c3",            kVec4f_GrSLType),
+    };
+    builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType,
+                          kVec4f_GrSLType,
+                          "cubicBlend",
+                          SK_ARRAY_COUNT(gCubicBlendArgs),
+                          gCubicBlendArgs,
+                          "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
+                          "\tvec4 c = coefficients * ts;\n"
+                          "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n",
+                          &cubicBlendName);
+    code->appendf("\tvec2 coord = %s - %s * vec2(0.5, 0.5);\n", coords, imgInc);
+    code->appendf("\tvec2 f = fract(coord / %s);\n", imgInc);
+    for (int y = 0; y < 4; ++y) {
+        for (int x = 0; x < 4; ++x) {
+            SkString coord;
+            coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
+            code->appendf("\tvec4 s%d%d = ", x, y);
+            builder->appendTextureLookup(&builder->fFSCode, samplers[0], coord.c_str());
+            code->appendf(";\n");
+        }
+        code->appendf("\tvec4 s%d = %s(%s, f.x, s0%d, s1%d, s2%d, s3%d);\n", y, cubicBlendName.c_str(), coeff, y, y, y, y);
+    }
+    code->appendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff);
+}
+
+GrGLEffect::EffectKey GrGLBicubicEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) {
+    const GrBicubicEffect& m =
+        static_cast<const GrBicubicEffect&>(*s.getEffect());
+    EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(),
+                                                   s.getCoordChangeMatrix(),
+                                                   m.texture(0));
+    return matrixKey;
+}
+
+void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
+                                const GrEffectStage& stage) {
+    const GrBicubicEffect& effect =
+        static_cast<const GrBicubicEffect&>(*stage.getEffect());
+    GrTexture& texture = *effect.texture(0);
+    float imageIncrement[2];
+    imageIncrement[0] = 1.0f / texture.width();
+    imageIncrement[1] = 1.0f / texture.height();
+    uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
+    uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
+    fEffectMatrix.setData(uman,
+                          effect.getMatrix(),
+                          stage.getCoordChangeMatrix(),
+                          effect.texture(0));
+}
+
+GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
+                                 const SkScalar coefficients[16])
+  : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) {
+    for (int y = 0; y < 4; y++) {
+        for (int x = 0; x < 4; x++) {
+            // Convert from row-major scalars to column-major floats.
+            fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
+        }
+    }
+}
+
+GrBicubicEffect::~GrBicubicEffect() {
+}
+
+const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
+    return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
+}
+
+bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
+    const GrBicubicEffect& s =
+        static_cast<const GrBicubicEffect&>(sBase);
+    return this->texture(0) == s.texture(0) &&
+           !memcmp(fCoefficients, s.coefficients(), 16);
+}
+
+void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
+    // FIXME:  Perhaps we can do better.
+    *validFlags = 0;
+    return;
+}
+
+GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
+
+GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random,
+                                         GrContext* context,
+                                         GrTexture* textures[]) {
+    int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
+                                      GrEffectUnitTest::kAlphaTextureIdx;
+    SkScalar coefficients[16];
+    for (int i = 0; i < 16; i++) {
+        coefficients[i] = random->nextSScalar1();
+    }
+    return GrBicubicEffect::Create(textures[texIdx], coefficients);
+}
+
+#if SK_SUPPORT_GPU
+GrTexture* SkBicubicImageFilter::filterImageGPU(Proxy* proxy, GrTexture* src, const SkRect& rect) {
+    SkAutoTUnref<GrTexture> srcTexture(this->getInputResultAsTexture(proxy, src, rect));
+    GrContext* context = srcTexture->getContext();
+
+    SkRect dstRect = SkRect::MakeWH(rect.width() * fScale.fWidth,
+                                    rect.height() * fScale.fHeight);
+
+    GrTextureDesc desc;
+    desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
+    desc.fWidth = SkScalarCeilToInt(dstRect.width());
+    desc.fHeight = SkScalarCeilToInt(dstRect.height());
+    desc.fConfig = kRGBA_8888_GrPixelConfig;
+
+    GrAutoScratchTexture ast(context, desc);
+    if (!ast.texture()) {
+        return NULL;
+    }
+    GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget());
+    GrPaint paint;
+    paint.colorStage(0)->setEffect(GrBicubicEffect::Create(srcTexture, fCoefficients));
+    context->drawRectToRect(paint, dstRect, rect);
+    return ast.detach();
+}
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
index 0cf3b35..8390164 100644 (file)
@@ -1428,6 +1428,8 @@ void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
         if (filteredTexture) {
             grPaint.colorStage(kBitmapTextureIdx)->setEffect(
                 GrSimpleTextureEffect::Create(filteredTexture, SkMatrix::I()))->unref();
+            w = filteredTexture->width();
+            h = filteredTexture->height();
             texture = filteredTexture;
             filteredTexture->unref();
         }
@@ -1491,6 +1493,10 @@ void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
         return;
     }
 
+    const SkBitmap& bm = dev->accessBitmap(false);
+    int w = bm.width();
+    int h = bm.height();
+
     GrTexture* devTex = grPaint.getColorStage(kBitmapTextureIdx).getEffect()->texture(0);
     SkASSERT(NULL != devTex);
 
@@ -1503,14 +1509,12 @@ void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
             grPaint.colorStage(kBitmapTextureIdx)->setEffect(
                 GrSimpleTextureEffect::Create(filteredTexture, SkMatrix::I()))->unref();
             devTex = filteredTexture;
+            w = devTex->width();
+            h = devTex->height();
             filteredTexture->unref();
         }
     }
 
-    const SkBitmap& bm = dev->accessBitmap(false);
-    int w = bm.width();
-    int h = bm.height();
-
     GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
                                       SkIntToScalar(y),
                                       SkIntToScalar(w),
index ae0c564..824895a 100644 (file)
@@ -17,6 +17,7 @@
 #include "Sk1DPathEffect.h"
 #include "Sk2DPathEffect.h"
 #include "SkAvoidXfermode.h"
+#include "SkBicubicImageFilter.h"
 #include "SkBitmapSource.h"
 #include "SkBlendImageFilter.h"
 #include "SkBlurDrawLooper.h"
@@ -53,6 +54,7 @@
 void SkFlattenable::InitializeFlattenables() {
 
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkAvoidXfermode)
+    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBicubicImageFilter)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBitmapProcShader)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBitmapSource)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlendImageFilter)