blob: e04c52ae0fd6e5423ed9016e254e860bb92d20df [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"
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000019#include "SkUnitMappers.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000020#include "SkBlurDrawLooper.h"
21
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000022static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
23 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000024 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@android.com1a2fec52009-06-22 02:17:34 +000026 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000032 SkUnitMapper* um = NULL;
33
34 um = new SkCosineMapper;
35// um = new SkDiscreteMapper(12);
36
37 SkAutoUnref au(um);
38
reed@android.com1a2fec52009-06-22 02:17:34 +000039 paint.setDither(true);
40 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000041 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
reed@android.com1a2fec52009-06-22 02:17:34 +000042 canvas.drawPaint(paint);
43}
44
45static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
46 SkShader::TileMode tmx, SkShader::TileMode tmy) {
47 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
48 paint->setShader(shader)->unref();
reed@google.com44699382013-10-31 17:28:30 +000049 paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@android.com1a2fec52009-06-22 02:17:34 +000050}
51
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000052static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000053 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000054 kRGB_565_SkColorType,
reed@android.com1a2fec52009-06-22 02:17:34 +000055};
reed@android.com1a2fec52009-06-22 02:17:34 +000056
mike@reedtribe.orga0591692012-10-18 02:01:59 +000057class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000058public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000059 TilingGM(bool powerOfTwoSize)
robertphillips@google.com6db2ae22013-08-30 12:41:42 +000060 : fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000061 }
62
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000063 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.com1a2fec52009-06-22 02:17:34 +000065protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000066
67 enum {
68 kPOTSize = 32,
69 kNPOTSize = 21,
70 };
71
reed@android.com1a2fec52009-06-22 02:17:34 +000072 SkString onShortName() {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000073 SkString name("tilemodes");
74 if (!fPowerOfTwoSize) {
75 name.append("_npot");
76 }
77 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000078 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
mike@reedtribe.orga0591692012-10-18 02:01:59 +000080 SkISize onISize() { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@google.com7775d662012-11-27 15:15:58 +000082 virtual void onOnceBeforeDraw() SK_OVERRIDE {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000083 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000084 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
85 makebm(&fTexture[i], gColorTypes[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000086 }
87 }
88
89 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000091 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
92
93 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000094
95 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com1a2fec52009-06-22 02:17:34 +000097 static const bool gFilters[] = { false, true };
98 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com1a2fec52009-06-22 02:17:34 +0000100 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
101 static const char* gModeNames[] = { "C", "R", "M" };
102
103 SkScalar y = SkIntToScalar(24);
104 SkScalar x = SkIntToScalar(10);
105
106 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
107 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
108 SkPaint p;
109 SkString str;
110 p.setAntiAlias(true);
111 p.setDither(true);
reed@android.com1a2fec52009-06-22 02:17:34 +0000112 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
113
114 p.setTextAlign(SkPaint::kCenter_Align);
115 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com1a2fec52009-06-22 02:17:34 +0000117 x += r.width() * 4 / 3;
118 }
119 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com1a2fec52009-06-22 02:17:34 +0000121 y += SkIntToScalar(16);
122
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000123 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000124 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
125 x = SkIntToScalar(10);
126 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
127 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
128 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000129#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
130 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
131 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000132 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000133 }
134#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000135 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
136 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com1a2fec52009-06-22 02:17:34 +0000138 canvas->save();
139 canvas->translate(x, y);
140 canvas->drawRect(r, paint);
141 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com1a2fec52009-06-22 02:17:34 +0000143 x += r.width() * 4 / 3;
144 }
145 }
146 {
147 SkPaint p;
148 SkString str;
149 p.setAntiAlias(true);
reed@android.com1a2fec52009-06-22 02:17:34 +0000150 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
151 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
152 }
153
154 y += r.height() * 4 / 3;
155 }
156 }
157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com1a2fec52009-06-22 02:17:34 +0000159private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000160 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000161 typedef skiagm::GM INHERITED;
162};
163
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000164static const int gWidth = 32;
165static const int gHeight = 32;
166
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000167static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
168 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000169 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000170 return SkShader::CreateBitmapShader(bm, tx, ty);
171}
172
173static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
174 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
175 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
176 SkScalar rad = SkIntToScalar(gWidth)/2;
177 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
178
179 int index = (int)ty;
180 switch (index % 3) {
181 case 0:
182 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
183 case 1:
184 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
185 case 2:
186 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
187 }
robertphillips@google.com93f03322012-12-03 17:35:19 +0000188
189 return NULL;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000190}
191
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000192typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
193
194class Tiling2GM : public skiagm::GM {
195 ShaderProc fProc;
196 SkString fName;
197public:
198 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
199 fName.printf("tilemode_%s", name);
200 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000201
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000202protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000203 virtual uint32_t onGetFlags() const SK_OVERRIDE {
204 return kSkipTiled_Flag;
205 }
206
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000207 SkString onShortName() {
208 return fName;
209 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000210
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000211 SkISize onISize() { return SkISize::Make(880, 560); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000212
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000213 virtual void onDraw(SkCanvas* canvas) {
214 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
215
216 const SkScalar w = SkIntToScalar(gWidth);
217 const SkScalar h = SkIntToScalar(gHeight);
218 SkRect r = { -w, -h, w*2, h*2 };
219
220 static const SkShader::TileMode gModes[] = {
221 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
222 };
223 static const char* gModeNames[] = {
224 "Clamp", "Repeat", "Mirror"
225 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000226
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000227 SkScalar y = SkIntToScalar(24);
228 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000229
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000230 SkPaint p;
231 p.setAntiAlias(true);
232 p.setTextAlign(SkPaint::kCenter_Align);
233
234 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
235 SkString str(gModeNames[kx]);
236 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
237 x += r.width() * 4 / 3;
238 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000239
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000240 y += SkIntToScalar(16) + h;
241 p.setTextAlign(SkPaint::kRight_Align);
242
243 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
244 x = SkIntToScalar(16) + w;
245
246 SkString str(gModeNames[ky]);
247 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
248
249 x += SkIntToScalar(50);
250 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
251 SkPaint paint;
252 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000253
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000254 canvas->save();
255 canvas->translate(x, y);
256 canvas->drawRect(r, paint);
257 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000258
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000259 x += r.width() * 4 / 3;
260 }
261 y += r.height() * 4 / 3;
262 }
263 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000264
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000265private:
266 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000267};
268
269//////////////////////////////////////////////////////////////////////////////
270
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000271DEF_GM( return new TilingGM(true); )
272DEF_GM( return new TilingGM(false); )
reed@google.com39342002013-02-22 17:28:16 +0000273DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
274DEF_GM( return new Tiling2GM(make_grad, "gradient"); )