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