epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 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 | |
| 15 | static SkBitmap make_bitmap() { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 16 | const int N = 1; |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 17 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 18 | SkPMColor c[N]; |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 19 | for (int i = 0; i < N; i++) { |
| 20 | c[i] = SkPackARGB32(0x80, 0x80, 0, 0); |
| 21 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 22 | SkColorTable* ctable = new SkColorTable(c, N); |
| 23 | |
| 24 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 25 | bm.allocPixels(SkImageInfo::Make(1, 1, kIndex_8_SkColorType, |
| 26 | kPremul_SkAlphaType), |
| 27 | NULL, ctable); |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 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 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 41 | class TinyBitmapView : public SampleView { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 42 | SkBitmap fBM; |
| 43 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | TinyBitmapView() { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 45 | fBM = make_bitmap(); |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 46 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 47 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 48 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 49 | protected: |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 59 | static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { |
| 60 | SkAutoLockPixels alp(*bm); // needed for ctable |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 61 | bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 62 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 64 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 72 | private: |
| 73 | typedef SkView INHERITED; |
| 74 | }; |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| 78 | static SkView* MyFactory() { return new TinyBitmapView; } |
| 79 | static SkViewRegister reg(MyFactory); |