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