Check for invalid SkPictures

- Remove hasRecorded() since nobody uses it.
- Add "success" boolean to SkPicture stream constructor
- Track failures in render_pictures and bench_pictures
Review URL: https://codereview.appspot.com/6493105

git-svn-id: http://skia.googlecode.com/svn/trunk@5573 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/bench_pictures_main.cpp b/tools/bench_pictures_main.cpp
index 93b1cf9..d6837e9 100644
--- a/tools/bench_pictures_main.cpp
+++ b/tools/bench_pictures_main.cpp
@@ -97,7 +97,7 @@
 
 SkBenchLogger gLogger;
 
-static void run_single_benchmark(const SkString& inputPath,
+static bool run_single_benchmark(const SkString& inputPath,
                                  sk_tools::PictureBenchmark& benchmark) {
     SkFILEStream inputStream;
 
@@ -106,11 +106,18 @@
         SkString err;
         err.printf("Could not open file %s\n", inputPath.c_str());
         gLogger.logError(err);
-        return;
+        return false;
     }
 
-    SkPicture* picture = SkNEW_ARGS(SkPicture, (&inputStream));
+    bool success = false;
+    SkPicture* picture = SkNEW_ARGS(SkPicture, (&inputStream, &success));
     SkAutoTUnref<SkPicture> aur(picture);
+    if (!success) {
+        SkString err;
+        err.printf("Could not read an SkPicture from %s\n", inputPath.c_str());
+        gLogger.logError(err);
+        return false;
+    }
 
     SkString filename;
     sk_tools::get_basename(&filename, inputPath);
@@ -124,6 +131,7 @@
     sk_tools::resize_if_needed(&aur);
 
     benchmark.run(aur);
+    return true;
 }
 
 static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
@@ -388,19 +396,23 @@
     gLogger.logProgress(commandLine);
 }
 
-static void process_input(const SkString& input, sk_tools::PictureBenchmark& benchmark) {
+static int process_input(const SkString& input,
+                         sk_tools::PictureBenchmark& benchmark) {
     SkOSFile::Iter iter(input.c_str(), "skp");
     SkString inputFilename;
-
+    int failures = 0;
     if (iter.next(&inputFilename)) {
         do {
             SkString inputPath;
             sk_tools::make_filepath(&inputPath, input, inputFilename);
-            run_single_benchmark(inputPath, benchmark);
+            if (!run_single_benchmark(inputPath, benchmark))
+                ++failures;
         } while(iter.next(&inputFilename));
     } else {
-          run_single_benchmark(input, benchmark);
+        if (!run_single_benchmark(input, benchmark))
+            ++failures;
     }
+    return failures;
 }
 
 int main(int argc, char* const argv[]) {
@@ -414,7 +426,15 @@
 
     parse_commandline(argc, argv, &inputs, &benchmark);
 
+    int failures = 0;
     for (int i = 0; i < inputs.count(); ++i) {
-        process_input(inputs[i], benchmark);
+        failures += process_input(inputs[i], benchmark);
+    }
+
+    if (failures != 0) {
+        SkString err;
+        err.printf("Failed to run %i benchmarks.\n", failures);
+        gLogger.logError(err);
+        return 1;
     }
 }
diff --git a/tools/render_pictures_main.cpp b/tools/render_pictures_main.cpp
index 7d54516..5ad0072 100644
--- a/tools/render_pictures_main.cpp
+++ b/tools/render_pictures_main.cpp
@@ -79,7 +79,7 @@
     path->append("png");
 }
 
-static void write_output(const SkString& outputDir, const SkString& inputFilename,
+static bool write_output(const SkString& outputDir, const SkString& inputFilename,
                          const sk_tools::PictureRenderer& renderer) {
     SkString outputPath;
     make_output_filepath(&outputPath, outputDir, inputFilename);
@@ -87,9 +87,10 @@
     if (!isWritten) {
         SkDebugf("Could not write to file %s\n", outputPath.c_str());
     }
+    return isWritten;
 }
 
-static void render_picture(const SkString& inputPath, const SkString& outputDir,
+static bool render_picture(const SkString& inputPath, const SkString& outputDir,
                            sk_tools::PictureRenderer& renderer) {
     SkString inputFilename;
     sk_tools::get_basename(&inputFilename, inputPath);
@@ -98,11 +99,16 @@
     inputStream.setPath(inputPath.c_str());
     if (!inputStream.isValid()) {
         SkDebugf("Could not open file %s\n", inputPath.c_str());
-        return;
+        return false;
     }
 
-    SkPicture* picture = SkNEW_ARGS(SkPicture, (&inputStream));
+    bool success = false;
+    SkPicture* picture = SkNEW_ARGS(SkPicture, (&inputStream, &success));
     SkAutoTUnref<SkPicture> aur(picture);
+    if (!success) {
+        SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
+        return false;
+    }
 
     SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
              inputPath.c_str());
@@ -116,26 +122,30 @@
 
     renderer.resetState();
 
-    write_output(outputDir, inputFilename, renderer);
+    success = write_output(outputDir, inputFilename, renderer);
 
     renderer.end();
+    return success;
 }
 
-static void process_input(const SkString& input, const SkString& outputDir,
+static int process_input(const SkString& input, const SkString& outputDir,
                           sk_tools::PictureRenderer& renderer) {
     SkOSFile::Iter iter(input.c_str(), "skp");
     SkString inputFilename;
-
+    int failures = 0;
     if (iter.next(&inputFilename)) {
         do {
             SkString inputPath;
             sk_tools::make_filepath(&inputPath, input, inputFilename);
-            render_picture(inputPath, outputDir, renderer);
+            if (!render_picture(inputPath, outputDir, renderer))
+              ++failures;
         } while(iter.next(&inputFilename));
     } else {
         SkString inputPath(input);
-        render_picture(inputPath, outputDir, renderer);
+        if (!render_picture(inputPath, outputDir, renderer))
+          ++failures;
     }
+    return failures;
 }
 
 static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
@@ -282,7 +292,7 @@
 }
 
 int main(int argc, char* const argv[]) {
-    SkGraphics::Init();
+    SkAutoGraphics ag;
     SkTArray<SkString> inputs;
     sk_tools::PictureRenderer* renderer = NULL;
 
@@ -290,10 +300,14 @@
     SkString outputDir = inputs[inputs.count() - 1];
     SkASSERT(renderer);
 
+    int failures = 0;
     for (int i = 0; i < inputs.count() - 1; i ++) {
-        process_input(inputs[i], outputDir, *renderer);
+        failures += process_input(inputs[i], outputDir, *renderer);
     }
-
+    if (failures != 0) {
+        SkDebugf("Failed to render %i pictures.\n", failures);
+        return 1;
+    }
 #if SK_SUPPORT_GPU
 #if GR_CACHE_STATS
     if (renderer->isUsingGpuDevice()) {
@@ -305,5 +319,4 @@
 #endif
 
     SkDELETE(renderer);
-    SkGraphics::Term();
 }