Add bench_record.

I got fed up trying to figure out how best to measure recording cost with bench_pictures, render_pictures, etc, so I wrote a new tool.

Features welcome.

BUG=
R=tomhudson@google.com

Author: mtklein@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13037 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/tools.gyp b/gyp/tools.gyp
index ce24d9d..049e393 100644
--- a/gyp/tools.gyp
+++ b/gyp/tools.gyp
@@ -15,6 +15,7 @@
       'dependencies': [
         'bbh_shootout',
         'bench_pictures',
+        'bench_record',
         'filter',
         'lua_app',
         'lua_pictures',
@@ -263,6 +264,16 @@
       ],
     },
     {
+      'target_name': 'bench_record',
+      'type': 'executable',
+      'sources': ['../tools/bench_record.cpp'],
+      'include_dirs': [ '../src/core/' ],
+      'dependencies': [
+        'flags.gyp:flags',
+        'skia_lib.gyp:skia_lib',
+      ],
+    },
+    {
       'target_name': 'picture_renderer',
       'type': 'static_library',
       'sources': [
diff --git a/tools/bench_record.cpp b/tools/bench_record.cpp
new file mode 100644
index 0000000..219a6b9
--- /dev/null
+++ b/tools/bench_record.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCommandLineFlags.h"
+#include "SkForceLinking.h"
+#include "SkGraphics.h"
+#include "SkOSFile.h"
+#include "SkPicture.h"
+#include "SkStream.h"
+#include "SkString.h"
+#include "SkTime.h"
+
+__SK_FORCE_IMAGE_DECODER_LINKING;
+
+// Just reading all the SKPs takes about 2 seconds for me, which is the same as about 100 loops of
+// rerecording all the SKPs.  So we default to --loops=900, which makes ~90% of our time spent in
+// recording, and this should take ~20 seconds to run.
+
+DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record.");
+DEFINE_int32(loops, 900, "Number of times to re-record each SKP.");
+DEFINE_int32(flags, SkPicture::kUsePathBoundsForClip_RecordingFlag, "RecordingFlags to use.");
+DEFINE_bool(endRecording, true, "If false, don't time SkPicture::endRecording()");
+
+int tool_main(int argc, char** argv);
+int tool_main(int argc, char** argv) {
+    SkCommandLineFlags::Parse(argc, argv);
+    SkAutoGraphics autoGraphics;
+
+    SkOSFile::Iter it(FLAGS_skps[0], ".skp");
+    SkString filename;
+    while (it.next(&filename)) {
+        const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
+
+        SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
+        if (!stream) {
+            SkDebugf("Could not read %s.\n", path.c_str());
+            continue;
+        }
+        SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
+        if (!src) {
+            SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
+            continue;
+        }
+
+        const SkMSec start = SkTime::GetMSecs();
+        for (int i = 0; i < FLAGS_loops; i++) {
+            SkPicture dst;
+            src->draw(dst.beginRecording(src->width(), src->height(), FLAGS_flags));
+            if (FLAGS_endRecording) dst.endRecording();
+        }
+
+        const SkMSec elapsed = SkTime::GetMSecs() - start;
+        const double msPerLoop = elapsed / (double)FLAGS_loops;
+        printf("%4.2f\t%s\n", msPerLoop, filename.c_str());
+    }
+
+    return 0;
+}
+
+#if !defined SK_BUILD_FOR_IOS
+int main(int argc, char * const argv[]) {
+    return tool_main(argc, (char**) argv);
+}
+#endif