1 /* 2 * Copyright 2013 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 "SkBenchmark.h" 9 #include "SkBitmap.h" 10 #include "SkData.h" 11 #include "SkForceLinking.h" 12 #include "SkImageDecoder.h" 13 #include "SkOSFile.h" 14 #include "SkStream.h" 15 #include "SkString.h" 16 17 __SK_FORCE_IMAGE_DECODER_LINKING; 18 19 class SkCanvas; 20 21 class ImageDecodeBench : public SkBenchmark { 22 public: 23 ImageDecodeBench(void* p, const char* filename) 24 : INHERITED(p) 25 , fName("image_decode_") 26 , fFilename(filename) 27 , fStream() 28 , fValid(false) { 29 fName.append(SkOSPath::SkBasename(filename)); 30 fIsRendering = false; 31 } 32 33 protected: 34 virtual const char* onGetName() SK_OVERRIDE { 35 return fName.c_str(); 36 } 37 38 virtual void onPreDraw() SK_OVERRIDE { 39 SkFILEStream fileStream(fFilename.c_str()); 40 fValid = fileStream.isValid() && fileStream.getLength() > 0; 41 if (fValid) { 42 const size_t size = fileStream.getLength(); 43 void* data = sk_malloc_throw(size); 44 if (fileStream.read(data, size) < size) { 45 fValid = false; 46 } else { 47 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size)); 48 fStream.setData(skdata.get()); 49 } 50 } 51 } 52 53 virtual void onDraw(SkCanvas*) SK_OVERRIDE { 54 #ifdef SK_DEBUG 55 if (!fValid) { 56 SkDebugf("stream was invalid: %s\n", fName.c_str()); 57 return; 58 } 59 #endif 60 // Decode a bunch of times 61 SkBitmap bm; 62 for (int i = 0; i < SkBENCHLOOP(1000); ++i) { 63 SkDEBUGCODE(bool success =) SkImageDecoder::DecodeStream(&fStream, &bm); 64 #ifdef SK_DEBUG 65 if (!success) { 66 SkDebugf("failed to decode %s\n", fName.c_str()); 67 return; 68 } 69 #endif 70 SkDEBUGCODE(success =) fStream.rewind(); 71 #ifdef SK_DEBUG 72 if (!success) { 73 SkDebugf("failed to rewind %s\n", fName.c_str()); 74 return; 75 } 76 #endif 77 } 78 } 79 80 private: 81 SkString fName; 82 const SkString fFilename; 83 SkMemoryStream fStream; 84 bool fValid; 85 86 typedef SkBenchmark INHERITED; 87 }; 88 89 // These are files which call decodePalette 90 //DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, (p, "/usr/local/google/home/scroggo/Downloads/images/hal_163x90.png")); ) 91 //DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, (p, "/usr/local/google/home/scroggo/Downloads/images/box_19_top-left.png")); ) 92