| 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 |  | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/CodecBench.h" | 
 | 9 | #include "bench/CodecBenchPriv.h" | 
 | 10 | #include "include/codec/SkCodec.h" | 
 | 11 | #include "include/core/SkBitmap.h" | 
 | 12 | #include "src/core/SkOSFile.h" | 
 | 13 | #include "tools/flags/CommandLineFlags.h" | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 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. | 
| Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 16 | static DEFINE_bool(zero_init, false, | 
 | 17 |                    "Pretend our destination is zero-intialized, simulating Android?"); | 
| mtklein | c146aa6 | 2016-01-08 14:20:36 -0800 | [diff] [blame] | 18 |  | 
| msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 19 | CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType, | 
 | 20 |         SkAlphaType alphaType) | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 21 |     : fColorType(colorType) | 
| msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 22 |     , fAlphaType(alphaType) | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 23 |     , fData(SkRef(encoded)) | 
 | 24 | { | 
 | 25 |     // Parse filename and the color type to give the benchmark a useful name | 
| msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 26 |     fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType), | 
 | 27 |             alpha_type_to_str(alphaType)); | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 28 |     // Ensure that we can create an SkCodec from this data. | 
| Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 29 |     SkASSERT(SkCodec::MakeFromData(fData)); | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 30 | } | 
 | 31 |  | 
 | 32 | const char* CodecBench::onGetName() { | 
 | 33 |     return fName.c_str(); | 
 | 34 | } | 
 | 35 |  | 
 | 36 | bool CodecBench::isSuitableFor(Backend backend) { | 
 | 37 |     return kNonRendering_Backend == backend; | 
 | 38 | } | 
 | 39 |  | 
| joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 40 | void CodecBench::onDelayedSetup() { | 
| Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 41 |     std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fData); | 
| scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 42 |  | 
| msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 43 |     fInfo = codec->getInfo().makeColorType(fColorType) | 
 | 44 |                             .makeAlphaType(fAlphaType) | 
 | 45 |                             .makeColorSpace(nullptr); | 
| scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 46 |  | 
| Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 47 |     fPixelStorage.reset(fInfo.computeMinByteSize()); | 
| 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) { | 
| Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 51 |     std::unique_ptr<SkCodec> codec; | 
| mtklein | c146aa6 | 2016-01-08 14:20:36 -0800 | [diff] [blame] | 52 |     SkCodec::Options options; | 
 | 53 |     if (FLAGS_zero_init) { | 
 | 54 |         options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; | 
 | 55 |     } | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 56 |     for (int i = 0; i < n; i++) { | 
| Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 57 |         codec = SkCodec::MakeFromData(fData); | 
| scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 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(), | 
| Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 62 |                          &options); | 
| 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 | } |