Bench baseline for mostly 0 image.

Add a baseline for decoding a mostly 0 image. This is in advance of
https://codereview.chromium.org/24269006/ which provides an option to
skip writing those 0s (as part of BUG=skia:1661). On my Nexus 4, the
benchmark does not slow down after that change.

As suggested in https://codereview.chromium.org/24269006/ add a
resourcePath flag to bench. Will require a change in buildbot in order
to actually use the flag.

Add an image used by the test.

Until https://codereview.chromium.org/24448002 is submitted,
the test will not actually be run by the bots (since it
won't know where to find the file).

BUG=skia:1661
R=djsollen@google.com, mtklein@google.com

Review URL: https://codereview.chromium.org/24440002

git-svn-id: http://skia.googlecode.com/svn/trunk@11461 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/bench/SkBenchmark.cpp b/bench/SkBenchmark.cpp
index 4d361e7..e11ebcf 100644
--- a/bench/SkBenchmark.cpp
+++ b/bench/SkBenchmark.cpp
@@ -1,4 +1,3 @@
-
 /*
  * Copyright 2011 Google Inc.
  *
@@ -15,6 +14,8 @@
 
 template BenchRegistry* BenchRegistry::gHead;
 
+SkString SkBenchmark::gResourcePath;
+
 SkBenchmark::SkBenchmark() {
     fForceAlpha = 0xFF;
     fForceAA = true;
diff --git a/bench/SkBenchmark.h b/bench/SkBenchmark.h
index 811a1db..ec40077 100644
--- a/bench/SkBenchmark.h
+++ b/bench/SkBenchmark.h
@@ -10,6 +10,7 @@
 
 #include "SkRefCnt.h"
 #include "SkPoint.h"
+#include "SkString.h"
 #include "SkTRegistry.h"
 
 #define DEF_BENCH(code) \
@@ -106,6 +107,10 @@
     //   for (int i = 0; i < this->getLoops(); i++) { <work here> }
     int getLoops() const { return fLoops; }
 
+    static void SetResourcePath(const char* resPath) { gResourcePath.set(resPath); }
+
+    static SkString& GetResourcePath() { return gResourcePath; }
+
 protected:
     virtual void setupPaint(SkPaint* paint);
 
@@ -125,6 +130,7 @@
     SkTriState::State  fDither;
     uint32_t    fOrMask, fClearMask;
     int fLoops;
+    static  SkString gResourcePath;
 
     typedef SkRefCnt INHERITED;
 };
diff --git a/bench/SkipZeroesBench.cpp b/bench/SkipZeroesBench.cpp
new file mode 100644
index 0000000..6e4b54b
--- /dev/null
+++ b/bench/SkipZeroesBench.cpp
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBenchmark.h"
+#include "SkBitmap.h"
+#include "SkData.h"
+#include "SkForceLinking.h"
+#include "SkImageDecoder.h"
+#include "SkOSFile.h"
+#include "SkStream.h"
+#include "SkString.h"
+
+__SK_FORCE_IMAGE_DECODER_LINKING;
+
+class SkCanvas;
+
+class SkipZeroesBench : public SkBenchmark {
+public:
+    SkipZeroesBench(const char* filename, bool skipZeroes)
+    : fName("SkipZeroes_")
+    , fDecoder(NULL)
+    , fFilename(filename)
+    , fStream()
+    , fSkipZeroes(skipZeroes)
+    , fValid(false) {
+        fName.append(filename);
+        if (skipZeroes) {
+            fName.append("_skip_zeroes");
+        } else {
+            fName.append("_write_zeroes");
+        }
+        fIsRendering = false;
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return fName.c_str();
+    }
+
+    virtual void onPreDraw() SK_OVERRIDE {
+        const char* resPath = GetResourcePath().c_str();
+        if (NULL == resPath) {
+            fValid = false;
+            return;
+        }
+
+        SkString fullPath = SkOSPath::SkPathJoin(resPath, fFilename.c_str());
+        SkFILEStream fileStream(fullPath.c_str());
+        fValid = fileStream.isValid() && fileStream.getLength() > 0;
+        if (fValid) {
+            const size_t size = fileStream.getLength();
+            void* data = sk_malloc_throw(size);
+            if (fileStream.read(data, size) < size) {
+                fValid = false;
+            } else {
+                SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
+                fStream.setData(skdata.get());
+                fDecoder.reset(SkImageDecoder::Factory(&fStream));
+                if (fDecoder.get()) {
+                    // Disabling until the feature is checked in.
+                    // See https://codereview.chromium.org/24269006/
+                    //fDecoder->setSkipWritingZeroes(fSkipZeroes);
+                } else {
+                    fValid = false;
+                }
+            }
+        }
+    }
+
+    virtual void onDraw(SkCanvas*) SK_OVERRIDE {
+        if (!fValid) {
+#ifdef SK_DEBUG
+            SkDebugf("stream was invalid: %s\n", fFilename.c_str());
+#endif
+            return;
+        }
+        // Decode a bunch of times
+        SkBitmap bm;
+        for (int i = 0; i < this->getLoops(); ++i) {
+            SkDEBUGCODE(bool success =) fDecoder->decode(&fStream, &bm,
+                                                         SkImageDecoder::kDecodePixels_Mode);
+#ifdef SK_DEBUG
+            if (!success) {
+                SkDebugf("failed to decode %s\n", fFilename.c_str());
+                return;
+            }
+#endif
+            SkDEBUGCODE(success =) fStream.rewind();
+#ifdef SK_DEBUG
+            if (!success) {
+                SkDebugf("failed to rewind %s\n", fFilename.c_str());
+                return;
+            }
+#endif
+        }
+    }
+
+private:
+    SkString                        fName;
+    SkAutoTDelete<SkImageDecoder>   fDecoder;
+    const SkString                  fFilename;
+    SkMemoryStream                  fStream;
+    bool                            fSkipZeroes;
+    bool                            fValid;
+
+    typedef SkBenchmark INHERITED;
+};
+
+// Enable the true version once the feature is checked in.
+//DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", true)));
+DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", false)));
diff --git a/bench/benchmain.cpp b/bench/benchmain.cpp
index 78993c1..57dfdd2 100644
--- a/bench/benchmain.cpp
+++ b/bench/benchmain.cpp
@@ -284,6 +284,7 @@
               "Ratio of subsequent bench measurements must drop within 1±error to converge.");
 DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
 DEFINE_bool2(verbose, v, false, "Print more.");
+DEFINE_string2(resourcePath, i, NULL, "directory for test resources.");
 
 // Has this bench converged?  First arguments are milliseconds / loop iteration,
 // last is overall runtime in milliseconds.
@@ -357,6 +358,11 @@
             }
         }
     }
+    // Set the resource path.
+    if (!FLAGS_resourcePath.isEmpty()) {
+        SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]);
+    }
+
 #if SK_SUPPORT_GPU
     for (int i = 0; i < configs.count(); ++i) {
         const Config& config = gConfigs[configs[i]];
diff --git a/gyp/bench.gypi b/gyp/bench.gypi
index 9f4bdb6..32902b4 100644
--- a/gyp/bench.gypi
+++ b/gyp/bench.gypi
@@ -62,6 +62,7 @@
     '../bench/RTreeBench.cpp',
     '../bench/ScalarBench.cpp',
     '../bench/ShaderMaskBench.cpp',
+    '../bench/SkipZeroesBench.cpp',
     '../bench/SortBench.cpp',
     '../bench/StrokeBench.cpp',
     '../bench/TableBench.cpp',
diff --git a/resources/arrow.png b/resources/arrow.png
new file mode 100644
index 0000000..382ef39
--- /dev/null
+++ b/resources/arrow.png
Binary files differ