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.h"
9 #include "SkCanvas.h"
10 #include "SkBlurImageFilter.h"
11 #include "SkColorFilterImageFilter.h"
12 #include "SkModeColorFilter.h"
13 #include "SkMorphologyImageFilter.h"
14 #include "SkOffsetImageFilter.h"
15 #include "SkSurface.h"
16
make_image(SkCanvas * rootCanvas)17 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) {
18 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
19 auto surface(rootCanvas->makeSurface(info));
20 if (!surface) {
21 surface = SkSurface::MakeRaster(info);
22 }
23
24 SkPaint paint;
25 paint.setAntiAlias(true);
26 paint.setColor(SK_ColorRED);
27 surface->getCanvas()->drawCircle(50, 50, 50, paint);
28 return surface->makeImageSnapshot();
29 }
30
show_image(SkCanvas * canvas,SkImage * image,sk_sp<SkImageFilter> filter)31 static void show_image(SkCanvas* canvas, SkImage* image, sk_sp<SkImageFilter> filter) {
32 SkPaint paint;
33 paint.setStyle(SkPaint::kStroke_Style);
34 SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(SK_ScalarHalf,
35 SK_ScalarHalf);
36 canvas->drawRect(r, paint);
37
38 paint.setStyle(SkPaint::kFill_Style);
39 paint.setImageFilter(filter);
40 canvas->drawImage(image, 0, 0, &paint);
41 }
42
43 typedef sk_sp<SkImageFilter> (*ImageFilterFactory)();
44
45 // +[]{...} did not work on windows (VS)
46 // (ImageFilterFactory)[]{...} did not work on linux (gcc)
47 // hence this cast function
IFCCast(T arg)48 template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; }
49
50 // Show the effect of localmatriximagefilter with various matrices, on various filters
51 DEF_SIMPLE_GM(localmatriximagefilter, canvas, 640, 640) {
52 sk_sp<SkImage> image0(make_image(canvas));
53
54 const ImageFilterFactory factories[] = {
__anonb0db15250102null55 IFCCast([]{ return SkBlurImageFilter::Make(8, 8, nullptr); }),
__anonb0db15250202null56 IFCCast([]{ return SkDilateImageFilter::Make(8, 8, nullptr); }),
__anonb0db15250302null57 IFCCast([]{ return SkErodeImageFilter::Make(8, 8, nullptr); }),
__anonb0db15250402null58 IFCCast([]{ return SkOffsetImageFilter::Make(8, 8, nullptr); }),
59 };
60
61 const SkMatrix matrices[] = {
62 SkMatrix::MakeScale(SK_ScalarHalf, SK_ScalarHalf),
63 SkMatrix::MakeScale(2, 2),
64 SkMatrix::MakeTrans(10, 10)
65 };
66
67 const SkScalar spacer = image0->width() * 3.0f / 2;
68
69 canvas->translate(40, 40);
70 for (auto&& factory : factories) {
71 sk_sp<SkImageFilter> filter(factory());
72
73 canvas->save();
74 show_image(canvas, image0.get(), filter);
75 for (const auto& matrix : matrices) {
76 sk_sp<SkImageFilter> localFilter(filter->makeWithLocalMatrix(matrix));
77 canvas->translate(spacer, 0);
78 show_image(canvas, image0.get(), std::move(localFilter));
79 }
80 canvas->restore();
81 canvas->translate(0, spacer);
82 }
83 }
84