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" |
mtklein | c146aa6 | 2016-01-08 14:20:36 -0800 | [diff] [blame] | 12 | #include "SkCommandLineFlags.h" |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| 14 | |
mtklein | c146aa6 | 2016-01-08 14:20:36 -0800 | [diff] [blame] | 15 | // Actually zeroing the memory would throw off timing, so we just lie. |
| 16 | DEFINE_bool(zero_init, false, "Pretend our destination is zero-intialized, simulating Android?"); |
| 17 | |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 18 | CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType, |
| 19 | SkAlphaType alphaType) |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 20 | : fColorType(colorType) |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 21 | , fAlphaType(alphaType) |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 22 | , fData(SkRef(encoded)) |
| 23 | { |
| 24 | // Parse filename and the color type to give the benchmark a useful name |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 25 | fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType), |
| 26 | alpha_type_to_str(alphaType)); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 27 | #ifdef SK_DEBUG |
| 28 | // Ensure that we can create an SkCodec from this data. |
| 29 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData)); |
| 30 | SkASSERT(codec); |
| 31 | #endif |
| 32 | } |
| 33 | |
| 34 | const char* CodecBench::onGetName() { |
| 35 | return fName.c_str(); |
| 36 | } |
| 37 | |
| 38 | bool CodecBench::isSuitableFor(Backend backend) { |
| 39 | return kNonRendering_Backend == backend; |
| 40 | } |
| 41 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 42 | void CodecBench::onDelayedSetup() { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 43 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData)); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 44 | |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 45 | fInfo = codec->getInfo().makeColorType(fColorType).makeAlphaType(fAlphaType); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 46 | |
| 47 | fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes())); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 48 | } |
| 49 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 50 | void CodecBench::onDraw(int n, SkCanvas* canvas) { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 51 | SkAutoTDelete<SkCodec> codec; |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 52 | SkPMColor colorTable[256]; |
| 53 | int colorCount; |
mtklein | c146aa6 | 2016-01-08 14:20:36 -0800 | [diff] [blame] | 54 | SkCodec::Options options; |
| 55 | if (FLAGS_zero_init) { |
| 56 | options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 57 | } |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 58 | for (int i = 0; i < n; i++) { |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 59 | colorCount = 256; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 60 | codec.reset(SkCodec::NewFromData(fData)); |
| 61 | #ifdef SK_DEBUG |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 62 | const SkCodec::Result result = |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 63 | #endif |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 64 | codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(), |
mtklein | c146aa6 | 2016-01-08 14:20:36 -0800 | [diff] [blame] | 65 | &options, colorTable, &colorCount); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 66 | SkASSERT(result == SkCodec::kSuccess |
| 67 | || result == SkCodec::kIncompleteInput); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 68 | } |
| 69 | } |