add helper makeShader for the common Clamp case
[platform/upstream/libSkiaSharp.git] / gm / filterbug.cpp
1 /*
2  * Copyright 2016 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 "SkImageShader.h"
10
11 static const sk_sp<SkImage> make_image(int firstBlackRow, int lastBlackRow) {
12     static const int kWidth = 25;
13     static const int kHeight = 27;
14
15     SkBitmap bm;
16     bm.allocN32Pixels(kWidth, kHeight);
17     bm.eraseColor(SK_ColorWHITE);
18     for (int y = firstBlackRow; y < lastBlackRow; ++y) {
19         for (int x = 0; x < kWidth; ++x) {
20             *bm.getAddr32(x, y) = SkPackARGB32(0xFF, 0x0, 0x0, 0x0);
21         }
22     }
23
24     bm.setAlphaType(SkAlphaType::kOpaque_SkAlphaType);
25     bm.setImmutable();
26
27     return SkImage::MakeFromBitmap(bm);
28 }
29
30 // GM to reproduce crbug.com/673261.
31 class FilterBugGM : public skiagm::GM {
32 public:
33     FilterBugGM() { this->setBGColor(SK_ColorRED); }
34
35 protected:
36     SkString onShortName() override { return SkString("filterbug"); }
37
38     SkISize onISize() override { return SkISize::Make(150, 150); }
39
40     void onOnceBeforeDraw() override {
41         // The top texture has 5 black rows on top and then 22 white rows on the bottom
42         fTop = make_image(0, 5);
43         // The bottom texture has 5 black rows on the bottom and then 22 white rows on the top
44         fBot = make_image(22, 27);
45     }
46
47     void onDraw(SkCanvas* canvas) override {
48         static const SkFilterQuality kFilterQuality = SkFilterQuality::kHigh_SkFilterQuality;
49         static const bool kDoAA = true;
50
51         {
52             SkRect r1 = SkRect::MakeXYWH(50.0f, 0.0f, 50.0f, 50.0f);
53             SkPaint p1;
54             p1.setAntiAlias(kDoAA);
55             p1.setFilterQuality(kFilterQuality);
56             SkMatrix localMat;
57             localMat.setScaleTranslate(2.0f, 2.0f, 50.0f, 0.0f);
58             p1.setShader(SkImageShader::Make(fTop,
59                                              SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
60                                              &localMat));
61
62             canvas->drawRect(r1, p1);
63         }
64
65         {
66             SkRect r2 = SkRect::MakeXYWH(50.0f, 50.0f, 50.0f, 36.0f);
67
68             SkPaint p2;
69             p2.setColor(SK_ColorWHITE);
70             p2.setAntiAlias(kDoAA);
71             p2.setFilterQuality(kFilterQuality);
72
73             canvas->drawRect(r2, p2);
74         }
75
76         {
77             SkRect r3 = SkRect::MakeXYWH(50.0f, 86.0f, 50.0f, 50.0f);
78
79             SkPaint p3;
80             p3.setAntiAlias(kDoAA);
81             p3.setFilterQuality(kFilterQuality);
82             SkMatrix localMat;
83             localMat.setScaleTranslate(2.0f, 2.0f, 50.0f, 86.0f);
84             p3.setShader(SkImageShader::Make(fBot,
85                                              SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
86                                              &localMat));
87
88             canvas->drawRect(r3, p3);
89         }
90     }
91
92 private:
93     sk_sp<SkImage> fTop;
94     sk_sp<SkImage> fBot;
95
96     typedef skiagm::GM INHERITED;
97 };
98
99 //////////////////////////////////////////////////////////////////////////////
100
101 DEF_GM(return new FilterBugGM;)