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