• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(sigmaX, sigmaY));
62                 paint.setImageFilter(blur);
63                 canvas->saveLayer(nullptr, &paint);
64 
65                 SkRandom rand;
66                 SkPaint textPaint;
67                 textPaint.setAntiAlias(false);
68                 textPaint.setColor(sk_tool_utils::color_to_565(rand.nextBits(24) | 0xFF000000));
69                 sk_tool_utils::set_portable_typeface(&textPaint);
70                 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 
84 private:
85     SkString fName;
86 
87     typedef GM INHERITED;
88 };
89 
90 //////////////////////////////////////////////////////////////////////////////
91 
MyFactory(void *)92 static GM* MyFactory(void*) { return new BlurImageFilter; }
93 static GMRegistry reg(MyFactory);
94 
95 }
96