epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 7 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 8 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "tools/Resources.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "tools/ToolUtils.h" |
| 31 | |
| 32 | #include <functional> |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 33 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 34 | static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
| 35 | bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 36 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 37 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 38 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 44 | paint.setDither(true); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 45 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 46 | SkTileMode::kClamp)); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 47 | canvas.drawPaint(paint); |
| 48 | } |
| 49 | |
| 50 | static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 51 | SkTileMode tmx, SkTileMode tmy) { |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 52 | paint->setShader(bm.makeShader(tmx, tmy)); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 53 | paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 56 | constexpr SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 57 | kN32_SkColorType, |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 58 | kRGB_565_SkColorType, |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 59 | }; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 60 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 61 | class TilingGM : public skiagm::GM { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 62 | public: |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 63 | TilingGM(bool powerOfTwoSize) |
robertphillips@google.com | 6db2ae2 | 2013-08-30 12:41:42 +0000 | [diff] [blame] | 64 | : fPowerOfTwoSize(powerOfTwoSize) { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 65 | } |
| 66 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 67 | SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 69 | protected: |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 70 | |
| 71 | enum { |
| 72 | kPOTSize = 32, |
| 73 | kNPOTSize = 21, |
| 74 | }; |
| 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | SkString onShortName() override { |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 77 | SkString name("tilemodes"); |
| 78 | if (!fPowerOfTwoSize) { |
| 79 | name.append("_npot"); |
| 80 | } |
| 81 | return name; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 82 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | SkISize onISize() override { return SkISize::Make(880, 560); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 87 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 88 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
| 89 | makebm(&fTexture[i], gColorTypes[i], size, size); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | void onDraw(SkCanvas* canvas) override { |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 94 | SkPaint textPaint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 95 | SkFont font(ToolUtils::create_portable_typeface(), 12); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 97 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
| 98 | |
| 99 | SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 100 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 101 | const char* gConfigNames[] = { "8888", "565", "4444" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 103 | constexpr bool gFilters[] = { false, true }; |
| 104 | static const char* gFilterNames[] = { "point", "bilinear" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 106 | constexpr SkTileMode gModes[] = { |
| 107 | SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 108 | static const char* gModeNames[] = { "C", "R", "M" }; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 109 | |
| 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.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 116 | p.setDither(true); |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 117 | SkString str; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 118 | SkFont font(ToolUtils::create_portable_typeface()); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 119 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 120 | |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 121 | SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, p, |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 122 | SkTextUtils::kCenter_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 123 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 124 | x += r.width() * 4 / 3; |
| 125 | } |
| 126 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 127 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 128 | y += SkIntToScalar(16); |
| 129 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 130 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 131 | 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.com | ab3c678 | 2013-08-06 20:47:52 +0000 | [diff] [blame] | 136 | #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.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 139 | makebm(&fTexture[i], gColorTypes[i], size, size); |
bsalomon@google.com | ab3c678 | 2013-08-06 20:47:52 +0000 | [diff] [blame] | 140 | } |
| 141 | #endif |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 142 | setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 143 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 145 | canvas->save(); |
| 146 | canvas->translate(x, y); |
| 147 | canvas->drawRect(r, paint); |
| 148 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 149 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 150 | x += r.width() * 4 / 3; |
| 151 | } |
| 152 | } |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 153 | canvas->drawString(SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]), |
| 154 | x, y + r.height() * 2 / 3, font, textPaint); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 155 | |
| 156 | y += r.height() * 4 / 3; |
| 157 | } |
| 158 | } |
| 159 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 160 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 161 | private: |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 162 | bool fPowerOfTwoSize; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 163 | using INHERITED = skiagm::GM; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 164 | }; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 165 | DEF_GM( return new TilingGM(true); ) |
| 166 | DEF_GM( return new TilingGM(false); ) |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 167 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 168 | constexpr int gWidth = 32; |
| 169 | constexpr int gHeight = 32; |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 170 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 171 | static sk_sp<SkShader> make_bm(SkTileMode tx, SkTileMode ty) { |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 172 | SkBitmap bm; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 173 | makebm(&bm, kN32_SkColorType, gWidth, gHeight); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 174 | return bm.makeShader(tx, ty); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 177 | static sk_sp<SkShader> make_grad(SkTileMode tx, SkTileMode ty) { |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 178 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 179 | SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 180 | SkScalar rad = SkIntToScalar(gWidth)/2; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 181 | SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)}; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 182 | |
| 183 | int index = (int)ty; |
| 184 | switch (index % 3) { |
| 185 | case 0: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 186 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 187 | case 1: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 188 | return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 189 | case 2: |
Florin Malita | 5a9a981 | 2017-08-01 16:38:08 -0400 | [diff] [blame] | 190 | return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, |
| 191 | SK_ARRAY_COUNT(colors), tx, 135, 225, 0, nullptr); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 192 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 193 | return nullptr; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 196 | typedef sk_sp<SkShader> (*ShaderProc)(SkTileMode, SkTileMode); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 197 | |
| 198 | class Tiling2GM : public skiagm::GM { |
| 199 | ShaderProc fProc; |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 200 | const char* fName; |
| 201 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 202 | public: |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 203 | Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {} |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 204 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 205 | private: |
| 206 | SkString onShortName() override { return SkString(fName); } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 207 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 208 | SkISize onISize() override { return SkISize::Make(650, 610); } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 209 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 210 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 211 | 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 217 | constexpr SkTileMode gModes[] = { |
| 218 | SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 219 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 220 | const char* gModeNames[] = { |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 221 | "Clamp", "Repeat", "Mirror" |
| 222 | }; |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 223 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 224 | SkScalar y = SkIntToScalar(24); |
| 225 | SkScalar x = SkIntToScalar(66); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 226 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 227 | SkFont font(ToolUtils::create_portable_typeface()); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 228 | |
| 229 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 230 | SkString str(gModeNames[kx]); |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 231 | SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(), |
| 232 | SkTextUtils::kCenter_Align); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 233 | x += r.width() * 4 / 3; |
| 234 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 235 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 236 | y += SkIntToScalar(16) + h; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 237 | |
| 238 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 239 | x = SkIntToScalar(16) + w; |
| 240 | |
| 241 | SkString str(gModeNames[ky]); |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 242 | SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), |
| 243 | SkTextUtils::kRight_Align); |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 244 | |
| 245 | x += SkIntToScalar(50); |
| 246 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 247 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 248 | paint.setShader(fProc(gModes[kx], gModes[ky])); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 249 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 250 | canvas->save(); |
| 251 | canvas->translate(x, y); |
| 252 | canvas->drawRect(r, paint); |
| 253 | canvas->restore(); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 254 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 255 | x += r.width() * 4 / 3; |
| 256 | } |
| 257 | y += r.height() * 4 / 3; |
| 258 | } |
| 259 | } |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 260 | }; |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 261 | |
| 262 | DEF_GM( return new Tiling2GM(make_bm, "tilemode_bitmap"); ) |
| 263 | DEF_GM( return new Tiling2GM(make_grad, "tilemode_gradient"); ) |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 264 | |
| 265 | //////////////////// |
| 266 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 267 | DEF_SIMPLE_GM(tilemode_decal, canvas, 720, 1100) { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 268 | 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 Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 273 | canvas->translate(45, 45); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 274 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 275 | std::function<void(SkPaint*, SkTileMode, SkTileMode)> shader_procs[] = { |
| 276 | [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 277 | // Test no filtering with decal mode |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 278 | paint->setShader(img->makeShader(tx, ty)); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 279 | paint->setFilterQuality(kNone_SkFilterQuality); |
| 280 | }, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 281 | [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 282 | // 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 286 | [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 287 | // Test bicubic filter with decal mode |
| 288 | paint->setShader(img->makeShader(tx, ty)); |
| 289 | paint->setFilterQuality(kHigh_SkFilterQuality); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 290 | }, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 291 | [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 292 | 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 298 | [img](SkPaint* paint, SkTileMode tx, SkTileMode ty) { |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 299 | 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 308 | SkTileMode fX; |
| 309 | SkTileMode fY; |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 310 | } pairs[] = { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 311 | { SkTileMode::kClamp, SkTileMode::kClamp }, |
| 312 | { SkTileMode::kClamp, SkTileMode::kDecal }, |
| 313 | { SkTileMode::kDecal, SkTileMode::kClamp }, |
| 314 | { SkTileMode::kDecal, SkTileMode::kDecal }, |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 315 | }; |
| 316 | for (const auto& p : pairs) { |
| 317 | SkPaint paint; |
| 318 | canvas->save(); |
| 319 | for (const auto& proc : shader_procs) { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 320 | canvas->save(); |
| 321 | // Apply a slight rotation to highlight the differences between filtered and unfiltered |
| 322 | // decal edges |
| 323 | canvas->rotate(4); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 324 | canvas->drawRect(r, bgpaint); |
| 325 | proc(&paint, p.fX, p.fY); |
| 326 | canvas->drawRect(r, paint); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 327 | canvas->restore(); |
Mike Reed | dfc0e91 | 2018-02-16 12:40:18 -0500 | [diff] [blame] | 328 | canvas->translate(0, r.height() + 20); |
| 329 | } |
| 330 | canvas->restore(); |
| 331 | canvas->translate(r.width() + 10, 0); |
| 332 | } |
| 333 | } |
| 334 | |