humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkRegion.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkUtils.h" |
| 13 | #include "SkColorPriv.h" |
| 14 | #include "SkColorFilter.h" |
| 15 | #include "SkTypeface.h" |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 16 | #include "SkBlurMask.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 17 | |
| 18 | // effects |
| 19 | #include "SkGradientShader.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 20 | #include "SkBlurDrawLooper.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)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 24 | bm->eraseColor(SK_ColorTRANSPARENT); |
| 25 | |
| 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; |
| 31 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 32 | paint.setDither(true); |
| 33 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 34 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref(); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 35 | canvas.drawPaint(paint); |
| 36 | } |
| 37 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 38 | static void setup(SkPaint* paint, const SkBitmap& bm, SkFilterQuality filter_level, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 39 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
| 40 | SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
| 41 | paint->setShader(shader)->unref(); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 42 | paint->setFilterQuality(filter_level); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 43 | } |
| 44 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 45 | static const SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 46 | kN32_SkColorType, |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 47 | kRGB_565_SkColorType, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | class ScaledTilingGM : public skiagm::GM { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 51 | SkAutoTUnref<SkBlurDrawLooper> fLooper; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 52 | public: |
| 53 | ScaledTilingGM(bool powerOfTwoSize) |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 54 | : fLooper(SkBlurDrawLooper::Create(0x88000000, |
| 55 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), |
| 56 | SkIntToScalar(2), SkIntToScalar(2))) |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 57 | , fPowerOfTwoSize(powerOfTwoSize) { |
| 58 | } |
| 59 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 60 | SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 61 | |
| 62 | protected: |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 63 | enum { |
| 64 | kPOTSize = 4, |
| 65 | kNPOTSize = 3, |
| 66 | }; |
| 67 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | SkString onShortName() override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 69 | SkString name("scaled_tilemodes"); |
| 70 | if (!fPowerOfTwoSize) { |
| 71 | name.append("_npot"); |
| 72 | } |
| 73 | return name; |
| 74 | } |
| 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | SkISize onISize() override { return SkISize::Make(880, 760); } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void onOnceBeforeDraw() override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 79 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 80 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
| 81 | makebm(&fTexture[i], gColorTypes[i], size, size); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void onDraw(SkCanvas* canvas) override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 86 | float scale = 32.f/kPOTSize; |
| 87 | |
| 88 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
| 89 | |
| 90 | SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; |
| 91 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 92 | static const char* gColorTypeNames[] = { "8888" , "565", "4444" }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 93 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 94 | static const SkFilterQuality gFilterQualitys[] = |
| 95 | { kNone_SkFilterQuality, |
| 96 | kLow_SkFilterQuality, |
| 97 | kMedium_SkFilterQuality, |
| 98 | kHigh_SkFilterQuality }; |
| 99 | static const char* gFilterNames[] = { "None", "Low", "Medium", "High" }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 100 | |
| 101 | static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; |
| 102 | static const char* gModeNames[] = { "C", "R", "M" }; |
| 103 | |
| 104 | SkScalar y = SkIntToScalar(24); |
| 105 | SkScalar x = SkIntToScalar(10)/scale; |
| 106 | |
| 107 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 108 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 109 | SkPaint p; |
| 110 | SkString str; |
| 111 | p.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 112 | sk_tool_utils::set_portable_typeface(&p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 113 | p.setDither(true); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 114 | p.setLooper(fLooper); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 115 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 116 | |
| 117 | p.setTextAlign(SkPaint::kCenter_Align); |
| 118 | canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p); |
| 119 | |
| 120 | x += r.width() * 4 / 3; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | y = SkIntToScalar(40) / scale; |
| 125 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 127 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterQualitys); j++) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 128 | x = SkIntToScalar(10)/scale; |
| 129 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 130 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 131 | SkPaint paint; |
| 132 | #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue |
| 133 | // on SGX where resizing NPOT textures to POT textures exhibits a driver bug. |
| 134 | if (!fPowerOfTwoSize) { |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 135 | makebm(&fTexture[i], gColorTypes[i], size, size); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 136 | } |
| 137 | #endif |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 138 | setup(&paint, fTexture[i], gFilterQualitys[j], gModes[kx], gModes[ky]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 139 | paint.setDither(true); |
| 140 | |
| 141 | canvas->save(); |
| 142 | canvas->scale(scale,scale); |
| 143 | canvas->translate(x, y); |
| 144 | canvas->drawRect(r, paint); |
| 145 | canvas->restore(); |
| 146 | |
| 147 | x += r.width() * 4 / 3; |
| 148 | } |
| 149 | } |
| 150 | { |
| 151 | SkPaint p; |
| 152 | SkString str; |
| 153 | p.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 154 | sk_tool_utils::set_portable_typeface(&p); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 155 | p.setLooper(fLooper); |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 156 | str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 157 | canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p); |
| 158 | } |
| 159 | |
| 160 | y += r.height() * 4 / 3; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private: |
| 166 | bool fPowerOfTwoSize; |
| 167 | typedef skiagm::GM INHERITED; |
| 168 | }; |
| 169 | |
| 170 | static const int gWidth = 32; |
| 171 | static const int gHeight = 32; |
| 172 | |
| 173 | static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 174 | SkBitmap bm; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 175 | makebm(&bm, kN32_SkColorType, gWidth, gHeight); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 176 | return SkShader::CreateBitmapShader(bm, tx, ty); |
| 177 | } |
| 178 | |
| 179 | static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 180 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 181 | SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 182 | SkScalar rad = SkIntToScalar(gWidth)/2; |
| 183 | SkColor colors[] = { 0xFFFF0000, 0xFF0044FF }; |
| 184 | |
| 185 | int index = (int)ty; |
| 186 | switch (index % 3) { |
| 187 | case 0: |
| 188 | return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 189 | case 1: |
| 190 | return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 191 | case 2: |
| 192 | return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors)); |
| 193 | } |
| 194 | |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); |
| 199 | |
| 200 | class ScaledTiling2GM : public skiagm::GM { |
| 201 | ShaderProc fProc; |
| 202 | SkString fName; |
| 203 | public: |
| 204 | ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { |
| 205 | fName.printf("scaled_tilemode_%s", name); |
| 206 | } |
| 207 | |
| 208 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 209 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 210 | SkString onShortName() override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 211 | return fName; |
| 212 | } |
| 213 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 214 | SkISize onISize() override { return SkISize::Make(880, 560); } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 215 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 216 | void onDraw(SkCanvas* canvas) override { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 217 | canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); |
| 218 | |
| 219 | const SkScalar w = SkIntToScalar(gWidth); |
| 220 | const SkScalar h = SkIntToScalar(gHeight); |
| 221 | SkRect r = { -w, -h, w*2, h*2 }; |
| 222 | |
| 223 | static const SkShader::TileMode gModes[] = { |
| 224 | SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
| 225 | }; |
| 226 | static const char* gModeNames[] = { |
| 227 | "Clamp", "Repeat", "Mirror" |
| 228 | }; |
| 229 | |
| 230 | SkScalar y = SkIntToScalar(24); |
| 231 | SkScalar x = SkIntToScalar(66); |
| 232 | |
| 233 | SkPaint p; |
| 234 | p.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 235 | sk_tool_utils::set_portable_typeface(&p); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 236 | p.setTextAlign(SkPaint::kCenter_Align); |
| 237 | |
| 238 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 239 | SkString str(gModeNames[kx]); |
| 240 | canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); |
| 241 | x += r.width() * 4 / 3; |
| 242 | } |
| 243 | |
| 244 | y += SkIntToScalar(16) + h; |
| 245 | p.setTextAlign(SkPaint::kRight_Align); |
| 246 | |
| 247 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 248 | x = SkIntToScalar(16) + w; |
| 249 | |
| 250 | SkString str(gModeNames[ky]); |
| 251 | canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); |
| 252 | |
| 253 | x += SkIntToScalar(50); |
| 254 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 255 | SkPaint paint; |
| 256 | paint.setShader(fProc(gModes[kx], gModes[ky]))->unref(); |
| 257 | |
| 258 | canvas->save(); |
| 259 | canvas->translate(x, y); |
| 260 | canvas->drawRect(r, paint); |
| 261 | canvas->restore(); |
| 262 | |
| 263 | x += r.width() * 4 / 3; |
| 264 | } |
| 265 | y += r.height() * 4 / 3; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private: |
| 270 | typedef skiagm::GM INHERITED; |
| 271 | }; |
| 272 | |
| 273 | ////////////////////////////////////////////////////////////////////////////// |
| 274 | |
| 275 | DEF_GM( return new ScaledTilingGM(true); ) |
| 276 | DEF_GM( return new ScaledTilingGM(false); ) |
| 277 | DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); ) |
| 278 | DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); ) |