1 /*
2  * Copyright 2011 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/SkFilterQuality.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkShader.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTileMode.h"
20 
21 /*
22  *  Want to ensure that our bitmap sampler (in bitmap shader) keeps plenty of
23  *  precision when scaling very large images (where the dx might get very small.
24  */
25 
26 #define W   257
27 #define H   161
28 
29 class GiantBitmapGM : public skiagm::GM {
30     SkBitmap* fBM;
31     SkTileMode fMode;
32     bool fDoFilter;
33     bool fDoRotate;
34 
getBitmap()35     const SkBitmap& getBitmap() {
36         if (nullptr == fBM) {
37             fBM = new SkBitmap;
38             fBM->allocN32Pixels(W, H);
39             fBM->eraseColor(SK_ColorWHITE);
40 
41             const SkColor colors[] = {
42                 SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
43             };
44 
45             SkCanvas canvas(*fBM);
46             SkPaint paint;
47             paint.setAntiAlias(true);
48             paint.setStrokeWidth(SkIntToScalar(20));
49 
50 #if 0
51             for (int y = -H*2; y < H; y += 50) {
52                 SkScalar yy = SkIntToScalar(y);
53                 paint.setColor(colors[y/50 & 0x3]);
54                 canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
55                                 paint);
56             }
57 #else
58             for (int x = -W; x < W; x += 60) {
59                 paint.setColor(colors[x/60 & 0x3]);
60 
61                 SkScalar xx = SkIntToScalar(x);
62                 canvas.drawLine(xx, 0, xx, SkIntToScalar(H),
63                                 paint);
64             }
65 #endif
66         }
67         return *fBM;
68     }
69 
70 public:
GiantBitmapGM(SkTileMode mode,bool doFilter,bool doRotate)71     GiantBitmapGM(SkTileMode mode, bool doFilter, bool doRotate) : fBM(nullptr) {
72         fMode = mode;
73         fDoFilter = doFilter;
74         fDoRotate = doRotate;
75     }
76 
~GiantBitmapGM()77     ~GiantBitmapGM() override { delete fBM; }
78 
79 protected:
80 
onShortName()81     SkString onShortName() override {
82         SkString str("giantbitmap_");
83         switch (fMode) {
84             case SkTileMode::kClamp:
85                 str.append("clamp");
86                 break;
87             case SkTileMode::kRepeat:
88                 str.append("repeat");
89                 break;
90             case SkTileMode::kMirror:
91                 str.append("mirror");
92                 break;
93             case SkTileMode::kDecal:
94                 str.append("decal");
95                 break;
96             default:
97                 break;
98         }
99         str.append(fDoFilter ? "_bilerp" : "_point");
100         str.append(fDoRotate ? "_rotate" : "_scale");
101         return str;
102     }
103 
onISize()104     SkISize onISize() override { return SkISize::Make(640, 480); }
105 
onDraw(SkCanvas * canvas)106     void onDraw(SkCanvas* canvas) override {
107         SkPaint paint;
108 
109         SkMatrix m;
110         if (fDoRotate) {
111 //            m.setRotate(SkIntToScalar(30), 0, 0);
112             m.setSkew(SK_Scalar1, 0, 0, 0);
113 //            m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
114         } else {
115             SkScalar scale = 11*SK_Scalar1/12;
116             m.setScale(scale, scale);
117         }
118         paint.setShader(getBitmap().makeShader(fMode, fMode, &m));
119         paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
120 
121         canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
122 
123 //        SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16);
124 //        canvas->drawRect(r, paint); return;
125         canvas->drawPaint(paint);
126     }
127 
128 private:
129     using INHERITED = GM;
130 };
131 
132 ///////////////////////////////////////////////////////////////////////////////
133 
134 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, false, false); )
135 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, false, false); )
136 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, false, false); )
137 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, true, false); )
138 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, true, false); )
139 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, true, false); )
140 
141 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, false, true); )
142 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, false, true); )
143 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, false, true); )
144 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, true, true); )
145 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, true, true); )
146 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, true, true); )
147