blob: d774939648d3ea7fa6b1f9e8ccc87f2d242056e8 [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"
reed@android.com1a2fec52009-06-22 02:17:34 +000013#include "SkColorFilter.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000014
15// effects
16#include "SkGradientShader.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000017#include "SkBlurDrawLooper.h"
18
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000019static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
20 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000021 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
reed@android.com1a2fec52009-06-22 02:17:34 +000023 SkCanvas canvas(*bm);
24 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
25 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
26 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
27 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
reed@android.com1a2fec52009-06-22 02:17:34 +000029 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070030 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors),
31 SkShader::kClamp_TileMode));
reed@android.com1a2fec52009-06-22 02:17:34 +000032 canvas.drawPaint(paint);
33}
34
35static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
36 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed1a9b9642016-03-13 14:13:58 -070037 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070038 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com1a2fec52009-06-22 02:17:34 +000039}
40
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000041static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000042 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000043 kRGB_565_SkColorType,
reed@android.com1a2fec52009-06-22 02:17:34 +000044};
reed@android.com1a2fec52009-06-22 02:17:34 +000045
mike@reedtribe.orga0591692012-10-18 02:01:59 +000046class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000047public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000048 TilingGM(bool powerOfTwoSize)
robertphillips@google.com6db2ae22013-08-30 12:41:42 +000049 : fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000050 }
51
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000052 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
reed@android.com1a2fec52009-06-22 02:17:34 +000054protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000055
56 enum {
57 kPOTSize = 32,
58 kNPOTSize = 21,
59 };
60
mtklein36352bf2015-03-25 18:17:31 -070061 SkString onShortName() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000062 SkString name("tilemodes");
63 if (!fPowerOfTwoSize) {
64 name.append("_npot");
65 }
66 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000067 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
mtklein36352bf2015-03-25 18:17:31 -070069 SkISize onISize() override { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
mtklein36352bf2015-03-25 18:17:31 -070071 void onOnceBeforeDraw() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000072 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000073 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
74 makebm(&fTexture[i], gColorTypes[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000075 }
76 }
77
mtklein36352bf2015-03-25 18:17:31 -070078 void onDraw(SkCanvas* canvas) override {
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000080 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
81
82 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000083
84 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@android.com1a2fec52009-06-22 02:17:34 +000086 static const bool gFilters[] = { false, true };
87 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com1a2fec52009-06-22 02:17:34 +000089 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
90 static const char* gModeNames[] = { "C", "R", "M" };
91
92 SkScalar y = SkIntToScalar(24);
93 SkScalar x = SkIntToScalar(10);
94
95 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
96 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
97 SkPaint p;
98 SkString str;
99 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700100 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000101 p.setDither(true);
reed@android.com1a2fec52009-06-22 02:17:34 +0000102 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
103
104 p.setTextAlign(SkPaint::kCenter_Align);
105 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@android.com1a2fec52009-06-22 02:17:34 +0000107 x += r.width() * 4 / 3;
108 }
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.com1a2fec52009-06-22 02:17:34 +0000111 y += SkIntToScalar(16);
112
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000113 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000114 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
115 x = SkIntToScalar(10);
116 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
117 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
118 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000119#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
120 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
121 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000122 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000123 }
124#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000125 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
126 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@android.com1a2fec52009-06-22 02:17:34 +0000128 canvas->save();
129 canvas->translate(x, y);
130 canvas->drawRect(r, paint);
131 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com1a2fec52009-06-22 02:17:34 +0000133 x += r.width() * 4 / 3;
134 }
135 }
136 {
137 SkPaint p;
138 SkString str;
139 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700140 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000141 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
142 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
143 }
144
145 y += r.height() * 4 / 3;
146 }
147 }
148 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com1a2fec52009-06-22 02:17:34 +0000150private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000151 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000152 typedef skiagm::GM INHERITED;
153};
154
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000155static const int gWidth = 32;
156static const int gHeight = 32;
157
reed1a9b9642016-03-13 14:13:58 -0700158static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000159 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000160 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
reed1a9b9642016-03-13 14:13:58 -0700161 return SkShader::MakeBitmapShader(bm, tx, ty);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000162}
163
reed1a9b9642016-03-13 14:13:58 -0700164static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000165 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
166 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
167 SkScalar rad = SkIntToScalar(gWidth)/2;
caryclark1e545b62015-07-13 08:19:58 -0700168 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000169
170 int index = (int)ty;
171 switch (index % 3) {
172 case 0:
reed1a9b9642016-03-13 14:13:58 -0700173 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000174 case 1:
reed1a9b9642016-03-13 14:13:58 -0700175 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000176 case 2:
reed1a9b9642016-03-13 14:13:58 -0700177 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000178 }
halcanary96fcdcc2015-08-27 07:41:13 -0700179 return nullptr;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000180}
181
reed1a9b9642016-03-13 14:13:58 -0700182typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000183
184class Tiling2GM : public skiagm::GM {
185 ShaderProc fProc;
186 SkString fName;
187public:
188 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
189 fName.printf("tilemode_%s", name);
190 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000191
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000192protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000193
mtklein36352bf2015-03-25 18:17:31 -0700194 SkString onShortName() override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000195 return fName;
196 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000197
mtklein36352bf2015-03-25 18:17:31 -0700198 SkISize onISize() override { return SkISize::Make(880, 560); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000199
mtklein36352bf2015-03-25 18:17:31 -0700200 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000201 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
202
203 const SkScalar w = SkIntToScalar(gWidth);
204 const SkScalar h = SkIntToScalar(gHeight);
205 SkRect r = { -w, -h, w*2, h*2 };
206
207 static const SkShader::TileMode gModes[] = {
208 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
209 };
210 static const char* gModeNames[] = {
211 "Clamp", "Repeat", "Mirror"
212 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000213
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000214 SkScalar y = SkIntToScalar(24);
215 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000216
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000217 SkPaint p;
218 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700219 sk_tool_utils::set_portable_typeface(&p);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000220 p.setTextAlign(SkPaint::kCenter_Align);
221
222 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
223 SkString str(gModeNames[kx]);
224 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
225 x += r.width() * 4 / 3;
226 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000227
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000228 y += SkIntToScalar(16) + h;
229 p.setTextAlign(SkPaint::kRight_Align);
230
231 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
232 x = SkIntToScalar(16) + w;
233
234 SkString str(gModeNames[ky]);
235 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
236
237 x += SkIntToScalar(50);
238 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
239 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700240 paint.setShader(fProc(gModes[kx], gModes[ky]));
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000241
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000242 canvas->save();
243 canvas->translate(x, y);
244 canvas->drawRect(r, paint);
245 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000246
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000247 x += r.width() * 4 / 3;
248 }
249 y += r.height() * 4 / 3;
250 }
251 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000252
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000253private:
254 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000255};
256
257//////////////////////////////////////////////////////////////////////////////
258
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000259DEF_GM( return new TilingGM(true); )
260DEF_GM( return new TilingGM(false); )
reed@google.com39342002013-02-22 17:28:16 +0000261DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
262DEF_GM( return new Tiling2GM(make_grad, "gradient"); )