joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 "SkBlurImageFilter.h" |
| 10 | #include "SkRandom.h" |
| 11 | |
| 12 | // TODO deprecate imageblur |
| 13 | |
| 14 | #define WIDTH 500 |
| 15 | #define HEIGHT 500 |
| 16 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 17 | constexpr float kBlurSigmas[] = { |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 18 | 0.0, 0.3f, 0.5f, 2.0f, 32.0f, 80.0f }; |
| 19 | |
| 20 | const char* kTestStrings[] = { |
| 21 | "The quick`~", |
| 22 | "brown fox[]", |
| 23 | "jumped over", |
| 24 | "the lazy@#$", |
| 25 | "dog.{}!%^&", |
| 26 | "*()+=-\\'\"/", |
| 27 | }; |
| 28 | |
| 29 | namespace skiagm { |
| 30 | |
| 31 | class BlurImageFilter : public GM { |
| 32 | public: |
| 33 | BlurImageFilter() { |
| 34 | this->setBGColor(0xFFFFFFFF); |
| 35 | fName.printf("imageblur2"); |
| 36 | } |
| 37 | |
| 38 | protected: |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 39 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | SkString onShortName() override { |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 41 | return fName; |
| 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkISize onISize() override { |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 45 | return SkISize::Make(WIDTH, HEIGHT); |
| 46 | } |
| 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | void onDraw(SkCanvas* canvas) override { |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 49 | const int sigmaCount = SK_ARRAY_COUNT(kBlurSigmas); |
| 50 | const int testStringCount = SK_ARRAY_COUNT(kTestStrings); |
| 51 | SkScalar dx = WIDTH / sigmaCount; |
| 52 | SkScalar dy = HEIGHT / sigmaCount; |
| 53 | const SkScalar textSize = 12; |
| 54 | |
| 55 | for (int x = 0; x < sigmaCount; x++) { |
| 56 | SkScalar sigmaX = kBlurSigmas[x]; |
| 57 | for (int y = 0; y < sigmaCount; y++) { |
| 58 | SkScalar sigmaY = kBlurSigmas[y]; |
| 59 | |
| 60 | SkPaint paint; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 61 | paint.setImageFilter(SkBlurImageFilter::Make(sigmaX, sigmaY, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | canvas->saveLayer(nullptr, &paint); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 63 | |
| 64 | SkRandom rand; |
| 65 | SkPaint textPaint; |
| 66 | textPaint.setAntiAlias(false); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 67 | textPaint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000)); |
| 68 | sk_tool_utils::set_portable_typeface(&textPaint); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 69 | textPaint.setTextSize(textSize); |
| 70 | |
| 71 | for (int i = 0; i < testStringCount; i++) { |
| 72 | canvas->drawText(kTestStrings[i], |
| 73 | strlen(kTestStrings[i]), |
| 74 | SkIntToScalar(x * dx), |
| 75 | SkIntToScalar(y * dy + textSize * i + textSize), |
| 76 | textPaint); |
| 77 | } |
| 78 | canvas->restore(); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | SkString fName; |
| 85 | |
| 86 | typedef GM INHERITED; |
| 87 | }; |
| 88 | |
| 89 | ////////////////////////////////////////////////////////////////////////////// |
| 90 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 91 | DEF_GM(return new BlurImageFilter;) |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 92 | |
| 93 | } |