Chromium Code Reviews| Index: bench/CodecSubsetBench.cpp |
| diff --git a/bench/CodecSubsetBench.cpp b/bench/CodecSubsetBench.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..188880548a4e2ab4f87e3b21c3d71b0216d85a9c |
| --- /dev/null |
| +++ b/bench/CodecSubsetBench.cpp |
| @@ -0,0 +1,340 @@ |
| +/* |
| + * 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 "CodecSubsetBench.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. |
| + * |
| + */ |
| +CodecSubsetBench::CodecSubsetBench(SkString* path, |
| + SubsetMode subsetMode, |
| + SkColorType colorType, |
| + uint32_t subsetWidth, |
| + uint32_t subsetHeight, |
| + uint32_t offsetLeft, |
| + uint32_t offsetTop, |
| + uint32_t divisor, |
| + bool useCodec) |
| + : fSubsetMode(subsetMode) |
| + , fColorType(colorType) |
| + , fSubsetWidth(subsetWidth) |
| + , fSubsetHeight(subsetHeight) |
| + , fOffsetLeft(offsetLeft) |
| + , fOffsetTop(offsetTop) |
| + , fDivisor(divisor) |
| + , fUseCodec(useCodec) |
| +{ |
| + // Parse the filename |
| + SkString baseName = SkOSPath::Basename(path->c_str()); |
| + |
| + // Choose an informative subset mode name |
| + SkString modeName; |
| + switch(fSubsetMode) { |
| + case kSingle_SubsetMode: |
| + SkASSERT(0 == divisor); |
| + modeName.printf("Single_%dx%d +%d_+%d", fSubsetWidth, fSubsetHeight, fOffsetLeft, |
| + fOffsetTop); |
| + break; |
| + case kDivisor_SubsetMode: |
| + SkASSERT(0 == subsetWidth); |
| + SkASSERT(0 == subsetHeight); |
| + SkASSERT(0 == offsetLeft); |
| + SkASSERT(0 == offsetTop); |
| + modeName.printf("Divisor_%dx%d", fDivisor, fDivisor); |
| + break; |
| + case kTranslate_SubsetMode: |
| + SkASSERT(0 == divisor); |
| + SkASSERT(0 == offsetLeft); |
| + SkASSERT(0 == offsetTop); |
| + modeName.printf("Size_%dx%d", fSubsetWidth, fSubsetHeight); |
| + break; |
| + case kScale_SubsetMode: |
| + SkASSERT(0 == divisor); |
| + SkASSERT(0 == offsetLeft); |
| + SkASSERT(0 == offsetTop); |
| + modeName.printf("Scale_%dx%d", fSubsetWidth, fSubsetHeight); |
| + break; |
| + } |
| + |
| + // Choose an informative color name |
| + const char* colorName; |
| + switch(fColorType) { |
|
scroggo
2015/05/29 15:36:02
What about kIndex8?
msarett
2015/06/01 14:03:25
Agreed!
|
| + case kN32_SkColorType: |
| + colorName = "N32"; |
| + break; |
| + case kRGB_565_SkColorType: |
| + colorName = "565"; |
| + break; |
| + case kGray_8_SkColorType: |
| + colorName = "Gray8"; |
| + break; |
| + case kAlpha_8_SkColorType: |
| + colorName = "Alpha8"; |
| + break; |
| + default: |
| + colorName = "Unknown"; |
| + } |
| + fName.printf("%sSubset%s_%s_%s", fUseCodec ? "Codec" : "Image", modeName.c_str(), |
| + baseName.c_str(), colorName); |
| + |
| + // Perform the decode setup |
| + SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| + fStream.reset(new SkMemoryStream(encoded)); |
| +} |
| + |
| +const char* CodecSubsetBench::onGetName() { |
| + return fName.c_str(); |
| +} |
| + |
| +bool CodecSubsetBench::isSuitableFor(Backend backend) { |
| + return kNonRendering_Backend == backend; |
| +} |
| + |
| +void CodecSubsetBench::onDraw(const int n, SkCanvas* canvas) { |
| + // Choose a different benchmark depending on the mode |
| + switch(fSubsetMode) { |
|
scroggo
2015/05/29 15:36:02
This giant switch statement might argue for using
msarett
2015/06/01 14:03:25
Agreed.
|
| + case kSingle_SubsetMode: |
| + 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); |
| + |
| + SkBitmap bitmap; |
| + if (!bitmap.tryAllocPixels(info.makeWH(fSubsetWidth, fSubsetHeight))) { |
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + scanlineDecoder->skipScanlines(fOffsetTop); |
| + uint32_t bpp = info.bytesPerPixel(); |
| + for (uint32_t y = 0; y < fSubsetHeight; y++) { |
| + scanlineDecoder->getScanlines(row.get(), 1, 0); |
| + memcpy(bitmap.getAddr(0, y), row.get() + fOffsetLeft * bpp, |
| + fSubsetWidth * bpp); |
| + } |
| + } |
| + } else { |
| + for (int count = 0; count < n; count++) { |
| + SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| + int width, height; |
| + decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
|
scroggo
2015/05/29 15:36:02
Do we need to safely early exit in case buildTileI
msarett
2015/06/01 14:03:24
Let's check that the decode succeeds before creati
|
| + SkBitmap bitmap; |
| + SkIRect rect = SkIRect::MakeXYWH(fOffsetLeft, fOffsetTop, fSubsetWidth, |
| + fSubsetHeight); |
| + decoder->decodeSubset(&bitmap, rect, fColorType); |
| + } |
| + } |
| + break; |
| + case kDivisor_SubsetMode: |
| + 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); |
| + |
| + uint32_t subsetWidth = info.width() / fDivisor; |
|
scroggo
2015/05/29 15:36:02
These variables could all be const?
msarett
2015/06/01 14:03:25
Done.
|
| + uint32_t subsetHeight = info.height() / fDivisor; |
| + uint32_t maxSubsetWidth = subsetWidth + info.width() % fDivisor; |
| + 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))) { |
| + 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); |
| + uint32_t currSubsetWidth = |
| + (blockX == fDivisor - 1) ? maxSubsetWidth : subsetWidth; |
| + uint32_t currSubsetHeight = |
| + (blockY == fDivisor - 1) ? maxSubsetHeight : subsetHeight; |
| + 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() + fOffsetLeft * 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); |
| + |
| + uint32_t subsetWidth = width / fDivisor; |
| + uint32_t subsetHeight = height / fDivisor; |
| + uint32_t maxSubsetWidth = subsetWidth + width % fDivisor; |
| + 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, |
|
scroggo
2015/05/29 15:36:02
When using SkImageDecoder, you don't need to alloc
msarett
2015/06/01 14:03:24
The reason I allocate here is so that SkImageDecod
scroggo
2015/06/01 17:25:14
Got it. We talked about this in person and you con
msarett
2015/06/01 20:37:01
Done.
|
| + 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++) { |
| + uint32_t currSubsetWidth = |
| + (blockX == fDivisor - 1) ? maxSubsetWidth : subsetWidth; |
| + uint32_t currSubsetHeight = |
| + (blockY == fDivisor - 1) ? maxSubsetHeight : subsetHeight; |
| + SkIRect rect = SkIRect::MakeXYWH(blockX * subsetWidth, |
| + blockY * subsetHeight, currSubsetWidth, currSubsetHeight); |
| + decoder->decodeSubset(&bitmap, rect, fColorType); |
| + } |
| + } |
| + } |
| + } |
| + break; |
| + case kTranslate_SubsetMode: |
| + 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); |
| + |
| + SkBitmap bitmap; |
| + // Note that we use the same bitmap for all of the subsets. |
| + // It might be larger than necessary for the end subsets. |
| + if (!bitmap.tryAllocPixels(info.makeWH(fSubsetWidth, fSubsetHeight))) { |
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + for (int x = 0; x < info.width(); x += fSubsetWidth) { |
| + for (int y = 0; y < info.height(); y += fSubsetHeight) { |
| + scanlineDecoder->skipScanlines(y); |
| + uint32_t currSubsetWidth = x + (int) fSubsetWidth > info.width() ? |
| + info.width() - x : fSubsetWidth; |
| + uint32_t currSubsetHeight = y + (int) fSubsetHeight > info.height() ? |
| + info.height() - y : fSubsetHeight; |
| + 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() + x * 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); |
| + SkBitmap bitmap; |
| + // Note that we use the same bitmap for all of the subsets. |
| + // It might be larger than necessary for the end subsets. |
| + if (!bitmap.tryAllocPixels(SkImageInfo::Make(fSubsetWidth, fSubsetHeight, |
|
scroggo
2015/05/29 15:36:02
Again, this is not necessary.
msarett
2015/06/01 14:03:25
Acknowledged.
|
| + fColorType, kOpaque_SkAlphaType))) { |
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + for (int x = 0; x < width; x += fSubsetWidth) { |
| + for (int y = 0; y < height; y += fSubsetHeight) { |
| + uint32_t currSubsetWidth = x + (int) fSubsetWidth > width ? |
| + width - x : fSubsetWidth; |
| + uint32_t currSubsetHeight = y + (int) fSubsetHeight > height ? |
| + height - y : fSubsetHeight; |
| + SkIRect rect = SkIRect::MakeXYWH(x, y, currSubsetWidth, |
| + currSubsetHeight); |
| + decoder->decodeSubset(&bitmap, rect, fColorType); |
| + } |
| + } |
| + } |
| + } |
| + break; |
| + case kScale_SubsetMode: |
| + 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); |
| + |
| + int centerX = info.width() / 2; |
| + int centerY = info.height() / 2; |
| + int w = fSubsetWidth; |
| + int h = fSubsetHeight; |
| + do { |
| + int subsetStartX = SkTMax(0, centerX - w / 2); |
| + int subsetStartY = SkTMax(0, centerY - h / 2); |
| + int subsetWidth = SkTMin(w, info.width() - subsetStartX); |
| + int subsetHeight = SkTMin(h, info.height() - subsetStartY); |
| + // Note that if we subseted and scaled in a single step, we could use the |
|
scroggo
2015/05/29 15:36:02
subsetted? (Technically not a verb, but it seems l
msarett
2015/06/01 14:03:25
Done.
|
| + // same bitmap - as is often done in actual use cases. |
| + SkBitmap bitmap; |
| + if (!bitmap.tryAllocPixels(info.makeWH(subsetWidth, subsetHeight))) { |
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + uint32_t bpp = info.bytesPerPixel(); |
| + scanlineDecoder->skipScanlines(subsetStartY); |
| + for (int y = 0; y < subsetHeight; y++) { |
| + scanlineDecoder->getScanlines(row.get(), 1, 0); |
| + memcpy(bitmap.getAddr(0, y), row.get() + subsetStartX * bpp, |
| + subsetWidth * bpp); |
| + } |
| + w <<= 1; |
| + h <<= 1; |
| + } while (w < 2 * info.width() || h < 2 * info.height()); |
| + } |
| + } else { |
| + for (int count = 0; count < n; count++) { |
| + int width, height; |
| + SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| + decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| + |
| + int centerX = width / 2; |
| + int centerY = height / 2; |
| + int w = fSubsetWidth; |
| + int h = fSubsetHeight; |
| + do { |
| + int subsetStartX = SkTMax(0, centerX - w / 2); |
| + int subsetStartY = SkTMax(0, centerY - h / 2); |
| + int subsetWidth = SkTMin(w, width - subsetStartX); |
| + int subsetHeight = SkTMin(h, height - subsetStartY); |
| + // Note that if we subseted and scaled in a single step, we could use the |
| + // same bitmap - as is often done in actual use cases. |
| + SkBitmap bitmap; |
| + if (!bitmap.tryAllocPixels(SkImageInfo::Make(subsetWidth, subsetHeight, |
|
scroggo
2015/05/29 15:36:03
Again, not needed. There are some cases where a bi
msarett
2015/06/01 14:03:24
Agreed that this allocation is unnecessary here.
|
| + fColorType, kOpaque_SkAlphaType))) { |
| + SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| + return; |
| + } |
| + |
| + SkIRect rect = SkIRect::MakeXYWH(subsetStartX, subsetStartY, subsetWidth, |
| + subsetHeight); |
| + decoder->decodeSubset(&bitmap, rect, fColorType); |
| + w <<= 1; |
| + h <<= 1; |
| + } while (w < 2 * width || h < 2 * height); |
| + } |
| + } |
| + break; |
| + } |
| +} |