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