blob: 674576965a19fdf76304556574908f5687ac6613 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@android.com1a2fec52009-06-22 02:17:34 +00008#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
reed@android.com1a2fec52009-06-22 02:17:34 +000022static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
23 bm->setConfig(config, w, h);
24 bm->allocPixels();
25 bm->eraseColor(0);
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com1a2fec52009-06-22 02:17:34 +000027 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +000032
33 SkUnitMapper* um = NULL;
reed@android.com1a2fec52009-06-22 02:17:34 +000034
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, bool filter,
47 SkShader::TileMode tmx, SkShader::TileMode tmy) {
48 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
49 paint->setShader(shader)->unref();
50 paint->setFilterBitmap(filter);
51}
52
53static const SkBitmap::Config gConfigs[] = {
54 SkBitmap::kARGB_8888_Config,
55 SkBitmap::kRGB_565_Config,
56 SkBitmap::kARGB_4444_Config
57};
58static const int gWidth = 32;
59static const int gHeight = 32;
60
mike@reedtribe.orga0591692012-10-18 02:01:59 +000061class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000062 SkBlurDrawLooper fLooper;
63public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000064 TilingGM()
reed@android.com1a2fec52009-06-22 02:17:34 +000065 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
66 0x88000000) {
67 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
68 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
69 }
70 }
71
72 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.com1a2fec52009-06-22 02:17:34 +000074protected:
75 SkString onShortName() {
76 return SkString("tilemodes");
77 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
mike@reedtribe.orga0591692012-10-18 02:01:59 +000079 SkISize onISize() { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@android.com1a2fec52009-06-22 02:17:34 +000081 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@android.com1a2fec52009-06-22 02:17:34 +000083 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
84
85 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@android.com1a2fec52009-06-22 02:17:34 +000087 static const bool gFilters[] = { false, true };
88 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com1a2fec52009-06-22 02:17:34 +000090 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
91 static const char* gModeNames[] = { "C", "R", "M" };
92
93 SkScalar y = SkIntToScalar(24);
94 SkScalar x = SkIntToScalar(10);
95
96 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
97 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
98 SkPaint p;
99 SkString str;
100 p.setAntiAlias(true);
101 p.setDither(true);
102 p.setLooper(&fLooper);
103 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
104
105 p.setTextAlign(SkPaint::kCenter_Align);
106 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@android.com1a2fec52009-06-22 02:17:34 +0000108 x += r.width() * 4 / 3;
109 }
110 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
reed@android.com1a2fec52009-06-22 02:17:34 +0000112 y += SkIntToScalar(16);
113
114 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
115 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
116 x = SkIntToScalar(10);
117 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
118 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
119 SkPaint paint;
120 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
121 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com1a2fec52009-06-22 02:17:34 +0000123 canvas->save();
124 canvas->translate(x, y);
125 canvas->drawRect(r, paint);
126 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@android.com1a2fec52009-06-22 02:17:34 +0000128 x += r.width() * 4 / 3;
129 }
130 }
131 {
132 SkPaint p;
133 SkString str;
134 p.setAntiAlias(true);
135 p.setLooper(&fLooper);
136 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
137 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
138 }
139
140 y += r.height() * 4 / 3;
141 }
142 }
143 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com1a2fec52009-06-22 02:17:34 +0000145private:
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000146 typedef skiagm::GM INHERITED;
147};
148
149static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
150 SkBitmap bm;
151 makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight);
152 return SkShader::CreateBitmapShader(bm, tx, ty);
153}
154
155static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
156 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
157 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
158 SkScalar rad = SkIntToScalar(gWidth)/2;
159 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
160
161 int index = (int)ty;
162 switch (index % 3) {
163 case 0:
164 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
165 case 1:
166 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
167 case 2:
168 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
169 }
170}
171
172static SkShader* make_radial(SkShader::TileMode tx, SkShader::TileMode ty) {
173 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
174 SkScalar rad = SkIntToScalar(gWidth)/2;
175 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000176
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000177 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
178}
179
180typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
181
182class Tiling2GM : public skiagm::GM {
183 ShaderProc fProc;
184 SkString fName;
185public:
186 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
187 fName.printf("tilemode_%s", name);
188 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000189
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000190protected:
191 SkString onShortName() {
192 return fName;
193 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000194
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000195 SkISize onISize() { return SkISize::Make(880, 560); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000196
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000197 virtual void onDraw(SkCanvas* canvas) {
198 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
199
200 const SkScalar w = SkIntToScalar(gWidth);
201 const SkScalar h = SkIntToScalar(gHeight);
202 SkRect r = { -w, -h, w*2, h*2 };
203
204 static const SkShader::TileMode gModes[] = {
205 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
206 };
207 static const char* gModeNames[] = {
208 "Clamp", "Repeat", "Mirror"
209 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000210
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000211 SkScalar y = SkIntToScalar(24);
212 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000213
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000214 SkPaint p;
215 p.setAntiAlias(true);
216 p.setTextAlign(SkPaint::kCenter_Align);
217
218 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
219 SkString str(gModeNames[kx]);
220 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
221 x += r.width() * 4 / 3;
222 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000223
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000224 y += SkIntToScalar(16) + h;
225 p.setTextAlign(SkPaint::kRight_Align);
226
227 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
228 x = SkIntToScalar(16) + w;
229
230 SkString str(gModeNames[ky]);
231 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
232
233 x += SkIntToScalar(50);
234 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
235 SkPaint paint;
236 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000237
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000238 canvas->save();
239 canvas->translate(x, y);
240 canvas->drawRect(r, paint);
241 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000242
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000243 x += r.width() * 4 / 3;
244 }
245 y += r.height() * 4 / 3;
246 }
247 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000248
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000249private:
250 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000251};
252
253//////////////////////////////////////////////////////////////////////////////
254
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000255static skiagm::GM* MyFactory(void*) { return new TilingGM; }
256static skiagm::GMRegistry reg(MyFactory);
reed@android.com1a2fec52009-06-22 02:17:34 +0000257
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000258static skiagm::GM* MyFactory2(void*) { return new Tiling2GM(make_bm, "bitmap"); }
259static skiagm::GMRegistry reg2(MyFactory2);
260
261static skiagm::GM* MyFactory3(void*) { return new Tiling2GM(make_grad, "gradient"); }
262static skiagm::GMRegistry reg3(MyFactory3);
263
reed@android.com1a2fec52009-06-22 02:17:34 +0000264