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 "SampleCode.h"
9 #include "SkColorPriv.h"
10 #include "SkShader.h"
11 #include "SkView.h"
12 #include "SkCanvas.h"
13 #include "SkUtils.h"
14
make_bitmap()15 static 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 nullptr, 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
41 class TinyBitmapView : public SampleView {
42 SkBitmap fBM;
43 public:
TinyBitmapView()44 TinyBitmapView() {
45 fBM = make_bitmap();
46 this->setBGColor(0xFFDDDDDD);
47 }
48
49 protected:
onQuery(SkEvent * evt)50 bool onQuery(SkEvent* evt) override {
51 if (SampleCode::TitleQ(*evt)) {
52 SampleCode::TitleR(evt, "TinyBitmap");
53 return true;
54 }
55 return this->INHERITED::onQuery(evt);
56 }
57
setBitmapOpaque(SkBitmap * bm,bool isOpaque)58 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
59 SkAutoLockPixels alp(*bm); // needed for ctable
60 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
61 }
62
onDrawContent(SkCanvas * canvas)63 void onDrawContent(SkCanvas* canvas) override {
64 SkPaint paint;
65 paint.setShader(SkShader::MakeBitmapShader(fBM, SkShader::kRepeat_TileMode,
66 SkShader::kMirror_TileMode));
67 canvas->drawPaint(paint);
68 }
69
70 private:
71 typedef SkView INHERITED;
72 };
73
74 //////////////////////////////////////////////////////////////////////////////
75
MyFactory()76 static SkView* MyFactory() { return new TinyBitmapView; }
77 static SkViewRegister reg(MyFactory);
78