blob: fe5ae48c32436b6ba2bbc07f5741d63fd22a311d [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 */
mtklein7a1f45f2016-08-04 06:19:33 -07007#include "SkColorFilter.h"
8#include "SkMaskFilter.h"
reed@android.com1a2fec52009-06-22 02:17:34 +00009#include "SkPath.h"
10#include "SkRegion.h"
11#include "SkShader.h"
Mike Reed331ccfd2018-10-25 12:36:06 -040012#include "SkTextUtils.h"
Hal Canaryea60b952018-08-21 11:45:46 -040013#include "SkUTF.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050014#include "ToolUtils.h"
15#include "gm.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000016// effects
17#include "SkGradientShader.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000018#include "SkBlurDrawLooper.h"
19
Mike Reeddfc0e912018-02-16 12:40:18 -050020#include "Resources.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
reed@android.com1a2fec52009-06-22 02:17:34 +000032 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070033 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors),
34 SkShader::kClamp_TileMode));
reed@android.com1a2fec52009-06-22 02:17:34 +000035 canvas.drawPaint(paint);
36}
37
38static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
39 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed1a9b9642016-03-13 14:13:58 -070040 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070041 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com1a2fec52009-06-22 02:17:34 +000042}
43
mtkleindbfd7ab2016-09-01 11:24:54 -070044constexpr 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 {
Hal Canarydf2d27e2019-01-08 09:38:02 -050082 SkPaint textPaint;
Mike Kleinea3f0142019-03-20 11:12:10 -050083 SkFont font(ToolUtils::create_portable_typeface(), 12);
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000085 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
86
87 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000088
mtkleindbfd7ab2016-09-01 11:24:54 -070089 const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
mtkleindbfd7ab2016-09-01 11:24:54 -070091 constexpr bool gFilters[] = { false, true };
92 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
mtkleindbfd7ab2016-09-01 11:24:54 -070094 constexpr SkShader::TileMode gModes[] = {
95 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
96 static const char* gModeNames[] = { "C", "R", "M" };
reed@android.com1a2fec52009-06-22 02:17:34 +000097
98 SkScalar y = SkIntToScalar(24);
99 SkScalar x = SkIntToScalar(10);
100
101 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
102 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
103 SkPaint p;
reed@android.com1a2fec52009-06-22 02:17:34 +0000104 p.setDither(true);
Mike Reeddc5863c2018-12-23 23:19:14 -0500105 SkString str;
Mike Kleinea3f0142019-03-20 11:12:10 -0500106 SkFont font(ToolUtils::create_portable_typeface());
reed@android.com1a2fec52009-06-22 02:17:34 +0000107 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
108
Mike Reeddc5863c2018-12-23 23:19:14 -0500109 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, p,
Mike Reed3a42ec02018-10-30 12:53:21 -0400110 SkTextUtils::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
reed@android.com1a2fec52009-06-22 02:17:34 +0000112 x += r.width() * 4 / 3;
113 }
114 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.com1a2fec52009-06-22 02:17:34 +0000116 y += SkIntToScalar(16);
117
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000118 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000119 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
120 x = SkIntToScalar(10);
121 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
122 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
123 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000124#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
125 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
126 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000127 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000128 }
129#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000130 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
131 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com1a2fec52009-06-22 02:17:34 +0000133 canvas->save();
134 canvas->translate(x, y);
135 canvas->drawRect(r, paint);
136 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com1a2fec52009-06-22 02:17:34 +0000138 x += r.width() * 4 / 3;
139 }
140 }
Hal Canarydf2d27e2019-01-08 09:38:02 -0500141 canvas->drawString(SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
142 x, y + r.height() * 2 / 3, font, textPaint);
reed@android.com1a2fec52009-06-22 02:17:34 +0000143
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};
Mike Reeddfc0e912018-02-16 12:40:18 -0500153DEF_GM( return new TilingGM(true); )
154DEF_GM( return new TilingGM(false); )
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000155
mtkleindbfd7ab2016-09-01 11:24:54 -0700156constexpr int gWidth = 32;
157constexpr int gHeight = 32;
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000158
reed1a9b9642016-03-13 14:13:58 -0700159static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000160 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000161 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
reed1a9b9642016-03-13 14:13:58 -0700162 return SkShader::MakeBitmapShader(bm, tx, ty);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000163}
164
reed1a9b9642016-03-13 14:13:58 -0700165static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000166 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
167 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
168 SkScalar rad = SkIntToScalar(gWidth)/2;
Mike Kleinea3f0142019-03-20 11:12:10 -0500169 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000170
171 int index = (int)ty;
172 switch (index % 3) {
173 case 0:
reed1a9b9642016-03-13 14:13:58 -0700174 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000175 case 1:
reed1a9b9642016-03-13 14:13:58 -0700176 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000177 case 2:
Florin Malita5a9a9812017-08-01 16:38:08 -0400178 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr,
179 SK_ARRAY_COUNT(colors), tx, 135, 225, 0, nullptr);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000180 }
halcanary96fcdcc2015-08-27 07:41:13 -0700181 return nullptr;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000182}
183
reed1a9b9642016-03-13 14:13:58 -0700184typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000185
186class Tiling2GM : public skiagm::GM {
187 ShaderProc fProc;
188 SkString fName;
189public:
190 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
191 fName.printf("tilemode_%s", name);
192 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000193
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000194protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000195
mtklein36352bf2015-03-25 18:17:31 -0700196 SkString onShortName() override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000197 return fName;
198 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000199
reed6dc14aa2016-04-11 07:46:38 -0700200 SkISize onISize() override { return SkISize::Make(650, 610); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000201
mtklein36352bf2015-03-25 18:17:31 -0700202 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000203 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
204
205 const SkScalar w = SkIntToScalar(gWidth);
206 const SkScalar h = SkIntToScalar(gHeight);
207 SkRect r = { -w, -h, w*2, h*2 };
208
mtkleindbfd7ab2016-09-01 11:24:54 -0700209 constexpr SkShader::TileMode gModes[] = {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000210 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
211 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700212 const char* gModeNames[] = {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000213 "Clamp", "Repeat", "Mirror"
214 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000215
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000216 SkScalar y = SkIntToScalar(24);
217 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000218
Mike Kleinea3f0142019-03-20 11:12:10 -0500219 SkFont font(ToolUtils::create_portable_typeface());
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000220
221 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
222 SkString str(gModeNames[kx]);
Mike Reeddc5863c2018-12-23 23:19:14 -0500223 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
224 SkTextUtils::kCenter_Align);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000225 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;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000229
230 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
231 x = SkIntToScalar(16) + w;
232
233 SkString str(gModeNames[ky]);
Mike Reeddc5863c2018-12-23 23:19:14 -0500234 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(),
235 SkTextUtils::kRight_Align);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000236
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};
reed@google.com39342002013-02-22 17:28:16 +0000256DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
257DEF_GM( return new Tiling2GM(make_grad, "gradient"); )
Mike Reeddfc0e912018-02-16 12:40:18 -0500258
259////////////////////
260
261#include "SkGradientShader.h"
262
Michael Ludwigbe315a22018-12-17 09:50:51 -0500263DEF_SIMPLE_GM(tilemode_decal, canvas, 720, 1100) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500264 auto img = GetResourceAsImage("images/mandrill_128.png");
265 SkPaint bgpaint;
266 bgpaint.setColor(SK_ColorYELLOW);
267
268 SkRect r = { -20, -20, img->width() + 20.0f, img->height() + 20.0f };
Michael Ludwigbe315a22018-12-17 09:50:51 -0500269 canvas->translate(45, 45);
Mike Reeddfc0e912018-02-16 12:40:18 -0500270
271 std::function<void(SkPaint*, SkShader::TileMode, SkShader::TileMode)> shader_procs[] = {
272 [img](SkPaint* paint, SkShader::TileMode tx, SkShader::TileMode ty) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500273 // Test no filtering with decal mode
Mike Reeddfc0e912018-02-16 12:40:18 -0500274 paint->setShader(img->makeShader(tx, ty));
Michael Ludwigbe315a22018-12-17 09:50:51 -0500275 paint->setFilterQuality(kNone_SkFilterQuality);
276 },
277 [img](SkPaint* paint, SkShader::TileMode tx, SkShader::TileMode ty) {
278 // Test bilerp approximation for decal mode (or clamp to border HW)
279 paint->setShader(img->makeShader(tx, ty));
280 paint->setFilterQuality(kLow_SkFilterQuality);
281 },
282 [img](SkPaint* paint, SkShader::TileMode tx, SkShader::TileMode ty) {
283 // Test bicubic filter with decal mode
284 paint->setShader(img->makeShader(tx, ty));
285 paint->setFilterQuality(kHigh_SkFilterQuality);
Mike Reeddfc0e912018-02-16 12:40:18 -0500286 },
287 [img](SkPaint* paint, SkShader::TileMode tx, SkShader::TileMode ty) {
288 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
289 const SkPoint pts[] = {{ 0, 0 }, {img->width()*1.0f, img->height()*1.0f }};
290 const SkScalar* pos = nullptr;
291 const int count = SK_ARRAY_COUNT(colors);
292 paint->setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, tx));
293 },
294 [img](SkPaint* paint, SkShader::TileMode tx, SkShader::TileMode ty) {
295 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
296 const SkScalar* pos = nullptr;
297 const int count = SK_ARRAY_COUNT(colors);
298 paint->setShader(SkGradientShader::MakeRadial({ img->width()*0.5f, img->width()*0.5f },
299 img->width()*0.5f, colors, pos, count, tx));
300 },
301 };
302
303 const struct XY {
304 SkShader::TileMode fX;
305 SkShader::TileMode fY;
306 } pairs[] = {
307 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode },
308 { SkShader::kClamp_TileMode, SkShader::kDecal_TileMode },
309 { SkShader::kDecal_TileMode, SkShader::kClamp_TileMode },
310 { SkShader::kDecal_TileMode, SkShader::kDecal_TileMode },
311 };
312 for (const auto& p : pairs) {
313 SkPaint paint;
314 canvas->save();
315 for (const auto& proc : shader_procs) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500316 canvas->save();
317 // Apply a slight rotation to highlight the differences between filtered and unfiltered
318 // decal edges
319 canvas->rotate(4);
Mike Reeddfc0e912018-02-16 12:40:18 -0500320 canvas->drawRect(r, bgpaint);
321 proc(&paint, p.fX, p.fY);
322 canvas->drawRect(r, paint);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500323 canvas->restore();
Mike Reeddfc0e912018-02-16 12:40:18 -0500324 canvas->translate(0, r.height() + 20);
325 }
326 canvas->restore();
327 canvas->translate(r.width() + 10, 0);
328 }
329}
330