| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkBitmapFactory.h" | |
| 10 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 11 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkDecodingImageGenerator.h" |
| 12 #include "SkDiscardableMemoryPool.h" |
| 13 #include "SkDiscardablePixelRef.h" |
| 12 #include "SkImageDecoder.h" | 14 #include "SkImageDecoder.h" |
| 13 #include "SkLruImageCache.h" | |
| 14 #include "SkOSFile.h" | 15 #include "SkOSFile.h" |
| 15 #include "SkStream.h" | 16 #include "SkStream.h" |
| 16 | 17 |
| 17 namespace skiagm { | 18 namespace skiagm { |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * Draw a PNG created by SkBitmapFactory. | 21 * Draw a PNG created by SkBitmapFactory. |
| 21 */ | 22 */ |
| 22 class FactoryGM : public GM { | 23 class FactoryGM : public GM { |
| 23 public: | 24 public: |
| 24 FactoryGM() {} | 25 FactoryGM() {} |
| 25 | 26 |
| 26 protected: | 27 protected: |
| 27 virtual void onOnceBeforeDraw() SK_OVERRIDE { | 28 virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 28 // Copyright-free file from http://openclipart.org/detail/29213/paper-pl
ane-by-ddoo | 29 // Copyright-free file from http://openclipart.org/detail/29213/paper-pl
ane-by-ddoo |
| 29 SkString filename = SkOSPath::SkPathJoin(INHERITED::gResourcePath.c_str(
), | 30 SkString filename = SkOSPath::SkPathJoin(INHERITED::gResourcePath.c_str(
), |
| 30 "plane.png"); | 31 "plane.png"); |
| 31 | 32 SkAutoDataUnref data(SkData::NewFromFileName(filename.c_str())); |
| 32 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str())); | 33 if (NULL != data.get()) { |
| 33 if (NULL != stream.get()) { | |
| 34 stream->rewind(); | |
| 35 size_t length = stream->getLength(); | |
| 36 void* buffer = sk_malloc_throw(length); | |
| 37 stream->read(buffer, length); | |
| 38 SkAutoDataUnref data(SkData::NewFromMalloc(buffer, length)); | |
| 39 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget); | |
| 40 // Create a cache which will boot the pixels out anytime the | 34 // Create a cache which will boot the pixels out anytime the |
| 41 // bitmap is unlocked. | 35 // bitmap is unlocked. |
| 42 SkAutoTUnref<SkLruImageCache> cache(SkNEW_ARGS(SkLruImageCache, (1))
); | 36 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 43 factory.setImageCache(cache); | 37 SkNEW_ARGS(SkDiscardableMemoryPool, (1))); |
| 44 factory.installPixelRef(data, &fBitmap); | 38 SkAssertResult(SkDecodingImageGenerator::Install(data, |
| 39 &fBitmap, pool)); |
| 45 } | 40 } |
| 46 } | 41 } |
| 47 | 42 |
| 48 virtual SkString onShortName() { | 43 virtual SkString onShortName() { |
| 49 return SkString("factory"); | 44 return SkString("factory"); |
| 50 } | 45 } |
| 51 | 46 |
| 52 virtual SkISize onISize() { | 47 virtual SkISize onISize() { |
| 53 return make_isize(640, 480); | 48 return make_isize(640, 480); |
| 54 } | 49 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 | 62 |
| 68 typedef GM INHERITED; | 63 typedef GM INHERITED; |
| 69 }; | 64 }; |
| 70 | 65 |
| 71 ////////////////////////////////////////////////////////////////////////////// | 66 ////////////////////////////////////////////////////////////////////////////// |
| 72 | 67 |
| 73 static GM* MyFactory(void*) { return new FactoryGM; } | 68 static GM* MyFactory(void*) { return new FactoryGM; } |
| 74 static GMRegistry reg(MyFactory); | 69 static GMRegistry reg(MyFactory); |
| 75 | 70 |
| 76 } | 71 } |
| OLD | NEW |