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 10#include "Resources.h" 11#include "SkCanvas.h" 12#include "SkData.h" 13#include "SkDiscardableMemoryPool.h" 14#include "SkDiscardablePixelRef.h" 15#include "SkImageDecoder.h" 16#include "SkImageGeneratorPriv.h" 17#include "SkOSFile.h" 18#include "SkStream.h" 19 20namespace skiagm { 21 22/** 23 * Draw a PNG created by SkBitmapFactory. 24 */ 25class FactoryGM : public GM { 26public: 27 FactoryGM() {} 28 29protected: 30 void onOnceBeforeDraw() override { 31 // Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo 32 SkString pngFilename = GetResourcePath("plane.png"); 33 SkAutoDataUnref data(SkData::NewFromFileName(pngFilename.c_str())); 34 if (data.get()) { 35 // Create a cache which will boot the pixels out anytime the 36 // bitmap is unlocked. 37 SkAutoTUnref<SkDiscardableMemoryPool> pool( 38 SkDiscardableMemoryPool::Create(1)); 39 SkAssertResult(SkInstallDiscardablePixelRef(SkImageGenerator::NewFromData(data), 40 &fBitmap, pool)); 41 } 42 } 43 44 virtual SkString onShortName() override { 45 return SkString("factory"); 46 } 47 48 virtual SkISize onISize() override { 49 return SkISize::Make(640, 480); 50 } 51 52 virtual void onDraw(SkCanvas* canvas) override { 53 canvas->drawBitmap(fBitmap, 0, 0); 54 } 55 56private: 57 SkBitmap fBitmap; 58 59 typedef GM INHERITED; 60}; 61 62////////////////////////////////////////////////////////////////////////////// 63 64static GM* MyFactory(void*) { return new FactoryGM; } 65static GMRegistry reg(MyFactory); 66 67} // namespace skiagm 68