blob: 2b95c02ab10078c137290b15be2deb1b8693808a [file] [log] [blame]
joshualitt5acfea72014-08-11 13:55:34 -07001/*
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
17static const float kBlurSigmas[] = {
18 0.0, 0.3f, 0.5f, 2.0f, 32.0f, 80.0f };
19
20const char* kTestStrings[] = {
21 "The quick`~",
22 "brown fox[]",
23 "jumped over",
24 "the lazy@#$",
25 "dog.{}!%^&",
26 "*()+=-\\'\"/",
27};
28
29namespace skiagm {
30
31class BlurImageFilter : public GM {
32public:
33 BlurImageFilter() {
34 this->setBGColor(0xFFFFFFFF);
35 fName.printf("imageblur2");
36 }
37
38protected:
joshualitt5acfea72014-08-11 13:55:34 -070039
mtklein36352bf2015-03-25 18:17:31 -070040 SkString onShortName() override {
joshualitt5acfea72014-08-11 13:55:34 -070041 return fName;
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 SkISize onISize() override {
joshualitt5acfea72014-08-11 13:55:34 -070045 return SkISize::Make(WIDTH, HEIGHT);
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 void onDraw(SkCanvas* canvas) override {
joshualitt5acfea72014-08-11 13:55:34 -070049 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;
xidachen467ddc02015-12-10 12:08:44 -080061 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(sigmaX, sigmaY));
62 paint.setImageFilter(blur);
halcanary96fcdcc2015-08-27 07:41:13 -070063 canvas->saveLayer(nullptr, &paint);
joshualitt5acfea72014-08-11 13:55:34 -070064
65 SkRandom rand;
66 SkPaint textPaint;
67 textPaint.setAntiAlias(false);
caryclarkf597c422015-07-28 10:37:53 -070068 textPaint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
69 sk_tool_utils::set_portable_typeface(&textPaint);
joshualitt5acfea72014-08-11 13:55:34 -070070 textPaint.setTextSize(textSize);
71
72 for (int i = 0; i < testStringCount; i++) {
73 canvas->drawText(kTestStrings[i],
74 strlen(kTestStrings[i]),
75 SkIntToScalar(x * dx),
76 SkIntToScalar(y * dy + textSize * i + textSize),
77 textPaint);
78 }
79 canvas->restore();
80 }
81 }
82 }
83
84private:
85 SkString fName;
86
87 typedef GM INHERITED;
88};
89
90//////////////////////////////////////////////////////////////////////////////
91
92static GM* MyFactory(void*) { return new BlurImageFilter; }
93static GMRegistry reg(MyFactory);
94
95}