blob: 5098a836e0f3a7c64e6a66004f730ec755ef0788 [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();
junov@google.comdbfac8a2012-12-06 21:47:40 +000025 bm->eraseColor(SK_ColorTRANSPARENT);
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,
reed@android.com1a2fec52009-06-22 02:17:34 +000056};
reed@android.com1a2fec52009-06-22 02:17:34 +000057
mike@reedtribe.orga0591692012-10-18 02:01:59 +000058class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000059 SkBlurDrawLooper fLooper;
60public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000061 TilingGM(bool powerOfTwoSize)
62 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), 0x88000000)
63 , fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000064 }
65
66 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.com1a2fec52009-06-22 02:17:34 +000068protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000069
70 enum {
71 kPOTSize = 32,
72 kNPOTSize = 21,
73 };
74
reed@android.com1a2fec52009-06-22 02:17:34 +000075 SkString onShortName() {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000076 SkString name("tilemodes");
77 if (!fPowerOfTwoSize) {
78 name.append("_npot");
79 }
80 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000081 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
mike@reedtribe.orga0591692012-10-18 02:01:59 +000083 SkISize onISize() { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@google.com7775d662012-11-27 15:15:58 +000085 virtual void onOnceBeforeDraw() SK_OVERRIDE {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000086 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
reed@google.com7775d662012-11-27 15:15:58 +000087 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000088 makebm(&fTexture[i], gConfigs[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000089 }
90 }
91
92 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000094 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
95
96 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000097
98 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com1a2fec52009-06-22 02:17:34 +0000100 static const bool gFilters[] = { false, true };
101 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com1a2fec52009-06-22 02:17:34 +0000103 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
104 static const char* gModeNames[] = { "C", "R", "M" };
105
106 SkScalar y = SkIntToScalar(24);
107 SkScalar x = SkIntToScalar(10);
108
109 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
110 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
111 SkPaint p;
112 SkString str;
113 p.setAntiAlias(true);
114 p.setDither(true);
115 p.setLooper(&fLooper);
116 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
117
118 p.setTextAlign(SkPaint::kCenter_Align);
119 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com1a2fec52009-06-22 02:17:34 +0000121 x += r.width() * 4 / 3;
122 }
123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com1a2fec52009-06-22 02:17:34 +0000125 y += SkIntToScalar(16);
126
127 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
128 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
129 x = SkIntToScalar(10);
130 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
131 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
132 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000133#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
134 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
135 if (!fPowerOfTwoSize) {
136 makebm(&fTexture[i], gConfigs[i], size, size);
137 }
138#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000139 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
140 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com1a2fec52009-06-22 02:17:34 +0000142 canvas->save();
143 canvas->translate(x, y);
144 canvas->drawRect(r, paint);
145 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com1a2fec52009-06-22 02:17:34 +0000147 x += r.width() * 4 / 3;
148 }
149 }
150 {
151 SkPaint p;
152 SkString str;
153 p.setAntiAlias(true);
154 p.setLooper(&fLooper);
155 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
156 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
157 }
158
159 y += r.height() * 4 / 3;
160 }
161 }
162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.com1a2fec52009-06-22 02:17:34 +0000164private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000165 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000166 typedef skiagm::GM INHERITED;
167};
168
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000169static const int gWidth = 32;
170static const int gHeight = 32;
171
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000172static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
173 SkBitmap bm;
174 makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight);
175 return SkShader::CreateBitmapShader(bm, tx, ty);
176}
177
178static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
179 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
180 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
181 SkScalar rad = SkIntToScalar(gWidth)/2;
182 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
183
184 int index = (int)ty;
185 switch (index % 3) {
186 case 0:
187 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
188 case 1:
189 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
190 case 2:
191 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
192 }
robertphillips@google.com93f03322012-12-03 17:35:19 +0000193
194 return NULL;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000195}
196
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000197typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
198
199class Tiling2GM : public skiagm::GM {
200 ShaderProc fProc;
201 SkString fName;
202public:
203 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
204 fName.printf("tilemode_%s", name);
205 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000206
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000207protected:
208 SkString onShortName() {
209 return fName;
210 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000211
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000212 SkISize onISize() { return SkISize::Make(880, 560); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000213
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000214 virtual void onDraw(SkCanvas* canvas) {
215 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
216
217 const SkScalar w = SkIntToScalar(gWidth);
218 const SkScalar h = SkIntToScalar(gHeight);
219 SkRect r = { -w, -h, w*2, h*2 };
220
221 static const SkShader::TileMode gModes[] = {
222 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
223 };
224 static const char* gModeNames[] = {
225 "Clamp", "Repeat", "Mirror"
226 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000227
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000228 SkScalar y = SkIntToScalar(24);
229 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000230
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000231 SkPaint p;
232 p.setAntiAlias(true);
233 p.setTextAlign(SkPaint::kCenter_Align);
234
235 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
236 SkString str(gModeNames[kx]);
237 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
238 x += r.width() * 4 / 3;
239 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000240
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000241 y += SkIntToScalar(16) + h;
242 p.setTextAlign(SkPaint::kRight_Align);
243
244 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
245 x = SkIntToScalar(16) + w;
246
247 SkString str(gModeNames[ky]);
248 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
249
250 x += SkIntToScalar(50);
251 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
252 SkPaint paint;
253 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000254
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000255 canvas->save();
256 canvas->translate(x, y);
257 canvas->drawRect(r, paint);
258 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000259
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000260 x += r.width() * 4 / 3;
261 }
262 y += r.height() * 4 / 3;
263 }
264 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000265
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000266private:
267 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000268};
269
270//////////////////////////////////////////////////////////////////////////////
271
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000272DEF_GM( return new TilingGM(true); )
273DEF_GM( return new TilingGM(false); )
reed@google.com39342002013-02-22 17:28:16 +0000274DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
275DEF_GM( return new Tiling2GM(make_grad, "gradient"); )