浏览代码

refine poolitem => poolobject

eliminate the use of dictionary/list in poolitemmanager
/main
xingwei.zhu 5 年前
当前提交
9ac5dbe6
共有 20 个文件被更改,包括 398 次插入302 次删除
  1. 34
      Runtime/ui/painting/txt/mesh_generator.cs
  2. 2
      Runtime/ui/utils/renderer/allocUtils/generic_list.cs
  3. 2
      Runtime/ui/utils/renderer/cmdbufferCanvas/command_buffer_canvas.cs
  4. 26
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_clip.cs
  5. 42
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  6. 16
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_shader.cs
  7. 2
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_shader_utils.cs
  8. 20
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs
  9. 28
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/render_cmd.cs
  10. 12
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/render_layer.cs
  11. 2
      Runtime/ui/utils/renderer/common/base_canvas.cs
  12. 121
      Runtime/ui/utils/renderer/common/draw_cmd.cs
  13. 22
      Runtime/ui/utils/renderer/common/geometry/mesh_mesh.cs
  14. 24
      Runtime/ui/utils/renderer/common/geometry/path/path.cs
  15. 18
      Runtime/ui/utils/renderer/common/geometry/path/path_cache.cs
  16. 18
      Runtime/ui/utils/renderer/common/geometry/path/tessellation_generator.cs
  17. 10
      Runtime/ui/utils/renderer/common/picture.cs
  18. 68
      Runtime/ui/utils/renderer/allocUtils/debug.cs
  19. 87
      Runtime/ui/utils/renderer/allocUtils/pool_object.cs
  20. 146
      Runtime/ui/utils/renderer/allocUtils/pool_items.cs

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


using UnityEngine;
namespace Unity.UIWidgets.ui {
class MeshKey : PoolItem, IEquatable<MeshKey> {
class MeshKey : PoolObject, IEquatable<MeshKey> {
public long textBlobId;
public float scale;

public static MeshKey create(long textBlobId, float scale) {
var newKey = ItemPoolManager.alloc<MeshKey>();
var newKey = ObjectPool<MeshKey>.alloc();
newKey.textBlobId = textBlobId;
newKey.scale = scale;
return newKey;

}
}
class MeshInfo : PoolItem {
class MeshInfo : PoolObject {
public MeshKey key;
public long textureVersion;
public uiMeshMesh mesh;

}
public static MeshInfo create(MeshKey key, uiMeshMesh mesh, long textureVersion, int timeToLive = 5) {
var meshInfo = ItemPoolManager.alloc<MeshInfo>();
var meshInfo = ObjectPool<MeshInfo>.alloc();
meshInfo.mesh = mesh;
meshInfo.key = key;
meshInfo.textureVersion = textureVersion;

public override void clear() {
this.key.dispose();
this.mesh.dispose();
ObjectPool<MeshKey>.release(this.key);
ObjectPool<uiMeshMesh>.release(this.mesh);
}
public long timeToLive {

}
class TextBlobMesh : PoolItem {
class TextBlobMesh : PoolObject {
static readonly Dictionary<MeshKey, MeshInfo> _meshes = new Dictionary<MeshKey, MeshInfo>();
static long _frameCount = 0;

public override void clear() {
this.textBlob = null;
this._mesh?.dispose();
ObjectPool<uiMeshMesh>.release(this._mesh);
TextBlobMesh newMesh = ItemPoolManager.alloc<TextBlobMesh>();
TextBlobMesh newMesh = ObjectPool<TextBlobMesh>.alloc();
newMesh.textBlob = textBlob;
newMesh.scale = scale;
newMesh.matrix = matrix;

foreach (var key in _keysToRemove)
{
_meshes[key].dispose();
ObjectPool<MeshInfo>.release(_meshes[key]);
_meshes.Remove(key);
}
_keysToRemove.Clear();

_meshes.TryGetValue(key, out var meshInfo);
if (meshInfo != null && meshInfo.textureVersion == fontInfo.textureVersion) {
key.dispose();
ObjectPool<MeshKey>.release(key);
meshInfo.touch();
this._mesh = meshInfo.mesh.transform(this.matrix);
return this._mesh;

var text = this.textBlob.text;
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * this.scale);
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var vertices = ObjectPool<uiList<Vector3>>.alloc();
var triangles = ItemPoolManager.alloc<uiList<int>>();
var triangles = ObjectPool<uiList<int>>.alloc();
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
var uv = ObjectPool<uiList<Vector2>>.alloc();
uv.SetCapacity(length * 4);
for (int charIndex = 0; charIndex < length; ++charIndex) {

if (vertices.Count == 0) {
this._mesh = null;
vertices.dispose();
uv.dispose();
triangles.dispose();
ObjectPool<uiList<Vector3>>.release(vertices);
ObjectPool<uiList<Vector2>>.release(uv);
ObjectPool<uiList<int>>.release(triangles);
return null;
}

2
Runtime/ui/utils/renderer/allocUtils/generic_list.cs


namespace Unity.UIWidgets.ui {
public class uiList<T> : PoolItem {
public class uiList<T> : PoolObject {
List<T> list;
public override void setup() {

2
Runtime/ui/utils/renderer/cmdbufferCanvas/command_buffer_canvas.cs


var picture = this._recorder.endRecording();
this._flusher.flush(picture);
this._recorder.reset();
picture.dispose();
ObjectPool<uiPicture>.release(picture);
}
public void dispose() {

26
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_clip.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
class ClipElement : PoolItem {
class ClipElement : PoolObject {
public int saveCount;
public uiMeshMesh mesh;
public bool convex;

}
public override void clear() {
this.mesh.dispose();
ObjectPool<uiMeshMesh>.release(this.mesh);
this.saveCount = 0;
this.mesh = null;
this.convex = false;

}
public static ClipElement create(int saveCount, uiPath uiPath, uiMatrix3 matrix, float scale) {
ClipElement newElement = ItemPoolManager.alloc<ClipElement>();
ClipElement newElement = ObjectPool<ClipElement>.alloc();
newElement.saveCount = saveCount;

}
}
class ClipStack : PoolItem {
class ClipStack : PoolObject {
static uint _genId = wideOpenGenID;
public static uint getNextGenID() {

int _saveCount;
public static ClipStack create() {
return ItemPoolManager.alloc<ClipStack>();
return ObjectPool<ClipStack>.alloc();
}
public override void clear() {

clipelement.dispose();
ObjectPool<ClipElement>.release(clipelement);
}
this.stack.Clear();

}
var lastelement = this.stack[this.stack.Count - 1];
lastelement.dispose();
ObjectPool<ClipElement>.release(lastelement);
this.stack.RemoveAt(this.stack.Count - 1);
this._lastElement = this.stack.Count == 0 ? null : this.stack[this.stack.Count - 1];

ClipElement prior = this._lastElement;
if (prior != null) {
if (prior.isEmpty()) {
element.dispose();
ObjectPool<ClipElement>.release(element);
return;
}

var isectRect = uiRectHelper.intersect(prior.rect.Value, element.rect.Value);
if (isectRect.isEmpty) {
prior.setEmpty();
element.dispose();
ObjectPool<ClipElement>.release(element);
return;
}

element.dispose();
ObjectPool<ClipElement>.release(element);
element.dispose();
ObjectPool<ClipElement>.release(element);
return;
}
}

}
}
class ReducedClip : PoolItem {
class ReducedClip : PoolObject {
public uiRect? scissor;
public List<ClipElement> maskElements = new List<ClipElement>();
ClipElement _lastElement;

}
public static ReducedClip create(ClipStack stack, uiRect layerBounds, uiRect queryBounds) {
ReducedClip clip = ItemPoolManager.alloc<ReducedClip>();
ReducedClip clip = ObjectPool<ReducedClip>.alloc();
uiRect? stackBounds;
bool iior;
stack.getBounds(out stackBounds, out iior);

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


public void dispose() {
if (this._currentLayer != null) {
this._clearLayer(this._currentLayer);
this._currentLayer.dispose();
ObjectPool<RenderLayer>.release(this._currentLayer);
this._currentLayer = null;
this._lastScissor = null;
this._layers.Clear();

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

var layer = this._currentLayer;
D.assert(layer.states.Count > 0);
if (layer.states.Count > 1) {
layer.states[layer.states.Count - 1].dispose();
ObjectPool<State>.release(layer.states[layer.states.Count - 1]);
layer.states.RemoveAt(layer.states.Count - 1);
layer.currentState = layer.states[layer.states.Count - 1];
layer.clipStack.restore();

var mesh = ImageMeshGenerator.imageMesh(state.matrix, uiRectHelper.one, layer.layerBounds);
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
return;
}

var path = uiPath.create();
path.addRect(uiRectHelper.fromRect(rect));
this._clipPath(path);
path.dispose();
ObjectPool<uiPath>.release(path);
}
void _clipUIRect(uiRect rect) {

path.dispose();
ObjectPool<uiPath>.release(path);
}
void _clipRRect(RRect rrect) {

path.dispose();
ObjectPool<uiPath>.release(path);
}
void _clipPath(uiPath path) {

var layerBounds = layer.layerBounds;
ReducedClip reducedClip = ReducedClip.create(layer.clipStack, layerBounds, queryBounds.Value);
if (reducedClip.isEmpty()) {
reducedClip.dispose();
ObjectPool<ReducedClip>.release(reducedClip);
return false;
}

deviceScissor = uiRectHelper.intersect(deviceScissor, physicalRect);
if (deviceScissor.isEmpty) {
reducedClip.dispose();
ObjectPool<ReducedClip>.release(reducedClip);
return false;
}

this._setLastClipGenId(maskGenID, reducedClip.scissor.Value);
}
reducedClip.dispose();
ObjectPool<ReducedClip>.release(reducedClip);
return true;
}

var blurMesh = ImageMeshGenerator.imageMesh(null, uiRectHelper.one, maskBounds);
if (!this._applyClip(blurMesh.bounds)) {
blurMesh.dispose();
ObjectPool<uiMeshMesh>.release(blurMesh);
return;
}

void _drawPathDrawMeshCallback(uiPaint p, uiMeshMesh mesh, bool convex, float alpha, Texture tex, uiRect textBlobBounds, TextBlobMesh textMesh) {
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
return;
}

void _drawPathDrawMeshCallback2(uiPaint p, uiMeshMesh mesh, bool convex, float alpha, Texture tex, uiRect textBlobBounds, TextBlobMesh textMesh) {
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
return;
}

void _drawTextDrawMeshCallback(uiPaint p, uiMeshMesh mesh, bool convex, float alpha, Texture tex, uiRect textBlobBounds, TextBlobMesh textMesh) {
if (!this._applyClip(textBlobBounds)) {
textMesh.dispose();
ObjectPool<TextBlobMesh>.release(textMesh);
return;
}

void _drawPathDrawMeshQuit(uiMeshMesh mesh) {
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
}
void _drawPath(uiPath path, uiPaint paint) {

var state = layer.currentState;
var mesh = ImageMeshGenerator.imageMesh(state.matrix, src.Value, dst);
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
return;
}

var mesh = ImageMeshGenerator.imageNineMesh(state.matrix, src.Value, center, image.width, image.height, dst);
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
return;
}

case DrawClipPath cmd: {
var uipath = uiPath.fromPath(cmd.path);
this._clipPath(uipath);
uipath.dispose();
ObjectPool<uiPath>.release(uipath);
uipath.dispose();
ObjectPool<uiPath>.release(uipath);
break;
}
case DrawImage cmd: {

break;
}
cmdObj.dispose();
cmdObj.release();
}
layer.draws.Clear();

subLayer.dispose();
ObjectPool<RenderLayer>.release(subLayer);
}
layer.layers.Clear();

16
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_shader.cs


out int pass, out MaterialPropertyBlockWrapper props) {
Vector4 viewport = layer.viewport;
props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
props.SetFloat(_alphaId, alpha);

var mat = _fill0Mat.getMaterial(layer.ignoreClip);
var pass = 0;
var props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
return PictureFlusher.CmdDraw.create(

properties : props
);
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
return ret;
}

var mat = _stroke1Mat;
var pass = 0;
var props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
return PictureFlusher.CmdDraw.create(

var mat = _stencilMat;
var pass = 0;
var props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
return PictureFlusher.CmdDraw.create(

var mat = _stencilMat;
var pass = 1;
var props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
return PictureFlusher.CmdDraw.create(

var mat = _stencilMat;
var pass = 2;
var props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
return PictureFlusher.CmdDraw.create(

var mat = _filterMat;
var pass = 0;
var props = ItemPoolManager.alloc<MaterialPropertyBlockWrapper>();
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
props.SetVector(_viewportId, viewport);
props.SetFloat(_mfRadiusId, radius);

2
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_shader_utils.cs


namespace Unity.UIWidgets.ui {
public class MaterialPropertyBlockWrapper : PoolItem {
public class MaterialPropertyBlockWrapper : PoolObject {
public readonly MaterialPropertyBlock mpb;
public MaterialPropertyBlockWrapper() {

20
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs


using System;
using System.Runtime.CompilerServices;
using Unity.UIWidgets.foundation;
using UnityEngine;

public static uiMeshMesh imageMesh(uiMatrix3? matrix,
uiOffset srcTL, uiOffset srcBL, uiOffset srcBR, uiOffset srcTR,
uiRect dst) {
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var vertices = ObjectPool<uiList<Vector3>>.alloc();
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
var uv = ObjectPool<uiList<Vector2>>.alloc();
uv.SetCapacity(4);
vertices.Add(new Vector2(dst.left, dst.top));

vertices.Add(new Vector2(dst.right, dst.top));
uv.Add(new Vector2(srcTR.dx, 1.0f - srcTR.dy));
var _triangles = ItemPoolManager.alloc<uiList<int>>();
var _triangles = ObjectPool<uiList<int>>.alloc();
_triangles.AddRange(_imageTriangles);
return uiMeshMesh.create(matrix, vertices, _triangles, uv);

var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var vertices = ObjectPool<uiList<Vector3>>.alloc();
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
var uv = ObjectPool<uiList<Vector2>>.alloc();
uv.SetCapacity(4);
float uvx0 = src.left;

vertices.Add(new Vector2(dst.right, dst.top));
uv.Add(new Vector2(uvx1, uvy0));
var _triangles = ItemPoolManager.alloc<uiList<int>>();
var _triangles = ObjectPool<uiList<int>>.alloc();
_triangles.AddRange(_imageTriangles);
return uiMeshMesh.create(matrix, vertices, _triangles, uv);

float ty2 = 1 - center.bottom;
float ty3 = 1 - src.bottom;
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var vertices = ObjectPool<uiList<Vector3>>.alloc();
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
var uv = ObjectPool<uiList<Vector2>>.alloc();
uv.SetCapacity(16);
vertices.Add(new Vector2(x0, y0));

vertices.Add(new Vector2(x3, y3));
uv.Add(new Vector2(tx3, ty3));
var _triangles = ItemPoolManager.alloc<uiList<int>>();
var _triangles = ObjectPool<uiList<int>>.alloc();
_triangles.AddRange(_imageNineTriangles);
return uiMeshMesh.create(matrix, vertices, _triangles, uv);

28
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/render_cmd.cs


namespace Unity.UIWidgets.ui {
public partial class PictureFlusher {
internal abstract class RenderCmd : PoolItem {
internal abstract class RenderCmd : PoolObject {
public abstract void release();
}
internal class CmdLayer : RenderCmd {

}
public static CmdLayer create(RenderLayer layer) {
CmdLayer newCmd = ItemPoolManager.alloc<CmdLayer>();
CmdLayer newCmd = ObjectPool<CmdLayer>.alloc();
public override void release() {
ObjectPool<CmdLayer>.release(this);
}
}
internal class CmdDraw : RenderCmd {

public override void clear() {
this.mesh?.dispose();
this.textMesh?.dispose();
this.properties?.dispose();
ObjectPool<uiMeshMesh>.release(this.mesh);
ObjectPool<TextBlobMesh>.release(this.textMesh);
ObjectPool<MaterialPropertyBlockWrapper>.release(this.properties);
}
public CmdDraw() {

MaterialPropertyBlockWrapper properties = null, int? layerId = null, Material material = null,
Image image = null, Mesh meshObj = null,
bool meshObjCreated = false) {
CmdDraw newCmd = ItemPoolManager.alloc<CmdDraw>();
CmdDraw newCmd = ObjectPool<CmdDraw>.alloc();
newCmd.mesh = mesh;
newCmd.textMesh = textMesh;
newCmd.pass = pass;

return newCmd;
}
public override void release() {
ObjectPool<CmdDraw>.release(this);
}
}
internal class CmdScissor : RenderCmd {

}
public static CmdScissor create(uiRect? deviceScissor) {
CmdScissor newCmd = ItemPoolManager.alloc<CmdScissor>();
CmdScissor newCmd = ObjectPool<CmdScissor>.alloc();
}
public override void release() {
ObjectPool<CmdScissor>.release(this);
}
}
}

12
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/render_layer.cs


namespace Unity.UIWidgets.ui {
public partial class PictureFlusher {
internal class RenderLayer : PoolItem {
internal class RenderLayer : PoolObject {
public int rtID;
public int width;
public int height;

public static RenderLayer create(int rtID = 0, int width = 0, int height = 0, FilterMode filterMode = FilterMode.Point,
bool noMSAA = false, uiRect? layerBounds = null, uiPaint? layerPaint = null, bool ignoreClip = true) {
D.assert(layerBounds != null);
var newLayer = ItemPoolManager.alloc<RenderLayer>();
var newLayer = ObjectPool<RenderLayer>.alloc();
newLayer.rtID = rtID;
newLayer.width = width;
newLayer.height = height;

this.layers.Clear();
foreach (var state in this.states) {
state.dispose();
ObjectPool<State>.release(state);
this.clipStack.dispose();
ObjectPool<ClipStack>.release(this.clipStack);
internal class State : PoolItem {
internal class State : PoolObject {
public State() {

uiMatrix3? _invMatrix;
public static State create(uiMatrix3? matrix = null, float? scale = null, uiMatrix3? invMatrix = null) {
State newState = ItemPoolManager.alloc<State>();
State newState = ObjectPool<State>.alloc();
newState._matrix = matrix ?? _id;
newState._scale = scale;
newState._invMatrix = invMatrix;

2
Runtime/ui/utils/renderer/common/base_canvas.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
public class uiRecorderCanvas : PoolItem, Canvas {
public class uiRecorderCanvas : Canvas {
public uiRecorderCanvas(uiPictureRecorder recorder) {
this._recorder = recorder;
}

121
Runtime/ui/utils/renderer/common/draw_cmd.cs


using System.Runtime.CompilerServices;
namespace Unity.UIWidgets.ui {
public abstract class uiDrawCmd : PoolItem {
public abstract class uiDrawCmd : PoolObject {
public abstract void release();
}
public class uiDrawSave : uiDrawCmd {

public static uiDrawSave create() {
var drawSave = ItemPoolManager.alloc<uiDrawSave>();
var drawSave = ObjectPool<uiDrawSave>.alloc();
public override void release() {
ObjectPool<uiDrawSave>.release(this);
}
}
public class uiDrawSaveLayer : uiDrawCmd {

public static uiDrawSaveLayer create(uiRect? rect, uiPaint paint) {
var drawSaveLayer = ItemPoolManager.alloc<uiDrawSaveLayer>();
var drawSaveLayer = ObjectPool<uiDrawSaveLayer>.alloc();
public override void release() {
ObjectPool<uiDrawSaveLayer>.release(this);
}
public override void clear() {
this.rect = null;

}
public static uiDrawRestore create() {
var drawRestore = ItemPoolManager.alloc<uiDrawRestore>();
var drawRestore = ObjectPool<uiDrawRestore>.alloc();
public override void release() {
ObjectPool<uiDrawRestore>.release(this);
}
}
public class uiDrawTranslate : uiDrawCmd {

public static uiDrawTranslate create(float dx, float dy) {
var drawTranslate = ItemPoolManager.alloc<uiDrawTranslate>();
var drawTranslate = ObjectPool<uiDrawTranslate>.alloc();
public override void release() {
ObjectPool<uiDrawTranslate>.release(this);
}
public float dx;
public float dy;
}

}
public static uiDrawScale create(float sx, float? sy) {
var drawScale = ItemPoolManager.alloc<uiDrawScale>();
var drawScale = ObjectPool<uiDrawScale>.alloc();
public override void release() {
ObjectPool<uiDrawScale>.release(this);
}
public float sx;
public float? sy;
}

}
public static uiDrawRotate create(float radians, uiOffset? offset) {
var drawRotate = ItemPoolManager.alloc<uiDrawRotate>();
var drawRotate = ObjectPool<uiDrawRotate>.alloc();
}
public override void release() {
ObjectPool<uiDrawRotate>.release(this);
}
public override void clear() {

}
public static uiDrawSkew create(float sx, float sy) {
var drawSkew = ItemPoolManager.alloc<uiDrawSkew>();
var drawSkew = ObjectPool<uiDrawSkew>.alloc();
}
public override void release() {
ObjectPool<uiDrawSkew>.release(this);
}
public float sx;

}
public static uiDrawConcat create(uiMatrix3? matrix) {
var drawConcat = ItemPoolManager.alloc<uiDrawConcat>();
var drawConcat = ObjectPool<uiDrawConcat>.alloc();
}
public override void release() {
ObjectPool<uiDrawConcat>.release(this);
}
public override void clear() {

}
public static uiDrawResetMatrix create() {
var drawResetMatrix = ItemPoolManager.alloc<uiDrawResetMatrix>();
var drawResetMatrix = ObjectPool<uiDrawResetMatrix>.alloc();
public override void release() {
ObjectPool<uiDrawResetMatrix>.release(this);
}
}
public class uiDrawSetMatrix : uiDrawCmd {

public static uiDrawSetMatrix create(uiMatrix3? matrix) {
var drawSetMatrix = ItemPoolManager.alloc<uiDrawSetMatrix>();
var drawSetMatrix = ObjectPool<uiDrawSetMatrix>.alloc();
public override void release() {
ObjectPool<uiDrawSetMatrix>.release(this);
}
public override void clear() {
this.matrix = null;
}

}
public static uiDrawClipRect create(uiRect? rect) {
var drawClipRect = ItemPoolManager.alloc<uiDrawClipRect>();
var drawClipRect = ObjectPool<uiDrawClipRect>.alloc();
}
public override void release() {
ObjectPool<uiDrawClipRect>.release(this);
}
public override void clear() {

}
public static uiDrawClipRRect create(RRect rrect) {
var drawClipRRect = ItemPoolManager.alloc<uiDrawClipRRect>();
var drawClipRRect = ObjectPool<uiDrawClipRRect>.alloc();
}
public override void release() {
ObjectPool<uiDrawClipRRect>.release(this);
}
public override void clear() {

}
public static uiDrawClipPath create(uiPath path) {
var drawClipPath = ItemPoolManager.alloc<uiDrawClipPath>();
var drawClipPath = ObjectPool<uiDrawClipPath>.alloc();
public override void release() {
ObjectPool<uiDrawClipPath>.release(this);
}
this.path.dispose();
ObjectPool<uiPath>.release(this.path);
this.path = null;
}

}
public static uiDrawPath create(uiPath path, uiPaint paint) {
var drawPath = ItemPoolManager.alloc<uiDrawPath>();
var drawPath = ObjectPool<uiDrawPath>.alloc();
public override void release() {
ObjectPool<uiDrawPath>.release(this);
}
this.path.dispose();
ObjectPool<uiPath>.release(this.path);
this.path = null;
}

}
public static uiDrawImage create(Image image, uiOffset? offset, uiPaint paint) {
var drawImage = ItemPoolManager.alloc<uiDrawImage>();
var drawImage = ObjectPool<uiDrawImage>.alloc();
}
public override void release() {
ObjectPool<uiDrawImage>.release(this);
}
public override void clear() {

}
public static uiDrawImageRect create(Image image, uiRect? src, uiRect? dst, uiPaint paint) {
var drawImageRect = ItemPoolManager.alloc<uiDrawImageRect>();
var drawImageRect = ObjectPool<uiDrawImageRect>.alloc();
drawImageRect.image = image;
drawImageRect.src = src;
drawImageRect.dst = dst;

public override void release() {
ObjectPool<uiDrawImageRect>.release(this);
}
public override void clear() {
this.image = null;

}
public static uiDrawImageNine create(Image image, uiRect? src, uiRect? center, uiRect? dst, uiPaint paint) {
var drawImageNine = ItemPoolManager.alloc<uiDrawImageNine>();
var drawImageNine = ObjectPool<uiDrawImageNine>.alloc();
drawImageNine.image = image;
drawImageNine.src = src;
drawImageNine.center = center;

}
public override void release() {
ObjectPool<uiDrawImageNine>.release(this);
}
public override void clear() {
this.image = null;

}
public static uiDrawPicture create(Picture picture) {
var drawPicture = ItemPoolManager.alloc<uiDrawPicture>();
var drawPicture = ObjectPool<uiDrawPicture>.alloc();
public override void release() {
ObjectPool<uiDrawPicture>.release(this);
}
public override void clear() {
this.picture = null;

}
public static uiDrawTextBlob create(TextBlob textBlob, uiOffset? offset, uiPaint paint) {
var drawTextBlob = ItemPoolManager.alloc<uiDrawTextBlob>();
var drawTextBlob = ObjectPool<uiDrawTextBlob>.alloc();
}
public override void release() {
ObjectPool<uiDrawTextBlob>.release(this);
}
public override void clear() {

22
Runtime/ui/utils/renderer/common/geometry/mesh_mesh.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
class uiMeshMesh : PoolItem {
class uiMeshMesh : PoolObject {
public uiList<Vector3> vertices;
public uiList<int> triangles;
public uiList<Vector2> uv;

}
public override void clear() {
this.vertices?.dispose();
this.triangles?.dispose();
this.uv?.dispose();
ObjectPool<uiList<Vector3>>.release(this.vertices);
ObjectPool<uiList<int>>.release(this.triangles);
ObjectPool<uiList<Vector2>>.release(this.uv);
this.vertices = null;
this.triangles = null;
this.uv = null;

public static uiMeshMesh create(uiRect rect) {
uiMeshMesh newMesh = ItemPoolManager.alloc<uiMeshMesh>();
uiMeshMesh newMesh = ObjectPool<uiMeshMesh>.alloc();
newMesh.vertices = ItemPoolManager.alloc<uiList<Vector3>>();
newMesh.vertices = ObjectPool<uiList<Vector3>>.alloc();
newMesh.triangles = ItemPoolManager.alloc<uiList<int>>();
newMesh.triangles = ObjectPool<uiList<int>>.alloc();
newMesh.triangles.AddRange(_boundsTriangles);
newMesh.rawBounds = rect;

D.assert(triangles.Count >= 0);
D.assert(uv == null || uv.Count == vertices.Count);
uiMeshMesh newMesh = ItemPoolManager.alloc<uiMeshMesh>();
uiMeshMesh newMesh = ObjectPool<uiMeshMesh>.alloc();
newMesh.matrix = matrix;
newMesh.vertices = vertices;
newMesh.triangles = triangles;

}
public uiMeshMesh transform(uiMatrix3? matrix) {
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var vertices = ObjectPool<uiList<Vector3>>.alloc();
var triangles = ItemPoolManager.alloc<uiList<int>>();
var triangles = ObjectPool<uiList<int>>.alloc();
triangles.SetCapacity(this.triangles.Count);
triangles.AddRange(this.triangles.data);

if (this.uv != null) {
uv = ItemPoolManager.alloc<uiList<Vector2>>();
uv = ObjectPool<uiList<Vector2>>.alloc();
uv.SetCapacity(this.uv.Count);
uv.AddRange(this.uv.data);
}

24
Runtime/ui/utils/renderer/common/geometry/path/path.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
public class uiPath : PoolItem {
public class uiPath : PoolObject {
const float _KAPPA90 = 0.5522847493f;
uiList<float> _commands;

uiPathCache _cache;
public static uiPath create(int capacity = 128) {
uiPath newPath = ItemPoolManager.alloc<uiPath>();
uiPath newPath = ObjectPool<uiPath>.alloc();
newPath._reset();
return newPath;
}

public override void clear() {
this._commands.dispose();
this._cache?.dispose();
ObjectPool<uiList<float>>.release(this._commands);
ObjectPool<uiPathCache>.release(this._cache);
this._commands = ItemPoolManager.alloc<uiList<float>>();
this._commands = ObjectPool<uiList<float>>.alloc();
this._commandx = 0;
this._commandy = 0;
this._minX = float.MaxValue;

this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = null;
}

}
_cache.normalize();
this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = _cache;
return _cache;
}

this._commandx = x;
this._commandy = y;
this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = null;
}

this._commandx = x;
this._commandy = y;
this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = null;
}

this._commandx = x3;
this._commandy = y3;
this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = null;
}

this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = null;
}

this._cache?.dispose();
ObjectPool<uiPathCache>.release(this._cache);
this._cache = null;
}

18
Runtime/ui/utils/renderer/common/geometry/path/path_cache.cs


namespace Unity.UIWidgets.ui {
class uiPathCache : PoolItem {
class uiPathCache : PoolObject {
float _distTol;
float _tessTol;

float _miterLimit;
public static uiPathCache create(float scale) {
uiPathCache newPathCache = ItemPoolManager.alloc<uiPathCache>();
uiPathCache newPathCache = ObjectPool<uiPathCache>.alloc();
newPathCache._distTol = 0.01f / scale;
newPathCache._tessTol = 0.25f / scale;
newPathCache._scale = scale;

public override void clear() {
this._paths.Clear();
this._points.Clear();
this._fillMesh?.dispose();
ObjectPool<uiMeshMesh>.release(this._fillMesh);
this._strokeMesh?.dispose();
ObjectPool<uiMeshMesh>.release(this._strokeMesh);
this._strokeMesh = null;
}

cvertices += path.count;
}
var _vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var _vertices = ObjectPool<uiList<Vector3>>.alloc();
_vertices.SetCapacity(cvertices);
for (var i = 0; i < paths.Count; i++) {
var path = paths[i];

}
}
var indices = ItemPoolManager.alloc<uiList<int>>();
var indices = ObjectPool<uiList<int>>.alloc();
indices.SetCapacity(cindices);
for (var i = 0; i < paths.Count; i++) {
var path = paths[i];

cvertices += 4;
}
var _vertices = ItemPoolManager.alloc<uiList<Vector3>>();
var _vertices = ObjectPool<uiList<Vector3>>.alloc();
_vertices.SetCapacity(cvertices);
for (var i = 0; i < paths.Count; i++) {
var path = paths[i];

}
}
var indices = ItemPoolManager.alloc<uiList<int>>();
var indices = ObjectPool<uiList<int>>.alloc();
indices.SetCapacity(cindices);
for (var i = 0; i < paths.Count; i++) {
var path = paths[i];

D.assert(indices.Count == cindices);
this._strokeMesh?.dispose();
ObjectPool<uiMeshMesh>.release(this._strokeMesh);
this._strokeMesh = uiMeshMesh.create(null, vertices, indices);
this._strokeWidth = strokeWidth;
this._lineCap = lineCap;

18
Runtime/ui/utils/renderer/common/geometry/path/tessellation_generator.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
class uiTessellationKey : PoolItem, IEquatable<uiTessellationKey> {
class uiTessellationKey : PoolObject, IEquatable<uiTessellationKey> {
public float x2;
public float y2;
public float x3;

public static uiTessellationKey create(float x1, float y1, float x2, float y2, float x3, float y3, float x4,
float y4,
float tessTol) {
var newKey = ItemPoolManager.alloc<uiTessellationKey>();
var newKey = ObjectPool<uiTessellationKey>.alloc();
newKey.x2 = x2 - x1;
newKey.y2 = y2 - y1;
newKey.x3 = x3 - x1;

}
}
class uiTessellationInfo : PoolItem {
class uiTessellationInfo : PoolObject {
var newInfo = ItemPoolManager.alloc<uiTessellationInfo>();
var newInfo = ObjectPool<uiTessellationInfo>.alloc();
newInfo.points = points;
newInfo.key = key;
newInfo.touch(timeToLive);

}
public override void clear() {
this.points.dispose();
ObjectPool<uiList<Vector2>>.release(this.points);
}
public long timeToLive {

var keysToRemove = _tessellations.Values.Where(info => info.timeToLive < _frameCount)
.Select(info => info.key).ToList();
foreach (var key in keysToRemove) {
key.dispose();
_tessellations[key].dispose();
ObjectPool<uiTessellationKey>.release(key);
ObjectPool<uiTessellationInfo>.release(_tessellations[key]);
_tessellations.Remove(key);
}
}

_tessellations.TryGetValue(key, out var uiTessellationInfo);
if (uiTessellationInfo != null) {
key.dispose();
ObjectPool<uiTessellationKey>.release(key);
uiTessellationInfo.touch();
return uiTessellationInfo.points;
}

x4 = x4 - x1;
y4 = y4 - y1;
var points = ItemPoolManager.alloc<uiList<Vector2>>();
var points = ObjectPool<uiList<Vector2>>.alloc();
_stack.Clear();
_stack.Push(new _StackData {

10
Runtime/ui/utils/renderer/common/picture.cs


namespace Unity.UIWidgets.ui {
public class uiPicture : PoolItem {
public class uiPicture : PoolObject {
var picture = ItemPoolManager.alloc<uiPicture>();
var picture = ObjectPool<uiPicture>.alloc();
picture.drawCmds = drawCmds;
picture.paintBounds = paintBounds;
return picture;

public void reset() {
foreach (var drawCmd in this._drawCmds) {
drawCmd.dispose();
drawCmd.release();
}
this._drawCmds.Clear();

var rect = transformedMesh.bounds;
state.scissor = state.scissor == null ? rect : state.scissor.Value.intersect(rect);
this._setState(state);
transformedMesh.dispose();
ObjectPool<uiMeshMesh>.release(transformedMesh);
break;
}
case uiDrawPath cmd: {

this._addPaintBounds(mesh.bounds);
}
mesh.dispose();
ObjectPool<uiMeshMesh>.release(mesh);
break;
}
case uiDrawImage cmd: {

68
Runtime/ui/utils/renderer/allocUtils/debug.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();
}
}
}

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


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;
}
}
}
}

146
Runtime/ui/utils/renderer/allocUtils/pool_items.cs


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);
}
}
}
正在加载...
取消
保存