SampleTiling.cpp revision da342a891779e0839a645d6634a2f0100bf2c0d6
1#include "SampleCode.h" 2#include "SkView.h" 3#include "SkCanvas.h" 4#include "SkPaint.h" 5#include "SkPath.h" 6#include "SkRegion.h" 7#include "SkShader.h" 8#include "SkUtils.h" 9#include "SkColorPriv.h" 10#include "SkColorFilter.h" 11#include "SkTypeface.h" 12 13// effects 14#include "SkGradientShader.h" 15#include "SkUnitMappers.h" 16#include "SkBlurDrawLooper.h" 17 18static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { 19 bm->setConfig(config, w, h); 20 bm->allocPixels(); 21 bm->eraseColor(0); 22 23 SkCanvas canvas(*bm); 24 SkPoint pts[] = { 0, 0, SkIntToScalar(w), SkIntToScalar(h) }; 25 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; 26 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; 27 SkPaint paint; 28 29 SkUnitMapper* um = NULL; 30 31 um = new SkCosineMapper; 32// um = new SkDiscreteMapper(12); 33 34 SkAutoUnref au(um); 35 36 paint.setDither(true); 37 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, 38 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); 39 canvas.drawPaint(paint); 40} 41 42static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, 43 SkShader::TileMode tmx, SkShader::TileMode tmy) { 44 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); 45 paint->setShader(shader)->unref(); 46 paint->setFilterBitmap(filter); 47} 48 49static const SkBitmap::Config gConfigs[] = { 50 SkBitmap::kARGB_8888_Config, 51 SkBitmap::kRGB_565_Config, 52 SkBitmap::kARGB_4444_Config 53}; 54static const int gWidth = 32; 55static const int gHeight = 32; 56 57class TilingView : public SkView { 58 SkBlurDrawLooper fLooper; 59public: 60 TilingView() 61 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2), 62 0x88000000) { 63 for (int i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { 64 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight); 65 } 66 } 67 68 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)]; 69 70protected: 71 // overrides from SkEventSink 72 virtual bool onQuery(SkEvent* evt) { 73 if (SampleCode::TitleQ(*evt)) { 74 SampleCode::TitleR(evt, "Tiling"); 75 return true; 76 } 77 return this->INHERITED::onQuery(evt); 78 } 79 80 void drawBG(SkCanvas* canvas) { 81 canvas->drawColor(SK_ColorWHITE); 82 } 83 84 virtual void onDraw(SkCanvas* canvas) { 85 this->drawBG(canvas); 86 87 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; 88 89 static const char* gConfigNames[] = { "8888", "565", "4444" }; 90 91 static const bool gFilters[] = { false, true }; 92 static const char* gFilterNames[] = { "point", "bilinear" }; 93 94 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; 95 static const char* gModeNames[] = { "C", "R", "M" }; 96 97 SkScalar y = SkIntToScalar(24); 98 SkScalar x = SkIntToScalar(10); 99 100 for (int kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { 101 for (int ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { 102 SkPaint p; 103 SkString str; 104 p.setAntiAlias(true); 105 p.setDither(true); 106 p.setLooper(&fLooper); 107 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); 108 109 p.setTextAlign(SkPaint::kCenter_Align); 110 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); 111 112 x += r.width() * 4 / 3; 113 } 114 } 115 116 y += SkIntToScalar(16); 117 118 for (int i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { 119 for (int j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { 120 x = SkIntToScalar(10); 121 for (int kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { 122 for (int ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { 123 SkPaint paint; 124 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); 125 paint.setDither(true); 126 127 canvas->save(); 128 canvas->translate(x, y); 129 canvas->drawRect(r, paint); 130 canvas->restore(); 131 132 x += r.width() * 4 / 3; 133 } 134 } 135 { 136 SkPaint p; 137 SkString str; 138 p.setAntiAlias(true); 139 p.setLooper(&fLooper); 140 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); 141 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p); 142 } 143 144 y += r.height() * 4 / 3; 145 } 146 } 147 148 #ifdef SK_RELEASE 149 this->inval(NULL); 150 #endif 151 } 152 153 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { 154 this->inval(NULL); 155 return this->INHERITED::onFindClickHandler(x, y); 156 } 157 158 virtual bool onClick(Click* click) { 159 return this->INHERITED::onClick(click); 160 } 161 162private: 163 typedef SkView INHERITED; 164}; 165 166////////////////////////////////////////////////////////////////////////////// 167 168static SkView* MyFactory() { return new TilingView; } 169static SkViewRegister reg(MyFactory); 170 171