blob: 08435519b67c5f3aa4fb6161ceaaeed939b3c60b [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.com945708a2013-07-02 19:55:32 +000014#include "SkStream.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000015#include "SkTDArray.h"
16
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000017#include "SkCLImageDiffer.h"
zachr@google.com945708a2013-07-02 19:55:32 +000018#include "SkDiffContext.h"
19#include "SkImageDiffer.h"
zachr@google.comc0a75a82013-06-28 15:34:56 +000020#include "SkPMetric.h"
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000021#include "skpdiff_util.h"
22
zachr@google.comdb54dd32013-06-27 17:51:35 +000023#include "SkForceLinking.h"
24__SK_FORCE_IMAGE_DECODER_LINKING;
25
26// Command line argument definitions go here
27DEFINE_bool2(list, l, false, "List out available differs");
28DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default");
29DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>");
30DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>");
zachr@google.com945708a2013-07-02 19:55:32 +000031DEFINE_string2(output, o, "skpdiff_output.json", "Writes the output of these diffs to output: <output>");
zachr@google.coma95959c2013-07-08 15:04:45 +000032DEFINE_bool(jsonp, true, "Output JSON with padding");
zachr@google.comdb54dd32013-06-27 17:51:35 +000033
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
zachr@google.comdb54dd32013-06-27 17:51:35 +000075
76
zachr@google.comc0a75a82013-06-28 15:34:56 +000077static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000078 // Setup OpenCL
79 cl::Device device;
80 cl::Context context;
81 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000082 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000083 }
84
85 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000086 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
87 return clDiffer->init(device(), context());
88}
89
zachr@google.comc0a75a82013-06-28 15:34:56 +000090static bool init_dummy(SkImageDiffer* differ) {
91 return true;
92}
93
94
zachr@google.comdb54dd32013-06-27 17:51:35 +000095// TODO Find a better home for the diff registry. One possibility is to have the differs self
96// register.
97
98// List here every differ
99SkDifferentPixelsImageDiffer gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +0000100SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000101
zachr@google.comc0a75a82013-06-28 15:34:56 +0000102// A null terminated array of pointer to every differ declared above
103SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000104
zachr@google.comc0a75a82013-06-28 15:34:56 +0000105// A parallel array of functions to initialize the above differs. The reason we don't initialize
106// everything immediately is that certain differs may require special initialization, but we still
107// want to construct all of them globally so they can be queried for things like their name and
108// description.
109bool (*gDiffInits[])(SkImageDiffer*) = { init_cl_diff, init_dummy, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000110
111
112int main(int argc, char** argv) {
113 // 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.com945708a2013-07-02 19:55:32 +0000136 chosenDiffers.push(differ);
137 gDiffInits[differIndex](differ);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000138 } else {
139 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000140 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
141 chosenDiffers.push(differ);
142 gDiffInits[differIndex](differ);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000143 break;
144 }
145 }
146 }
147 }
148
149 // Don't attempt to initialize the differ if we aren't going to use it
150 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
151 return 0;
152 }
153
154 // Validate command line flags
155 if (!FLAGS_folders.isEmpty()) {
156 if (2 != FLAGS_folders.count()) {
157 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
158 return 1;
159 }
160 }
161
162 if (!FLAGS_patterns.isEmpty()) {
163 if (2 != FLAGS_patterns.count()) {
164 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
165 return 1;
166 }
167 }
168
zachr@google.com945708a2013-07-02 19:55:32 +0000169 SkDiffContext ctx;
170 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000171
zachr@google.com945708a2013-07-02 19:55:32 +0000172 // Perform a folder diff if one is requested
173 if (!FLAGS_folders.isEmpty()) {
174 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
175 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000176
zachr@google.com945708a2013-07-02 19:55:32 +0000177 // Perform a pattern diff if one is requested
178 if (!FLAGS_patterns.isEmpty()) {
179 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
180 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000181
zachr@google.com945708a2013-07-02 19:55:32 +0000182 // Output to the file specified
183 if (!FLAGS_output.isEmpty()) {
184 SkFILEWStream outputStream(FLAGS_output[0]);
zachr@google.coma95959c2013-07-08 15:04:45 +0000185 ctx.outputRecords(outputStream, FLAGS_jsonp);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000186 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000187
188 return 0;
189}