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 | |
| 22 | namespace skiagm { |
| 23 | |
| 24 | static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { |
| 25 | bm->setConfig(config, w, h); |
| 26 | bm->allocPixels(); |
| 27 | bm->eraseColor(0); |
| 28 | |
| 29 | SkCanvas canvas(*bm); |
| 30 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} }; |
| 31 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 32 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 33 | SkPaint paint; |
| 34 | |
| 35 | SkUnitMapper* um = NULL; |
| 36 | |
| 37 | um = new SkCosineMapper; |
| 38 | // um = new SkDiscreteMapper(12); |
| 39 | |
| 40 | SkAutoUnref au(um); |
| 41 | |
| 42 | paint.setDither(true); |
| 43 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
| 44 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); |
| 45 | canvas.drawPaint(paint); |
| 46 | } |
| 47 | |
| 48 | static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
| 49 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
| 50 | SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
| 51 | paint->setShader(shader)->unref(); |
| 52 | paint->setFilterBitmap(filter); |
| 53 | } |
| 54 | |
| 55 | static const SkBitmap::Config gConfigs[] = { |
| 56 | SkBitmap::kARGB_8888_Config, |
| 57 | SkBitmap::kRGB_565_Config, |
| 58 | SkBitmap::kARGB_4444_Config |
| 59 | }; |
| 60 | static const int gWidth = 32; |
| 61 | static const int gHeight = 32; |
| 62 | |
| 63 | class TilingGM : public GM { |
| 64 | SkBlurDrawLooper fLooper; |
| 65 | public: |
| 66 | TilingGM() |
| 67 | : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), |
| 68 | 0x88000000) { |
| 69 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
| 70 | makebm(&fTexture[i], gConfigs[i], gWidth, gHeight); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)]; |
| 75 | |
| 76 | protected: |
| 77 | SkString onShortName() { |
| 78 | return SkString("tilemodes"); |
| 79 | } |
| 80 | |
| 81 | SkISize onISize() { return make_isize(880, 560); } |
| 82 | |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 83 | virtual void onDraw(SkCanvas* canvas) { |
reed@android.com | 1a2fec5 | 2009-06-22 02:17:34 +0000 | [diff] [blame] | 84 | |
| 85 | SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; |
| 86 | |
| 87 | static const char* gConfigNames[] = { "8888", "565", "4444" }; |
| 88 | |
| 89 | static const bool gFilters[] = { false, true }; |
| 90 | static const char* gFilterNames[] = { "point", "bilinear" }; |
| 91 | |
| 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); |
| 109 | |
| 110 | x += r.width() * 4 / 3; |
| 111 | } |
| 112 | } |
| 113 | |
| 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); |
| 124 | |
| 125 | canvas->save(); |
| 126 | canvas->translate(x, y); |
| 127 | canvas->drawRect(r, paint); |
| 128 | canvas->restore(); |
| 129 | |
| 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 | } |
| 146 | |
| 147 | private: |
| 148 | typedef GM INHERITED; |
| 149 | }; |
| 150 | |
| 151 | ////////////////////////////////////////////////////////////////////////////// |
| 152 | |
| 153 | static GM* MyFactory(void*) { return new TilingGM; } |
| 154 | static GMRegistry reg(MyFactory); |
| 155 | |
| 156 | } |
| 157 | |