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