blob: d258c147c62015e1f41968e41eb449dc71f6cce2 [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");
zachr@google.comdb54dd32013-06-27 17:51:35 +000035
zachr@google.comd6585682013-07-17 19:29:19 +000036#if SK_SUPPORT_OPENCL
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000037/// A callback for any OpenCL errors
zachr@google.com904f86e2013-07-22 13:29:20 +000038CL_CALLBACK void error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000039 SkDebugf("OpenCL error notify: %s\n", errorInfo);
40 exit(1);
41}
42
43/// Creates a device and context with OpenCL
44static bool init_device_and_context(cl::Device* device, cl::Context* context) {
45 // Query for a platform
46 cl::vector<cl::Platform> platformList;
47 cl::Platform::get(&platformList);
48 SkDebugf("The number of platforms is %u\n", platformList.size());
49
50 // Print some information about the platform for debugging
51 cl::Platform& platform = platformList[0];
52 cl::STRING_CLASS platformName;
53 platform.getInfo(CL_PLATFORM_NAME, &platformName);
54 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
55
56 // Query for a device
57 cl::vector<cl::Device> deviceList;
zachr@google.com904f86e2013-07-22 13:29:20 +000058 platform.getDevices(CL_DEVICE_TYPE_GPU, &deviceList);
59 SkDebugf("The number of GPU devices is %u\n", deviceList.size());
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000060
61 // Print some information about the device for debugging
62 *device = deviceList[0];
63 cl::STRING_CLASS deviceName;
64 device->getInfo(CL_DEVICE_NAME, &deviceName);
65 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
66
67 // Create a CL context and check for all errors
68 cl_int contextErr = CL_SUCCESS;
69 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
70 if (contextErr != CL_SUCCESS) {
71 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
72 return false;
73 }
74
75 return true;
76}
77
zachr@google.comc0a75a82013-06-28 15:34:56 +000078static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000079 // Setup OpenCL
80 cl::Device device;
81 cl::Context context;
82 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000083 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000084 }
85
86 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000087 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
88 return clDiffer->init(device(), context());
89}
zachr@google.comd6585682013-07-17 19:29:19 +000090#endif
zachr@google.comc0a75a82013-06-28 15:34:56 +000091
zachr@google.comdb54dd32013-06-27 17:51:35 +000092// TODO Find a better home for the diff registry. One possibility is to have the differs self
93// register.
94
95// List here every differ
zachr@google.comd6585682013-07-17 19:29:19 +000096SkDifferentPixelsMetric gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +000097SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +000098
zachr@google.comc0a75a82013-06-28 15:34:56 +000099// A null terminated array of pointer to every differ declared above
100SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000101
zachr@google.comdb54dd32013-06-27 17:51:35 +0000102int main(int argc, char** argv) {
103 // Setup command line parsing
104 SkCommandLineFlags::SetUsage("Compare images using various metrics.");
105 SkCommandLineFlags::Parse(argc, argv);
106
107 // Needed by various Skia components
108 SkAutoGraphics ag;
109
110 if (FLAGS_list) {
111 SkDebugf("Available Metrics:\n");
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000112 }
113
zachr@google.comdb54dd32013-06-27 17:51:35 +0000114 // Figure which differs the user chose, and optionally print them if the user requests it
zachr@google.com945708a2013-07-02 19:55:32 +0000115 SkTDArray<SkImageDiffer*> chosenDiffers;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000116 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000117 SkImageDiffer* differ = gDiffers[differIndex];
zachr@google.comdb54dd32013-06-27 17:51:35 +0000118 if (FLAGS_list) {
zachr@google.com945708a2013-07-02 19:55:32 +0000119 SkDebugf(" %s", differ->getName());
zachr@google.comdb54dd32013-06-27 17:51:35 +0000120 SkDebugf("\n");
121 }
122
zachr@google.com945708a2013-07-02 19:55:32 +0000123 // 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 +0000124 if (FLAGS_differs.isEmpty()) {
125 // If no differs were chosen, they all get added
zachr@google.comd6585682013-07-17 19:29:19 +0000126 if (differ->requiresOpenCL()) {
127#if SK_SUPPORT_OPENCL
128 init_cl_diff(differ);
129 chosenDiffers.push(differ);
130#endif
131 } else {
132 chosenDiffers.push(differ);
133 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000134 } else {
135 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000136 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
zachr@google.comd6585682013-07-17 19:29:19 +0000137 // Initialize OpenCL for the differ if it needs it and support was compiled in.
138 if (differ->requiresOpenCL()) {
139#if SK_SUPPORT_OPENCL
140 init_cl_diff(differ);
141 chosenDiffers.push(differ);
142#endif
143 } else {
144 chosenDiffers.push(differ);
145 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000146 break;
147 }
148 }
149 }
150 }
151
152 // Don't attempt to initialize the differ if we aren't going to use it
153 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
154 return 0;
155 }
156
157 // Validate command line flags
158 if (!FLAGS_folders.isEmpty()) {
159 if (2 != FLAGS_folders.count()) {
160 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
161 return 1;
162 }
163 }
164
165 if (!FLAGS_patterns.isEmpty()) {
166 if (2 != FLAGS_patterns.count()) {
167 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
168 return 1;
169 }
170 }
171
zachr@google.com945708a2013-07-02 19:55:32 +0000172 SkDiffContext ctx;
173 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000174
zachr@google.com945708a2013-07-02 19:55:32 +0000175 // Perform a folder diff if one is requested
176 if (!FLAGS_folders.isEmpty()) {
177 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
178 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000179
zachr@google.com945708a2013-07-02 19:55:32 +0000180 // Perform a pattern diff if one is requested
181 if (!FLAGS_patterns.isEmpty()) {
182 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
183 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000184
zachr@google.com945708a2013-07-02 19:55:32 +0000185 // Output to the file specified
186 if (!FLAGS_output.isEmpty()) {
187 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000188 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000189 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000190
191 return 0;
192}