xingwei.zhu
5 年前
当前提交
9ac5dbe6
共有 20 个文件被更改,包括 398 次插入 和 302 次删除
-
34Runtime/ui/painting/txt/mesh_generator.cs
-
2Runtime/ui/utils/renderer/allocUtils/generic_list.cs
-
2Runtime/ui/utils/renderer/cmdbufferCanvas/command_buffer_canvas.cs
-
26Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_clip.cs
-
42Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
-
16Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_shader.cs
-
2Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_shader_utils.cs
-
20Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs
-
28Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/render_cmd.cs
-
12Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/render_layer.cs
-
2Runtime/ui/utils/renderer/common/base_canvas.cs
-
121Runtime/ui/utils/renderer/common/draw_cmd.cs
-
22Runtime/ui/utils/renderer/common/geometry/mesh_mesh.cs
-
24Runtime/ui/utils/renderer/common/geometry/path/path.cs
-
18Runtime/ui/utils/renderer/common/geometry/path/path_cache.cs
-
18Runtime/ui/utils/renderer/common/geometry/path/tessellation_generator.cs
-
10Runtime/ui/utils/renderer/common/picture.cs
-
68Runtime/ui/utils/renderer/allocUtils/debug.cs
-
87Runtime/ui/utils/renderer/allocUtils/pool_object.cs
-
146Runtime/ui/utils/renderer/allocUtils/pool_items.cs
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.ui { |
|||
|
|||
class DebugMeta { |
|||
public string objName; |
|||
public int watermark; |
|||
public int prev_watermark; |
|||
public int borrowed; |
|||
|
|||
public void onAlloc() { |
|||
this.borrowed++; |
|||
this.watermark = this.borrowed > this.watermark ? this.borrowed : this.watermark; |
|||
} |
|||
|
|||
public void onRelease() { |
|||
this.borrowed--; |
|||
} |
|||
} |
|||
|
|||
public static class AllocDebugger { |
|||
|
|||
public const bool enableDebugging = true; |
|||
|
|||
static int allocCount = 0; |
|||
|
|||
static readonly Dictionary<int, DebugMeta> debugInfos = new Dictionary<int, DebugMeta>(); |
|||
|
|||
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(); |
|||
|
|||
allocCount++; |
|||
if (allocCount >= 1000) { |
|||
allocCount = 0; |
|||
|
|||
string debugInfo = "Alloc Stats: "; |
|||
foreach (var key in debugInfos.Keys) { |
|||
var item = debugInfos[key]; |
|||
if (item.watermark <= item.prev_watermark) { |
|||
continue; |
|||
} |
|||
|
|||
item.prev_watermark = item.watermark; |
|||
debugInfo += "|" + item.objName + " = " + item.watermark + "|"; |
|||
} |
|||
|
|||
if (debugInfo == "Alloc Stats: ") { |
|||
return; |
|||
} |
|||
Debug.Log(debugInfo); |
|||
} |
|||
} |
|||
|
|||
public static void onRelease(int objKey, string objName) { |
|||
Debug.Assert(debugInfos.ContainsKey(objKey), "An unregistered pool object cannot be released"); |
|||
debugInfos[objKey].onRelease(); |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections.Generic; |
|||
using System.Diagnostics; |
|||
|
|||
namespace Unity.UIWidgets.ui { |
|||
public abstract class PoolObject |
|||
{ |
|||
public bool activated_flag; |
|||
|
|||
public virtual void setup() {} |
|||
public virtual void clear() {} |
|||
} |
|||
|
|||
public static class ObjectPool<TObject> where TObject :PoolObject, new() { |
|||
static readonly Stack<TObject> pool = new Stack<TObject>(); |
|||
|
|||
public static TObject alloc() { |
|||
if (pool.Count == 0) { |
|||
for (int i = 0; i < 128; i++) { |
|||
var obj = new TObject(); |
|||
pool.Push(obj); |
|||
} |
|||
} |
|||
|
|||
var ret = pool.Pop(); |
|||
ret.setup(); |
|||
|
|||
if (AllocDebugger.enableDebugging) { |
|||
AllocDebugger.onAlloc(debugKey, debugName); |
|||
ret.activated_flag = true; |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
public static void release(TObject obj) { |
|||
if (obj == null) { |
|||
return; |
|||
} |
|||
|
|||
if (AllocDebugger.enableDebugging) { |
|||
if (!obj.activated_flag) { |
|||
Debug.Assert(false, "an item has been recycled more than once !"); |
|||
} |
|||
obj.activated_flag = false; |
|||
|
|||
AllocDebugger.onRelease(debugKey, debugName); |
|||
} |
|||
|
|||
obj.clear(); |
|||
pool.Push(obj); |
|||
} |
|||
|
|||
//For debugger
|
|||
static bool _debugInfoReady = false; |
|||
static string _debugName = null; |
|||
|
|||
static void _generateDebugInfo() { |
|||
var ctype = typeof(TObject); |
|||
_debugName = ctype.ToString(); |
|||
_debugKey = ctype.GetHashCode(); |
|||
|
|||
_debugInfoReady = true; |
|||
} |
|||
public static string debugName { |
|||
get { |
|||
if(_debugInfoReady) |
|||
{ |
|||
return _debugName; |
|||
} |
|||
_generateDebugInfo(); |
|||
return _debugName; |
|||
} |
|||
} |
|||
|
|||
static int _debugKey = -1; |
|||
|
|||
public static int debugKey { |
|||
get { |
|||
if (_debugInfoReady) { |
|||
return _debugKey; |
|||
} |
|||
_generateDebugInfo(); |
|||
return _debugKey; |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.ui { |
|||
|
|||
public class ItemDebugInfo { |
|||
public int _watermark; |
|||
public int _old_watermark; |
|||
public int _size; |
|||
|
|||
public void consume() { |
|||
this._size++; |
|||
if (this._size > this._watermark) { |
|||
this._watermark = this._size; |
|||
} |
|||
} |
|||
|
|||
public void recycle() { |
|||
this._size--; |
|||
} |
|||
} |
|||
|
|||
public static class ItemPoolManager { |
|||
static readonly Dictionary<Type, List<PoolItem>> poolDict = new Dictionary<Type, List<PoolItem>>(); |
|||
static readonly Dictionary<Type, ItemDebugInfo> debugInfo = new Dictionary<Type, ItemDebugInfo>(); |
|||
|
|||
const bool _debugFlag = true; |
|||
|
|||
static int _allocTick = 0; |
|||
|
|||
public static void showDebugInfo() { |
|||
string info = ""; |
|||
foreach (var key in debugInfo.Keys) { |
|||
if (debugInfo[key]._old_watermark == debugInfo[key]._watermark) { |
|||
continue; |
|||
} |
|||
info += "| " + key + " = " + debugInfo[key]._watermark + " |\n"; |
|||
debugInfo[key]._old_watermark = debugInfo[key]._watermark; |
|||
} |
|||
|
|||
if (info != "") { |
|||
Debug.Log(info); |
|||
} |
|||
} |
|||
|
|||
public static T alloc<T>() where T : PoolItem, new() { |
|||
|
|||
if (_debugFlag) { |
|||
if (_allocTick >= 5000) { |
|||
showDebugInfo(); |
|||
_allocTick = 0; |
|||
} |
|||
|
|||
_allocTick++; |
|||
} |
|||
|
|||
if (!poolDict.ContainsKey(typeof(T))) { |
|||
var pool = new List<PoolItem>(128); |
|||
|
|||
for (int i = 0; i < 128; i++) { |
|||
var item = new T(); |
|||
item.setup(); |
|||
|
|||
pool.Add(item); |
|||
} |
|||
|
|||
poolDict[typeof(T)] = pool; |
|||
|
|||
if (_debugFlag) { |
|||
debugInfo[typeof(T)] = new ItemDebugInfo {_watermark = 0, _size = 0}; |
|||
} |
|||
} |
|||
|
|||
var curPool = poolDict[typeof(T)]; |
|||
if (curPool.Count == 0) { |
|||
for (int i = 0; i < 128; i++) { |
|||
var item = new T(); |
|||
item.setup(); |
|||
|
|||
curPool.Add(item); |
|||
} |
|||
} |
|||
|
|||
var removeIdx = curPool.Count - 1; |
|||
var curItem = curPool[curPool.Count - 1]; |
|||
curPool.RemoveAt(removeIdx); |
|||
|
|||
if (_debugFlag) { |
|||
debugInfo[typeof(T)].consume(); |
|||
} |
|||
|
|||
curItem.activate(); |
|||
return (T)curItem; |
|||
} |
|||
|
|||
public static void recycle<T>(T item) where T : PoolItem { |
|||
var typeofT = item.GetType(); |
|||
D.assert(poolDict.ContainsKey(typeofT)); |
|||
|
|||
poolDict[typeofT].Add(item); |
|||
|
|||
if (_debugFlag) { |
|||
debugInfo[typeofT].recycle(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public abstract class PoolItem { |
|||
//ensure that base class has a empty constructor
|
|||
bool __activated_flag; |
|||
|
|||
public PoolItem() { |
|||
|
|||
} |
|||
|
|||
public void activate() { |
|||
this.__activated_flag = true; |
|||
} |
|||
|
|||
public virtual void setup() { |
|||
|
|||
} |
|||
|
|||
public virtual void clear() { |
|||
|
|||
} |
|||
|
|||
public void dispose() { |
|||
if (!this.__activated_flag) { |
|||
Debug.Assert(false, "an item has been recycled more than once !"); |
|||
return; |
|||
} |
|||
|
|||
this.clear(); |
|||
this.recycle(); |
|||
this.__activated_flag = false; |
|||
} |
|||
|
|||
public void recycle() { |
|||
ItemPoolManager.recycle(this); |
|||
} |
|||
} |
|||
|
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue