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