humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +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 | */ |
| 7 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
mtklein | 7a1f45f | 2016-08-04 06:19:33 -0700 | [diff] [blame] | 9 | #include "SkColorFilter.h" |
| 10 | #include "SkMaskFilter.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
| 12 | #include "SkRegion.h" |
| 13 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 14 | #include "SkUTF.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 15 | |
| 16 | // effects |
| 17 | #include "SkGradientShader.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 18 | #include "SkBlurDrawLooper.h" |
| 19 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 20 | static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
| 21 | bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 22 | 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.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 30 | paint.setDither(true); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 31 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, |
| 32 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 33 | canvas.drawPaint(paint); |
| 34 | } |
| 35 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 36 | static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 37 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 38 | paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy)); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 39 | paint->setFilterQuality(filter_level); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 40 | } |
| 41 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 42 | constexpr SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 43 | kN32_SkColorType, |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 44 | kRGB_565_SkColorType, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class ScaledTilingGM : public skiagm::GM { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 48 | public: |
| 49 | ScaledTilingGM(bool powerOfTwoSize) |
caryclark | 7aa1899 | 2015-07-13 12:08:45 -0700 | [diff] [blame] | 50 | : fPowerOfTwoSize(powerOfTwoSize) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 51 | } |
| 52 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 53 | SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 54 | |
| 55 | protected: |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 56 | enum { |
| 57 | kPOTSize = 4, |
| 58 | kNPOTSize = 3, |
| 59 | }; |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | SkString onShortName() override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 62 | SkString name("scaled_tilemodes"); |
| 63 | if (!fPowerOfTwoSize) { |
| 64 | name.append("_npot"); |
| 65 | } |
| 66 | return name; |
| 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | SkISize onISize() override { return SkISize::Make(880, 760); } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void onOnceBeforeDraw() override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 72 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 73 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
| 74 | makebm(&fTexture[i], gColorTypes[i], size, size); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void onDraw(SkCanvas* canvas) override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 79 | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 85 | const char* gColorTypeNames[] = { "8888" , "565", "4444" }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 86 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 87 | constexpr SkFilterQuality gFilterQualitys[] = |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 88 | { kNone_SkFilterQuality, |
| 89 | kLow_SkFilterQuality, |
| 90 | kMedium_SkFilterQuality, |
| 91 | kHigh_SkFilterQuality }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 92 | const char* gFilterNames[] = { "None", "Low", "Medium", "High" }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +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 | const char* gModeNames[] = { "C", "R", "M" }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 97 | |
| 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); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 106 | sk_tool_utils::set_portable_typeface(&p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 107 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 108 | |
| 109 | p.setTextAlign(SkPaint::kCenter_Align); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 110 | canvas->drawString(str, scale*(x + r.width()/2), y, p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 111 | |
| 112 | x += r.width() * 4 / 3; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | y = SkIntToScalar(40) / scale; |
| 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 | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 119 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 120 | 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.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 127 | makebm(&fTexture[i], gColorTypes[i], size, size); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 128 | } |
| 129 | #endif |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 130 | setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 131 | 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); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 146 | sk_tool_utils::set_portable_typeface(&p); |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 147 | str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 148 | canvas->drawString(str, scale*x, scale*(y + r.height() * 2 / 3), p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | y += r.height() * 4 / 3; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | bool fPowerOfTwoSize; |
| 158 | typedef skiagm::GM INHERITED; |
| 159 | }; |
| 160 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 161 | constexpr int gWidth = 32; |
| 162 | constexpr int gHeight = 32; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 163 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 164 | static sk_sp<SkShader> make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 165 | SkBitmap bm; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 166 | makebm(&bm, kN32_SkColorType, gWidth, gHeight); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 167 | return SkShader::MakeBitmapShader(bm, tx, ty); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 168 | } |
| 169 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 170 | static sk_sp<SkShader> make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 171 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 172 | SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 173 | SkScalar rad = SkIntToScalar(gWidth)/2; |
caryclark | 7aa1899 | 2015-07-13 12:08:45 -0700 | [diff] [blame] | 174 | SkColor colors[] = { 0xFFFF0000, sk_tool_utils::color_to_565(0xFF0044FF) }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 175 | |
| 176 | int index = (int)ty; |
| 177 | switch (index % 3) { |
| 178 | case 0: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 179 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), tx); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 180 | case 1: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 181 | return SkGradientShader::MakeRadial(center, rad, colors, nullptr, SK_ARRAY_COUNT(colors), tx); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 182 | case 2: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 183 | return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr, SK_ARRAY_COUNT(colors)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 184 | } |
| 185 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | return nullptr; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 187 | } |
| 188 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 189 | typedef sk_sp<SkShader> (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 190 | |
| 191 | class ScaledTiling2GM : public skiagm::GM { |
| 192 | ShaderProc fProc; |
| 193 | SkString fName; |
| 194 | public: |
| 195 | ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { |
| 196 | fName.printf("scaled_tilemode_%s", name); |
| 197 | } |
| 198 | |
| 199 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 200 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 201 | SkString onShortName() override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 202 | return fName; |
| 203 | } |
| 204 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 205 | SkISize onISize() override { return SkISize::Make(650, 610); } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 206 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 207 | void onDraw(SkCanvas* canvas) override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 208 | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 214 | constexpr SkShader::TileMode gModes[] = { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 215 | SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
| 216 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 217 | const char* gModeNames[] = { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 218 | "Clamp", "Repeat", "Mirror" |
| 219 | }; |
| 220 | |
| 221 | SkScalar y = SkIntToScalar(24); |
| 222 | SkScalar x = SkIntToScalar(66); |
| 223 | |
| 224 | SkPaint p; |
| 225 | p.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 226 | sk_tool_utils::set_portable_typeface(&p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 227 | p.setTextAlign(SkPaint::kCenter_Align); |
| 228 | |
| 229 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 230 | SkString str(gModeNames[kx]); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 231 | canvas->drawString(str, x + r.width()/2, y, p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 232 | 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 Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 242 | canvas->drawString(str, x, y + h/2, p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 243 | |
| 244 | x += SkIntToScalar(50); |
| 245 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 246 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 247 | paint.setShader(fProc(gModes[kx], gModes[ky])); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 248 | |
| 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 | |
| 260 | private: |
| 261 | typedef skiagm::GM INHERITED; |
| 262 | }; |
| 263 | |
| 264 | ////////////////////////////////////////////////////////////////////////////// |
| 265 | |
| 266 | DEF_GM( return new ScaledTilingGM(true); ) |
| 267 | DEF_GM( return new ScaledTilingGM(false); ) |
| 268 | DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); ) |
| 269 | DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); ) |