Debian Codegraph @

gm/tilemodes_scaled.cpp

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