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