blob: 57ce2ff319ce9770de3b9775d95e954138af21d2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comc6459962009-08-25 19:15:31 +00008#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() {
reed@android.comc6459962009-08-25 19:15:31 +000016 const int N = 1;
reed@android.comc6459962009-08-25 19:15:31 +000017
reed@google.com0a6151d2013-10-10 14:44:56 +000018 SkPMColor c[N];
reed@android.comc6459962009-08-25 19:15:31 +000019 for (int i = 0; i < N; i++) {
20 c[i] = SkPackARGB32(0x80, 0x80, 0, 0);
21 }
reed@google.com0a6151d2013-10-10 14:44:56 +000022 SkColorTable* ctable = new SkColorTable(c, N);
23
24 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000025 bm.allocPixels(SkImageInfo::Make(1, 1, kIndex_8_SkColorType,
26 kPremul_SkAlphaType),
27 NULL, ctable);
reed@android.comc6459962009-08-25 19:15:31 +000028 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
reed@google.com17fb3872011-05-04 14:31:07 +000041class TinyBitmapView : public SampleView {
reed@android.comc6459962009-08-25 19:15:31 +000042 SkBitmap fBM;
43public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000044 TinyBitmapView() {
reed@android.comc6459962009-08-25 19:15:31 +000045 fBM = make_bitmap();
reed@google.com17fb3872011-05-04 14:31:07 +000046 this->setBGColor(0xFFDDDDDD);
reed@android.comc6459962009-08-25 19:15:31 +000047 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
reed@android.comc6459962009-08-25 19:15:31 +000049protected:
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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.comc6459962009-08-25 19:15:31 +000059 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
60 SkAutoLockPixels alp(*bm); // needed for ctable
reed@google.com383a6972013-10-21 14:00:07 +000061 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed@android.comc6459962009-08-25 19:15:31 +000062 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@google.com17fb3872011-05-04 14:31:07 +000064 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comc6459962009-08-25 19:15:31 +000065 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.comc6459962009-08-25 19:15:31 +000072private:
73 typedef SkView INHERITED;
74};
75
76//////////////////////////////////////////////////////////////////////////////
77
78static SkView* MyFactory() { return new TinyBitmapView; }
79static SkViewRegister reg(MyFactory);