浏览代码

fix a minor memory leak in mesh_generator

try to keep the object pool as small as possible
/main
xingwei.zhu 5 年前
当前提交
3c2b3a5f
共有 7 个文件被更改,包括 47 次插入33 次删除
  1. 2
      Runtime/editor/rasterizer.cs
  2. 5
      Runtime/ui/painting/txt/mesh_generator.cs
  3. 33
      Runtime/ui/utils/renderer/allocUtils/debug.cs
  4. 10
      Runtime/ui/utils/renderer/allocUtils/pool_object.cs
  5. 9
      Runtime/ui/utils/renderer/allocUtils/uipath_cache_manager.cs
  6. 19
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  7. 2
      Samples/UIWidgetSample/TextInput.unity

2
Runtime/editor/rasterizer.cs


if (compositorFrame != null && compositorFrame.raster(layerTree, false)) {
frame.submit();
this._fireNextFrameCallbackIfPresent();
AllocDebugger.onFrameEnd();
AllocDebugger.onFrameEnd();
return false;
}
}

5
Runtime/ui/painting/txt/mesh_generator.cs


}
uiMeshMesh mesh = vertices.Count > 0 ? uiMeshMesh.create(null, vertices, triangles, uv) : null;
if (_meshes.ContainsKey(key)) {
ObjectPool<MeshInfo>.release(_meshes[key]);
_meshes.Remove(key);
}
_meshes[key] = MeshInfo.create(key, mesh, fontInfo.textureVersion);
this._mesh = mesh.transform(this.matrix);

33
Runtime/ui/utils/renderer/allocUtils/debug.cs


public static class AllocDebugger {
public const bool enableDebugging = false;
public const bool enableDebugging = true;
public static void onAlloc(int objKey, string objName) {
if (!debugInfos.ContainsKey(objKey)) {
debugInfos[objKey] = new DebugMeta {
objName = objName,
watermark = 0,
borrowed = 0
};
public static void onFrameEnd() {
if (!enableDebugging) {
return;
debugInfos[objKey].onAlloc();
if (allocCount >= 1000) {
if (allocCount >= 120) {
allocCount = 0;
string debugInfo = "Alloc Stats: ";

debugInfo += "|" + item.objName + " = " + item.watermark + "," + item.borrowed + "|";
debugInfo += "|" + item.objName + " = " + item.watermark + "|";
debugInfo += "|" + item.objName + " = " + item.watermark + "," + item.borrowed + "|";
}
if (debugInfo == "Alloc Stats: ") {

}
}
public static void onAlloc(int objKey, string objName) {
if (!debugInfos.ContainsKey(objKey)) {
debugInfos[objKey] = new DebugMeta {
objName = objName,
watermark = 0,
borrowed = 0
};
}
debugInfos[objKey].onAlloc();
}
public static void onRelease(int objKey, string objName) {

10
Runtime/ui/utils/renderer/allocUtils/pool_object.cs


if (obj == null) {
return;
}
if (AllocDebugger.enableDebugging) {
if (!obj.activated_flag) {
Debug.Assert(false, "an item has been recycled more than once !");

AllocDebugger.onRelease(debugKey, debugName);
}
if (pool.Count > 256) {
//there are enough items in the pool
//just release the obj to GC
return;
}
obj.clear();
pool.Push(obj);
}

9
Runtime/ui/utils/renderer/allocUtils/uipath_cache_manager.cs


public static class uiPathCacheManager {
static Dictionary<uint, uiPath> cache = new Dictionary<uint, uiPath>(256);
//remove unused cache items every 30 frame
//remove unused cache items every 1 frame
static Dictionary<uint, bool> touched = new Dictionary<uint, bool>(256);
static float curFrame;

public static void tickNextFrame() {
curFrame++;
if (curFrame < 30) {
return;
}
curFrame = 0;
untouched.Clear();
foreach (var key in cache.Keys) {
if (!touched.ContainsKey(key)) {

19
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_impl.cs


void _reset() {
//clear all states
D.assert(this._layers.Count == 0 || (this._layers.Count == 1 && this._layers[0] == this._currentLayer));
if (this._currentLayer != null) {
this._clearLayer(this._currentLayer);
ObjectPool<RenderLayer>.release(this._currentLayer);
this._currentLayer = null;
this._lastScissor = null;
this._layers.Clear();
}
D.assert(this._currentLayer == null && this._layers.Count == 0);
var width = this._renderTexture.width;
var height = this._renderTexture.height;

cmdBuf.DisableScissorRect();
Graphics.ExecuteCommandBuffer(cmdBuf);
}
D.assert(this._layers.Count == 0 || (this._layers.Count == 1 && this._layers[0] == this._currentLayer));
if (this._currentLayer != null) {
this._clearLayer(this._currentLayer);
ObjectPool<RenderLayer>.release(this._currentLayer);
this._currentLayer = null;
this._lastScissor = null;
this._layers.Clear();
}
}

2
Samples/UIWidgetSample/TextInput.unity


width: 1
height: 1
devicePixelRatioOverride: 0
antiAliasingOverride: 4
antiAliasingOverride: 0
--- !u!222 &1387978529
CanvasRenderer:
m_ObjectHideFlags: 0

正在加载...
取消
保存