1 /*
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
11 namespace skiagm {
12
13 class PerlinNoiseGM : public GM {
14 public:
PerlinNoiseGM()15 PerlinNoiseGM() {
16 this->setBGColor(0xFF000000);
17 fSize = SkISize::Make(80, 80);
18 }
19
20 protected:
onShortName()21 virtual SkString onShortName() {
22 return SkString("perlinnoise");
23 }
24
onISize()25 virtual SkISize onISize() {
26 return make_isize(200, 500);
27 }
28
drawClippedRect(SkCanvas * canvas,int x,int y,const SkPaint & paint)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
test(SkCanvas * canvas,int x,int y,SkPerlinNoiseShader::Type type,float baseFrequencyX,float baseFrequencyY,int numOctaves,float seed,bool stitchTiles)40 void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
41 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
42 bool stitchTiles) {
43 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
onDraw(SkCanvas * canvas)53 virtual void onDraw(SkCanvas* canvas) {
54 canvas->clear(0x00000000);
55 test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
56 0.1f, 0.1f, 0, 0, false);
57 test(canvas, 100, 0, SkPerlinNoiseShader::kTurbulence_Type,
58 0.1f, 0.1f, 0, 0, false);
59
60 test(canvas, 0, 100, SkPerlinNoiseShader::kFractalNoise_Type,
61 0.1f, 0.1f, 2, 0, false);
62 test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type,
63 0.2f, 0.4f, 5, 0, true);
64
65 test(canvas, 0, 200, SkPerlinNoiseShader::kTurbulence_Type,
66 0.1f, 0.1f, 2, 0, true);
67 test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type,
68 0.2f, 0.4f, 5, 0, false);
69
70 test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type,
71 0.1f, 0.1f, 3, 1, false);
72 test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type,
73 0.1f, 0.1f, 3, 4, false);
74
75 canvas->scale(0.75f, 1.0f);
76
77 test(canvas, 0, 400, SkPerlinNoiseShader::kFractalNoise_Type,
78 0.1f, 0.1f, 2, 0, false);
79 test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type,
80 0.2f, 0.4f, 5, 0, true);
81 }
82
83 private:
84 typedef GM INHERITED;
85 SkISize fSize;
86 };
87
88 //////////////////////////////////////////////////////////////////////////////
89
MyFactory(void *)90 static GM* MyFactory(void*) { return new PerlinNoiseGM; }
91 static GMRegistry reg(MyFactory);
92
93 }
94