blob: 3833fa29436dbb233294dfdc41c8af25af4ed88f [file] [log] [blame]
humper@google.com3aad3b02013-09-04 19:23:53 +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 */
7#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
mtklein7a1f45f2016-08-04 06:19:33 -07009#include "SkColorFilter.h"
10#include "SkMaskFilter.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000011#include "SkPath.h"
12#include "SkRegion.h"
13#include "SkShader.h"
14#include "SkUtils.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000015
16// effects
17#include "SkGradientShader.h"
humper@google.com3aad3b02013-09-04 19:23:53 +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));
humper@google.com3aad3b02013-09-04 19:23:53 +000022 bm->eraseColor(SK_ColorTRANSPARENT);
23
24 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;
29
humper@google.com3aad3b02013-09-04 19:23:53 +000030 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070031 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
32 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
humper@google.com3aad3b02013-09-04 19:23:53 +000033 canvas.drawPaint(paint);
34}
35
reed93a12152015-03-16 10:08:34 -070036static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level,
humper@google.com3aad3b02013-09-04 19:23:53 +000037 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_level);
humper@google.com3aad3b02013-09-04 19:23:53 +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,
humper@google.com3aad3b02013-09-04 19:23:53 +000045};
46
47class ScaledTilingGM : public skiagm::GM {
humper@google.com3aad3b02013-09-04 19:23:53 +000048public:
49 ScaledTilingGM(bool powerOfTwoSize)
caryclark7aa18992015-07-13 12:08:45 -070050 : fPowerOfTwoSize(powerOfTwoSize) {
humper@google.com3aad3b02013-09-04 19:23:53 +000051 }
52
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000053 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
humper@google.com3aad3b02013-09-04 19:23:53 +000054
55protected:
humper@google.com3aad3b02013-09-04 19:23:53 +000056 enum {
57 kPOTSize = 4,
58 kNPOTSize = 3,
59 };
60
mtklein36352bf2015-03-25 18:17:31 -070061 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000062 SkString name("scaled_tilemodes");
63 if (!fPowerOfTwoSize) {
64 name.append("_npot");
65 }
66 return name;
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 SkISize onISize() override { return SkISize::Make(880, 760); }
humper@google.com3aad3b02013-09-04 19:23:53 +000070
mtklein36352bf2015-03-25 18:17:31 -070071 void onOnceBeforeDraw() override {
humper@google.com3aad3b02013-09-04 19:23:53 +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);
humper@google.com3aad3b02013-09-04 19:23:53 +000075 }
76 }
77
mtklein36352bf2015-03-25 18:17:31 -070078 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +000079 float scale = 32.f/kPOTSize;
80
81 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
82
83 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
84
mtkleindbfd7ab2016-09-01 11:24:54 -070085 const char* gColorTypeNames[] = { "8888" , "565", "4444" };
humper@google.com3aad3b02013-09-04 19:23:53 +000086
mtkleindbfd7ab2016-09-01 11:24:54 -070087 constexpr SkFilterQuality gFilterQualitys[] =
reed93a12152015-03-16 10:08:34 -070088 { kNone_SkFilterQuality,
89 kLow_SkFilterQuality,
90 kMedium_SkFilterQuality,
91 kHigh_SkFilterQuality };
mtkleindbfd7ab2016-09-01 11:24:54 -070092 const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
humper@google.com3aad3b02013-09-04 19:23:53 +000093
mtkleindbfd7ab2016-09-01 11:24:54 -070094 constexpr SkShader::TileMode gModes[] = {
95 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
96 const char* gModeNames[] = { "C", "R", "M" };
humper@google.com3aad3b02013-09-04 19:23:53 +000097
98 SkScalar y = SkIntToScalar(24);
99 SkScalar x = SkIntToScalar(10)/scale;
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;
104 SkString str;
105 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700106 sk_tool_utils::set_portable_typeface(&p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000107 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
108
109 p.setTextAlign(SkPaint::kCenter_Align);
Cary Clark2a475ea2017-04-28 15:35:12 -0400110 canvas->drawString(str, scale*(x + r.width()/2), y, p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000111
112 x += r.width() * 4 / 3;
113 }
114 }
115
116 y = SkIntToScalar(40) / scale;
117
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000118 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed93a12152015-03-16 10:08:34 -0700119 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000120 x = SkIntToScalar(10)/scale;
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;
124#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);
humper@google.com3aad3b02013-09-04 19:23:53 +0000128 }
129#endif
reed93a12152015-03-16 10:08:34 -0700130 setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000131 paint.setDither(true);
132
133 canvas->save();
134 canvas->scale(scale,scale);
135 canvas->translate(x, y);
136 canvas->drawRect(r, paint);
137 canvas->restore();
138
139 x += r.width() * 4 / 3;
140 }
141 }
142 {
143 SkPaint p;
144 SkString str;
145 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700146 sk_tool_utils::set_portable_typeface(&p);
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000147 str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400148 canvas->drawString(str, scale*x, scale*(y + r.height() * 2 / 3), p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000149 }
150
151 y += r.height() * 4 / 3;
152 }
153 }
154 }
155
156private:
157 bool fPowerOfTwoSize;
158 typedef skiagm::GM INHERITED;
159};
160
mtkleindbfd7ab2016-09-01 11:24:54 -0700161constexpr int gWidth = 32;
162constexpr int gHeight = 32;
humper@google.com3aad3b02013-09-04 19:23:53 +0000163
reed1a9b9642016-03-13 14:13:58 -0700164static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000165 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000166 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
reed1a9b9642016-03-13 14:13:58 -0700167 return SkShader::MakeBitmapShader(bm, tx, ty);
humper@google.com3aad3b02013-09-04 19:23:53 +0000168}
169
reed1a9b9642016-03-13 14:13:58 -0700170static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000171 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
172 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
173 SkScalar rad = SkIntToScalar(gWidth)/2;
caryclark7aa18992015-07-13 12:08:45 -0700174 SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) };
humper@google.com3aad3b02013-09-04 19:23:53 +0000175
176 int index = (int)ty;
177 switch (index % 3) {
178 case 0:
reed1a9b9642016-03-13 14:13:58 -0700179 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
humper@google.com3aad3b02013-09-04 19:23:53 +0000180 case 1:
reed1a9b9642016-03-13 14:13:58 -0700181 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
humper@google.com3aad3b02013-09-04 19:23:53 +0000182 case 2:
reed1a9b9642016-03-13 14:13:58 -0700183 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
humper@google.com3aad3b02013-09-04 19:23:53 +0000184 }
185
halcanary96fcdcc2015-08-27 07:41:13 -0700186 return nullptr;
humper@google.com3aad3b02013-09-04 19:23:53 +0000187}
188
reed1a9b9642016-03-13 14:13:58 -0700189typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode);
humper@google.com3aad3b02013-09-04 19:23:53 +0000190
191class ScaledTiling2GM : public skiagm::GM {
192 ShaderProc fProc;
193 SkString fName;
194public:
195 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) {
196 fName.printf("scaled_tilemode_%s", name);
197 }
198
199protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000200
mtklein36352bf2015-03-25 18:17:31 -0700201 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000202 return fName;
203 }
204
reed6dc14aa2016-04-11 07:46:38 -0700205 SkISize onISize() override { return SkISize::Make(650, 610); }
humper@google.com3aad3b02013-09-04 19:23:53 +0000206
mtklein36352bf2015-03-25 18:17:31 -0700207 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000208 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
209
210 const SkScalar w = SkIntToScalar(gWidth);
211 const SkScalar h = SkIntToScalar(gHeight);
212 SkRect r = { -w, -h, w*2, h*2 };
213
mtkleindbfd7ab2016-09-01 11:24:54 -0700214 constexpr SkShader::TileMode gModes[] = {
humper@google.com3aad3b02013-09-04 19:23:53 +0000215 SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
216 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700217 const char* gModeNames[] = {
humper@google.com3aad3b02013-09-04 19:23:53 +0000218 "Clamp", "Repeat", "Mirror"
219 };
220
221 SkScalar y = SkIntToScalar(24);
222 SkScalar x = SkIntToScalar(66);
223
224 SkPaint p;
225 p.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700226 sk_tool_utils::set_portable_typeface(&p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000227 p.setTextAlign(SkPaint::kCenter_Align);
228
229 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
230 SkString str(gModeNames[kx]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400231 canvas->drawString(str, x + r.width()/2, y, p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000232 x += r.width() * 4 / 3;
233 }
234
235 y += SkIntToScalar(16) + h;
236 p.setTextAlign(SkPaint::kRight_Align);
237
238 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
239 x = SkIntToScalar(16) + w;
240
241 SkString str(gModeNames[ky]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400242 canvas->drawString(str, x, y + h/2, p);
humper@google.com3aad3b02013-09-04 19:23:53 +0000243
244 x += SkIntToScalar(50);
245 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
246 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700247 paint.setShader(fProc(gModes[kx], gModes[ky]));
humper@google.com3aad3b02013-09-04 19:23:53 +0000248
249 canvas->save();
250 canvas->translate(x, y);
251 canvas->drawRect(r, paint);
252 canvas->restore();
253
254 x += r.width() * 4 / 3;
255 }
256 y += r.height() * 4 / 3;
257 }
258 }
259
260private:
261 typedef skiagm::GM INHERITED;
262};
263
264//////////////////////////////////////////////////////////////////////////////
265
266DEF_GM( return new ScaledTilingGM(true); )
267DEF_GM( return new ScaledTilingGM(false); )
268DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); )
269DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); )