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" |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 9 | #include "CodecBenchPriv.h" |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCodec.h" |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 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 |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 19 | fName.printf("Codec_%s_%s", baseName.c_str(), color_type_to_str(colorType)); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 20 | #ifdef SK_DEBUG |
| 21 | // Ensure that we can create an SkCodec from this data. |
| 22 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData)); |
| 23 | SkASSERT(codec); |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | const char* CodecBench::onGetName() { |
| 28 | return fName.c_str(); |
| 29 | } |
| 30 | |
| 31 | bool CodecBench::isSuitableFor(Backend backend) { |
| 32 | return kNonRendering_Backend == backend; |
| 33 | } |
| 34 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 35 | void CodecBench::onDelayedSetup() { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 36 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData)); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 37 | |
| 38 | fInfo = codec->getInfo().makeColorType(fColorType); |
| 39 | SkAlphaType alphaType; |
| 40 | // Caller should not have created this CodecBench if the alpha type was |
| 41 | // invalid. |
| 42 | SkAssertResult(SkColorTypeValidateAlphaType(fColorType, fInfo.alphaType(), |
| 43 | &alphaType)); |
| 44 | if (alphaType != fInfo.alphaType()) { |
| 45 | fInfo = fInfo.makeAlphaType(alphaType); |
| 46 | } |
| 47 | |
| 48 | fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes())); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 49 | } |
| 50 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 51 | void CodecBench::onDraw(int n, SkCanvas* canvas) { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 52 | SkAutoTDelete<SkCodec> codec; |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 53 | SkPMColor colorTable[256]; |
| 54 | int colorCount; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 55 | for (int i = 0; i < n; i++) { |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 56 | colorCount = 256; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 57 | codec.reset(SkCodec::NewFromData(fData)); |
| 58 | #ifdef SK_DEBUG |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 59 | const SkCodec::Result result = |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 60 | #endif |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 61 | codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | nullptr, colorTable, &colorCount); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 63 | SkASSERT(result == SkCodec::kSuccess |
| 64 | || result == SkCodec::kIncompleteInput); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 65 | } |
| 66 | } |