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