reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkGraphics.h" |
| 6 | #include "SkImageDecoder.h" |
| 7 | #include "SkPath.h" |
| 8 | #include "SkPorterDuff.h" |
| 9 | #include "SkRegion.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkUtils.h" |
| 12 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
| 14 | #include "SkColorFilter.h" |
| 15 | #include "SkTime.h" |
| 16 | #include "SkTypeface.h" |
| 17 | |
| 18 | #include "SkImageRef_GlobalPool.h" |
| 19 | #include "SkStream.h" |
| 20 | |
| 21 | static const char* gNames[] = { |
| 22 | "1.bmp", "1.gif", "1.jpg", "1.png", |
| 23 | "2.bmp", "2.gif", "2.jpg", "2.png" |
| 24 | }; |
| 25 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | static bool SetImageRef(SkBitmap* bitmap, SkStream* stream, |
| 27 | SkBitmap::Config pref, const char name[] = NULL) |
| 28 | { |
| 29 | if (SkImageDecoder::DecodeStream(stream, bitmap, pref, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame^] | 30 | SkImageDecoder::kDecodeBounds_Mode, NULL)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | SkASSERT(bitmap->config() != SkBitmap::kNo_Config); |
| 32 | |
| 33 | SkImageRef* ref = new SkImageRef_GlobalPool(stream, bitmap->config()); |
| 34 | ref->setURI(name); |
| 35 | bitmap->setPixelRef(ref)->unref(); |
| 36 | return true; |
| 37 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | class ImageView : public SkView { |
| 43 | public: |
| 44 | SkBitmap* fBitmaps; |
| 45 | SkShader* fShader; |
| 46 | |
| 47 | ImageView() { |
| 48 | SkImageRef_GlobalPool::SetRAMBudget(32 * 1024); |
| 49 | |
| 50 | int i, N = SK_ARRAY_COUNT(gNames); |
| 51 | fBitmaps = new SkBitmap[N]; |
| 52 | |
| 53 | for (i = 0; i < N; i++) { |
| 54 | SkString str("/skimages/"); |
| 55 | str.append(gNames[i]); |
| 56 | SkFILEStream* stream = new SkFILEStream(str.c_str()); |
| 57 | |
| 58 | SetImageRef(&fBitmaps[i], stream, SkBitmap::kNo_Config, gNames[i]); |
| 59 | if (i & 1) |
| 60 | fBitmaps[i].buildMipMap(); |
reed@android.com | 791f5a1 | 2009-03-16 13:53:11 +0000 | [diff] [blame] | 61 | stream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | fShader = SkShader::CreateBitmapShader(fBitmaps[5], |
| 65 | SkShader::kRepeat_TileMode, |
| 66 | SkShader::kRepeat_TileMode); |
| 67 | |
| 68 | if (true) { |
| 69 | SkMatrix m; |
| 70 | |
| 71 | m.setRotate(SkIntToScalar(30)); |
| 72 | fShader->setLocalMatrix(m); |
| 73 | } |
| 74 | |
| 75 | #if 0 |
| 76 | SkImageRef::DumpPool(); |
| 77 | for (i = 0; i < N; i++) { |
| 78 | SkBitmap& bm = fBitmaps[i]; |
| 79 | |
| 80 | SkDebugf("<%s> addr=%p", gNames[i], bm.getPixels()); |
| 81 | bool success = bm.lockPixels(); |
| 82 | SkDebugf(" addr=%d", bm.getPixels()); |
| 83 | if (success) |
| 84 | bm.unlockPixels(); |
| 85 | SkDebugf(" addr=%p", bm.getPixels()); |
| 86 | success = bm.lockPixels(); |
| 87 | SkDebugf(" addr=%d", bm.getPixels()); |
| 88 | if (success) |
| 89 | bm.unlockPixels(); |
| 90 | SkDebugf("\n"); |
| 91 | } |
| 92 | SkImageRef::DumpPool(); |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | virtual ~ImageView() { |
| 97 | delete[] fBitmaps; |
| 98 | delete fShader; |
| 99 | |
| 100 | SkImageRef_GlobalPool::DumpPool(); |
| 101 | } |
| 102 | |
| 103 | protected: |
| 104 | // overrides from SkEventSink |
| 105 | virtual bool onQuery(SkEvent* evt) { |
| 106 | if (SampleCode::TitleQ(*evt)) { |
| 107 | SampleCode::TitleR(evt, "Image"); |
| 108 | return true; |
| 109 | } |
| 110 | return this->INHERITED::onQuery(evt); |
| 111 | } |
| 112 | |
| 113 | void drawBG(SkCanvas* canvas) { |
| 114 | canvas->drawColor(0xFFDDDDDD); |
| 115 | // canvas->drawColor(SK_ColorWHITE); |
| 116 | } |
| 117 | |
| 118 | virtual void onDraw(SkCanvas* canvas) { |
| 119 | this->drawBG(canvas); |
| 120 | |
| 121 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 122 | |
| 123 | SkScalar x = 0, y = 0; |
| 124 | |
| 125 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); i++) { |
| 126 | canvas->drawBitmap(fBitmaps[i], x, y); |
| 127 | x += SkIntToScalar(fBitmaps[i].width() + 10); |
| 128 | } |
| 129 | |
| 130 | canvas->translate(0, SkIntToScalar(120)); |
| 131 | |
| 132 | SkPaint paint; |
| 133 | paint.setShader(fShader); |
| 134 | paint.setFilterBitmap(true); |
| 135 | SkRect r = { 0, 0, SkIntToScalar(300), SkIntToScalar(100) }; |
| 136 | |
| 137 | canvas->drawRect(r, paint); |
| 138 | } |
| 139 | |
| 140 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 141 | this->inval(NULL); |
| 142 | return this->INHERITED::onFindClickHandler(x, y); |
| 143 | } |
| 144 | |
| 145 | virtual bool onClick(Click* click) { |
| 146 | return this->INHERITED::onClick(click); |
| 147 | } |
| 148 | |
| 149 | private: |
| 150 | typedef SkView INHERITED; |
| 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | static SkView* MyFactory() { return new ImageView; } |
| 156 | static SkViewRegister reg(MyFactory); |
| 157 | |