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 
17 static const float kBlurSigmas[] = {
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:
BlurImageFilter()33     BlurImageFilter() {
34         this->setBGColor(0xFFFFFFFF);
35         fName.printf("imageblur2");
36     }
37 
38 protected:
39 
onShortName()40     SkString onShortName() override {
41         return fName;
42     }
43 
onISize()44     SkISize onISize() override {
45         return SkISize::Make(WIDTH, HEIGHT);
46     }
47 
onDraw(SkCanvas * canvas)48     void onDraw(SkCanvas* canvas) override {
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;
61                 paint.setImageFilter(SkBlurImageFilter::Create(sigmaX, sigmaY))->unref();
62                 canvas->saveLayer(NULL, &paint);
63 
64                 SkRandom rand;
65                 SkPaint textPaint;
66                 textPaint.setAntiAlias(false);
67                 textPaint.setColor(rand.nextBits(24) | 0xFF000000);
68                 textPaint.setTextSize(textSize);
69 
70                 for (int i = 0; i < testStringCount; i++) {
71                     canvas->drawText(kTestStrings[i],
72                                      strlen(kTestStrings[i]),
73                                      SkIntToScalar(x * dx),
74                                      SkIntToScalar(y * dy + textSize * i + textSize),
75                                      textPaint);
76                 }
77                 canvas->restore();
78             }
79         }
80     }
81 
82 private:
83     SkString fName;
84 
85     typedef GM INHERITED;
86 };
87 
88 //////////////////////////////////////////////////////////////////////////////
89 
MyFactory(void *)90 static GM* MyFactory(void*) { return new BlurImageFilter; }
91 static GMRegistry reg(MyFactory);
92 
93 }
94