• 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/SkImage.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSurface.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "tools/ToolUtils.h"
22 
23 #include <utility>
24 
make_image(SkCanvas * rootCanvas)25 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) {
26     SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
27     auto        surface(ToolUtils::makeSurface(rootCanvas, info));
28 
29     SkPaint paint;
30     paint.setAntiAlias(true);
31     paint.setColor(SK_ColorRED);
32     surface->getCanvas()->drawCircle(50, 50, 50, paint);
33     return surface->makeImageSnapshot();
34 }
35 
show_image(SkCanvas * canvas,SkImage * image,sk_sp<SkImageFilter> filter)36 static void show_image(SkCanvas* canvas, SkImage* image, sk_sp<SkImageFilter> filter) {
37     SkPaint paint;
38     paint.setStyle(SkPaint::kStroke_Style);
39     SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(SK_ScalarHalf,
40                                                                            SK_ScalarHalf);
41     canvas->drawRect(r, paint);
42 
43     paint.setStyle(SkPaint::kFill_Style);
44     paint.setImageFilter(filter);
45     canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
46 }
47 
48 typedef sk_sp<SkImageFilter> (*ImageFilterFactory)();
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[] = {
__anonbc7067ac0102null55         []{ return SkImageFilters::Blur(8, 8, nullptr); },
__anonbc7067ac0202null56         []{ return SkImageFilters::Dilate(8, 8, nullptr); },
__anonbc7067ac0302null57         []{ return SkImageFilters::Erode(8, 8, nullptr); },
__anonbc7067ac0402null58         []{ return SkImageFilters::Offset(8, 8, nullptr); },
59     };
60 
61     const SkMatrix matrices[] = {
62         SkMatrix::Scale(SK_ScalarHalf, SK_ScalarHalf),
63         SkMatrix::Scale(2, 2),
64         SkMatrix::Translate(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