blob: b9d533df84be954b121c620fc8539bb3d1ac48e2 [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
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.comdb54dd32013-06-27 17:51:35 +000012#include "SkCommandLineFlags.h"
13#include "SkGraphics.h"
zachr@google.com572b54d2013-06-28 16:27:33 +000014#include "SkPoint.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000015#include "SkOSFile.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000016#include "SkString.h"
17#include "SkTArray.h"
18#include "SkTDArray.h"
19
20#include "SkImageDiffer.h"
21#include "SkCLImageDiffer.h"
zachr@google.comc0a75a82013-06-28 15:34:56 +000022#include "SkPMetric.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000023#include "skpdiff_util.h"
24
zachr@google.comdb54dd32013-06-27 17:51:35 +000025#include "SkForceLinking.h"
26__SK_FORCE_IMAGE_DECODER_LINKING;
27
28// Command line argument definitions go here
29DEFINE_bool2(list, l, false, "List out available differs");
30DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default");
31DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>");
32DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>");
33
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000034/// A callback for any OpenCL errors
35CL_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
41static 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
75/// Compares two directories of images with the given differ
76static void diff_directories(const char baselinePath[], const char testPath[], SkImageDiffer* differ) {
77 // Get the files in the baseline, we will then look for those inside the test path
78 SkTArray<SkString> baselineEntries;
79 if (!get_directory(baselinePath, &baselineEntries)) {
80 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
81 return;
82 }
83
84 SkTDArray<int> queuedDiffIDs;
85 for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselineIndex++) {
86 const char* baseFilename = baselineEntries[baselineIndex].c_str();
zachr@google.com572b54d2013-06-28 16:27:33 +000087 SkDebugf("\n%s\n", baseFilename);
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000088
89 // Find the real location of each file to compare
90 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
91 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
92
93 // Check that the test file exists and is a file
94 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
95 // Queue up the comparison with the differ
96 int diffID = differ->queueDiffOfFile(baselineFile.c_str(), testFile.c_str());
97 if (diffID >= 0) {
98 queuedDiffIDs.push(diffID);
99 SkDebugf("Result: %f\n", differ->getResult(diffID));
zachr@google.com572b54d2013-06-28 16:27:33 +0000100 SkDebugf("POI Count: %i\n", differ->getPointsOfInterestCount(diffID));
101 differ->deleteDiff(diffID);
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000102 }
103 } else {
104 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
105 }
106 }
107}
108
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000109
zachr@google.comdb54dd32013-06-27 17:51:35 +0000110/// Compares two sets of images identified by glob style patterns with the given differ
111static void diff_patterns(const char baselinePattern[], const char testPattern[], SkImageDiffer* differ) {
112 // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy
113 // to find corresponding images by matching entry indices.
zachr@google.comc0a75a82013-06-28 15:34:56 +0000114
zachr@google.comdb54dd32013-06-27 17:51:35 +0000115 SkTArray<SkString> baselineEntries;
116 if (!glob_files(baselinePattern, &baselineEntries)) {
117 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
118 return;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000119 }
120
zachr@google.comdb54dd32013-06-27 17:51:35 +0000121 SkTArray<SkString> testEntries;
122 if (!glob_files(testPattern, &testEntries)) {
123 SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
124 return;
125 }
126
127 if (baselineEntries.count() != testEntries.count()) {
128 SkDebugf("Baseline and test patterns do not yield corresponding number of files\n");
129 return;
130 }
131
132 SkTDArray<int> queuedDiffIDs;
133 for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) {
134 const char* baselineFilename = baselineEntries[entryIndex].c_str();
135 const char* testFilename = testEntries [entryIndex].c_str();
zachr@google.com572b54d2013-06-28 16:27:33 +0000136 SkDebugf("\n%s %s\n", baselineFilename, testFilename);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000137
138 int diffID = differ->queueDiffOfFile(baselineFilename, testFilename);
139 if (diffID >= 0) {
140 queuedDiffIDs.push(diffID);
141 SkDebugf("Result: %f\n", differ->getResult(diffID));
zachr@google.com572b54d2013-06-28 16:27:33 +0000142 SkDebugf("POI Count: %i\n", differ->getPointsOfInterestCount(diffID));
143 differ->deleteDiff(diffID);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000144 }
145 }
146}
147
148
zachr@google.comc0a75a82013-06-28 15:34:56 +0000149static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000150 // Setup OpenCL
151 cl::Device device;
152 cl::Context context;
153 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +0000154 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000155 }
156
157 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +0000158 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
159 return clDiffer->init(device(), context());
160}
161
zachr@google.comc0a75a82013-06-28 15:34:56 +0000162static bool init_dummy(SkImageDiffer* differ) {
163 return true;
164}
165
166
zachr@google.comdb54dd32013-06-27 17:51:35 +0000167// TODO Find a better home for the diff registry. One possibility is to have the differs self
168// register.
169
170// List here every differ
171SkDifferentPixelsImageDiffer gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +0000172SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000173
zachr@google.comc0a75a82013-06-28 15:34:56 +0000174// A null terminated array of pointer to every differ declared above
175SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000176
zachr@google.comc0a75a82013-06-28 15:34:56 +0000177// A parallel array of functions to initialize the above differs. The reason we don't initialize
178// everything immediately is that certain differs may require special initialization, but we still
179// want to construct all of them globally so they can be queried for things like their name and
180// description.
181bool (*gDiffInits[])(SkImageDiffer*) = { init_cl_diff, init_dummy, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000182
183
184int main(int argc, char** argv) {
185 // Setup command line parsing
186 SkCommandLineFlags::SetUsage("Compare images using various metrics.");
187 SkCommandLineFlags::Parse(argc, argv);
188
189 // Needed by various Skia components
190 SkAutoGraphics ag;
191
192 if (FLAGS_list) {
193 SkDebugf("Available Metrics:\n");
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000194 }
195
zachr@google.comdb54dd32013-06-27 17:51:35 +0000196 // Figure which differs the user chose, and optionally print them if the user requests it
197 SkTDArray<int> chosenDiffers;
198 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
199 if (FLAGS_list) {
200 SkDebugf(" %s", gDiffers[differIndex]->getName());
201 SkDebugf("\n");
202 }
203
204 // Check if this differ was chosen by any of the flags
205 if (FLAGS_differs.isEmpty()) {
206 // If no differs were chosen, they all get added
207 chosenDiffers.push(differIndex);
208 } else {
209 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
210 if (SkString(FLAGS_differs[flagIndex]).equals(gDiffers[differIndex]->getName())) {
211 chosenDiffers.push(differIndex);
212 break;
213 }
214 }
215 }
216 }
217
218 // Don't attempt to initialize the differ if we aren't going to use it
219 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
220 return 0;
221 }
222
223 // Validate command line flags
224 if (!FLAGS_folders.isEmpty()) {
225 if (2 != FLAGS_folders.count()) {
226 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
227 return 1;
228 }
229 }
230
231 if (!FLAGS_patterns.isEmpty()) {
232 if (2 != FLAGS_patterns.count()) {
233 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
234 return 1;
235 }
236 }
237
238 // TODO Move the differ loop to after the bitmaps are decoded and/or uploaded to the OpenCL
239 // device. Those are often the slowest processes and should not be done more than once if it can
240 // be helped.
241
242 // Perform each requested diff
zachr@google.comc0a75a82013-06-28 15:34:56 +0000243 for (int chosenDifferIndex = 0; chosenDifferIndex < chosenDiffers.count(); chosenDifferIndex++) {
244 int differIndex = chosenDiffers[chosenDifferIndex];
245
zachr@google.comdb54dd32013-06-27 17:51:35 +0000246 // Get the chosen differ and say which one they chose
247 SkImageDiffer * differ = gDiffers[differIndex];
zachr@google.comc0a75a82013-06-28 15:34:56 +0000248 SkDebugf("Using metric \"%s\"\n", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000249
250 // Initialize the differ using the global list of init functions that match the list of
251 // differs
252 gDiffInits[differIndex](differ);
253
254 // Perform a folder diff if one is requested
255 if (!FLAGS_folders.isEmpty()) {
256 diff_directories(FLAGS_folders[0], FLAGS_folders[1], differ);
257 }
258
259 // Perform a pattern diff if one is requested
260 if (!FLAGS_patterns.isEmpty()) {
261 diff_patterns(FLAGS_patterns[0], FLAGS_patterns[1], differ);
262 }
263 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000264
265 return 0;
266}