blob: 3d1bcda0f475159b3c36c203ffa0487c8dac4658 [file] [log] [blame]
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +00001/*
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
epoger54f1ad82014-07-02 07:43:04 -07008// 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
zachr@google.comd6585682013-07-17 19:29:19 +000013#if SK_SUPPORT_OPENCL
zachr@google.com35f02fb2013-07-22 17:05:24 +000014
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000015#define __NO_STD_VECTOR // Uses cl::vectpr instead of std::vectpr
16#define __NO_STD_STRING // Uses cl::STRING_CLASS instead of std::string
zachr@google.com35f02fb2013-07-22 17:05:24 +000017#if SK_BUILD_FOR_MAC
18// Note that some macs don't have this header and it can be downloaded from the Khronos registry
19# include <OpenCL/cl.hpp>
20#else
21# include <CL/cl.hpp>
22#endif
23
zachr@google.comd6585682013-07-17 19:29:19 +000024#endif
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000025
zachr@google.comdb54dd32013-06-27 17:51:35 +000026#include "SkCommandLineFlags.h"
27#include "SkGraphics.h"
zachr@google.com945708a2013-07-02 19:55:32 +000028#include "SkStream.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000029#include "SkTDArray.h"
30
zachr@google.comd6585682013-07-17 19:29:19 +000031#include "SkDifferentPixelsMetric.h"
zachr@google.com945708a2013-07-02 19:55:32 +000032#include "SkDiffContext.h"
33#include "SkImageDiffer.h"
zachr@google.comc0a75a82013-06-28 15:34:56 +000034#include "SkPMetric.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000035#include "skpdiff_util.h"
36
zachr@google.comdb54dd32013-06-27 17:51:35 +000037#include "SkForceLinking.h"
38__SK_FORCE_IMAGE_DECODER_LINKING;
39
40// Command line argument definitions go here
41DEFINE_bool2(list, l, false, "List out available differs");
42DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default");
43DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>");
44DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>");
epoger54f1ad82014-07-02 07:43:04 -070045DEFINE_string2(output, o, "", "Writes a JSON summary of these diffs to file: <filepath>");
46DEFINE_string(alphaDir, "", "If the differ can generate an alpha mask, write it into directory: <dirpath>");
47DEFINE_string(rgbDiffDir, "", "If the differ can generate an image showing the RGB diff at each pixel, write it into directory: <dirpath>");
48DEFINE_string(whiteDiffDir, "", "If the differ can generate an image showing every changed pixel in white, write it into directory: <dirpath>");
zachr@google.coma95959c2013-07-08 15:04:45 +000049DEFINE_bool(jsonp, true, "Output JSON with padding");
epoger54f1ad82014-07-02 07:43:04 -070050DEFINE_string(csv, "", "Writes the output of these diffs to a csv file: <filepath>");
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000051DEFINE_int32(threads, -1, "run N threads in parallel [default is derived from CPUs available]");
zachr@google.comdb54dd32013-06-27 17:51:35 +000052
zachr@google.comd6585682013-07-17 19:29:19 +000053#if SK_SUPPORT_OPENCL
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000054/// A callback for any OpenCL errors
zachr@google.com35f02fb2013-07-22 17:05:24 +000055static void CL_CALLBACK error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000056 SkDebugf("OpenCL error notify: %s\n", errorInfo);
57 exit(1);
58}
59
60/// Creates a device and context with OpenCL
61static bool init_device_and_context(cl::Device* device, cl::Context* context) {
62 // Query for a platform
63 cl::vector<cl::Platform> platformList;
64 cl::Platform::get(&platformList);
65 SkDebugf("The number of platforms is %u\n", platformList.size());
66
67 // Print some information about the platform for debugging
68 cl::Platform& platform = platformList[0];
69 cl::STRING_CLASS platformName;
70 platform.getInfo(CL_PLATFORM_NAME, &platformName);
71 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
72
73 // Query for a device
74 cl::vector<cl::Device> deviceList;
zachr@google.com35f02fb2013-07-22 17:05:24 +000075 platform.getDevices(CL_DEVICE_TYPE_ALL, &deviceList);
76 SkDebugf("The number of devices is %u\n", deviceList.size());
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000077
78 // Print some information about the device for debugging
79 *device = deviceList[0];
80 cl::STRING_CLASS deviceName;
81 device->getInfo(CL_DEVICE_NAME, &deviceName);
82 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
83
84 // Create a CL context and check for all errors
85 cl_int contextErr = CL_SUCCESS;
86 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
87 if (contextErr != CL_SUCCESS) {
88 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
89 return false;
90 }
91
92 return true;
93}
94
zachr@google.comc0a75a82013-06-28 15:34:56 +000095static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000096 // Setup OpenCL
97 cl::Device device;
98 cl::Context context;
99 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +0000100 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000101 }
102
103 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +0000104 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
105 return clDiffer->init(device(), context());
106}
zachr@google.comd6585682013-07-17 19:29:19 +0000107#endif
zachr@google.comc0a75a82013-06-28 15:34:56 +0000108
zachr@google.comdb54dd32013-06-27 17:51:35 +0000109// TODO Find a better home for the diff registry. One possibility is to have the differs self
110// register.
111
112// List here every differ
zachr@google.comd6585682013-07-17 19:29:19 +0000113SkDifferentPixelsMetric gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +0000114SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000115
zachr@google.comc0a75a82013-06-28 15:34:56 +0000116// A null terminated array of pointer to every differ declared above
117SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000118
zachr@google.com35f02fb2013-07-22 17:05:24 +0000119int tool_main(int argc, char * argv[]);
120int tool_main(int argc, char * argv[]) {
zachr@google.comdb54dd32013-06-27 17:51:35 +0000121 // Setup command line parsing
122 SkCommandLineFlags::SetUsage("Compare images using various metrics.");
123 SkCommandLineFlags::Parse(argc, argv);
124
125 // Needed by various Skia components
126 SkAutoGraphics ag;
127
128 if (FLAGS_list) {
129 SkDebugf("Available Metrics:\n");
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000130 }
131
zachr@google.comdb54dd32013-06-27 17:51:35 +0000132 // Figure which differs the user chose, and optionally print them if the user requests it
zachr@google.com945708a2013-07-02 19:55:32 +0000133 SkTDArray<SkImageDiffer*> chosenDiffers;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000134 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000135 SkImageDiffer* differ = gDiffers[differIndex];
zachr@google.comdb54dd32013-06-27 17:51:35 +0000136 if (FLAGS_list) {
zachr@google.com945708a2013-07-02 19:55:32 +0000137 SkDebugf(" %s", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000138 SkDebugf("\n");
139 }
140
zachr@google.com945708a2013-07-02 19:55:32 +0000141 // Check if this differ was chosen by any of the flags. Initialize them if they were chosen.
zachr@google.comdb54dd32013-06-27 17:51:35 +0000142 if (FLAGS_differs.isEmpty()) {
143 // If no differs were chosen, they all get added
zachr@google.comd6585682013-07-17 19:29:19 +0000144 if (differ->requiresOpenCL()) {
145#if SK_SUPPORT_OPENCL
146 init_cl_diff(differ);
147 chosenDiffers.push(differ);
148#endif
149 } else {
150 chosenDiffers.push(differ);
151 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000152 } else {
153 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000154 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
zachr@google.comd6585682013-07-17 19:29:19 +0000155 // Initialize OpenCL for the differ if it needs it and support was compiled in.
156 if (differ->requiresOpenCL()) {
157#if SK_SUPPORT_OPENCL
158 init_cl_diff(differ);
159 chosenDiffers.push(differ);
160#endif
161 } else {
162 chosenDiffers.push(differ);
163 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000164 break;
165 }
166 }
167 }
168 }
169
170 // Don't attempt to initialize the differ if we aren't going to use it
171 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
172 return 0;
173 }
174
175 // Validate command line flags
176 if (!FLAGS_folders.isEmpty()) {
177 if (2 != FLAGS_folders.count()) {
178 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
179 return 1;
180 }
181 }
182
183 if (!FLAGS_patterns.isEmpty()) {
184 if (2 != FLAGS_patterns.count()) {
185 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
186 return 1;
187 }
188 }
189
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000190 if (!FLAGS_csv.isEmpty()) {
191 if (1 != FLAGS_csv.count()) {
192 SkDebugf("csv flag expects one argument: <csv file>\n");
193 return 1;
194 }
195 }
196
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000197 if (!FLAGS_alphaDir.isEmpty()) {
198 if (1 != FLAGS_alphaDir.count()) {
199 SkDebugf("alphaDir flag expects one argument: <directory>\n");
200 return 1;
201 }
202 }
epoger54f1ad82014-07-02 07:43:04 -0700203 if (!FLAGS_rgbDiffDir.isEmpty()) {
204 if (1 != FLAGS_rgbDiffDir.count()) {
205 SkDebugf("rgbDiffDir flag expects one argument: <directory>\n");
206 return 1;
207 }
208 }
209 if (!FLAGS_whiteDiffDir.isEmpty()) {
210 if (1 != FLAGS_whiteDiffDir.count()) {
211 SkDebugf("whiteDiffDir flag expects one argument: <directory>\n");
212 return 1;
213 }
214 }
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000215
zachr@google.com945708a2013-07-02 19:55:32 +0000216 SkDiffContext ctx;
217 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000218
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000219 if (!FLAGS_alphaDir.isEmpty()) {
epoger54f1ad82014-07-02 07:43:04 -0700220 ctx.setAlphaMaskDir(SkString(FLAGS_alphaDir[0]));
221 }
222 if (!FLAGS_rgbDiffDir.isEmpty()) {
223 ctx.setRgbDiffDir(SkString(FLAGS_rgbDiffDir[0]));
224 }
225 if (!FLAGS_whiteDiffDir.isEmpty()) {
226 ctx.setWhiteDiffDir(SkString(FLAGS_whiteDiffDir[0]));
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000227 }
228
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000229 if (FLAGS_threads >= 0) {
230 ctx.setThreadCount(FLAGS_threads);
231 }
232
zachr@google.com945708a2013-07-02 19:55:32 +0000233 // Perform a folder diff if one is requested
234 if (!FLAGS_folders.isEmpty()) {
235 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
236 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000237
zachr@google.com945708a2013-07-02 19:55:32 +0000238 // Perform a pattern diff if one is requested
239 if (!FLAGS_patterns.isEmpty()) {
240 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
241 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000242
zachr@google.com945708a2013-07-02 19:55:32 +0000243 // Output to the file specified
244 if (!FLAGS_output.isEmpty()) {
245 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000246 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000247 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000248
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000249 if (!FLAGS_csv.isEmpty()) {
250 SkFILEWStream outputStream(FLAGS_csv[0]);
251 ctx.outputCsv(outputStream);
252 }
253
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000254 return 0;
255}
zachr@google.com35f02fb2013-07-22 17:05:24 +0000256
257#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
258int main(int argc, char * argv[]) {
259 return tool_main(argc, (char**) argv);
260}
261#endif