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 "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkTArray.h" | 10 #include "SkTArray.h" |
11 | 11 |
12 namespace skiagm { | 12 namespace skiagm { |
13 | 13 |
14 class HairlinesGM : public GM { | 14 class HairlinesGM : public GM { |
15 protected: | 15 protected: |
16 virtual uint32_t onGetFlags() const SK_OVERRIDE { | 16 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
17 return kSkipTiled_Flag; | 17 return kSkipTiled_Flag; |
18 } | 18 } |
19 | 19 |
20 | 20 |
21 virtual SkString onShortName() SK_OVERRIDE { | 21 virtual SkString onShortName() SK_OVERRIDE { |
22 return SkString("hairlines"); | 22 return SkString("hairlines"); |
23 } | 23 } |
24 | 24 |
25 virtual SkISize onISize() { return SkISize::Make(800, 600); } | 25 virtual SkISize onISize() { return SkISize::Make(1250, 1250); } |
26 | 26 |
27 virtual void onOnceBeforeDraw() SK_OVERRIDE { | 27 virtual void onOnceBeforeDraw() SK_OVERRIDE { |
28 { | 28 { |
29 SkPath* lineAnglesPath = &fPaths.push_back(); | 29 SkPath* lineAnglesPath = &fPaths.push_back(); |
30 enum { | 30 enum { |
31 kNumAngles = 15, | 31 kNumAngles = 15, |
32 kRadius = 40, | 32 kRadius = 40, |
33 }; | 33 }; |
34 for (int i = 0; i < kNumAngles; ++i) { | 34 for (int i = 0; i < kNumAngles; ++i) { |
35 SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles; | 35 SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 159 |
160 SkPoint p1 = SkPoint::Make(kRad * cosV, kRad * sinV); | 160 SkPoint p1 = SkPoint::Make(kRad * cosV, kRad * sinV); |
161 | 161 |
162 bug->moveTo(p0); | 162 bug->moveTo(p0); |
163 bug->lineTo(p1); | 163 bug->lineTo(p1); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 167 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
168 static const SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; | 168 static const SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; |
| 169 static const SkScalar kWidths[] = { 0, 0.5f, 1.5f }; |
169 | 170 |
170 enum { | 171 enum { |
171 kMargin = 5, | 172 kMargin = 5, |
172 }; | 173 }; |
173 int wrapX = canvas->getDeviceSize().fWidth - kMargin; | 174 int wrapX = canvas->getDeviceSize().fWidth - kMargin; |
174 | 175 |
175 SkScalar maxH = 0; | 176 SkScalar maxH = 0; |
176 canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin)); | 177 canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin)); |
177 canvas->save(); | 178 canvas->save(); |
178 | 179 |
179 SkScalar x = SkIntToScalar(kMargin); | 180 SkScalar x = SkIntToScalar(kMargin); |
180 for (int p = 0; p < fPaths.count(); ++p) { | 181 for (int p = 0; p < fPaths.count(); ++p) { |
181 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) { | 182 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) { |
182 for (int aa = 0; aa < 2; ++aa) { | 183 for (int aa = 0; aa < 2; ++aa) { |
183 const SkRect& bounds = fPaths[p].getBounds(); | 184 for (size_t w = 0; w < SK_ARRAY_COUNT(kWidths); w++) { |
| 185 const SkRect& bounds = fPaths[p].getBounds(); |
184 | 186 |
185 if (x + bounds.width() > wrapX) { | 187 if (x + bounds.width() > wrapX) { |
| 188 canvas->restore(); |
| 189 canvas->translate(0, maxH + SkIntToScalar(kMargin)); |
| 190 canvas->save(); |
| 191 maxH = 0; |
| 192 x = SkIntToScalar(kMargin); |
| 193 } |
| 194 |
| 195 SkPaint paint; |
| 196 paint.setARGB(kAlphaValue[a], 0, 0, 0); |
| 197 paint.setAntiAlias(SkToBool(aa)); |
| 198 paint.setStyle(SkPaint::kStroke_Style); |
| 199 paint.setStrokeWidth(kWidths[w]); |
| 200 |
| 201 canvas->save(); |
| 202 canvas->translate(-bounds.fLeft, -bounds.fTop); |
| 203 canvas->drawPath(fPaths[p], paint); |
186 canvas->restore(); | 204 canvas->restore(); |
187 canvas->translate(0, maxH + SkIntToScalar(kMargin)); | 205 |
188 canvas->save(); | 206 maxH = SkMaxScalar(maxH, bounds.height()); |
189 maxH = 0; | 207 |
190 x = SkIntToScalar(kMargin); | 208 SkScalar dx = bounds.width() + SkIntToScalar(kMargin); |
| 209 x += dx; |
| 210 canvas->translate(dx, 0); |
191 } | 211 } |
192 | |
193 SkPaint paint; | |
194 paint.setARGB(kAlphaValue[a], 0, 0, 0); | |
195 paint.setAntiAlias(SkToBool(aa)); | |
196 paint.setStyle(SkPaint::kStroke_Style); | |
197 paint.setStrokeWidth(0); | |
198 | |
199 canvas->save(); | |
200 canvas->translate(-bounds.fLeft, -bounds.fTop); | |
201 canvas->drawPath(fPaths[p], paint); | |
202 canvas->restore(); | |
203 | |
204 maxH = SkMaxScalar(maxH, bounds.height()); | |
205 | |
206 SkScalar dx = bounds.width() + SkIntToScalar(kMargin); | |
207 x += dx; | |
208 canvas->translate(dx, 0); | |
209 } | 212 } |
210 } | 213 } |
211 } | 214 } |
212 canvas->restore(); | 215 canvas->restore(); |
213 } | 216 } |
214 | 217 |
215 private: | 218 private: |
216 SkTArray<SkPath> fPaths; | 219 SkTArray<SkPath> fPaths; |
217 typedef GM INHERITED; | 220 typedef GM INHERITED; |
218 }; | 221 }; |
219 | 222 |
220 ////////////////////////////////////////////////////////////////////////////// | 223 ////////////////////////////////////////////////////////////////////////////// |
221 | 224 |
222 static GM* MyFactory(void*) { return new HairlinesGM; } | 225 static GM* MyFactory(void*) { return new HairlinesGM; } |
223 static GMRegistry reg(MyFactory); | 226 static GMRegistry reg(MyFactory); |
224 | 227 |
225 } | 228 } |
OLD | NEW |