| Index: bench/SubsetZoomBench.cpp
|
| diff --git a/bench/SubsetZoomBench.cpp b/bench/SubsetZoomBench.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0da31c5c1a717e11cf53d6ec3be3d6dd7df1106e
|
| --- /dev/null
|
| +++ b/bench/SubsetZoomBench.cpp
|
| @@ -0,0 +1,147 @@
|
| +/*
|
| + * 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 "SubsetZoomBench.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.
|
| + * Choose subsets to mimic a user zooming in or out on a photo.
|
| + *
|
| + */
|
| +
|
| +SubsetZoomBench* SubsetZoomBench::Create(SkString* path,
|
| + SkColorType colorType,
|
| + uint32_t subsetWidth,
|
| + uint32_t subsetHeight,
|
| + 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 SubsetZoomBench(path, colorType, subsetWidth, subsetHeight, useCodec);
|
| +}
|
| +
|
| +SubsetZoomBench::SubsetZoomBench(SkString* path,
|
| + SkColorType colorType,
|
| + uint32_t subsetWidth,
|
| + uint32_t subsetHeight,
|
| + bool useCodec)
|
| + : fColorType(colorType)
|
| + , fSubsetWidth(subsetWidth)
|
| + , fSubsetHeight(subsetHeight)
|
| + , 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";
|
| + break;
|
| + case kAlpha_8_SkColorType:
|
| + colorName = "Alpha8";
|
| + break;
|
| + default:
|
| + colorName = "Unknown";
|
| + }
|
| + fName.printf("%sSubsetScale_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth,
|
| + fSubsetHeight, baseName.c_str(), colorName);
|
| +
|
| + // Perform the decode setup
|
| + SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str()));
|
| + fStream.reset(new SkMemoryStream(encoded));
|
| +}
|
| +
|
| +const char* SubsetZoomBench::onGetName() {
|
| + return fName.c_str();
|
| +}
|
| +
|
| +bool SubsetZoomBench::isSuitableFor(Backend backend) {
|
| + return kNonRendering_Backend == backend;
|
| +}
|
| +
|
| +void SubsetZoomBench::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 int centerX = info.width() / 2;
|
| + const int centerY = info.height() / 2;
|
| + int w = fSubsetWidth;
|
| + int h = fSubsetHeight;
|
| + do {
|
| + const int subsetStartX = SkTMax(0, centerX - w / 2);
|
| + const int subsetStartY = SkTMax(0, centerY - h / 2);
|
| + const int subsetWidth = SkTMin(w, info.width() - subsetStartX);
|
| + const int subsetHeight = SkTMin(h, info.height() - subsetStartY);
|
| + // Note that if we subsetted 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(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);
|
| +
|
| + const int centerX = width / 2;
|
| + const int centerY = height / 2;
|
| + int w = fSubsetWidth;
|
| + int h = fSubsetHeight;
|
| + do {
|
| + const int subsetStartX = SkTMax(0, centerX - w / 2);
|
| + const int subsetStartY = SkTMax(0, centerY - h / 2);
|
| + const int subsetWidth = SkTMin(w, width - subsetStartX);
|
| + const int subsetHeight = SkTMin(h, height - subsetStartY);
|
| + SkBitmap bitmap;
|
| + SkIRect rect = SkIRect::MakeXYWH(subsetStartX, subsetStartY, subsetWidth,
|
| + subsetHeight);
|
| + decoder->decodeSubset(&bitmap, rect, fColorType);
|
| + w <<= 1;
|
| + h <<= 1;
|
| + } while (w < 2 * width || h < 2 * height);
|
| + }
|
| + }
|
| +}
|
|
|