Panda3D
Loading...
Searching...
No Matches
winGraphicsPipe.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file winGraphicsPipe.cxx
10 * @author drose
11 * @date 2002-12-20
12 */
13
14#include "winGraphicsPipe.h"
15#include "config_windisplay.h"
17#include "displayInformation.h"
18#include "dtool_config.h"
19#include "pbitops.h"
20
21#include <psapi.h>
22#include <powrprof.h>
23#include <intrin.h>
24
25TypeHandle WinGraphicsPipe::_type_handle;
26
27#ifndef MAXIMUM_PROCESSORS
28#define MAXIMUM_PROCESSORS 32
29#endif
30
31typedef enum _Process_DPI_Awareness {
32 Process_DPI_Unaware = 0,
33 Process_System_DPI_Aware = 1,
34 Process_Per_Monitor_DPI_Aware = 2
35} Process_DPI_Awareness;
36
37typedef struct _PROCESSOR_POWER_INFORMATION {
38 ULONG Number;
39 ULONG MaxMhz;
40 ULONG CurrentMhz;
41 ULONG MhzLimit;
42 ULONG MaxIdleState;
43 ULONG CurrentIdleState;
44} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
45
46typedef BOOL (WINAPI *GetProcessMemoryInfoType) (HANDLE Process, PROCESS_MEMORY_COUNTERS *ppsmemCounters, DWORD cb);
47typedef long (__stdcall *CallNtPowerInformationType) (POWER_INFORMATION_LEVEL information_level, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength);
48
49static int initialize = false;
50static HMODULE psapi_dll = 0;
51static GetProcessMemoryInfoType GetProcessMemoryInfoFunction = 0;
52static CallNtPowerInformationType CallNtPowerInformationFunction = 0;
53
54void get_memory_information (DisplayInformation *display_information) {
55 if (initialize == false) {
56 psapi_dll = LoadLibrary("psapi.dll");
57 if (psapi_dll) {
58 GetProcessMemoryInfoFunction = (GetProcessMemoryInfoType) GetProcAddress(psapi_dll, "GetProcessMemoryInfo");
59 }
60
61 initialize = true;
62 }
63
64 MEMORYSTATUSEX memory_status;
65
66 memory_status.dwLength = sizeof(MEMORYSTATUSEX);
67 if (GlobalMemoryStatusEx(&memory_status)) {
68 display_information->_physical_memory = memory_status.ullTotalPhys;
69 display_information->_available_physical_memory = memory_status.ullAvailPhys;
70 display_information->_page_file_size = memory_status.ullTotalPageFile;
71 display_information->_available_page_file_size = memory_status.ullAvailPageFile;
72 display_information->_process_virtual_memory = memory_status.ullTotalVirtual;
73 display_information->_available_process_virtual_memory = memory_status.ullAvailVirtual;
74 display_information->_memory_load = memory_status.dwMemoryLoad;
75 }
76
77 if (GetProcessMemoryInfoFunction) {
78 HANDLE process;
79 DWORD process_id;
80 PROCESS_MEMORY_COUNTERS process_memory_counters;
81
82 process_id = GetCurrentProcessId();
83 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_id);
84 if (process) {
85 if (GetProcessMemoryInfoFunction (process, &process_memory_counters, sizeof(PROCESS_MEMORY_COUNTERS))) {
86 display_information->_page_fault_count = process_memory_counters.PageFaultCount;
87 display_information->_process_memory = process_memory_counters.WorkingSetSize;
88 display_information->_peak_process_memory = process_memory_counters.PeakWorkingSetSize;
89 display_information->_page_file_usage = process_memory_counters.PagefileUsage;
90 display_information->_peak_page_file_usage = process_memory_counters.PeakPagefileUsage;
91 }
92
93 CloseHandle(process);
94 }
95 }
96}
97
98int update_cpu_frequency_function(int processor_number, DisplayInformation *display_information) {
99 int update;
100
101 update = false;
102 display_information->_maximum_cpu_frequency = 0;
103 display_information->_current_cpu_frequency = 0;
104
105 if (CallNtPowerInformationFunction) {
106
107 int i;
108 PVOID input_buffer;
109 PVOID output_buffer;
110 ULONG input_buffer_size;
111 ULONG output_buffer_size;
112 POWER_INFORMATION_LEVEL information_level;
113 PROCESSOR_POWER_INFORMATION *processor_power_information;
114 PROCESSOR_POWER_INFORMATION processor_power_information_array [MAXIMUM_PROCESSORS];
115
116 memset(processor_power_information_array, 0, sizeof(PROCESSOR_POWER_INFORMATION) * MAXIMUM_PROCESSORS);
117
118 processor_power_information = processor_power_information_array;
119 for (i = 0; i < MAXIMUM_PROCESSORS; i++) {
120 processor_power_information->Number = 0xFFFFFFFF;
121 processor_power_information++;
122 }
123
124 information_level = ProcessorInformation;
125 input_buffer = nullptr;
126 output_buffer = processor_power_information_array;
127 input_buffer_size = 0;
128 output_buffer_size = sizeof(PROCESSOR_POWER_INFORMATION) * MAXIMUM_PROCESSORS;
129 if (CallNtPowerInformationFunction(information_level, input_buffer, input_buffer_size, output_buffer, output_buffer_size) == 0) {
130 processor_power_information = processor_power_information_array;
131 for (i = 0; i < MAXIMUM_PROCESSORS; i++) {
132 if (processor_power_information->Number == processor_number) {
133 uint64_t value;
134
135 value = processor_power_information->MaxMhz;
136 display_information->_maximum_cpu_frequency = value * 1000000;
137
138 value = processor_power_information->CurrentMhz;
139 display_information->_current_cpu_frequency = value * 1000000;
140 update = true;
141
142 break;
143 }
144
145 processor_power_information++;
146 }
147 }
148 }
149
150 return update;
151}
152
153void
154count_number_of_cpus(DisplayInformation *display_information) {
155 int num_cpu_cores = 0;
156 int num_logical_cpus = 0;
157
158 // Get a pointer to the GetLogicalProcessorInformation function.
159 typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,
160 PDWORD);
161 LPFN_GLPI glpi;
162 glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
163 "GetLogicalProcessorInformation");
164 if (glpi == nullptr) {
165 windisplay_cat.info()
166 << "GetLogicalProcessorInformation is not supported.\n";
167 return;
168 }
169
170 // Allocate a buffer to hold the result of the
171 // GetLogicalProcessorInformation call.
172 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = nullptr;
173 DWORD buffer_length = 0;
174 DWORD rc = glpi(buffer, &buffer_length);
175 while (!rc) {
176 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
177 if (buffer != nullptr) {
178 PANDA_FREE_ARRAY(buffer);
179 }
180
181 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)PANDA_MALLOC_ARRAY(buffer_length);
182 nassertv(buffer != nullptr);
183 } else {
184 windisplay_cat.info()
185 << "GetLogicalProcessorInformation failed: " << GetLastError()
186 << "\n";
187 return;
188 }
189 rc = glpi(buffer, &buffer_length);
190 }
191
192 // Now get the results.
193 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
194 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION end = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)((char *)buffer + buffer_length);
195
196 while (ptr < end) {
197 if (ptr->Relationship == RelationProcessorCore) {
198 num_cpu_cores++;
199
200 // A hyperthreaded core supplies more than one logical processor.
201 num_logical_cpus += count_bits_in_word((uint64_t)(ptr->ProcessorMask));
202 }
203 ++ptr;
204 }
205
206 PANDA_FREE_ARRAY(buffer);
207
208 windisplay_cat.info()
209 << num_cpu_cores << " CPU cores, with "
210 << num_logical_cpus << " logical processors.\n";
211
212 display_information->_num_cpu_cores = num_cpu_cores;
213 display_information->_num_logical_cpus = num_logical_cpus;
214}
215
216
217/**
218 *
219 */
220WinGraphicsPipe::
221WinGraphicsPipe() {
222 char string [512];
223
224 _supported_types = OT_window | OT_fullscreen_window;
225
226 HMODULE user32 = GetModuleHandleA("user32.dll");
227 if (user32 != nullptr) {
228 if (dpi_aware) {
229 typedef HRESULT (WINAPI *PFN_SETPROCESSDPIAWARENESS)(Process_DPI_Awareness);
230 PFN_SETPROCESSDPIAWARENESS pfnSetProcessDpiAwareness =
231 (PFN_SETPROCESSDPIAWARENESS)GetProcAddress(user32, "SetProcessDpiAwarenessInternal");
232
233 if (pfnSetProcessDpiAwareness == nullptr) {
234 if (windisplay_cat.is_debug()) {
235 windisplay_cat.debug() << "Unable to find SetProcessDpiAwareness in user32.dll.\n";
236 }
237 } else {
238 if (windisplay_cat.is_debug()) {
239 windisplay_cat.debug() << "Calling SetProcessDpiAwareness().\n";
240 }
241 pfnSetProcessDpiAwareness(Process_Per_Monitor_DPI_Aware);
242
243 HDC dc = GetDC(nullptr);
244 if (dc) {
245 int dpi = GetDeviceCaps(dc, LOGPIXELSX);
246 if (dpi > 0) {
247 PN_stdfloat zoom = (double)dpi / 96.0;
248 set_detected_display_zoom(zoom);
249
250 if (windisplay_cat.is_debug()) {
251 windisplay_cat.debug()
252 << "Determined display zoom to be " << zoom
253 << " based on LOGPIXELSX " << dpi << "\n";
254 }
255 }
256 ReleaseDC(nullptr, dc);
257 }
258 }
259 }
260 }
261
262 if (windisplay_cat.is_debug()) {
263 windisplay_cat.debug()
264 << "Detected display devices:\n";
265
266 DISPLAY_DEVICEA device;
267 device.cb = sizeof(device);
268 for (DWORD devnum = 0; EnumDisplayDevicesA(nullptr, devnum, &device, 0); ++devnum) {
269 std::ostream &out = windisplay_cat.debug();
270 out << " " << device.DeviceName << " [" << device.DeviceString << "]";
271 if (device.StateFlags & DISPLAY_DEVICE_ACTIVE) {
272 out << " (active)";
273 }
274 if (device.StateFlags & DISPLAY_DEVICE_MULTI_DRIVER) {
275 out << " (multi-driver)";
276 }
277 if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
278 out << " (primary)";
279 }
280 if (device.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) {
281 out << " (mirroring)";
282 }
283 if (device.StateFlags & DISPLAY_DEVICE_REMOVABLE) {
284 out << " (removable)";
285 }
286 out << "\n";
287 }
288
289 int nmonitor = GetSystemMetrics(SM_CMONITORS);
290 windisplay_cat.debug()
291 << "Detected " << nmonitor << " monitors, "
292 << (GetSystemMetrics(SM_SAMEDISPLAYFORMAT) != 0 ? "" : "NOT ")
293 << "sharing same display format:\n";
294
295 EnumDisplayMonitors(
296 nullptr,
297 nullptr,
298 [](HMONITOR monitor, HDC dc, LPRECT rect, LPARAM param) -> BOOL {
299 MONITORINFOEXA info;
300 info.cbSize = sizeof(info);
301 if (GetMonitorInfoA(monitor, &info)) {
302 std::ostream &out = windisplay_cat.debug() << " ";
303
304 DISPLAY_DEVICEA device;
305 device.cb = sizeof(device);
306 device.StateFlags = 0;
307 if (EnumDisplayDevicesA(info.szDevice, 0, &device, 0)) {
308 out << device.DeviceName << " [" << device.DeviceString << "]";
309 }
310 else {
311 out << info.szDevice << " (device enum failed)";
312 }
313
314 if (info.dwFlags & MONITORINFOF_PRIMARY) {
315 out << " (primary)";
316 }
317 if (info.rcWork.left != 0 || info.rcWork.top != 0) {
318 out << " (at " << info.rcWork.left << "x" << info.rcWork.top << ")";
319 }
320 out << "\n";
321 }
322 return TRUE;
323 },
324 0);
325 }
326
327#ifdef HAVE_DX9
328 // Use D3D to get display info. This is disabled by default as it is slow.
329 if (request_dxdisplay_information) {
330 if (windisplay_cat.is_debug()) {
331 windisplay_cat.debug() << "Using Direct3D 9 to fetch display information.\n";
332 }
333 DisplaySearchParameters display_search_parameters_dx9;
334 int dx9_display_information (DisplaySearchParameters &display_search_parameters_dx9, DisplayInformation *display_information);
335 dx9_display_information(display_search_parameters_dx9, _display_information);
336 } else
337#endif
338 {
339 // Use the Win32 API to query the available display modes.
340 if (windisplay_cat.is_debug()) {
341 windisplay_cat.debug() << "Using EnumDisplaySettings to fetch display information.\n";
342 }
343
344 pvector<DisplayMode> display_modes;
345 DisplayMode current_mode = {0};
346 int current_mode_index = -1;
347 DEVMODE dm{};
348 dm.dmSize = sizeof(dm);
349
350 if (EnumDisplaySettings(nullptr, ENUM_CURRENT_SETTINGS, &dm) != 0) {
351 current_mode.width = dm.dmPelsWidth;
352 current_mode.height = dm.dmPelsHeight;
353 current_mode.bits_per_pixel = dm.dmBitsPerPel;
354 current_mode.refresh_rate = dm.dmDisplayFrequency;
355 current_mode.fullscreen_only = 0;
356 }
357
358 for (int i = 0; EnumDisplaySettings(nullptr, i, &dm) != 0; ++i) {
359 DisplayMode mode;
360 mode.width = dm.dmPelsWidth;
361 mode.height = dm.dmPelsHeight;
362 mode.bits_per_pixel = dm.dmBitsPerPel;
363 mode.refresh_rate = dm.dmDisplayFrequency;
364 mode.fullscreen_only = 0;
365 if (i == 0 || mode != display_modes.back()) {
366 if (current_mode_index < 0 && mode == current_mode) {
367 current_mode_index = (int)display_modes.size();
368 }
369 display_modes.push_back(mode);
370 }
371 }
372
373 // Copy this information to the DisplayInformation object.
374 _display_information->_total_display_modes = display_modes.size();
375 if (!display_modes.empty()) {
376 _display_information->_current_display_mode_index = current_mode_index;
377 _display_information->_display_mode_array = new DisplayMode[display_modes.size()];
378 std::copy(display_modes.begin(), display_modes.end(),
379 _display_information->_display_mode_array);
380 }
381 }
382
383 if (auto_cpu_data) {
385 }
386
387 OSVERSIONINFO version_info;
388
389 version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
390 if (GetVersionEx(&version_info)) {
391 if (windisplay_cat.is_info()) {
392 sprintf(string, "OS version: %lu.%lu.%lu.%lu\n", version_info.dwMajorVersion, version_info.dwMinorVersion, version_info.dwPlatformId, version_info.dwBuildNumber);
393 windisplay_cat.info() << string;
394 windisplay_cat.info() << " " << version_info.szCSDVersion << "\n";
395 }
396
397 _display_information->_os_version_major = version_info.dwMajorVersion;
398 _display_information->_os_version_minor = version_info.dwMinorVersion;
399 _display_information->_os_version_build = version_info.dwBuildNumber;
400 _display_information->_os_platform_id = version_info.dwPlatformId;
401 }
402 // Screen size
403 _display_width = GetSystemMetrics(SM_CXSCREEN);
404 _display_height = GetSystemMetrics(SM_CYSCREEN);
405
406 HMODULE power_dll;
407
408 power_dll = LoadLibrary("PowrProf.dll");
409 if (power_dll) {
410 CallNtPowerInformationFunction = (CallNtPowerInformationType) GetProcAddress(power_dll, "CallNtPowerInformation");
411 if (CallNtPowerInformationFunction) {
412
413 _display_information->_update_cpu_frequency_function = update_cpu_frequency_function;
414 update_cpu_frequency_function(0, _display_information);
415
416 sprintf(string, "max Mhz %I64d, current Mhz %I64d\n", _display_information->_maximum_cpu_frequency, _display_information->_current_cpu_frequency);
417
418 windisplay_cat.info() << string;
419 }
420 }
421}
422
423/**
424 * Looks up the detailed CPU information and stores it in
425 * _display_information, if supported by the OS. This may take a second or
426 * two.
427 */
430 char string [512];
431
432 // set callback for memory function
433 _display_information->_get_memory_information_function = get_memory_information;
434
435 // determine CPU frequency
436 uint64_t time;
437 uint64_t end_time;
438 LARGE_INTEGER counter;
439 LARGE_INTEGER end;
440 LARGE_INTEGER frequency;
441
442 time = 0;
443 end_time = 0;
444 counter.QuadPart = 0;
445 end.QuadPart = 0;
446 frequency.QuadPart = 0;
447
448 int priority;
449 HANDLE thread;
450
451 windisplay_cat.info() << "begin QueryPerformanceFrequency\n";
452 thread = GetCurrentThread();
453 priority = GetThreadPriority (thread);
454 SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL);
455
456 if (QueryPerformanceFrequency(&frequency)) {
457 if (frequency.QuadPart > 0) {
458 if (QueryPerformanceCounter (&counter)) {
459 time = __rdtsc();
460 end.QuadPart = counter.QuadPart + frequency.QuadPart;
461 while (QueryPerformanceCounter (&counter) && counter.QuadPart < end.QuadPart) {
462
463 }
464 end_time = __rdtsc();
465
466 _display_information->_cpu_frequency = end_time - time;
467 }
468 }
469 }
470
471 SetThreadPriority(thread, priority);
472 sprintf(string, "QueryPerformanceFrequency: %I64d\n", frequency.QuadPart);
473 windisplay_cat.info() << string;
474 sprintf(string, "CPU frequency: %I64d\n", _display_information->_cpu_frequency);
475 windisplay_cat.info() << string;
476
477 // Number of CPU's
478 count_number_of_cpus(_display_information);
479}
480
481/**
482 *
483 */
484WinGraphicsPipe::
485~WinGraphicsPipe() {
486}
This class contains various display information.
Parameters used for searching display capabilities.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
virtual void lookup_cpu_data()
Looks up the detailed CPU information and stores it in _display_information, if supported by the OS.
This is our own Panda specialization on the default STL vector.
Definition pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int count_bits_in_word(unsigned short x)
Returns the number of 1 bits in the indicated word.
Definition pbitops.I:18
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.