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