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