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