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/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/SkSurface.h"
21 #include "include/effects/SkImageFilters.h"
22
23 #include <utility>
24
create_circle_texture(int size,SkColor color)25 static sk_sp<SkImage> create_circle_texture(int size, SkColor color) {
26 auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(size, size)));
27 SkCanvas* canvas = surface->getCanvas();
28 canvas->clear(0xFF000000);
29
30 SkPaint paint;
31 paint.setColor(color);
32 paint.setStrokeWidth(3);
33 paint.setStyle(SkPaint::kStroke_Style);
34
35 canvas->drawCircle(SkScalarHalf(size), SkScalarHalf(size), SkScalarHalf(size), paint);
36
37 return surface->makeImageSnapshot();
38 }
39
40 namespace skiagm {
41
42 class BigTileImageFilterGM : public GM {
43 public:
BigTileImageFilterGM()44 BigTileImageFilterGM() {
45 this->setBGColor(0xFF000000);
46 }
47
48 protected:
getName() const49 SkString getName() const override { return SkString("bigtileimagefilter"); }
50
getISize()51 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
52
onOnceBeforeDraw()53 void onOnceBeforeDraw() override {
54 fRedImage = create_circle_texture(kBitmapSize, SK_ColorRED);
55 fGreenImage = create_circle_texture(kBitmapSize, SK_ColorGREEN);
56 }
57
onDraw(SkCanvas * canvas)58 void onDraw(SkCanvas* canvas) override {
59 canvas->clear(SK_ColorBLACK);
60
61 {
62 SkPaint p;
63
64 const SkRect bound = SkRect::MakeIWH(kWidth, kHeight);
65 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fRedImage,
66 SkFilterMode::kLinear));
67
68 sk_sp<SkImageFilter> tif(SkImageFilters::Tile(
69 SkRect::MakeIWH(kBitmapSize, kBitmapSize), SkRect::MakeIWH(kWidth, kHeight),
70 std::move(imageSource)));
71
72 p.setImageFilter(std::move(tif));
73
74 canvas->saveLayer(&bound, &p);
75 canvas->restore();
76 }
77
78 {
79 SkPaint p2;
80
81 const SkRect bound2 = SkRect::MakeIWH(kBitmapSize, kBitmapSize);
82
83 sk_sp<SkImageFilter> tif(SkImageFilters::Tile(
84 SkRect::MakeIWH(kBitmapSize, kBitmapSize),
85 SkRect::MakeIWH(kBitmapSize, kBitmapSize),
86 nullptr));
87
88 p2.setImageFilter(std::move(tif));
89
90 canvas->translate(320, 320);
91 canvas->saveLayer(&bound2, &p2);
92 canvas->setMatrix(SkMatrix::I());
93
94 SkRect bound3 = SkRect::MakeXYWH(320, 320,
95 SkIntToScalar(kBitmapSize),
96 SkIntToScalar(kBitmapSize));
97 canvas->drawImageRect(fGreenImage.get(), bound2, bound3, SkSamplingOptions(), nullptr,
98 SkCanvas::kStrict_SrcRectConstraint);
99 canvas->restore();
100 }
101 }
102
103 private:
104 inline static constexpr int kWidth = 512;
105 inline static constexpr int kHeight = 512;
106 inline static constexpr int kBitmapSize = 64;
107
108 sk_sp<SkImage> fRedImage;
109 sk_sp<SkImage> fGreenImage;
110
111 using INHERITED = GM;
112 };
113
114 //////////////////////////////////////////////////////////////////////////////
115
116 DEF_GM(return new BigTileImageFilterGM;)
117 } // namespace skiagm
118