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