scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkBitmapFactory.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkData.h" |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkLruImageCache.h" |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 14 | #include "SkOSFile.h" |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | /** |
| 20 | * Draw a PNG created by SkBitmapFactory. |
| 21 | */ |
| 22 | class FactoryGM : public GM { |
| 23 | public: |
| 24 | FactoryGM() {} |
| 25 | |
| 26 | protected: |
| 27 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 28 | // Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 29 | SkString filename = SkOSPath::SkPathJoin(INHERITED::gResourcePath.c_str(), |
| 30 | "plane.png"); |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 32 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str())); |
| 33 | if (NULL != stream.get()) { |
| 34 | stream->rewind(); |
| 35 | size_t length = stream->getLength(); |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 36 | void* buffer = sk_malloc_throw(length); |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 37 | stream->read(buffer, length); |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 38 | SkAutoDataUnref data(SkData::NewFromMalloc(buffer, length)); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 39 | SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget); |
| 40 | // Create a cache which will boot the pixels out anytime the |
| 41 | // bitmap is unlocked. |
scroggo@google.com | a560d00b | 2013-03-04 21:32:32 +0000 | [diff] [blame] | 42 | SkAutoTUnref<SkLruImageCache> cache(SkNEW_ARGS(SkLruImageCache, (1))); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 43 | factory.setImageCache(cache); |
| 44 | factory.installPixelRef(data, &fBitmap); |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | virtual SkString onShortName() { |
| 49 | return SkString("factory"); |
| 50 | } |
| 51 | |
| 52 | virtual SkISize onISize() { |
| 53 | return make_isize(640, 480); |
| 54 | } |
| 55 | |
| 56 | virtual void onDraw(SkCanvas* canvas) { |
| 57 | canvas->drawBitmap(fBitmap, 0, 0); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | SkBitmap fBitmap; |
| 62 | |
| 63 | typedef GM INHERITED; |
| 64 | }; |
| 65 | |
| 66 | ////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | static GM* MyFactory(void*) { return new FactoryGM; } |
| 69 | static GMRegistry reg(MyFactory); |
| 70 | |
| 71 | } |