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