2012-11-29 21:05:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkBitmapFactory.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkData.h"
|
2013-02-22 21:38:35 +00:00
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkLruImageCache.h"
|
2012-11-29 21:05:37 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw a PNG created by SkBitmapFactory.
|
|
|
|
*/
|
|
|
|
class FactoryGM : public GM {
|
|
|
|
public:
|
|
|
|
FactoryGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void onOnceBeforeDraw() SK_OVERRIDE {
|
|
|
|
SkString filename(INHERITED::gResourcePath);
|
|
|
|
if (!filename.endsWith("/") && !filename.endsWith("\\")) {
|
|
|
|
filename.append("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo
|
|
|
|
filename.append("plane.png");
|
|
|
|
|
2013-04-24 20:03:00 +00:00
|
|
|
SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
|
|
|
|
if (NULL != stream.get()) {
|
|
|
|
stream->rewind();
|
|
|
|
size_t length = stream->getLength();
|
2012-11-29 21:05:37 +00:00
|
|
|
void* buffer = sk_malloc_throw(length);
|
2013-04-24 20:03:00 +00:00
|
|
|
stream->read(buffer, length);
|
2012-11-29 21:05:37 +00:00
|
|
|
SkAutoDataUnref data(SkData::NewFromMalloc(buffer, length));
|
2013-02-22 21:38:35 +00:00
|
|
|
SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
|
|
|
|
// Create a cache which will boot the pixels out anytime the
|
|
|
|
// bitmap is unlocked.
|
2013-03-04 21:32:32 +00:00
|
|
|
SkAutoTUnref<SkLruImageCache> cache(SkNEW_ARGS(SkLruImageCache, (1)));
|
2013-02-22 21:38:35 +00:00
|
|
|
factory.setImageCache(cache);
|
|
|
|
factory.installPixelRef(data, &fBitmap);
|
2012-11-29 21:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("factory");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
|
|
|
return make_isize(640, 480);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new FactoryGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|