blob: 4fb508b898413d4dded756b4e4b873aa73959d51 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed8a21c9f2016-03-08 18:50:00 -08007
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),
halcanary96fcdcc2015-08-27 07:41:13 -070027 nullptr, 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:
reed8a21c9f2016-03-08 18:50:00 -080050 bool onQuery(SkEvent* evt) override {
reed@android.comc6459962009-08-25 19:15:31 +000051 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
reed@google.com383a6972013-10-21 14:00:07 +000060 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed@android.comc6459962009-08-25 19:15:31 +000061 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
reed8a21c9f2016-03-08 18:50:00 -080063 void onDrawContent(SkCanvas* canvas) override {
reed@android.comc6459962009-08-25 19:15:31 +000064 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -080065 paint.setShader(SkShader::MakeBitmapShader(fBM, SkShader::kRepeat_TileMode,
66 SkShader::kMirror_TileMode));
reed@android.comc6459962009-08-25 19:15:31 +000067 canvas->drawPaint(paint);
68 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@android.comc6459962009-08-25 19:15:31 +000070private:
71 typedef SkView INHERITED;
72};
73
74//////////////////////////////////////////////////////////////////////////////
75
76static SkView* MyFactory() { return new TinyBitmapView; }
77static SkViewRegister reg(MyFactory);