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