blob: 391d969c08114dbb53b93e5451fa314dcb146b5e [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"
reed@android.com1a2fec52009-06-22 02:17:34 +000019#include "SkBlurDrawLooper.h"
20
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000021static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
22 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000023 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000024
reed@android.com1a2fec52009-06-22 02:17:34 +000025 SkCanvas canvas(*bm);
26 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
27 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
28 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
29 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.com1a2fec52009-06-22 02:17:34 +000031 paint.setDither(true);
32 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000033 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
reed@android.com1a2fec52009-06-22 02:17:34 +000034 canvas.drawPaint(paint);
35}
36
37static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
38 SkShader::TileMode tmx, SkShader::TileMode tmy) {
39 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
40 paint->setShader(shader)->unref();
reed93a12152015-03-16 10:08:34 -070041 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com1a2fec52009-06-22 02:17:34 +000042}
43
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000044static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000045 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000046 kRGB_565_SkColorType,
reed@android.com1a2fec52009-06-22 02:17:34 +000047};
reed@android.com1a2fec52009-06-22 02:17:34 +000048
mike@reedtribe.orga0591692012-10-18 02:01:59 +000049class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000050public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000051 TilingGM(bool powerOfTwoSize)
robertphillips@google.com6db2ae22013-08-30 12:41:42 +000052 : fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000053 }
54
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000055 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@android.com1a2fec52009-06-22 02:17:34 +000057protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000058
59 enum {
60 kPOTSize = 32,
61 kNPOTSize = 21,
62 };
63
mtklein36352bf2015-03-25 18:17:31 -070064 SkString onShortName() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000065 SkString name("tilemodes");
66 if (!fPowerOfTwoSize) {
67 name.append("_npot");
68 }
69 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000070 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
mtklein36352bf2015-03-25 18:17:31 -070072 SkISize onISize() override { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
mtklein36352bf2015-03-25 18:17:31 -070074 void onOnceBeforeDraw() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000075 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000076 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
77 makebm(&fTexture[i], gColorTypes[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000078 }
79 }
80
mtklein36352bf2015-03-25 18:17:31 -070081 void onDraw(SkCanvas* canvas) override {
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000083 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
84
85 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000086
87 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com1a2fec52009-06-22 02:17:34 +000089 static const bool gFilters[] = { false, true };
90 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com1a2fec52009-06-22 02:17:34 +000092 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
93 static const char* gModeNames[] = { "C", "R", "M" };
94
95 SkScalar y = SkIntToScalar(24);
96 SkScalar x = SkIntToScalar(10);
97
98 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
99 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
100 SkPaint p;
101 SkString str;
102 p.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400103 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000104 p.setDither(true);
reed@android.com1a2fec52009-06-22 02:17:34 +0000105 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
106
107 p.setTextAlign(SkPaint::kCenter_Align);
108 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@android.com1a2fec52009-06-22 02:17:34 +0000110 x += r.width() * 4 / 3;
111 }
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com1a2fec52009-06-22 02:17:34 +0000114 y += SkIntToScalar(16);
115
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000116 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000117 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
118 x = SkIntToScalar(10);
119 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
120 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
121 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000122#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
123 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
124 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000125 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000126 }
127#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000128 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
129 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com1a2fec52009-06-22 02:17:34 +0000131 canvas->save();
132 canvas->translate(x, y);
133 canvas->drawRect(r, paint);
134 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
reed@android.com1a2fec52009-06-22 02:17:34 +0000136 x += r.width() * 4 / 3;
137 }
138 }
139 {
140 SkPaint p;
141 SkString str;
142 p.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400143 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000144 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
145 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
146 }
147
148 y += r.height() * 4 / 3;
149 }
150 }
151 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
reed@android.com1a2fec52009-06-22 02:17:34 +0000153private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000154 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000155 typedef skiagm::GM INHERITED;
156};
157
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000158static const int gWidth = 32;
159static const int gHeight = 32;
160
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000161static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
162 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000163 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000164 return SkShader::CreateBitmapShader(bm, tx, ty);
165}
166
167static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
168 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
169 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
170 SkScalar rad = SkIntToScalar(gWidth)/2;
171 SkColor colors[] = { 0xFFFF0000, 0xFF0044FF };
172
173 int index = (int)ty;
174 switch (index % 3) {
175 case 0:
176 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx);
177 case 1:
178 return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx);
179 case 2:
180 return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors));
181 }
robertphillips@google.com93f03322012-12-03 17:35:19 +0000182
183 return NULL;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000184}
185
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000186typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
187
188class Tiling2GM : public skiagm::GM {
189 ShaderProc fProc;
190 SkString fName;
191public:
192 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
193 fName.printf("tilemode_%s", name);
194 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000195
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000196protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000197
mtklein36352bf2015-03-25 18:17:31 -0700198 SkString onShortName() override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000199 return fName;
200 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000201
mtklein36352bf2015-03-25 18:17:31 -0700202 SkISize onISize() override { return SkISize::Make(880, 560); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000203
mtklein36352bf2015-03-25 18:17:31 -0700204 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000205 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
206
207 const SkScalar w = SkIntToScalar(gWidth);
208 const SkScalar h = SkIntToScalar(gHeight);
209 SkRect r = { -w, -h, w*2, h*2 };
210
211 static const SkShader::TileMode gModes[] = {
212 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
213 };
214 static const char* gModeNames[] = {
215 "Clamp", "Repeat", "Mirror"
216 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000217
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000218 SkScalar y = SkIntToScalar(24);
219 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000220
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000221 SkPaint p;
222 p.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400223 sk_tool_utils::set_portable_typeface(&p);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000224 p.setTextAlign(SkPaint::kCenter_Align);
225
226 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
227 SkString str(gModeNames[kx]);
228 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
229 x += r.width() * 4 / 3;
230 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000231
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000232 y += SkIntToScalar(16) + h;
233 p.setTextAlign(SkPaint::kRight_Align);
234
235 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
236 x = SkIntToScalar(16) + w;
237
238 SkString str(gModeNames[ky]);
239 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
240
241 x += SkIntToScalar(50);
242 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
243 SkPaint paint;
244 paint.setShader(fProc(gModes[kx], gModes[ky]))->unref();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000245
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000246 canvas->save();
247 canvas->translate(x, y);
248 canvas->drawRect(r, paint);
249 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000250
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000251 x += r.width() * 4 / 3;
252 }
253 y += r.height() * 4 / 3;
254 }
255 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000256
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000257private:
258 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000259};
260
261//////////////////////////////////////////////////////////////////////////////
262
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000263DEF_GM( return new TilingGM(true); )
264DEF_GM( return new TilingGM(false); )
reed@google.com39342002013-02-22 17:28:16 +0000265DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
266DEF_GM( return new Tiling2GM(make_grad, "gradient"); )