Skip to content

Commit

Permalink
fix(sample): memory leaks when using VideoSource
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 2, 2021
1 parent 737311d commit cab8e26
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions Assets/Mediapipe/Samples/Common/Scripts/ImageSource/TextureFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ public class ReleaseEvent : UnityEvent<TextureFrame> { }
// Buffers that will be used to copy texture data on CPU.
// They won't be initialized until it's necessary.
private Texture2D _textureBuffer;
private Texture2D textureBuffer
{
get
{
if (_textureBuffer == null)
{
_textureBuffer = new Texture2D(_texture.width, _texture.height, _texture.format, false);
}
return _textureBuffer;
}
}

private Color32[] _pixelsBuffer; // for WebCamTexture
private Color32[] pixelsBuffer
Expand Down Expand Up @@ -146,16 +135,7 @@ public bool ReadTextureFromOnGPU(Texture src)
/// </remarks>
public void ReadTextureFromOnCPU(Texture src)
{
var currentRenderTexture = RenderTexture.active;
var tmpRenderTexture = new RenderTexture(src.width, src.height, 32);
Graphics.Blit(src, tmpRenderTexture);
RenderTexture.active = tmpRenderTexture;

var rect = new UnityEngine.Rect(0, 0, Mathf.Min(tmpRenderTexture.width, textureBuffer.width), Mathf.Min(tmpRenderTexture.height, textureBuffer.height));
textureBuffer.ReadPixels(rect, 0, 0);
textureBuffer.Apply();
RenderTexture.active = currentRenderTexture;

var textureBuffer = LoadToTextureBuffer(src);
SetPixels32(textureBuffer.GetPixels32());
}

Expand Down Expand Up @@ -358,24 +338,28 @@ private void ChangeNameFrom(uint oldName)
}
}

private Texture2D GetTextureBufferFor(Texture texture)
private Texture2D LoadToTextureBuffer(Texture texture)
{
var textureFormat = GetTextureFormat(texture);

if (_textureBuffer == null || _textureBuffer.format != textureFormat)
{
_textureBuffer = new Texture2D(texture.width, texture.height, textureFormat, false);
_textureBuffer = new Texture2D(width, height, textureFormat, false);
}

var currentRenderTexture = RenderTexture.active;
var tmpRenderTexture = new RenderTexture(texture.width, texture.height, 32);

Graphics.Blit(texture, tmpRenderTexture);

var currentRenderTexture = RenderTexture.active;
RenderTexture.active = tmpRenderTexture;

_textureBuffer.ReadPixels(new UnityEngine.Rect(0, 0, tmpRenderTexture.width, tmpRenderTexture.height), 0, 0);
var rect = new UnityEngine.Rect(0, 0, Mathf.Min(tmpRenderTexture.width, _textureBuffer.width), Mathf.Min(tmpRenderTexture.height, _textureBuffer.height));
_textureBuffer.ReadPixels(rect, 0, 0);
_textureBuffer.Apply();
RenderTexture.active = currentRenderTexture;

tmpRenderTexture.Release();

return _textureBuffer;
}
}
Expand Down

0 comments on commit cab8e26

Please sign in to comment.