Use GrTextureDomain in GrBicubicEffect to perform non-bleeding HQ filter drawBitmap.
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 16 Dec 2013 15:18:11 +0000 (15:18 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 16 Dec 2013 15:18:11 +0000 (15:18 +0000)
R=senorblanco@chromium.org, robertphillips@google.com

Author: bsalomon@google.com

Review URL: https://codereview.chromium.org/99203011

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

src/gpu/SkGpuDevice.cpp
src/gpu/effects/GrBicubicEffect.cpp
src/gpu/effects/GrBicubicEffect.h
src/gpu/effects/GrTextureDomain.h

index c8d2896..a0692d0 100644 (file)
@@ -1208,8 +1208,7 @@ void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
             break;
         case SkPaint::kHigh_FilterLevel: {
             // Minification can look bad with the bicubic effect.
-            if (fContext->getMatrix().getMinStretch() >= SK_Scalar1 &&
-                (flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
+            if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
                 // We will install an effect that does the filtering in the shader.
                 textureFilterMode = GrTextureParams::kNone_FilterMode;
                 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
@@ -1292,8 +1291,6 @@ void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
                     // but stay within the bitmap bounds
                     iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
                 } else {
-                    SkASSERT(!bicubic); // Bicubic is not supported with non-bleed yet.
-
                     // In texture-domain/clamp mode we only want to expand the
                     // tile on edges interior to "srcRect" (i.e., we want to
                     // not bleed across the original clamped edges)
@@ -1391,12 +1388,11 @@ void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
 
     bool needsTextureDomain = false;
     if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
-        params.filterMode() != GrTextureParams::kNone_FilterMode) {
-        SkASSERT(!bicubic);
-        // Need texture domain if drawing a sub rect.
+        (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
+        // Need texture domain if drawing a sub rect
         needsTextureDomain = srcRect.width() < bitmap.width() ||
                              srcRect.height() < bitmap.height();
-        if (needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
+        if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
             const SkMatrix& matrix = fContext->getMatrix();
             // sampling is axis-aligned
             SkRect transformedRect;
@@ -1432,11 +1428,15 @@ void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
             top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
         }
         textureDomain.setLTRB(left, top, right, bottom);
-        effect.reset(GrTextureDomainEffect::Create(texture,
-                                                   SkMatrix::I(),
-                                                   textureDomain,
-                                                   GrTextureDomain::kClamp_Mode,
-                                                   params.filterMode()));
+        if (bicubic) {
+            effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
+        } else {
+            effect.reset(GrTextureDomainEffect::Create(texture,
+                                                       SkMatrix::I(),
+                                                       textureDomain,
+                                                       GrTextureDomain::kClamp_Mode,
+                                                       params.filterMode()));
+        }
     } else if (bicubic) {
         SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
         SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
index a6e08f2..89124ff 100644 (file)
@@ -14,6 +14,7 @@ class GrGLBicubicEffect : public GrGLEffect {
 public:
     GrGLBicubicEffect(const GrBackendEffectFactory& factory,
                       const GrDrawEffect&);
+
     virtual void emitCode(GrGLShaderBuilder*,
                           const GrDrawEffect&,
                           EffectKey,
@@ -24,11 +25,17 @@ public:
 
     virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
 
+    static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
+        const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
+        return GrTextureDomain::GLDomain::DomainKey(domain);
+    }
+
 private:
     typedef GrGLUniformManager::UniformHandle        UniformHandle;
 
-    UniformHandle       fCoefficientsUni;
-    UniformHandle       fImageIncrementUni;
+    UniformHandle               fCoefficientsUni;
+    UniformHandle               fImageIncrementUni;
+    GrTextureDomain::GLDomain   fDomain;
 
     typedef GrGLEffect INHERITED;
 };
@@ -38,13 +45,13 @@ GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, cons
 }
 
 void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
-                                 const GrDrawEffect&,
+                                 const GrDrawEffect& drawEffect,
                                  EffectKey key,
                                  const char* outputColor,
                                  const char* inputColor,
                                  const TransformedCoordsArray& coords,
                                  const TextureSamplerArray& samplers) {
-    sk_ignore_unused_variable(inputColor);
+    const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().domain();
 
     SkString coords2D = builder->ensureFSCoords2D(coords, 0);
     fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
@@ -81,42 +88,60 @@ void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
     builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc);
     builder->fsCodeAppend("\tvec2 f = fract(coord);\n");
     builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
+    builder->fsCodeAppend("\tvec4 rowColors[4];\n");
     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);
-            builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y);
-            builder->fsAppendTextureLookup(samplers[0], coord.c_str());
-            builder->fsCodeAppend(";\n");
+            SkString sampleVar;
+            sampleVar.printf("rowColors[%d]", x);
+            fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, samplers[0]);
         }
-        builder->fsCodeAppendf("\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);
+        builder->fsCodeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors[1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff);
     }
-    builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff);
+    SkString bicubicColor;
+    bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), coeff);
+    builder->fsCodeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColor.c_str()) * GrGLSLExpr4(inputColor)).c_str());
 }
 
 void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
                                 const GrDrawEffect& drawEffect) {
     const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
-    GrTexture& texture = *effect.texture(0);
+    const GrTexture& texture = *effect.texture(0);
     float imageIncrement[2];
     imageIncrement[0] = 1.0f / texture.width();
     imageIncrement[1] = 1.0f / texture.height();
     uman.set2fv(fImageIncrementUni, 1, imageIncrement);
     uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
+    fDomain.setData(uman, effect.domain(), texture.origin());
 }
 
-GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
-                                 const SkScalar coefficients[16],
-                                 const SkMatrix &matrix,
-                                 const SkShader::TileMode tileModes[2])
-  : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode)) {
+static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
+                                                                          const SkScalar src[16]) {
     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]);
+            dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
         }
     }
-    this->setWillNotUseInputColor();
+}
+
+GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
+                                 const SkScalar coefficients[16],
+                                 const SkMatrix &matrix,
+                                 const SkShader::TileMode tileModes[2])
+  : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone_FilterMode))
+  , fDomain(GrTextureDomain::IgnoredDomain()) {
+    convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
+}
+
+GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
+                                 const SkScalar coefficients[16],
+                                 const SkMatrix &matrix,
+                                 const SkRect& domain)
+  : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
+                                               GrTextureParams::kNone_FilterMode))
+  , fDomain(domain, GrTextureDomain::kClamp_Mode) {
+    convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coefficients);
 }
 
 GrBicubicEffect::~GrBicubicEffect() {
@@ -133,7 +158,7 @@ bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
 }
 
 void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
-    // FIXME:  Perhaps we can do better.
+    // FIXME: Perhaps we can do better.
     *validFlags = 0;
     return;
 }
index 85bec77..cc8b120 100644 (file)
@@ -9,6 +9,7 @@
 #define GrBicubicTextureEffect_DEFINED
 
 #include "GrSingleTextureEffect.h"
+#include "GrTextureDomain.h"
 #include "GrDrawEffect.h"
 #include "gl/GrGLEffect.h"
 #include "GrTBackendEffectFactory.h"
@@ -31,46 +32,61 @@ public:
     virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
     virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
 
-    /**
-     * Create a simple Mitchell filter effect.
-     */
-    static GrEffectRef* Create(GrTexture* tex) {
-        return Create(tex, gMitchellCoefficients);
-    }
+    const GrTextureDomain& domain() const { return fDomain; }
 
     /**
-     * Create a simple filter effect with custom bicubic coefficients.
+     * Create a simple filter effect with custom bicubic coefficients and optional domain.
      */
-    static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16]) {
-        const SkShader::TileMode tm[] = { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
-        return Create(tex, coefficients, MakeDivByTextureWHMatrix(tex), tm);
+    static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16],
+                               const SkRect* domain = NULL) {
+        if (NULL == domain) {
+            static const SkShader::TileMode kTileModes[] = { SkShader::kClamp_TileMode,
+                                                             SkShader::kClamp_TileMode };
+            return Create(tex, coefficients, MakeDivByTextureWHMatrix(tex), kTileModes);
+        } else {
+            AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients,
+                                                                MakeDivByTextureWHMatrix(tex),
+                                                                *domain)));
+            return CreateEffectRef(effect);
+        }
     }
 
     /**
      * Create a Mitchell filter effect with specified texture matrix and x/y tile modes.
      */
-    static GrEffectRef* Create(GrTexture* tex,
-                               const SkMatrix& matrix,
+    static GrEffectRef* Create(GrTexture* tex, const SkMatrix& matrix,
                                SkShader::TileMode tileModes[2]) {
         return Create(tex, gMitchellCoefficients, matrix, tileModes);
     }
 
     /**
-     * The most general Create method. This allows specification of the bicubic coefficients, the
-     * texture matrix, and the x/y tilemodes.
+     * Create a filter effect with custom bicubic coefficients, the texture matrix, and the x/y
+     * tilemodes.
      */
     static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16],
-                               const SkMatrix& matrix,
-                               const SkShader::TileMode tileModes[2]) {
+                               const SkMatrix& matrix, const SkShader::TileMode tileModes[2]) {
         AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients, matrix, tileModes)));
         return CreateEffectRef(effect);
     }
 
+    /**
+     * Create a Mitchell filter effect with a texture matrix and a domain.
+     */
+    static GrEffectRef* Create(GrTexture* tex, const SkMatrix& matrix, const SkRect& domain) {
+        AutoEffectUnref effect(SkNEW_ARGS(GrBicubicEffect, (tex, gMitchellCoefficients, matrix,
+                                                            domain)));
+        return CreateEffectRef(effect);
+    }
+
 private:
     GrBicubicEffect(GrTexture*, const SkScalar coefficients[16],
                     const SkMatrix &matrix, const SkShader::TileMode tileModes[2]);
+    GrBicubicEffect(GrTexture*, const SkScalar coefficients[16],
+                    const SkMatrix &matrix, const SkRect& domain);
     virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
-    float    fCoefficients[16];
+
+    float           fCoefficients[16];
+    GrTextureDomain fDomain;
 
     GR_DECLARE_EFFECT_TEST;
 
index f64d5c3..b4c0b29 100644 (file)
@@ -31,6 +31,12 @@ public:
     };
     static const int kModeCount = kLastMode + 1;
 
+    static const GrTextureDomain& IgnoredDomain() {
+        static const SkRect gDummyRect = {0, 0, 0, 0};
+        static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode);
+        return gDomain;
+    }
+
     /**
      * @param index     Pass a value >= 0 if using multiple texture domains in the same effect.
      *                  It is used to keep inserted variables from causing name collisions.