Skip to content

Commit 22160cf

Browse files
walrusmcdryanlai2
authored andcommitted
User/paulm/fps (#198)
* added FPS counters * handle FPS less than 0 * PR feedback
1 parent 4f9c6de commit 22160cf

File tree

2 files changed

+80
-15
lines changed

2 files changed

+80
-15
lines changed

Samples/FNSCandyStyleTransfer/UWP/cs/MainPage.xaml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,30 @@ Copyright (C) Microsoft Corporation. All rights reserved.
180180
</StackPanel>
181181

182182
<Border x:Name="UIStatusBorder" Grid.Row="2">
183-
<ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" MaxHeight="200">
183+
<StackPanel Orientation="Horizontal">
184184
<TextBlock x:Name="StatusBlock"
185-
Text="Select a style to begin"
186-
FontWeight="Bold"
187-
MaxWidth="{Binding ElementName=Splitter, Path=ActualWidth}"
188-
Margin="10,10,10,20"
189-
TextWrapping="Wrap" />
190-
</ScrollViewer>
185+
Text="Select a style to begin"
186+
FontWeight="Bold"
187+
Width="700"
188+
Margin="10,2,0,0"
189+
TextWrapping="Wrap" />
190+
<TextBlock
191+
Text="Capture FPS"
192+
Margin="10,2,0,0"
193+
FontWeight="Bold" />
194+
<TextBlock x:Name="CaptureFPS"
195+
Text="0 fps"
196+
Margin="10,2,0,0"
197+
TextWrapping="Wrap" />
198+
<TextBlock
199+
Text="Render FPS"
200+
Margin="10,2,0,0"
201+
FontWeight="Bold"/>
202+
<TextBlock x:Name="RenderFPS"
203+
Text="0 fps"
204+
Margin="10,2,0,0"
205+
TextWrapping="Wrap" />
206+
</StackPanel>
191207
</Border>
192208

193209
</Grid>

Samples/FNSCandyStyleTransfer/UWP/cs/MainPage.xaml.cs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public sealed partial class MainPage : Page
8080

8181
// Debug
8282
private Stopwatch _perfStopwatch = new Stopwatch(); // performance Stopwatch used throughout
83+
private DispatcherTimer _FramesPerSecondTimer = new DispatcherTimer();
84+
private long _CaptureFPS = 0;
85+
public static long _RenderFPS = 0;
86+
private int _LastFPSTick = 0;
8387

8488
public MainPage()
8589
{
@@ -110,6 +114,47 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
110114

111115
// Select first style
112116
UIStyleList.SelectedIndex = 0;
117+
118+
// Create a 1 second timer
119+
_FramesPerSecondTimer.Tick += _FramesPerSecond_Tick;
120+
_FramesPerSecondTimer.Interval = new TimeSpan(0, 0, 1);
121+
_FramesPerSecondTimer.Start();
122+
}
123+
124+
private void _FramesPerSecond_Tick(object sender, object e)
125+
{
126+
// how many seconds has it been?
127+
// Note: we do this math since even though we asked for the event to be
128+
// dispatched every 1s , due to timing and delays, it might not come
129+
// exactly every second. and on a busy system it could even be a couple of
130+
// seconds until it is delivered.
131+
int fpsTick = System.Environment.TickCount;
132+
133+
if (_LastFPSTick > 0)
134+
{
135+
float numberOfSeconds = ((float)(fpsTick - _LastFPSTick)) / (float)1000;
136+
137+
// how many frames did we capture?
138+
float intervalFPS = ((float)_CaptureFPS) / numberOfSeconds;
139+
if (intervalFPS == 0.0)
140+
return;
141+
NotifyUser(CaptureFPS, $"{intervalFPS:F1}", NotifyType.StatusMessage);
142+
143+
// how many frames did we render
144+
intervalFPS = ((float)_RenderFPS) / numberOfSeconds;
145+
if (intervalFPS == 0.0)
146+
return;
147+
NotifyUser(RenderFPS, $"{intervalFPS:F1}", NotifyType.StatusMessage);
148+
}
149+
150+
_CaptureFPS = 0;
151+
_RenderFPS = 0;
152+
_LastFPSTick = fpsTick;
153+
}
154+
155+
public void NotifyUser(string strMessage, NotifyType type)
156+
{
157+
NotifyUser(StatusBlock, strMessage, type);
113158
}
114159

115160
/// <summary>
@@ -118,17 +163,17 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
118163
/// </summary>
119164
/// <param name="strMessage"></param>
120165
/// <param name="type"></param>
121-
public void NotifyUser(string strMessage, NotifyType type)
166+
public void NotifyUser(TextBlock block, string strMessage, NotifyType type)
122167
{
123168
// If called from the UI thread, then update immediately.
124169
// Otherwise, schedule a task on the UI thread to perform the update.
125170
if (Dispatcher.HasThreadAccess)
126171
{
127-
UpdateStatus(strMessage, type);
172+
UpdateStatus(block, strMessage, type);
128173
}
129174
else
130175
{
131-
var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateStatus(strMessage, type));
176+
var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateStatus(block, strMessage, type));
132177
task.AsTask().Wait();
133178
}
134179
}
@@ -138,7 +183,7 @@ public void NotifyUser(string strMessage, NotifyType type)
138183
/// </summary>
139184
/// <param name="strMessage"></param>
140185
/// <param name="type"></param>
141-
private void UpdateStatus(string strMessage, NotifyType type)
186+
private void UpdateStatus(TextBlock block, string strMessage, NotifyType type)
142187
{
143188
switch (type)
144189
{
@@ -150,11 +195,11 @@ private void UpdateStatus(string strMessage, NotifyType type)
150195
break;
151196
}
152197

153-
StatusBlock.Text = strMessage;
198+
block.Text = strMessage;
154199

155-
// Collapse the StatusBlock if it has no text to conserve real estate.
156-
UIStatusBorder.Visibility = (StatusBlock.Text != String.Empty) ? Visibility.Visible : Visibility.Collapsed;
157-
if (StatusBlock.Text != String.Empty)
200+
// Collapse the TextBlock if it has no text to conserve real estate.
201+
UIStatusBorder.Visibility = (block.Text != String.Empty) ? Visibility.Visible : Visibility.Collapsed;
202+
if (block.Text != String.Empty)
158203
{
159204
UIStatusBorder.Visibility = Visibility.Visible;
160205
UIStatusPanel.Visibility = Visibility.Visible;
@@ -816,6 +861,8 @@ private async void _modelInputFrameReader_FrameArrived(MediaFrameReader sender,
816861
// Do not attempt processing of more than 1 frame at a time
817862
_frameAquisitionLock.Wait();
818863
{
864+
_CaptureFPS += 1;
865+
819866
if (_isProcessingFrames)
820867
{
821868
_frameAquisitionLock.Release();
@@ -1059,6 +1106,8 @@ public void RenderFrame(SoftwareBitmap softwareBitmap)
10591106
{
10601107
var imageSource = (SoftwareBitmapSource)_imageElement.Source;
10611108
await imageSource.SetBitmapAsync(_backBuffer);
1109+
1110+
MainPage._RenderFPS += 1;
10621111
}
10631112
catch (Exception ex)
10641113
{

0 commit comments

Comments
 (0)