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 "SkColorPriv.h"
10#include "SkShader.h"
11#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkUtils.h"
14
15static SkBitmap make_bitmap() {
16    const int N = 1;
17
18    SkPMColor c[N];
19    for (int i = 0; i < N; i++) {
20        c[i] = SkPackARGB32(0x80, 0x80, 0, 0);
21    }
22    SkColorTable* ctable = new SkColorTable(c, N);
23
24    SkBitmap bm;
25    bm.allocPixels(SkImageInfo::Make(1, 1, kIndex_8_SkColorType,
26                                     kPremul_SkAlphaType),
27                   NULL, ctable);
28    ctable->unref();
29
30    bm.lockPixels();
31    for (int y = 0; y < bm.height(); y++) {
32        uint8_t* p = bm.getAddr8(0, y);
33        for (int x = 0; x < bm.width(); x++) {
34            p[x] = 0;
35        }
36    }
37    bm.unlockPixels();
38    return bm;
39}
40
41class TinyBitmapView : public SampleView {
42    SkBitmap    fBM;
43public:
44    TinyBitmapView() {
45        fBM = make_bitmap();
46        this->setBGColor(0xFFDDDDDD);
47    }
48
49protected:
50    // overrides from SkEventSink
51    virtual bool onQuery(SkEvent* evt) {
52        if (SampleCode::TitleQ(*evt)) {
53            SampleCode::TitleR(evt, "TinyBitmap");
54            return true;
55        }
56        return this->INHERITED::onQuery(evt);
57    }
58
59    static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
60        SkAutoLockPixels alp(*bm);  // needed for ctable
61        bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
62    }
63
64    virtual void onDrawContent(SkCanvas* canvas) {
65        SkShader* s = SkShader::CreateBitmapShader(fBM, SkShader::kRepeat_TileMode,
66                                                   SkShader::kMirror_TileMode);
67        SkPaint paint;
68        paint.setShader(s)->unref();
69        canvas->drawPaint(paint);
70    }
71
72private:
73    typedef SkView INHERITED;
74};
75
76//////////////////////////////////////////////////////////////////////////////
77
78static SkView* MyFactory() { return new TinyBitmapView; }
79static SkViewRegister reg(MyFactory);
80