blob: b1bf9173c7c5fad03e6d30aa23f12f17d3a34afe [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
zachr@google.comd6585682013-07-17 19:29:19 +00008#if SK_SUPPORT_OPENCL
zachr@google.com35f02fb2013-07-22 17:05:24 +00009
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000010#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.com35f02fb2013-07-22 17:05:24 +000012#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.comd6585682013-07-17 19:29:19 +000019#endif
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000020
zachr@google.comdb54dd32013-06-27 17:51:35 +000021#include "SkCommandLineFlags.h"
22#include "SkGraphics.h"
zachr@google.com945708a2013-07-02 19:55:32 +000023#include "SkStream.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000024#include "SkTDArray.h"
25
zachr@google.comd6585682013-07-17 19:29:19 +000026#include "SkDifferentPixelsMetric.h"
zachr@google.com945708a2013-07-02 19:55:32 +000027#include "SkDiffContext.h"
28#include "SkImageDiffer.h"
zachr@google.comc0a75a82013-06-28 15:34:56 +000029#include "SkPMetric.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000030#include "skpdiff_util.h"
31
zachr@google.comdb54dd32013-06-27 17:51:35 +000032#include "SkForceLinking.h"
33__SK_FORCE_IMAGE_DECODER_LINKING;
34
35// Command line argument definitions go here
36DEFINE_bool2(list, l, false, "List out available differs");
37DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default");
38DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>");
39DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>");
edisonn@google.comd2d9f562013-07-23 20:15:08 +000040DEFINE_string2(output, o, "", "Writes the output of these diffs to output: <output>");
djsollen@google.com513a7bf2013-11-07 19:24:06 +000041DEFINE_string(alphaDir, "", "Writes the alpha mask of these diffs to output: <output>");
zachr@google.coma95959c2013-07-08 15:04:45 +000042DEFINE_bool(jsonp, true, "Output JSON with padding");
edisonn@google.comc93c8ac2013-07-22 15:24:26 +000043DEFINE_string(csv, "", "Writes the output of these diffs to a csv file");
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000044DEFINE_int32(threads, -1, "run N threads in parallel [default is derived from CPUs available]");
zachr@google.comdb54dd32013-06-27 17:51:35 +000045
zachr@google.comd6585682013-07-17 19:29:19 +000046#if SK_SUPPORT_OPENCL
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000047/// A callback for any OpenCL errors
zachr@google.com35f02fb2013-07-22 17:05:24 +000048static 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 +000049 SkDebugf("OpenCL error notify: %s\n", errorInfo);
50 exit(1);
51}
52
53/// Creates a device and context with OpenCL
54static 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.com35f02fb2013-07-22 17:05:24 +000068 platform.getDevices(CL_DEVICE_TYPE_ALL, &deviceList);
69 SkDebugf("The number of devices is %u\n", deviceList.size());
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000070
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.comc0a75a82013-06-28 15:34:56 +000088static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000089 // Setup OpenCL
90 cl::Device device;
91 cl::Context context;
92 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000093 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000094 }
95
96 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000097 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
98 return clDiffer->init(device(), context());
99}
zachr@google.comd6585682013-07-17 19:29:19 +0000100#endif
zachr@google.comc0a75a82013-06-28 15:34:56 +0000101
zachr@google.comdb54dd32013-06-27 17:51:35 +0000102// 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.comd6585682013-07-17 19:29:19 +0000106SkDifferentPixelsMetric gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +0000107SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000108
zachr@google.comc0a75a82013-06-28 15:34:56 +0000109// A null terminated array of pointer to every differ declared above
110SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000111
zachr@google.com35f02fb2013-07-22 17:05:24 +0000112int tool_main(int argc, char * argv[]);
113int tool_main(int argc, char * argv[]) {
zachr@google.comdb54dd32013-06-27 17:51:35 +0000114 // 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.orgbe19b9e2013-06-14 17:26:54 +0000123 }
124
zachr@google.comdb54dd32013-06-27 17:51:35 +0000125 // Figure which differs the user chose, and optionally print them if the user requests it
zachr@google.com945708a2013-07-02 19:55:32 +0000126 SkTDArray<SkImageDiffer*> chosenDiffers;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000127 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000128 SkImageDiffer* differ = gDiffers[differIndex];
zachr@google.comdb54dd32013-06-27 17:51:35 +0000129 if (FLAGS_list) {
zachr@google.com945708a2013-07-02 19:55:32 +0000130 SkDebugf(" %s", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000131 SkDebugf("\n");
132 }
133
zachr@google.com945708a2013-07-02 19:55:32 +0000134 // 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 +0000135 if (FLAGS_differs.isEmpty()) {
136 // If no differs were chosen, they all get added
zachr@google.comd6585682013-07-17 19:29:19 +0000137 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.comdb54dd32013-06-27 17:51:35 +0000145 } else {
146 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000147 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
zachr@google.comd6585682013-07-17 19:29:19 +0000148 // 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.comdb54dd32013-06-27 17:51:35 +0000157 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.comc93c8ac2013-07-22 15:24:26 +0000183 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.com513a7bf2013-11-07 19:24:06 +0000190 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.com945708a2013-07-02 19:55:32 +0000197 SkDiffContext ctx;
198 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000199
djsollen@google.com513a7bf2013-11-07 19:24:06 +0000200 if (!FLAGS_alphaDir.isEmpty()) {
201 ctx.setDifferenceDir(SkString(FLAGS_alphaDir[0]));
202 }
203
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000204 if (FLAGS_threads >= 0) {
205 ctx.setThreadCount(FLAGS_threads);
206 }
207
zachr@google.com945708a2013-07-02 19:55:32 +0000208 // 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.comc0a75a82013-06-28 15:34:56 +0000212
zachr@google.com945708a2013-07-02 19:55:32 +0000213 // 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.comdb54dd32013-06-27 17:51:35 +0000217
zachr@google.com945708a2013-07-02 19:55:32 +0000218 // Output to the file specified
219 if (!FLAGS_output.isEmpty()) {
220 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000221 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000222 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000223
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000224 if (!FLAGS_csv.isEmpty()) {
225 SkFILEWStream outputStream(FLAGS_csv[0]);
226 ctx.outputCsv(outputStream);
227 }
228
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000229 return 0;
230}
zachr@google.com35f02fb2013-07-22 17:05:24 +0000231
232#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
233int main(int argc, char * argv[]) {
234 return tool_main(argc, (char**) argv);
235}
236#endif