• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 "sk_tool_utils.h"
9 #include "SkBlurImageFilter.h"
10 #include "SkColor.h"
11 #include "SkDropShadowImageFilter.h"
12 #include "SkOffsetImageFilter.h"
13 #include "SkScalar.h"
14 #include "gm.h"
15 
16 #define RESIZE_FACTOR_X SkIntToScalar(2)
17 #define RESIZE_FACTOR_Y SkIntToScalar(5)
18 
19 namespace skiagm {
20 
21 class ImageFiltersStrokedGM : public GM {
22 public:
ImageFiltersStrokedGM()23     ImageFiltersStrokedGM() {
24         this->setBGColor(0x00000000);
25     }
26 
27 protected:
28 
onShortName()29     SkString onShortName() override {
30         return SkString("imagefiltersstroked");
31     }
32 
onISize()33     SkISize onISize() override {
34         return SkISize::Make(860, 500);
35     }
36 
draw_circle(SkCanvas * canvas,const SkRect & r,const SkPaint & paint)37     static void draw_circle(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) {
38         canvas->drawCircle(r.centerX(), r.centerY(),
39                            r.width() * 2 / 5, paint);
40     }
41 
draw_line(SkCanvas * canvas,const SkRect & r,const SkPaint & paint)42     static void draw_line(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) {
43         canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, paint);
44     }
45 
draw_rect(SkCanvas * canvas,const SkRect & r,const SkPaint & paint)46     static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) {
47         canvas->drawRect(r, paint);
48     }
49 
onDraw(SkCanvas * canvas)50     void onDraw(SkCanvas* canvas) override {
51         void (*drawProc[])(SkCanvas*, const SkRect&, const SkPaint&) = {
52             draw_line, draw_rect, draw_circle,
53         };
54 
55         canvas->clear(SK_ColorBLACK);
56 
57         SkMatrix resizeMatrix;
58         resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y);
59 
60         SkImageFilter* filters[] = {
61             SkBlurImageFilter::Create(5, 5),
62             SkDropShadowImageFilter::Create(10, 10, 3, 3, SK_ColorGREEN,
63                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
64             SkOffsetImageFilter::Create(-16, 32),
65             SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQuality),
66         };
67 
68         SkRect r = SkRect::MakeWH(64, 64);
69         SkScalar margin = 32;
70         SkPaint paint;
71         paint.setColor(SK_ColorWHITE);
72         paint.setAntiAlias(true);
73         paint.setStrokeWidth(10);
74         paint.setStyle(SkPaint::kStroke_Style);
75         paint.setTextSize(48);
76         paint.setTextAlign(SkPaint::kCenter_Align);
77 
78         for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) {
79             canvas->translate(0, margin);
80             canvas->save();
81             for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
82                 canvas->translate(margin, 0);
83                 canvas->save();
84                 if (2 == j) {
85                     canvas->translate(16, -32);
86                 } else if (3 == j) {
87                     canvas->scale(SkScalarInvert(RESIZE_FACTOR_X),
88                                   SkScalarInvert(RESIZE_FACTOR_Y));
89                 }
90                 paint.setImageFilter(filters[j]);
91                 drawProc[i](canvas, r, paint);
92                 canvas->restore();
93                 canvas->translate(r.width() + margin, 0);
94             }
95             canvas->restore();
96             canvas->translate(0, r.height());
97         }
98 
99         for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
100             SkSafeUnref(filters[i]);
101         }
102     }
103 
104 private:
105     typedef GM INHERITED;
106 };
107 
108 //////////////////////////////////////////////////////////////////////////////
109 
110 DEF_GM(return new ImageFiltersStrokedGM;)
111 
112 }
113