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 | |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 8 | #include "CodecBenchPriv.h" |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 9 | #include "SubsetZoomBench.h" |
| 10 | #include "SubsetBenchPriv.h" |
| 11 | #include "SkData.h" |
| 12 | #include "SkCodec.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkOSFile.h" |
| 15 | #include "SkStream.h" |
| 16 | |
| 17 | /* |
| 18 | * |
| 19 | * This benchmark is designed to test the performance of subset decoding. |
| 20 | * Choose subsets to mimic a user zooming in or out on a photo. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | SubsetZoomBench::SubsetZoomBench(const SkString& path, |
| 25 | SkColorType colorType, |
| 26 | uint32_t subsetWidth, |
| 27 | uint32_t subsetHeight, |
| 28 | bool useCodec) |
| 29 | : fColorType(colorType) |
| 30 | , fSubsetWidth(subsetWidth) |
| 31 | , fSubsetHeight(subsetHeight) |
| 32 | , fUseCodec(useCodec) |
| 33 | { |
| 34 | // Parse the filename |
| 35 | SkString baseName = SkOSPath::Basename(path.c_str()); |
| 36 | |
| 37 | // Choose an informative color name |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 38 | const char* colorName = color_type_to_str(fColorType); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 39 | |
| 40 | fName.printf("%sSubsetZoom_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth, |
| 41 | fSubsetHeight, baseName.c_str(), colorName); |
| 42 | |
| 43 | // Perform the decode setup |
| 44 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 45 | fStream.reset(new SkMemoryStream(encoded)); |
| 46 | } |
| 47 | |
| 48 | const char* SubsetZoomBench::onGetName() { |
| 49 | return fName.c_str(); |
| 50 | } |
| 51 | |
| 52 | bool SubsetZoomBench::isSuitableFor(Backend backend) { |
| 53 | return kNonRendering_Backend == backend; |
| 54 | } |
| 55 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame^] | 56 | void SubsetZoomBench::onDraw(int n, SkCanvas* canvas) { |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 57 | // When the color type is kIndex8, we will need to store the color table. If it is |
| 58 | // used, it will be initialized by the codec. |
| 59 | int colorCount; |
| 60 | SkPMColor colors[256]; |
| 61 | if (fUseCodec) { |
| 62 | for (int count = 0; count < n; count++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 63 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate())); |
| 64 | const SkImageInfo info = codec->getInfo().makeColorType(fColorType); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 65 | SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 66 | codec->startScanlineDecode(info, nullptr, colors, &colorCount); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 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; |
msarett | 7f6283b | 2015-06-30 13:29:37 -0700 | [diff] [blame] | 80 | SkImageInfo subsetInfo = info.makeWH(subsetWidth, subsetHeight); |
| 81 | alloc_pixels(&bitmap, subsetInfo, colors, colorCount); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 82 | |
| 83 | uint32_t bpp = info.bytesPerPixel(); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 84 | codec->skipScanlines(subsetStartY); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 85 | for (int y = 0; y < subsetHeight; y++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 86 | codec->getScanlines(row.get(), 1, 0); |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 87 | memcpy(bitmap.getAddr(0, y), row.get() + subsetStartX * bpp, |
| 88 | subsetWidth * bpp); |
| 89 | } |
| 90 | w <<= 1; |
| 91 | h <<= 1; |
| 92 | } while (w < 2 * info.width() || h < 2 * info.height()); |
| 93 | } |
| 94 | } else { |
| 95 | for (int count = 0; count < n; count++) { |
| 96 | int width, height; |
| 97 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| 98 | decoder->buildTileIndex(fStream->duplicate(), &width, &height); |
| 99 | |
| 100 | const int centerX = width / 2; |
| 101 | const int centerY = height / 2; |
| 102 | int w = fSubsetWidth; |
| 103 | int h = fSubsetHeight; |
| 104 | do { |
| 105 | const int subsetStartX = SkTMax(0, centerX - w / 2); |
| 106 | const int subsetStartY = SkTMax(0, centerY - h / 2); |
| 107 | const int subsetWidth = SkTMin(w, width - subsetStartX); |
| 108 | const int subsetHeight = SkTMin(h, height - subsetStartY); |
| 109 | SkBitmap bitmap; |
| 110 | SkIRect rect = SkIRect::MakeXYWH(subsetStartX, subsetStartY, subsetWidth, |
| 111 | subsetHeight); |
| 112 | decoder->decodeSubset(&bitmap, rect, fColorType); |
| 113 | w <<= 1; |
| 114 | h <<= 1; |
| 115 | } while (w < 2 * width || h < 2 * height); |
| 116 | } |
| 117 | } |
| 118 | } |