blob: 88a070afb5b627d16a1afd7aab70cdce8bbf0093 [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.org4b8f8022014-05-21 19:56:46 +000034 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
humper@google.com3aad3b02013-09-04 19:23:53 +000035 canvas.drawPaint(paint);
36}
37
38static void setup(SkPaint* paint, const SkBitmap& bm, SkPaint::FilterLevel filter_level,
39 SkShader::TileMode tmx, SkShader::TileMode tmy) {
40 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
41 paint->setShader(shader)->unref();
42 paint->setFilterLevel(filter_level);
43}
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
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000068 virtual uint32_t onGetFlags() const SK_OVERRIDE {
69 if (!fPowerOfTwoSize) {
70 return kSkipTiled_Flag; // Only for 565. 8888 is fine.
71 }
72 return 0;
73 }
74
humper@google.com3aad3b02013-09-04 19:23:53 +000075 SkString onShortName() {
76 SkString name("scaled_tilemodes");
77 if (!fPowerOfTwoSize) {
78 name.append("_npot");
79 }
80 return name;
81 }
82
83 SkISize onISize() { return SkISize::Make(880, 760); }
84
85 virtual void onOnceBeforeDraw() SK_OVERRIDE {
86 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000087 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
88 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +000089 }
90 }
91
92 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +000093
humper@google.com3aad3b02013-09-04 19:23:53 +000094 float scale = 32.f/kPOTSize;
95
96 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
97
98 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
99
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000100 static const char* gColorTypeNames[] = { "8888" , "565", "4444" };
humper@google.com3aad3b02013-09-04 19:23:53 +0000101
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +0000102 static const SkPaint::FilterLevel gFilterLevels[] =
103 { SkPaint::kNone_FilterLevel,
humper@google.com3aad3b02013-09-04 19:23:53 +0000104 SkPaint::kLow_FilterLevel,
105 SkPaint::kMedium_FilterLevel,
106 SkPaint::kHigh_FilterLevel };
107 static const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
108
109 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
110 static const char* gModeNames[] = { "C", "R", "M" };
111
112 SkScalar y = SkIntToScalar(24);
113 SkScalar x = SkIntToScalar(10)/scale;
114
115 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
116 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
117 SkPaint p;
118 SkString str;
119 p.setAntiAlias(true);
120 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000121 p.setLooper(fLooper);
humper@google.com3aad3b02013-09-04 19:23:53 +0000122 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
123
124 p.setTextAlign(SkPaint::kCenter_Align);
125 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p);
126
127 x += r.width() * 4 / 3;
128 }
129 }
130
131 y = SkIntToScalar(40) / scale;
132
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000133 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000134 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterLevels); j++) {
135 x = SkIntToScalar(10)/scale;
136 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
137 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
138 SkPaint paint;
139#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
140 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
141 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000142 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +0000143 }
144#endif
145 setup(&paint, fTexture[i], gFilterLevels[j], gModes[kx], gModes[ky]);
146 paint.setDither(true);
147
148 canvas->save();
149 canvas->scale(scale,scale);
150 canvas->translate(x, y);
151 canvas->drawRect(r, paint);
152 canvas->restore();
153
154 x += r.width() * 4 / 3;
155 }
156 }
157 {
158 SkPaint p;
159 SkString str;
160 p.setAntiAlias(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000161 p.setLooper(fLooper);
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000162 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000163 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p);
164 }
165
166 y += r.height() * 4 / 3;
167 }
168 }
169 }
170
171private:
172 bool fPowerOfTwoSize;
173 typedef skiagm::GM INHERITED;
174};
175
176static const int gWidth = 32;
177static const int gHeight = 32;
178
179static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
180 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000181 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
humper@google.com3aad3b02013-09-04 19:23:53 +0000182 return SkShader::CreateBitmapShader(bm, tx, ty);
183}
184
185static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
186 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
187 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
188 SkScalar rad = SkIntToScalar(gWidth)/2;
189 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
190
191 int index = (int)ty;
192 switch (index % 3) {
193 case 0:
194 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
195 case 1:
196 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
197 case 2:
198 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
199 }
200
201 return NULL;
202}
203
204typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
205
206class ScaledTiling2GM : public skiagm::GM {
207 ShaderProc fProc;
208 SkString fName;
209public:
210 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
211 fName.printf("scaled_tilemode_%s", name);
212 }
213
214protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000215 virtual uint32_t onGetFlags() const SK_OVERRIDE {
216 return kSkipTiled_Flag;
217 }
218
humper@google.com3aad3b02013-09-04 19:23:53 +0000219 SkString onShortName() {
220 return fName;
221 }
222
223 SkISize onISize() { return SkISize::Make(880, 560); }
224
225 virtual void onDraw(SkCanvas* canvas) {
226 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
227
228 const SkScalar w = SkIntToScalar(gWidth);
229 const SkScalar h = SkIntToScalar(gHeight);
230 SkRect r = { -w, -h, w*2, h*2 };
231
232 static const SkShader::TileMode gModes[] = {
233 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
234 };
235 static const char* gModeNames[] = {
236 "Clamp", "Repeat", "Mirror"
237 };
238
239 SkScalar y = SkIntToScalar(24);
240 SkScalar x = SkIntToScalar(66);
241
242 SkPaint p;
243 p.setAntiAlias(true);
244 p.setTextAlign(SkPaint::kCenter_Align);
245
246 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
247 SkString str(gModeNames[kx]);
248 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
249 x += r.width() * 4 / 3;
250 }
251
252 y += SkIntToScalar(16) + h;
253 p.setTextAlign(SkPaint::kRight_Align);
254
255 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
256 x = SkIntToScalar(16) + w;
257
258 SkString str(gModeNames[ky]);
259 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
260
261 x += SkIntToScalar(50);
262 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
263 SkPaint paint;
264 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
265
266 canvas->save();
267 canvas->translate(x, y);
268 canvas->drawRect(r, paint);
269 canvas->restore();
270
271 x += r.width() * 4 / 3;
272 }
273 y += r.height() * 4 / 3;
274 }
275 }
276
277private:
278 typedef skiagm::GM INHERITED;
279};
280
281//////////////////////////////////////////////////////////////////////////////
282
283DEF_GM( return new ScaledTilingGM(true); )
284DEF_GM( return new ScaledTilingGM(false); )
285DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
286DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )