skia2/bench/subset/SubsetSingleBench.cpp
scroggo eb602a5c94 SkCodec no longer inherits from SkImageGenerator.
SkImageGenerator makes some assumptions that are not necessarily valid
for SkCodec. For example, SkCodec does not assume that it can always be
rewound.

We also have an ongoing question of what an SkCodec should report as
its default settings (i.e. the return from getInfo). It makes sense for
an SkCodec to report that its pixels are unpremultiplied, if that is
the case for the underlying data, but if a client of SkImageGenerator
uses the default settings (as many do), they will receive
unpremultiplied pixels which cannot (currently) be drawn with Skia. We
may ultimately decide to revisit SkCodec reporting an SkImageInfo, but
I have left it unchanged for now.

Import features of SkImageGenerator used by SkCodec into SkCodec.

I have left SkImageGenerator unchanged for now, but it no longer needs
Result or Options. This will require changes to Chromium.

Manually handle the lifetime of fScanlineDecoder, so SkScanlineDecoder.h
can include SkCodec.h (where Result is), and SkCodec.h does not need
to include it (to delete fScanlineDecoder).

In many places, make the following simple changes:
- Now include SkScanlineDecoder.h, which is no longer included by
  SkCodec.h
- Use the enums in SkCodec, rather than SkImageGenerator
- Stop including SkImageGenerator.h where no longer needed

Review URL: https://codereview.chromium.org/1220733013
2015-07-09 08:16:03 -07:00

97 lines
3.5 KiB
C++

/*
* 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 "SubsetSingleBench.h"
#include "SubsetBenchPriv.h"
#include "SkData.h"
#include "SkCodec.h"
#include "SkImageDecoder.h"
#include "SkOSFile.h"
#include "SkScanlineDecoder.h"
#include "SkStream.h"
/*
*
* This benchmark is designed to test the performance of subset decoding.
* It uses an input width, height, left, and top to decode a single subset.
*
*/
SubsetSingleBench::SubsetSingleBench(const SkString& path,
SkColorType colorType,
uint32_t subsetWidth,
uint32_t subsetHeight,
uint32_t offsetLeft,
uint32_t offsetTop,
bool useCodec)
: fColorType(colorType)
, fSubsetWidth(subsetWidth)
, fSubsetHeight(subsetHeight)
, fOffsetLeft(offsetLeft)
, fOffsetTop(offsetTop)
, fUseCodec(useCodec)
{
// Parse the filename
SkString baseName = SkOSPath::Basename(path.c_str());
// Choose an informative color name
const char* colorName = get_color_name(fColorType);
fName.printf("%sSubsetSingle_%dx%d +%d_+%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth,
fSubsetHeight, fOffsetLeft, fOffsetTop, baseName.c_str(), colorName);
// Perform the decode setup
SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
fStream.reset(new SkMemoryStream(encoded));
}
const char* SubsetSingleBench::onGetName() {
return fName.c_str();
}
bool SubsetSingleBench::isSuitableFor(Backend backend) {
return kNonRendering_Backend == backend;
}
void SubsetSingleBench::onDraw(const int n, SkCanvas* canvas) {
// When the color type is kIndex8, we will need to store the color table. If it is
// used, it will be initialized by the codec.
int colorCount;
SkPMColor colors[256];
if (fUseCodec) {
for (int count = 0; count < n; count++) {
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate()));
const SkImageInfo info = codec->getInfo().makeColorType(fColorType);
SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes()));
SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(
info, NULL, colors, &colorCount);
SkBitmap bitmap;
SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight);
alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
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);
SkBitmap bitmap;
SkIRect rect = SkIRect::MakeXYWH(fOffsetLeft, fOffsetTop, fSubsetWidth,
fSubsetHeight);
decoder->decodeSubset(&bitmap, rect, fColorType);
}
}
}