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; |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 25 | 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.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 40 | class TinyBitmapView : public SampleView { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 41 | SkBitmap fBM; |
| 42 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | TinyBitmapView() { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 44 | fBM = make_bitmap(); |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 45 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 46 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 48 | protected: |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 58 | static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { |
| 59 | SkAutoLockPixels alp(*bm); // needed for ctable |
| 60 | bm->setIsOpaque(isOpaque); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 61 | #if 0 |
| 62 | // TODO - I think we just want to not allow this anymore |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 63 | SkColorTable* ctable = bm->getColorTable(); |
| 64 | if (ctable) { |
| 65 | ctable->setIsOpaque(isOpaque); |
| 66 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 67 | #endif |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 68 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 70 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 71 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 78 | private: |
| 79 | typedef SkView INHERITED; |
| 80 | }; |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | static SkView* MyFactory() { return new TinyBitmapView; } |
| 85 | static SkViewRegister reg(MyFactory); |