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