xref: /aosp_15_r20/external/skia/gm/giantbitmap.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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:
getName() const79     SkString getName() const override {
80         SkString str("giantbitmap_");
81         switch (fMode) {
82             case SkTileMode::kClamp:
83                 str.append("clamp");
84                 break;
85             case SkTileMode::kRepeat:
86                 str.append("repeat");
87                 break;
88             case SkTileMode::kMirror:
89                 str.append("mirror");
90                 break;
91             case SkTileMode::kDecal:
92                 str.append("decal");
93                 break;
94             default:
95                 break;
96         }
97         str.append(fDoFilter ? "_bilerp" : "_point");
98         str.append(fDoRotate ? "_rotate" : "_scale");
99         return str;
100     }
101 
getISize()102     SkISize getISize() override { return SkISize::Make(640, 480); }
103 
onDraw(SkCanvas * canvas)104     void onDraw(SkCanvas* canvas) override {
105         SkPaint paint;
106 
107         SkMatrix m;
108         if (fDoRotate) {
109             m.setSkew(SK_Scalar1, 0, 0, 0);
110         } else {
111             SkScalar scale = 11*SK_Scalar1/12;
112             m.setScale(scale, scale);
113         }
114         paint.setShader(getBitmap().makeShader(
115                                            fMode, fMode,
116                                            SkSamplingOptions(fDoFilter ? SkFilterMode::kLinear
117                                                                        : SkFilterMode::kNearest),
118                                            m));
119 
120         canvas->translate(50, 50);
121 
122         canvas->drawPaint(paint);
123     }
124 
125 private:
126     using INHERITED = GM;
127 };
128 
129 ///////////////////////////////////////////////////////////////////////////////
130 
131 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, false, false); )
132 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, false, false); )
133 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, false, false); )
134 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, true, false); )
135 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, true, false); )
136 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, true, false); )
137 
138 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, false, true); )
139 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, false, true); )
140 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, false, true); )
141 DEF_GM( return new GiantBitmapGM(SkTileMode::kClamp, true, true); )
142 DEF_GM( return new GiantBitmapGM(SkTileMode::kRepeat, true, true); )
143 DEF_GM( return new GiantBitmapGM(SkTileMode::kMirror, true, true); )
144