• 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/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(SkSurface::MakeRasterN32Premul(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:
49 
onShortName()50     SkString onShortName() override {
51         return SkString("bigtileimagefilter");
52     }
53 
onISize()54     SkISize onISize() override {
55         return SkISize::Make(kWidth, kHeight);
56     }
57 
onOnceBeforeDraw()58     void onOnceBeforeDraw() override {
59         fRedImage = create_circle_texture(kBitmapSize, SK_ColorRED);
60         fGreenImage = create_circle_texture(kBitmapSize, SK_ColorGREEN);
61     }
62 
onDraw(SkCanvas * canvas)63     void onDraw(SkCanvas* canvas) override {
64         canvas->clear(SK_ColorBLACK);
65 
66         {
67             SkPaint p;
68 
69             const SkRect bound = SkRect::MakeIWH(kWidth, kHeight);
70             sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fRedImage));
71 
72             sk_sp<SkImageFilter> tif(SkImageFilters::Tile(
73                     SkRect::MakeIWH(kBitmapSize, kBitmapSize), SkRect::MakeIWH(kWidth, kHeight),
74                     std::move(imageSource)));
75 
76             p.setImageFilter(std::move(tif));
77 
78             canvas->saveLayer(&bound, &p);
79             canvas->restore();
80         }
81 
82         {
83             SkPaint p2;
84 
85             const SkRect bound2 = SkRect::MakeIWH(kBitmapSize, kBitmapSize);
86 
87             sk_sp<SkImageFilter> tif(SkImageFilters::Tile(
88                     SkRect::MakeIWH(kBitmapSize, kBitmapSize),
89                     SkRect::MakeIWH(kBitmapSize, kBitmapSize),
90                     nullptr));
91 
92             p2.setImageFilter(std::move(tif));
93 
94             canvas->translate(320, 320);
95             canvas->saveLayer(&bound2, &p2);
96             canvas->setMatrix(SkMatrix::I());
97 
98             SkRect bound3 = SkRect::MakeXYWH(320, 320,
99                                              SkIntToScalar(kBitmapSize),
100                                              SkIntToScalar(kBitmapSize));
101             canvas->drawImageRect(fGreenImage.get(), bound2, bound3, SkSamplingOptions(), nullptr,
102                                   SkCanvas::kStrict_SrcRectConstraint);
103             canvas->restore();
104         }
105     }
106 
107 private:
108     inline static constexpr int kWidth = 512;
109     inline static constexpr int kHeight = 512;
110     inline static constexpr int kBitmapSize = 64;
111 
112     sk_sp<SkImage> fRedImage;
113     sk_sp<SkImage> fGreenImage;
114 
115     using INHERITED = GM;
116 };
117 
118 //////////////////////////////////////////////////////////////////////////////
119 
120 DEF_GM(return new BigTileImageFilterGM;)
121 }  // namespace skiagm
122