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