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