blob: fe39744f31d0a99adc5956629737dbf542789e4b [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
Ben Wagner7fde8e12019-05-01 17:28:53 -04008#include "gm/gm.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFilterQuality.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkImage.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkShader.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
24#include "include/core/SkTileMode.h"
25#include "include/core/SkTypeface.h"
26#include "include/core/SkTypes.h"
27#include "include/effects/SkGradientShader.h"
28#include "include/utils/SkTextUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "tools/ToolUtils.h"
31
32#include <functional>
Mike Reeddfc0e912018-02-16 12:40:18 -050033
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000034static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
35 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000036 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
reed@android.com1a2fec52009-06-22 02:17:34 +000038 SkCanvas canvas(*bm);
39 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
40 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
41 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
42 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.com1a2fec52009-06-22 02:17:34 +000044 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070045 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040046 SkTileMode::kClamp));
reed@android.com1a2fec52009-06-22 02:17:34 +000047 canvas.drawPaint(paint);
48}
49
50static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
Mike Reedfae8fce2019-04-03 10:27:45 -040051 SkTileMode tmx, SkTileMode tmy) {
Mike Reed50acf8f2019-04-08 13:20:23 -040052 paint->setShader(bm.makeShader(tmx, tmy));
reed93a12152015-03-16 10:08:34 -070053 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com1a2fec52009-06-22 02:17:34 +000054}
55
mtkleindbfd7ab2016-09-01 11:24:54 -070056constexpr SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000057 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000058 kRGB_565_SkColorType,
reed@android.com1a2fec52009-06-22 02:17:34 +000059};
reed@android.com1a2fec52009-06-22 02:17:34 +000060
mike@reedtribe.orga0591692012-10-18 02:01:59 +000061class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000062public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000063 TilingGM(bool powerOfTwoSize)
robertphillips@google.com6db2ae22013-08-30 12:41:42 +000064 : fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000065 }
66
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000067 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@android.com1a2fec52009-06-22 02:17:34 +000069protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000070
71 enum {
72 kPOTSize = 32,
73 kNPOTSize = 21,
74 };
75
mtklein36352bf2015-03-25 18:17:31 -070076 SkString onShortName() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000077 SkString name("tilemodes");
78 if (!fPowerOfTwoSize) {
79 name.append("_npot");
80 }
81 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000082 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
mtklein36352bf2015-03-25 18:17:31 -070084 SkISize onISize() override { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
mtklein36352bf2015-03-25 18:17:31 -070086 void onOnceBeforeDraw() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000087 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000088 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
89 makebm(&fTexture[i], gColorTypes[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000090 }
91 }
92
mtklein36352bf2015-03-25 18:17:31 -070093 void onDraw(SkCanvas* canvas) override {
Hal Canarydf2d27e2019-01-08 09:38:02 -050094 SkPaint textPaint;
Mike Kleinea3f0142019-03-20 11:12:10 -050095 SkFont font(ToolUtils::create_portable_typeface(), 12);
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000097 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
98
99 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +0000100
mtkleindbfd7ab2016-09-01 11:24:54 -0700101 const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
mtkleindbfd7ab2016-09-01 11:24:54 -0700103 constexpr bool gFilters[] = { false, true };
104 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
Mike Reedfae8fce2019-04-03 10:27:45 -0400106 constexpr SkTileMode gModes[] = {
107 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
mtkleindbfd7ab2016-09-01 11:24:54 -0700108 static const char* gModeNames[] = { "C", "R", "M" };
reed@android.com1a2fec52009-06-22 02:17:34 +0000109
110 SkScalar y = SkIntToScalar(24);
111 SkScalar x = SkIntToScalar(10);
112
113 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
114 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
115 SkPaint p;
reed@android.com1a2fec52009-06-22 02:17:34 +0000116 p.setDither(true);
Mike Reeddc5863c2018-12-23 23:19:14 -0500117 SkString str;
Mike Kleinea3f0142019-03-20 11:12:10 -0500118 SkFont font(ToolUtils::create_portable_typeface());
reed@android.com1a2fec52009-06-22 02:17:34 +0000119 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
120
Mike Reeddc5863c2018-12-23 23:19:14 -0500121 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, p,
Mike Reed3a42ec02018-10-30 12:53:21 -0400122 SkTextUtils::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@android.com1a2fec52009-06-22 02:17:34 +0000124 x += r.width() * 4 / 3;
125 }
126 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@android.com1a2fec52009-06-22 02:17:34 +0000128 y += SkIntToScalar(16);
129
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000130 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000131 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
132 x = SkIntToScalar(10);
133 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
134 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
135 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000136#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
137 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
138 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000139 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000140 }
141#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000142 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
143 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com1a2fec52009-06-22 02:17:34 +0000145 canvas->save();
146 canvas->translate(x, y);
147 canvas->drawRect(r, paint);
148 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com1a2fec52009-06-22 02:17:34 +0000150 x += r.width() * 4 / 3;
151 }
152 }
Hal Canarydf2d27e2019-01-08 09:38:02 -0500153 canvas->drawString(SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
154 x, y + r.height() * 2 / 3, font, textPaint);
reed@android.com1a2fec52009-06-22 02:17:34 +0000155
156 y += r.height() * 4 / 3;
157 }
158 }
159 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
reed@android.com1a2fec52009-06-22 02:17:34 +0000161private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000162 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000163 typedef skiagm::GM INHERITED;
164};
Mike Reeddfc0e912018-02-16 12:40:18 -0500165DEF_GM( return new TilingGM(true); )
166DEF_GM( return new TilingGM(false); )
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000167
mtkleindbfd7ab2016-09-01 11:24:54 -0700168constexpr int gWidth = 32;
169constexpr int gHeight = 32;
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000170
Mike Reedfae8fce2019-04-03 10:27:45 -0400171static sk_sp<SkShader> make_bm(SkTileMode tx, SkTileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000172 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000173 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
Mike Reed50acf8f2019-04-08 13:20:23 -0400174 return bm.makeShader(tx, ty);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000175}
176
Mike Reedfae8fce2019-04-03 10:27:45 -0400177static sk_sp<SkShader> make_grad(SkTileMode tx, SkTileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000178 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
179 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
180 SkScalar rad = SkIntToScalar(gWidth)/2;
Mike Kleinea3f0142019-03-20 11:12:10 -0500181 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000182
183 int index = (int)ty;
184 switch (index % 3) {
185 case 0:
reed1a9b9642016-03-13 14:13:58 -0700186 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000187 case 1:
reed1a9b9642016-03-13 14:13:58 -0700188 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000189 case 2:
Florin Malita5a9a9812017-08-01 16:38:08 -0400190 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr,
191 SK_ARRAY_COUNT(colors), tx, 135, 225, 0, nullptr);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000192 }
halcanary96fcdcc2015-08-27 07:41:13 -0700193 return nullptr;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000194}
195
Mike Reedfae8fce2019-04-03 10:27:45 -0400196typedef sk_sp<SkShader> (*ShaderProc)(SkTileMode, SkTileMode);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000197
198class Tiling2GM : public skiagm::GM {
199 ShaderProc fProc;
Hal Canary594fe852019-07-18 13:35:49 -0400200 const char* fName;
201
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000202public:
Hal Canary594fe852019-07-18 13:35:49 -0400203 Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000204
Hal Canary594fe852019-07-18 13:35:49 -0400205private:
206 SkString onShortName() override { return SkString(fName); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000207
reed6dc14aa2016-04-11 07:46:38 -0700208 SkISize onISize() override { return SkISize::Make(650, 610); }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000209
mtklein36352bf2015-03-25 18:17:31 -0700210 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000211 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
212
213 const SkScalar w = SkIntToScalar(gWidth);
214 const SkScalar h = SkIntToScalar(gHeight);
215 SkRect r = { -w, -h, w*2, h*2 };
216
Mike Reedfae8fce2019-04-03 10:27:45 -0400217 constexpr SkTileMode gModes[] = {
218 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000219 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700220 const char* gModeNames[] = {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000221 "Clamp", "Repeat", "Mirror"
222 };
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000223
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000224 SkScalar y = SkIntToScalar(24);
225 SkScalar x = SkIntToScalar(66);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000226
Mike Kleinea3f0142019-03-20 11:12:10 -0500227 SkFont font(ToolUtils::create_portable_typeface());
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000228
229 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
230 SkString str(gModeNames[kx]);
Mike Reeddc5863c2018-12-23 23:19:14 -0500231 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
232 SkTextUtils::kCenter_Align);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000233 x += r.width() * 4 / 3;
234 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000235
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000236 y += SkIntToScalar(16) + h;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000237
238 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
239 x = SkIntToScalar(16) + w;
240
241 SkString str(gModeNames[ky]);
Mike Reeddc5863c2018-12-23 23:19:14 -0500242 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(),
243 SkTextUtils::kRight_Align);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000244
245 x += SkIntToScalar(50);
246 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
247 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700248 paint.setShader(fProc(gModes[kx], gModes[ky]));
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000249
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000250 canvas->save();
251 canvas->translate(x, y);
252 canvas->drawRect(r, paint);
253 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000254
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000255 x += r.width() * 4 / 3;
256 }
257 y += r.height() * 4 / 3;
258 }
259 }
reed@android.com1a2fec52009-06-22 02:17:34 +0000260};
Hal Canary594fe852019-07-18 13:35:49 -0400261
262DEF_GM( return new Tiling2GM(make_bm, "tilemode_bitmap"); )
263DEF_GM( return new Tiling2GM(make_grad, "tilemode_gradient"); )
Mike Reeddfc0e912018-02-16 12:40:18 -0500264
265////////////////////
266
Michael Ludwigbe315a22018-12-17 09:50:51 -0500267DEF_SIMPLE_GM(tilemode_decal, canvas, 720, 1100) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500268 auto img = GetResourceAsImage("images/mandrill_128.png");
269 SkPaint bgpaint;
270 bgpaint.setColor(SK_ColorYELLOW);
271
272 SkRect r = { -20, -20, img->width() + 20.0f, img->height() + 20.0f };
Michael Ludwigbe315a22018-12-17 09:50:51 -0500273 canvas->translate(45, 45);
Mike Reeddfc0e912018-02-16 12:40:18 -0500274
Mike Reedfae8fce2019-04-03 10:27:45 -0400275 std::function<void(SkPaint*, SkTileMode, SkTileMode)> shader_procs[] = {
276 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500277 // Test no filtering with decal mode
Mike Reeddfc0e912018-02-16 12:40:18 -0500278 paint->setShader(img->makeShader(tx, ty));
Michael Ludwigbe315a22018-12-17 09:50:51 -0500279 paint->setFilterQuality(kNone_SkFilterQuality);
280 },
Mike Reedfae8fce2019-04-03 10:27:45 -0400281 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500282 // Test bilerp approximation for decal mode (or clamp to border HW)
283 paint->setShader(img->makeShader(tx, ty));
284 paint->setFilterQuality(kLow_SkFilterQuality);
285 },
Mike Reedfae8fce2019-04-03 10:27:45 -0400286 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500287 // Test bicubic filter with decal mode
288 paint->setShader(img->makeShader(tx, ty));
289 paint->setFilterQuality(kHigh_SkFilterQuality);
Mike Reeddfc0e912018-02-16 12:40:18 -0500290 },
Mike Reedfae8fce2019-04-03 10:27:45 -0400291 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500292 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
293 const SkPoint pts[] = {{ 0, 0 }, {img->width()*1.0f, img->height()*1.0f }};
294 const SkScalar* pos = nullptr;
295 const int count = SK_ARRAY_COUNT(colors);
296 paint->setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, tx));
297 },
Mike Reedfae8fce2019-04-03 10:27:45 -0400298 [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) {
Mike Reeddfc0e912018-02-16 12:40:18 -0500299 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
300 const SkScalar* pos = nullptr;
301 const int count = SK_ARRAY_COUNT(colors);
302 paint->setShader(SkGradientShader::MakeRadial({ img->width()*0.5f, img->width()*0.5f },
303 img->width()*0.5f, colors, pos, count, tx));
304 },
305 };
306
307 const struct XY {
Mike Reedfae8fce2019-04-03 10:27:45 -0400308 SkTileMode fX;
309 SkTileMode fY;
Mike Reeddfc0e912018-02-16 12:40:18 -0500310 } pairs[] = {
Mike Reedfae8fce2019-04-03 10:27:45 -0400311 { SkTileMode::kClamp, SkTileMode::kClamp },
312 { SkTileMode::kClamp, SkTileMode::kDecal },
313 { SkTileMode::kDecal, SkTileMode::kClamp },
314 { SkTileMode::kDecal, SkTileMode::kDecal },
Mike Reeddfc0e912018-02-16 12:40:18 -0500315 };
316 for (const auto& p : pairs) {
317 SkPaint paint;
318 canvas->save();
319 for (const auto& proc : shader_procs) {
Michael Ludwigbe315a22018-12-17 09:50:51 -0500320 canvas->save();
321 // Apply a slight rotation to highlight the differences between filtered and unfiltered
322 // decal edges
323 canvas->rotate(4);
Mike Reeddfc0e912018-02-16 12:40:18 -0500324 canvas->drawRect(r, bgpaint);
325 proc(&paint, p.fX, p.fY);
326 canvas->drawRect(r, paint);
Michael Ludwigbe315a22018-12-17 09:50:51 -0500327 canvas->restore();
Mike Reeddfc0e912018-02-16 12:40:18 -0500328 canvas->translate(0, r.height() + 20);
329 }
330 canvas->restore();
331 canvas->translate(r.width() + 10, 0);
332 }
333}
334