blob: 7ed0e4d90d2aa961934650fbe45ecef8e032ade0 [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
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +00009#define __NO_STD_VECTOR // Uses cl::vectpr instead of std::vectpr
10#define __NO_STD_STRING // Uses cl::STRING_CLASS instead of std::string
zachr@google.com904f86e2013-07-22 13:29:20 +000011#include <CL/cl.hpp>
zachr@google.comd6585682013-07-17 19:29:19 +000012#endif
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000013
zachr@google.comdb54dd32013-06-27 17:51:35 +000014#include "SkCommandLineFlags.h"
15#include "SkGraphics.h"
zachr@google.com945708a2013-07-02 19:55:32 +000016#include "SkStream.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000017#include "SkTDArray.h"
18
zachr@google.comd6585682013-07-17 19:29:19 +000019#include "SkDifferentPixelsMetric.h"
zachr@google.com945708a2013-07-02 19:55:32 +000020#include "SkDiffContext.h"
21#include "SkImageDiffer.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>");
zachr@google.com945708a2013-07-02 19:55:32 +000033DEFINE_string2(output, o, "skpdiff_output.json", "Writes the output of these diffs to output: <output>");
zachr@google.coma95959c2013-07-08 15:04:45 +000034DEFINE_bool(jsonp, true, "Output JSON with padding");
edisonn@google.comc93c8ac2013-07-22 15:24:26 +000035DEFINE_string(csv, "", "Writes the output of these diffs to a csv file");
zachr@google.comdb54dd32013-06-27 17:51:35 +000036
zachr@google.comd6585682013-07-17 19:29:19 +000037#if SK_SUPPORT_OPENCL
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000038/// A callback for any OpenCL errors
zachr@google.com904f86e2013-07-22 13:29:20 +000039CL_CALLBACK void error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000040 SkDebugf("OpenCL error notify: %s\n", errorInfo);
41 exit(1);
42}
43
44/// Creates a device and context with OpenCL
45static bool init_device_and_context(cl::Device* device, cl::Context* context) {
46 // Query for a platform
47 cl::vector<cl::Platform> platformList;
48 cl::Platform::get(&platformList);
49 SkDebugf("The number of platforms is %u\n", platformList.size());
50
51 // Print some information about the platform for debugging
52 cl::Platform& platform = platformList[0];
53 cl::STRING_CLASS platformName;
54 platform.getInfo(CL_PLATFORM_NAME, &platformName);
55 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
56
57 // Query for a device
58 cl::vector<cl::Device> deviceList;
zachr@google.com904f86e2013-07-22 13:29:20 +000059 platform.getDevices(CL_DEVICE_TYPE_GPU, &deviceList);
60 SkDebugf("The number of GPU devices is %u\n", deviceList.size());
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000061
62 // Print some information about the device for debugging
63 *device = deviceList[0];
64 cl::STRING_CLASS deviceName;
65 device->getInfo(CL_DEVICE_NAME, &deviceName);
66 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
67
68 // Create a CL context and check for all errors
69 cl_int contextErr = CL_SUCCESS;
70 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
71 if (contextErr != CL_SUCCESS) {
72 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
73 return false;
74 }
75
76 return true;
77}
78
zachr@google.comc0a75a82013-06-28 15:34:56 +000079static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000080 // Setup OpenCL
81 cl::Device device;
82 cl::Context context;
83 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000084 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000085 }
86
87 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000088 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
89 return clDiffer->init(device(), context());
90}
zachr@google.comd6585682013-07-17 19:29:19 +000091#endif
zachr@google.comc0a75a82013-06-28 15:34:56 +000092
zachr@google.comdb54dd32013-06-27 17:51:35 +000093// TODO Find a better home for the diff registry. One possibility is to have the differs self
94// register.
95
96// List here every differ
zachr@google.comd6585682013-07-17 19:29:19 +000097SkDifferentPixelsMetric gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +000098SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +000099
zachr@google.comc0a75a82013-06-28 15:34:56 +0000100// A null terminated array of pointer to every differ declared above
101SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000102
zachr@google.comdb54dd32013-06-27 17:51:35 +0000103int main(int argc, char** argv) {
104 // Setup command line parsing
105 SkCommandLineFlags::SetUsage("Compare images using various metrics.");
106 SkCommandLineFlags::Parse(argc, argv);
107
108 // Needed by various Skia components
109 SkAutoGraphics ag;
110
111 if (FLAGS_list) {
112 SkDebugf("Available Metrics:\n");
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000113 }
114
zachr@google.comdb54dd32013-06-27 17:51:35 +0000115 // Figure which differs the user chose, and optionally print them if the user requests it
zachr@google.com945708a2013-07-02 19:55:32 +0000116 SkTDArray<SkImageDiffer*> chosenDiffers;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000117 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000118 SkImageDiffer* differ = gDiffers[differIndex];
zachr@google.comdb54dd32013-06-27 17:51:35 +0000119 if (FLAGS_list) {
zachr@google.com945708a2013-07-02 19:55:32 +0000120 SkDebugf(" %s", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000121 SkDebugf("\n");
122 }
123
zachr@google.com945708a2013-07-02 19:55:32 +0000124 // 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 +0000125 if (FLAGS_differs.isEmpty()) {
126 // If no differs were chosen, they all get added
zachr@google.comd6585682013-07-17 19:29:19 +0000127 if (differ->requiresOpenCL()) {
128#if SK_SUPPORT_OPENCL
129 init_cl_diff(differ);
130 chosenDiffers.push(differ);
131#endif
132 } else {
133 chosenDiffers.push(differ);
134 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000135 } else {
136 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000137 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
zachr@google.comd6585682013-07-17 19:29:19 +0000138 // Initialize OpenCL for the differ if it needs it and support was compiled in.
139 if (differ->requiresOpenCL()) {
140#if SK_SUPPORT_OPENCL
141 init_cl_diff(differ);
142 chosenDiffers.push(differ);
143#endif
144 } else {
145 chosenDiffers.push(differ);
146 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000147 break;
148 }
149 }
150 }
151 }
152
153 // Don't attempt to initialize the differ if we aren't going to use it
154 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
155 return 0;
156 }
157
158 // Validate command line flags
159 if (!FLAGS_folders.isEmpty()) {
160 if (2 != FLAGS_folders.count()) {
161 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
162 return 1;
163 }
164 }
165
166 if (!FLAGS_patterns.isEmpty()) {
167 if (2 != FLAGS_patterns.count()) {
168 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
169 return 1;
170 }
171 }
172
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000173 if (!FLAGS_csv.isEmpty()) {
174 if (1 != FLAGS_csv.count()) {
175 SkDebugf("csv flag expects one argument: <csv file>\n");
176 return 1;
177 }
178 }
179
zachr@google.com945708a2013-07-02 19:55:32 +0000180 SkDiffContext ctx;
181 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000182
zachr@google.com945708a2013-07-02 19:55:32 +0000183 // Perform a folder diff if one is requested
184 if (!FLAGS_folders.isEmpty()) {
185 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
186 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000187
zachr@google.com945708a2013-07-02 19:55:32 +0000188 // Perform a pattern diff if one is requested
189 if (!FLAGS_patterns.isEmpty()) {
190 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
191 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000192
zachr@google.com945708a2013-07-02 19:55:32 +0000193 // Output to the file specified
194 if (!FLAGS_output.isEmpty()) {
195 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000196 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000197 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000198
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000199 if (!FLAGS_csv.isEmpty()) {
200 SkFILEWStream outputStream(FLAGS_csv[0]);
201 ctx.outputCsv(outputStream);
202 }
203
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000204 return 0;
205}