1 /* 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 #if SK_SUPPORT_OPENCL 9 10 #define __NO_STD_VECTOR // Uses cl::vectpr instead of std::vectpr 11 #define __NO_STD_STRING // Uses cl::STRING_CLASS instead of std::string 12 #if SK_BUILD_FOR_MAC 13 // Note that some macs don't have this header and it can be downloaded from the Khronos registry 14 # include <OpenCL/cl.hpp> 15 #else 16 # include <CL/cl.hpp> 17 #endif 18 19 #endif 20 21 #include "SkCommandLineFlags.h" 22 #include "SkGraphics.h" 23 #include "SkStream.h" 24 #include "SkTDArray.h" 25 26 #include "SkDifferentPixelsMetric.h" 27 #include "SkDiffContext.h" 28 #include "SkImageDiffer.h" 29 #include "SkPMetric.h" 30 #include "skpdiff_util.h" 31 32 #include "SkForceLinking.h" 33 __SK_FORCE_IMAGE_DECODER_LINKING; 34 35 // Command line argument definitions go here 36 DEFINE_bool2(list, l, false, "List out available differs"); 37 DEFINE_string2(differs, d, "", "The names of the differs to use or all of them by default"); 38 DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>"); 39 DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>"); 40 DEFINE_string2(output, o, "", "Writes the output of these diffs to output: <output>"); 41 DEFINE_bool(jsonp, true, "Output JSON with padding"); 42 DEFINE_string(csv, "", "Writes the output of these diffs to a csv file"); 43 44 #if SK_SUPPORT_OPENCL 45 /// A callback for any OpenCL errors 46 static void CL_CALLBACK error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) { 47 SkDebugf("OpenCL error notify: %s\n", errorInfo); 48 exit(1); 49 } 50 51 /// Creates a device and context with OpenCL 52 static bool init_device_and_context(cl::Device* device, cl::Context* context) { 53 // Query for a platform 54 cl::vector<cl::Platform> platformList; 55 cl::Platform::get(&platformList); 56 SkDebugf("The number of platforms is %u\n", platformList.size()); 57 58 // Print some information about the platform for debugging 59 cl::Platform& platform = platformList[0]; 60 cl::STRING_CLASS platformName; 61 platform.getInfo(CL_PLATFORM_NAME, &platformName); 62 SkDebugf("Platform index 0 is named %s\n", platformName.c_str()); 63 64 // Query for a device 65 cl::vector<cl::Device> deviceList; 66 platform.getDevices(CL_DEVICE_TYPE_ALL, &deviceList); 67 SkDebugf("The number of devices is %u\n", deviceList.size()); 68 69 // Print some information about the device for debugging 70 *device = deviceList[0]; 71 cl::STRING_CLASS deviceName; 72 device->getInfo(CL_DEVICE_NAME, &deviceName); 73 SkDebugf("Device index 0 is named %s\n", deviceName.c_str()); 74 75 // Create a CL context and check for all errors 76 cl_int contextErr = CL_SUCCESS; 77 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr); 78 if (contextErr != CL_SUCCESS) { 79 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr)); 80 return false; 81 } 82 83 return true; 84 } 85 86 static bool init_cl_diff(SkImageDiffer* differ) { 87 // Setup OpenCL 88 cl::Device device; 89 cl::Context context; 90 if (!init_device_and_context(&device, &context)) { 91 return false; 92 } 93 94 // Setup our differ of choice 95 SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ; 96 return clDiffer->init(device(), context()); 97 } 98 #endif 99 100 // TODO Find a better home for the diff registry. One possibility is to have the differs self 101 // register. 102 103 // List here every differ 104 SkDifferentPixelsMetric gDiffPixel; 105 SkPMetric gPDiff; 106 107 // A null terminated array of pointer to every differ declared above 108 SkImageDiffer* gDiffers[] = { &gDiffPixel, &gPDiff, NULL }; 109 110 int tool_main(int argc, char * argv[]); 111 int tool_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"); 121 } 122 123 // Figure which differs the user chose, and optionally print them if the user requests it 124 SkTDArray<SkImageDiffer*> chosenDiffers; 125 for (int differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) { 126 SkImageDiffer* differ = gDiffers[differIndex]; 127 if (FLAGS_list) { 128 SkDebugf(" %s", differ->getName()); 129 SkDebugf("\n"); 130 } 131 132 // Check if this differ was chosen by any of the flags. Initialize them if they were chosen. 133 if (FLAGS_differs.isEmpty()) { 134 // If no differs were chosen, they all get added 135 if (differ->requiresOpenCL()) { 136 #if SK_SUPPORT_OPENCL 137 init_cl_diff(differ); 138 chosenDiffers.push(differ); 139 #endif 140 } else { 141 chosenDiffers.push(differ); 142 } 143 } else { 144 for (int flagIndex = 0; flagIndex < FLAGS_differs.count(); flagIndex++) { 145 if (SkString(FLAGS_differs[flagIndex]).equals(differ->getName())) { 146 // Initialize OpenCL for the differ if it needs it and support was compiled in. 147 if (differ->requiresOpenCL()) { 148 #if SK_SUPPORT_OPENCL 149 init_cl_diff(differ); 150 chosenDiffers.push(differ); 151 #endif 152 } else { 153 chosenDiffers.push(differ); 154 } 155 break; 156 } 157 } 158 } 159 } 160 161 // Don't attempt to initialize the differ if we aren't going to use it 162 if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) { 163 return 0; 164 } 165 166 // Validate command line flags 167 if (!FLAGS_folders.isEmpty()) { 168 if (2 != FLAGS_folders.count()) { 169 SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n"); 170 return 1; 171 } 172 } 173 174 if (!FLAGS_patterns.isEmpty()) { 175 if (2 != FLAGS_patterns.count()) { 176 SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n"); 177 return 1; 178 } 179 } 180 181 if (!FLAGS_csv.isEmpty()) { 182 if (1 != FLAGS_csv.count()) { 183 SkDebugf("csv flag expects one argument: <csv file>\n"); 184 return 1; 185 } 186 } 187 188 SkDiffContext ctx; 189 ctx.setDiffers(chosenDiffers); 190 191 // Perform a folder diff if one is requested 192 if (!FLAGS_folders.isEmpty()) { 193 ctx.diffDirectories(FLAGS_folders[0], FLAGS_folders[1]); 194 } 195 196 // Perform a pattern diff if one is requested 197 if (!FLAGS_patterns.isEmpty()) { 198 ctx.diffPatterns(FLAGS_patterns[0], FLAGS_patterns[1]); 199 } 200 201 // Output to the file specified 202 if (!FLAGS_output.isEmpty()) { 203 SkFILEWStream outputStream(FLAGS_output[0]); 204 ctx.outputRecords(outputStream, FLAGS_jsonp); 205 } 206 207 if (!FLAGS_csv.isEmpty()) { 208 SkFILEWStream outputStream(FLAGS_csv[0]); 209 ctx.outputCsv(outputStream); 210 } 211 212 return 0; 213 } 214 215 #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) 216 int main(int argc, char * argv[]) { 217 return tool_main(argc, (char**) argv); 218 } 219 #endif 220