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 "CodecBenchPriv.h" 10 #include "SkBitmap.h" 11 #include "SkCodec.h" 12 #include "SkCommandLineFlags.h" 13 #include "SkOSFile.h" 14 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 18 CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType, 19 SkAlphaType alphaType) 20 : fColorType(colorType) 21 , fAlphaType(alphaType) 22 , fData(SkRef(encoded)) 23 { 24 // Parse filename and the color type to give the benchmark a useful name 25 fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType), 26 alpha_type_to_str(alphaType)); 27 #ifdef SK_DEBUG 28 // Ensure that we can create an SkCodec from this data. 29 std::unique_ptr<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 42 void CodecBench::onDelayedSetup() { 43 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fData)); 44 45 fInfo = codec->getInfo().makeColorType(fColorType) 46 .makeAlphaType(fAlphaType) 47 .makeColorSpace(nullptr); 48 49 fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes())); 50 } 51 52 void CodecBench::onDraw(int n, SkCanvas* canvas) { 53 std::unique_ptr<SkCodec> codec; 54 SkCodec::Options options; 55 if (FLAGS_zero_init) { 56 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; 57 } 58 for (int i = 0; i < n; i++) { 59 codec.reset(SkCodec::NewFromData(fData)); 60 #ifdef SK_DEBUG 61 const SkCodec::Result result = 62 #endif 63 codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(), 64 &options); 65 SkASSERT(result == SkCodec::kSuccess 66 || result == SkCodec::kIncompleteInput); 67 } 68 } 69