blob: efb6f8537adfbc931b6dea568cce6c8084835bfa [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"
13#include "SkColorPriv.h"
14#include "SkColorFilter.h"
15#include "SkTypeface.h"
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000016#include "SkBlurMask.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000017
18// effects
19#include "SkGradientShader.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000020#include "SkBlurDrawLooper.h"
21
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000022static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
23 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
humper@google.com3aad3b02013-09-04 19:23:53 +000024 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
humper@google.com3aad3b02013-09-04 19:23:53 +000032 paint.setDither(true);
33 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000034 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
humper@google.com3aad3b02013-09-04 19:23:53 +000035 canvas.drawPaint(paint);
36}
37
reed93a12152015-03-16 10:08:34 -070038static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level,
humper@google.com3aad3b02013-09-04 19:23:53 +000039 SkShader::TileMode tmx, SkShader::TileMode tmy) {
40 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
41 paint->setShader(shader)->unref();
reed93a12152015-03-16 10:08:34 -070042 paint->setFilterQuality(filter_level);
humper@google.com3aad3b02013-09-04 19:23:53 +000043}
44
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000045static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000046 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000047 kRGB_565_SkColorType,
humper@google.com3aad3b02013-09-04 19:23:53 +000048};
49
50class ScaledTilingGM : public skiagm::GM {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000051 SkAutoTUnref<SkBlurDrawLooper> fLooper;
humper@google.com3aad3b02013-09-04 19:23:53 +000052public:
53 ScaledTilingGM(bool powerOfTwoSize)
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000054 : fLooper(SkBlurDrawLooper::Create(0x88000000,
55 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
56 SkIntToScalar(2), SkIntToScalar(2)))
humper@google.com3aad3b02013-09-04 19:23:53 +000057 , fPowerOfTwoSize(powerOfTwoSize) {
58 }
59
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000060 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
humper@google.com3aad3b02013-09-04 19:23:53 +000061
62protected:
humper@google.com3aad3b02013-09-04 19:23:53 +000063 enum {
64 kPOTSize = 4,
65 kNPOTSize = 3,
66 };
67
mtklein36352bf2015-03-25 18:17:31 -070068 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000069 SkString name("scaled_tilemodes");
70 if (!fPowerOfTwoSize) {
71 name.append("_npot");
72 }
73 return name;
74 }
75
mtklein36352bf2015-03-25 18:17:31 -070076 SkISize onISize() override { return SkISize::Make(880, 760); }
humper@google.com3aad3b02013-09-04 19:23:53 +000077
mtklein36352bf2015-03-25 18:17:31 -070078 void onOnceBeforeDraw() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000079 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000080 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
81 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +000082 }
83 }
84
mtklein36352bf2015-03-25 18:17:31 -070085 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +000086 float scale = 32.f/kPOTSize;
87
88 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
89
90 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
91
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000092 static const char* gColorTypeNames[] = { "8888" , "565", "4444" };
humper@google.com3aad3b02013-09-04 19:23:53 +000093
reed93a12152015-03-16 10:08:34 -070094 static const SkFilterQuality gFilterQualitys[] =
95 { kNone_SkFilterQuality,
96 kLow_SkFilterQuality,
97 kMedium_SkFilterQuality,
98 kHigh_SkFilterQuality };
99 static const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
humper@google.com3aad3b02013-09-04 19:23:53 +0000100
101 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
102 static const char* gModeNames[] = { "C", "R", "M" };
103
104 SkScalar y = SkIntToScalar(24);
105 SkScalar x = SkIntToScalar(10)/scale;
106
107 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
108 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
109 SkPaint p;
110 SkString str;
111 p.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400112 sk_tool_utils::set_portable_typeface(&p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000113 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000114 p.setLooper(fLooper);
humper@google.com3aad3b02013-09-04 19:23:53 +0000115 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
116
117 p.setTextAlign(SkPaint::kCenter_Align);
118 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p);
119
120 x += r.width() * 4 / 3;
121 }
122 }
123
124 y = SkIntToScalar(40) / scale;
125
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000126 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed93a12152015-03-16 10:08:34 -0700127 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000128 x = SkIntToScalar(10)/scale;
129 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
130 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
131 SkPaint paint;
132#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
133 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
134 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000135 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +0000136 }
137#endif
reed93a12152015-03-16 10:08:34 -0700138 setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000139 paint.setDither(true);
140
141 canvas->save();
142 canvas->scale(scale,scale);
143 canvas->translate(x, y);
144 canvas->drawRect(r, paint);
145 canvas->restore();
146
147 x += r.width() * 4 / 3;
148 }
149 }
150 {
151 SkPaint p;
152 SkString str;
153 p.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400154 sk_tool_utils::set_portable_typeface(&p);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000155 p.setLooper(fLooper);
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000156 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000157 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p);
158 }
159
160 y += r.height() * 4 / 3;
161 }
162 }
163 }
164
165private:
166 bool fPowerOfTwoSize;
167 typedef skiagm::GM INHERITED;
168};
169
170static const int gWidth = 32;
171static const int gHeight = 32;
172
173static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
174 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000175 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
humper@google.com3aad3b02013-09-04 19:23:53 +0000176 return SkShader::CreateBitmapShader(bm, tx, ty);
177}
178
179static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
180 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
181 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
182 SkScalar rad = SkIntToScalar(gWidth)/2;
183 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
184
185 int index = (int)ty;
186 switch (index % 3) {
187 case 0:
188 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
189 case 1:
190 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
191 case 2:
192 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
193 }
194
195 return NULL;
196}
197
198typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
199
200class ScaledTiling2GM : public skiagm::GM {
201 ShaderProc fProc;
202 SkString fName;
203public:
204 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
205 fName.printf("scaled_tilemode_%s", name);
206 }
207
208protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000209
mtklein36352bf2015-03-25 18:17:31 -0700210 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000211 return fName;
212 }
213
mtklein36352bf2015-03-25 18:17:31 -0700214 SkISize onISize() override { return SkISize::Make(880, 560); }
humper@google.com3aad3b02013-09-04 19:23:53 +0000215
mtklein36352bf2015-03-25 18:17:31 -0700216 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000217 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
218
219 const SkScalar w = SkIntToScalar(gWidth);
220 const SkScalar h = SkIntToScalar(gHeight);
221 SkRect r = { -w, -h, w*2, h*2 };
222
223 static const SkShader::TileMode gModes[] = {
224 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
225 };
226 static const char* gModeNames[] = {
227 "Clamp", "Repeat", "Mirror"
228 };
229
230 SkScalar y = SkIntToScalar(24);
231 SkScalar x = SkIntToScalar(66);
232
233 SkPaint p;
234 p.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400235 sk_tool_utils::set_portable_typeface(&p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000236 p.setTextAlign(SkPaint::kCenter_Align);
237
238 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
239 SkString str(gModeNames[kx]);
240 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
241 x += r.width() * 4 / 3;
242 }
243
244 y += SkIntToScalar(16) + h;
245 p.setTextAlign(SkPaint::kRight_Align);
246
247 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
248 x = SkIntToScalar(16) + w;
249
250 SkString str(gModeNames[ky]);
251 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
252
253 x += SkIntToScalar(50);
254 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
255 SkPaint paint;
256 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
257
258 canvas->save();
259 canvas->translate(x, y);
260 canvas->drawRect(r, paint);
261 canvas->restore();
262
263 x += r.width() * 4 / 3;
264 }
265 y += r.height() * 4 / 3;
266 }
267 }
268
269private:
270 typedef skiagm::GM INHERITED;
271};
272
273//////////////////////////////////////////////////////////////////////////////
274
275DEF_GM( return new ScaledTilingGM(true); )
276DEF_GM( return new ScaledTilingGM(false); )
277DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
278DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )