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
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12
13/*
14 *  Want to ensure that our bitmap sampler (in bitmap shader) keeps plenty of
15 *  precision when scaling very large images (where the dx might get very small.
16 */
17
18#define W   257
19#define H   161
20
21class GiantBitmapGM : public skiagm::GM {
22    SkBitmap* fBM;
23    SkShader::TileMode fMode;
24    bool fDoFilter;
25    bool fDoRotate;
26
27    const SkBitmap& getBitmap() {
28        if (NULL == fBM) {
29            fBM = new SkBitmap;
30            fBM->allocN32Pixels(W, H);
31            fBM->eraseColor(SK_ColorWHITE);
32
33            const SkColor colors[] = {
34                SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
35            };
36
37            SkCanvas canvas(*fBM);
38            SkPaint paint;
39            paint.setAntiAlias(true);
40            paint.setStrokeWidth(SkIntToScalar(20));
41
42#if 0
43            for (int y = -H*2; y < H; y += 50) {
44                SkScalar yy = SkIntToScalar(y);
45                paint.setColor(colors[y/50 & 0x3]);
46                canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
47                                paint);
48            }
49#else
50            for (int x = -W; x < W; x += 60) {
51                paint.setColor(colors[x/60 & 0x3]);
52
53                SkScalar xx = SkIntToScalar(x);
54                canvas.drawLine(xx, 0, xx, SkIntToScalar(H),
55                                paint);
56            }
57#endif
58        }
59        return *fBM;
60    }
61
62public:
63    GiantBitmapGM(SkShader::TileMode mode, bool doFilter, bool doRotate) : fBM(NULL) {
64        fMode = mode;
65        fDoFilter = doFilter;
66        fDoRotate = doRotate;
67    }
68
69    virtual ~GiantBitmapGM() {
70        SkDELETE(fBM);
71    }
72
73protected:
74    virtual uint32_t onGetFlags() const SK_OVERRIDE {
75        if (fDoFilter && fDoRotate && fMode != SkShader::kClamp_TileMode) {
76            return kSkipTiled_Flag;
77        }
78        return 0;
79    }
80
81    virtual SkString onShortName() {
82        SkString str("giantbitmap_");
83        switch (fMode) {
84            case SkShader::kClamp_TileMode:
85                str.append("clamp");
86                break;
87            case SkShader::kRepeat_TileMode:
88                str.append("repeat");
89                break;
90            case SkShader::kMirror_TileMode:
91                str.append("mirror");
92                break;
93            default:
94                break;
95        }
96        str.append(fDoFilter ? "_bilerp" : "_point");
97        str.append(fDoRotate ? "_rotate" : "_scale");
98        return str;
99    }
100
101    virtual SkISize onISize() { return SkISize::Make(640, 480); }
102
103    virtual void onDraw(SkCanvas* canvas) {
104        SkPaint paint;
105
106        SkMatrix m;
107        if (fDoRotate) {
108//            m.setRotate(SkIntToScalar(30), 0, 0);
109            m.setSkew(SK_Scalar1, 0, 0, 0);
110//            m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
111        } else {
112            SkScalar scale = 11*SK_Scalar1/12;
113            m.setScale(scale, scale);
114        }
115        SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode, &m);
116
117        paint.setShader(s)->unref();
118        paint.setFilterLevel(fDoFilter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
119
120        canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
121
122//        SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16);
123//        canvas->drawRect(r, paint); return;
124        canvas->drawPaint(paint);
125    }
126
127private:
128    typedef GM INHERITED;
129};
130
131///////////////////////////////////////////////////////////////////////////////
132
133static skiagm::GM* G000(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); }
134static skiagm::GM* G100(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); }
135static skiagm::GM* G200(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); }
136static skiagm::GM* G010(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); }
137static skiagm::GM* G110(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); }
138static skiagm::GM* G210(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); }
139
140static skiagm::GM* G001(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); }
141static skiagm::GM* G101(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); }
142static skiagm::GM* G201(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); }
143static skiagm::GM* G011(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); }
144static skiagm::GM* G111(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); }
145static skiagm::GM* G211(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); }
146
147static skiagm::GMRegistry reg000(G000);
148static skiagm::GMRegistry reg100(G100);
149static skiagm::GMRegistry reg200(G200);
150static skiagm::GMRegistry reg010(G010);
151static skiagm::GMRegistry reg110(G110);
152static skiagm::GMRegistry reg210(G210);
153
154static skiagm::GMRegistry reg001(G001);
155static skiagm::GMRegistry reg101(G101);
156static skiagm::GMRegistry reg201(G201);
157static skiagm::GMRegistry reg011(G011);
158static skiagm::GMRegistry reg111(G111);
159static skiagm::GMRegistry reg211(G211);
160