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