reland r14391 ('fix contents of render_pictures JSON summary')

BUG=skia:2043,skia:2044,skia:1942,skia:2466
R=caryclark@google.com
TBR=caryclark

Author: epoger@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@14443 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/PictureRenderer.cpp b/tools/PictureRenderer.cpp
index e531a30..c712ae6 100644
--- a/tools/PictureRenderer.cpp
+++ b/tools/PictureRenderer.cpp
@@ -323,26 +323,17 @@
 }
 
 /**
- * Write the canvas to the specified path.
+ * Write the canvas to an image file and/or JSON summary.
  *
  * @param canvas Must be non-null. Canvas to be written to a file.
- * @param outputDir If nonempty, write the binary image to a file within this directory.
+ * @param outputDir If nonempty, write the binary image to a file within this directory;
+ *     if empty, don't write out the image at all.
  * @param inputFilename If we are writing out a binary image, use this to build its filename.
- * @param jsonSummaryPtr If not null, add image results to this summary.
+ * @param jsonSummaryPtr If not null, add image results (checksum) to this summary.
  * @param useChecksumBasedFilenames If true, use checksum-based filenames when writing to disk.
  * @param tileNumberPtr If not null, which tile number this image contains.
- * @return bool True if the Canvas is written to a file.
  *
- * TODO(epoger): Right now, all canvases must pass through this function in order to be appended
- * to the ImageResultsSummary.  We need some way to add bitmaps to the ImageResultsSummary
- * even if --writePath has not been specified (and thus this function is not called).
- *
- * One fix would be to pass in these path elements separately, and allow this function to be
- * called even if --writePath was not specified...
- *  const char *outputDir   // NULL if we don't want to write image files to disk
- *  const char *filename    // name we use within JSON summary, and as the filename within outputDir
- *
- * UPDATE: Now that outputDir and inputFilename are passed separately, we should be able to do that.
+ * @return bool True if the operation completed successfully.
  */
 static bool write(SkCanvas* canvas, const SkString& outputDir, const SkString& inputFilename,
                   ImageResultsSummary *jsonSummaryPtr, bool useChecksumBasedFilenames,
@@ -407,8 +398,10 @@
                             hash, tileNumberPtr);
     }
 
-    SkASSERT(!outputDir.isEmpty()); // TODO(epoger): we want to remove this constraint,
-                                    // as noted above
+    if (outputDir.isEmpty()) {
+        return true;
+    }
+
     SkString dirPath;
     if (outputSubdirPtr) {
         dirPath = SkOSPath::SkPathJoin(outputDir.c_str(), outputSubdirPtr);
@@ -470,16 +463,13 @@
     pipeCanvas->drawPicture(*fPicture);
     writer.endRecording();
     fCanvas->flush();
-    if (!fOutputDir.isEmpty()) {
-        return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
-                     fUseChecksumBasedFilenames);
-    }
     if (NULL != out) {
         *out = SkNEW(SkBitmap);
         setup_bitmap(*out, fPicture->width(), fPicture->height());
         fCanvas->readPixels(*out, 0, 0);
     }
-    return true;
+    return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
+                 fUseChecksumBasedFilenames);
 }
 
 SkString PipePictureRenderer::getConfigNameInternal() {
@@ -503,18 +493,13 @@
 
     fCanvas->drawPicture(*fPicture);
     fCanvas->flush();
-    if (!fOutputDir.isEmpty()) {
-        return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
-                     fUseChecksumBasedFilenames);
-    }
-
     if (NULL != out) {
         *out = SkNEW(SkBitmap);
         setup_bitmap(*out, fPicture->width(), fPicture->height());
         fCanvas->readPixels(*out, 0, 0);
     }
-
-    return true;
+    return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
+                 fUseChecksumBasedFilenames);
 }
 
 SkString SimplePictureRenderer::getConfigNameInternal() {
@@ -722,10 +707,8 @@
     bool success = true;
     for (int i = 0; i < fTileRects.count(); ++i) {
         draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture);
-        if (!fOutputDir.isEmpty()) {
-            success &= write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
-                             fUseChecksumBasedFilenames, &i);
-        }
+        success &= write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
+                         fUseChecksumBasedFilenames, &i);
         if (NULL != out) {
             if (fCanvas->readPixels(&bitmap, 0, 0)) {
                 // Add this tile to the entire bitmap.
@@ -807,9 +790,8 @@
 
         for (int i = fStart; i < fEnd; i++) {
             draw_tile_to_canvas(fCanvas, fRects[i], fClone);
-            if ((!fOutputDir.isEmpty())
-                && !write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
-                          fUseChecksumBasedFilenames, &i)
+            if (!write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr,
+                       fUseChecksumBasedFilenames, &i)
                 && fSuccess != NULL) {
                 *fSuccess = false;
                 // If one tile fails to write to a file, do not continue drawing the rest.