2015-02-13 17:05:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DecodingBench.h"
|
2015-04-02 20:22:38 +00:00
|
|
|
#include "SkBitmap.h"
|
2015-02-13 17:05:41 +00:00
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkImageDecoder.h"
|
2015-04-01 19:09:17 +00:00
|
|
|
#include "SkMallocPixelRef.h"
|
2015-02-13 17:05:41 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* This benchmark is designed to test the performance of image decoding.
|
|
|
|
* It is invoked from the nanobench.cpp file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
DecodingBench::DecodingBench(SkString path, SkColorType colorType)
|
|
|
|
: fColorType(colorType)
|
2015-04-01 19:09:17 +00:00
|
|
|
, fData(SkData::NewFromFileName(path.c_str()))
|
2015-02-13 17:05:41 +00:00
|
|
|
{
|
|
|
|
// Parse filename and the color type to give the benchmark a useful name
|
|
|
|
SkString baseName = SkOSPath::Basename(path.c_str());
|
|
|
|
const char* colorName;
|
|
|
|
switch(colorType) {
|
|
|
|
case kN32_SkColorType:
|
|
|
|
colorName = "N32";
|
|
|
|
break;
|
|
|
|
case kRGB_565_SkColorType:
|
|
|
|
colorName = "565";
|
|
|
|
break;
|
|
|
|
case kAlpha_8_SkColorType:
|
|
|
|
colorName = "Alpha8";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
colorName = "Unknown";
|
|
|
|
}
|
|
|
|
fName.printf("Decode_%s_%s", baseName.c_str(), colorName);
|
|
|
|
|
2015-04-01 19:09:17 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
// Ensure that we can create a decoder.
|
|
|
|
SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData));
|
|
|
|
SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
|
|
|
|
SkASSERT(decoder != NULL);
|
|
|
|
#endif
|
2015-02-13 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* DecodingBench::onGetName() {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecodingBench::isSuitableFor(Backend backend) {
|
|
|
|
return kNonRendering_Backend == backend;
|
|
|
|
}
|
|
|
|
|
2015-04-01 19:09:17 +00:00
|
|
|
void DecodingBench::onPreDraw() {
|
|
|
|
// Allocate the pixels now, to remove it from the loop.
|
|
|
|
SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData));
|
|
|
|
SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
|
2015-04-02 20:22:38 +00:00
|
|
|
SkBitmap bm;
|
2015-04-01 19:09:17 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
SkImageDecoder::Result result =
|
|
|
|
#endif
|
2015-04-02 20:22:38 +00:00
|
|
|
decoder->decode(stream, &bm, fColorType, SkImageDecoder::kDecodeBounds_Mode);
|
2015-04-01 19:09:17 +00:00
|
|
|
SkASSERT(SkImageDecoder::kFailure != result);
|
2015-04-02 20:22:38 +00:00
|
|
|
|
|
|
|
const size_t rowBytes = bm.info().minRowBytes();
|
|
|
|
fPixelStorage.reset(bm.info().getSafeSize(rowBytes));
|
2015-04-01 19:09:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 20:22:38 +00:00
|
|
|
// Allocator which just uses an existing block of memory.
|
|
|
|
class TargetAllocator : public SkBitmap::Allocator {
|
2015-04-01 19:09:17 +00:00
|
|
|
public:
|
2015-04-02 20:22:38 +00:00
|
|
|
explicit TargetAllocator(void* storage)
|
|
|
|
: fPixelStorage(storage) {}
|
2015-04-01 19:09:17 +00:00
|
|
|
|
|
|
|
bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) override {
|
2015-04-02 20:22:38 +00:00
|
|
|
// We depend on the fact that this will only ever be used to
|
|
|
|
// decode to a bitmap with the same settings used to create
|
|
|
|
// fPixelStorage.
|
2015-04-01 19:09:17 +00:00
|
|
|
bm->setPixelRef(SkMallocPixelRef::NewDirect(bm->info(),
|
2015-04-02 20:22:38 +00:00
|
|
|
fPixelStorage, bm->rowBytes(), ct))->unref();
|
2015-04-01 19:09:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-04-02 20:22:38 +00:00
|
|
|
void* fPixelStorage; // Unowned. DecodingBench owns this.
|
2015-04-01 19:09:17 +00:00
|
|
|
};
|
|
|
|
|
2015-02-13 17:05:41 +00:00
|
|
|
void DecodingBench::onDraw(const int n, SkCanvas* canvas) {
|
|
|
|
SkBitmap bitmap;
|
2015-04-01 19:09:17 +00:00
|
|
|
// Declare the allocator before the decoder, so it will outlive the
|
|
|
|
// decoder, which will unref it.
|
2015-04-02 20:22:38 +00:00
|
|
|
TargetAllocator allocator(fPixelStorage.get());
|
2015-04-01 19:09:17 +00:00
|
|
|
SkAutoTDelete<SkImageDecoder> decoder;
|
|
|
|
SkAutoTDelete<SkStreamRewindable> stream;
|
2015-02-13 17:05:41 +00:00
|
|
|
for (int i = 0; i < n; i++) {
|
2015-04-01 19:09:17 +00:00
|
|
|
// create a new stream and a new decoder to mimic the behavior of
|
|
|
|
// CodecBench.
|
|
|
|
stream.reset(new SkMemoryStream(fData));
|
|
|
|
decoder.reset(SkImageDecoder::Factory(stream));
|
|
|
|
decoder->setAllocator(&allocator);
|
|
|
|
decoder->decode(stream, &bitmap, fColorType,
|
|
|
|
SkImageDecoder::kDecodePixels_Mode);
|
2015-02-13 17:05:41 +00:00
|
|
|
}
|
|
|
|
}
|