blob: 559d9d4914ef6247cbe418a0d0dbedd84906efec [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"
11#include "SkTypeface.h"
12
13// effects
14#include "SkGradientShader.h"
15#include "SkShaderExtras.h"
16#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 {
59 SkBlurDrawLooper fLooper;
60public:
61 TilingView()
62 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
63 0x88000000) {
64 for (int i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
65 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
66 }
67 }
68
69 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
70
71protected:
72 // overrides from SkEventSink
73 virtual bool onQuery(SkEvent* evt) {
74 if (SampleCode::TitleQ(*evt)) {
75 SampleCode::TitleR(evt, "Tiling");
76 return true;
77 }
78 return this->INHERITED::onQuery(evt);
79 }
80
81 void drawBG(SkCanvas* canvas) {
82 canvas->drawColor(SK_ColorWHITE);
83 }
84
85 virtual void onDraw(SkCanvas* canvas) {
86 this->drawBG(canvas);
87
88 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
89
90 static const char* gConfigNames[] = { "8888", "565", "4444" };
91
92 static const bool gFilters[] = { false, true };
93 static const char* gFilterNames[] = { "point", "bilinear" };
94
95 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
96 static const char* gModeNames[] = { "C", "R", "M" };
97
98 SkScalar y = SkIntToScalar(24);
99 SkScalar x = SkIntToScalar(10);
100
101 for (int kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
102 for (int ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
103 SkPaint p;
104 SkString str;
105 p.setAntiAlias(true);
106 p.setDither(true);
107 p.setLooper(&fLooper);
108 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
109
110 p.setTextAlign(SkPaint::kCenter_Align);
111 canvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
112
113 x += r.width() * 4 / 3;
114 }
115 }
116
117 y += SkIntToScalar(16);
118
119 for (int i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
120 for (int j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
121 x = SkIntToScalar(10);
122 for (int kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
123 for (int ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
124 SkPaint paint;
125 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
126 paint.setDither(true);
127
128 canvas->save();
129 canvas->translate(x, y);
130 canvas->drawRect(r, paint);
131 canvas->restore();
132
133 x += r.width() * 4 / 3;
134 }
135 }
136 {
137 SkPaint p;
138 SkString str;
139 p.setAntiAlias(true);
140 p.setLooper(&fLooper);
141 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
142 canvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
143 }
144
145 y += r.height() * 4 / 3;
146 }
147 }
148
149 #ifdef SK_RELEASE
150 this->inval(NULL);
151 #endif
152 }
153
154 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
155 this->inval(NULL);
156 return this->INHERITED::onFindClickHandler(x, y);
157 }
158
159 virtual bool onClick(Click* click) {
160 return this->INHERITED::onClick(click);
161 }
162
163private:
164 typedef SkView INHERITED;
165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169static SkView* MyFactory() { return new TilingView; }
170static SkViewRegister reg(MyFactory);
171