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" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | /** |
| 17 | * Draw a PNG created by SkBitmapFactory. |
| 18 | */ |
| 19 | class FactoryGM : public GM { |
| 20 | public: |
| 21 | FactoryGM() {} |
| 22 | |
| 23 | protected: |
| 24 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 25 | SkString filename(INHERITED::gResourcePath); |
| 26 | if (!filename.endsWith("/") && !filename.endsWith("\\")) { |
| 27 | filename.append("/"); |
| 28 | } |
| 29 | |
| 30 | // Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo |
| 31 | filename.append("plane.png"); |
| 32 | |
| 33 | SkFILEStream stream(filename.c_str()); |
| 34 | if (stream.isValid()) { |
| 35 | stream.rewind(); |
| 36 | size_t length = stream.getLength(); |
| 37 | void* buffer = sk_malloc_throw(length); |
| 38 | stream.read(buffer, length); |
| 39 | SkAutoDataUnref data(SkData::NewFromMalloc(buffer, length)); |
| 40 | SkBitmapFactory::DecodeBitmap(&fBitmap, data); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | virtual SkString onShortName() { |
| 45 | return SkString("factory"); |
| 46 | } |
| 47 | |
| 48 | virtual SkISize onISize() { |
| 49 | return make_isize(640, 480); |
| 50 | } |
| 51 | |
| 52 | virtual void onDraw(SkCanvas* canvas) { |
| 53 | canvas->drawBitmap(fBitmap, 0, 0); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | SkBitmap fBitmap; |
| 58 | |
| 59 | typedef GM INHERITED; |
| 60 | }; |
| 61 | |
| 62 | ////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | static GM* MyFactory(void*) { return new FactoryGM; } |
| 65 | static GMRegistry reg(MyFactory); |
| 66 | |
| 67 | } |