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/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTileMode.h"
23 #include "include/core/SkTypeface.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkGradientShader.h"
26 #include "include/utils/SkTextUtils.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/fonts/FontToolUtils.h"
29
30 const SkSamplingOptions gSamplings[] = {
31 SkSamplingOptions(SkFilterMode::kNearest),
32 SkSamplingOptions(SkFilterMode::kLinear),
33 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
34 SkSamplingOptions(SkCubicResampler::Mitchell()),
35 SkSamplingOptions::Aniso(16),
36 };
37
makebm(SkBitmap * bm,SkColorType ct,int w,int h)38 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
39 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
40 bm->eraseColor(SK_ColorTRANSPARENT);
41
42 SkCanvas canvas(*bm);
43 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
44 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
45 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
46 SkPaint paint;
47
48 paint.setDither(true);
49 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
50 std::size(colors), SkTileMode::kClamp));
51 canvas.drawPaint(paint);
52 }
53
setup(SkPaint * paint,const SkBitmap & bm,const SkSamplingOptions & sampling,SkTileMode tmx,SkTileMode tmy)54 static void setup(SkPaint* paint, const SkBitmap& bm, const SkSamplingOptions& sampling,
55 SkTileMode tmx, SkTileMode tmy) {
56 paint->setShader(bm.makeShader(tmx, tmy, sampling));
57 }
58
59 constexpr SkColorType gColorTypes[] = {
60 kN32_SkColorType,
61 kRGB_565_SkColorType,
62 };
63
64 class ScaledTilingGM : public skiagm::GM {
65 public:
ScaledTilingGM(bool powerOfTwoSize)66 ScaledTilingGM(bool powerOfTwoSize)
67 : fPowerOfTwoSize(powerOfTwoSize) {
68 }
69
70 SkBitmap fTexture[std::size(gColorTypes)];
71
72 protected:
73 static constexpr int kPOTSize = 4;
74 static constexpr int kNPOTSize = 3;
75
getName() const76 SkString getName() const override {
77 SkString name("scaled_tilemodes");
78 if (!fPowerOfTwoSize) {
79 name.append("_npot");
80 }
81 return name;
82 }
83
getISize()84 SkISize getISize() override { return SkISize::Make(880, 880); }
85
onOnceBeforeDraw()86 void onOnceBeforeDraw() override {
87 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
88 for (size_t i = 0; i < std::size(gColorTypes); i++) {
89 makebm(&fTexture[i], gColorTypes[i], size, size);
90 }
91 }
92
onDraw(SkCanvas * canvas)93 void onDraw(SkCanvas* canvas) override {
94 SkPaint textPaint;
95 SkFont font(ToolUtils::DefaultPortableTypeface(), 12);
96
97 float scale = 32.f/kPOTSize;
98
99 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
100
101 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
102
103 const char* gColorTypeNames[] = { "8888", "565" };
104
105 const char* gFilterNames[] = { "Nearest", "Linear", "Trilinear", "Mitchell", "Aniso" };
106
107 constexpr SkTileMode gModes[] = {
108 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
109 const char* gModeNames[] = { "C", "R", "M" };
110
111 SkScalar y = SkIntToScalar(24);
112 SkScalar x = SkIntToScalar(10)/scale;
113
114 for (size_t kx = 0; kx < std::size(gModes); kx++) {
115 for (size_t ky = 0; ky < std::size(gModes); ky++) {
116 SkString str;
117 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
118
119 SkTextUtils::DrawString(canvas, str.c_str(), scale*(x + r.width()/2), y, font, SkPaint(),
120 SkTextUtils::kCenter_Align);
121
122 x += r.width() * 4 / 3;
123 }
124 }
125
126 y = SkIntToScalar(40) / scale;
127
128 for (size_t i = 0; i < std::size(gColorTypes); i++) {
129 for (size_t j = 0; j < std::size(gSamplings); j++) {
130 x = SkIntToScalar(10)/scale;
131 for (size_t kx = 0; kx < std::size(gModes); kx++) {
132 for (size_t ky = 0; ky < std::size(gModes); ky++) {
133 SkPaint paint;
134 #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
135 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
136 if (!fPowerOfTwoSize) {
137 makebm(&fTexture[i], gColorTypes[i], size, size);
138 }
139 #endif
140 setup(&paint, fTexture[i], gSamplings[j], gModes[kx], gModes[ky]);
141 paint.setDither(true);
142
143 canvas->save();
144 canvas->scale(scale,scale);
145 canvas->translate(x, y);
146 canvas->drawRect(r, paint);
147 canvas->restore();
148
149 x += r.width() * 4 / 3;
150 }
151 }
152 canvas->drawString(SkStringPrintf("%s, %s", gColorTypeNames[i], gFilterNames[j]),
153 scale * x, scale * (y + r.height() * 2 / 3), font, textPaint);
154
155 y += r.height() * 4 / 3;
156 }
157 }
158 }
159
160 private:
161 bool fPowerOfTwoSize;
162 using INHERITED = skiagm::GM;
163 };
164
165 constexpr int gWidth = 32;
166 constexpr int gHeight = 32;
167
make_bm(SkTileMode tx,SkTileMode ty)168 static sk_sp<SkShader> make_bm(SkTileMode tx, SkTileMode ty) {
169 SkBitmap bm;
170 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
171 return bm.makeShader(tx, ty, SkSamplingOptions());
172 }
173
make_grad(SkTileMode tx,SkTileMode ty)174 static sk_sp<SkShader> make_grad(SkTileMode tx, SkTileMode ty) {
175 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
176 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
177 SkScalar rad = SkIntToScalar(gWidth)/2;
178 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
179
180 int index = (int)ty;
181 switch (index % 3) {
182 case 0:
183 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors), tx);
184 case 1:
185 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, std::size(colors), tx);
186 case 2:
187 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr,
188 std::size(colors), tx, 135, 225, 0, nullptr);
189 }
190
191 return nullptr;
192 }
193
194 typedef sk_sp<SkShader> (*ShaderProc)(SkTileMode, SkTileMode);
195
196 class ScaledTiling2GM : public skiagm::GM {
197 ShaderProc fProc;
198 const char* fName;
199 public:
ScaledTiling2GM(ShaderProc proc,const char name[])200 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
201
202 private:
getName() const203 SkString getName() const override { return SkString(fName); }
204
getISize()205 SkISize getISize() override { return SkISize::Make(650, 610); }
206
onDraw(SkCanvas * canvas)207 void onDraw(SkCanvas* canvas) override {
208 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
209
210 const SkScalar w = SkIntToScalar(gWidth);
211 const SkScalar h = SkIntToScalar(gHeight);
212 SkRect r = { -w, -h, w*2, h*2 };
213
214 constexpr SkTileMode gModes[] = {
215 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror
216 };
217 const char* gModeNames[] = {
218 "Clamp", "Repeat", "Mirror"
219 };
220
221 SkScalar y = SkIntToScalar(24);
222 SkScalar x = SkIntToScalar(66);
223
224 SkFont font = ToolUtils::DefaultPortableFont();
225
226 for (size_t kx = 0; kx < std::size(gModes); kx++) {
227 SkString str(gModeNames[kx]);
228 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
229 SkTextUtils::kCenter_Align);
230 x += r.width() * 4 / 3;
231 }
232
233 y += SkIntToScalar(16) + h;
234
235 for (size_t ky = 0; ky < std::size(gModes); ky++) {
236 x = SkIntToScalar(16) + w;
237
238 SkString str(gModeNames[ky]);
239 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), SkTextUtils::kRight_Align);
240
241 x += SkIntToScalar(50);
242 for (size_t kx = 0; kx < std::size(gModes); kx++) {
243 SkPaint paint;
244 paint.setShader(fProc(gModes[kx], gModes[ky]));
245
246 canvas->save();
247 canvas->translate(x, y);
248 canvas->drawRect(r, paint);
249 canvas->restore();
250
251 x += r.width() * 4 / 3;
252 }
253 y += r.height() * 4 / 3;
254 }
255 }
256 };
257
258 //////////////////////////////////////////////////////////////////////////////
259
260 DEF_GM( return new ScaledTilingGM(true); )
261 DEF_GM( return new ScaledTilingGM(false); )
262 DEF_GM( return new ScaledTiling2GM(make_bm, "scaled_tilemode_bitmap"); )
263 DEF_GM( return new ScaledTiling2GM(make_grad, "scaled_tilemode_gradient"); )
264