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 "SubsetSingleBench.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 an input width, height, left, and top to decode a single subset. |
| 20 * |
| 21 */ |
| 22 |
| 23 SubsetSingleBench* SubsetSingleBench::Create(SkString* path, |
| 24 SkColorType colorType, |
| 25 uint32_t subsetWidth, |
| 26 uint32_t subsetHeight, |
| 27 uint32_t offsetLeft, |
| 28 uint32_t offsetTop, |
| 29 bool useCodec) { |
| 30 // Check that the decode will succeed |
| 31 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| 32 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
| 33 if (!valid_subset_bench(stream, colorType, useCodec)) { |
| 34 return NULL; |
| 35 } |
| 36 return new SubsetSingleBench(path, colorType, subsetWidth, subsetHeight, off
setLeft, |
| 37 offsetTop, useCodec); |
| 38 } |
| 39 |
| 40 SubsetSingleBench::SubsetSingleBench(SkString* path, |
| 41 SkColorType colorType, |
| 42 uint32_t subsetWidth, |
| 43 uint32_t subsetHeight, |
| 44 uint32_t offsetLeft, |
| 45 uint32_t offsetTop, |
| 46 bool useCodec) |
| 47 : fColorType(colorType) |
| 48 , fSubsetWidth(subsetWidth) |
| 49 , fSubsetHeight(subsetHeight) |
| 50 , fOffsetLeft(offsetLeft) |
| 51 , fOffsetTop(offsetTop) |
| 52 , fUseCodec(useCodec) |
| 53 { |
| 54 // Parse the filename |
| 55 SkString baseName = SkOSPath::Basename(path->c_str()); |
| 56 |
| 57 // Choose an informative color name |
| 58 const char* colorName; |
| 59 switch(fColorType) { |
| 60 case kN32_SkColorType: |
| 61 colorName = "N32"; |
| 62 break; |
| 63 case kRGB_565_SkColorType: |
| 64 colorName = "565"; |
| 65 break; |
| 66 case kGray_8_SkColorType: |
| 67 colorName = "Gray8"; |
| 68 break; |
| 69 case kIndex_8_SkColorType: |
| 70 colorName = "Gray8"; |
| 71 break; |
| 72 case kAlpha_8_SkColorType: |
| 73 colorName = "Alpha8"; |
| 74 break; |
| 75 default: |
| 76 colorName = "Unknown"; |
| 77 } |
| 78 fName.printf("%sSubsetSingle_%dx%d +%d_+%d_%s_%s", fUseCodec ? "Codec" : "Im
age", fSubsetWidth, |
| 79 fSubsetHeight, fOffsetLeft, fOffsetTop, baseName.c_str(), colorName)
; |
| 80 |
| 81 // Perform the decode setup |
| 82 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path->c_str())); |
| 83 fStream.reset(new SkMemoryStream(encoded)); |
| 84 } |
| 85 |
| 86 const char* SubsetSingleBench::onGetName() { |
| 87 return fName.c_str(); |
| 88 } |
| 89 |
| 90 bool SubsetSingleBench::isSuitableFor(Backend backend) { |
| 91 return kNonRendering_Backend == backend; |
| 92 } |
| 93 |
| 94 void SubsetSingleBench::onDraw(const int n, SkCanvas* canvas) { |
| 95 if (fUseCodec) { |
| 96 for (int count = 0; count < n; count++) { |
| 97 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplica
te())); |
| 98 const SkImageInfo info = codec->getInfo(); |
| 99 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowByte
s())); |
| 100 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info)
; |
| 101 |
| 102 SkBitmap bitmap; |
| 103 if (!bitmap.tryAllocPixels(info.makeWH(fSubsetWidth, fSubsetHeight))
) { |
| 104 SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| 105 return; |
| 106 } |
| 107 |
| 108 scanlineDecoder->skipScanlines(fOffsetTop); |
| 109 uint32_t bpp = info.bytesPerPixel(); |
| 110 for (uint32_t y = 0; y < fSubsetHeight; y++) { |
| 111 scanlineDecoder->getScanlines(row.get(), 1, 0); |
| 112 memcpy(bitmap.getAddr(0, y), row.get() + fOffsetLeft * bpp, |
| 113 fSubsetWidth * bpp); |
| 114 } |
| 115 } |
| 116 } else { |
| 117 for (int count = 0; count < n; count++) { |
| 118 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStrea
m)); |
| 119 int width, height; |
| 120 decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 121 SkBitmap bitmap; |
| 122 SkIRect rect = SkIRect::MakeXYWH(fOffsetLeft, fOffsetTop, fSubsetWid
th, |
| 123 fSubsetHeight); |
| 124 decoder->decodeSubset(&bitmap, rect, fColorType); |
| 125 } |
| 126 } |
| 127 } |
OLD | NEW |