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(); |
| 50 | paint->setFilterBitmap(filter); |
| 51 | } |
| 52 | |
| 53 | static const SkBitmap::Config gConfigs[] = { |
| 54 | SkBitmap::kARGB_8888_Config, |
| 55 | SkBitmap::kRGB_565_Config, |
| 56 | SkBitmap::kARGB_4444_Config |
| 57 | }; |
| 58 | static const int gWidth = 32; |
| 59 | static const int gHeight = 32; |
| 60 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 61 | class TilingGM : public skiagm::GM { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 62 | SkBlurDrawLooper fLooper; |
| 63 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | TilingGM() |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 65 | : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), |
| 66 | 0x88000000) { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 71 | protected: |
| 72 | SkString onShortName() { |
| 73 | return SkString("tilemodes"); |
| 74 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 75 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 76 | SkISize onISize() { return SkISize::Make(880, 560); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 78 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 79 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
| 80 | makebm(&fTexture[i], gConfigs[i], gWidth, gHeight); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 86 | SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; |
| 87 | |
| 88 | static const char* gConfigNames[] = { "8888", "565", "4444" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 90 | static const bool gFilters[] = { false, true }; |
| 91 | static const char* gFilterNames[] = { "point", "bilinear" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 93 | static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; |
| 94 | static const char* gModeNames[] = { "C", "R", "M" }; |
| 95 | |
| 96 | SkScalar y = SkIntToScalar(24); |
| 97 | SkScalar x = SkIntToScalar(10); |
| 98 | |
| 99 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 100 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 101 | SkPaint p; |
| 102 | SkString str; |
| 103 | p.setAntiAlias(true); |
| 104 | p.setDither(true); |
| 105 | p.setLooper(&fLooper); |
| 106 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 107 | |
| 108 | p.setTextAlign(SkPaint::kCenter_Align); |
| 109 | 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] | 110 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 111 | x += r.width() * 4 / 3; |
| 112 | } |
| 113 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 115 | y += SkIntToScalar(16); |
| 116 | |
| 117 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
| 118 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { |
| 119 | x = SkIntToScalar(10); |
| 120 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 121 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 122 | SkPaint paint; |
| 123 | setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 124 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 126 | canvas->save(); |
| 127 | canvas->translate(x, y); |
| 128 | canvas->drawRect(r, paint); |
| 129 | canvas->restore(); |
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 | x += r.width() * 4 / 3; |
| 132 | } |
| 133 | } |
| 134 | { |
| 135 | SkPaint p; |
| 136 | SkString str; |
| 137 | p.setAntiAlias(true); |
| 138 | p.setLooper(&fLooper); |
| 139 | str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); |
| 140 | canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p); |
| 141 | } |
| 142 | |
| 143 | y += r.height() * 4 / 3; |
| 144 | } |
| 145 | } |
| 146 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 147 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 148 | private: |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 149 | typedef skiagm::GM INHERITED; |
| 150 | }; |
| 151 | |
| 152 | static SkShader* make_bm(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 153 | SkBitmap bm; |
| 154 | makebm(&bm, SkBitmap::kARGB_8888_Config, gWidth, gHeight); |
| 155 | return SkShader::CreateBitmapShader(bm, tx, ty); |
| 156 | } |
| 157 | |
| 158 | static SkShader* make_grad(SkShader::TileMode tx, SkShader::TileMode ty) { |
| 159 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} }; |
| 160 | SkPoint center = { SkIntToScalar(gWidth)/2, SkIntToScalar(gHeight)/2 }; |
| 161 | SkScalar rad = SkIntToScalar(gWidth)/2; |
| 162 | SkColor colors[] = { 0xFFFF0000, 0xFF0044FF }; |
| 163 | |
| 164 | int index = (int)ty; |
| 165 | switch (index % 3) { |
| 166 | case 0: |
| 167 | return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 168 | case 1: |
| 169 | return SkGradientShader::CreateRadial(center, rad, colors, NULL, SK_ARRAY_COUNT(colors), tx); |
| 170 | case 2: |
| 171 | return SkGradientShader::CreateSweep(center.fX, center.fY, colors, NULL, SK_ARRAY_COUNT(colors)); |
| 172 | } |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 173 | |
| 174 | return NULL; |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 175 | } |
| 176 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 177 | typedef SkShader* (*ShaderProc)(SkShader::TileMode, SkShader::TileMode); |
| 178 | |
| 179 | class Tiling2GM : public skiagm::GM { |
| 180 | ShaderProc fProc; |
| 181 | SkString fName; |
| 182 | public: |
| 183 | Tiling2GM(ShaderProc proc, const char name[]) : fProc(proc) { |
| 184 | fName.printf("tilemode_%s", name); |
| 185 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 186 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 187 | protected: |
| 188 | SkString onShortName() { |
| 189 | return fName; |
| 190 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 191 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 192 | SkISize onISize() { return SkISize::Make(880, 560); } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 193 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 194 | virtual void onDraw(SkCanvas* canvas) { |
| 195 | canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); |
| 196 | |
| 197 | const SkScalar w = SkIntToScalar(gWidth); |
| 198 | const SkScalar h = SkIntToScalar(gHeight); |
| 199 | SkRect r = { -w, -h, w*2, h*2 }; |
| 200 | |
| 201 | static const SkShader::TileMode gModes[] = { |
| 202 | SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
| 203 | }; |
| 204 | static const char* gModeNames[] = { |
| 205 | "Clamp", "Repeat", "Mirror" |
| 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 | SkScalar y = SkIntToScalar(24); |
| 209 | SkScalar x = SkIntToScalar(66); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 210 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 211 | SkPaint p; |
| 212 | p.setAntiAlias(true); |
| 213 | p.setTextAlign(SkPaint::kCenter_Align); |
| 214 | |
| 215 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 216 | SkString str(gModeNames[kx]); |
| 217 | canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); |
| 218 | x += r.width() * 4 / 3; |
| 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 | y += SkIntToScalar(16) + h; |
| 222 | p.setTextAlign(SkPaint::kRight_Align); |
| 223 | |
| 224 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
| 225 | x = SkIntToScalar(16) + w; |
| 226 | |
| 227 | SkString str(gModeNames[ky]); |
| 228 | canvas->drawText(str.c_str(), str.size(), x, y + h/2, p); |
| 229 | |
| 230 | x += SkIntToScalar(50); |
| 231 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 232 | SkPaint paint; |
| 233 | paint.setShader(fProc(gModes[kx], gModes[ky]))->unref(); |
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 | canvas->save(); |
| 236 | canvas->translate(x, y); |
| 237 | canvas->drawRect(r, paint); |
| 238 | canvas->restore(); |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 239 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 240 | x += r.width() * 4 / 3; |
| 241 | } |
| 242 | y += r.height() * 4 / 3; |
| 243 | } |
| 244 | } |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 245 | |
mike@reedtribe.org | a059169 | 2012-10-18 02:01:59 +0000 | [diff] [blame] | 246 | private: |
| 247 | typedef skiagm::GM INHERITED; |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | ////////////////////////////////////////////////////////////////////////////// |
| 251 | |
reed@google.com | 3934200 | 2013-02-22 17:28:16 +0000 | [diff] [blame] | 252 | DEF_GM( return new TilingGM; ) |
| 253 | DEF_GM( return new Tiling2GM(make_bm, "bitmap"); ) |
| 254 | DEF_GM( return new Tiling2GM(make_grad, "gradient"); ) |