blob: a2ec916a39052783a26d579cdfd57a75ccec229f [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkPerlinNoiseShader.h"
10
11namespace skiagm {
12
13class PerlinNoiseGM : public GM {
14public:
15 PerlinNoiseGM() {
16 this->setBGColor(0xFF000000);
17 fSize = SkISize::Make(80, 80);
18 }
19
20protected:
21 virtual SkString onShortName() {
22 return SkString("perlinnoise");
23 }
24
25 virtual SkISize onISize() {
commit-bot@chromium.org344cf452013-06-17 14:19:01 +000026 return make_isize(200, 400);
sugoi@google.come3b4c502013-04-05 13:47:09 +000027 }
28
29 void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
30 canvas->save();
31 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
32 SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height())));
33 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
34 SkIntToScalar(fSize.width()),
35 SkIntToScalar(fSize.height()));
36 canvas->drawRect(r, paint);
37 canvas->restore();
38 }
39
40 void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
41 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
sugoi@google.com4775cba2013-04-17 13:46:56 +000042 bool stitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +000043 SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
44 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
45 seed, stitchTiles ? &fSize : NULL) :
46 SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, numOctaves,
47 seed, stitchTiles ? &fSize : NULL);
48 SkPaint paint;
49 paint.setShader(shader)->unref();
50 drawClippedRect(canvas, x, y, paint);
51 }
52
53 virtual void onDraw(SkCanvas* canvas) {
54 canvas->clear(0x00000000);
skia.committer@gmail.comcff02432013-04-06 07:01:10 +000055
sugoi@google.come3b4c502013-04-05 13:47:09 +000056 test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
57 0.1f, 0.1f, 2, 0, false);
58 test(canvas, 100, 0, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000059 0.2f, 0.4f, 5, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000060
61 test(canvas, 0, 100, SkPerlinNoiseShader::kTurbulence_Type,
62 0.1f, 0.1f, 2, 0, true);
63 test(canvas, 100, 100, SkPerlinNoiseShader::kTurbulence_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000064 0.2f, 0.4f, 5, 0, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000065
66 test(canvas, 0, 200, SkPerlinNoiseShader::kFractalNoise_Type,
67 0.1f, 0.1f, 3, 1, false);
68 test(canvas, 100, 200, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000069 0.1f, 0.1f, 3, 4, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000070
71 canvas->scale(SkFloatToScalar(0.75f), SkFloatToScalar(1.0f));
72
73 test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type,
74 0.1f, 0.1f, 2, 0, false);
75 test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000076 0.2f, 0.4f, 5, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000077 }
78
79private:
80 typedef GM INHERITED;
81 SkISize fSize;
82};
83
84//////////////////////////////////////////////////////////////////////////////
85
86static GM* MyFactory(void*) { return new PerlinNoiseGM; }
87static GMRegistry reg(MyFactory);
88
89}