scroggo | 60869a4 | 2015-04-01 12:09:17 -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 "CodecBench.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCodec.h" |
| 11 | #include "SkImageGenerator.h" |
| 12 | #include "SkOSFile.h" |
| 13 | |
| 14 | CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType) |
| 15 | : fColorType(colorType) |
| 16 | , fData(SkRef(encoded)) |
| 17 | { |
| 18 | // Parse filename and the color type to give the benchmark a useful name |
| 19 | const char* colorName; |
| 20 | switch(colorType) { |
| 21 | case kN32_SkColorType: |
| 22 | colorName = "N32"; |
| 23 | break; |
| 24 | case kRGB_565_SkColorType: |
| 25 | colorName = "565"; |
| 26 | break; |
| 27 | case kAlpha_8_SkColorType: |
| 28 | colorName = "Alpha8"; |
| 29 | break; |
| 30 | default: |
| 31 | colorName = "Unknown"; |
| 32 | } |
| 33 | fName.printf("Codec_%s_%s", baseName.c_str(), colorName); |
| 34 | #ifdef SK_DEBUG |
| 35 | // Ensure that we can create an SkCodec from this data. |
| 36 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData)); |
| 37 | SkASSERT(codec); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | const char* CodecBench::onGetName() { |
| 42 | return fName.c_str(); |
| 43 | } |
| 44 | |
| 45 | bool CodecBench::isSuitableFor(Backend backend) { |
| 46 | return kNonRendering_Backend == backend; |
| 47 | } |
| 48 | |
| 49 | void CodecBench::onPreDraw() { |
| 50 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData)); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 51 | |
| 52 | fInfo = codec->getInfo().makeColorType(fColorType); |
| 53 | SkAlphaType alphaType; |
| 54 | // Caller should not have created this CodecBench if the alpha type was |
| 55 | // invalid. |
| 56 | SkAssertResult(SkColorTypeValidateAlphaType(fColorType, fInfo.alphaType(), |
| 57 | &alphaType)); |
| 58 | if (alphaType != fInfo.alphaType()) { |
| 59 | fInfo = fInfo.makeAlphaType(alphaType); |
| 60 | } |
| 61 | |
| 62 | fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes())); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void CodecBench::onDraw(const int n, SkCanvas* canvas) { |
| 66 | SkAutoTDelete<SkCodec> codec; |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 67 | SkPMColor colorTable[256]; |
| 68 | int colorCount; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 69 | for (int i = 0; i < n; i++) { |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 70 | colorCount = 256; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 71 | codec.reset(SkCodec::NewFromData(fData)); |
| 72 | #ifdef SK_DEBUG |
| 73 | const SkImageGenerator::Result result = |
| 74 | #endif |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 75 | codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(), |
| 76 | NULL, colorTable, &colorCount); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 77 | SkASSERT(result == SkImageGenerator::kSuccess |
| 78 | || result == SkImageGenerator::kIncompleteInput); |
| 79 | } |
| 80 | } |