OLD | NEW |
(Empty) | |
| 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 susbe
tW x subsetH. |
| 20 * |
| 21 */ |
| 22 |
| 23 SubsetTranslateBench* SubsetTranslateBench::Create(SkString* path, |
| 24 SkColorType colorType, |
| 25 uint32_t subsetWidth, |
| 26 uint32_t subsetHeight, |
| 27 bool useCodec) { |
| 28 // Check that the decode will succeed |
| 29 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| 30 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| 31 if (!valid_subset_bench(stream, colorType, useCodec)) { |
| 32 return NULL; |
| 33 } |
| 34 return new SubsetTranslateBench(path, colorType, subsetWidth, subsetHeight,
useCodec); |
| 35 } |
| 36 |
| 37 SubsetTranslateBench::SubsetTranslateBench(SkString* path, |
| 38 SkColorType colorType, |
| 39 uint32_t subsetWidth, |
| 40 uint32_t subsetHeight, |
| 41 bool useCodec) |
| 42 : fColorType(colorType) |
| 43 , fSubsetWidth(subsetWidth) |
| 44 , fSubsetHeight(subsetHeight) |
| 45 , fUseCodec(useCodec) |
| 46 { |
| 47 // Parse the filename |
| 48 SkString baseName = SkOSPath::Basename(path->c_str()); |
| 49 |
| 50 // Choose an informative color name |
| 51 const char* colorName; |
| 52 switch(fColorType) { |
| 53 case kN32_SkColorType: |
| 54 colorName = "N32"; |
| 55 break; |
| 56 case kRGB_565_SkColorType: |
| 57 colorName = "565"; |
| 58 break; |
| 59 case kGray_8_SkColorType: |
| 60 colorName = "Gray8"; |
| 61 break; |
| 62 case kIndex_8_SkColorType: |
| 63 colorName = "Gray8"; |
| 64 break; |
| 65 case kAlpha_8_SkColorType: |
| 66 colorName = "Alpha8"; |
| 67 break; |
| 68 default: |
| 69 colorName = "Unknown"; |
| 70 } |
| 71 fName.printf("%sSubsetTranslate_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image",
fSubsetWidth, |
| 72 fSubsetHeight, baseName.c_str(), colorName); |
| 73 |
| 74 // Perform the decode setup |
| 75 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| 76 fStream.reset(new SkMemoryStream(encoded)); |
| 77 } |
| 78 |
| 79 const char* SubsetTranslateBench::onGetName() { |
| 80 return fName.c_str(); |
| 81 } |
| 82 |
| 83 bool SubsetTranslateBench::isSuitableFor(Backend backend) { |
| 84 return kNonRendering_Backend == backend; |
| 85 } |
| 86 |
| 87 void SubsetTranslateBench::onDraw(const int n, SkCanvas* canvas) { |
| 88 if (fUseCodec) { |
| 89 for (int count = 0; count < n; count++) { |
| 90 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplica
te())); |
| 91 const SkImageInfo info = codec->getInfo(); |
| 92 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowByte
s())); |
| 93 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info)
; |
| 94 |
| 95 SkBitmap bitmap; |
| 96 // Note that we use the same bitmap for all of the subsets. |
| 97 // It might be larger than necessary for the end subsets. |
| 98 if (!bitmap.tryAllocPixels(info.makeWH(fSubsetWidth, fSubsetHeight))
) { |
| 99 SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| 100 return; |
| 101 } |
| 102 |
| 103 for (int x = 0; x < info.width(); x += fSubsetWidth) { |
| 104 for (int y = 0; y < info.height(); y += fSubsetHeight) { |
| 105 scanlineDecoder->skipScanlines(y); |
| 106 const uint32_t currSubsetWidth = |
| 107 x + (int) fSubsetWidth > info.width() ? |
| 108 info.width() - x : fSubsetWidth; |
| 109 const uint32_t currSubsetHeight = |
| 110 y + (int) fSubsetHeight > info.height() ? |
| 111 info.height() - y : fSubsetHeight; |
| 112 const uint32_t bpp = info.bytesPerPixel(); |
| 113 for (uint32_t y = 0; y < currSubsetHeight; y++) { |
| 114 scanlineDecoder->getScanlines(row.get(), 1, 0); |
| 115 memcpy(bitmap.getAddr(0, y), row.get() + x * bpp, |
| 116 currSubsetWidth * bpp); |
| 117 } |
| 118 } |
| 119 } |
| 120 } |
| 121 } else { |
| 122 for (int count = 0; count < n; count++) { |
| 123 int width, height; |
| 124 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStrea
m)); |
| 125 decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 126 SkBitmap bitmap; |
| 127 // Note that we use the same bitmap for all of the subsets. |
| 128 // It might be larger than necessary for the end subsets. |
| 129 if (!bitmap.tryAllocPixels(SkImageInfo::Make(fSubsetWidth, fSubsetHe
ight, |
| 130 fColorType, kOpaque_SkAlphaType))) { |
| 131 SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| 132 return; |
| 133 } |
| 134 |
| 135 for (int x = 0; x < width; x += fSubsetWidth) { |
| 136 for (int y = 0; y < height; y += fSubsetHeight) { |
| 137 const uint32_t currSubsetWidth = x + (int) fSubsetWidth > wi
dth ? |
| 138 width - x : fSubsetWidth; |
| 139 const uint32_t currSubsetHeight = y + (int) fSubsetHeight >
height ? |
| 140 height - y : fSubsetHeight; |
| 141 SkIRect rect = SkIRect::MakeXYWH(x, y, currSubsetWidth, |
| 142 currSubsetHeight); |
| 143 decoder->decodeSubset(&bitmap, rect, fColorType); |
| 144 } |
| 145 } |
| 146 } |
| 147 } |
| 148 } |
OLD | NEW |