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