blob: a7356a96e30a61288c5be5aaa883a201fbe21927 [file] [log] [blame]
scroggo@google.com9f686f32012-11-29 21:05:37 +00001/*
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.comf8d7d272013-02-22 21:38:35 +000012#include "SkImageDecoder.h"
13#include "SkLruImageCache.h"
scroggo@google.comccd7afb2013-05-28 16:45:07 +000014#include "SkOSFile.h"
scroggo@google.com9f686f32012-11-29 21:05:37 +000015#include "SkStream.h"
16
17namespace skiagm {
18
19/**
20 * Draw a PNG created by SkBitmapFactory.
21 */
22class FactoryGM : public GM {
23public:
24 FactoryGM() {}
25
26protected:
27 virtual void onOnceBeforeDraw() SK_OVERRIDE {
scroggo@google.com9f686f32012-11-29 21:05:37 +000028 // Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo
scroggo@google.comccd7afb2013-05-28 16:45:07 +000029 SkString filename = SkOSPath::SkPathJoin(INHERITED::gResourcePath.c_str(),
30 "plane.png");
scroggo@google.com9f686f32012-11-29 21:05:37 +000031
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000032 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
33 if (NULL != stream.get()) {
34 stream->rewind();
35 size_t length = stream->getLength();
scroggo@google.com9f686f32012-11-29 21:05:37 +000036 void* buffer = sk_malloc_throw(length);
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000037 stream->read(buffer, length);
scroggo@google.com9f686f32012-11-29 21:05:37 +000038 SkAutoDataUnref data(SkData::NewFromMalloc(buffer, length));
scroggo@google.comf8d7d272013-02-22 21:38:35 +000039 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
40 // Create a cache which will boot the pixels out anytime the
41 // bitmap is unlocked.
scroggo@google.coma560d00b2013-03-04 21:32:32 +000042 SkAutoTUnref<SkLruImageCache> cache(SkNEW_ARGS(SkLruImageCache, (1)));
scroggo@google.comf8d7d272013-02-22 21:38:35 +000043 factory.setImageCache(cache);
44 factory.installPixelRef(data, &fBitmap);
scroggo@google.com9f686f32012-11-29 21:05:37 +000045 }
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
60private:
61 SkBitmap fBitmap;
62
63 typedef GM INHERITED;
64};
65
66//////////////////////////////////////////////////////////////////////////////
67
68static GM* MyFactory(void*) { return new FactoryGM; }
69static GMRegistry reg(MyFactory);
70
71}