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