Skip to content

Commit

Permalink
Mark render threads as long-running, makes the UI more responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-zhao committed Jun 29, 2024
1 parent abb7363 commit ae6e780
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions LibSidWiz/WaveformRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,7 @@ private void Render(int startFrame, int endFrame, Action<SKImage, byte[]> onFram
{
for (var frameIndex = startFrame; frameIndex <= endFrame; ++frameIndex)
{
// Compute the start of the sample window
int frameIndexSamples = (int)((long)frameIndex * SamplingRate / FramesPerSecond);
var info = new FrameInfo
{
FrameIndex = frameIndex,
ChannelTriggerPoints = Enumerable.Repeat(frameIndexSamples, _channels.Count).ToList()
};
// For each channel...
for (int channelIndex = 0; channelIndex < _channels.Count; ++channelIndex)
{
var channel = _channels[channelIndex];
if (channel.IsEmpty ||
!string.IsNullOrEmpty(channel.ErrorMessage) ||
channel.Loading ||
channel.IsSilent)
{
// No trigger to find
continue;
}

// Compute the "trigger point". This will be the centre of our rendering.
var triggerPoint = channel.GetTriggerPoint(frameIndexSamples, frameSamples,
triggerPoints[channelIndex]);
info.ChannelTriggerPoints[channelIndex] = triggerPoint;
triggerPoints[channelIndex] = triggerPoint;
}
var info = GetFrameInfo(frameIndex, frameSamples, triggerPoints);

// Add to the queue (may block)
queue.Add(info);
Expand All @@ -150,7 +125,6 @@ private void Render(int startFrame, int endFrame, Action<SKImage, byte[]> onFram
var renderedFrames = new ConcurrentDictionary<int, FrameInfo>();
var frameReadySignal = new AutoResetEvent(false);
// Then we kick off some parallel threads to do the rendering, consuming from queue and emitting to renderedFrames indexed by frame index

foreach (var _ in Enumerable.Range(0, numThreads))
{
Task.Factory.StartNew(() =>
Expand Down Expand Up @@ -260,7 +234,7 @@ private void Render(int startFrame, int endFrame, Action<SKImage, byte[]> onFram
paint?.Dispose();
}
}
});
}, TaskCreationOptions.LongRunning);
}

// Finally, we consume the queue
Expand All @@ -285,6 +259,38 @@ private void Render(int startFrame, int endFrame, Action<SKImage, byte[]> onFram
}
}

private FrameInfo GetFrameInfo(int frameIndex, int frameSamples, int[] triggerPoints)
{
// Compute the start of the sample window
int frameIndexSamples = (int)((long)frameIndex * SamplingRate / FramesPerSecond);
var info = new FrameInfo
{
FrameIndex = frameIndex,
ChannelTriggerPoints = Enumerable.Repeat(frameIndexSamples, _channels.Count).ToList()
};
// For each channel...
for (int channelIndex = 0; channelIndex < _channels.Count; ++channelIndex)
{
var channel = _channels[channelIndex];
if (channel.IsEmpty ||
!string.IsNullOrEmpty(channel.ErrorMessage) ||
channel.Loading ||
channel.IsSilent)
{
// No trigger to find
continue;
}

// Compute the "trigger point". This will be the centre of our rendering.
var triggerPoint = channel.GetTriggerPoint(frameIndexSamples, frameSamples,
triggerPoints[channelIndex]);
info.ChannelTriggerPoints[channelIndex] = triggerPoint;
triggerPoints[channelIndex] = triggerPoint;
}

return info;
}

private class FrameInfo
{
public int FrameIndex { get; set; }
Expand Down

0 comments on commit ae6e780

Please sign in to comment.