blob: 88d8c8bfedd02c6d66cf4f8fe4981dfa435f0dba [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 */
humper@google.com3aad3b02013-09-04 19:23:53 +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/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkShader.h"
21#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
23#include "include/core/SkTileMode.h"
24#include "include/core/SkTypeface.h"
25#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/effects/SkGradientShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/utils/SkTextUtils.h"
28#include "tools/ToolUtils.h"
humper@google.com3aad3b02013-09-04 19:23:53 +000029
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000030static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
31 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
humper@google.com3aad3b02013-09-04 19:23:53 +000032 bm->eraseColor(SK_ColorTRANSPARENT);
33
34 SkCanvas canvas(*bm);
35 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
36 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
37 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
38 SkPaint paint;
39
humper@google.com3aad3b02013-09-04 19:23:53 +000040 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070041 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
Mike Reedfae8fce2019-04-03 10:27:45 -040042 SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
humper@google.com3aad3b02013-09-04 19:23:53 +000043 canvas.drawPaint(paint);
44}
45
reed93a12152015-03-16 10:08:34 -070046static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level,
Mike Reedfae8fce2019-04-03 10:27:45 -040047 SkTileMode tmx, SkTileMode tmy) {
Mike Reed50acf8f2019-04-08 13:20:23 -040048 paint->setShader(bm.makeShader(tmx, tmy));
reed93a12152015-03-16 10:08:34 -070049 paint->setFilterQuality(filter_level);
humper@google.com3aad3b02013-09-04 19:23:53 +000050}
51
mtkleindbfd7ab2016-09-01 11:24:54 -070052constexpr SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000053 kN32_SkColorType,
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000054 kRGB_565_SkColorType,
humper@google.com3aad3b02013-09-04 19:23:53 +000055};
56
57class ScaledTilingGM : public skiagm::GM {
humper@google.com3aad3b02013-09-04 19:23:53 +000058public:
59 ScaledTilingGM(bool powerOfTwoSize)
caryclark7aa18992015-07-13 12:08:45 -070060 : fPowerOfTwoSize(powerOfTwoSize) {
humper@google.com3aad3b02013-09-04 19:23:53 +000061 }
62
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000063 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
humper@google.com3aad3b02013-09-04 19:23:53 +000064
65protected:
humper@google.com3aad3b02013-09-04 19:23:53 +000066 enum {
67 kPOTSize = 4,
68 kNPOTSize = 3,
69 };
70
mtklein36352bf2015-03-25 18:17:31 -070071 SkString onShortName() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000072 SkString name("scaled_tilemodes");
73 if (!fPowerOfTwoSize) {
74 name.append("_npot");
75 }
76 return name;
77 }
78
mtklein36352bf2015-03-25 18:17:31 -070079 SkISize onISize() override { return SkISize::Make(880, 760); }
humper@google.com3aad3b02013-09-04 19:23:53 +000080
mtklein36352bf2015-03-25 18:17:31 -070081 void onOnceBeforeDraw() override {
humper@google.com3aad3b02013-09-04 19:23:53 +000082 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000083 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
84 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +000085 }
86 }
87
mtklein36352bf2015-03-25 18:17:31 -070088 void onDraw(SkCanvas* canvas) override {
Hal Canarydf2d27e2019-01-08 09:38:02 -050089 SkPaint textPaint;
Mike Kleinea3f0142019-03-20 11:12:10 -050090 SkFont font(ToolUtils::create_portable_typeface(), 12);
Hal Canarydf2d27e2019-01-08 09:38:02 -050091
humper@google.com3aad3b02013-09-04 19:23:53 +000092 float scale = 32.f/kPOTSize;
93
94 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
95
96 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
97
mtkleindbfd7ab2016-09-01 11:24:54 -070098 const char* gColorTypeNames[] = { "8888" , "565", "4444" };
humper@google.com3aad3b02013-09-04 19:23:53 +000099
mtkleindbfd7ab2016-09-01 11:24:54 -0700100 constexpr SkFilterQuality gFilterQualitys[] =
reed93a12152015-03-16 10:08:34 -0700101 { kNone_SkFilterQuality,
102 kLow_SkFilterQuality,
103 kMedium_SkFilterQuality,
104 kHigh_SkFilterQuality };
mtkleindbfd7ab2016-09-01 11:24:54 -0700105 const char* gFilterNames[] = { "None", "Low", "Medium", "High" };
humper@google.com3aad3b02013-09-04 19:23:53 +0000106
Mike Reedfae8fce2019-04-03 10:27:45 -0400107 constexpr SkTileMode gModes[] = {
108 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
mtkleindbfd7ab2016-09-01 11:24:54 -0700109 const char* gModeNames[] = { "C", "R", "M" };
humper@google.com3aad3b02013-09-04 19:23:53 +0000110
111 SkScalar y = SkIntToScalar(24);
112 SkScalar x = SkIntToScalar(10)/scale;
113
114 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
115 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000116 SkString str;
humper@google.com3aad3b02013-09-04 19:23:53 +0000117 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
118
Mike Reedb579f072019-01-03 15:45:53 -0500119 SkTextUtils::DrawString(canvas, str.c_str(), scale*(x + r.width()/2), y, font, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400120 SkTextUtils::kCenter_Align);
humper@google.com3aad3b02013-09-04 19:23:53 +0000121
122 x += r.width() * 4 / 3;
123 }
124 }
125
126 y = SkIntToScalar(40) / scale;
127
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000128 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
reed93a12152015-03-16 10:08:34 -0700129 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000130 x = SkIntToScalar(10)/scale;
131 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
132 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
133 SkPaint paint;
134#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
135 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
136 if (!fPowerOfTwoSize) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000137 makebm(&fTexture[i], gColorTypes[i], size, size);
humper@google.com3aad3b02013-09-04 19:23:53 +0000138 }
139#endif
reed93a12152015-03-16 10:08:34 -0700140 setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]);
humper@google.com3aad3b02013-09-04 19:23:53 +0000141 paint.setDither(true);
142
143 canvas->save();
144 canvas->scale(scale,scale);
145 canvas->translate(x, y);
146 canvas->drawRect(r, paint);
147 canvas->restore();
148
149 x += r.width() * 4 / 3;
150 }
151 }
Hal Canarydf2d27e2019-01-08 09:38:02 -0500152 canvas->drawString(SkStringPrintf("%s, %s", gColorTypeNames[i], gFilterNames[j]),
153 scale * x, scale * (y + r.height() * 2 / 3), font, textPaint);
humper@google.com3aad3b02013-09-04 19:23:53 +0000154
155 y += r.height() * 4 / 3;
156 }
157 }
158 }
159
160private:
161 bool fPowerOfTwoSize;
162 typedef skiagm::GM INHERITED;
163};
164
mtkleindbfd7ab2016-09-01 11:24:54 -0700165constexpr int gWidth = 32;
166constexpr int gHeight = 32;
humper@google.com3aad3b02013-09-04 19:23:53 +0000167
Mike Reedfae8fce2019-04-03 10:27:45 -0400168static sk_sp<SkShader> make_bm(SkTileMode tx, SkTileMode ty) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000169 SkBitmap bm;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000170 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
Mike Reed50acf8f2019-04-08 13:20:23 -0400171 return bm.makeShader(tx, ty);
humper@google.com3aad3b02013-09-04 19:23:53 +0000172}
173
Mike Reedfae8fce2019-04-03 10:27:45 -0400174static sk_sp<SkShader> make_grad(SkTileMode tx, SkTileMode ty) {
humper@google.com3aad3b02013-09-04 19:23:53 +0000175 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
176 SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 };
177 SkScalar rad = SkIntToScalar(gWidth)/2;
Mike Kleinea3f0142019-03-20 11:12:10 -0500178 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
humper@google.com3aad3b02013-09-04 19:23:53 +0000179
180 int index = (int)ty;
181 switch (index % 3) {
182 case 0:
reed1a9b9642016-03-13 14:13:58 -0700183 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
humper@google.com3aad3b02013-09-04 19:23:53 +0000184 case 1:
reed1a9b9642016-03-13 14:13:58 -0700185 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx);
humper@google.com3aad3b02013-09-04 19:23:53 +0000186 case 2:
reed1a9b9642016-03-13 14:13:58 -0700187 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors));
humper@google.com3aad3b02013-09-04 19:23:53 +0000188 }
189
halcanary96fcdcc2015-08-27 07:41:13 -0700190 return nullptr;
humper@google.com3aad3b02013-09-04 19:23:53 +0000191}
192
Mike Reedfae8fce2019-04-03 10:27:45 -0400193typedef sk_sp<SkShader> (*ShaderProc)(SkTileMode, SkTileMode);
humper@google.com3aad3b02013-09-04 19:23:53 +0000194
195class ScaledTiling2GM : public skiagm::GM {
196 ShaderProc fProc;
Hal Canary594fe852019-07-18 13:35:49 -0400197 const char* fName;
humper@google.com3aad3b02013-09-04 19:23:53 +0000198public:
Hal Canary594fe852019-07-18 13:35:49 -0400199 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
humper@google.com3aad3b02013-09-04 19:23:53 +0000200
Hal Canary594fe852019-07-18 13:35:49 -0400201private:
202 SkString onShortName() override { return SkString(fName); }
humper@google.com3aad3b02013-09-04 19:23:53 +0000203
reed6dc14aa2016-04-11 07:46:38 -0700204 SkISize onISize() override { return SkISize::Make(650, 610); }
humper@google.com3aad3b02013-09-04 19:23:53 +0000205
mtklein36352bf2015-03-25 18:17:31 -0700206 void onDraw(SkCanvas* canvas) override {
humper@google.com3aad3b02013-09-04 19:23:53 +0000207 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
208
209 const SkScalar w = SkIntToScalar(gWidth);
210 const SkScalar h = SkIntToScalar(gHeight);
211 SkRect r = { -w, -h, w*2, h*2 };
212
Mike Reedfae8fce2019-04-03 10:27:45 -0400213 constexpr SkTileMode gModes[] = {
214 SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror
humper@google.com3aad3b02013-09-04 19:23:53 +0000215 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700216 const char* gModeNames[] = {
humper@google.com3aad3b02013-09-04 19:23:53 +0000217 "Clamp", "Repeat", "Mirror"
218 };
219
220 SkScalar y = SkIntToScalar(24);
221 SkScalar x = SkIntToScalar(66);
222
Mike Kleinea3f0142019-03-20 11:12:10 -0500223 SkFont font(ToolUtils::create_portable_typeface());
humper@google.com3aad3b02013-09-04 19:23:53 +0000224
225 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
226 SkString str(gModeNames[kx]);
Mike Reedb579f072019-01-03 15:45:53 -0500227 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
228 SkTextUtils::kCenter_Align);
humper@google.com3aad3b02013-09-04 19:23:53 +0000229 x += r.width() * 4 / 3;
230 }
231
232 y += SkIntToScalar(16) + h;
humper@google.com3aad3b02013-09-04 19:23:53 +0000233
234 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
235 x = SkIntToScalar(16) + w;
236
237 SkString str(gModeNames[ky]);
Mike Reedb579f072019-01-03 15:45:53 -0500238 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), SkTextUtils::kRight_Align);
humper@google.com3aad3b02013-09-04 19:23:53 +0000239
240 x += SkIntToScalar(50);
241 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
242 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700243 paint.setShader(fProc(gModes[kx], gModes[ky]));
humper@google.com3aad3b02013-09-04 19:23:53 +0000244
245 canvas->save();
246 canvas->translate(x, y);
247 canvas->drawRect(r, paint);
248 canvas->restore();
249
250 x += r.width() * 4 / 3;
251 }
252 y += r.height() * 4 / 3;
253 }
254 }
humper@google.com3aad3b02013-09-04 19:23:53 +0000255};
256
257//////////////////////////////////////////////////////////////////////////////
258
259DEF_GM( return new ScaledTilingGM(true); )
260DEF_GM( return new ScaledTilingGM(false); )
Hal Canary594fe852019-07-18 13:35:49 -0400261DEF_GM( return new ScaledTiling2GM(make_bm, "scaled_tilemode_bitmap"); )
262DEF_GM( return new ScaledTiling2GM(make_grad, "scaled_tilemode_gradient"); )