blob: 21bf7169d4dfa607bf627724612bb4cc9f85c675 [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
12#include "SkOSFile.h"
13#include "SkStream.h"
14#include "SkString.h"
15#include "SkTArray.h"
16#include "SkTDArray.h"
17
18#include "SkImageDiffer.h"
19#include "SkCLImageDiffer.h"
20#include "skpdiff_util.h"
21
22/// A callback for any OpenCL errors
23CL_CALLBACK void error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) {
24 SkDebugf("OpenCL error notify: %s\n", errorInfo);
25 exit(1);
26}
27
28/// Creates a device and context with OpenCL
29static bool init_device_and_context(cl::Device* device, cl::Context* context) {
30 // Query for a platform
31 cl::vector<cl::Platform> platformList;
32 cl::Platform::get(&platformList);
33 SkDebugf("The number of platforms is %u\n", platformList.size());
34
35 // Print some information about the platform for debugging
36 cl::Platform& platform = platformList[0];
37 cl::STRING_CLASS platformName;
38 platform.getInfo(CL_PLATFORM_NAME, &platformName);
39 SkDebugf("Platform index 0 is named %s\n", platformName.c_str());
40
41 // Query for a device
42 cl::vector<cl::Device> deviceList;
43 platform.getDevices(CL_DEVICE_TYPE_GPU, &deviceList);
44 SkDebugf("The number of GPU devices is %u\n", deviceList.size());
45
46 // Print some information about the device for debugging
47 *device = deviceList[0];
48 cl::STRING_CLASS deviceName;
49 device->getInfo(CL_DEVICE_NAME, &deviceName);
50 SkDebugf("Device index 0 is named %s\n", deviceName.c_str());
51
52 // Create a CL context and check for all errors
53 cl_int contextErr = CL_SUCCESS;
54 *context = cl::Context(deviceList, NULL, error_notify, NULL, &contextErr);
55 if (contextErr != CL_SUCCESS) {
56 SkDebugf("Context creation failed: %s\n", cl_error_to_string(contextErr));
57 return false;
58 }
59
60 return true;
61}
62
63/// Compares two directories of images with the given differ
64static void diff_directories(const char baselinePath[], const char testPath[], SkImageDiffer* differ) {
65 // Get the files in the baseline, we will then look for those inside the test path
66 SkTArray<SkString> baselineEntries;
67 if (!get_directory(baselinePath, &baselineEntries)) {
68 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
69 return;
70 }
71
72 SkTDArray<int> queuedDiffIDs;
73 for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselineIndex++) {
74 const char* baseFilename = baselineEntries[baselineIndex].c_str();
75 SkDebugf("%s\n", baseFilename);
76
77 // Find the real location of each file to compare
78 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
79 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
80
81 // Check that the test file exists and is a file
82 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
83 // Queue up the comparison with the differ
84 int diffID = differ->queueDiffOfFile(baselineFile.c_str(), testFile.c_str());
85 if (diffID >= 0) {
86 queuedDiffIDs.push(diffID);
87 SkDebugf("Result: %f\n", differ->getResult(diffID));
88 }
89 } else {
90 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
91 }
92 }
93}
94
95static void print_help()
96{
97 SkDebugf(
98 "Usage:\n" \
99 "skpdiff <baseline directory> <test directory>\n\n"
100 );
101}
102
103int main(int argc, char** argv) {
104 if (argc != 3)
105 {
106 print_help();
107 return 1;
108 }
109
110 // Setup OpenCL
111 cl::Device device;
112 cl::Context context;
113 if (!init_device_and_context(&device, &context)) {
114 return 1;
115 }
116
117 // Setup our differ of choice
118 SkCLImageDiffer* differ = SkNEW(SkDifferentPixelsImageDiffer);
119 if (!differ->init(device(), context())) {
120 return 1;
121 }
122
123 // Diff our folders
124 diff_directories(argv[1], argv[2], differ);
125
126 return 0;
127}