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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// CPU_PROFILE.CPP
//
// Cpu Profiling Display
//=====================================================================================//
#include "vxconsole.h"
#define PROFILE_MAXCOUNTERS 64
#define PROFILE_MAXSAMPLES 512
#define PROFILE_HISTORY_TIMINGHEIGHT 100
#define PROFILE_HISTORY_NUMMINORTICKS 3
#define PROFILE_HISTORY_LABELWIDTH 50
#define PROFILE_HISTORY_SCALESTEPS 5
#define PROFILE_HISTORY_MINSCALE 0.3f
#define PROFILE_HISTORY_MAXSCALE 3.0f
#define PROFILE_SAMPLES_ITEMHEIGHT 15
#define PROFILE_SAMPLES_BARHEIGHT 10
#define PROFILE_SAMPLES_TIMINGWIDTH 200
#define PROFILE_SAMPLES_LABELWIDTH 150
#define PROFILE_SAMPLES_LABELGAP 5
#define PROFILE_SAMPLES_NUMMINORTICKS 3
#define PROFILE_SAMPLES_PEAKHOLDTIME 3000
#define PROFILE_SAMPLES_SCALESTEPS 10
#define PROFILE_SAMPLES_MINSCALE 0.3f
#define PROFILE_SAMPLES_MAXSCALE 3.0f
#define ID_CPUPROFILE_SAMPLES 1
#define ID_CPUPROFILE_HISTORY 2
typedef struct
{
unsigned int samples[PROFILE_MAXSAMPLES];
unsigned int peakSample;
char label[64];
COLORREF color;
} profileCounter_t;
HWND g_cpuProfile_hWndSamples;
HWND g_cpuProfile_hWndHistory;
int g_cpuProfile_numCounters;
profileCounter_t g_cpuProfile_counters[PROFILE_MAXCOUNTERS];
RECT g_cpuProfile_samplesWindowRect;
RECT g_cpuProfile_historyWindowRect;
DWORD g_cpuProfile_lastPeakTime;
bool g_cpuProfile_history_tickMarks = true;
bool g_cpuProfile_history_colors = true;
int g_cpuProfile_history_scale;
bool g_cpuProfile_samples_tickMarks = true;
bool g_cpuProfile_samples_colors = true;
int g_cpuProfile_samples_scale;
int g_cpuProfile_numSamples;
int g_cpuProfile_fpsLabels;
//-----------------------------------------------------------------------------
// CpuProfile_SaveConfig
//
//-----------------------------------------------------------------------------
void CpuProfile_SaveConfig()
{
char buff[256];
WINDOWPLACEMENT wp;
// profile samples
if ( g_cpuProfile_hWndSamples )
{
memset( &wp, 0, sizeof( wp ) );
wp.length = sizeof( WINDOWPLACEMENT );
GetWindowPlacement( g_cpuProfile_hWndSamples, &wp );
g_cpuProfile_samplesWindowRect = wp.rcNormalPosition;
sprintf( buff, "%d %d %d %d", wp.rcNormalPosition.left, wp.rcNormalPosition.top, wp.rcNormalPosition.right, wp.rcNormalPosition.bottom );
Sys_SetRegistryString( "profileSamplesWindowRect", buff );
}
Sys_SetRegistryInteger( "profileSamplesScale", g_cpuProfile_samples_scale );
// profile history
if ( g_cpuProfile_hWndHistory )
{
memset( &wp, 0, sizeof( wp ) );
wp.length = sizeof( WINDOWPLACEMENT );
GetWindowPlacement( g_cpuProfile_hWndHistory, &wp );
g_cpuProfile_historyWindowRect = wp.rcNormalPosition;
sprintf( buff, "%d %d %d %d", wp.rcNormalPosition.left, wp.rcNormalPosition.top, wp.rcNormalPosition.right, wp.rcNormalPosition.bottom );
Sys_SetRegistryString( "profileHistoryWindowRect", buff );
}
Sys_SetRegistryInteger( "profileHistoryScale", g_cpuProfile_history_scale );
Sys_SetRegistryInteger( "cpuProfileFpsLabels", g_cpuProfile_fpsLabels );
}
//-----------------------------------------------------------------------------
// CpuProfile_LoadConfig
//
//-----------------------------------------------------------------------------
void CpuProfile_LoadConfig()
{
int numArgs;
char buff[256];
// profile samples
Sys_GetRegistryString( "profileSamplesWindowRect", buff, "", sizeof( buff ) );
numArgs = sscanf( buff, "%d %d %d %d", &g_cpuProfile_samplesWindowRect.left, &g_cpuProfile_samplesWindowRect.top, &g_cpuProfile_samplesWindowRect.right, &g_cpuProfile_samplesWindowRect.bottom );
if ( numArgs != 4 )
memset( &g_cpuProfile_samplesWindowRect, 0, sizeof( g_cpuProfile_samplesWindowRect ) );
Sys_GetRegistryInteger( "profileSamplesScale", 0, g_cpuProfile_samples_scale );
if ( g_cpuProfile_samples_scale < -PROFILE_SAMPLES_SCALESTEPS || g_cpuProfile_samples_scale > PROFILE_SAMPLES_SCALESTEPS )
g_cpuProfile_samples_scale = 0;
// profile history
Sys_GetRegistryString( "profileHistoryWindowRect", buff, "", sizeof( buff ) );
numArgs = sscanf( buff, "%d %d %d %d", &g_cpuProfile_historyWindowRect.left, &g_cpuProfile_historyWindowRect.top, &g_cpuProfile_historyWindowRect.right, &g_cpuProfile_historyWindowRect.bottom );
if ( numArgs != 4 )
memset( &g_cpuProfile_historyWindowRect, 0, sizeof( g_cpuProfile_historyWindowRect ) );
Sys_GetRegistryInteger( "profileHistoryScale", 0, g_cpuProfile_history_scale );
if ( g_cpuProfile_history_scale < -PROFILE_HISTORY_SCALESTEPS || g_cpuProfile_history_scale > PROFILE_HISTORY_SCALESTEPS )
g_cpuProfile_history_scale = 0;
Sys_GetRegistryInteger( "cpuProfileFpsLabels", 0, g_cpuProfile_fpsLabels );
}
//-----------------------------------------------------------------------------
// CpuProfile_SetTitle
//
//-----------------------------------------------------------------------------
void CpuProfile_SetTitle()
{
char titleBuff[128];
if ( g_cpuProfile_hWndSamples )
{
strcpy( titleBuff, "CPU Usage Snapshot" );
if ( VProf_GetState() == VPROF_CPU )
strcat( titleBuff, " [ON]" );
SetWindowText( g_cpuProfile_hWndSamples, titleBuff );
}
if ( g_cpuProfile_hWndHistory )
{
strcpy( titleBuff, "CPU Usage History" );
if ( VProf_GetState() == VPROF_CPU )
strcat( titleBuff, " [ON]" );
SetWindowText( g_cpuProfile_hWndHistory, titleBuff );
}
}
//-----------------------------------------------------------------------------
// CpuProfile_UpdateWindow
//
//-----------------------------------------------------------------------------
void CpuProfile_UpdateWindow()
{
if ( g_cpuProfile_hWndSamples && !IsIconic( g_cpuProfile_hWndSamples ) )
{
// visible - force a client repaint
InvalidateRect( g_cpuProfile_hWndSamples, NULL, true );
}
if ( g_cpuProfile_hWndHistory && !IsIconic( g_cpuProfile_hWndHistory ) )
{
// visible - force a client repaint
InvalidateRect( g_cpuProfile_hWndHistory, NULL, true );
}
}
//-----------------------------------------------------------------------------
// rc_SetCpuProfile
//
//-----------------------------------------------------------------------------
int rc_SetCpuProfile( char* commandPtr )
{
int i;
char* cmdToken;
int retAddr;
int errCode = -1;
xrProfile_t* localList;
int profileList;
int numProfiles;
int retVal;
// get numProfiles
cmdToken = GetToken( &commandPtr );
if ( !cmdToken[0] )
goto cleanUp;
sscanf( cmdToken,"%x",&numProfiles );
// get profile attributes
cmdToken = GetToken( &commandPtr );
if ( !cmdToken[0] )
goto cleanUp;
sscanf( cmdToken, "%x", &profileList );
// get retAddr
cmdToken = GetToken( &commandPtr );
if ( !cmdToken[0] )
goto cleanUp;
sscanf( cmdToken,"%x",&retAddr );
localList = new xrProfile_t[numProfiles];
memset( localList, 0, numProfiles*sizeof( xrProfile_t ) );
// get the caller's profile list
DmGetMemory( ( void* )profileList, numProfiles*sizeof( xrProfile_t ), localList, NULL );
g_cpuProfile_numCounters = numProfiles;
if ( g_cpuProfile_numCounters > PROFILE_MAXCOUNTERS-1 )
g_cpuProfile_numCounters = PROFILE_MAXCOUNTERS-1;
for ( i=0; i<g_cpuProfile_numCounters; i++ )
{
// swap the structure
localList[i].color = BigDWord( localList[i].color );
// clear the old counter
memset( &g_cpuProfile_counters[i], 0, sizeof( profileCounter_t ) );
V_strncpy( g_cpuProfile_counters[i].label, localList[i].labelString, sizeof( g_cpuProfile_counters[i].label ) );
g_cpuProfile_counters[i].color = localList[i].color;
}
// build out the reserved last counter as total count
memset( &g_cpuProfile_counters[g_cpuProfile_numCounters], 0, sizeof( profileCounter_t ) );
strcpy( g_cpuProfile_counters[g_cpuProfile_numCounters].label, "Total" );
g_cpuProfile_counters[i].color = RGB( 255,255,255 );
g_cpuProfile_numCounters++;
// set the return code
retVal = g_cpuProfile_numCounters-1;
int xboxRetVal = BigDWord( retVal );
DmSetMemory( ( void* )retAddr, sizeof( int ), &xboxRetVal, NULL );
DebugCommand( "0x%8.8x = SetCpuProfile( 0x%8.8x, 0x%8.8x )\n", retVal, numProfiles, profileList );
delete [] localList;
// success
errCode = 0;
cleanUp:
return ( errCode );
}
//-----------------------------------------------------------------------------
// rc_SetCpuProfileData
//
//-----------------------------------------------------------------------------
int rc_SetCpuProfileData( char* commandPtr )
{
int i;
int total;
char* cmdToken;
int errCode = -1;
int counters;
int currentSample;
bool newPeaks;
unsigned int localCounters[PROFILE_MAXCOUNTERS];
DWORD newTime;
// get profiles
cmdToken = GetToken( &commandPtr );
if ( !cmdToken[0] )
{
goto cleanUp;
}
sscanf( cmdToken, "%x", &counters );
// get the caller's profile list
if ( g_cpuProfile_numCounters )
{
DmGetMemory( ( void* )counters, ( g_cpuProfile_numCounters-1 )*sizeof( int ), localCounters, NULL );
}
// timeout peaks
newTime = Sys_GetSystemTime();
if ( newTime - g_cpuProfile_lastPeakTime > PROFILE_SAMPLES_PEAKHOLDTIME )
{
g_cpuProfile_lastPeakTime = newTime;
newPeaks = true;
}
else
{
newPeaks = false;
}
// next sample
currentSample = g_cpuProfile_numSamples % PROFILE_MAXSAMPLES;
g_cpuProfile_numSamples++;
total = 0;
for ( i=0; i<g_cpuProfile_numCounters; i++ )
{
// swap
localCounters[i] = BigDWord( localCounters[i] );
if ( i != g_cpuProfile_numCounters-1 )
{
g_cpuProfile_counters[i].samples[currentSample] = localCounters[i];
total += localCounters[i];
}
else
{
// reserved total counter
g_cpuProfile_counters[i].samples[currentSample] = total;
}
if ( newPeaks || g_cpuProfile_counters[i].peakSample < g_cpuProfile_counters[i].samples[currentSample] )
{
g_cpuProfile_counters[i].peakSample = g_cpuProfile_counters[i].samples[currentSample];
}
}
DebugCommand( "SetCpuProfileData( 0x%8.8x )\n", counters );
CpuProfile_UpdateWindow();
// success
errCode = 0;
cleanUp:
return ( errCode );
}
//-----------------------------------------------------------------------------
// CpuProfile_ZoomIn
//
//-----------------------------------------------------------------------------
void CpuProfile_ZoomIn( int& scale, int numSteps )
{
scale++;
if ( scale > numSteps )
{
scale = numSteps;
return;
}
CpuProfile_UpdateWindow();
}
//-----------------------------------------------------------------------------
// CpuProfile_ZoomOut
//
//-----------------------------------------------------------------------------
void CpuProfile_ZoomOut( int& scale, int numSteps )
{
scale--;
if ( scale < -numSteps )
{
scale = -numSteps;
return;
}
CpuProfile_UpdateWindow();
}
//-----------------------------------------------------------------------------
// CpuProfile_CalcScale
//
//-----------------------------------------------------------------------------
float CpuProfile_CalcScale( int scale, int numSteps, float min, float max )
{
float t;
// from integral scale [-numSteps..numSteps] to float scale [min..max]
t = ( float )( scale + numSteps )/( float )( 2*numSteps );
t = min + t*( max-min );
return t;
}
//-----------------------------------------------------------------------------
// ProfileSamples_Draw
//
//-----------------------------------------------------------------------------
void ProfileSamples_Draw( HDC hdc, RECT* clientRect )
{
int i;
int j;
int x;
int y;
int x0;
int y0;
int w;
float t;
float scale;
float sampleTime;
char labelBuff[128];
HPEN hBlackPen;
HPEN hPenOld;
HPEN hGreyPen;
HBRUSH hColoredBrush;
HBRUSH hbrushOld;
HFONT hFontOld;
RECT rect;
int currentSample;
int numTicks;
int timingWidth;
int windowWidth;
int windowHeight;
hBlackPen = CreatePen( PS_SOLID, 1, RGB( 0,0,0 ) );
hGreyPen = CreatePen( PS_SOLID, 1, Sys_ColorScale( g_backgroundColor, 0.85f ) );
hPenOld = ( HPEN )SelectObject( hdc, hBlackPen );
hFontOld = SelectFont( hdc, g_hProportionalFont );
SetBkColor( hdc, g_backgroundColor );
// zoom
scale = CpuProfile_CalcScale( g_cpuProfile_samples_scale, PROFILE_SAMPLES_SCALESTEPS, PROFILE_SAMPLES_MINSCALE, PROFILE_SAMPLES_MAXSCALE );
timingWidth = ( int )( PROFILE_SAMPLES_TIMINGWIDTH*scale );
windowWidth = clientRect->right-clientRect->left;
windowHeight = clientRect->bottom-clientRect->top;
numTicks = ( windowWidth-PROFILE_SAMPLES_LABELWIDTH )/timingWidth + 1;
if ( numTicks < 0 )
numTicks = 1;
rect.left = 0;
rect.right = PROFILE_SAMPLES_LABELWIDTH;
rect.top = 0;
rect.bottom = PROFILE_SAMPLES_ITEMHEIGHT;
DrawText( hdc, "Name", -1, &rect, DT_LEFT );
// draw timing ticks
x = PROFILE_SAMPLES_LABELWIDTH;
y = 0;
for ( i=0; i<numTicks; i++ )
{
// tick labels
rect.left = x-40;
rect.right = x+40;
rect.top = y;
rect.bottom = y+PROFILE_SAMPLES_ITEMHEIGHT;
if ( !g_cpuProfile_fpsLabels )
sprintf( labelBuff, "%.2fms", i*( 1000.0f/60.0f ) );
else
sprintf( labelBuff, "%.2ffps", i == 0 ? 0 : 60.0f/i );
DrawText( hdc, labelBuff, -1, &rect, DT_CENTER );
// major ticks
x0 = x;
y0 = y + PROFILE_SAMPLES_ITEMHEIGHT;
SelectObject( hdc, hBlackPen );
MoveToEx( hdc, x0, y0, NULL );
LineTo( hdc, x0, y0+windowHeight );
if ( g_cpuProfile_samples_tickMarks && g_cpuProfile_samples_scale > -PROFILE_SAMPLES_SCALESTEPS )
{
// minor ticks
x0 = x;
y0 = y + PROFILE_SAMPLES_ITEMHEIGHT;
SelectObject( hdc, hGreyPen );
for ( j=0; j<PROFILE_SAMPLES_NUMMINORTICKS; j++ )
{
x0 += timingWidth/( PROFILE_SAMPLES_NUMMINORTICKS+1 );
MoveToEx( hdc, x0, y0, NULL );
LineTo( hdc, x0, y0+windowHeight );
}
}
x += timingWidth;
}
// seperator
SelectObject( hdc, hBlackPen );
MoveToEx( hdc, 0, PROFILE_SAMPLES_ITEMHEIGHT, NULL );
LineTo( hdc, windowWidth, PROFILE_SAMPLES_ITEMHEIGHT );
// draw labels
x = 0;
y = PROFILE_SAMPLES_ITEMHEIGHT;
for ( i=0; i<g_cpuProfile_numCounters; i++ )
{
if ( !g_cpuProfile_counters[i].label )
continue;
rect.left = x;
rect.right = x+PROFILE_SAMPLES_LABELWIDTH-PROFILE_SAMPLES_LABELGAP;
rect.top = y;
rect.bottom = y+PROFILE_SAMPLES_ITEMHEIGHT;
DrawText( hdc, g_cpuProfile_counters[i].label, -1, &rect, DT_VCENTER|DT_RIGHT|DT_SINGLELINE|DT_END_ELLIPSIS|DT_MODIFYSTRING );
// draw the under line
MoveToEx( hdc, x, y+PROFILE_SAMPLES_ITEMHEIGHT, NULL );
LineTo( hdc, x+PROFILE_SAMPLES_LABELWIDTH, y+PROFILE_SAMPLES_ITEMHEIGHT );
y += PROFILE_SAMPLES_ITEMHEIGHT;
}
// draw bars
SelectObject( hdc, hBlackPen );
x = PROFILE_SAMPLES_LABELWIDTH;
y = PROFILE_SAMPLES_ITEMHEIGHT;
currentSample = g_cpuProfile_numSamples-1;
if ( currentSample < 0 )
currentSample = 0;
else
currentSample %= PROFILE_MAXSAMPLES;
for ( i=0; i<g_cpuProfile_numCounters; i++ )
{
if ( !g_cpuProfile_counters[i].label )
continue;
hColoredBrush = CreateSolidBrush( g_cpuProfile_samples_colors ? g_cpuProfile_counters[i].color : g_backgroundColor );
hbrushOld = ( HBRUSH )SelectObject( hdc, hColoredBrush );
// bar - count is in us i.e. 1 major tick = 16667us
t = ( float )g_cpuProfile_counters[i].samples[currentSample]/( 1000000.0f/60.0f );
w = ( int )( t * ( float )timingWidth );
if ( w > windowWidth )
w = windowWidth;
x0 = x;
y0 = y + ( PROFILE_SAMPLES_ITEMHEIGHT-PROFILE_SAMPLES_BARHEIGHT )/2 + 1;
Rectangle( hdc, x0, y0, x0 + w, y0 + PROFILE_SAMPLES_BARHEIGHT );
// peak
t = ( float )g_cpuProfile_counters[i].peakSample/( 1000000.0f/60.0f );
w = ( int )( t * ( float )timingWidth );
if ( w > windowWidth )
w = windowWidth;
x0 = x + w;
y0 = y + PROFILE_SAMPLES_ITEMHEIGHT/2 + 1;
POINT points[4];
points[0].x = x0;
points[0].y = y0-4;
points[1].x = x0+4;
points[1].y = y0;
points[2].x = x0;
points[2].y = y0+4;
points[3].x = x0-4;
points[3].y = y0;
Polygon( hdc, points, 4 );
SelectObject( hdc, hbrushOld );
DeleteObject( hColoredBrush );
// draw peak times
sampleTime = ( float )g_cpuProfile_counters[i].peakSample/1000.0f;
if ( sampleTime >= 0.01F )
{
sprintf( labelBuff, "%.2f", sampleTime );
rect.left = x0 + 8;
rect.right = x0 + 8 + 100;
rect.top = y;
rect.bottom = y + PROFILE_SAMPLES_ITEMHEIGHT;
DrawText( hdc, labelBuff, -1, &rect, DT_VCENTER|DT_LEFT|DT_SINGLELINE );
}
y += PROFILE_SAMPLES_ITEMHEIGHT;
}
SelectObject( hdc, hFontOld );
SelectObject( hdc, hPenOld );
DeleteObject( hBlackPen );
DeleteObject( hGreyPen );
}
//-----------------------------------------------------------------------------
// ProfileHistory_Draw
//
//-----------------------------------------------------------------------------
void ProfileHistory_Draw( HDC hdc, RECT* clientRect )
{
char labelBuff[128];
HPEN hBlackPen;
HPEN hPenOld;
HPEN hNullPen;
HPEN hGreyPen;
HBRUSH hColoredBrush;
HBRUSH hBrushOld;
HFONT hFontOld;
int currentSample;
int numTicks;
int timingHeight;
int windowWidth;
int windowHeight;
int x;
int y;
int y0;
int i;
int j;
int h;
int numbars;
RECT rect;
float t;
float scale;
hBlackPen = CreatePen( PS_SOLID, 1, RGB( 0,0,0 ) );
hGreyPen = CreatePen( PS_SOLID, 1, Sys_ColorScale( g_backgroundColor, 0.85f ) );
hNullPen = CreatePen( PS_NULL, 0, RGB( 0,0,0 ) );
hPenOld = ( HPEN )SelectObject( hdc, hBlackPen );
hFontOld = SelectFont( hdc, g_hProportionalFont );
// zoom
scale = CpuProfile_CalcScale( g_cpuProfile_history_scale, PROFILE_HISTORY_SCALESTEPS, PROFILE_HISTORY_MINSCALE, PROFILE_HISTORY_MAXSCALE );
timingHeight = ( int )( PROFILE_HISTORY_TIMINGHEIGHT*scale );
windowWidth = clientRect->right-clientRect->left;
windowHeight = clientRect->bottom-clientRect->top;
numTicks = windowHeight/timingHeight + 2;
if ( numTicks < 0 )
numTicks = 1;
SetBkColor( hdc, g_backgroundColor );
x = 0;
y = windowHeight;
for ( i=0; i<numTicks; i++ )
{
// major ticks
SelectObject( hdc, hBlackPen );
MoveToEx( hdc, 0, y, NULL );
LineTo( hdc, windowWidth, y );
if ( g_cpuProfile_history_tickMarks && g_cpuProfile_history_scale > -PROFILE_HISTORY_SCALESTEPS )
{
// minor ticks
y0 = y;
SelectObject( hdc, hGreyPen );
for ( j=0; j<PROFILE_HISTORY_NUMMINORTICKS; j++ )
{
y0 += timingHeight/( PROFILE_SAMPLES_NUMMINORTICKS+1 );
MoveToEx( hdc, 0, y0, NULL );
LineTo( hdc, windowWidth, y0 );
}
}
// tick labels
if ( i )
{
rect.left = windowWidth-50;
rect.right = windowWidth;
rect.top = y-20;
rect.bottom = y;
if ( !g_cpuProfile_fpsLabels )
sprintf( labelBuff, "%.2fms", i*( 1000.0f/60.0f ) );
else
sprintf( labelBuff, "%.2ffps", 60.0f/i );
DrawText( hdc, labelBuff, -1, &rect, DT_RIGHT|DT_SINGLELINE|DT_BOTTOM );
}
y -= timingHeight;
}
// vertical bars
if ( g_cpuProfile_numSamples )
{
SelectObject( hdc, hNullPen );
numbars = windowWidth-PROFILE_HISTORY_LABELWIDTH;
currentSample = g_cpuProfile_numSamples-1;
for ( x=numbars-1; x>=0; x-=4 )
{
// all the counters at this sample
y = windowHeight;
for ( j=0; j<g_cpuProfile_numCounters-1; j++ )
{
if ( !g_cpuProfile_counters[j].label )
continue;
t = ( float )g_cpuProfile_counters[j].samples[currentSample % PROFILE_MAXSAMPLES]/( 1000000.0f/60.0f );
h = ( int )( t * ( float )timingHeight );
if ( h )
{
if ( h > windowHeight )
h = windowHeight;
hColoredBrush = CreateSolidBrush( g_cpuProfile_history_colors ? g_cpuProfile_counters[j].color : RGB( 80,80,80 ) );
hBrushOld = ( HBRUSH )SelectObject( hdc, hColoredBrush );
Rectangle( hdc, x-4, y-h, x, y+1 );
y -= h;
SelectObject( hdc, hBrushOld );
DeleteObject( hColoredBrush );
}
}
currentSample--;
if ( currentSample < 0 )
{
// no data
break;
}
}
}
SelectObject( hdc, hFontOld );
SelectObject( hdc, hPenOld );
DeleteObject( hBlackPen );
DeleteObject( hGreyPen );
}
//-----------------------------------------------------------------------------
// CpuProfile_WndProc
//
//-----------------------------------------------------------------------------
LRESULT CALLBACK CpuProfile_WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
WORD wID = LOWORD( wParam );
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
int id;
bool bIsSamples;
bool bIsHistory;
CREATESTRUCT *createStructPtr;
// identify window
id = ( int )GetWindowLong( hwnd, GWL_USERDATA+0 );
bIsSamples = ( id == ID_CPUPROFILE_SAMPLES );
bIsHistory = ( id == ID_CPUPROFILE_HISTORY );
switch ( message )
{
case WM_CREATE:
// set the window identifier
createStructPtr = ( CREATESTRUCT* )lParam;
SetWindowLong( hwnd, GWL_USERDATA+0, ( LONG )createStructPtr->lpCreateParams );
// reset peaks
g_cpuProfile_lastPeakTime = 0;
return 0L;
case WM_DESTROY:
CpuProfile_SaveConfig();
if ( bIsSamples )
g_cpuProfile_hWndSamples = NULL;
else if ( bIsHistory )
g_cpuProfile_hWndHistory = NULL;
if ( VProf_GetState() == VPROF_CPU )
{
VProf_Enable( VPROF_OFF );
}
return 0L;
case WM_INITMENU:
if ( bIsSamples )
{
CheckMenuItem( ( HMENU )wParam, IDM_CPUPROFILE_TICKMARKS, MF_BYCOMMAND | ( g_cpuProfile_samples_tickMarks ? MF_CHECKED : MF_UNCHECKED ) );
CheckMenuItem( ( HMENU )wParam, IDM_CPUPROFILE_COLORS, MF_BYCOMMAND | ( g_cpuProfile_samples_colors ? MF_CHECKED : MF_UNCHECKED ) );
}
else if ( bIsHistory )
{
CheckMenuItem( ( HMENU )wParam, IDM_CPUPROFILE_TICKMARKS, MF_BYCOMMAND | ( g_cpuProfile_history_tickMarks ? MF_CHECKED : MF_UNCHECKED ) );
CheckMenuItem( ( HMENU )wParam, IDM_CPUPROFILE_COLORS, MF_BYCOMMAND | ( g_cpuProfile_history_colors ? MF_CHECKED : MF_UNCHECKED ) );
}
CheckMenuItem( ( HMENU )wParam, IDM_CPUPROFILE_FPSLABELS, MF_BYCOMMAND | ( g_cpuProfile_fpsLabels ? MF_CHECKED : MF_UNCHECKED ) );
CheckMenuItem( ( HMENU )wParam, IDM_CPUPROFILE_ENABLE, MF_BYCOMMAND | ( VProf_GetState() == VPROF_CPU ? MF_CHECKED : MF_UNCHECKED ) );
return 0L;
case WM_PAINT:
GetClientRect( hwnd, &rect );
hdc = BeginPaint( hwnd, &ps );
if ( bIsSamples )
ProfileSamples_Draw( hdc, &rect );
else if ( bIsHistory )
ProfileHistory_Draw( hdc, &rect );
EndPaint( hwnd, &ps );
return 0L;
case WM_SIZE:
// force a redraw
CpuProfile_UpdateWindow();
return 0L;
case WM_KEYDOWN:
switch ( wParam )
{
case VK_INSERT:
if ( bIsSamples )
CpuProfile_ZoomIn( g_cpuProfile_samples_scale, PROFILE_SAMPLES_SCALESTEPS );
else if ( bIsHistory )
CpuProfile_ZoomIn( g_cpuProfile_history_scale, PROFILE_HISTORY_SCALESTEPS );
return 0L;
case VK_DELETE:
if ( bIsSamples )
CpuProfile_ZoomOut( g_cpuProfile_samples_scale, PROFILE_SAMPLES_SCALESTEPS );
else if ( bIsHistory )
CpuProfile_ZoomOut( g_cpuProfile_history_scale, PROFILE_HISTORY_SCALESTEPS );
return 0L;
}
break;
case WM_COMMAND:
switch ( wID )
{
case IDM_CPUPROFILE_TICKMARKS:
if ( bIsSamples )
g_cpuProfile_samples_tickMarks ^= 1;
else if ( bIsHistory )
g_cpuProfile_history_tickMarks ^= 1;
CpuProfile_UpdateWindow();
return 0L;
case IDM_CPUPROFILE_COLORS:
if ( bIsSamples )
g_cpuProfile_samples_colors ^= 1;
else if ( bIsHistory )
g_cpuProfile_history_colors ^= 1;
CpuProfile_UpdateWindow();
return 0L;
case IDM_CPUPROFILE_FPSLABELS:
g_cpuProfile_fpsLabels ^= 1;
CpuProfile_UpdateWindow();
return 0L;
case IDM_CPUPROFILE_ZOOMIN:
if ( bIsSamples )
CpuProfile_ZoomIn( g_cpuProfile_samples_scale, PROFILE_SAMPLES_SCALESTEPS );
else if ( bIsHistory )
CpuProfile_ZoomIn( g_cpuProfile_history_scale, PROFILE_HISTORY_SCALESTEPS );
return 0L;
case IDM_CPUPROFILE_ZOOMOUT:
if ( bIsSamples )
CpuProfile_ZoomOut( g_cpuProfile_samples_scale, PROFILE_SAMPLES_SCALESTEPS );
else if ( bIsHistory )
CpuProfile_ZoomOut( g_cpuProfile_history_scale, PROFILE_HISTORY_SCALESTEPS );
return 0L;
case IDM_CPUPROFILE_ENABLE:
bool bEnable = ( VProf_GetState() == VPROF_CPU );
bEnable ^= 1;
if ( !bEnable )
VProf_Enable( VPROF_OFF );
else
VProf_Enable( VPROF_CPU );
CpuProfile_SetTitle();
return 0L;
}
break;
}
return ( DefWindowProc( hwnd, message, wParam, lParam ) );
}
//-----------------------------------------------------------------------------
// CpuProfileHistory_Open
//
//-----------------------------------------------------------------------------
void CpuProfileHistory_Open()
{
HWND hWnd;
if ( g_cpuProfile_hWndHistory )
{
// only one profile instance
if ( IsIconic( g_cpuProfile_hWndHistory ) )
ShowWindow( g_cpuProfile_hWndHistory, SW_RESTORE );
SetForegroundWindow( g_cpuProfile_hWndHistory );
return;
}
if ( VProf_GetState() == VPROF_OFF )
{
VProf_Enable( VPROF_CPU );
}
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"CPUPROFILEHISTORYCLASS",
"",
WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
0,
0,
600,
500,
g_hDlgMain,
NULL,
g_hInstance,
( void* )ID_CPUPROFILE_HISTORY );
g_cpuProfile_hWndHistory = hWnd;
CpuProfile_SetTitle();
if ( g_cpuProfile_historyWindowRect.right && g_cpuProfile_historyWindowRect.bottom )
MoveWindow( g_cpuProfile_hWndHistory, g_cpuProfile_historyWindowRect.left, g_cpuProfile_historyWindowRect.top, g_cpuProfile_historyWindowRect.right-g_cpuProfile_historyWindowRect.left, g_cpuProfile_historyWindowRect.bottom-g_cpuProfile_historyWindowRect.top, FALSE );
ShowWindow( g_cpuProfile_hWndHistory, SHOW_OPENWINDOW );
}
//-----------------------------------------------------------------------------
// CpuProfileSamples_Open
//
//-----------------------------------------------------------------------------
void CpuProfileSamples_Open()
{
HWND hWnd;
if ( g_cpuProfile_hWndSamples )
{
// only one profile instance
if ( IsIconic( g_cpuProfile_hWndSamples ) )
ShowWindow( g_cpuProfile_hWndSamples, SW_RESTORE );
SetForegroundWindow( g_cpuProfile_hWndSamples );
return;
}
if ( VProf_GetState() == VPROF_OFF )
{
VProf_Enable( VPROF_CPU );
}
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"CPUPROFILESAMPLESCLASS",
"",
WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
0,
0,
600,
500,
g_hDlgMain,
NULL,
g_hInstance,
( void* )ID_CPUPROFILE_SAMPLES );
g_cpuProfile_hWndSamples = hWnd;
CpuProfile_SetTitle();
if ( g_cpuProfile_samplesWindowRect.right && g_cpuProfile_samplesWindowRect.bottom )
MoveWindow( g_cpuProfile_hWndSamples, g_cpuProfile_samplesWindowRect.left, g_cpuProfile_samplesWindowRect.top, g_cpuProfile_samplesWindowRect.right-g_cpuProfile_samplesWindowRect.left, g_cpuProfile_samplesWindowRect.bottom-g_cpuProfile_samplesWindowRect.top, FALSE );
ShowWindow( g_cpuProfile_hWndSamples, SHOW_OPENWINDOW );
}
//-----------------------------------------------------------------------------
// CpuProfile_Clear
//
//-----------------------------------------------------------------------------
void CpuProfile_Clear()
{
// clear counters and history
g_cpuProfile_numCounters = 0;
g_cpuProfile_numSamples = 0;
CpuProfile_UpdateWindow();
}
//-----------------------------------------------------------------------------
// CpuProfile_Init
//
//-----------------------------------------------------------------------------
bool CpuProfile_Init()
{
WNDCLASS wndclass;
// set up our window class
memset( &wndclass, 0, sizeof( wndclass ) );
wndclass.style = 0;
wndclass.lpfnWndProc = CpuProfile_WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = g_hInstance;
wndclass.hIcon = g_hIcons[ICON_APPLICATION];
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = g_hBackgroundBrush;
wndclass.lpszMenuName = MAKEINTRESOURCE( MENU_CPUPROFILE );
wndclass.lpszClassName = "CPUPROFILESAMPLESCLASS";
if ( !RegisterClass( &wndclass ) )
return false;
// set up our window class
memset( &wndclass, 0, sizeof( wndclass ) );
wndclass.style = 0;
wndclass.lpfnWndProc = CpuProfile_WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = g_hInstance;
wndclass.hIcon = g_hIcons[ICON_APPLICATION];
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = g_hBackgroundBrush;
wndclass.lpszMenuName = MAKEINTRESOURCE( MENU_CPUPROFILE );
wndclass.lpszClassName = "CPUPROFILEHISTORYCLASS";
if ( !RegisterClass( &wndclass ) )
return false;
CpuProfile_LoadConfig();
return true;
}
|