blob: 87bd1dd57b859e35a32150037141a6f69a979c57 [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"
16
17// effects
18#include "SkGradientShader.h"
19#include "SkUnitMappers.h"
20#include "SkBlurDrawLooper.h"
21
22static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
23 bm->setConfig(config, w, h);
24 bm->allocPixels();
25 bm->eraseColor(SK_ColorTRANSPARENT);
26
27 SkCanvas canvas(*bm);
28 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
29 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
30 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
31 SkPaint paint;
32
33 SkUnitMapper* um = NULL;
34
35 um = new SkCosineMapper;
36// um = new SkDiscreteMapper(12);
37
38 SkAutoUnref au(um);
39
40 paint.setDither(true);
41 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
42 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
43 canvas.drawPaint(paint);
44}
45
46static void setup(SkPaint* paint, const SkBitmap& bm, SkPaint::FilterLevel filter_level,
47 SkShader::TileMode tmx, SkShader::TileMode tmy) {
48 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
49 paint->setShader(shader)->unref();
50 paint->setFilterLevel(filter_level);
51}
52
53static const SkBitmap::Config gConfigs[] = {
54 SkBitmap::kARGB_8888_Config,
55 SkBitmap::kRGB_565_Config,
56};
57
58class ScaledTilingGM : public skiagm::GM {
59 SkBlurDrawLooper fLooper;
60public:
61 ScaledTilingGM(bool powerOfTwoSize)
62 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), 0x88000000)
63 , fPowerOfTwoSize(powerOfTwoSize) {
64 }
65
66 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
67
68protected:
69
70 enum {
71 kPOTSize = 4,
72 kNPOTSize = 3,
73 };
74
75 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;
87 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
88 makebm(&fTexture[i], gConfigs[i], size, size);
89 }
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
100 static const char* gConfigNames[] = { "8888" , "565", "4444" };
101
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);
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(gConfigs); 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], gConfigs[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", gConfigNames[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
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;
181 makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight);
182 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:
215 SkString onShortName() {
216 return fName;
217 }
218
219 SkISize onISize() { return SkISize::Make(880, 560); }
220
221 virtual void onDraw(SkCanvas* canvas) {
222 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
223
224 const SkScalar w = SkIntToScalar(gWidth);
225 const SkScalar h = SkIntToScalar(gHeight);
226 SkRect r = { -w, -h, w*2, h*2 };
227
228 static const SkShader::TileMode gModes[] = {
229 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
230 };
231 static const char* gModeNames[] = {
232 "Clamp", "Repeat", "Mirror"
233 };
234
235 SkScalar y = SkIntToScalar(24);
236 SkScalar x = SkIntToScalar(66);
237
238 SkPaint p;
239 p.setAntiAlias(true);
240 p.setTextAlign(SkPaint::kCenter_Align);
241
242 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
243 SkString str(gModeNames[kx]);
244 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
245 x += r.width() * 4 / 3;
246 }
247
248 y += SkIntToScalar(16) + h;
249 p.setTextAlign(SkPaint::kRight_Align);
250
251 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
252 x = SkIntToScalar(16) + w;
253
254 SkString str(gModeNames[ky]);
255 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
256
257 x += SkIntToScalar(50);
258 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
259 SkPaint paint;
260 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
261
262 canvas->save();
263 canvas->translate(x, y);
264 canvas->drawRect(r, paint);
265 canvas->restore();
266
267 x += r.width() * 4 / 3;
268 }
269 y += r.height() * 4 / 3;
270 }
271 }
272
273private:
274 typedef skiagm::GM INHERITED;
275};
276
277//////////////////////////////////////////////////////////////////////////////
278
279DEF_GM( return new ScaledTilingGM(true); )
280DEF_GM( return new ScaledTilingGM(false); )
281DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
282DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )