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 "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(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("%sSubsetZoom_%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* SubsetZoomBench::onGetName() { |
| 48 | return fName.c_str(); |
| 49 | } |
| 50 | |
| 51 | bool SubsetZoomBench::isSuitableFor(Backend backend) { |
| 52 | return kNonRendering_Backend == backend; |
| 53 | } |
| 54 | |
| 55 | void SubsetZoomBench::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 | const int centerX = info.width() / 2; |
| 69 | const int centerY = info.height() / 2; |
| 70 | int w = fSubsetWidth; |
| 71 | int h = fSubsetHeight; |
| 72 | do { |
| 73 | const int subsetStartX = SkTMax(0, centerX - w / 2); |
| 74 | const int subsetStartY = SkTMax(0, centerY - h / 2); |
| 75 | const int subsetWidth = SkTMin(w, info.width() - subsetStartX); |
| 76 | const int subsetHeight = SkTMin(h, info.height() - subsetStartY); |
| 77 | // Note that if we subsetted and scaled in a single step, we could use the |
| 78 | // same bitmap - as is often done in actual use cases. |
| 79 | SkBitmap bitmap; |
| 80 | bitmap.allocPixels(info.makeWH(subsetWidth, subsetHeight)); |
| 81 | |
| 82 | uint32_t bpp = info.bytesPerPixel(); |
| 83 | scanlineDecoder->skipScanlines(subsetStartY); |
| 84 | for (int y = 0; y < subsetHeight; y++) { |
| 85 | scanlineDecoder->getScanlines(row.get(), 1, 0); |
| 86 | memcpy(bitmap.getAddr(0, y), row.get() + subsetStartX * bpp, |
| 87 | subsetWidth * bpp); |
| 88 | } |
| 89 | w <<= 1; |
| 90 | h <<= 1; |
| 91 | } while (w < 2 * info.width() || h < 2 * info.height()); |
| 92 | } |
| 93 | } else { |
| 94 | for (int count = 0; count < n; count++) { |
| 95 | int width, height; |
| 96 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 97 | decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 98 | |
| 99 | const int centerX = width / 2; |
| 100 | const int centerY = height / 2; |
| 101 | int w = fSubsetWidth; |
| 102 | int h = fSubsetHeight; |
| 103 | do { |
| 104 | const int subsetStartX = SkTMax(0, centerX - w / 2); |
| 105 | const int subsetStartY = SkTMax(0, centerY - h / 2); |
| 106 | const int subsetWidth = SkTMin(w, width - subsetStartX); |
| 107 | const int subsetHeight = SkTMin(h, height - subsetStartY); |
| 108 | SkBitmap bitmap; |
| 109 | SkIRect rect = SkIRect::MakeXYWH(subsetStartX, subsetStartY, subsetWidth, |
| 110 | subsetHeight); |
| 111 | decoder->decodeSubset(&bitmap, rect, fColorType); |
| 112 | w <<= 1; |
| 113 | h <<= 1; |
| 114 | } while (w < 2 * width || h < 2 * height); |
| 115 | } |
| 116 | } |
| 117 | } |