blob: 55640f7f0ade5f7c58930fa73845b68af52e8496 [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>");
zachr@google.coma95959c2013-07-08 15:04:45 +000041DEFINE_bool(jsonp, true, "Output JSON with padding");
edisonn@google.comc93c8ac2013-07-22 15:24:26 +000042DEFINE_string(csv, "", "Writes the output of these diffs to a csv file");
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +000043DEFINE_int32(threads, -1, "run N threads in parallel [default is derived from CPUs available]");
zachr@google.comdb54dd32013-06-27 17:51:35 +000044
zachr@google.comd6585682013-07-17 19:29:19 +000045#if SK_SUPPORT_OPENCL
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000046/// A callback for any OpenCL errors
zachr@google.com35f02fb2013-07-22 17:05:24 +000047static 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 +000048 SkDebugf("OpenCL error notify: %s\n", errorInfo);
49 exit(1);
50}
51
52/// Creates a device and context with OpenCL
53static bool init_device_and_context(cl::Device* device, cl::Context* context) {
54 // Query for a platform
55 cl::vector<cl::Platform> platformList;
56 cl::Platform::get(&platformList);
57 SkDebugf("The number of platforms is %u\n", platformList.size());
58
59 // Print some information about the platform for debugging
60 cl::Platform& platform = platformList[0];
61 cl::STRING_CLASS platformName;
62 platform.getInfo(CL_PLATFORM_NAME, &platformName);
63 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
64
65 // Query for a device
66 cl::vector<cl::Device> deviceList;
zachr@google.com35f02fb2013-07-22 17:05:24 +000067 platform.getDevices(CL_DEVICE_TYPE_ALL, &deviceList);
68 SkDebugf("The number of devices is %u\n", deviceList.size());
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000069
70 // Print some information about the device for debugging
71 *device = deviceList[0];
72 cl::STRING_CLASS deviceName;
73 device->getInfo(CL_DEVICE_NAME, &deviceName);
74 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
75
76 // Create a CL context and check for all errors
77 cl_int contextErr = CL_SUCCESS;
78 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
79 if (contextErr != CL_SUCCESS) {
80 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
81 return false;
82 }
83
84 return true;
85}
86
zachr@google.comc0a75a82013-06-28 15:34:56 +000087static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000088 // Setup OpenCL
89 cl::Device device;
90 cl::Context context;
91 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000092 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000093 }
94
95 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000096 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
97 return clDiffer->init(device(), context());
98}
zachr@google.comd6585682013-07-17 19:29:19 +000099#endif
zachr@google.comc0a75a82013-06-28 15:34:56 +0000100
zachr@google.comdb54dd32013-06-27 17:51:35 +0000101// TODO Find a better home for the diff registry. One possibility is to have the differs self
102// register.
103
104// List here every differ
zachr@google.comd6585682013-07-17 19:29:19 +0000105SkDifferentPixelsMetric gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +0000106SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000107
zachr@google.comc0a75a82013-06-28 15:34:56 +0000108// A null terminated array of pointer to every differ declared above
109SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000110
zachr@google.com35f02fb2013-07-22 17:05:24 +0000111int tool_main(int argc, char * argv[]);
112int tool_main(int argc, char * argv[]) {
zachr@google.comdb54dd32013-06-27 17:51:35 +0000113 // 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.orgbe19b9e2013-06-14 17:26:54 +0000122 }
123
zachr@google.comdb54dd32013-06-27 17:51:35 +0000124 // Figure which differs the user chose, and optionally print them if the user requests it
zachr@google.com945708a2013-07-02 19:55:32 +0000125 SkTDArray<SkImageDiffer*> chosenDiffers;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000126 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000127 SkImageDiffer* differ = gDiffers[differIndex];
zachr@google.comdb54dd32013-06-27 17:51:35 +0000128 if (FLAGS_list) {
zachr@google.com945708a2013-07-02 19:55:32 +0000129 SkDebugf(" %s", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000130 SkDebugf("\n");
131 }
132
zachr@google.com945708a2013-07-02 19:55:32 +0000133 // 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 +0000134 if (FLAGS_differs.isEmpty()) {
135 // If no differs were chosen, they all get added
zachr@google.comd6585682013-07-17 19:29:19 +0000136 if (differ->requiresOpenCL()) {
137#if SK_SUPPORT_OPENCL
138 init_cl_diff(differ);
139 chosenDiffers.push(differ);
140#endif
141 } else {
142 chosenDiffers.push(differ);
143 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000144 } else {
145 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000146 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
zachr@google.comd6585682013-07-17 19:29:19 +0000147 // Initialize OpenCL for the differ if it needs it and support was compiled in.
148 if (differ->requiresOpenCL()) {
149#if SK_SUPPORT_OPENCL
150 init_cl_diff(differ);
151 chosenDiffers.push(differ);
152#endif
153 } else {
154 chosenDiffers.push(differ);
155 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000156 break;
157 }
158 }
159 }
160 }
161
162 // Don't attempt to initialize the differ if we aren't going to use it
163 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
164 return 0;
165 }
166
167 // Validate command line flags
168 if (!FLAGS_folders.isEmpty()) {
169 if (2 != FLAGS_folders.count()) {
170 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
171 return 1;
172 }
173 }
174
175 if (!FLAGS_patterns.isEmpty()) {
176 if (2 != FLAGS_patterns.count()) {
177 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
178 return 1;
179 }
180 }
181
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000182 if (!FLAGS_csv.isEmpty()) {
183 if (1 != FLAGS_csv.count()) {
184 SkDebugf("csv flag expects one argument: <csv file>\n");
185 return 1;
186 }
187 }
188
zachr@google.com945708a2013-07-02 19:55:32 +0000189 SkDiffContext ctx;
190 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000191
djsollen@google.comcbbf1ca2013-10-16 18:36:49 +0000192 if (FLAGS_threads >= 0) {
193 ctx.setThreadCount(FLAGS_threads);
194 }
195
zachr@google.com945708a2013-07-02 19:55:32 +0000196 // Perform a folder diff if one is requested
197 if (!FLAGS_folders.isEmpty()) {
198 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
199 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000200
zachr@google.com945708a2013-07-02 19:55:32 +0000201 // Perform a pattern diff if one is requested
202 if (!FLAGS_patterns.isEmpty()) {
203 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
204 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000205
zachr@google.com945708a2013-07-02 19:55:32 +0000206 // Output to the file specified
207 if (!FLAGS_output.isEmpty()) {
208 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000209 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000210 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000211
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000212 if (!FLAGS_csv.isEmpty()) {
213 SkFILEWStream outputStream(FLAGS_csv[0]);
214 ctx.outputCsv(outputStream);
215 }
216
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000217 return 0;
218}
zachr@google.com35f02fb2013-07-22 17:05:24 +0000219
220#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
221int main(int argc, char * argv[]) {
222 return tool_main(argc, (char**) argv);
223}
224#endif