OLD | NEW |
| (Empty) |
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 #include "SkTypes.h" | |
9 | |
10 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_
FOR_ANDROID) | |
11 # include <unistd.h> | |
12 # include <sys/time.h> | |
13 # include <dirent.h> | |
14 #endif | |
15 | |
16 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) | |
17 # include <glob.h> | |
18 #endif | |
19 | |
20 #if defined(SK_BUILD_FOR_MAC) | |
21 # include <sys/syslimits.h> // PATH_MAX is here for Macs | |
22 #endif | |
23 | |
24 #if defined(SK_BUILD_FOR_WIN32) | |
25 # include <windows.h> | |
26 #endif | |
27 | |
28 #include <stdlib.h> | |
29 #include <time.h> | |
30 #include "SkOSFile.h" | |
31 #include "skpdiff_util.h" | |
32 | |
33 #if SK_SUPPORT_OPENCL | |
34 const char* cl_error_to_string(cl_int err) { | |
35 switch (err) { | |
36 case CL_SUCCESS: return "CL_SUCCESS"; | |
37 case CL_DEVICE_NOT_FOUND: return "CL_DEVICE_NOT_FOUND"; | |
38 case CL_DEVICE_NOT_AVAILABLE: return "CL_DEVICE_NOT_AVAILABLE
"; | |
39 case CL_COMPILER_NOT_AVAILABLE: return "CL_COMPILER_NOT_AVAILAB
LE"; | |
40 case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "CL_MEM_OBJECT_ALLOCATIO
N_FAILURE"; | |
41 case CL_OUT_OF_RESOURCES: return "CL_OUT_OF_RESOURCES"; | |
42 case CL_OUT_OF_HOST_MEMORY: return "CL_OUT_OF_HOST_MEMORY"; | |
43 case CL_PROFILING_INFO_NOT_AVAILABLE: return "CL_PROFILING_INFO_NOT_A
VAILABLE"; | |
44 case CL_MEM_COPY_OVERLAP: return "CL_MEM_COPY_OVERLAP"; | |
45 case CL_IMAGE_FORMAT_MISMATCH: return "CL_IMAGE_FORMAT_MISMATC
H"; | |
46 case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "CL_IMAGE_FORMAT_NOT_SUP
PORTED"; | |
47 case CL_BUILD_PROGRAM_FAILURE: return "CL_BUILD_PROGRAM_FAILUR
E"; | |
48 case CL_MAP_FAILURE: return "CL_MAP_FAILURE"; | |
49 case CL_INVALID_VALUE: return "CL_INVALID_VALUE"; | |
50 case CL_INVALID_DEVICE_TYPE: return "CL_INVALID_DEVICE_TYPE"
; | |
51 case CL_INVALID_PLATFORM: return "CL_INVALID_PLATFORM"; | |
52 case CL_INVALID_DEVICE: return "CL_INVALID_DEVICE"; | |
53 case CL_INVALID_CONTEXT: return "CL_INVALID_CONTEXT"; | |
54 case CL_INVALID_QUEUE_PROPERTIES: return "CL_INVALID_QUEUE_PROPER
TIES"; | |
55 case CL_INVALID_COMMAND_QUEUE: return "CL_INVALID_COMMAND_QUEU
E"; | |
56 case CL_INVALID_HOST_PTR: return "CL_INVALID_HOST_PTR"; | |
57 case CL_INVALID_MEM_OBJECT: return "CL_INVALID_MEM_OBJECT"; | |
58 case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "CL_INVALID_IMAGE_FORMAT
_DESCRIPTOR"; | |
59 case CL_INVALID_IMAGE_SIZE: return "CL_INVALID_IMAGE_SIZE"; | |
60 case CL_INVALID_SAMPLER: return "CL_INVALID_SAMPLER"; | |
61 case CL_INVALID_BINARY: return "CL_INVALID_BINARY"; | |
62 case CL_INVALID_BUILD_OPTIONS: return "CL_INVALID_BUILD_OPTION
S"; | |
63 case CL_INVALID_PROGRAM: return "CL_INVALID_PROGRAM"; | |
64 case CL_INVALID_PROGRAM_EXECUTABLE: return "CL_INVALID_PROGRAM_EXEC
UTABLE"; | |
65 case CL_INVALID_KERNEL_NAME: return "CL_INVALID_KERNEL_NAME"
; | |
66 case CL_INVALID_KERNEL_DEFINITION: return "CL_INVALID_KERNEL_DEFIN
ITION"; | |
67 case CL_INVALID_KERNEL: return "CL_INVALID_KERNEL"; | |
68 case CL_INVALID_ARG_INDEX: return "CL_INVALID_ARG_INDEX"; | |
69 case CL_INVALID_ARG_VALUE: return "CL_INVALID_ARG_VALUE"; | |
70 case CL_INVALID_ARG_SIZE: return "CL_INVALID_ARG_SIZE"; | |
71 case CL_INVALID_KERNEL_ARGS: return "CL_INVALID_KERNEL_ARGS"
; | |
72 case CL_INVALID_WORK_DIMENSION: return "CL_INVALID_WORK_DIMENSI
ON"; | |
73 case CL_INVALID_WORK_GROUP_SIZE: return "CL_INVALID_WORK_GROUP_S
IZE"; | |
74 case CL_INVALID_WORK_ITEM_SIZE: return "CL_INVALID_WORK_ITEM_SI
ZE"; | |
75 case CL_INVALID_GLOBAL_OFFSET: return "CL_INVALID_GLOBAL_OFFSE
T"; | |
76 case CL_INVALID_EVENT_WAIT_LIST: return "CL_INVALID_EVENT_WAIT_L
IST"; | |
77 case CL_INVALID_EVENT: return "CL_INVALID_EVENT"; | |
78 case CL_INVALID_OPERATION: return "CL_INVALID_OPERATION"; | |
79 case CL_INVALID_GL_OBJECT: return "CL_INVALID_GL_OBJECT"; | |
80 case CL_INVALID_BUFFER_SIZE: return "CL_INVALID_BUFFER_SIZE"
; | |
81 case CL_INVALID_MIP_LEVEL: return "CL_INVALID_MIP_LEVEL"; | |
82 default: return "UNKNOWN"; | |
83 } | |
84 return "UNKNOWN"; | |
85 } | |
86 #endif | |
87 | |
88 // TODO refactor Timer to be used here | |
89 double get_seconds() { | |
90 #if defined(SK_BUILD_FOR_WIN32) | |
91 LARGE_INTEGER currentTime; | |
92 LARGE_INTEGER frequency; | |
93 QueryPerformanceCounter(¤tTime); | |
94 QueryPerformanceFrequency(&frequency); | |
95 return (double)currentTime.QuadPart / (double)frequency.QuadPart; | |
96 #elif _POSIX_TIMERS > 0 && defined(CLOCK_REALTIME) | |
97 struct timespec currentTime; | |
98 clock_gettime(CLOCK_REALTIME, ¤tTime); | |
99 return currentTime.tv_sec + (double)currentTime.tv_nsec / 1e9; | |
100 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUIL
D_FOR_ANDROID) | |
101 struct timeval currentTime; | |
102 gettimeofday(¤tTime, nullptr); | |
103 return currentTime.tv_sec + (double)currentTime.tv_usec / 1e6; | |
104 #else | |
105 return clock() / (double)CLOCKS_PER_SEC; | |
106 #endif | |
107 } | |
108 | |
109 bool get_directory(const char path[], SkTArray<SkString>* entries) { | |
110 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_
FOR_ANDROID) | |
111 // Open the directory and check for success | |
112 DIR* dir = opendir(path); | |
113 if (nullptr == dir) { | |
114 return false; | |
115 } | |
116 | |
117 // Loop through dir entries until there are none left (i.e. readdir returns
nullptr) | |
118 struct dirent* entry; | |
119 while ((entry = readdir(dir))) { | |
120 // dirent only gives relative paths, we need to join them to the base pa
th to check if they | |
121 // are directories. | |
122 SkString joinedPath = SkOSPath::Join(path, entry->d_name); | |
123 | |
124 // We only care about files | |
125 if (!sk_isdir(joinedPath.c_str())) { | |
126 entries->push_back(SkString(entry->d_name)); | |
127 } | |
128 } | |
129 | |
130 closedir(dir); | |
131 | |
132 return true; | |
133 #elif defined(SK_BUILD_FOR_WIN32) | |
134 char pathDirGlob[MAX_PATH]; | |
135 size_t pathLength = strlen(path); | |
136 strncpy(pathDirGlob, path, pathLength); | |
137 | |
138 if (path[pathLength - 1] == '/' || path[pathLength - 1] == '\\') { | |
139 SkASSERT(pathLength + 2 <= MAX_PATH); | |
140 pathDirGlob[pathLength] = '*'; | |
141 pathDirGlob[pathLength + 1] = '\0'; | |
142 } else { | |
143 SkASSERT(pathLength + 3 <= MAX_PATH); | |
144 pathDirGlob[pathLength] = '\\'; | |
145 pathDirGlob[pathLength + 1] = '*'; | |
146 pathDirGlob[pathLength + 2] = '\0'; | |
147 } | |
148 | |
149 WIN32_FIND_DATA findFileData; | |
150 HANDLE hFind = FindFirstFile(pathDirGlob, &findFileData); | |
151 if (INVALID_HANDLE_VALUE == hFind) { | |
152 return false; | |
153 } | |
154 | |
155 do { | |
156 if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { | |
157 entries->push_back(SkString(findFileData.cFileName)); | |
158 } | |
159 } while (FindNextFile(hFind, &findFileData) != 0); | |
160 | |
161 FindClose(hFind); | |
162 return true; | |
163 #else | |
164 return false; | |
165 #endif | |
166 } | |
167 | |
168 bool glob_files(const char globPattern[], SkTArray<SkString>* entries) { | |
169 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) | |
170 // TODO Make sure this works on windows. This may require use of FindNextFil
e windows function. | |
171 glob_t globBuffer; | |
172 if (glob(globPattern, 0, nullptr, &globBuffer) != 0) { | |
173 return false; | |
174 } | |
175 | |
176 // Note these paths are in sorted order by default according to http://linux
.die.net/man/3/glob | |
177 // Check under the flag GLOB_NOSORT | |
178 char** paths = globBuffer.gl_pathv; | |
179 while(*paths) { | |
180 entries->push_back(SkString(*paths)); | |
181 paths++; | |
182 } | |
183 | |
184 globfree(&globBuffer); | |
185 | |
186 return true; | |
187 #else | |
188 return false; | |
189 #endif | |
190 } | |
191 | |
192 SkString get_absolute_path(const SkString& path) { | |
193 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_
FOR_ANDROID) | |
194 SkString fullPath(PATH_MAX + 1); | |
195 if (realpath(path.c_str(), fullPath.writable_str()) == nullptr) { | |
196 fullPath.reset(); | |
197 } | |
198 return fullPath; | |
199 #elif defined(SK_BUILD_FOR_WIN32) | |
200 SkString fullPath(MAX_PATH); | |
201 if (_fullpath(fullPath.writable_str(), path.c_str(), MAX_PATH) == nullptr) { | |
202 fullPath.reset(); | |
203 } | |
204 return fullPath; | |
205 #else | |
206 return SkString(); | |
207 #endif | |
208 } | |
OLD | NEW |