Skip to content

Commit

Permalink
Merge pull request #25 from xmedeko/d3drenderer-cloneclean
Browse files Browse the repository at this point in the history
D3DRenderer clones optimalization
  • Loading branch information
Sascha-L authored Jan 19, 2017
2 parents db0274e + b643c7c commit 3f44720
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions Source/DirectShow/Controls/D3DRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,44 +336,13 @@ private void CompositionTargetRendering(object sender, EventArgs e)
InternalInvalidateVideoImage();
}

/// <summary>
/// Cleans up any dead references we may have to any cloned renderers
/// </summary>
private void CleanZombieRenderers()
{
lock (m_clonedD3Drenderers)
{
var deadObjects = new List<WeakReference>();

for (int i = 0; i < m_clonedD3Drenderers.Count; i++)
{
if (!m_clonedD3Drenderers[i].IsAlive)
deadObjects.Add(m_clonedD3Drenderers[i]);
}

foreach (var deadGuy in deadObjects)
{
m_clonedD3Drenderers.Remove(deadGuy);
}
}
}

/// <summary>
/// Sets the backbuffer for any cloned D3DRenderers
/// </summary>
private void SetBackBufferForClones()
{
lock (m_clonedD3Drenderers)
{
CleanZombieRenderers();

foreach (var rendererRef in m_clonedD3Drenderers)
{
var renderer = rendererRef.Target as D3DRenderer;
if (renderer != null)
renderer.SetBackBuffer(m_pBackBuffer);
}
}
var backBuffer = m_pBackBuffer;
ForEachCloneD3DRenderer(r => r.SetBackBuffer(backBuffer));
}

/// <summary>
Expand Down Expand Up @@ -430,17 +399,37 @@ private bool GetSetVideoImageInvalid(bool value)
/// Invalidates any possible cloned renderer we may have
/// </summary>
private void InvalidateClonedVideoImages()
{
ForEachCloneD3DRenderer(r => r.InvalidateVideoImage());
}

private void ForEachCloneD3DRenderer(Action<D3DRenderer> action)
{
lock (m_clonedD3Drenderers)
{
CleanZombieRenderers();

bool needClean = false;
foreach (var rendererRef in m_clonedD3Drenderers)
{
var renderer = rendererRef.Target as D3DRenderer;
if (renderer != null)
renderer.InvalidateVideoImage();
action(renderer);
else
needClean = true;
}

if (needClean)
CleanZombieRenderers();
}
}

/// <summary>
/// Cleans up any dead references we may have to any cloned renderers
/// </summary>
private void CleanZombieRenderers()
{
lock (m_clonedD3Drenderers)
{
m_clonedD3Drenderers.RemoveAll(c => !c.IsAlive);
}
}

Expand Down

0 comments on commit 3f44720

Please sign in to comment.