SkBitmapProcState rounding bias
authorfmalita <fmalita@chromium.org>
Tue, 15 Dec 2015 14:48:48 +0000 (06:48 -0800)
committerCommit bot <commit-bot@chromium.org>
Tue, 15 Dec 2015 14:48:48 +0000 (06:48 -0800)
Epsilon bias to keep bitmap sample rounding consistent with geometry
rounding.

Also update the GM to draw an outer border + drop uninteresting
scales in favor of negative scale variants.

BUG=skia:4680,skia:4649
R=reed@google.com,caryclark@google.com,mtklein@google.com

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

gm/imagescalealigned.cpp
src/core/SkBitmapProcState.cpp
src/core/SkBitmapProcState.h
src/core/SkBitmapProcState_matrix.h

index fbcdafb..c9e697c 100644 (file)
@@ -34,39 +34,29 @@ protected:
     }
 
     SkISize onISize() override {
-        return SkISize::Make(550, 300);
+        return SkISize::Make(580, 780);
     }
 
     void onDraw(SkCanvas* canvas) override {
         struct {
             SkPoint offset;
-            SkScalar scale;
+            SkVector scale;
         } cfgs[] = {
-            { {  10,    10    }, 1 },
-            { { 140.5f, 10    }, 1 },
-            { {  10,    80.5f }, 1 },
-            { { 140.5f, 80.5f }, 1 },
-
-            { { 280.49f, 10.49f }, 1 },
-            { { 410.51f, 10.49f }, 1 },
-            { { 280.49f, 80.51f }, 1 },
-            { { 410.51f, 80.51f }, 1 },
-
-            { {  10,    170    }, 1.1f },
-            { { 140.5f, 170    }, 1.1f },
-            { {  10,    240.5f }, 1.1f },
-            { { 140.5f, 240.5f }, 1.1f },
-
-            { { 280.49f, 170.49f }, 1.1f },
-            { { 410.51f, 170.49f }, 1.1f },
-            { { 280.49f, 240.51f }, 1.1f },
-            { { 410.51f, 240.51f }, 1.1f },
+            {{  10,    10    }, { 1, 1 }},
+            {{ 300.5f, 10    }, { 1, 1 }},
+            {{  10,    200.5f }, { 1, 1 }},
+            {{ 300.5f, 200.5f }, { 1, 1 }},
+
+            {{  10.5f, 400.5f }, {  1,  1 }},
+            {{ 550.5f, 400.5f }, { -1,  1 }},
+            {{  10.5f, 750.5f }, {  1, -1 }},
+            {{ 550.5f, 750.5f }, { -1, -1 }},
         };
 
         for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) {
             SkAutoCanvasRestore acr(canvas, true);
             canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y());
-            canvas->scale(cfgs[i].scale, cfgs[i].scale);
+            canvas->scale(cfgs[i].scale.x(), cfgs[i].scale.y());
             drawSets(canvas);
         }
     }
@@ -88,6 +78,11 @@ private:
 
         SkPaint paint;
         paint.setAntiAlias(true);
+        const SkRect border = SkRect::MakeIWH(kSegLen, kSegLen).makeInset(.5f, .5f);
+        paint.setColor(SK_ColorBLUE);
+        paint.setStyle(SkPaint::kStroke_Style);
+        surface->getCanvas()->drawRect(border, paint);
+
         paint.setColor(SK_ColorBLACK);
         surface->getCanvas()->drawLine(start.x(), start.y(), end.x(), end.y(), paint);
         surface->getCanvas()->drawPoint(start.x(), start.y(), color);
@@ -116,12 +111,12 @@ private:
                 for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) {
                     paint.setFilterQuality(filters[k]);
                     lastPt = drawSet(canvas, set, paint);
-                    canvas->translate((kSegLen / 2) * set.fVector.y(),
-                                      (kSegLen / 2) * set.fVector.x());
+                    canvas->translate((kSegLen + 4) * set.fVector.y(),
+                                      (kSegLen + 4) * set.fVector.x());
                 }
             }
             canvas->translate(lastPt.x() + kSegLen,
-                - SkIntToScalar(kSegLen) / 2 * SK_ARRAY_COUNT(filters) * SK_ARRAY_COUNT(AAs));
+                - SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(filters) * SK_ARRAY_COUNT(AAs));
         }
     }
 
@@ -143,8 +138,8 @@ private:
         return pt;
     }
 
-    static const unsigned  kSegLen = 9;
-    static const unsigned  kStretchFactor = 2;
+    static const unsigned  kSegLen = 15;
+    static const unsigned  kStretchFactor = 4;
     SkSTArray<2, ImageSet> fSets;
 
     typedef GM INHERITED;
index 5ec6721..487bd80 100644 (file)
@@ -823,10 +823,12 @@ void  Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const SkBitmapProcState& s, in
         SkPoint pt;
         s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf,
                    &pt);
-        fx = SkScalarToFractionalInt(pt.fY);
+        fx = SkScalarToFractionalInt(pt.fY)
+            + bitmap_sampler_inv_bias(s.fInvMatrix.getScaleY());
         const unsigned maxY = s.fPixmap.height() - 1;
         dstY = SkClampMax(SkFractionalIntToInt(fx), maxY);
-        fx = SkScalarToFractionalInt(pt.fX);
+        fx = SkScalarToFractionalInt(pt.fX)
+            + bitmap_sampler_inv_bias(s.fInvMatrix.getScaleX());
     }
 
     const SkPMColor* SK_RESTRICT src = s.fPixmap.addr32(0, dstY);
index 2ce76bc..e6e7a3f 100644 (file)
@@ -12,6 +12,7 @@
 #include "SkBitmapController.h"
 #include "SkBitmapFilter.h"
 #include "SkBitmapProvider.h"
+#include "SkFloatBits.h"
 #include "SkMatrix.h"
 #include "SkMipMap.h"
 #include "SkPaint.h"
@@ -24,6 +25,17 @@ typedef SkFixed3232    SkFractionalInt;
 #define SkFixedToFractionalInt(x)   SkFixedToFixed3232(x)
 #define SkFractionalIntToInt(x)     SkFixed3232ToInt(x)
 
+// Applying a fixed point (SkFixed, SkFractionalInt) epsilon bias ensures that the inverse-mapped
+// bitmap coordinates are rounded consistently WRT geometry.  Note that we only have to do this
+// when the scale is positive - for negative scales we're already rounding in the right direction.
+static inline int bitmap_sampler_inv_bias(SkScalar scale) {
+#ifndef SK_SUPPORT_LEGACY_BITMAP_SAMPLER_BIAS
+    return -(scale > 0);
+#else
+    return 0;
+#endif
+}
+
 class SkPaint;
 
 struct SkBitmapProcState {
index bdab846..42a5ab6 100644 (file)
@@ -64,12 +64,14 @@ void SCALE_FILTER_NAME(const SkBitmapProcState& s,
         SkPoint pt;
         s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf,
                                   SkIntToScalar(y) + SK_ScalarHalf, &pt);
-        const SkFixed fy = SkScalarToFixed(pt.fY) - (s.fFilterOneY >> 1);
+        const SkFixed fy = SkScalarToFixed(pt.fY) - (s.fFilterOneY >> 1)
+            + bitmap_sampler_inv_bias(s.fInvMatrix.getScaleY());
         const unsigned maxY = s.fPixmap.height() - 1;
         // compute our two Y values up front
         *xy++ = PACK_FILTER_Y_NAME(fy, maxY, s.fFilterOneY PREAMBLE_ARG_Y);
         // now initialize fx
-        fx = SkScalarToFractionalInt(pt.fX) - (SkFixedToFractionalInt(one) >> 1);
+        fx = SkScalarToFractionalInt(pt.fX) - (SkFixedToFractionalInt(one) >> 1)
+            + bitmap_sampler_inv_bias(s.fInvMatrix.getScaleX());
     }
 
 #ifdef CHECK_FOR_DECAL
@@ -102,8 +104,10 @@ void AFFINE_FILTER_NAME(const SkBitmapProcState& s,
 
     SkFixed oneX = s.fFilterOneX;
     SkFixed oneY = s.fFilterOneY;
-    SkFixed fx = SkScalarToFixed(srcPt.fX) - (oneX >> 1);
-    SkFixed fy = SkScalarToFixed(srcPt.fY) - (oneY >> 1);
+    SkFixed fx = SkScalarToFixed(srcPt.fX) - (oneX >> 1)
+        + bitmap_sampler_inv_bias(s.fInvMatrix.getScaleX());
+    SkFixed fy = SkScalarToFixed(srcPt.fY) - (oneY >> 1)
+        + bitmap_sampler_inv_bias(s.fInvMatrix.getScaleY());
     SkFixed dx = s.fInvSx;
     SkFixed dy = s.fInvKy;
     unsigned maxX = s.fPixmap.width() - 1;