-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMXAnimation.SystemInfo.pas
More file actions
375 lines (334 loc) · 8.36 KB
/
Copy pathFMXAnimation.SystemInfo.pas
File metadata and controls
375 lines (334 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/// <summary>
/// FMXAnimation.SystemInfo
/// Host/runtime diagnostics: platform, Skia/FMX canvas backend, process CPU.
/// </summary>
///
/// <remarks>
/// Data only — no UI. The stats HUD (FMXAnimation.Stats.Hud) formats and draws
/// these values. Call GetActiveRenderBackendLabel after Application.Initialize
/// and GlobalUseSkia setup.
///
/// Architecture conditionals check iOS before macOS: Delphi defines MACOS on
/// both, so MACOS-first would mislabel iOS devices as macOS.
/// </remarks>
///
/// <copyright>
/// Copyright © 2026 Olaf Monien
/// Licensed under MIT
/// </copyright>
unit FMXAnimation.SystemInfo;
interface
/// <summary>
/// Human-readable host platform (OS + CPU architecture of this binary).
/// Omits build number when TOSVersion.Build is 0 (common on iOS/macOS).
/// </summary>
function GetHostPlatformLabel: string;
/// <summary>
/// Human-readable active rendering backend (Skia Raster/GL/Vulkan/Metal or FMX canvas).
/// Call after Application.Initialize / GlobalUseSkia is applied.
/// </summary>
function GetActiveRenderBackendLabel: string;
/// <summary>
/// FMX display-link preferred frame rate (GlobalPreferredFramesPerSecond).
/// This is a request to the platform, not measured panel Hz.
/// </summary>
function GetPreferredFramesPerSecond: Integer;
/// <summary>
/// Sets GlobalPreferredFramesPerSecond. Caller should restart the game loop
/// (StopLoop/StartLoop) so Mac/iOS CADisplayLink re-applies the range.
/// </summary>
procedure SetPreferredFramesPerSecond(const AValue: Integer);
/// <summary>Number of logical processors visible to this process.</summary>
function GetLogicalProcessorCount: Integer;
/// <summary>
/// Stateful process CPU sampler. First Sample primes and returns 0.
/// </summary>
type
TProcessCpuSampler = record
private
FHasPrior: Boolean;
FLastWallSeconds: Double;
FLastProcessSeconds: Double;
FLastPercentOfOneCore: Double;
function ReadProcessCpuSeconds: Double;
function ReadWallSeconds: Double;
public
procedure Reset;
/// <summary>
/// CPU time used by this process since last Sample, as percent of one logical CPU.
/// 100 = one core fully busy for the interval. Multi-threaded can exceed 100.
/// </summary>
function Sample: Double;
property LastPercentOfOneCore: Double read FLastPercentOfOneCore;
end;
implementation
uses
System.SysUtils,
System.Types,
System.Diagnostics,
FMX.Types,
FMX.Graphics,
FMX.Skia,
FMX.Skia.Canvas,
{$IF Defined(MSWINDOWS)}
Winapi.Windows
{$ELSEIF Defined(POSIX)}
Posix.Base,
Posix.SysTypes,
Posix.SysTimes,
Posix.Unistd
{$ENDIF}
;
var
GWallClock: TStopwatch;
{ Platform }
function GetArchitectureLabel: string;
begin
// iOS before macOS: on iOS targets Delphi also defines MACOS.
{$IF Defined(IOS64)}
Result := 'iOS ARM64';
{$ELSEIF Defined(IOS32)}
Result := 'iOS ARM';
{$ELSEIF Defined(IOS) and Defined(CPUARM64)}
Result := 'iOS ARM64';
{$ELSEIF Defined(IOS) and Defined(CPUARM)}
Result := 'iOS ARM';
{$ELSEIF Defined(IOS)}
Result := 'iOS';
{$ELSEIF Defined(WIN64)}
Result := 'Win64';
{$ELSEIF Defined(WIN32)}
Result := 'Win32';
{$ELSEIF Defined(MACOS) and Defined(CPUARM64)}
Result := 'macOS ARM64';
{$ELSEIF Defined(MACOS) and Defined(CPUX64)}
Result := 'macOS x64';
{$ELSEIF Defined(MACOS64) or Defined(OSX64)}
Result := 'macOS64';
{$ELSEIF Defined(LINUX64)}
Result := 'Linux64';
{$ELSEIF Defined(ANDROID64)}
Result := 'Android64';
{$ELSEIF Defined(ANDROID32)}
Result := 'Android32';
{$ELSEIF Defined(ANDROID)}
Result := 'Android';
{$ELSE}
Result := 'UnknownArch';
{$ENDIF}
end;
function GetHostPlatformLabel: string;
var
LOs: string;
begin
// TOSVersion.Build is reliable on Windows; often 0 on iOS/macOS — omit then.
LOs := Format('%s %d.%d', [TOSVersion.Name, TOSVersion.Major, TOSVersion.Minor]);
if TOSVersion.Build > 0 then
begin
LOs := LOs + Format(' (build %d)', [TOSVersion.Build]);
end;
Result := Format('%s - %s', [GetArchitectureLabel, LOs]);
end;
{ Skia / FMX canvas backend }
function FriendlyCanvasClassName(const AClass: TClass): string;
var
LName: string;
begin
if AClass = nil then
begin
Exit('(none)');
end;
LName := AClass.ClassName;
if LName.Contains('Raster') then
begin
Exit('Skia Raster (CPU)');
end;
if LName.Contains('Gl') or LName.Contains('GL') then
begin
Exit('Skia OpenGL (GPU)');
end;
if LName.Contains('Vk') or LName.Contains('Vulkan') then
begin
Exit('Skia Vulkan (GPU)');
end;
if LName.Contains('Mtl') or LName.Contains('Metal') then
begin
Exit('Skia Metal (GPU)');
end;
if GlobalUseSkia then
begin
Result := Format('Skia (%s)', [LName]);
end
else
begin
Result := Format('FMX (%s)', [LName]);
end;
end;
function GetActiveRenderBackendLabel: string;
var
LSkiaClass: TSkCanvasBaseClass;
LCanvasClass: TCanvasClass;
LFlags: string;
begin
LFlags := '';
if GlobalUseSkia then
begin
LFlags := LFlags + ' Skia=on';
end;
{$IF Defined(MACOS) or Defined(IOS)}
if GlobalUseMetal then
begin
LFlags := LFlags + ' Metal=on';
end
else
begin
LFlags := LFlags + ' Metal=off';
end;
{$ENDIF}
if GlobalUseSkiaRasterWhenAvailable then
begin
LFlags := LFlags + ' PreferRaster';
end;
if GlobalUseSkia then
begin
LSkiaClass := DefaultSkiaRenderCanvasClass;
if LSkiaClass <> nil then
begin
Exit(FriendlyCanvasClassName(LSkiaClass) + LFlags);
end;
end;
LCanvasClass := TCanvasManager.DefaultCanvas;
if LCanvasClass <> nil then
begin
Exit(FriendlyCanvasClassName(LCanvasClass) + LFlags);
end;
if GlobalUseSkia then
begin
Result := 'Skia (pending registration)' + LFlags;
end
else
begin
Result := 'FMX (default)' + LFlags;
end;
end;
{ Preferred frames per second }
function GetPreferredFramesPerSecond: Integer;
begin
Result := GlobalPreferredFramesPerSecond;
if Result < 1 then
begin
Result := 60;
end;
end;
procedure SetPreferredFramesPerSecond(const AValue: Integer);
begin
if AValue < 1 then
begin
GlobalPreferredFramesPerSecond := 60;
end
else
begin
GlobalPreferredFramesPerSecond := AValue;
end;
end;
{ CPU }
function GetLogicalProcessorCount: Integer;
begin
Result := CPUCount;
if Result < 1 then
begin
Result := 1;
end;
end;
function TProcessCpuSampler.ReadWallSeconds: Double;
begin
if not GWallClock.IsRunning then
begin
GWallClock := TStopwatch.StartNew;
end;
Result := GWallClock.Elapsed.TotalSeconds;
end;
function TProcessCpuSampler.ReadProcessCpuSeconds: Double;
{$IF Defined(MSWINDOWS)}
var
LCreation, LExit, LKernel, LUser: TFileTime;
LKernel64, LUser64: UInt64;
begin
if not GetProcessTimes(GetCurrentProcess, LCreation, LExit, LKernel, LUser) then
begin
Exit(0);
end;
LKernel64 := UInt64(LKernel.dwLowDateTime) or (UInt64(LKernel.dwHighDateTime) shl 32);
LUser64 := UInt64(LUser.dwLowDateTime) or (UInt64(LUser.dwHighDateTime) shl 32);
Result := (LKernel64 + LUser64) / 1.0E7;
end;
{$ELSEIF Defined(POSIX)}
var
LTms: tms;
LTicksPerSec: LongInt;
LTicks: clock_t;
begin
FillChar(LTms, SizeOf(LTms), 0);
LTicks := times(LTms);
if LTicks = clock_t(-1) then
begin
Exit(0);
end;
LTicksPerSec := sysconf(_SC_CLK_TCK);
if LTicksPerSec <= 0 then
begin
LTicksPerSec := 100;
end;
Result := (Double(LTms.tms_utime) + Double(LTms.tms_stime)) / Double(LTicksPerSec);
end;
{$ELSE}
begin
Result := 0;
end;
{$ENDIF}
procedure TProcessCpuSampler.Reset;
begin
FHasPrior := False;
FLastWallSeconds := 0;
FLastProcessSeconds := 0;
FLastPercentOfOneCore := 0;
if not GWallClock.IsRunning then
begin
GWallClock := TStopwatch.StartNew;
end;
end;
function TProcessCpuSampler.Sample: Double;
var
LWall: Double;
LProc: Double;
LWallDelta: Double;
LProcDelta: Double;
begin
LWall := ReadWallSeconds;
LProc := ReadProcessCpuSeconds;
if not FHasPrior then
begin
FHasPrior := True;
FLastWallSeconds := LWall;
FLastProcessSeconds := LProc;
FLastPercentOfOneCore := 0;
Exit(0);
end;
LWallDelta := LWall - FLastWallSeconds;
LProcDelta := LProc - FLastProcessSeconds;
FLastWallSeconds := LWall;
FLastProcessSeconds := LProc;
if LWallDelta <= 1.0E-6 then
begin
Exit(FLastPercentOfOneCore);
end;
if LProcDelta < 0 then
begin
LProcDelta := 0;
end;
Result := 100.0 * (LProcDelta / LWallDelta);
FLastPercentOfOneCore := Result;
end;
initialization
GWallClock := TStopwatch.StartNew;
end.