BsWin32PlatformUtility.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Prerequisites/BsPrerequisitesUtil.h"
  4. #include "Win32/BsWin32PlatformUtility.h"
  5. #include "Image/BsColor.h"
  6. #include <windows.h>
  7. #include <iphlpapi.h>
  8. #include <VersionHelpers.h>
  9. #include <intrin.h>
  10. namespace bs
  11. {
  12. GPUInfo PlatformUtility::sGPUInfo;
  13. void PlatformUtility::terminate(bool force)
  14. {
  15. if (!force)
  16. PostQuitMessage(0);
  17. else
  18. TerminateProcess(GetCurrentProcess(), 0);
  19. }
  20. typedef LONG NTSTATUS, *PNTSTATUS;
  21. typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
  22. RTL_OSVERSIONINFOW GetRealOSVersion()
  23. {
  24. HMODULE handle = GetModuleHandleW(L"ntdll.dll");
  25. if (handle)
  26. {
  27. RtlGetVersionPtr rtlGetVersionFunc = (RtlGetVersionPtr)GetProcAddress(handle, "RtlGetVersion");
  28. if (rtlGetVersionFunc != nullptr)
  29. {
  30. RTL_OSVERSIONINFOW rovi = { 0 };
  31. rovi.dwOSVersionInfoSize = sizeof(rovi);
  32. if (rtlGetVersionFunc(&rovi) == 0)
  33. return rovi;
  34. }
  35. }
  36. RTL_OSVERSIONINFOW rovi = { 0 };
  37. return rovi;
  38. }
  39. SystemInfo PlatformUtility::getSystemInfo()
  40. {
  41. SystemInfo output;
  42. INT32 CPUInfo[4] = { -1 };
  43. // Get CPU manufacturer
  44. __cpuid(CPUInfo, 0);
  45. output.cpuManufacturer = String(12, ' ');
  46. memcpy((char*)output.cpuManufacturer.data(), &CPUInfo[1], 4);
  47. memcpy((char*)output.cpuManufacturer.data() + 4, &CPUInfo[3], 4);
  48. memcpy((char*)output.cpuManufacturer.data() + 8, &CPUInfo[2], 4);
  49. // Get CPU brand string
  50. char brandString[48];
  51. //// Get the information associated with each extended ID.
  52. __cpuid(CPUInfo, 0x80000000);
  53. UINT32 numExtensionIds = CPUInfo[0];
  54. for (UINT32 i = 0x80000000; i <= numExtensionIds; ++i)
  55. {
  56. __cpuid(CPUInfo, i);
  57. if (i == 0x80000002)
  58. memcpy(brandString, CPUInfo, sizeof(CPUInfo));
  59. else if (i == 0x80000003)
  60. memcpy(brandString + 16, CPUInfo, sizeof(CPUInfo));
  61. else if (i == 0x80000004)
  62. memcpy(brandString + 32, CPUInfo, sizeof(CPUInfo));
  63. }
  64. output.cpuModel = brandString;
  65. // Get number of CPU cores
  66. SYSTEM_INFO sysInfo;
  67. GetSystemInfo(&sysInfo);
  68. output.cpuNumCores = (UINT32)sysInfo.dwNumberOfProcessors;
  69. // Get CPU clock speed
  70. HKEY hKey;
  71. long status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ,
  72. &hKey);
  73. if (status == ERROR_SUCCESS)
  74. {
  75. DWORD mhz;
  76. DWORD bufferSize = 4;
  77. RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &mhz, &bufferSize);
  78. output.cpuClockSpeedMhz = (UINT32)mhz;
  79. }
  80. else
  81. output.cpuClockSpeedMhz = 0;
  82. // Get amount of system memory
  83. MEMORYSTATUSEX statex;
  84. statex.dwLength = sizeof(statex);
  85. GlobalMemoryStatusEx(&statex);
  86. output.memoryAmountMb = (UINT32)(statex.ullTotalPhys / (1024 * 1024));
  87. if (BS_ARCH_TYPE == BS_ARCHITECTURE_x86_64)
  88. output.osIs64Bit = true;
  89. else
  90. {
  91. HANDLE process = GetCurrentProcess();
  92. BOOL is64Bit = false;
  93. IsWow64Process(process, (PBOOL)&is64Bit);
  94. output.osIs64Bit = is64Bit > 0;
  95. }
  96. // Get OS version
  97. output.osName = "Windows " + toString((UINT32)GetRealOSVersion().dwMajorVersion);
  98. // Get GPU info
  99. output.gpuInfo = sGPUInfo;
  100. return output;
  101. }
  102. UUID PlatformUtility::generateUUID()
  103. {
  104. ::UUID uuid;
  105. UuidCreate(&uuid);
  106. // Endianess might not be correct, but it shouldn't matter
  107. UINT32 data1 = uuid.Data1;
  108. UINT32 data2 = uuid.Data2 | (uuid.Data3 << 16);
  109. UINT32 data3 = uuid.Data3 | (uuid.Data4[0] << 16) | (uuid.Data4[1] << 24);
  110. UINT32 data4 = uuid.Data4[2] | (uuid.Data4[3] << 8) | (uuid.Data4[4] << 16) | (uuid.Data4[5] << 24);
  111. return UUID(data1, data2, data3, data4);
  112. }
  113. HBITMAP Win32PlatformUtility::createBitmap(const Color* pixels, UINT32 width, UINT32 height, bool premultiplyAlpha)
  114. {
  115. BITMAPINFO bi;
  116. ZeroMemory(&bi, sizeof(BITMAPINFO));
  117. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  118. bi.bmiHeader.biWidth = width;
  119. bi.bmiHeader.biHeight = height;
  120. bi.bmiHeader.biPlanes = 1;
  121. bi.bmiHeader.biBitCount = 32;
  122. bi.bmiHeader.biCompression = BI_RGB;
  123. HDC hDC = GetDC(nullptr);
  124. void* data = nullptr;
  125. HBITMAP hBitmap = CreateDIBSection(hDC, &bi, DIB_RGB_COLORS, (void**)&data, nullptr, 0);
  126. HDC hBitmapDC = CreateCompatibleDC(hDC);
  127. ReleaseDC(nullptr, hDC);
  128. //Select the bitmaps to DC
  129. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
  130. //Scan each pixel of the source bitmap and create the masks
  131. Color pixel;
  132. DWORD *dst = (DWORD*)data;
  133. for (UINT32 y = 0; y < height; ++y)
  134. {
  135. for (UINT32 x = 0; x < width; ++x)
  136. {
  137. UINT32 revY = height - y - 1;
  138. pixel = pixels[revY * width + x];
  139. if (premultiplyAlpha)
  140. {
  141. pixel.r *= pixel.a;
  142. pixel.g *= pixel.a;
  143. pixel.b *= pixel.a;
  144. }
  145. *dst = pixel.getAsBGRA();
  146. dst++;
  147. }
  148. }
  149. SelectObject(hBitmapDC, hOldBitmap);
  150. DeleteDC(hBitmapDC);
  151. return hBitmap;
  152. }
  153. }