• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Tester Core
3  * ----------------------------------------
4  *
5  * Copyright 2016 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Win32 Vulkan platform
22  *//*--------------------------------------------------------------------*/
23 
24 #include "tcuWin32VulkanPlatform.hpp"
25 #include "tcuWin32Window.hpp"
26 
27 #include "tcuFormatUtil.hpp"
28 #include "tcuFunctionLibrary.hpp"
29 #include "tcuVector.hpp"
30 
31 #include "vkWsiPlatform.hpp"
32 
33 #include "deUniquePtr.hpp"
34 #include "deMemory.h"
35 
36 #pragma comment(lib, "version.lib")
37 
38 namespace tcu
39 {
40 namespace win32
41 {
42 
43 using de::MovePtr;
44 using de::UniquePtr;
45 
46 DE_STATIC_ASSERT(sizeof(vk::pt::Win32InstanceHandle)	== sizeof(HINSTANCE));
47 DE_STATIC_ASSERT(sizeof(vk::pt::Win32WindowHandle)		== sizeof(HWND));
48 
49 class VulkanWindow : public vk::wsi::Win32WindowInterface
50 {
51 public:
VulkanWindow(MovePtr<win32::Window> window)52 	VulkanWindow (MovePtr<win32::Window> window)
53 		: vk::wsi::Win32WindowInterface	(vk::pt::Win32WindowHandle(window->getHandle()))
54 		, m_window						(window)
55 	{
56 	}
57 
resize(const UVec2 & newSize)58 	void resize (const UVec2& newSize)
59 	{
60 		m_window->setSize((int)newSize.x(), (int)newSize.y());
61 	}
62 
63 private:
64 	UniquePtr<win32::Window>	m_window;
65 };
66 
67 class VulkanDisplay : public vk::wsi::Win32DisplayInterface
68 {
69 public:
VulkanDisplay(HINSTANCE instance)70 	VulkanDisplay (HINSTANCE instance)
71 		: vk::wsi::Win32DisplayInterface	(vk::pt::Win32InstanceHandle(instance))
72 	{
73 	}
74 
createWindow(const Maybe<UVec2> & initialSize) const75 	vk::wsi::Window* createWindow (const Maybe<UVec2>& initialSize) const
76 	{
77 		const HINSTANCE	instance	= (HINSTANCE)m_native.internal;
78 		const deUint32	width		= !initialSize ? 400 : initialSize->x();
79 		const deUint32	height		= !initialSize ? 300 : initialSize->y();
80 
81 		return new VulkanWindow(MovePtr<win32::Window>(new win32::Window(instance, (int)width, (int)height)));
82 	}
83 };
84 
85 class VulkanLibrary : public vk::Library
86 {
87 public:
VulkanLibrary(void)88 	VulkanLibrary (void)
89 		: m_library	("vulkan-1.dll")
90 		, m_driver	(m_library)
91 	{
92 	}
93 
getPlatformInterface(void) const94 	const vk::PlatformInterface&	getPlatformInterface	(void) const
95 	{
96 		return m_driver;
97 	}
getFunctionLibrary(void) const98 	const tcu::FunctionLibrary&		getFunctionLibrary		(void) const
99 	{
100 		return m_library;
101 	}
102 
103 private:
104 	const tcu::DynamicFunctionLibrary	m_library;
105 	const vk::PlatformDriver			m_driver;
106 };
107 
VulkanPlatform(HINSTANCE instance)108 VulkanPlatform::VulkanPlatform (HINSTANCE instance)
109 	: m_instance(instance)
110 {
111 }
112 
~VulkanPlatform(void)113 VulkanPlatform::~VulkanPlatform (void)
114 {
115 }
116 
createLibrary(void) const117 vk::Library* VulkanPlatform::createLibrary (void) const
118 {
119 	return new VulkanLibrary();
120 }
121 
getStringRegKey(const std::string & regKey,const std::string & strValueName,std::string & strValue)122 ULONG getStringRegKey (const std::string& regKey, const std::string& strValueName, std::string& strValue)
123 {
124 	HKEY	hKey;
125 	ULONG	nError;
126 	CHAR	szBuffer[512];
127 	DWORD	dwBufferSize = sizeof(szBuffer);
128 
129 	nError = RegOpenKeyExA(HKEY_LOCAL_MACHINE, regKey.c_str(), 0, KEY_READ, &hKey);
130 
131 	if (ERROR_SUCCESS == nError)
132 		nError = RegQueryValueExA(hKey, strValueName.c_str(), 0, DE_NULL, (LPBYTE)szBuffer, &dwBufferSize);
133 
134 	if (ERROR_SUCCESS == nError)
135 		strValue = szBuffer;
136 
137 	return nError;
138 }
139 
getWindowsBits(std::ostream & dst)140 void getWindowsBits (std::ostream& dst)
141 {
142 #if defined(_WIN64)
143 	dst << "64"; // 64-bit programs run only on Win64
144 	return;
145 #elif defined(_WIN32)
146 	BOOL is64 = false;
147 	// 32-bit programs run on both 32-bit and 64-bit Windows.
148 	// Function is defined from XP SP2 onwards, so we don't need to
149 	// check if it exists.
150 	if (IsWow64Process(GetCurrentProcess(), &is64))
151 	{
152 		if (is64)
153 			dst << "64";
154 		else
155 			dst << "32";
156 		return;
157 	}
158 #endif
159 #if !defined(_WIN64)
160 	// IsWow64Process returns failure or neither of
161 	// _WIN64 or _WIN32 is defined
162 	dst << "Unknown";
163 #endif
164 }
165 
getOSNameFromRegistry(std::ostream & dst)166 void getOSNameFromRegistry (std::ostream& dst)
167 {
168 	const char* keypath		= "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
169 	std::string productname	= "Unknown";
170 	std::string releaseid	= "Unknown";
171 	std::string optional;
172 
173 	getStringRegKey(keypath, "ProductName", productname);
174 	getStringRegKey(keypath, "ReleaseId", releaseid);
175 
176 	getWindowsBits(dst);
177 
178 	dst << " bit Windows Product: " << productname << ", Release: " << releaseid;
179 
180 	if (ERROR_SUCCESS == getStringRegKey(keypath, "EditionID", optional))
181 	{
182 		dst << ", Edition: " << optional;
183 		if (ERROR_SUCCESS == getStringRegKey(keypath, "EditionSubstring", optional)
184 			&& optional.length() > 0)
185 			dst << " " << optional;
186 	}
187 }
188 
getOSVersionFromDLL(std::ostream & dst)189 void getOSVersionFromDLL (std::ostream& dst)
190 {
191 	DWORD	buffer_size	= GetFileVersionInfoSize(("kernel32.dll"), DE_NULL);
192 	char*	buffer		= 0;
193 
194 	if (buffer_size != 0)
195 	{
196 		buffer = new char[buffer_size];
197 		if (buffer != 0)
198 		{
199 			if (GetFileVersionInfo("kernel32.dll", 0, buffer_size, buffer))
200 			{
201 				VS_FIXEDFILEINFO*	version		= DE_NULL;
202 				UINT				version_len	= 0;
203 
204 				if (VerQueryValue(buffer, "\\", (LPVOID*)&version, &version_len))
205 				{
206 					dst << ", DLL Version: " << HIWORD(version->dwProductVersionMS) << "." << LOWORD(version->dwProductVersionMS)
207 						<< ", DLL Build: "   << HIWORD(version->dwProductVersionLS) << "." << LOWORD(version->dwProductVersionLS);
208 				}
209 			}
210 			delete[] buffer;
211 		}
212 	}
213 }
214 
215 // Old windows version query APIs lie about the version number. There's no replacement
216 // API, and instead applications are supposed to queriy about capabilities instead of
217 // relying on operating system version numbers.
218 //
219 // Since we want to actually know the version number for debugging purposes, we need
220 // to use roundabout ways to fetch the information.
221 //
222 // The registry contains some useful strings, which we print out if the keys
223 // are available. The current official way to get version number is to look at a
224 // system DLL file and read its version number, so we do that too, in case the
225 // registry becomes unreliable in the future.
226 //
227 // If the DLL method fails, we simply don't print out anything about it.
228 // The minimal output from this function is "Windows Product: Unknown, Release: Unknown"
getOSInfo(std::ostream & dst)229 static void getOSInfo (std::ostream& dst)
230 {
231 	getOSNameFromRegistry(dst);
232 	getOSVersionFromDLL(dst);
233 }
234 
getProcessorArchitectureName(WORD arch)235 const char* getProcessorArchitectureName (WORD arch)
236 {
237 	switch (arch)
238 	{
239 		case PROCESSOR_ARCHITECTURE_AMD64:		return "AMD64";
240 		case PROCESSOR_ARCHITECTURE_ARM:		return "ARM";
241 		case PROCESSOR_ARCHITECTURE_IA64:		return "IA64";
242 		case PROCESSOR_ARCHITECTURE_INTEL:		return "INTEL";
243 		case PROCESSOR_ARCHITECTURE_UNKNOWN:	return "UNKNOWN";
244 		default:								return DE_NULL;
245 	}
246 }
247 
getProcessorInfo(std::ostream & dst)248 static void getProcessorInfo (std::ostream& dst)
249 {
250 	SYSTEM_INFO	sysInfo;
251 
252 	deMemset(&sysInfo, 0, sizeof(sysInfo));
253 	GetSystemInfo(&sysInfo);
254 
255 	dst << "arch ";
256 	{
257 		const char* const	archName	= getProcessorArchitectureName(sysInfo.wProcessorArchitecture);
258 
259 		if (archName)
260 			dst << archName;
261 		else
262 			dst << tcu::toHex(sysInfo.wProcessorArchitecture);
263 	}
264 
265 	dst << ", level " << tcu::toHex(sysInfo.wProcessorLevel) << ", revision " << tcu::toHex(sysInfo.wProcessorRevision);
266 }
267 
describePlatform(std::ostream & dst) const268 void VulkanPlatform::describePlatform (std::ostream& dst) const
269 {
270 	dst << "OS: ";
271 	getOSInfo(dst);
272 	dst << "\n";
273 
274 	dst << "CPU: ";
275 	getProcessorInfo(dst);
276 	dst << "\n";
277 }
278 
getMemoryLimits(vk::PlatformMemoryLimits & limits) const279 void VulkanPlatform::getMemoryLimits (vk::PlatformMemoryLimits& limits) const
280 {
281 	limits.totalSystemMemory					= 256*1024*1024;
282 	limits.totalDeviceLocalMemory				= 128*1024*1024;
283 	limits.deviceMemoryAllocationGranularity	= 64*1024;
284 	limits.devicePageSize						= 4096;
285 	limits.devicePageTableEntrySize				= 8;
286 	limits.devicePageTableHierarchyLevels		= 3;
287 }
288 
createWsiDisplay(vk::wsi::Type wsiType) const289 vk::wsi::Display* VulkanPlatform::createWsiDisplay (vk::wsi::Type wsiType) const
290 {
291 	if (wsiType != vk::wsi::TYPE_WIN32)
292 		TCU_THROW(NotSupportedError, "WSI type not supported");
293 
294 	return new VulkanDisplay(m_instance);
295 }
296 
hasDisplay(vk::wsi::Type wsiType) const297 bool VulkanPlatform::hasDisplay (vk::wsi::Type wsiType)  const
298 {
299 	if (wsiType != vk::wsi::TYPE_WIN32)
300 		return false;
301 
302 	return true;
303 }
304 
305 } // win32
306 } // tcu
307