blob: 42866d07adfe6b370e7be9f4430fbfbfb6631dbd [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;
reed@android.comc6459962009-08-25 19:15:31 +000025 bm.setConfig(SkBitmap::kIndex8_Config, 1, 1);
26 bm.allocPixels(ctable);
27 ctable->unref();
28
29 bm.lockPixels();
30 for (int y = 0; y < bm.height(); y++) {
31 uint8_t* p = bm.getAddr8(0, y);
32 for (int x = 0; x < bm.width(); x++) {
33 p[x] = 0;
34 }
35 }
36 bm.unlockPixels();
37 return bm;
38}
39
reed@google.com17fb3872011-05-04 14:31:07 +000040class TinyBitmapView : public SampleView {
reed@android.comc6459962009-08-25 19:15:31 +000041 SkBitmap fBM;
42public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000043 TinyBitmapView() {
reed@android.comc6459962009-08-25 19:15:31 +000044 fBM = make_bitmap();
reed@google.com17fb3872011-05-04 14:31:07 +000045 this->setBGColor(0xFFDDDDDD);
reed@android.comc6459962009-08-25 19:15:31 +000046 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.comc6459962009-08-25 19:15:31 +000048protected:
49 // overrides from SkEventSink
50 virtual bool onQuery(SkEvent* evt) {
51 if (SampleCode::TitleQ(*evt)) {
52 SampleCode::TitleR(evt, "TinyBitmap");
53 return true;
54 }
55 return this->INHERITED::onQuery(evt);
56 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
reed@android.comc6459962009-08-25 19:15:31 +000058 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
59 SkAutoLockPixels alp(*bm); // needed for ctable
60 bm->setIsOpaque(isOpaque);
reed@google.com0a6151d2013-10-10 14:44:56 +000061#if 0
62 // TODO - I think we just want to not allow this anymore
reed@android.comc6459962009-08-25 19:15:31 +000063 SkColorTable* ctable = bm->getColorTable();
64 if (ctable) {
65 ctable->setIsOpaque(isOpaque);
66 }
reed@google.com0a6151d2013-10-10 14:44:56 +000067#endif
reed@android.comc6459962009-08-25 19:15:31 +000068 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@google.com17fb3872011-05-04 14:31:07 +000070 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comc6459962009-08-25 19:15:31 +000071 SkShader* s = SkShader::CreateBitmapShader(fBM, SkShader::kRepeat_TileMode,
72 SkShader::kMirror_TileMode);
73 SkPaint paint;
74 paint.setShader(s)->unref();
75 canvas->drawPaint(paint);
76 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000077
reed@android.comc6459962009-08-25 19:15:31 +000078private:
79 typedef SkView INHERITED;
80};
81
82//////////////////////////////////////////////////////////////////////////////
83
84static SkView* MyFactory() { return new TinyBitmapView; }
85static SkViewRegister reg(MyFactory);