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