OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkPerlinNoiseShader.h" | 9 #include "SkPerlinNoiseShader.h" |
10 | 10 |
11 namespace skiagm { | 11 class PerlinNoiseGM : public skiagm::GM { |
12 | |
13 class PerlinNoiseGM : public GM { | |
14 public: | 12 public: |
15 PerlinNoiseGM() { | 13 PerlinNoiseGM() { |
16 this->setBGColor(0xFF000000); | 14 this->setBGColor(0xFF000000); |
17 fSize = SkISize::Make(80, 80); | 15 fSize = SkISize::Make(80, 80); |
18 } | 16 } |
19 | 17 |
20 protected: | 18 protected: |
21 virtual SkString onShortName() { | 19 virtual SkString onShortName() { |
22 return SkString("perlinnoise"); | 20 return SkString("perlinnoise"); |
23 } | 21 } |
24 | 22 |
25 virtual SkISize onISize() { | 23 virtual SkISize onISize() { |
26 return make_isize(200, 500); | 24 return SkISize::Make(200, 500); |
27 } | 25 } |
28 | 26 |
29 void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) { | 27 void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
30 canvas->save(); | 28 canvas->save(); |
31 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), | 29 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
32 SkIntToScalar(fSize.width()), SkIntToScalar(fSize.heigh
t()))); | 30 SkIntToScalar(fSize.width()), SkIntToScalar(fSize.heigh
t()))); |
33 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), | 31 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
34 SkIntToScalar(fSize.width()), | 32 SkIntToScalar(fSize.width()), |
35 SkIntToScalar(fSize.height())); | 33 SkIntToScalar(fSize.height())); |
36 canvas->drawRect(r, paint); | 34 canvas->drawRect(r, paint); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 0.1f, 0.1f, 2, 0, false); | 76 0.1f, 0.1f, 2, 0, false); |
79 test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type, | 77 test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
80 0.2f, 0.4f, 5, 0, true); | 78 0.2f, 0.4f, 5, 0, true); |
81 } | 79 } |
82 | 80 |
83 private: | 81 private: |
84 typedef GM INHERITED; | 82 typedef GM INHERITED; |
85 SkISize fSize; | 83 SkISize fSize; |
86 }; | 84 }; |
87 | 85 |
| 86 class PerlinNoiseGM2 : public skiagm::GM { |
| 87 public: |
| 88 PerlinNoiseGM2() { |
| 89 fSize = SkISize::Make(80, 80); |
| 90 } |
| 91 |
| 92 protected: |
| 93 virtual SkString onShortName() { |
| 94 return SkString("perlinnoise_localmatrix"); |
| 95 } |
| 96 |
| 97 virtual SkISize onISize() { |
| 98 return SkISize::Make(640, 480); |
| 99 } |
| 100 |
| 101 void install(SkPaint* paint, SkPerlinNoiseShader::Type type, |
| 102 float baseFrequencyX, float baseFrequencyY, int numOctaves, float
seed, |
| 103 bool stitchTiles) { |
| 104 SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 105 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, |
| 106 seed, stitchTiles ? &fSize : NUL
L) : |
| 107 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, |
| 108 seed, stitchTiles ? &fSize : NULL)
; |
| 109 paint->setShader(shader)->unref(); |
| 110 } |
| 111 |
| 112 virtual void onDraw(SkCanvas* canvas) { |
| 113 canvas->translate(10, 10); |
| 114 |
| 115 SkPaint paint; |
| 116 install(&paint, SkPerlinNoiseShader::kFractalNoise_Type, 0.1f, 0.1f, 2,
0, false); |
| 117 |
| 118 const SkScalar w = SkIntToScalar(fSize.width()); |
| 119 const SkScalar h = SkIntToScalar(fSize.height()); |
| 120 |
| 121 SkRect r = SkRect::MakeWH(w, h); |
| 122 canvas->drawRect(r, paint); |
| 123 |
| 124 canvas->save(); |
| 125 canvas->translate(w * 5/4, 0); |
| 126 canvas->drawRect(r, paint); |
| 127 canvas->restore(); |
| 128 |
| 129 canvas->save(); |
| 130 canvas->translate(0, h + 10); |
| 131 canvas->scale(2, 2); |
| 132 canvas->drawRect(r, paint); |
| 133 canvas->restore(); |
| 134 |
| 135 canvas->save(); |
| 136 canvas->translate(w + 100, h + 10); |
| 137 canvas->scale(2, 2); |
| 138 canvas->drawRect(r, paint); |
| 139 canvas->restore(); |
| 140 |
| 141 // The next row should draw the same as the previous, even though we are
using a local |
| 142 // matrix instead of the canvas. |
| 143 |
| 144 canvas->translate(0, h * 2 + 10); |
| 145 |
| 146 SkMatrix lm; |
| 147 lm.setScale(2, 2); |
| 148 paint.getShader()->setLocalMatrix(lm); |
| 149 r.fRight += r.width(); |
| 150 r.fBottom += r.height(); |
| 151 |
| 152 canvas->save(); |
| 153 canvas->translate(0, h + 10); |
| 154 canvas->drawRect(r, paint); |
| 155 canvas->restore(); |
| 156 |
| 157 canvas->save(); |
| 158 canvas->translate(w + 100, h + 10); |
| 159 canvas->drawRect(r, paint); |
| 160 canvas->restore(); |
| 161 } |
| 162 |
| 163 private: |
| 164 typedef GM INHERITED; |
| 165 SkISize fSize; |
| 166 }; |
| 167 |
88 ////////////////////////////////////////////////////////////////////////////// | 168 ////////////////////////////////////////////////////////////////////////////// |
89 | 169 |
90 static GM* MyFactory(void*) { return new PerlinNoiseGM; } | 170 DEF_GM( return new PerlinNoiseGM; ) |
91 static GMRegistry reg(MyFactory); | 171 DEF_GM( return new PerlinNoiseGM2; ) |
92 | |
93 } | |
OLD | NEW |