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