blob: de1171ff3a47c083eaa15454ec67b995b95ef7fe [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.comdb54dd32013-06-27 17:51:35 +000032
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000033/// A callback for any OpenCL errors
34CL_CALLBACK void error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) {
35 SkDebugf("OpenCL error notify: %s\n", errorInfo);
36 exit(1);
37}
38
39/// Creates a device and context with OpenCL
40static bool init_device_and_context(cl::Device* device, cl::Context* context) {
41 // Query for a platform
42 cl::vector<cl::Platform> platformList;
43 cl::Platform::get(&platformList);
44 SkDebugf("The number of platforms is %u\n", platformList.size());
45
46 // Print some information about the platform for debugging
47 cl::Platform& platform = platformList[0];
48 cl::STRING_CLASS platformName;
49 platform.getInfo(CL_PLATFORM_NAME, &platformName);
50 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
51
52 // Query for a device
53 cl::vector<cl::Device> deviceList;
54 platform.getDevices(CL_DEVICE_TYPE_GPU, &deviceList);
55 SkDebugf("The number of GPU devices is %u\n", deviceList.size());
56
57 // Print some information about the device for debugging
58 *device = deviceList[0];
59 cl::STRING_CLASS deviceName;
60 device->getInfo(CL_DEVICE_NAME, &deviceName);
61 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
62
63 // Create a CL context and check for all errors
64 cl_int contextErr = CL_SUCCESS;
65 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
66 if (contextErr != CL_SUCCESS) {
67 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
68 return false;
69 }
70
71 return true;
72}
73
zachr@google.comdb54dd32013-06-27 17:51:35 +000074
75
zachr@google.comc0a75a82013-06-28 15:34:56 +000076static bool init_cl_diff(SkImageDiffer* differ) {
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000077 // Setup OpenCL
78 cl::Device device;
79 cl::Context context;
80 if (!init_device_and_context(&device, &context)) {
zachr@google.comdb54dd32013-06-27 17:51:35 +000081 return false;
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +000082 }
83
84 // Setup our differ of choice
zachr@google.comdb54dd32013-06-27 17:51:35 +000085 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ;
86 return clDiffer->init(device(), context());
87}
88
zachr@google.comc0a75a82013-06-28 15:34:56 +000089static bool init_dummy(SkImageDiffer* differ) {
90 return true;
91}
92
93
zachr@google.comdb54dd32013-06-27 17:51:35 +000094// TODO Find a better home for the diff registry. One possibility is to have the differs self
95// register.
96
97// List here every differ
98SkDifferentPixelsImageDiffer gDiffPixel;
zachr@google.comc0a75a82013-06-28 15:34:56 +000099SkPMetric gPDiff;
zachr@google.comdb54dd32013-06-27 17:51:35 +0000100
zachr@google.comc0a75a82013-06-28 15:34:56 +0000101// A null terminated array of pointer to every differ declared above
102SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000103
zachr@google.comc0a75a82013-06-28 15:34:56 +0000104// A parallel array of functions to initialize the above differs. The reason we don't initialize
105// everything immediately is that certain differs may require special initialization, but we still
106// want to construct all of them globally so they can be queried for things like their name and
107// description.
108bool (*gDiffInits[])(SkImageDiffer*) = { init_cl_diff, init_dummy, NULL };
zachr@google.comdb54dd32013-06-27 17:51:35 +0000109
110
111int main(int argc, char** argv) {
112 // 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.com945708a2013-07-02 19:55:32 +0000135 chosenDiffers.push(differ);
136 gDiffInits[differIndex](differ);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000137 } else {
138 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000139 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) {
140 chosenDiffers.push(differ);
141 gDiffInits[differIndex](differ);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000142 break;
143 }
144 }
145 }
146 }
147
148 // Don't attempt to initialize the differ if we aren't going to use it
149 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) {
150 return 0;
151 }
152
153 // Validate command line flags
154 if (!FLAGS_folders.isEmpty()) {
155 if (2 != FLAGS_folders.count()) {
156 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n");
157 return 1;
158 }
159 }
160
161 if (!FLAGS_patterns.isEmpty()) {
162 if (2 != FLAGS_patterns.count()) {
163 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n");
164 return 1;
165 }
166 }
167
zachr@google.com945708a2013-07-02 19:55:32 +0000168 SkDiffContext ctx;
169 ctx.setDiffers(chosenDiffers);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000170
zachr@google.com945708a2013-07-02 19:55:32 +0000171 // Perform a folder diff if one is requested
172 if (!FLAGS_folders.isEmpty()) {
173 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]);
174 }
zachr@google.comc0a75a82013-06-28 15:34:56 +0000175
zachr@google.com945708a2013-07-02 19:55:32 +0000176 // Perform a pattern diff if one is requested
177 if (!FLAGS_patterns.isEmpty()) {
178 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]);
179 }
zachr@google.comdb54dd32013-06-27 17:51:35 +0000180
zachr@google.com945708a2013-07-02 19:55:32 +0000181 // Output to the file specified
182 if (!FLAGS_output.isEmpty()) {
183 SkFILEWStream outputStream(FLAGS_output[0]);
184 ctx.outputRecords(outputStream);
zachr@google.comdb54dd32013-06-27 17:51:35 +0000185 }
commit-bot@chromium.orgbe19b9e2013-06-14 17:26:54 +0000186
187 return 0;
188}