giantbitmap.cpp revision 584493e6533259a6c2bccbb4fd983e2342b9fac3
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   2560
19#define H   1600
20
21class GiantBitmapGM : public skiagm::GM {
22    SkBitmap* fBM;
23    SkShader::TileMode fMode;
24
25    const SkBitmap& getBitmap() {
26        if (NULL == fBM) {
27            fBM = new SkBitmap;
28            fBM->setConfig(SkBitmap::kARGB_8888_Config, W, H);
29            fBM->allocPixels();
30            fBM->eraseColor(SK_ColorWHITE);
31
32            const SkColor colors[] = {
33                SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
34            };
35
36            SkCanvas canvas(*fBM);
37            SkPaint paint;
38            paint.setAntiAlias(true);
39            paint.setStrokeWidth(SkIntToScalar(30));
40            for (int y = -H; y < H; y += 80) {
41                SkScalar yy = SkIntToScalar(y);
42                paint.setColor(colors[y/80 & 0x3]);
43                canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
44                                paint);
45            }
46        }
47        return *fBM;
48    }
49
50public:
51    GiantBitmapGM(SkShader::TileMode mode) : fBM(NULL), fMode(mode) {}
52
53    virtual ~GiantBitmapGM() {
54        SkDELETE(fBM);
55    }
56
57protected:
58    // work-around for bug http://code.google.com/p/skia/issues/detail?id=520
59    //
60    virtual uint32_t onGetFlags() const { return kSkipPDF_Flag; }
61
62    virtual SkString onShortName() {
63        SkString str("giantbitmap_");
64        switch (fMode) {
65            case SkShader::kClamp_TileMode:
66                str.append("clamp");
67                break;
68            case SkShader::kRepeat_TileMode:
69                str.append("repeat");
70                break;
71            case SkShader::kMirror_TileMode:
72                str.append("mirror");
73                break;
74            default:
75                break;
76        }
77        return str;
78    }
79
80    virtual SkISize onISize() { return SkISize::Make(640, 480); }
81
82    virtual void onDraw(SkCanvas* canvas) {
83        SkPaint paint;
84        SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode);
85        SkMatrix m;
86        m.setScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
87        s->setLocalMatrix(m);
88        paint.setShader(s)->unref();
89
90        canvas->drawPaint(paint);
91    }
92
93private:
94    typedef GM INHERITED;
95};
96
97///////////////////////////////////////////////////////////////////////////////
98
99static skiagm::GM* G0(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode); }
100static skiagm::GM* G1(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode); }
101static skiagm::GM* G2(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode); }
102
103static skiagm::GMRegistry reg0(G0);
104static skiagm::GMRegistry reg1(G1);
105static skiagm::GMRegistry reg2(G2);
106
107