epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 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" |
| 16 | |
| 17 | // effects |
| 18 | #include "SkGradientShader.h" |
| 19 | #include "SkUnitMappers.h" |
| 20 | #include "SkBlurDrawLooper.h" |
| 21 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 22 | static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { |
| 23 | bm->setConfig(config, w, h); |
| 24 | bm->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 25 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 26 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 27 | SkCanvas canvas(*bm); |
| 28 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; |
| 29 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 30 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 31 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 32 | |
| 33 | SkUnitMapper* um = NULL; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 34 | |
| 35 | um = new SkCosineMapper; |
| 36 | // um = new SkDiscreteMapper(12); |
| 37 | |
| 38 | SkAutoUnref au(um); |
| 39 | |
| 40 | paint.setDither(true); |
| 41 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
| 42 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); |
| 43 | canvas.drawPaint(paint); |
| 44 | } |
| 45 | |
| 46 | static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
| 47 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
| 48 | SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
| 49 | paint->setShader(shader)->unref(); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 50 | paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static const SkBitmap::Config gConfigs[] = { |
| 54 | SkBitmap::kARGB_8888_Config, |
| 55 | SkBitmap::kRGB_565_Config, |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 56 | }; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 57 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 58 | class TilingGM : public skiagm::GM { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 59 | public: |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 60 | TilingGM(bool powerOfTwoSize) |
robertphillips@google.com | 6db2ae2 | 2013-08-30 12:41:42 +0000 | [diff] [blame] | 61 | : fPowerOfTwoSize(powerOfTwoSize) { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 66 | protected: |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 67 | |
| 68 | enum { |
| 69 | kPOTSize = 32, |
| 70 | kNPOTSize = 21, |
| 71 | }; |
| 72 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 73 | SkString onShortName() { |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 74 | SkString name("tilemodes"); |
| 75 | if (!fPowerOfTwoSize) { |
| 76 | name.append("_npot"); |
| 77 | } |
| 78 | return name; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 79 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 81 | SkISize onISize() { return SkISize::Make(880, 560); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 83 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 84 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 85 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 86 | makebm(&fTexture[i], gConfigs[i], size, size); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 92 | int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize; |
| 93 | |
| 94 | SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) }; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 95 | |
| 96 | static const char* gConfigNames[] = { "8888", "565", "4444" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 97 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 98 | static const bool gFilters[] = { false, true }; |
| 99 | static const char* gFilterNames[] = { "point", "bilinear" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 100 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 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); |
| 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); |
| 112 | p.setDither(true); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 113 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 114 | |
| 115 | p.setTextAlign(SkPaint::kCenter_Align); |
| 116 | canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 117 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 118 | x += r.width() * 4 / 3; |
| 119 | } |
| 120 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 121 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 122 | y += SkIntToScalar(16); |
| 123 | |
| 124 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
| 125 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { |
| 126 | x = SkIntToScalar(10); |
| 127 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 128 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 129 | SkPaint paint; |
bsalomon@google.com | ab3c678 | 2013-08-06 20:47:52 +0000 | [diff] [blame] | 130 | #if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue |
| 131 | // on SGX where resizing NPOT textures to POT textures exhibits a driver bug. |
| 132 | if (!fPowerOfTwoSize) { |
| 133 | makebm(&fTexture[i], gConfigs[i], size, size); |
| 134 | } |
| 135 | #endif |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 136 | setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 137 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 139 | canvas->save(); |
| 140 | canvas->translate(x, y); |
| 141 | canvas->drawRect(r, paint); |
| 142 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 143 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 144 | x += r.width() * 4 / 3; |
| 145 | } |
| 146 | } |
| 147 | { |
| 148 | SkPaint p; |
| 149 | SkString str; |
| 150 | p.setAntiAlias(true); |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 151 | str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); |
| 152 | canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p); |
| 153 | } |
| 154 | |
| 155 | y += r.height() * 4 / 3; |
| 156 | } |
| 157 | } |
| 158 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 160 | private: |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 161 | bool fPowerOfTwoSize; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 162 | typedef skiagm::GM INHERITED; |
| 163 | }; |
| 164 | |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 165 | static const int gWidth = 32; |
| 166 | static const int gHeight = 32; |
| 167 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 168 | static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 169 | SkBitmap bm; |
| 170 | makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight); |
| 171 | return SkShader::CreateBitmapShader(bm, tx, ty); |
| 172 | } |
| 173 | |
| 174 | static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 175 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 176 | SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 177 | SkScalar rad = SkIntToScalar(gWidth)/2; |
| 178 | SkColor colors[] = { 0xFFFF0000, 0xFF0044FF }; |
| 179 | |
| 180 | int index = (int)ty; |
| 181 | switch (index % 3) { |
| 182 | case 0: |
| 183 | return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 184 | case 1: |
| 185 | return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 186 | case 2: |
| 187 | return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors)); |
| 188 | } |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 189 | |
| 190 | return NULL; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 191 | } |
| 192 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 193 | typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); |
| 194 | |
| 195 | class Tiling2GM : public skiagm::GM { |
| 196 | ShaderProc fProc; |
| 197 | SkString fName; |
| 198 | public: |
| 199 | Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { |
| 200 | fName.printf("tilemode_%s", name); |
| 201 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 202 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 203 | protected: |
| 204 | SkString onShortName() { |
| 205 | return fName; |
| 206 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 207 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 208 | SkISize onISize() { return SkISize::Make(880, 560); } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 209 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 210 | virtual void onDraw(SkCanvas* canvas) { |
| 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 | |
| 217 | static const SkShader::TileMode gModes[] = { |
| 218 | SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
| 219 | }; |
| 220 | static const char* gModeNames[] = { |
| 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@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 227 | SkPaint p; |
| 228 | p.setAntiAlias(true); |
| 229 | p.setTextAlign(SkPaint::kCenter_Align); |
| 230 | |
| 231 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 232 | SkString str(gModeNames[kx]); |
| 233 | canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); |
| 234 | x += r.width() * 4 / 3; |
| 235 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 236 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 237 | y += SkIntToScalar(16) + h; |
| 238 | p.setTextAlign(SkPaint::kRight_Align); |
| 239 | |
| 240 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 241 | x = SkIntToScalar(16) + w; |
| 242 | |
| 243 | SkString str(gModeNames[ky]); |
| 244 | canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); |
| 245 | |
| 246 | x += SkIntToScalar(50); |
| 247 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 248 | SkPaint paint; |
| 249 | paint.setShader(fProc(gModes[kx], gModes[ky]))->unref(); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 250 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 251 | canvas->save(); |
| 252 | canvas->translate(x, y); |
| 253 | canvas->drawRect(r, paint); |
| 254 | canvas->restore(); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 255 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 256 | x += r.width() * 4 / 3; |
| 257 | } |
| 258 | y += r.height() * 4 / 3; |
| 259 | } |
| 260 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 261 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 262 | private: |
| 263 | typedef skiagm::GM INHERITED; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | ////////////////////////////////////////////////////////////////////////////// |
| 267 | |
commit-bot@chromium.org | 37799e1 | 2013-07-25 17:52:32 +0000 | [diff] [blame] | 268 | DEF_GM( return new TilingGM(true); ) |
| 269 | DEF_GM( return new TilingGM(false); ) |
reed@google.com | 3934200 | 2013-02-22 17:28:16 +0000 | [diff] [blame] | 270 | DEF_GM( return new Tiling2GM(make_bm, "bitmap"); ) |
| 271 | DEF_GM( return new Tiling2GM(make_grad, "gradient"); ) |