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