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 | 4b8f802 | 2014-05-21 19:56:46 +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 | |
| 38 | static void setup(SkPaint* paint, const SkBitmap& bm, SkPaint::FilterLevel filter_level, |
| 39 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
| 40 | SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
| 41 | paint->setShader(shader)->unref(); |
| 42 | paint->setFilterLevel(filter_level); |
| 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 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 68 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 69 | if (!fPowerOfTwoSize) { |
| 70 | return kSkipTiled_Flag; // Only for 565. 8888 is fine. |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 75 | SkString onShortName() { |
| 76 | SkString name("scaled_tilemodes"); |
| 77 | if (!fPowerOfTwoSize) { |
| 78 | name.append("_npot"); |
| 79 | } |
| 80 | return name; |
| 81 | } |
| 82 | |
| 83 | SkISize onISize() { return SkISize::Make(880, 760); } |
| 84 | |
| 85 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 86 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 87 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
| 88 | makebm(&fTexture[i], gColorTypes[i], size, size); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
skia.committer@gmail.com | c3723db | 2013-09-05 07:01:19 +0000 | [diff] [blame] | 93 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 94 | float scale = 32.f/kPOTSize; |
| 95 | |
| 96 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
| 97 | |
| 98 | SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; |
| 99 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 100 | static const char* gColorTypeNames[] = { "8888" , "565", "4444" }; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 101 | |
skia.committer@gmail.com | c3723db | 2013-09-05 07:01:19 +0000 | [diff] [blame] | 102 | static const SkPaint::FilterLevel gFilterLevels[] = |
| 103 | { SkPaint::kNone_FilterLevel, |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 104 | SkPaint::kLow_FilterLevel, |
| 105 | SkPaint::kMedium_FilterLevel, |
| 106 | SkPaint::kHigh_FilterLevel }; |
| 107 | static const char* gFilterNames[] = { "None", "Low", "Medium", "High" }; |
| 108 | |
| 109 | static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; |
| 110 | static const char* gModeNames[] = { "C", "R", "M" }; |
| 111 | |
| 112 | SkScalar y = SkIntToScalar(24); |
| 113 | SkScalar x = SkIntToScalar(10)/scale; |
| 114 | |
| 115 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 116 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 117 | SkPaint p; |
| 118 | SkString str; |
| 119 | p.setAntiAlias(true); |
| 120 | p.setDither(true); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 121 | p.setLooper(fLooper); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 122 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 123 | |
| 124 | p.setTextAlign(SkPaint::kCenter_Align); |
| 125 | canvas->drawText(str.c_str(), str.size(), scale*(x + r.width()/2), y, p); |
| 126 | |
| 127 | x += r.width() * 4 / 3; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | y = SkIntToScalar(40) / scale; |
| 132 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 133 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 134 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilterLevels); j++) { |
| 135 | x = SkIntToScalar(10)/scale; |
| 136 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 137 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 138 | SkPaint paint; |
| 139 | #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue |
| 140 | // on SGX where resizing NPOT textures to POT textures exhibits a driver bug. |
| 141 | if (!fPowerOfTwoSize) { |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 142 | makebm(&fTexture[i], gColorTypes[i], size, size); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 143 | } |
| 144 | #endif |
| 145 | setup(&paint, fTexture[i], gFilterLevels[j], gModes[kx], gModes[ky]); |
| 146 | paint.setDither(true); |
| 147 | |
| 148 | canvas->save(); |
| 149 | canvas->scale(scale,scale); |
| 150 | canvas->translate(x, y); |
| 151 | canvas->drawRect(r, paint); |
| 152 | canvas->restore(); |
| 153 | |
| 154 | x += r.width() * 4 / 3; |
| 155 | } |
| 156 | } |
| 157 | { |
| 158 | SkPaint p; |
| 159 | SkString str; |
| 160 | p.setAntiAlias(true); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 161 | p.setLooper(fLooper); |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 162 | str.printf("%s, %s", gColorTypeNames[i], gFilterNames[j]); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 163 | canvas->drawText(str.c_str(), str.size(), scale*x, scale*(y + r.height() * 2 / 3), p); |
| 164 | } |
| 165 | |
| 166 | y += r.height() * 4 / 3; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | bool fPowerOfTwoSize; |
| 173 | typedef skiagm::GM INHERITED; |
| 174 | }; |
| 175 | |
| 176 | static const int gWidth = 32; |
| 177 | static const int gHeight = 32; |
| 178 | |
| 179 | static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 180 | SkBitmap bm; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 181 | makebm(&bm, kN32_SkColorType, gWidth, gHeight); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 182 | return SkShader::CreateBitmapShader(bm, tx, ty); |
| 183 | } |
| 184 | |
| 185 | static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 186 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 187 | SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 188 | SkScalar rad = SkIntToScalar(gWidth)/2; |
| 189 | SkColor colors[] = { 0xFFFF0000, 0xFF0044FF }; |
| 190 | |
| 191 | int index = (int)ty; |
| 192 | switch (index % 3) { |
| 193 | case 0: |
| 194 | return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 195 | case 1: |
| 196 | return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 197 | case 2: |
| 198 | return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors)); |
| 199 | } |
| 200 | |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); |
| 205 | |
| 206 | class ScaledTiling2GM : public skiagm::GM { |
| 207 | ShaderProc fProc; |
| 208 | SkString fName; |
| 209 | public: |
| 210 | ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { |
| 211 | fName.printf("scaled_tilemode_%s", name); |
| 212 | } |
| 213 | |
| 214 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 215 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 216 | return kSkipTiled_Flag; |
| 217 | } |
| 218 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 219 | SkString onShortName() { |
| 220 | return fName; |
| 221 | } |
| 222 | |
| 223 | SkISize onISize() { return SkISize::Make(880, 560); } |
| 224 | |
| 225 | virtual void onDraw(SkCanvas* canvas) { |
| 226 | canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); |
| 227 | |
| 228 | const SkScalar w = SkIntToScalar(gWidth); |
| 229 | const SkScalar h = SkIntToScalar(gHeight); |
| 230 | SkRect r = { -w, -h, w*2, h*2 }; |
| 231 | |
| 232 | static const SkShader::TileMode gModes[] = { |
| 233 | SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
| 234 | }; |
| 235 | static const char* gModeNames[] = { |
| 236 | "Clamp", "Repeat", "Mirror" |
| 237 | }; |
| 238 | |
| 239 | SkScalar y = SkIntToScalar(24); |
| 240 | SkScalar x = SkIntToScalar(66); |
| 241 | |
| 242 | SkPaint p; |
| 243 | p.setAntiAlias(true); |
| 244 | p.setTextAlign(SkPaint::kCenter_Align); |
| 245 | |
| 246 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 247 | SkString str(gModeNames[kx]); |
| 248 | canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); |
| 249 | x += r.width() * 4 / 3; |
| 250 | } |
| 251 | |
| 252 | y += SkIntToScalar(16) + h; |
| 253 | p.setTextAlign(SkPaint::kRight_Align); |
| 254 | |
| 255 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 256 | x = SkIntToScalar(16) + w; |
| 257 | |
| 258 | SkString str(gModeNames[ky]); |
| 259 | canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); |
| 260 | |
| 261 | x += SkIntToScalar(50); |
| 262 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 263 | SkPaint paint; |
| 264 | paint.setShader(fProc(gModes[kx], gModes[ky]))->unref(); |
| 265 | |
| 266 | canvas->save(); |
| 267 | canvas->translate(x, y); |
| 268 | canvas->drawRect(r, paint); |
| 269 | canvas->restore(); |
| 270 | |
| 271 | x += r.width() * 4 / 3; |
| 272 | } |
| 273 | y += r.height() * 4 / 3; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | typedef skiagm::GM INHERITED; |
| 279 | }; |
| 280 | |
| 281 | ////////////////////////////////////////////////////////////////////////////// |
| 282 | |
| 283 | DEF_GM( return new ScaledTilingGM(true); ) |
| 284 | DEF_GM( return new ScaledTilingGM(false); ) |
| 285 | DEF_GM( return new ScaledTiling2GM(make_bm, "bitmap"); ) |
| 286 | DEF_GM( return new ScaledTiling2GM(make_grad, "gradient"); ) |