xref: /aosp_15_r20/external/skia/gm/tilemodes.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/SkFont.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.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 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "include/utils/SkTextUtils.h"
28 #include "tools/DecodeUtils.h"
29 #include "tools/GpuToolUtils.h"
30 #include "tools/Resources.h"
31 #include "tools/ToolUtils.h"
32 #include "tools/fonts/FontToolUtils.h"
33 
34 #include <functional>
35 
makebm(SkBitmap * bm,SkColorType ct,int w,int h)36 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
37     bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
38     bm->eraseColor(SK_ColorTRANSPARENT);
39 
40     SkCanvas    canvas(*bm);
41     SkPoint     pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
42     SkColor     colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
43     SkScalar    pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
44     SkPaint     paint;
45 
46     paint.setDither(true);
47     paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, std::size(colors),
48                                                  SkTileMode::kClamp));
49     canvas.drawPaint(paint);
50 }
51 
setup(SkCanvas * canvas,SkPaint * paint,const SkBitmap & bm,SkFilterMode fm,SkTileMode tmx,SkTileMode tmy)52 static void setup(SkCanvas* canvas, SkPaint* paint, const SkBitmap& bm, SkFilterMode fm,
53                   SkTileMode tmx, SkTileMode tmy) {
54     sk_sp<SkImage> img = SkImages::RasterFromBitmap(bm);
55     img = ToolUtils::MakeTextureImage(canvas, std::move(img));
56     if (img) {
57         // img can be null if the GPU context has been abandoned.
58         paint->setShader(img->makeShader(tmx, tmy, SkSamplingOptions(fm)));
59     }
60 }
61 
62 constexpr SkColorType gColorTypes[] = {
63     kN32_SkColorType,
64     kRGB_565_SkColorType,
65 };
66 
67 class TilingGM : public skiagm::GM {
68 public:
TilingGM(bool powerOfTwoSize)69     TilingGM(bool powerOfTwoSize)
70             : fPowerOfTwoSize(powerOfTwoSize) {
71     }
72 
73     SkBitmap    fTexture[std::size(gColorTypes)];
74 
75 protected:
76 
77     enum {
78         kPOTSize = 32,
79         kNPOTSize = 21,
80     };
81 
getName() const82     SkString getName() const override {
83         SkString name("tilemodes");
84         if (!fPowerOfTwoSize) {
85             name.append("_npot");
86         }
87         return name;
88     }
89 
getISize()90     SkISize getISize() override { return SkISize::Make(880, 560); }
91 
onOnceBeforeDraw()92     void onOnceBeforeDraw() override {
93         int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
94         for (size_t i = 0; i < std::size(gColorTypes); i++) {
95             makebm(&fTexture[i], gColorTypes[i], size, size);
96         }
97     }
98 
onDraw(SkCanvas * canvas)99     void onDraw(SkCanvas* canvas) override {
100         SkPaint textPaint;
101         SkFont  font = ToolUtils::DefaultPortableFont();
102 
103         int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
104 
105         SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
106 
107         const char* gConfigNames[] = { "8888", "565" };
108 
109         constexpr SkFilterMode gFilters[] = { SkFilterMode::kNearest, SkFilterMode::kLinear };
110         static const char* gFilterNames[] = { "point", "bilinear" };
111 
112         constexpr SkTileMode gModes[] = {
113             SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
114         static const char* gModeNames[] = { "C", "R", "M" };
115 
116         SkScalar y = SkIntToScalar(24);
117         SkScalar x = SkIntToScalar(10);
118 
119         for (size_t kx = 0; kx < std::size(gModes); kx++) {
120             for (size_t ky = 0; ky < std::size(gModes); ky++) {
121                 SkPaint p;
122                 p.setDither(true);
123                 SkString str;
124                 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
125 
126                 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, p,
127                                         SkTextUtils::kCenter_Align);
128 
129                 x += r.width() * 4 / 3;
130             }
131         }
132 
133         y += SkIntToScalar(16);
134 
135         for (size_t i = 0; i < std::size(gColorTypes); i++) {
136             for (size_t j = 0; j < std::size(gFilters); j++) {
137                 x = SkIntToScalar(10);
138                 for (size_t kx = 0; kx < std::size(gModes); kx++) {
139                     for (size_t ky = 0; ky < std::size(gModes); ky++) {
140                         SkPaint paint;
141 #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
142       // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
143                         if (!fPowerOfTwoSize) {
144                             makebm(&fTexture[i], gColorTypes[i], size, size);
145                         }
146 #endif
147                         setup(canvas, &paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
148                         paint.setDither(true);
149 
150                         canvas->save();
151                         canvas->translate(x, y);
152                         canvas->drawRect(r, paint);
153                         canvas->restore();
154 
155                         x += r.width() * 4 / 3;
156                     }
157                 }
158                 canvas->drawString(SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
159                                    x, y + r.height() * 2 / 3, font, textPaint);
160 
161                 y += r.height() * 4 / 3;
162             }
163         }
164     }
165 
166 private:
167     bool fPowerOfTwoSize;
168     using INHERITED = skiagm::GM;
169 };
170 DEF_GM( return new TilingGM(true); )
171 DEF_GM( return new TilingGM(false); )
172 
173 constexpr int gWidth = 32;
174 constexpr int gHeight = 32;
175 
make_bm(SkTileMode tx,SkTileMode ty)176 static sk_sp<SkShader> make_bm(SkTileMode tx, SkTileMode ty) {
177     SkBitmap bm;
178     makebm(&bm, kN32_SkColorType, gWidth, gHeight);
179     return bm.makeShader(tx, ty, SkSamplingOptions());
180 }
181 
make_grad(SkTileMode tx,SkTileMode ty)182 static sk_sp<SkShader> make_grad(SkTileMode tx, SkTileMode ty) {
183     SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
184     SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
185     SkScalar rad = SkIntToScalar(gWidth)/2;
186     SkColor  colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
187 
188     int index = (int)ty;
189     switch (index % 3) {
190         case 0:
191             return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors), tx);
192         case 1:
193             return SkGradientShader::MakeRadial(center, rad, colors, nullptr, std::size(colors), tx);
194         case 2:
195             return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr,
196                                                std::size(colors), tx, 135, 225, 0, nullptr);
197     }
198     return nullptr;
199 }
200 
201 typedef sk_sp<SkShader> (*ShaderProc)(SkTileMode, SkTileMode);
202 
203 class Tiling2GM : public skiagm::GM {
204     ShaderProc fProc;
205     const char* fName;
206 
207 public:
Tiling2GM(ShaderProc proc,const char name[])208     Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
209 
210 private:
getName() const211     SkString getName() const override { return SkString(fName); }
212 
getISize()213     SkISize getISize() override { return SkISize::Make(650, 610); }
214 
onDraw(SkCanvas * canvas)215     void onDraw(SkCanvas* canvas) override {
216         canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
217 
218         const SkScalar w = SkIntToScalar(gWidth);
219         const SkScalar h = SkIntToScalar(gHeight);
220         SkRect r = { -w, -h, w*2, h*2 };
221 
222         constexpr SkTileMode gModes[] = {
223             SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror
224         };
225         const char* gModeNames[] = {
226             "Clamp", "Repeat", "Mirror"
227         };
228 
229         SkScalar y = SkIntToScalar(24);
230         SkScalar x = SkIntToScalar(66);
231 
232         SkFont font = ToolUtils::DefaultPortableFont();
233 
234         for (size_t kx = 0; kx < std::size(gModes); kx++) {
235             SkString str(gModeNames[kx]);
236             SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
237                                     SkTextUtils::kCenter_Align);
238             x += r.width() * 4 / 3;
239         }
240 
241         y += SkIntToScalar(16) + h;
242 
243         for (size_t ky = 0; ky < std::size(gModes); ky++) {
244             x = SkIntToScalar(16) + w;
245 
246             SkString str(gModeNames[ky]);
247             SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(),
248                                     SkTextUtils::kRight_Align);
249 
250             x += SkIntToScalar(50);
251             for (size_t kx = 0; kx < std::size(gModes); kx++) {
252                 SkPaint paint;
253                 paint.setShader(fProc(gModes[kx], gModes[ky]));
254 
255                 canvas->save();
256                 canvas->translate(x, y);
257                 canvas->drawRect(r, paint);
258                 canvas->restore();
259 
260                 x += r.width() * 4 / 3;
261             }
262             y += r.height() * 4 / 3;
263         }
264     }
265 };
266 
267 DEF_GM( return new Tiling2GM(make_bm,   "tilemode_bitmap"); )
268 DEF_GM( return new Tiling2GM(make_grad, "tilemode_gradient"); )
269 
270 ////////////////////
271 
272 DEF_SIMPLE_GM(tilemode_decal, canvas, 720, 1100) {
273     auto img = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
274     SkPaint bgpaint;
275     bgpaint.setColor(SK_ColorYELLOW);
276 
277     SkRect r = { -20, -20, img->width() + 20.0f, img->height() + 20.0f };
278     canvas->translate(45, 45);
279 
280     std::function<void(SkPaint*, SkTileMode, SkTileMode)> shader_procs[] = {
__anon3c3fea380202() 281         [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
282             // Test no filtering with decal mode
283             paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkFilterMode::kNearest)));
284         },
__anon3c3fea380302() 285         [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
286             // Test bilerp approximation for decal mode (or clamp to border HW)
287             paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkFilterMode::kLinear)));
288         },
__anon3c3fea380402() 289         [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
290             // Test bicubic filter with decal mode
291             paint->setShader(img->makeShader(tx, ty, SkSamplingOptions(SkCubicResampler::Mitchell())));
292         },
__anon3c3fea380502() 293         [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
294             SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
295             const SkPoint pts[] = {{ 0, 0 }, {img->width()*1.0f, img->height()*1.0f }};
296             const SkScalar* pos = nullptr;
297             const int count = std::size(colors);
298             paint->setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, tx));
299         },
__anon3c3fea380602() 300         [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
301             SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
302             const SkScalar* pos = nullptr;
303             const int count = std::size(colors);
304             paint->setShader(SkGradientShader::MakeRadial({ img->width()*0.5f, img->width()*0.5f },
305                                                       img->width()*0.5f, colors, pos, count, tx));
306         },
307     };
308 
309     const struct XY {
310         SkTileMode  fX;
311         SkTileMode  fY;
312     } pairs[] = {
313         { SkTileMode::kClamp,    SkTileMode::kClamp },
314         { SkTileMode::kClamp,    SkTileMode::kDecal },
315         { SkTileMode::kDecal,    SkTileMode::kClamp },
316         { SkTileMode::kDecal,    SkTileMode::kDecal },
317     };
318     for (const auto& p : pairs) {
319         SkPaint paint;
320         canvas->save();
321         for (const auto& proc : shader_procs) {
322             canvas->save();
323             // Apply a slight rotation to highlight the differences between filtered and unfiltered
324             // decal edges
325             canvas->rotate(4);
326             canvas->drawRect(r, bgpaint);
327             proc(&paint, p.fX, p.fY);
328             canvas->drawRect(r, paint);
329             canvas->restore();
330             canvas->translate(0, r.height() + 20);
331         }
332         canvas->restore();
333         canvas->translate(r.width() + 10, 0);
334     }
335 }
336