blob: 9664f33de5595cdec65ce49558e3f2b9c51a3b45 [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>");
zachr@google.com945708a2013-07-02 19:55:32 +000040DEFINE_string2(output, o, "skpdiff_output.json", "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");
zachr@google.comdb54dd32013-06-27 17:51:35 +000043
zachr@google.comd6585682013-07-17 19:29:19 +000044#if SK_SUPPORT_OPENCL
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000045/// A callback for any OpenCL errors
zachr@google.com35f02fb2013-07-22 17:05:24 +000046static 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 +000047 SkDebugf("OpenCL error notify: %s\n", errorInfo);
48 exit(1);
49}
50
51/// Creates a device and context with OpenCL
52static bool init_device_and_context(cl::Device* device, cl::Context* context) {
53 // Query for a platform
54 cl::vector<cl::Platform> platformList;
55 cl::Platform::get(&platformList);
56 SkDebugf("The number of platforms is %u\n", platformList.size());
57
58 // Print some information about the platform for debugging
59 cl::Platform& platform = platformList[0];
60 cl::STRING_CLASS platformName;
61 platform.getInfo(CL_PLATFORM_NAME, &platformName);
62 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
63
64 // Query for a device
65 cl::vector<cl::Device> deviceList;
zachr@google.com35f02fb2013-07-22 17:05:24 +000066 platform.getDevices(CL_DEVICE_TYPE_ALL, &deviceList);
67 SkDebugf("The number of devices is %u\n", deviceList.size());
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000068
69 // Print some information about the device for debugging
70 *device = deviceList[0];
71 cl::STRING_CLASS deviceName;
72 device->getInfo(CL_DEVICE_NAME, &deviceName);
73 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
74
75 // Create a CL context and check for all errors
76 cl_int contextErr = CL_SUCCESS;
77 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
78 if (contextErr != CL_SUCCESS) {
79 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
80 return false;
81 }
82
83 return true;
84}
85
zachr@google.comc0a75a82013-06-28 15:34:56 +000086static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000087 // Setup OpenCL
88 cl::Device device;
89 cl::Context context;
90 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000091 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000092 }
93
94 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000095 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
96 return clDiffer->init(device(), context());
97}
zachr@google.comd6585682013-07-17 19:29:19 +000098#endif
zachr@google.comc0a75a82013-06-28 15:34:56 +000099
zachr@google.comdb54dd32013-06-27 17:51:35 +0000100// TODO Find a better home for the diff registry. One possibility is to have the differs self
101// register.
102
103// List here every differ
zachr@google.comd6585682013-07-17 19:29:19 +0000104SkDifferentPixelsMetric gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +0000105SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000106
zachr@google.comc0a75a82013-06-28 15:34:56 +0000107// A null terminated array of pointer to every differ declared above
108SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000109
zachr@google.com35f02fb2013-07-22 17:05:24 +0000110int tool_main(int argc, char * argv[]);
111int tool_main(int argc, char * argv[]) {
zachr@google.comdb54dd32013-06-27 17:51:35 +0000112 // Setup command line parsing
113 SkCommandLineFlags::SetUsage("Compare images using various metrics.");
114 SkCommandLineFlags::Parse(argc, argv);
115
116 // Needed by various Skia components
117 SkAutoGraphics ag;
118
119 if (FLAGS_list) {
120 SkDebugf("Available Metrics:\n");
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000121 }
122
zachr@google.comdb54dd32013-06-27 17:51:35 +0000123 // Figure which differs the user chose, and optionally print them if the user requests it
zachr@google.com945708a2013-07-02 19:55:32 +0000124 SkTDArray<SkImageDiffer*> chosenDiffers;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000125 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000126 SkImageDiffer* differ = gDiffers[differIndex];
zachr@google.comdb54dd32013-06-27 17:51:35 +0000127 if (FLAGS_list) {
zachr@google.com945708a2013-07-02 19:55:32 +0000128 SkDebugf(" %s", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000129 SkDebugf("\n");
130 }
131
zachr@google.com945708a2013-07-02 19:55:32 +0000132 // 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 +0000133 if (FLAGS_differs.isEmpty()) {
134 // If no differs were chosen, they all get added
zachr@google.comd6585682013-07-17 19:29:19 +0000135 if (differ->requiresOpenCL()) {
136#if SK_SUPPORT_OPENCL
137 init_cl_diff(differ);
138 chosenDiffers.push(differ);
139#endif
140 } else {
141 chosenDiffers.push(differ);
142 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000143 } else {
144 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000145 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
zachr@google.comd6585682013-07-17 19:29:19 +0000146 // Initialize OpenCL for the differ if it needs it and support was compiled in.
147 if (differ->requiresOpenCL()) {
148#if SK_SUPPORT_OPENCL
149 init_cl_diff(differ);
150 chosenDiffers.push(differ);
151#endif
152 } else {
153 chosenDiffers.push(differ);
154 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000155 break;
156 }
157 }
158 }
159 }
160
161 // Don't attempt to initialize the differ if we aren't going to use it
162 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
163 return 0;
164 }
165
166 // Validate command line flags
167 if (!FLAGS_folders.isEmpty()) {
168 if (2 != FLAGS_folders.count()) {
169 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
170 return 1;
171 }
172 }
173
174 if (!FLAGS_patterns.isEmpty()) {
175 if (2 != FLAGS_patterns.count()) {
176 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
177 return 1;
178 }
179 }
180
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000181 if (!FLAGS_csv.isEmpty()) {
182 if (1 != FLAGS_csv.count()) {
183 SkDebugf("csv flag expects one argument: <csv file>\n");
184 return 1;
185 }
186 }
187
zachr@google.com945708a2013-07-02 19:55:32 +0000188 SkDiffContext ctx;
189 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000190
zachr@google.com945708a2013-07-02 19:55:32 +0000191 // Perform a folder diff if one is requested
192 if (!FLAGS_folders.isEmpty()) {
193 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
194 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000195
zachr@google.com945708a2013-07-02 19:55:32 +0000196 // Perform a pattern diff if one is requested
197 if (!FLAGS_patterns.isEmpty()) {
198 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
199 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000200
zachr@google.com945708a2013-07-02 19:55:32 +0000201 // Output to the file specified
202 if (!FLAGS_output.isEmpty()) {
203 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000204 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000205 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000206
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000207 if (!FLAGS_csv.isEmpty()) {
208 SkFILEWStream outputStream(FLAGS_csv[0]);
209 ctx.outputCsv(outputStream);
210 }
211
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000212 return 0;
213}
zachr@google.com35f02fb2013-07-22 17:05:24 +0000214
215#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
216int main(int argc, char * argv[]) {
217 return tool_main(argc, (char**) argv);
218}
219#endif