diff options
author | 2013-10-16 18:36:49 +0000 | |
---|---|---|
committer | 2013-10-16 18:36:49 +0000 | |
commit | cbbf1ca304d35e3acd944609cf7a1c5127d0ca56 (patch) | |
tree | 65ae91615007755eb46e2c067cda524b48976e69 /tools | |
parent | 9cf0cb169bda63ad50f4c394739deb6c67003647 (diff) |
add initial support ofr multi-threaded execution
Review URL: https://codereview.chromium.org/27541003
git-svn-id: http://skia.googlecode.com/svn/trunk@11823 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tools')
-rw-r--r-- | tools/skpdiff/SkDiffContext.cpp | 56 | ||||
-rw-r--r-- | tools/skpdiff/SkDiffContext.h | 4 | ||||
-rw-r--r-- | tools/skpdiff/skpdiff_main.cpp | 5 |
3 files changed, 56 insertions, 9 deletions
diff --git a/tools/skpdiff/SkDiffContext.cpp b/tools/skpdiff/SkDiffContext.cpp index 88941cab0f..07d304b64c 100644 --- a/tools/skpdiff/SkDiffContext.cpp +++ b/tools/skpdiff/SkDiffContext.cpp @@ -8,8 +8,10 @@ #include "SkBitmap.h" #include "SkImageDecoder.h" #include "SkOSFile.h" +#include "SkRunnable.h" #include "SkStream.h" #include "SkTDict.h" +#include "SkThreadPool.h" #include "SkDiffContext.h" #include "SkImageDiffer.h" @@ -22,6 +24,7 @@ SkDiffContext::SkDiffContext() { fRecords = NULL; fDiffers = NULL; fDifferCount = 0; + fThreadCount = SkThreadPool::kThreadPerCore; } SkDiffContext::~SkDiffContext() { @@ -93,8 +96,29 @@ void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) { differ->deleteDiff(diffID); } } + + // if we get a difference and we want the alpha mask then compute it here. } +class SkThreadedDiff : public SkRunnable { +public: + SkThreadedDiff() : fDiffContext(NULL) { } + + void setup(SkDiffContext* diffContext, const SkString& baselinePath, const SkString& testPath) { + fDiffContext = diffContext; + fBaselinePath = baselinePath; + fTestPath = testPath; + } + + virtual void run() SK_OVERRIDE { + fDiffContext->addDiff(fBaselinePath.c_str(), fTestPath.c_str()); + } + +private: + SkDiffContext* fDiffContext; + SkString fBaselinePath; + SkString fTestPath; +}; void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) { // Get the files in the baseline, we will then look for those inside the test path @@ -104,9 +128,12 @@ void SkDiffContext::diffDirectories(const char baselinePath[], const char testPa return; } - for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselineIndex++) { - SkDebugf("[%i/%i] ", baselineIndex, baselineEntries.count()); - const char* baseFilename = baselineEntries[baselineIndex].c_str(); + SkThreadPool threadPool(fThreadCount); + SkTArray<SkThreadedDiff> runnableDiffs; + runnableDiffs.reset(baselineEntries.count()); + + for (int x = 0; x < baselineEntries.count(); x++) { + const char* baseFilename = baselineEntries[x].c_str(); // Find the real location of each file to compare SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename); @@ -115,11 +142,14 @@ void SkDiffContext::diffDirectories(const char baselinePath[], const char testPa // 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 - this->addDiff(baselineFile.c_str(), testFile.c_str()); + runnableDiffs[x].setup(this, baselineFile, testFile); + threadPool.add(&runnableDiffs[x]); } else { SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str()); } } + + threadPool.wait(); } @@ -144,13 +174,16 @@ void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPa return; } - for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) { - SkDebugf("[%i/%i] ", entryIndex, baselineEntries.count()); - const char* baselineFilename = baselineEntries[entryIndex].c_str(); - const char* testFilename = testEntries [entryIndex].c_str(); + SkThreadPool threadPool(fThreadCount); + SkTArray<SkThreadedDiff> runnableDiffs; + runnableDiffs.reset(baselineEntries.count()); - this->addDiff(baselineFilename, testFilename); + for (int x = 0; x < baselineEntries.count(); x++) { + runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); + threadPool.add(&runnableDiffs[x]); } + + threadPool.wait(); } void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { @@ -164,6 +197,7 @@ void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { while (NULL != currentRecord) { stream.writeText(" {\n"); + SkString differenceAbsPath = get_absolute_path(currentRecord->fDifferencePath); SkString baselineAbsPath = get_absolute_path(currentRecord->fBaselinePath); SkString testAbsPath = get_absolute_path(currentRecord->fTestPath); @@ -181,6 +215,10 @@ void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { stream.writeText(baseName.c_str()); stream.writeText("\",\n"); + stream.writeText(" \"differencePath\": \""); + stream.writeText(differenceAbsPath.c_str()); + stream.writeText("\",\n"); + stream.writeText(" \"baselinePath\": \""); stream.writeText(baselineAbsPath.c_str()); stream.writeText("\",\n"); diff --git a/tools/skpdiff/SkDiffContext.h b/tools/skpdiff/SkDiffContext.h index 93354e8dec..20193b3851 100644 --- a/tools/skpdiff/SkDiffContext.h +++ b/tools/skpdiff/SkDiffContext.h @@ -23,6 +23,8 @@ public: SkDiffContext(); ~SkDiffContext(); + void setThreadCount(int threadCount) { fThreadCount = threadCount; } + /** * Sets the differs to be used in each diff. Already started diffs will not retroactively use * these. @@ -104,6 +106,7 @@ private: }; struct DiffRecord { + SkString fDifferencePath; SkString fBaselinePath; SkString fTestPath; SkTArray<DiffData> fDiffs; @@ -117,6 +120,7 @@ private: SkImageDiffer** fDiffers; int fDifferCount; + int fThreadCount; }; #endif diff --git a/tools/skpdiff/skpdiff_main.cpp b/tools/skpdiff/skpdiff_main.cpp index f89cc372c5..55640f7f0a 100644 --- a/tools/skpdiff/skpdiff_main.cpp +++ b/tools/skpdiff/skpdiff_main.cpp @@ -40,6 +40,7 @@ DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> DEFINE_string2(output, o, "", "Writes the output of these diffs to output: <output>"); DEFINE_bool(jsonp, true, "Output JSON with padding"); DEFINE_string(csv, "", "Writes the output of these diffs to a csv file"); +DEFINE_int32(threads, -1, "run N threads in parallel [default is derived from CPUs available]"); #if SK_SUPPORT_OPENCL /// A callback for any OpenCL errors @@ -188,6 +189,10 @@ int tool_main(int argc, char * argv[]) { SkDiffContext ctx; ctx.setDiffers(chosenDiffers); + if (FLAGS_threads >= 0) { + ctx.setThreadCount(FLAGS_threads); + } + // Perform a folder diff if one is requested if (!FLAGS_folders.isEmpty()) { ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]); |