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