blob: b1f6d14984353ed327084e15ece7106083fa4f90 [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"
mtklein7a1f45f2016-08-04 06:19:33 -07008#include "SkColorFilter.h"
9#include "SkMaskFilter.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000010#include "SkPath.h"
11#include "SkRegion.h"
12#include "SkShader.h"
13#include "SkUtils.h"
mtklein7a1f45f2016-08-04 06:19:33 -070014
reed@android.com1a2fec52009-06-22 02:17:34 +000015
16// effects
17#include "SkGradientShader.h"
reed@android.com1a2fec52009-06-22 02:17:34 +000018#include "SkBlurDrawLooper.h"
19
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000020static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
21 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000022 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
reed@android.com1a2fec52009-06-22 02:17:34 +000024 SkCanvas canvas(*bm);
25 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
26 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
27 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
28 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
reed@android.com1a2fec52009-06-22 02:17:34 +000030 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070031 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors),
32 SkShader::kClamp_TileMode));
reed@android.com1a2fec52009-06-22 02:17:34 +000033 canvas.drawPaint(paint);
34}
35
36static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
37 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed1a9b9642016-03-13 14:13:58 -070038 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070039 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com1a2fec52009-06-22 02:17:34 +000040}
41
mtkleindbfd7ab2016-09-01 11:24:54 -070042constexpr SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000043 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000044 kRGB_565_SkColorType,
reed@android.com1a2fec52009-06-22 02:17:34 +000045};
reed@android.com1a2fec52009-06-22 02:17:34 +000046
mike@reedtribe.orga0591692012-10-18 02:01:59 +000047class TilingGM : public skiagm::GM {
reed@android.com1a2fec52009-06-22 02:17:34 +000048public:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000049 TilingGM(bool powerOfTwoSize)
robertphillips@google.com6db2ae22013-08-30 12:41:42 +000050 : fPowerOfTwoSize(powerOfTwoSize) {
reed@android.com1a2fec52009-06-22 02:17:34 +000051 }
52
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000053 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.com1a2fec52009-06-22 02:17:34 +000055protected:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000056
57 enum {
58 kPOTSize = 32,
59 kNPOTSize = 21,
60 };
61
mtklein36352bf2015-03-25 18:17:31 -070062 SkString onShortName() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000063 SkString name("tilemodes");
64 if (!fPowerOfTwoSize) {
65 name.append("_npot");
66 }
67 return name;
reed@android.com1a2fec52009-06-22 02:17:34 +000068 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
mtklein36352bf2015-03-25 18:17:31 -070070 SkISize onISize() override { return SkISize::Make(880, 560); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
mtklein36352bf2015-03-25 18:17:31 -070072 void onOnceBeforeDraw() override {
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000073 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000074 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
75 makebm(&fTexture[i], gColorTypes[i], size, size);
reed@google.com7775d662012-11-27 15:15:58 +000076 }
77 }
78
mtklein36352bf2015-03-25 18:17:31 -070079 void onDraw(SkCanvas* canvas) override {
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
commit-bot@chromium.org37799e12013-07-25 17:52:32 +000081 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
82
83 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
reed@android.com1a2fec52009-06-22 02:17:34 +000084
mtkleindbfd7ab2016-09-01 11:24:54 -070085 const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
mtkleindbfd7ab2016-09-01 11:24:54 -070087 constexpr bool gFilters[] = { false, true };
88 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
mtkleindbfd7ab2016-09-01 11:24:54 -070090 constexpr SkShader::TileMode gModes[] = {
91 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
92 static const char* gModeNames[] = { "C", "R", "M" };
reed@android.com1a2fec52009-06-22 02:17:34 +000093
94 SkScalar y = SkIntToScalar(24);
95 SkScalar x = SkIntToScalar(10);
96
97 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
98 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
99 SkPaint p;
100 SkString str;
101 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700102 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000103 p.setDither(true);
reed@android.com1a2fec52009-06-22 02:17:34 +0000104 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
105
106 p.setTextAlign(SkPaint::kCenter_Align);
107 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@android.com1a2fec52009-06-22 02:17:34 +0000109 x += r.width() * 4 / 3;
110 }
111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@android.com1a2fec52009-06-22 02:17:34 +0000113 y += SkIntToScalar(16);
114
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000115 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed@android.com1a2fec52009-06-22 02:17:34 +0000116 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
117 x = SkIntToScalar(10);
118 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
119 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
120 SkPaint paint;
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000121#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
122 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
123 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000124 makebm(&fTexture[i], gColorTypes[i], size, size);
bsalomon@google.comab3c6782013-08-06 20:47:52 +0000125 }
126#endif
reed@android.com1a2fec52009-06-22 02:17:34 +0000127 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
128 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com1a2fec52009-06-22 02:17:34 +0000130 canvas->save();
131 canvas->translate(x, y);
132 canvas->drawRect(r, paint);
133 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@android.com1a2fec52009-06-22 02:17:34 +0000135 x += r.width() * 4 / 3;
136 }
137 }
138 {
139 SkPaint p;
140 SkString str;
141 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700142 sk_tool_utils::set_portable_typeface(&p);
reed@android.com1a2fec52009-06-22 02:17:34 +0000143 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
144 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
145 }
146
147 y += r.height() * 4 / 3;
148 }
149 }
150 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.com1a2fec52009-06-22 02:17:34 +0000152private:
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000153 bool fPowerOfTwoSize;
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000154 typedef skiagm::GM INHERITED;
155};
156
mtkleindbfd7ab2016-09-01 11:24:54 -0700157constexpr int gWidth = 32;
158constexpr int gHeight = 32;
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000159
reed1a9b9642016-03-13 14:13:58 -0700160static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000161 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000162 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
reed1a9b9642016-03-13 14:13:58 -0700163 return SkShader::MakeBitmapShader(bm, tx, ty);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000164}
165
reed1a9b9642016-03-13 14:13:58 -0700166static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000167 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
168 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
169 SkScalar rad = SkIntToScalar(gWidth)/2;
caryclark1e545b62015-07-13 08:19:58 -0700170 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000171
172 int index = (int)ty;
173 switch (index % 3) {
174 case 0:
reed1a9b9642016-03-13 14:13:58 -0700175 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000176 case 1:
reed1a9b9642016-03-13 14:13:58 -0700177 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000178 case 2:
reed1a9b9642016-03-13 14:13:58 -0700179 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
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@reedtribe.orga0591692012-10-18 02:01:59 +0000219 SkPaint p;
220 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700221 sk_tool_utils::set_portable_typeface(&p);
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000222 p.setTextAlign(SkPaint::kCenter_Align);
223
224 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
225 SkString str(gModeNames[kx]);
226 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
227 x += r.width() * 4 / 3;
228 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000229
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000230 y += SkIntToScalar(16) + h;
231 p.setTextAlign(SkPaint::kRight_Align);
232
233 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
234 x = SkIntToScalar(16) + w;
235
236 SkString str(gModeNames[ky]);
237 canvas->drawText(str.c_str(), str.size(), x, y + h/2, p);
238
239 x += SkIntToScalar(50);
240 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
241 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700242 paint.setShader(fProc(gModes[kx], gModes[ky]));
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000243
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000244 canvas->save();
245 canvas->translate(x, y);
246 canvas->drawRect(r, paint);
247 canvas->restore();
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000248
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000249 x += r.width() * 4 / 3;
250 }
251 y += r.height() * 4 / 3;
252 }
253 }
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000254
mike@reedtribe.orga0591692012-10-18 02:01:59 +0000255private:
256 typedef skiagm::GM INHERITED;
reed@android.com1a2fec52009-06-22 02:17:34 +0000257};
258
259//////////////////////////////////////////////////////////////////////////////
260
commit-bot@chromium.org37799e12013-07-25 17:52:32 +0000261DEF_GM( return new TilingGM(true); )
262DEF_GM( return new TilingGM(false); )
reed@google.com39342002013-02-22 17:28:16 +0000263DEF_GM( return new Tiling2GM(make_bm, "bitmap"); )
264DEF_GM( return new Tiling2GM(make_grad, "gradient"); )