aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm
diff options
context:
space:
mode:
authorGravatar sugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-04-05 13:47:09 +0000
committerGravatar sugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-04-05 13:47:09 +0000
commite3b4c5097a6fd9b6c09d2ffbc3db170a287fdd99 (patch)
tree969c6dad88f065f7d5c61515cf4a0e499dbf2c65 /gm
parentbde506cf99a5ab2acec69d11066636b7430ac29c (diff)
New SVG turbulence in Skia
This cl contains the code for CPU generation of noise. TEST:Added 'perlinnoise' gm Review URL: https://codereview.chromium.org/13094003 git-svn-id: http://skia.googlecode.com/svn/trunk@8541 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'gm')
-rw-r--r--gm/perlinnoise.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/gm/perlinnoise.cpp b/gm/perlinnoise.cpp
new file mode 100644
index 0000000000..f572371233
--- /dev/null
+++ b/gm/perlinnoise.cpp
@@ -0,0 +1,114 @@
+/*
+ * 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 "SkPerlinNoiseShader.h"
+
+namespace skiagm {
+
+class PerlinNoiseGM : public GM {
+public:
+ PerlinNoiseGM() {
+ this->setBGColor(0xFF000000);
+ fSize = SkISize::Make(80, 80);
+ }
+
+protected:
+ virtual SkString onShortName() {
+ return SkString("perlinnoise");
+ }
+
+ virtual SkISize onISize() {
+ return make_isize(500, 400);
+ }
+
+ void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
+ 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->drawRect(r, paint);
+ canvas->restore();
+ }
+
+ void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
+ float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
+ bool stitchTiles)
+ {
+ SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
+ SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
+ seed, stitchTiles ? &fSize : NULL) :
+ SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, numOctaves,
+ seed, stitchTiles ? &fSize : NULL);
+ SkPaint paint;
+ paint.setShader(shader)->unref();
+ drawClippedRect(canvas, x, y, paint);
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ canvas->clear(0x00000000);
+
+ test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 2, 0, false);
+ test(canvas, 100, 0, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.4f, 0.2f, 3, 0, true);
+ test(canvas, 200, 0, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.3f, 0.6f, 4, 0, false);
+ test(canvas, 300, 0, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.2f, 0.4f, 5, 0, true);
+ test(canvas, 400, 0, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.5f, 0.8f, 6, 0, false);
+
+ test(canvas, 0, 100, SkPerlinNoiseShader::kTurbulence_Type,
+ 0.1f, 0.1f, 2, 0, true);
+ test(canvas, 100, 100, SkPerlinNoiseShader::kTurbulence_Type,
+ 0.4f, 0.2f, 3, 0, false);
+ test(canvas, 200, 100, SkPerlinNoiseShader::kTurbulence_Type,
+ 0.3f, 0.6f, 4, 0, true);
+ test(canvas, 300, 100, SkPerlinNoiseShader::kTurbulence_Type,
+ 0.2f, 0.4f, 5, 0, false);
+ test(canvas, 400, 100, SkPerlinNoiseShader::kTurbulence_Type,
+ 0.5f, 0.8f, 6, 0, true);
+
+ test(canvas, 0, 200, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 3, 1, false);
+ test(canvas, 100, 200, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 3, 2, false);
+ test(canvas, 200, 200, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 3, 3, false);
+ test(canvas, 300, 200, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 3, 4, false);
+ test(canvas, 400, 200, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 3, 5, false);
+
+ canvas->scale(SkFloatToScalar(0.75f), SkFloatToScalar(1.0f));
+
+ test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.1f, 0.1f, 2, 0, false);
+ test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.4f, 0.2f, 3, 0, true);
+ test(canvas, 200, 300, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.3f, 0.6f, 4, 0, false);
+ test(canvas, 300, 300, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.2f, 0.4f, 5, 0, true);
+ test(canvas, 400, 300, SkPerlinNoiseShader::kFractalNoise_Type,
+ 0.5f, 0.8f, 6, 0, false);
+ }
+
+private:
+ typedef GM INHERITED;
+ SkISize fSize;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static GM* MyFactory(void*) { return new PerlinNoiseGM; }
+static GMRegistry reg(MyFactory);
+
+}