1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // system_utils_win.cpp: Implementation of OS-specific functions for Windows
8
9 #include "system_utils.h"
10
11 #include <stdarg.h>
12 #include <windows.h>
13 #include <array>
14 #include <vector>
15
16 namespace angle
17 {
18
19 namespace
20 {
21
GetPath(HMODULE module)22 std::string GetPath(HMODULE module)
23 {
24 std::array<wchar_t, MAX_PATH> executableFileBuf;
25 DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(),
26 static_cast<DWORD>(executableFileBuf.size()));
27 return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L"");
28 }
29
GetDirectory(HMODULE module)30 std::string GetDirectory(HMODULE module)
31 {
32 std::string executablePath = GetPath(module);
33 size_t lastPathSepLoc = executablePath.find_last_of("\\/");
34 return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
35 }
36
37 } // anonymous namespace
38
GetExecutablePath()39 std::string GetExecutablePath()
40 {
41 return GetPath(nullptr);
42 }
43
GetExecutableDirectory()44 std::string GetExecutableDirectory()
45 {
46 return GetDirectory(nullptr);
47 }
48
GetSharedLibraryExtension()49 const char *GetSharedLibraryExtension()
50 {
51 return "dll";
52 }
53
GetCWD()54 Optional<std::string> GetCWD()
55 {
56 std::array<wchar_t, MAX_PATH> pathBuf;
57 DWORD result = GetCurrentDirectoryW(static_cast<DWORD>(pathBuf.size()), pathBuf.data());
58 if (result == 0)
59 {
60 return Optional<std::string>::Invalid();
61 }
62 return Narrow(pathBuf.data());
63 }
64
SetCWD(const char * dirName)65 bool SetCWD(const char *dirName)
66 {
67 return (SetCurrentDirectoryW(Widen(dirName).c_str()) == TRUE);
68 }
69
GetPathSeparatorForEnvironmentVar()70 const char *GetPathSeparatorForEnvironmentVar()
71 {
72 return ";";
73 }
74
GetCurrentSystemTime()75 double GetCurrentSystemTime()
76 {
77 LARGE_INTEGER frequency = {};
78 QueryPerformanceFrequency(&frequency);
79
80 LARGE_INTEGER curTime;
81 QueryPerformanceCounter(&curTime);
82
83 return static_cast<double>(curTime.QuadPart) / frequency.QuadPart;
84 }
85
GetCurrentProcessCpuTime()86 double GetCurrentProcessCpuTime()
87 {
88 FILETIME creationTime = {};
89 FILETIME exitTime = {};
90 FILETIME kernelTime = {};
91 FILETIME userTime = {};
92
93 // Note this will not give accurate results if the current thread is
94 // scheduled for less than the tick rate, which is often 15 ms. In that
95 // case, GetProcessTimes will not return different values, making it
96 // possible to end up with 0 ms for a process that takes 93% of a core
97 // (14/15 ms)! An alternative is QueryProcessCycleTime but there is no
98 // simple way to convert cycles back to seconds, and on top of that, it's
99 // not supported pre-Windows Vista.
100
101 // Returns 100-ns intervals, so we want to divide by 1e7 to get seconds
102 GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime);
103
104 ULARGE_INTEGER kernelInt64;
105 kernelInt64.LowPart = kernelTime.dwLowDateTime;
106 kernelInt64.HighPart = kernelTime.dwHighDateTime;
107 double systemTimeSeconds = static_cast<double>(kernelInt64.QuadPart) * 1e-7;
108
109 ULARGE_INTEGER userInt64;
110 userInt64.LowPart = userTime.dwLowDateTime;
111 userInt64.HighPart = userTime.dwHighDateTime;
112 double userTimeSeconds = static_cast<double>(userInt64.QuadPart) * 1e-7;
113
114 return systemTimeSeconds + userTimeSeconds;
115 }
116
IsDirectory(const char * filename)117 bool IsDirectory(const char *filename)
118 {
119 WIN32_FILE_ATTRIBUTE_DATA fileInformation;
120
121 BOOL result =
122 GetFileAttributesExW(Widen(filename).c_str(), GetFileExInfoStandard, &fileInformation);
123 if (result)
124 {
125 DWORD attribs = fileInformation.dwFileAttributes;
126 return (attribs != INVALID_FILE_ATTRIBUTES) && ((attribs & FILE_ATTRIBUTE_DIRECTORY) > 0);
127 }
128
129 return false;
130 }
131
IsDebuggerAttached()132 bool IsDebuggerAttached()
133 {
134 return !!::IsDebuggerPresent();
135 }
136
BreakDebugger()137 void BreakDebugger()
138 {
139 __debugbreak();
140 }
141
GetExecutableExtension()142 const char *GetExecutableExtension()
143 {
144 return ".exe";
145 }
146
GetPathSeparator()147 char GetPathSeparator()
148 {
149 return '\\';
150 }
151
GetModuleDirectory()152 std::string GetModuleDirectory()
153 {
154 // GetModuleHandleEx is unavailable on UWP
155 #if !defined(ANGLE_IS_WINUWP)
156 static int placeholderSymbol = 0;
157 HMODULE module = nullptr;
158 if (GetModuleHandleExW(
159 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
160 reinterpret_cast<LPCWSTR>(&placeholderSymbol), &module))
161 {
162 return GetDirectory(module);
163 }
164 #endif
165 return GetDirectory(nullptr);
166 }
167
GetRootDirectory()168 std::string GetRootDirectory()
169 {
170 return "C:\\";
171 }
172
GetLibraryPath(void * libraryHandle)173 std::string GetLibraryPath(void *libraryHandle)
174 {
175 if (!libraryHandle)
176 {
177 return "";
178 }
179
180 std::array<wchar_t, MAX_PATH> buffer;
181 if (GetModuleFileNameW(reinterpret_cast<HMODULE>(libraryHandle), buffer.data(),
182 buffer.size()) == 0)
183 {
184 return "";
185 }
186
187 return Narrow(buffer.data());
188 }
189
GetLibrarySymbol(void * libraryHandle,const char * symbolName)190 void *GetLibrarySymbol(void *libraryHandle, const char *symbolName)
191 {
192 if (!libraryHandle)
193 {
194 fprintf(stderr, "Module was not loaded\n");
195 return nullptr;
196 }
197
198 return reinterpret_cast<void *>(
199 GetProcAddress(reinterpret_cast<HMODULE>(libraryHandle), symbolName));
200 }
201
CloseSystemLibrary(void * libraryHandle)202 void CloseSystemLibrary(void *libraryHandle)
203 {
204 if (libraryHandle)
205 {
206 FreeLibrary(reinterpret_cast<HMODULE>(libraryHandle));
207 }
208 }
Narrow(const std::wstring_view & utf16)209 std::string Narrow(const std::wstring_view &utf16)
210 {
211 if (utf16.empty())
212 {
213 return {};
214 }
215 int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()),
216 nullptr, 0, nullptr, nullptr);
217 std::string utf8(requiredSize, '\0');
218 WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0],
219 requiredSize, nullptr, nullptr);
220 return utf8;
221 }
222
Widen(const std::string_view & utf8)223 std::wstring Widen(const std::string_view &utf8)
224 {
225 if (utf8.empty())
226 {
227 return {};
228 }
229 int requiredSize =
230 MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), nullptr, 0);
231 std::wstring utf16(requiredSize, L'\0');
232 MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), &utf16[0],
233 requiredSize);
234 return utf16;
235 }
236
237 } // namespace angle
238