commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 8 | // TODO(djsollen): Rename this whole package (perhaps to "SkMultiDiffer"). |
| 9 | // It's not just for "pdiff" (perceptual diffs)--it's a harness that allows |
| 10 | // the execution of an arbitrary set of difference algorithms. |
| 11 | // See http://skbug.com/2711 ('rename skpdiff') |
| 12 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 13 | #if SK_SUPPORT_OPENCL |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 15 | #define __NO_STD_VECTOR // Uses cl::vectpr instead of std::vectpr |
| 16 | #define __NO_STD_STRING // Uses cl::STRING_CLASS instead of std::string |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 17 | #if SK_BUILD_FOR_MAC |
| 18 | // Note that some macs don't have this header and it can be downloaded from the Khronos registry |
| 19 | # include <OpenCL/cl.hpp> |
| 20 | #else |
| 21 | # include <CL/cl.hpp> |
| 22 | #endif |
| 23 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 24 | #endif |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 25 | |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 26 | #include "SkCommandLineFlags.h" |
| 27 | #include "SkGraphics.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 28 | #include "SkStream.h" |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 29 | #include "SkTDArray.h" |
| 30 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 31 | #include "SkDifferentPixelsMetric.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 32 | #include "SkDiffContext.h" |
| 33 | #include "SkImageDiffer.h" |
zachr@google.com | c0a75a8 | 2013-06-28 15:34:56 +0000 | [diff] [blame] | 34 | #include "SkPMetric.h" |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 35 | #include "skpdiff_util.h" |
| 36 | |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 37 | #include "SkForceLinking.h" |
| 38 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 39 | |
| 40 | // Command line argument definitions go here |
| 41 | DEFINE_bool2(list, l, false, "List out available differs"); |
| 42 | DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default"); |
| 43 | DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>"); |
| 44 | DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>"); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 45 | DEFINE_string2(output, o, "", "Writes a JSON summary of these diffs to file: <filepath>"); |
| 46 | DEFINE_string(alphaDir, "", "If the differ can generate an alpha mask, write it into directory: <dirpath>"); |
| 47 | DEFINE_string(rgbDiffDir, "", "If the differ can generate an image showing the RGB diff at each pixel, write it into directory: <dirpath>"); |
| 48 | DEFINE_string(whiteDiffDir, "", "If the differ can generate an image showing every changed pixel in white, write it into directory: <dirpath>"); |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 49 | DEFINE_bool(jsonp, true, "Output JSON with padding"); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 50 | DEFINE_string(csv, "", "Writes the output of these diffs to a csv file: <filepath>"); |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 51 | DEFINE_int32(threads, -1, "run N threads in parallel [default is derived from CPUs available]"); |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 52 | DEFINE_bool(longnames, false, "Output image names are a combination of baseline and test names"); |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 53 | |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 54 | #if SK_SUPPORT_OPENCL |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 55 | /// A callback for any OpenCL errors |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 56 | static void CL_CALLBACK error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) { |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 57 | SkDebugf("OpenCL error notify: %s\n", errorInfo); |
| 58 | exit(1); |
| 59 | } |
| 60 | |
| 61 | /// Creates a device and context with OpenCL |
| 62 | static bool init_device_and_context(cl::Device* device, cl::Context* context) { |
| 63 | // Query for a platform |
| 64 | cl::vector<cl::Platform> platformList; |
| 65 | cl::Platform::get(&platformList); |
| 66 | SkDebugf("The number of platforms is %u\n", platformList.size()); |
| 67 | |
| 68 | // Print some information about the platform for debugging |
| 69 | cl::Platform& platform = platformList[0]; |
| 70 | cl::STRING_CLASS platformName; |
| 71 | platform.getInfo(CL_PLATFORM_NAME, &platformName); |
| 72 | SkDebugf("Platform index 0 is named %s\n", platformName.c_str()); |
| 73 | |
| 74 | // Query for a device |
| 75 | cl::vector<cl::Device> deviceList; |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 76 | platform.getDevices(CL_DEVICE_TYPE_ALL, &deviceList); |
| 77 | SkDebugf("The number of devices is %u\n", deviceList.size()); |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 78 | |
| 79 | // Print some information about the device for debugging |
| 80 | *device = deviceList[0]; |
| 81 | cl::STRING_CLASS deviceName; |
| 82 | device->getInfo(CL_DEVICE_NAME, &deviceName); |
| 83 | SkDebugf("Device index 0 is named %s\n", deviceName.c_str()); |
| 84 | |
| 85 | // Create a CL context and check for all errors |
| 86 | cl_int contextErr = CL_SUCCESS; |
| 87 | *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr); |
| 88 | if (contextErr != CL_SUCCESS) { |
| 89 | SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr)); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
zachr@google.com | c0a75a8 | 2013-06-28 15:34:56 +0000 | [diff] [blame] | 96 | static bool init_cl_diff(SkImageDiffer* differ) { |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 97 | // Setup OpenCL |
| 98 | cl::Device device; |
| 99 | cl::Context context; |
| 100 | if (!init_device_and_context(&device, &context)) { |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 101 | return false; |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Setup our differ of choice |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 105 | SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ; |
| 106 | return clDiffer->init(device(), context()); |
| 107 | } |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 108 | #endif |
zachr@google.com | c0a75a8 | 2013-06-28 15:34:56 +0000 | [diff] [blame] | 109 | |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 110 | // TODO Find a better home for the diff registry. One possibility is to have the differs self |
| 111 | // register. |
| 112 | |
| 113 | // List here every differ |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 114 | SkDifferentPixelsMetric gDiffPixel; |
zachr@google.com | c0a75a8 | 2013-06-28 15:34:56 +0000 | [diff] [blame] | 115 | SkPMetric gPDiff; |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 116 | |
zachr@google.com | c0a75a8 | 2013-06-28 15:34:56 +0000 | [diff] [blame] | 117 | // A null terminated array of pointer to every differ declared above |
| 118 | SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL }; |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 119 | |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 120 | int tool_main(int argc, char * argv[]); |
| 121 | int tool_main(int argc, char * argv[]) { |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 122 | // Setup command line parsing |
| 123 | SkCommandLineFlags::SetUsage("Compare images using various metrics."); |
| 124 | SkCommandLineFlags::Parse(argc, argv); |
| 125 | |
| 126 | // Needed by various Skia components |
| 127 | SkAutoGraphics ag; |
| 128 | |
| 129 | if (FLAGS_list) { |
| 130 | SkDebugf("Available Metrics:\n"); |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 131 | } |
| 132 | |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 133 | // Figure which differs the user chose, and optionally print them if the user requests it |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 134 | SkTDArray<SkImageDiffer*> chosenDiffers; |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 135 | for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 136 | SkImageDiffer* differ = gDiffers[differIndex]; |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 137 | if (FLAGS_list) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 138 | SkDebugf(" %s", differ->getName()); |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 139 | SkDebugf("\n"); |
| 140 | } |
| 141 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 142 | // Check if this differ was chosen by any of the flags. Initialize them if they were chosen. |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 143 | if (FLAGS_differs.isEmpty()) { |
| 144 | // If no differs were chosen, they all get added |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 145 | if (differ->requiresOpenCL()) { |
| 146 | #if SK_SUPPORT_OPENCL |
| 147 | init_cl_diff(differ); |
| 148 | chosenDiffers.push(differ); |
| 149 | #endif |
| 150 | } else { |
| 151 | chosenDiffers.push(differ); |
| 152 | } |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 153 | } else { |
| 154 | for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 155 | if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) { |
zachr@google.com | d658568 | 2013-07-17 19:29:19 +0000 | [diff] [blame] | 156 | // Initialize OpenCL for the differ if it needs it and support was compiled in. |
| 157 | if (differ->requiresOpenCL()) { |
| 158 | #if SK_SUPPORT_OPENCL |
| 159 | init_cl_diff(differ); |
| 160 | chosenDiffers.push(differ); |
| 161 | #endif |
| 162 | } else { |
| 163 | chosenDiffers.push(differ); |
| 164 | } |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Don't attempt to initialize the differ if we aren't going to use it |
| 172 | if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) { |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | // Validate command line flags |
| 177 | if (!FLAGS_folders.isEmpty()) { |
| 178 | if (2 != FLAGS_folders.count()) { |
| 179 | SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n"); |
| 180 | return 1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (!FLAGS_patterns.isEmpty()) { |
| 185 | if (2 != FLAGS_patterns.count()) { |
| 186 | SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n"); |
| 187 | return 1; |
| 188 | } |
| 189 | } |
| 190 | |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 191 | if (!FLAGS_csv.isEmpty()) { |
| 192 | if (1 != FLAGS_csv.count()) { |
| 193 | SkDebugf("csv flag expects one argument: <csv file>\n"); |
| 194 | return 1; |
| 195 | } |
| 196 | } |
| 197 | |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 198 | if (!FLAGS_alphaDir.isEmpty()) { |
| 199 | if (1 != FLAGS_alphaDir.count()) { |
| 200 | SkDebugf("alphaDir flag expects one argument: <directory>\n"); |
| 201 | return 1; |
| 202 | } |
| 203 | } |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 204 | if (!FLAGS_rgbDiffDir.isEmpty()) { |
| 205 | if (1 != FLAGS_rgbDiffDir.count()) { |
| 206 | SkDebugf("rgbDiffDir flag expects one argument: <directory>\n"); |
| 207 | return 1; |
| 208 | } |
| 209 | } |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 210 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 211 | if (!FLAGS_whiteDiffDir.isEmpty()) { |
| 212 | if (1 != FLAGS_whiteDiffDir.count()) { |
| 213 | SkDebugf("whiteDiffDir flag expects one argument: <directory>\n"); |
| 214 | return 1; |
| 215 | } |
| 216 | } |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 217 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 218 | SkDiffContext ctx; |
| 219 | ctx.setDiffers(chosenDiffers); |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 220 | ctx.setLongNames(FLAGS_longnames); |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 221 | |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 222 | if (!FLAGS_alphaDir.isEmpty()) { |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 223 | ctx.setAlphaMaskDir(SkString(FLAGS_alphaDir[0])); |
| 224 | } |
| 225 | if (!FLAGS_rgbDiffDir.isEmpty()) { |
| 226 | ctx.setRgbDiffDir(SkString(FLAGS_rgbDiffDir[0])); |
| 227 | } |
| 228 | if (!FLAGS_whiteDiffDir.isEmpty()) { |
| 229 | ctx.setWhiteDiffDir(SkString(FLAGS_whiteDiffDir[0])); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 230 | } |
| 231 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 232 | if (FLAGS_threads >= 0) { |
| 233 | ctx.setThreadCount(FLAGS_threads); |
| 234 | } |
| 235 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 236 | // Perform a folder diff if one is requested |
| 237 | if (!FLAGS_folders.isEmpty()) { |
| 238 | ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]); |
| 239 | } |
zachr@google.com | c0a75a8 | 2013-06-28 15:34:56 +0000 | [diff] [blame] | 240 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 241 | // Perform a pattern diff if one is requested |
| 242 | if (!FLAGS_patterns.isEmpty()) { |
| 243 | ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]); |
| 244 | } |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 245 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 246 | // Output to the file specified |
| 247 | if (!FLAGS_output.isEmpty()) { |
| 248 | SkFILEWStream outputStream(FLAGS_output[0]); |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 249 | ctx.outputRecords(outputStream, FLAGS_jsonp); |
zachr@google.com | db54dd3 | 2013-06-27 17:51:35 +0000 | [diff] [blame] | 250 | } |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 251 | |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 252 | if (!FLAGS_csv.isEmpty()) { |
| 253 | SkFILEWStream outputStream(FLAGS_csv[0]); |
| 254 | ctx.outputCsv(outputStream); |
| 255 | } |
| 256 | |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 257 | return 0; |
| 258 | } |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 259 | |
| 260 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
| 261 | int main(int argc, char * argv[]) { |
| 262 | return tool_main(argc, (char**) argv); |
| 263 | } |
| 264 | #endif |