skia2/gm/factory.cpp
scroggo@google.com a560d00ba5 Provide an option in bench_pictures to count pixel ram.
In SkLruImageCache, provide an option to keep all pixels, and document
the new behavior.

In gm/factory.cpp, set the budget for the Lru image cache to 1, to
retain (basically) the old behavior.

BUG=https://code.google.com/p/skia/issues/detail?id=1010

Review URL: https://codereview.chromium.org/12378075

git-svn-id: http://skia.googlecode.com/svn/trunk@7972 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-03-04 21:32:32 +00:00

75 lines
1.9 KiB
C++

/*
* 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"
#include "SkImageDecoder.h"
#include "SkLruImageCache.h"
#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");
SkFILEStream stream(filename.c_str());
if (stream.isValid()) {
stream.rewind();
size_t length = stream.getLength();
void* buffer = sk_malloc_throw(length);
stream.read(buffer, length);
SkAutoDataUnref data(SkData::NewFromMalloc(buffer, length));
SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
// Create a cache which will boot the pixels out anytime the
// bitmap is unlocked.
SkAutoTUnref<SkLruImageCache> cache(SkNEW_ARGS(SkLruImageCache, (1)));
factory.setImageCache(cache);
factory.installPixelRef(data, &fBitmap);
}
}
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);
}