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 | |
| 8 | #include "SubsetTranslateBench.h" |
| 9 | #include "SubsetBenchPriv.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkCodec.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkOSFile.h" |
| 14 | #include "SkStream.h" |
| 15 | |
| 16 | /* |
| 17 | * |
| 18 | * This benchmark is designed to test the performance of subset decoding. |
| 19 | * It uses input dimensions to decode the entire image where each block is susbetW x subsetH. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | SubsetTranslateBench::SubsetTranslateBench(const SkString& path, |
| 24 | SkColorType colorType, |
| 25 | uint32_t subsetWidth, |
| 26 | uint32_t subsetHeight, |
| 27 | bool useCodec) |
| 28 | : fColorType(colorType) |
| 29 | , fSubsetWidth(subsetWidth) |
| 30 | , fSubsetHeight(subsetHeight) |
| 31 | , fUseCodec(useCodec) |
| 32 | { |
| 33 | // Parse the filename |
| 34 | SkString baseName = SkOSPath::Basename(path.c_str()); |
| 35 | |
| 36 | // Choose an informative color name |
| 37 | const char* colorName = get_color_name(fColorType); |
| 38 | |
| 39 | fName.printf("%sSubsetTranslate_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth, |
| 40 | fSubsetHeight, baseName.c_str(), colorName); |
| 41 | |
| 42 | // Perform the decode setup |
| 43 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 44 | fStream.reset(new SkMemoryStream(encoded)); |
| 45 | } |
| 46 | |
| 47 | const char* SubsetTranslateBench::onGetName() { |
| 48 | return fName.c_str(); |
| 49 | } |
| 50 | |
| 51 | bool SubsetTranslateBench::isSuitableFor(Backend backend) { |
| 52 | return kNonRendering_Backend == backend; |
| 53 | } |
| 54 | |
| 55 | void SubsetTranslateBench::onDraw(const int n, SkCanvas* canvas) { |
| 56 | // When the color type is kIndex8, we will need to store the color table. If it is |
| 57 | // used, it will be initialized by the codec. |
| 58 | int colorCount; |
| 59 | SkPMColor colors[256]; |
| 60 | if (fUseCodec) { |
| 61 | for (int count = 0; count < n; count++) { |
| 62 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate())); |
| 63 | const SkImageInfo info = codec->getInfo().makeColorType(fColorType); |
| 64 | SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes())); |
| 65 | SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder( |
| 66 | info, NULL, colors, &colorCount); |
| 67 | |
| 68 | SkBitmap bitmap; |
| 69 | // Note that we use the same bitmap for all of the subsets. |
| 70 | // It might be larger than necessary for the end subsets. |
msarett | 7f6283b | 2015-06-30 13:29:37 -0700 | [diff] [blame^] | 71 | SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight); |
| 72 | alloc_pixels(&bitmap, subsetInfo, colors, colorCount); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 73 | |
| 74 | for (int x = 0; x < info.width(); x += fSubsetWidth) { |
| 75 | for (int y = 0; y < info.height(); y += fSubsetHeight) { |
| 76 | scanlineDecoder->skipScanlines(y); |
| 77 | const uint32_t currSubsetWidth = |
| 78 | x + (int) fSubsetWidth > info.width() ? |
| 79 | info.width() - x : fSubsetWidth; |
| 80 | const uint32_t currSubsetHeight = |
| 81 | y + (int) fSubsetHeight > info.height() ? |
| 82 | info.height() - y : fSubsetHeight; |
| 83 | const uint32_t bpp = info.bytesPerPixel(); |
| 84 | for (uint32_t y = 0; y < currSubsetHeight; y++) { |
| 85 | scanlineDecoder->getScanlines(row.get(), 1, 0); |
| 86 | memcpy(bitmap.getAddr(0, y), row.get() + x * bpp, |
| 87 | currSubsetWidth * bpp); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } else { |
| 93 | // We create a color table here to satisfy allocPixels() when the output |
| 94 | // type is kIndex8. It's okay that this is uninitialized since we never |
| 95 | // use it. |
| 96 | SkColorTable* colorTable = SkNEW_ARGS(SkColorTable, (colors, 0)); |
| 97 | for (int count = 0; count < n; count++) { |
| 98 | int width, height; |
| 99 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 100 | decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 101 | SkBitmap bitmap; |
| 102 | // Note that we use the same bitmap for all of the subsets. |
| 103 | // It might be larger than necessary for the end subsets. |
| 104 | // If we do not include this step, decodeSubset() would allocate space |
| 105 | // for the pixels automatically, but this would not allow us to reuse the |
| 106 | // same bitmap as the other subsets. We want to reuse the same bitmap |
| 107 | // because it gives a more fair comparison with SkCodec and is a common |
| 108 | // use case of BitmapRegionDecoder. |
| 109 | bitmap.allocPixels(SkImageInfo::Make(fSubsetWidth, fSubsetHeight, |
| 110 | fColorType, kOpaque_SkAlphaType), NULL, colorTable); |
| 111 | |
| 112 | for (int x = 0; x < width; x += fSubsetWidth) { |
| 113 | for (int y = 0; y < height; y += fSubsetHeight) { |
| 114 | const uint32_t currSubsetWidth = x + (int) fSubsetWidth > width ? |
| 115 | width - x : fSubsetWidth; |
| 116 | const uint32_t currSubsetHeight = y + (int) fSubsetHeight > height ? |
| 117 | height - y : fSubsetHeight; |
| 118 | SkIRect rect = SkIRect::MakeXYWH(x, y, currSubsetWidth, |
| 119 | currSubsetHeight); |
| 120 | decoder->decodeSubset(&bitmap, rect, fColorType); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |