1
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"
16 #include "SkBlurMask.h"
17
18 // effects
19 #include "SkGradientShader.h"
20 #include "SkBlurDrawLooper.h"
21
makebm(SkBitmap * bm,SkColorType ct,int w,int h)22 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
23 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
24 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
32 paint.setDither(true);
33 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
34 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
35 canvas.drawPaint(paint);
36 }
37
setup(SkPaint * paint,const SkBitmap & bm,SkPaint::FilterLevel filter_level,SkShader::TileMode tmx,SkShader::TileMode tmy)38 static 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
45 static const SkColorType gColorTypes[] = {
46 kN32_SkColorType,
47 kRGB_565_SkColorType,
48 };
49
50 class ScaledTilingGM : public skiagm::GM {
51 SkAutoTUnref<SkBlurDrawLooper> fLooper;
52 public:
ScaledTilingGM(bool powerOfTwoSize)53 ScaledTilingGM(bool powerOfTwoSize)
54 : fLooper(SkBlurDrawLooper::Create(0x88000000,
55 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
56 SkIntToScalar(2), SkIntToScalar(2)))
57 , fPowerOfTwoSize(powerOfTwoSize) {
58 }
59
60 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
61
62 protected:
63 enum {
64 kPOTSize = 4,
65 kNPOTSize = 3,
66 };
67
onGetFlags() const68 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
onShortName()75 SkString onShortName() {
76 SkString name("scaled_tilemodes");
77 if (!fPowerOfTwoSize) {
78 name.append("_npot");
79 }
80 return name;
81 }
82
onISize()83 SkISize onISize() { return SkISize::Make(880, 760); }
84
onOnceBeforeDraw()85 virtual void onOnceBeforeDraw() SK_OVERRIDE {
86 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
87 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
88 makebm(&fTexture[i], gColorTypes[i], size, size);
89 }
90 }
91
onDraw(SkCanvas * canvas)92 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
93
94 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
100 static const char* gColorTypeNames[] = { "8888" , "565", "4444" };
101
102 static const SkPaint::FilterLevel gFilterLevels[] =
103 { SkPaint::kNone_FilterLevel,
104 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);
121 p.setLooper(fLooper);
122 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
133 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
134 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) {
142 makebm(&fTexture[i], gColorTypes[i], size, size);
143 }
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);
161 p.setLooper(fLooper);
162 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
163 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
171 private:
172 bool fPowerOfTwoSize;
173 typedef skiagm::GM INHERITED;
174 };
175
176 static const int gWidth = 32;
177 static const int gHeight = 32;
178
make_bm(SkShader::TileMode tx,SkShader::TileMode ty)179 static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
180 SkBitmap bm;
181 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
182 return SkShader::CreateBitmapShader(bm, tx, ty);
183 }
184
make_grad(SkShader::TileMode tx,SkShader::TileMode ty)185 static 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
204 typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
205
206 class ScaledTiling2GM : public skiagm::GM {
207 ShaderProc fProc;
208 SkString fName;
209 public:
ScaledTiling2GM(ShaderProc proc,const char name[])210 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
211 fName.printf("scaled_tilemode_%s", name);
212 }
213
214 protected:
onGetFlags() const215 virtual uint32_t onGetFlags() const SK_OVERRIDE {
216 return kSkipTiled_Flag;
217 }
218
onShortName()219 SkString onShortName() {
220 return fName;
221 }
222
onISize()223 SkISize onISize() { return SkISize::Make(880, 560); }
224
onDraw(SkCanvas * canvas)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
277 private:
278 typedef skiagm::GM INHERITED;
279 };
280
281 //////////////////////////////////////////////////////////////////////////////
282
283 DEF_GM( return new ScaledTilingGM(true); )
284 DEF_GM( return new ScaledTilingGM(false); )
285 DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
286 DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )
287