From cab8e26cb627f393f6dcbc3849b1675f39f56427 Mon Sep 17 00:00:00 2001 From: homuler Date: Thu, 2 Dec 2021 11:49:29 +0900 Subject: [PATCH] fix(sample): memory leaks when using VideoSource --- .../Scripts/ImageSource/TextureFrame.cs | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/TextureFrame.cs b/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/TextureFrame.cs index 4659ab8ef..32b965a28 100644 --- a/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/TextureFrame.cs +++ b/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/TextureFrame.cs @@ -30,17 +30,6 @@ public class ReleaseEvent : UnityEvent { } // 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 @@ -146,16 +135,7 @@ public bool ReadTextureFromOnGPU(Texture src) /// 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()); } @@ -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; } }