Chromium Code Reviews| Index: bench/SubsetDivisorBench.cpp |
| diff --git a/bench/SubsetDivisorBench.cpp b/bench/SubsetDivisorBench.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6ca9433828e6cede397c606b30890d681a31b699 |
| --- /dev/null |
| +++ b/bench/SubsetDivisorBench.cpp |
| @@ -0,0 +1,152 @@ |
| +/* |
| + * 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 "SubsetDivisorBench.h" |
| +#include "SubsetBenchPriv.h" |
| +#include "SkData.h" |
| +#include "SkCodec.h" |
| +#include "SkImageDecoder.h" |
| +#include "SkOSFile.h" |
| +#include "SkStream.h" |
| + |
| +/* |
| + * |
| + * This benchmark is designed to test the performance of subset decoding. |
| + * It uses a divisor to decode the entire image in a grid of divisor x divisor blocks. |
| + * |
| + */ |
| + |
| +SubsetDivisorBench* SubsetDivisorBench::Create(SkString* path, |
| + SkColorType colorType, |
| + uint32_t divisor, |
| + bool useCodec) { |
| + // Check that the decode will succeed |
| + SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| + SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| + if (!valid_subset_bench(stream, colorType, useCodec)) { |
| + return NULL; |
| + } |
| + return new SubsetDivisorBench(path, colorType, divisor, useCodec); |
| +} |
| + |
| +SubsetDivisorBench::SubsetDivisorBench(SkString* path, |
| + SkColorType colorType, |
| + uint32_t divisor, |
| + bool useCodec) |
| + : fColorType(colorType) |
| + , fDivisor(divisor) |
| + , fUseCodec(useCodec) |
| +{ |
| + // Parse the filename |
| + SkString baseName = SkOSPath::Basename(path->c_str()); |
| + |
| + // Choose an informative color name |
| + const char* colorName; |
| + switch(fColorType) { |
| + case kN32_SkColorType: |
| + colorName = "N32"; |
| + break; |
| + case kRGB_565_SkColorType: |
| + colorName = "565"; |
| + break; |
| + case kGray_8_SkColorType: |
| + colorName = "Gray8"; |
| + break; |
| + case kIndex_8_SkColorType: |
| + colorName = "Gray8"; |
|
scroggo
2015/06/01 17:25:14
Index8*
This whole block should probably go insid
msarett
2015/06/01 20:37:02
Done.
|
| + break; |
| + case kAlpha_8_SkColorType: |
| + colorName = "Alpha8"; |
| + break; |
| + default: |
| + colorName = "Unknown"; |
| + } |
| + fName.printf("%sSubsetDivisor_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image", fDivisor, fDivisor, |
| + baseName.c_str(), colorName); |
| + |
| + // Perform the decode setup |
| + SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| + fStream.reset(new SkMemoryStream(encoded)); |
| +} |
| + |
| +const char* SubsetDivisorBench::onGetName() { |
| + return fName.c_str(); |
| +} |
| + |
| +bool SubsetDivisorBench::isSuitableFor(Backend backend) { |
| + return kNonRendering_Backend == backend; |
| +} |
| + |
| +void SubsetDivisorBench::onDraw(const int n, SkCanvas* canvas) { |
| + if (fUseCodec) { |
| + for (int count = 0; count < n; count++) { |
| + SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate())); |
| + const SkImageInfo info = codec->getInfo(); |
| + SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes())); |
| + SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info); |
| + |
| + const uint32_t subsetWidth = info.width() / fDivisor; |
| + const uint32_t subsetHeight = info.height() / fDivisor; |
| + const uint32_t maxSubsetWidth = subsetWidth + info.width() % fDivisor; |
| + const uint32_t maxSubsetHeight = subsetHeight + info.height() % fDivisor; |
| + SkBitmap bitmap; |
| + // Note that we use the same bitmap for all of the subsets. |
| + // It might be slightly larger than necessary for some of the subsets. |
| + if (!bitmap.tryAllocPixels(info.makeWH(maxSubsetWidth, maxSubsetHeight))) { |
|
scroggo
2015/06/01 17:25:14
Inside the benches, maybe we should call bitmap.al
msarett
2015/06/01 20:37:02
Yes this makes sense. And it has the additional b
|
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + for (uint32_t blockX = 0; blockX < fDivisor; blockX++) { |
| + for (uint32_t blockY = 0; blockY < fDivisor; blockY++) { |
| + scanlineDecoder->skipScanlines(blockY * subsetHeight); |
| + const uint32_t currSubsetWidth = |
| + (blockX == fDivisor - 1) ? maxSubsetWidth : subsetWidth; |
| + const uint32_t currSubsetHeight = |
| + (blockY == fDivisor - 1) ? maxSubsetHeight : subsetHeight; |
| + const uint32_t bpp = info.bytesPerPixel(); |
| + for (uint32_t y = 0; y < currSubsetHeight; y++) { |
| + scanlineDecoder->getScanlines(row.get(), 1, 0); |
| + memcpy(bitmap.getAddr(0, y), row.get() + blockX * subsetWidth * bpp, |
| + currSubsetWidth * bpp); |
| + } |
| + } |
| + } |
| + } |
| + } else { |
| + for (int count = 0; count < n; count++) { |
| + int width, height; |
| + SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| + decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| + |
| + const uint32_t subsetWidth = width / fDivisor; |
| + const uint32_t subsetHeight = height / fDivisor; |
| + const uint32_t maxSubsetWidth = subsetWidth + width % fDivisor; |
| + const uint32_t maxSubsetHeight = subsetHeight + height % fDivisor; |
| + SkBitmap bitmap; |
| + // Note that we use the same bitmap for all of the subsets. |
| + // It might be slightly larger than necessary for some of the subsets. |
| + if (!bitmap.tryAllocPixels(SkImageInfo::Make(maxSubsetWidth, maxSubsetHeight, |
| + fColorType, kOpaque_SkAlphaType))) { |
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + for (uint32_t blockX = 0; blockX < fDivisor; blockX++) { |
| + for (uint32_t blockY = 0; blockY < fDivisor; blockY++) { |
| + const uint32_t currSubsetWidth = |
| + (blockX == fDivisor - 1) ? maxSubsetWidth : subsetWidth; |
| + const uint32_t currSubsetHeight = |
| + (blockY == fDivisor - 1) ? maxSubsetHeight : subsetHeight; |
| + SkIRect rect = SkIRect::MakeXYWH(blockX * subsetWidth, |
| + blockY * subsetHeight, currSubsetWidth, currSubsetHeight); |
| + decoder->decodeSubset(&bitmap, rect, fColorType); |
| + } |
| + } |
| + } |
| + } |
| +} |