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 "SubsetZoomBench.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 * Choose subsets to mimic a user zooming in or out on a photo. |
| 20 * |
| 21 */ |
| 22 |
| 23 SubsetZoomBench* SubsetZoomBench::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 SubsetZoomBench(path, colorType, subsetWidth, subsetHeight, useCo
dec); |
| 35 } |
| 36 |
| 37 SubsetZoomBench::SubsetZoomBench(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("%sSubsetScale_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image", fSu
bsetWidth, |
| 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* SubsetZoomBench::onGetName() { |
| 80 return fName.c_str(); |
| 81 } |
| 82 |
| 83 bool SubsetZoomBench::isSuitableFor(Backend backend) { |
| 84 return kNonRendering_Backend == backend; |
| 85 } |
| 86 |
| 87 void SubsetZoomBench::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 const int centerX = info.width() / 2; |
| 96 const int centerY = info.height() / 2; |
| 97 int w = fSubsetWidth; |
| 98 int h = fSubsetHeight; |
| 99 do { |
| 100 const int subsetStartX = SkTMax(0, centerX - w / 2); |
| 101 const int subsetStartY = SkTMax(0, centerY - h / 2); |
| 102 const int subsetWidth = SkTMin(w, info.width() - subsetStartX); |
| 103 const int subsetHeight = SkTMin(h, info.height() - subsetStartY)
; |
| 104 // Note that if we subsetted and scaled in a single step, we cou
ld use the |
| 105 // same bitmap - as is often done in actual use cases. |
| 106 SkBitmap bitmap; |
| 107 if (!bitmap.tryAllocPixels(info.makeWH(subsetWidth, subsetHeight
))) { |
| 108 SkDebugf("Could not allocate memory. Aborting bench.\n"); |
| 109 return; |
| 110 } |
| 111 |
| 112 uint32_t bpp = info.bytesPerPixel(); |
| 113 scanlineDecoder->skipScanlines(subsetStartY); |
| 114 for (int y = 0; y < subsetHeight; y++) { |
| 115 scanlineDecoder->getScanlines(row.get(), 1, 0); |
| 116 memcpy(bitmap.getAddr(0, y), row.get() + subsetStartX * bpp, |
| 117 subsetWidth * bpp); |
| 118 } |
| 119 w <<= 1; |
| 120 h <<= 1; |
| 121 } while (w < 2 * info.width() || h < 2 * info.height()); |
| 122 } |
| 123 } else { |
| 124 for (int count = 0; count < n; count++) { |
| 125 int width, height; |
| 126 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStrea
m)); |
| 127 decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 128 |
| 129 const int centerX = width / 2; |
| 130 const int centerY = height / 2; |
| 131 int w = fSubsetWidth; |
| 132 int h = fSubsetHeight; |
| 133 do { |
| 134 const int subsetStartX = SkTMax(0, centerX - w / 2); |
| 135 const int subsetStartY = SkTMax(0, centerY - h / 2); |
| 136 const int subsetWidth = SkTMin(w, width - subsetStartX); |
| 137 const int subsetHeight = SkTMin(h, height - subsetStartY); |
| 138 SkBitmap bitmap; |
| 139 SkIRect rect = SkIRect::MakeXYWH(subsetStartX, subsetStartY, sub
setWidth, |
| 140 subsetHeight); |
| 141 decoder->decodeSubset(&bitmap, rect, fColorType); |
| 142 w <<= 1; |
| 143 h <<= 1; |
| 144 } while (w < 2 * width || h < 2 * height); |
| 145 } |
| 146 } |
| 147 } |
OLD | NEW |