• 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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/core/SkTypes.h"
24 #include "include/effects/SkImageFilters.h"
25 #include "include/utils/SkTextUtils.h"
26 #include "tools/ToolUtils.h"
27 
28 #include <utility>
29 
30 ///////////////////////////////////////////////////////////////////////////////
31 
draw_paint(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)32 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
33     SkPaint paint;
34     paint.setImageFilter(std::move(imf));
35     paint.setColor(SK_ColorBLACK);
36     canvas->save();
37     canvas->clipRect(r);
38     canvas->drawPaint(paint);
39     canvas->restore();
40 }
41 
draw_path(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)42 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
43     SkPaint paint;
44     paint.setColor(SK_ColorMAGENTA);
45     paint.setImageFilter(std::move(imf));
46     paint.setAntiAlias(true);
47     canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
48 }
49 
draw_text(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)50 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
51     SkPaint paint;
52     paint.setImageFilter(std::move(imf));
53     paint.setColor(SK_ColorGREEN);
54 
55     SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
56     SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
57 }
58 
draw_bitmap(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)59 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
60     SkPaint paint;
61 
62     SkIRect bounds;
63     r.roundOut(&bounds);
64 
65     auto surf = SkSurface::MakeRasterN32Premul(bounds.width(), bounds.height());
66     draw_path(surf->getCanvas(), r, nullptr);
67 
68     paint.setImageFilter(std::move(imf));
69     surf->draw(canvas, 0, 0, SkSamplingOptions(), &paint);
70 }
71 
72 ///////////////////////////////////////////////////////////////////////////////
73 
74 class ImageFiltersCroppedGM : public skiagm::GM {
75 public:
ImageFiltersCroppedGM()76     ImageFiltersCroppedGM () {}
77 
78 protected:
onShortName()79     SkString onShortName() override {
80         return SkString("imagefilterscropped");
81     }
82 
onISize()83     SkISize onISize() override { return SkISize::Make(400, 960); }
84 
make_checkerboard()85     void make_checkerboard() {
86         auto surf = SkSurface::MakeRasterN32Premul(80, 80);
87         auto canvas = surf->getCanvas();
88         SkPaint darkPaint;
89         darkPaint.setColor(0xFF404040);
90         SkPaint lightPaint;
91         lightPaint.setColor(0xFFA0A0A0);
92         for (int y = 0; y < 80; y += 16) {
93             for (int x = 0; x < 80; x += 16) {
94                 canvas->save();
95                 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
96                 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
97                 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
98                 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
99                 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
100                 canvas->restore();
101             }
102         }
103         fCheckerboard = surf->makeImageSnapshot();
104     }
105 
draw_frame(SkCanvas * canvas,const SkRect & r)106     void draw_frame(SkCanvas* canvas, const SkRect& r) {
107         SkPaint paint;
108         paint.setStyle(SkPaint::kStroke_Style);
109         paint.setColor(SK_ColorRED);
110         canvas->drawRect(r, paint);
111     }
112 
onOnceBeforeDraw()113     void onOnceBeforeDraw() override {
114         make_checkerboard();
115     }
116 
onDraw(SkCanvas * canvas)117     void onDraw(SkCanvas* canvas) override {
118         void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
119             draw_bitmap, draw_path, draw_paint, draw_text
120         };
121 
122         sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorBLUE,
123                                                               SkBlendMode::kSrcIn));
124         SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
125         SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10);
126 
127         sk_sp<SkImageFilter> offset(SkImageFilters::Offset(-10, -10, nullptr));
128 
129         sk_sp<SkImageFilter> cfOffset(SkImageFilters::ColorFilter(cf, std::move(offset)));
130 
131         sk_sp<SkImageFilter> erodeX(SkImageFilters::Erode(8, 0, nullptr, &cropRect));
132         sk_sp<SkImageFilter> erodeY(SkImageFilters::Erode(0, 8, nullptr, &cropRect));
133 
134         sk_sp<SkImageFilter> filters[] = {
135             nullptr,
136             SkImageFilters::ColorFilter(cf, nullptr, &cropRect),
137             SkImageFilters::Blur(0.0f, 0.0f, nullptr, &cropRect),
138             SkImageFilters::Blur(1.0f, 1.0f, nullptr, &cropRect),
139             SkImageFilters::Blur(8.0f, 0.0f, nullptr, &cropRect),
140             SkImageFilters::Blur(0.0f, 8.0f, nullptr, &cropRect),
141             SkImageFilters::Blur(8.0f, 8.0f, nullptr, &cropRect),
142             SkImageFilters::Erode(1, 1, nullptr, &cropRect),
143             SkImageFilters::Erode(8, 0, std::move(erodeY), &cropRect),
144             SkImageFilters::Erode(0, 8, std::move(erodeX), &cropRect),
145             SkImageFilters::Erode(8, 8, nullptr, &cropRect),
146             SkImageFilters::Merge(nullptr, std::move(cfOffset), &cropRect),
147             SkImageFilters::Blur(8.0f, 8.0f, nullptr, &bogusRect),
148             SkImageFilters::ColorFilter(cf, nullptr, &bogusRect),
149         };
150 
151         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
152         SkScalar MARGIN = SkIntToScalar(16);
153         SkScalar DX = r.width() + MARGIN;
154         SkScalar DY = r.height() + MARGIN;
155 
156         canvas->translate(MARGIN, MARGIN);
157         for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
158             canvas->save();
159             for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
160                 SkPaint paint;
161                 canvas->drawImage(fCheckerboard, 0, 0);
162                 drawProc[j](canvas, r, filters[i]);
163                 canvas->translate(0, DY);
164             }
165             canvas->restore();
166             canvas->translate(DX, 0);
167         }
168     }
169 
170 private:
171     sk_sp<SkImage> fCheckerboard;
172     using INHERITED = GM;
173 };
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 
177 DEF_GM( return new ImageFiltersCroppedGM; )
178