msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 8 | #include "CodecBenchPriv.h" |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 9 | #include "SubsetSingleBench.h" |
| 10 | #include "SubsetBenchPriv.h" |
| 11 | #include "SkData.h" |
| 12 | #include "SkCodec.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkOSFile.h" |
| 15 | #include "SkStream.h" |
| 16 | |
| 17 | /* |
| 18 | * |
| 19 | * This benchmark is designed to test the performance of subset decoding. |
| 20 | * It uses an input width, height, left, and top to decode a single subset. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | SubsetSingleBench::SubsetSingleBench(const SkString& path, |
| 25 | SkColorType colorType, |
| 26 | uint32_t subsetWidth, |
| 27 | uint32_t subsetHeight, |
| 28 | uint32_t offsetLeft, |
| 29 | uint32_t offsetTop, |
| 30 | bool useCodec) |
| 31 | : fColorType(colorType) |
| 32 | , fSubsetWidth(subsetWidth) |
| 33 | , fSubsetHeight(subsetHeight) |
| 34 | , fOffsetLeft(offsetLeft) |
| 35 | , fOffsetTop(offsetTop) |
| 36 | , fUseCodec(useCodec) |
| 37 | { |
| 38 | // Parse the filename |
| 39 | SkString baseName = SkOSPath::Basename(path.c_str()); |
| 40 | |
| 41 | // Choose an informative color name |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 42 | const char* colorName = color_type_to_str(fColorType); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 43 | |
| 44 | fName.printf("%sSubsetSingle_%dx%d +%d_+%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth, |
| 45 | fSubsetHeight, fOffsetLeft, fOffsetTop, baseName.c_str(), colorName); |
| 46 | |
| 47 | // Perform the decode setup |
| 48 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 49 | fStream.reset(new SkMemoryStream(encoded)); |
| 50 | } |
| 51 | |
| 52 | const char* SubsetSingleBench::onGetName() { |
| 53 | return fName.c_str(); |
| 54 | } |
| 55 | |
| 56 | bool SubsetSingleBench::isSuitableFor(Backend backend) { |
| 57 | return kNonRendering_Backend == backend; |
| 58 | } |
| 59 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame^] | 60 | void SubsetSingleBench::onDraw(int n, SkCanvas* canvas) { |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 61 | // When the color type is kIndex8, we will need to store the color table. If it is |
| 62 | // used, it will be initialized by the codec. |
| 63 | int colorCount; |
| 64 | SkPMColor colors[256]; |
| 65 | if (fUseCodec) { |
| 66 | for (int count = 0; count < n; count++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 67 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate())); |
| 68 | const SkImageInfo info = codec->getInfo().makeColorType(fColorType); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 69 | SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 70 | codec->startScanlineDecode(info, nullptr, colors, &colorCount); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 71 | |
| 72 | SkBitmap bitmap; |
msarett | 7f6283b | 2015-06-30 13:29:37 -0700 | [diff] [blame] | 73 | SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight); |
| 74 | alloc_pixels(&bitmap, subsetInfo, colors, colorCount); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 75 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 76 | codec->skipScanlines(fOffsetTop); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 77 | uint32_t bpp = info.bytesPerPixel(); |
| 78 | for (uint32_t y = 0; y < fSubsetHeight; y++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 79 | codec->getScanlines(row.get(), 1, 0); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 80 | memcpy(bitmap.getAddr(0, y), row.get() + fOffsetLeft * bpp, |
| 81 | fSubsetWidth * bpp); |
| 82 | } |
| 83 | } |
| 84 | } else { |
| 85 | for (int count = 0; count < n; count++) { |
| 86 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 87 | int width, height; |
| 88 | decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 89 | SkBitmap bitmap; |
| 90 | SkIRect rect = SkIRect::MakeXYWH(fOffsetLeft, fOffsetTop, fSubsetWidth, |
| 91 | fSubsetHeight); |
| 92 | decoder->decodeSubset(&bitmap, rect, fColorType); |
| 93 | } |
| 94 | } |
| 95 | } |