Fix tiled perlin noise.

It turns out that the perlin implementation we inherited from WebKit
does not actually generate tileable noise (see Chromium bug
http://crbug.com/383495).

The main problem is that when generating coordinates for gradient
interpolation, it was attempting to wrap both x and (x + 1)
simultaneously at the tile boundary (that is, either both or neither
are wrapped). This obviously won't work, since along the tile seams,
(x + 1) should be wrapped, but x should not. The same is true in y.

This patch fixes both the CPU and GPU paths, renames some variables to
more closely match the spec, and modifies the perlin noise GM to
actually test tiling. (Note that the clipping the GM was doing was
removed, since it's superfluous: it used to be necessary for image
filters, but isn't anymore, and this isn't an image filter GM anyway.)

R=sugoi@google.com, sugoi
TBR=senorblanco

Author: senorblanco@chromium.org

Review URL: https://codereview.chromium.org/332523006
diff --git a/gm/perlinnoise.cpp b/gm/perlinnoise.cpp
index ee58f6f..b69b7d0 100644
--- a/gm/perlinnoise.cpp
+++ b/gm/perlinnoise.cpp
@@ -24,13 +24,11 @@
         return SkISize::Make(200, 500);
     }
 
-    void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
+    void drawRect(SkCanvas* canvas, int x, int y, const SkPaint& paint, const SkISize& size) {
         canvas->save();
-        canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
-                         SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height())));
-        SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
-                                    SkIntToScalar(fSize.width()),
-                                    SkIntToScalar(fSize.height()));
+        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
+        SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
+                                  SkIntToScalar(size.height()));
         canvas->drawRect(r, paint);
         canvas->restore();
     }
@@ -38,14 +36,25 @@
     void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
               float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
               bool stitchTiles) {
+        SkISize tileSize = SkISize::Make(fSize.width() / 2, fSize.height() / 2);
         SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
             SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
-                                                   seed, stitchTiles ? &fSize : NULL) :
+                                                   seed, stitchTiles ? &tileSize : NULL) :
             SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
-                                                seed, stitchTiles ? &fSize : NULL);
+                                                seed, stitchTiles ? &tileSize : NULL);
         SkPaint paint;
         paint.setShader(shader)->unref();
-        drawClippedRect(canvas, x, y, paint);
+        if (stitchTiles) {
+            drawRect(canvas, x, y, paint, tileSize);
+            x += tileSize.width();
+            drawRect(canvas, x, y, paint, tileSize);
+            y += tileSize.width();
+            drawRect(canvas, x, y, paint, tileSize);
+            x -= tileSize.width();
+            drawRect(canvas, x, y, paint, tileSize);
+        } else {
+            drawRect(canvas, x, y, paint, fSize);
+        }
     }
 
     virtual void onDraw(SkCanvas* canvas) {
@@ -58,10 +67,10 @@
         test(canvas,   0, 100, SkPerlinNoiseShader::kFractalNoise_Type,
              0.1f, 0.1f, 2, 0, false);
         test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type,
-             0.2f, 0.4f, 5, 0, true);
+             0.05f, 0.1f, 1, 0, true);
 
         test(canvas,   0, 200, SkPerlinNoiseShader::kTurbulence_Type,
-             0.1f, 0.1f, 2, 0, true);
+             0.1f, 0.1f, 1, 0, true);
         test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type,
              0.2f, 0.4f, 5, 0, false);
 
@@ -75,7 +84,7 @@
         test(canvas,   0, 400, SkPerlinNoiseShader::kFractalNoise_Type,
              0.1f, 0.1f, 2, 0, false);
         test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type,
-             0.2f, 0.4f, 5, 0, true);
+             0.1f, 0.05f, 1, 0, true);
     }
 
 private: