Home | History | Annotate | Download | only in bench
      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 SkipZeroesBench : public SkBenchmark {
     22 public:
     23     SkipZeroesBench(const char* filename, bool skipZeroes)
     24     : fName("SkipZeroes_")
     25     , fDecoder(NULL)
     26     , fFilename(filename)
     27     , fStream()
     28     , fSkipZeroes(skipZeroes)
     29     , fValid(false) {
     30         fName.append(SkOSPath::SkBasename(filename));
     31         if (skipZeroes) {
     32             fName.append("_skip_zeroes");
     33         } else {
     34             fName.append("_write_zeroes");
     35         }
     36         fIsRendering = false;
     37     }
     38 
     39     ~SkipZeroesBench() {
     40         SkDELETE(fDecoder);
     41     }
     42 protected:
     43     virtual const char* onGetName() SK_OVERRIDE {
     44         return fName.c_str();
     45     }
     46 
     47     virtual void onPreDraw() SK_OVERRIDE {
     48         SkFILEStream fileStream(fFilename.c_str());
     49         fValid = fileStream.isValid() && fileStream.getLength() > 0;
     50         if (fValid) {
     51             const size_t size = fileStream.getLength();
     52             void* data = sk_malloc_throw(size);
     53             if (fileStream.read(data, size) < size) {
     54                 fValid = false;
     55             } else {
     56                 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
     57                 fStream.setData(skdata.get());
     58                 fDecoder = SkImageDecoder::Factory(&fStream);
     59                 if (fDecoder) {
     60                     fDecoder->setSkipWritingZeroes(fSkipZeroes);
     61                 } else {
     62                     fValid = false;
     63                 }
     64             }
     65         }
     66     }
     67 
     68     virtual void onDraw(SkCanvas*) SK_OVERRIDE {
     69 #ifdef SK_DEBUG
     70         if (!fValid) {
     71             SkDebugf("stream was invalid: %s\n", fName.c_str());
     72             return;
     73         }
     74 #endif
     75         // Decode a bunch of times
     76         SkBitmap bm;
     77         for (int i = 0; i < this->getLoops(); ++i) {
     78             SkDEBUGCODE(bool success =) fDecoder->decode(&fStream, &bm,
     79                                                          SkImageDecoder::kDecodePixels_Mode);
     80 #ifdef SK_DEBUG
     81             if (!success) {
     82                 SkDebugf("failed to decode %s\n", fName.c_str());
     83                 return;
     84             }
     85 #endif
     86             SkDEBUGCODE(success =) fStream.rewind();
     87 #ifdef SK_DEBUG
     88             if (!success) {
     89                 SkDebugf("failed to rewind %s\n", fName.c_str());
     90                 return;
     91             }
     92 #endif
     93         }
     94     }
     95 
     96 private:
     97     SkString        fName;
     98     SkImageDecoder* fDecoder;
     99     const SkString  fFilename;
    100     SkMemoryStream  fStream;
    101     bool            fSkipZeroes;
    102     bool            fValid;
    103 
    104     typedef SkBenchmark INHERITED;
    105 };
    106 
    107 //DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("/sdcard/skia/images/arrow.png", true)));
    108 //DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("/sdcard/skia/images/arrow.png", false)));
    109