| Index: experimental/skpdiff/main.cpp
 | 
| diff --git a/experimental/skpdiff/main.cpp b/experimental/skpdiff/main.cpp
 | 
| index b9d533df84be954b121c620fc8539bb3d1ac48e2..de1171ff3a47c083eaa15454ec67b995b95ef7fe 100644
 | 
| --- a/experimental/skpdiff/main.cpp
 | 
| +++ b/experimental/skpdiff/main.cpp
 | 
| @@ -11,14 +11,12 @@
 | 
|  
 | 
|  #include "SkCommandLineFlags.h"
 | 
|  #include "SkGraphics.h"
 | 
| -#include "SkPoint.h"
 | 
| -#include "SkOSFile.h"
 | 
| -#include "SkString.h"
 | 
| -#include "SkTArray.h"
 | 
| +#include "SkStream.h"
 | 
|  #include "SkTDArray.h"
 | 
|  
 | 
| -#include "SkImageDiffer.h"
 | 
|  #include "SkCLImageDiffer.h"
 | 
| +#include "SkDiffContext.h"
 | 
| +#include "SkImageDiffer.h"
 | 
|  #include "SkPMetric.h"
 | 
|  #include "skpdiff_util.h"
 | 
|  
 | 
| @@ -30,6 +28,7 @@ DEFINE_bool2(list, l, false, "List out available differs");
 | 
|  DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default");
 | 
|  DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>");
 | 
|  DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>");
 | 
| +DEFINE_string2(output, o, "skpdiff_output.json", "Writes the output of these diffs to output: <output>");
 | 
|  
 | 
|  /// A callback for any OpenCL errors
 | 
|  CL_CALLBACK void error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) {
 | 
| @@ -72,78 +71,6 @@ static bool init_device_and_context(cl::Device* device, cl::Context* context) {
 | 
|      return true;
 | 
|  }
 | 
|  
 | 
| -/// Compares two directories of images with the given differ
 | 
| -static void diff_directories(const char baselinePath[], const char testPath[], SkImageDiffer* differ) {
 | 
| -    // Get the files in the baseline, we will then look for those inside the test path
 | 
| -    SkTArray<SkString> baselineEntries;
 | 
| -    if (!get_directory(baselinePath, &baselineEntries)) {
 | 
| -        SkDebugf("Unable to open path \"%s\"\n", baselinePath);
 | 
| -        return;
 | 
| -    }
 | 
| -
 | 
| -    SkTDArray<int> queuedDiffIDs;
 | 
| -    for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselineIndex++) {
 | 
| -        const char* baseFilename = baselineEntries[baselineIndex].c_str();
 | 
| -        SkDebugf("\n%s\n", baseFilename);
 | 
| -
 | 
| -        // Find the real location of each file to compare
 | 
| -        SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
 | 
| -        SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
 | 
| -
 | 
| -        // Check that the test file exists and is a file
 | 
| -        if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
 | 
| -            // Queue up the comparison with the differ
 | 
| -            int diffID = differ->queueDiffOfFile(baselineFile.c_str(), testFile.c_str());
 | 
| -            if (diffID >= 0) {
 | 
| -                queuedDiffIDs.push(diffID);
 | 
| -                SkDebugf("Result: %f\n", differ->getResult(diffID));
 | 
| -                SkDebugf("POI Count: %i\n", differ->getPointsOfInterestCount(diffID));
 | 
| -                differ->deleteDiff(diffID);
 | 
| -            }
 | 
| -        } else {
 | 
| -            SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
 | 
| -        }
 | 
| -    }
 | 
| -}
 | 
| -
 | 
| -
 | 
| -/// Compares two sets of images identified by glob style patterns with the given differ
 | 
| -static void diff_patterns(const char baselinePattern[], const char testPattern[], SkImageDiffer* differ) {
 | 
| -    // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy
 | 
| -    // to find corresponding images by matching entry indices.
 | 
| -
 | 
| -    SkTArray<SkString> baselineEntries;
 | 
| -    if (!glob_files(baselinePattern, &baselineEntries)) {
 | 
| -        SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
 | 
| -        return;
 | 
| -    }
 | 
| -
 | 
| -    SkTArray<SkString> testEntries;
 | 
| -    if (!glob_files(testPattern, &testEntries)) {
 | 
| -        SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
 | 
| -        return;
 | 
| -    }
 | 
| -
 | 
| -    if (baselineEntries.count() != testEntries.count()) {
 | 
| -        SkDebugf("Baseline and test patterns do not yield corresponding number of files\n");
 | 
| -        return;
 | 
| -    }
 | 
| -
 | 
| -    SkTDArray<int> queuedDiffIDs;
 | 
| -    for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) {
 | 
| -        const char* baselineFilename = baselineEntries[entryIndex].c_str();
 | 
| -        const char* testFilename     = testEntries    [entryIndex].c_str();
 | 
| -        SkDebugf("\n%s %s\n", baselineFilename, testFilename);
 | 
| -
 | 
| -        int diffID = differ->queueDiffOfFile(baselineFilename, testFilename);
 | 
| -        if (diffID >= 0) {
 | 
| -            queuedDiffIDs.push(diffID);
 | 
| -            SkDebugf("Result: %f\n", differ->getResult(diffID));
 | 
| -            SkDebugf("POI Count: %i\n", differ->getPointsOfInterestCount(diffID));
 | 
| -            differ->deleteDiff(diffID);
 | 
| -        }
 | 
| -    }
 | 
| -}
 | 
|  
 | 
|  
 | 
|  static bool init_cl_diff(SkImageDiffer* differ) {
 | 
| @@ -194,21 +121,24 @@ int main(int argc, char** argv) {
 | 
|      }
 | 
|  
 | 
|      // Figure which differs the user chose, and optionally print them if the user requests it
 | 
| -    SkTDArray<int> chosenDiffers;
 | 
| +    SkTDArray<SkImageDiffer*> chosenDiffers;
 | 
|      for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
 | 
| +        SkImageDiffer* differ = gDiffers[differIndex];
 | 
|          if (FLAGS_list) {
 | 
| -            SkDebugf("    %s", gDiffers[differIndex]->getName());
 | 
| +            SkDebugf("    %s", differ->getName());
 | 
|              SkDebugf("\n");
 | 
|          }
 | 
|  
 | 
| -        // Check if this differ was chosen by any of the flags
 | 
| +        // Check if this differ was chosen by any of the flags. Initialize them if they were chosen.
 | 
|          if (FLAGS_differs.isEmpty()) {
 | 
|              // If no differs were chosen, they all get added
 | 
| -            chosenDiffers.push(differIndex);
 | 
| +            chosenDiffers.push(differ);
 | 
| +            gDiffInits[differIndex](differ);
 | 
|          } else {
 | 
|              for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
 | 
| -                if (SkString(FLAGS_differs[flagIndex]).equals(gDiffers[differIndex]->getName())) {
 | 
| -                    chosenDiffers.push(differIndex);
 | 
| +                if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
 | 
| +                    chosenDiffers.push(differ);
 | 
| +                    gDiffInits[differIndex](differ);
 | 
|                      break;
 | 
|                  }
 | 
|              }
 | 
| @@ -235,31 +165,23 @@ int main(int argc, char** argv) {
 | 
|          }
 | 
|      }
 | 
|  
 | 
| -    // TODO Move the differ loop to after the bitmaps are decoded and/or uploaded to the OpenCL
 | 
| -    // device. Those are often the slowest processes and should not be done more than once if it can
 | 
| -    // be helped.
 | 
| -
 | 
| -    // Perform each requested diff
 | 
| -    for (int chosenDifferIndex = 0; chosenDifferIndex < chosenDiffers.count(); chosenDifferIndex++) {
 | 
| -        int differIndex = chosenDiffers[chosenDifferIndex];
 | 
| +    SkDiffContext ctx;
 | 
| +    ctx.setDiffers(chosenDiffers);
 | 
|  
 | 
| -        // Get the chosen differ and say which one they chose
 | 
| -        SkImageDiffer * differ = gDiffers[differIndex];
 | 
| -        SkDebugf("Using metric \"%s\"\n", differ->getName());
 | 
| -
 | 
| -        // Initialize the differ using the global list of init functions that match the list of
 | 
| -        // differs
 | 
| -        gDiffInits[differIndex](differ);
 | 
| +    // Perform a folder diff if one is requested
 | 
| +    if (!FLAGS_folders.isEmpty()) {
 | 
| +        ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
 | 
| +    }
 | 
|  
 | 
| -        // Perform a folder diff if one is requested
 | 
| -        if (!FLAGS_folders.isEmpty()) {
 | 
| -            diff_directories(FLAGS_folders[0], FLAGS_folders[1], differ);
 | 
| -        }
 | 
| +    // Perform a pattern diff if one is requested
 | 
| +    if (!FLAGS_patterns.isEmpty()) {
 | 
| +        ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
 | 
| +    }
 | 
|  
 | 
| -        // Perform a pattern diff if one is requested
 | 
| -        if (!FLAGS_patterns.isEmpty()) {
 | 
| -            diff_patterns(FLAGS_patterns[0], FLAGS_patterns[1], differ);
 | 
| -        }
 | 
| +    // Output to the file specified
 | 
| +    if (!FLAGS_output.isEmpty()) {
 | 
| +        SkFILEWStream outputStream(FLAGS_output[0]);
 | 
| +        ctx.outputRecords(outputStream);
 | 
|      }
 | 
|  
 | 
|      return 0;
 | 
| 
 |