blob: 603a78fb1e958ba1ca4ff2cb0ac7baf09ad262f9 [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) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000023 bm->allocConfigPixels(config, w, h);
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
32 SkUnitMapper* um = NULL;
33
34 um = new SkCosineMapper;
35// um = new SkDiscreteMapper(12);
36
37 SkAutoUnref au(um);
38
39 paint.setDither(true);
40 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
41 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
42 canvas.drawPaint(paint);
43}
44
45static void setup(SkPaint* paint, const SkBitmap& bm, SkPaint::FilterLevel filter_level,
46 SkShader::TileMode tmx, SkShader::TileMode tmy) {
47 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
48 paint->setShader(shader)->unref();
49 paint->setFilterLevel(filter_level);
50}
51
52static const SkBitmap::Config gConfigs[] = {
53 SkBitmap::kARGB_8888_Config,
54 SkBitmap::kRGB_565_Config,
55};
56
57class ScaledTilingGM : public skiagm::GM {
58 SkBlurDrawLooper fLooper;
59public:
60 ScaledTilingGM(bool powerOfTwoSize)
61 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), 0x88000000)
62 , fPowerOfTwoSize(powerOfTwoSize) {
63 }
64
65 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
66
67protected:
68
69 enum {
70 kPOTSize = 4,
71 kNPOTSize = 3,
72 };
73
74 SkString onShortName() {
75 SkString name("scaled_tilemodes");
76 if (!fPowerOfTwoSize) {
77 name.append("_npot");
78 }
79 return name;
80 }
81
82 SkISize onISize() { return SkISize::Make(880, 760); }
83
84 virtual void onOnceBeforeDraw() SK_OVERRIDE {
85 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
86 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
87 makebm(&fTexture[i], gConfigs[i], size, size);
88 }
89 }
90
91 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +000092
humper@google.com3aad3b02013-09-04 19:23:53 +000093 float scale = 32.f/kPOTSize;
94
95 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
96
97 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
98
99 static const char* gConfigNames[] = { "8888" , "565", "4444" };
100
skia.committer@gmail.comc3723db2013-09-05 07:01:19 +0000101 static const SkPaint::FilterLevel gFilterLevels[] =
102 { SkPaint::kNone_FilterLevel,
humper@google.com3aad3b02013-09-04 19:23:53 +0000103 SkPaint::kLow_FilterLevel,
104 SkPaint::kMedium_FilterLevel,
105 SkPaint::kHigh_FilterLevel };
106 static const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
107
108 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
109 static const char* gModeNames[] = { "C", "R", "M" };
110
111 SkScalar y = SkIntToScalar(24);
112 SkScalar x = SkIntToScalar(10)/scale;
113
114 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
115 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
116 SkPaint p;
117 SkString str;
118 p.setAntiAlias(true);
119 p.setDither(true);
120 p.setLooper(&fLooper);
121 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
122
123 p.setTextAlign(SkPaint::kCenter_Align);
124 canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p);
125
126 x += r.width() * 4 / 3;
127 }
128 }
129
130 y = SkIntToScalar(40) / scale;
131
132 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
133 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterLevels); j++) {
134 x = SkIntToScalar(10)/scale;
135 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
136 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
137 SkPaint paint;
138#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
139 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
140 if (!fPowerOfTwoSize) {
141 makebm(&fTexture[i], gConfigs[i], size, size);
142 }
143#endif
144 setup(&paint, fTexture[i], gFilterLevels[j], gModes[kx], gModes[ky]);
145 paint.setDither(true);
146
147 canvas->save();
148 canvas->scale(scale,scale);
149 canvas->translate(x, y);
150 canvas->drawRect(r, paint);
151 canvas->restore();
152
153 x += r.width() * 4 / 3;
154 }
155 }
156 {
157 SkPaint p;
158 SkString str;
159 p.setAntiAlias(true);
160 p.setLooper(&fLooper);
161 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
162 canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p);
163 }
164
165 y += r.height() * 4 / 3;
166 }
167 }
168 }
169
170private:
171 bool fPowerOfTwoSize;
172 typedef skiagm::GM INHERITED;
173};
174
175static const int gWidth = 32;
176static const int gHeight = 32;
177
178static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
179 SkBitmap bm;
180 makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight);
181 return SkShader::CreateBitmapShader(bm, tx, ty);
182}
183
184static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
185 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
186 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
187 SkScalar rad = SkIntToScalar(gWidth)/2;
188 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
189
190 int index = (int)ty;
191 switch (index % 3) {
192 case 0:
193 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
194 case 1:
195 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
196 case 2:
197 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
198 }
199
200 return NULL;
201}
202
203typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
204
205class ScaledTiling2GM : public skiagm::GM {
206 ShaderProc fProc;
207 SkString fName;
208public:
209 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
210 fName.printf("scaled_tilemode_%s", name);
211 }
212
213protected:
214 SkString onShortName() {
215 return fName;
216 }
217
218 SkISize onISize() { return SkISize::Make(880, 560); }
219
220 virtual void onDraw(SkCanvas* canvas) {
221 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
222
223 const SkScalar w = SkIntToScalar(gWidth);
224 const SkScalar h = SkIntToScalar(gHeight);
225 SkRect r = { -w, -h, w*2, h*2 };
226
227 static const SkShader::TileMode gModes[] = {
228 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
229 };
230 static const char* gModeNames[] = {
231 "Clamp", "Repeat", "Mirror"
232 };
233
234 SkScalar y = SkIntToScalar(24);
235 SkScalar x = SkIntToScalar(66);
236
237 SkPaint p;
238 p.setAntiAlias(true);
239 p.setTextAlign(SkPaint::kCenter_Align);
240
241 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
242 SkString str(gModeNames[kx]);
243 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
244 x += r.width() * 4 / 3;
245 }
246
247 y += SkIntToScalar(16) + h;
248 p.setTextAlign(SkPaint::kRight_Align);
249
250 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
251 x = SkIntToScalar(16) + w;
252
253 SkString str(gModeNames[ky]);
254 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
255
256 x += SkIntToScalar(50);
257 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
258 SkPaint paint;
259 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
260
261 canvas->save();
262 canvas->translate(x, y);
263 canvas->drawRect(r, paint);
264 canvas->restore();
265
266 x += r.width() * 4 / 3;
267 }
268 y += r.height() * 4 / 3;
269 }
270 }
271
272private:
273 typedef skiagm::GM INHERITED;
274};
275
276//////////////////////////////////////////////////////////////////////////////
277
278DEF_GM( return new ScaledTilingGM(true); )
279DEF_GM( return new ScaledTilingGM(false); )
280DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
281DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )