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