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 "SkImageDecoder.h" 13 #include "SkLruImageCache.h" 14 #include "SkOSFile.h" 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 { 28 // Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo 29 SkString filename = SkOSPath::SkPathJoin(INHERITED::gResourcePath.c_str(), 30 "plane.png"); 31 32 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str())); 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 41 // bitmap is unlocked. 42 SkAutoTUnref<SkLruImageCache> cache(SkNEW_ARGS(SkLruImageCache, (1))); 43 factory.setImageCache(cache); 44 factory.installPixelRef(data, &fBitmap); 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 } 72