blob: 3d86bc4cd924df482b6158c5de0cbf3fbc9e3bda [file] [log] [blame]
humper@google.com3aad3b02013-09-04 19:23:53 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "gm.h"
9#include "SkPath.h"
10#include "SkRegion.h"
11#include "SkShader.h"
12#include "SkUtils.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000013#include "SkColorFilter.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000014
15// effects
16#include "SkGradientShader.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000017#include "SkBlurDrawLooper.h"
18
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000019static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
20 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
humper@google.com3aad3b02013-09-04 19:23:53 +000021 bm->eraseColor(SK_ColorTRANSPARENT);
22
23 SkCanvas canvas(*bm);
24 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
25 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
26 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
27 SkPaint paint;
28
humper@google.com3aad3b02013-09-04 19:23:53 +000029 paint.setDither(true);
30 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000031 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
humper@google.com3aad3b02013-09-04 19:23:53 +000032 canvas.drawPaint(paint);
33}
34
reed93a12152015-03-16 10:08:34 -070035static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level,
humper@google.com3aad3b02013-09-04 19:23:53 +000036 SkShader::TileMode tmx, SkShader::TileMode tmy) {
37 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
38 paint->setShader(shader)->unref();
reed93a12152015-03-16 10:08:34 -070039 paint->setFilterQuality(filter_level);
humper@google.com3aad3b02013-09-04 19:23:53 +000040}
41
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000042static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000043 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000044 kRGB_565_SkColorType,
humper@google.com3aad3b02013-09-04 19:23:53 +000045};
46
47class ScaledTilingGM : public skiagm::GM {
humper@google.com3aad3b02013-09-04 19:23:53 +000048public:
49 ScaledTilingGM(bool powerOfTwoSize)
caryclark7aa18992015-07-13 12:08:45 -070050 : fPowerOfTwoSize(powerOfTwoSize) {
humper@google.com3aad3b02013-09-04 19:23:53 +000051 }
52
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000053 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
humper@google.com3aad3b02013-09-04 19:23:53 +000054
55protected:
humper@google.com3aad3b02013-09-04 19:23:53 +000056 enum {
57 kPOTSize = 4,
58 kNPOTSize = 3,
59 };
60
mtklein36352bf2015-03-25 18:17:31 -070061 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000062 SkString name("scaled_tilemodes");
63 if (!fPowerOfTwoSize) {
64 name.append("_npot");
65 }
66 return name;
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 SkISize onISize() override { return SkISize::Make(880, 760); }
humper@google.com3aad3b02013-09-04 19:23:53 +000070
mtklein36352bf2015-03-25 18:17:31 -070071 void onOnceBeforeDraw() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000072 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000073 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
74 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +000075 }
76 }
77
mtklein36352bf2015-03-25 18:17:31 -070078 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +000079 float scale = 32.f/kPOTSize;
80
81 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
82
83 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
84
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000085 static const char* gColorTypeNames[] = { "8888" , "565", "4444" };
humper@google.com3aad3b02013-09-04 19:23:53 +000086
reed93a12152015-03-16 10:08:34 -070087 static const SkFilterQuality gFilterQualitys[] =
88 { kNone_SkFilterQuality,
89 kLow_SkFilterQuality,
90 kMedium_SkFilterQuality,
91 kHigh_SkFilterQuality };
92 static const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
humper@google.com3aad3b02013-09-04 19:23:53 +000093
94 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
95 static const char* gModeNames[] = { "C", "R", "M" };
96
97 SkScalar y = SkIntToScalar(24);
98 SkScalar x = SkIntToScalar(10)/scale;
99
100 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
101 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
102 SkPaint p;
103 SkString str;
104 p.setAntiAlias(true);
caryclark7aa18992015-07-13 12:08:45 -0700105 sk_tool_utils::set_portable_typeface_always(&p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000106 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
107
108 p.setTextAlign(SkPaint::kCenter_Align);
109 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p);
110
111 x += r.width() * 4 / 3;
112 }
113 }
114
115 y = SkIntToScalar(40) / scale;
116
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000117 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed93a12152015-03-16 10:08:34 -0700118 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000119 x = SkIntToScalar(10)/scale;
120 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
121 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
122 SkPaint paint;
123#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
124 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
125 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000126 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +0000127 }
128#endif
reed93a12152015-03-16 10:08:34 -0700129 setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000130 paint.setDither(true);
131
132 canvas->save();
133 canvas->scale(scale,scale);
134 canvas->translate(x, y);
135 canvas->drawRect(r, paint);
136 canvas->restore();
137
138 x += r.width() * 4 / 3;
139 }
140 }
141 {
142 SkPaint p;
143 SkString str;
144 p.setAntiAlias(true);
caryclark7aa18992015-07-13 12:08:45 -0700145 sk_tool_utils::set_portable_typeface_always(&p);
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000146 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000147 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p);
148 }
149
150 y += r.height() * 4 / 3;
151 }
152 }
153 }
154
155private:
156 bool fPowerOfTwoSize;
157 typedef skiagm::GM INHERITED;
158};
159
160static const int gWidth = 32;
161static const int gHeight = 32;
162
163static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
164 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000165 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
humper@google.com3aad3b02013-09-04 19:23:53 +0000166 return SkShader::CreateBitmapShader(bm, tx, ty);
167}
168
169static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
170 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
171 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
172 SkScalar rad = SkIntToScalar(gWidth)/2;
caryclark7aa18992015-07-13 12:08:45 -0700173 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
humper@google.com3aad3b02013-09-04 19:23:53 +0000174
175 int index = (int)ty;
176 switch (index % 3) {
177 case 0:
178 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
179 case 1:
180 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
181 case 2:
182 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
183 }
184
185 return NULL;
186}
187
188typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
189
190class ScaledTiling2GM : public skiagm::GM {
191 ShaderProc fProc;
192 SkString fName;
193public:
194 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
195 fName.printf("scaled_tilemode_%s", name);
196 }
197
198protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000199
mtklein36352bf2015-03-25 18:17:31 -0700200 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000201 return fName;
202 }
203
mtklein36352bf2015-03-25 18:17:31 -0700204 SkISize onISize() override { return SkISize::Make(880, 560); }
humper@google.com3aad3b02013-09-04 19:23:53 +0000205
mtklein36352bf2015-03-25 18:17:31 -0700206 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000207 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 static const SkShader::TileMode gModes[] = {
214 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
215 };
216 static const char* gModeNames[] = {
217 "Clamp", "Repeat", "Mirror"
218 };
219
220 SkScalar y = SkIntToScalar(24);
221 SkScalar x = SkIntToScalar(66);
222
223 SkPaint p;
224 p.setAntiAlias(true);
caryclark7aa18992015-07-13 12:08:45 -0700225 sk_tool_utils::set_portable_typeface_always(&p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000226 p.setTextAlign(SkPaint::kCenter_Align);
227
228 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
229 SkString str(gModeNames[kx]);
230 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
231 x += r.width() * 4 / 3;
232 }
233
234 y += SkIntToScalar(16) + h;
235 p.setTextAlign(SkPaint::kRight_Align);
236
237 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
238 x = SkIntToScalar(16) + w;
239
240 SkString str(gModeNames[ky]);
241 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
242
243 x += SkIntToScalar(50);
244 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
245 SkPaint paint;
246 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
247
248 canvas->save();
249 canvas->translate(x, y);
250 canvas->drawRect(r, paint);
251 canvas->restore();
252
253 x += r.width() * 4 / 3;
254 }
255 y += r.height() * 4 / 3;
256 }
257 }
258
259private:
260 typedef skiagm::GM INHERITED;
261};
262
263//////////////////////////////////////////////////////////////////////////////
264
265DEF_GM( return new ScaledTilingGM(true); )
266DEF_GM( return new ScaledTilingGM(false); )
267DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
268DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )