SampleTiling.cpp revision f21833999d90ad9c8c584cff3238797b39cfc00b
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 "SkPicture.h"
12#include "SkTypeface.h"
13
14// effects
15#include "SkGradientShader.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 SampleView {
59    SkPicture           fTextPicture;
60    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    virtual void onDrawContent(SkCanvas* canvas) {
83        SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
84
85        static const char* gConfigNames[] = { "8888", "565", "4444" };
86
87        static const bool           gFilters[] = { false, true };
88        static const char*          gFilterNames[] = {     "point",                     "bilinear" };
89
90        static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
91        static const char*          gModeNames[] = {    "C",                    "R",                   "M" };
92
93        SkScalar y = SkIntToScalar(24);
94        SkScalar x = SkIntToScalar(10);
95
96        SkCanvas* textCanvas = NULL;
97        if (fTextPicture.width() == 0) {
98            textCanvas = fTextPicture.beginRecording(1000, 1000);
99        }
100
101        if (textCanvas) {
102            for (int kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
103                for (int ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
104                    SkPaint p;
105                    SkString str;
106                    p.setAntiAlias(true);
107                    p.setDither(true);
108                    p.setLooper(&fLooper);
109                    str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
110
111                    p.setTextAlign(SkPaint::kCenter_Align);
112                    textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
113
114                    x += r.width() * 4 / 3;
115                }
116            }
117        }
118
119        y += SkIntToScalar(16);
120
121        for (int i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
122            for (int j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
123                x = SkIntToScalar(10);
124                for (int kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
125                    for (int ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
126                        SkPaint paint;
127                        setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
128                        paint.setDither(true);
129
130                        canvas->save();
131                        canvas->translate(x, y);
132                        canvas->drawRect(r, paint);
133                        canvas->restore();
134
135                        x += r.width() * 4 / 3;
136                    }
137                }
138                if (textCanvas) {
139                    SkPaint p;
140                    SkString str;
141                    p.setAntiAlias(true);
142                    p.setLooper(&fLooper);
143                    str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
144                    textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
145                }
146
147                y += r.height() * 4 / 3;
148            }
149        }
150
151        canvas->drawPicture(fTextPicture);
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 SampleView INHERITED;
165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169static SkView* MyFactory() { return new TilingView; }
170static SkViewRegister reg(MyFactory);
171
172