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