blob: bd2dcfef161b66d9912871741dcc41df2d930da3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com1a2fec52009-06-22 02:17:34 +00007#include "gm.h"
8#include "SkPath.h"
9#include "SkRegion.h"
10#include "SkShader.h"
11#include "SkUtils.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000012#include "SkColorFilter.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000013
14// effects
15#include "SkGradientShader.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000016#include "SkBlurDrawLooper.h"
17
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000018static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
19 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000020 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000021
reed@android.com1a2fec52009-06-22 02:17:34 +000022 SkCanvas canvas(*bm);
23 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
24 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
25 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
26 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@android.com1a2fec52009-06-22 02:17:34 +000028 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070029 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors),
30 SkShader::kClamp_TileMode));
reed@android.com1a2fec52009-06-22 02:17:34 +000031 canvas.drawPaint(paint);
32}
33
34static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
35 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed1a9b9642016-03-13 14:13:58 -070036 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070037 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com1a2fec52009-06-22 02:17:34 +000038}
39
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000040static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000041 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000042 kRGB_565_SkColorType,
reed@android.com1a2fec52009-06-22 02:17:34 +000043};
reed@android.com1a2fec52009-06-22 02:17:34 +000044
mike@reedtribe.orga0591692012-10-18 02:01:59 +000045class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000046public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000047 TilingGM(bool powerOfTwoSize)
robertphillips@google.com6db2ae22013-08-30 12:41:42 +000048 : fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000049 }
50
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000051 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com1a2fec52009-06-22 02:17:34 +000053protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000054
55 enum {
56 kPOTSize = 32,
57 kNPOTSize = 21,
58 };
59
mtklein36352bf2015-03-25 18:17:31 -070060 SkString onShortName() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000061 SkString name("tilemodes");
62 if (!fPowerOfTwoSize) {
63 name.append("_npot");
64 }
65 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000066 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
mtklein36352bf2015-03-25 18:17:31 -070068 SkISize onISize() override { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
mtklein36352bf2015-03-25 18:17:31 -070070 void onOnceBeforeDraw() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000071 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000072 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
73 makebm(&fTexture[i], gColorTypes[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000074 }
75 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 void onDraw(SkCanvas* canvas) override {
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000079 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
80
81 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000082
83 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@android.com1a2fec52009-06-22 02:17:34 +000085 static const bool gFilters[] = { false, true };
86 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com1a2fec52009-06-22 02:17:34 +000088 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
89 static const char* gModeNames[] = { "C", "R", "M" };
90
91 SkScalar y = SkIntToScalar(24);
92 SkScalar x = SkIntToScalar(10);
93
94 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
95 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
96 SkPaint p;
97 SkString str;
98 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070099 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000100 p.setDither(true);
reed@android.com1a2fec52009-06-22 02:17:34 +0000101 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
102
103 p.setTextAlign(SkPaint::kCenter_Align);
104 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com1a2fec52009-06-22 02:17:34 +0000106 x += r.width() * 4 / 3;
107 }
108 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@android.com1a2fec52009-06-22 02:17:34 +0000110 y += SkIntToScalar(16);
111
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000112 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000113 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
114 x = SkIntToScalar(10);
115 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
116 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
117 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000118#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
119 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
120 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000121 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000122 }
123#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000124 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
125 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@android.com1a2fec52009-06-22 02:17:34 +0000127 canvas->save();
128 canvas->translate(x, y);
129 canvas->drawRect(r, paint);
130 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com1a2fec52009-06-22 02:17:34 +0000132 x += r.width() * 4 / 3;
133 }
134 }
135 {
136 SkPaint p;
137 SkString str;
138 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700139 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000140 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
141 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
142 }
143
144 y += r.height() * 4 / 3;
145 }
146 }
147 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com1a2fec52009-06-22 02:17:34 +0000149private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000150 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000151 typedef skiagm::GM INHERITED;
152};
153
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000154static const int gWidth = 32;
155static const int gHeight = 32;
156
reed1a9b9642016-03-13 14:13:58 -0700157static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000158 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000159 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
reed1a9b9642016-03-13 14:13:58 -0700160 return SkShader::MakeBitmapShader(bm, tx, ty);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000161}
162
reed1a9b9642016-03-13 14:13:58 -0700163static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000164 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
165 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
166 SkScalar rad = SkIntToScalar(gWidth)/2;
caryclark1e545b62015-07-13 08:19:58 -0700167 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000168
169 int index = (int)ty;
170 switch (index % 3) {
171 case 0:
reed1a9b9642016-03-13 14:13:58 -0700172 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000173 case 1:
reed1a9b9642016-03-13 14:13:58 -0700174 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000175 case 2:
reed1a9b9642016-03-13 14:13:58 -0700176 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000177 }
halcanary96fcdcc2015-08-27 07:41:13 -0700178 return nullptr;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000179}
180
reed1a9b9642016-03-13 14:13:58 -0700181typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000182
183class Tiling2GM : public skiagm::GM {
184 ShaderProc fProc;
185 SkString fName;
186public:
187 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
188 fName.printf("tilemode_%s", name);
189 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000190
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000191protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000192
mtklein36352bf2015-03-25 18:17:31 -0700193 SkString onShortName() override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000194 return fName;
195 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000196
reed6dc14aa2016-04-11 07:46:38 -0700197 SkISize onISize() override { return SkISize::Make(650, 610); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000198
mtklein36352bf2015-03-25 18:17:31 -0700199 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000200 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
201
202 const SkScalar w = SkIntToScalar(gWidth);
203 const SkScalar h = SkIntToScalar(gHeight);
204 SkRect r = { -w, -h, w*2, h*2 };
205
206 static const SkShader::TileMode gModes[] = {
207 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
208 };
209 static const char* gModeNames[] = {
210 "Clamp", "Repeat", "Mirror"
211 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000212
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000213 SkScalar y = SkIntToScalar(24);
214 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000215
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000216 SkPaint p;
217 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700218 sk_tool_utils::set_portable_typeface(&p);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000219 p.setTextAlign(SkPaint::kCenter_Align);
220
221 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
222 SkString str(gModeNames[kx]);
223 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
224 x += r.width() * 4 / 3;
225 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000226
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000227 y += SkIntToScalar(16) + h;
228 p.setTextAlign(SkPaint::kRight_Align);
229
230 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
231 x = SkIntToScalar(16) + w;
232
233 SkString str(gModeNames[ky]);
234 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
235
236 x += SkIntToScalar(50);
237 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
238 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700239 paint.setShader(fProc(gModes[kx], gModes[ky]));
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000240
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000241 canvas->save();
242 canvas->translate(x, y);
243 canvas->drawRect(r, paint);
244 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000245
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000246 x += r.width() * 4 / 3;
247 }
248 y += r.height() * 4 / 3;
249 }
250 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000251
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000252private:
253 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000254};
255
256//////////////////////////////////////////////////////////////////////////////
257
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000258DEF_GM( return new TilingGM(true); )
259DEF_GM( return new TilingGM(false); )
reed@google.com39342002013-02-22 17:28:16 +0000260DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
261DEF_GM( return new Tiling2GM(make_grad, "gradient"); )