浏览代码

more object => PoolItem

/main
xingwei.zhu 6 年前
当前提交
721d02ef
共有 5 个文件被更改,包括 293 次插入151 次删除
  1. 131
      Runtime/ui/painting/canvas_impl.cs
  2. 152
      Runtime/ui/painting/canvas_shader.cs
  3. 48
      Runtime/ui/painting/txt/mesh_generator.cs
  4. 7
      Runtime/ui/utils/basic_types/generic_list.cs
  5. 106
      Runtime/ui/utils/basic_types/path.cs

131
Runtime/ui/painting/canvas_impl.cs


return;
}
layer.draws.Add(new CmdScissor {
deviceScissor = scissor,
});
layer.draws.Add(CmdScissor.create(
deviceScissor : scissor
));
this._lastScissor = scissor;
}

// need to inflate a bit to make sure all area is cleared.
var inflatedScissor = reducedClip.scissor.inflate(this._fringeWidth);
var boundsMesh = new uiMeshMesh(inflatedScissor);
var boundsMesh = uiMeshMesh.create(inflatedScissor);
layer.draws.Add(CanvasShader.stencilClear(layer, boundsMesh));
foreach (var maskElement in reducedClip.maskElements) {

path.dispose();
bool convex;
var mesh = cache.getFillMesh(out convex).transform(state.matrix);
var fillMesh = cache.getFillMesh(out convex);
var mesh = fillMesh.transform(state.matrix);
fillMesh.dispose();
cache.dispose();
Action<Paint> drawMesh = p => {

var cache = path.flatten(state.scale * this._devicePixelRatio);
path.dispose();
var mesh = cache.getStrokeMesh(
var strokenMesh = cache.getStrokeMesh(
paint.strokeMiterLimit).transform(state.matrix);
paint.strokeMiterLimit);
var mesh = strokenMesh.transform(state.matrix);
strokenMesh.dispose();
cache.dispose();
Action<Paint> drawMesh = p => {

var matrix = new uiMatrix3(state.matrix);
matrix.preTranslate(offset.dx, offset.dy);
var mesh = new TextBlobMesh(textBlob, scale, matrix);
var mesh = TextBlobMesh.create(textBlob, scale, matrix);
var textBlobBounds = matrix.mapRect(textBlob.boundsInText);
// request font texture so text mesh could be generated correctly

}
D.assert(mesh.vertices.Count > 0);
cmd.meshObj.SetVertices(mesh.vertices);
cmd.meshObj.SetTriangles(mesh.triangles, 0, false);
cmd.meshObj.SetUVs(0, mesh.uv);
cmd.meshObj.SetVertices(mesh.vertices?.data);
cmd.meshObj.SetTriangles(mesh.triangles?.data, 0, false);
cmd.meshObj.SetUVs(0, mesh.uv?.data);
if (mesh.matrix == null) {
cmd.properties.SetFloatArray(CmdDraw.matId, CmdDraw.idMat3.fMat);

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

public bool noMSAA = false;
public Rect layerBounds;
public Paint layerPaint;
public readonly List<object> draws = new List<object>();
public readonly List<RenderCmd> draws = new List<RenderCmd>();
public readonly List<RenderLayer> layers = new List<RenderLayer>();
public readonly List<State> states = new List<State>();
public State currentState;

public void addLayer(RenderLayer layer) {
this.layers.Add(layer);
this.draws.Add(new CmdLayer {layer = layer});
this.draws.Add(CmdLayer.create(layer : layer));
}
public override void clear() {

return new State(this._matrix, this._scale, this._invMatrix);
}
}
internal abstract class RenderCmd : PoolItem {
}
internal class CmdLayer : PoolItem {
internal class CmdLayer : RenderCmd {
public CmdLayer() {
}
public override void clear() {
this.layer = null;
}
public static CmdLayer create(RenderLayer layer) {
CmdLayer newCmd = ItemPoolManager.alloc<CmdLayer>();
newCmd.layer = layer;
return newCmd;
}
internal class CmdDraw : PoolItem {
internal class CmdDraw : RenderCmd {
public uiMeshMesh mesh;
public TextBlobMesh textMesh;
public int pass;

public override void clear() {
this.mesh?.dispose();
}
public CmdDraw() {
}
public static CmdDraw create(uiMeshMesh mesh = null, TextBlobMesh textMesh = null, int pass = 0,
MaterialPropertyBlock properties = null, RenderLayer layer = null, Material material = null,
Image image = null, Mesh meshObj = null,
bool meshObjCreated = false) {
CmdDraw newCmd = ItemPoolManager.alloc<CmdDraw>();
newCmd.mesh = mesh;
newCmd.textMesh = textMesh;
newCmd.pass = pass;
newCmd.properties = properties;
newCmd.layer = layer;
newCmd.material = material;
newCmd.image = image;
newCmd.meshObj = meshObj;
newCmd.meshObjCreated = meshObjCreated;
return newCmd;
internal class CmdScissor {
internal class CmdScissor : RenderCmd {
public CmdScissor() {
}
public override void clear() {
this.deviceScissor = null;
}
public static CmdScissor create(Rect deviceScissor) {
CmdScissor newCmd = ItemPoolManager.alloc<CmdScissor>();
newCmd.deviceScissor = deviceScissor;
return newCmd;
}
}
}

0, 2, 3, 0, 3, 2,
};
static readonly List<int> _imageNineTriangles = new List<int>(12 * 9) {
static readonly List<int> _imageNineTriangles = new List<int> {
0, 4, 1, 1, 4, 5,
0, 1, 4, 1, 5, 4,
1, 5, 2, 2, 5, 6,

public static uiMeshMesh imageMesh(uiMatrix3 matrix,
Offset srcTL, Offset srcBL, Offset srcBR, Offset srcTR,
Rect dst) {
var vertices = new List<Vector3>(4);
var uv = new List<Vector2>(4);
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
vertices.SetCapacity(4);
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
uv.SetCapacity(4);
vertices.Add(new Vector2(dst.left, dst.top));
uv.Add(new Vector2(srcTL.dx, 1.0f - srcTL.dy));

vertices.Add(new Vector2(dst.right, dst.top));
uv.Add(new Vector2(srcTR.dx, 1.0f - srcTR.dy));
return new uiMeshMesh(matrix, vertices, _imageTriangles, uv);
var _triangles = ItemPoolManager.alloc<uiList<int>>();
_triangles.AddRange(_imageTriangles);
return uiMeshMesh.create(matrix, vertices, _triangles, uv);
var vertices = new List<Vector3>(4);
var uv = new List<Vector2>(4);
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
vertices.SetCapacity(4);
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
uv.SetCapacity(4);
float uvx0 = src.left;
float uvx1 = src.right;

uv.Add(new Vector2(uvx1, uvy1));
vertices.Add(new Vector2(dst.right, dst.top));
uv.Add(new Vector2(uvx1, uvy0));
var _triangles = ItemPoolManager.alloc<uiList<int>>();
_triangles.AddRange(_imageTriangles);
return new uiMeshMesh(matrix, vertices, _imageTriangles, uv);
return uiMeshMesh.create(matrix, vertices, _triangles, uv);
}
public static uiMeshMesh imageNineMesh(uiMatrix3 matrix, Rect src, Rect center, int srcWidth, int srcHeight, Rect dst) {

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

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

152
Runtime/ui/painting/canvas_shader.cs


var mat = _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, out var pass, out var props);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw fill0(PictureFlusher.RenderLayer layer, uiMeshMesh mesh) {

var props = new MaterialPropertyBlock();
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw fill1(PictureFlusher.RenderLayer layer, Paint paint,

return new PictureFlusher.CmdDraw {
mesh = mesh.boundsMesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh.boundsMesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw stroke0(PictureFlusher.RenderLayer layer, Paint paint,

return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw stroke1(PictureFlusher.RenderLayer layer, uiMeshMesh mesh) {

var props = new MaterialPropertyBlock();
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw stencilClear(

var props = new MaterialPropertyBlock();
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw stencil0(PictureFlusher.RenderLayer layer, uiMeshMesh mesh) {

var props = new MaterialPropertyBlock();
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw stencil1(PictureFlusher.RenderLayer layer, uiMeshMesh mesh) {

var props = new MaterialPropertyBlock();
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw tex(PictureFlusher.RenderLayer layer, Paint paint,

props.SetTexture(_texId, image.texture);
props.SetInt(_texModeId, image.texture is RenderTexture ? 1 : 0); // pre alpha if RT else post alpha
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
image = image, // keep a reference to avoid GC.
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props,
image : image // keep a reference to avoid GC.
);
}
public static PictureFlusher.CmdDraw texRT(PictureFlusher.RenderLayer layer, Paint paint,

props.SetInt(_texModeId, 1); // pre alpha
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
layer = renderLayer,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props,
layer : renderLayer
);
}
public static PictureFlusher.CmdDraw texAlpha(PictureFlusher.RenderLayer layer, Paint paint,

props.SetTexture(_texId, tex);
props.SetInt(_texModeId, 2); // alpha only
return new PictureFlusher.CmdDraw {
mesh = mesh,
textMesh = textMesh,
pass = pass,
material = mat,
properties = props,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
textMesh : textMesh,
pass : pass,
material : mat,
properties : props
);
}
public static PictureFlusher.CmdDraw maskFilter(PictureFlusher.RenderLayer layer, uiMeshMesh mesh,

props.SetVector(_mfImgIncId, imgInc);
props.SetFloatArray(_mfKernelId, kernel);
return new PictureFlusher.CmdDraw {
mesh = mesh,
pass = pass,
material = mat,
properties = props,
layer = renderLayer,
};
return PictureFlusher.CmdDraw.create(
mesh : mesh,
pass : pass,
material : mat,
properties : props,
layer : renderLayer
);
}
}
}

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


}
class TextBlobMesh {
class TextBlobMesh : PoolItem {
public readonly TextBlob textBlob;
public readonly float scale;
public readonly uiMatrix3 matrix;
public TextBlob textBlob;
public float scale;
public uiMatrix3 matrix;
public TextBlobMesh(TextBlob textBlob, float scale, uiMatrix3 matrix) {
this.textBlob = textBlob;
this.scale = scale;
this.matrix = matrix;
public TextBlobMesh() {
}
public override void clear() {
this.textBlob = null;
this.matrix = null;
this._mesh?.dispose();
this._mesh = null;
this._resolved = false;
}
public static TextBlobMesh create(TextBlob textBlob, float scale, uiMatrix3 matrix) {
TextBlobMesh newMesh = ItemPoolManager.alloc<TextBlobMesh>();
newMesh.textBlob = textBlob;
newMesh.scale = scale;
newMesh.matrix = matrix;
return newMesh;
}
public static long frameCount {

var keysToRemove = _meshes.Values.Where(info => info.timeToLive < _frameCount)
.Select(info => info.key).ToList();
foreach (var key in keysToRemove) {
_meshes[key].mesh.dispose();
_meshes.Remove(key);
}
}

var text = this.textBlob.text;
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * this.scale);
var vertices = new List<Vector3>(length * 4);
var triangles = new List<int>(length * 6);
var uv = new List<Vector2>(length * 4);
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
vertices.SetCapacity(length * 4);
var triangles = ItemPoolManager.alloc<uiList<int>>();
triangles.SetCapacity(length * 6);
var uv = ItemPoolManager.alloc<uiList<Vector2>>();
uv.SetCapacity(length * 4);
for (int charIndex = 0; charIndex < length; ++charIndex) {
var ch = text[charIndex + this.textBlob.textOffset];
// first char as origin for mesh position

if (vertices.Count == 0) {
this._mesh = null;
vertices.dispose();
uv.dispose();
triangles.dispose();
uiMeshMesh mesh = vertices.Count > 0 ? new uiMeshMesh(null, vertices, triangles, uv) : null;
uiMeshMesh mesh = vertices.Count > 0 ? uiMeshMesh.create(null, vertices, triangles, uv) : null;
_meshes[key] = new MeshInfo(key, mesh, fontInfo.textureVersion);
this._mesh = mesh.transform(this.matrix);

7
Runtime/ui/utils/basic_types/generic_list.cs


this.list = this.list ?? new List<T>(128);
}
public uiList(List<T> inner) {
this.list = inner;
}
public uiList() {
}
public List<T> data => this.list;
public void Add(T item) {

106
Runtime/ui/utils/basic_types/path.cs


D.assert(indices.Count == cindices);
var mesh = new uiMeshMesh(null, vertices.data, indices.data);
var mesh = uiMeshMesh.create(null, vertices, indices);
var _fillMesh = mesh;
var _fillConvex = false;

D.assert(indices.Count == cindices);
var _strokeMesh = new uiMeshMesh(null, vertices.data, indices.data);
var _strokeMesh = uiMeshMesh.create(null, vertices, indices);
return _strokeMesh;
}
}

}
}
class uiMeshMesh {
public readonly List<Vector3> vertices;
public readonly List<int> triangles;
public readonly List<Vector2> uv;
public readonly uiMatrix3 matrix;
public readonly Rect rawBounds;
class uiMeshMesh : PoolItem {
public uiList<Vector3> vertices;
public uiList<int> triangles;
public uiList<Vector2> uv;
public uiMatrix3 matrix;
public Rect rawBounds;
Rect _bounds;

}
}
uiMeshMesh _boundsMesh;
static readonly List<int> _boundsTriangles = new List<int>(6) {
0, 2, 1, 1, 2, 3
};

if (this._boundsMesh == null) {
this._boundsMesh = new uiMeshMesh(this.bounds);
}
return uiMeshMesh.create(this.bounds);
}
}
return this._boundsMesh;
}
public uiMeshMesh() {
public uiMeshMesh(Rect rect) {
this.vertices = new List<Vector3>(4) {
new Vector3(rect.right, rect.bottom),
new Vector3(rect.right, rect.top),
new Vector3(rect.left, rect.bottom),
new Vector3(rect.left, rect.top)
};
public override void clear() {
this.vertices?.dispose();
this.triangles?.dispose();
this.uv?.dispose();
this.vertices = null;
this.triangles = null;
this.uv = null;
this.matrix = null;
this.rawBounds = null;
this._bounds = null;
}
this.triangles = _boundsTriangles;
this.rawBounds = rect;
public static uiMeshMesh create(Rect rect) {
uiMeshMesh newMesh = ItemPoolManager.alloc<uiMeshMesh>();
this._bounds = this.rawBounds;
this._boundsMesh = this;
newMesh.vertices = ItemPoolManager.alloc<uiList<Vector3>>();
newMesh.vertices.Add(new Vector3(rect.right, rect.bottom));
newMesh.vertices.Add(new Vector3(rect.right, rect.top));
newMesh.vertices.Add(new Vector3(rect.left, rect.bottom));
newMesh.vertices.Add(new Vector3(rect.left, rect.top));
newMesh.triangles = ItemPoolManager.alloc<uiList<int>>();
newMesh.triangles.AddRange(_boundsTriangles);
newMesh.rawBounds = rect;
newMesh._bounds = newMesh.rawBounds;
return newMesh;
public uiMeshMesh(uiMatrix3 matrix, List<Vector3> vertices, List<int> triangles, List<Vector2> uv = null,
public static uiMeshMesh create(uiMatrix3 matrix, uiList<Vector3> vertices, uiList<int> triangles, uiList<Vector2> uv = null,
Rect rawBounds = null) {
D.assert(vertices != null);
D.assert(vertices.Count >= 0);

this.matrix = matrix;
this.vertices = vertices;
this.triangles = triangles;
this.uv = uv;
uiMeshMesh newMesh = ItemPoolManager.alloc<uiMeshMesh>();
newMesh.matrix = matrix;
newMesh.vertices = vertices;
newMesh.triangles = triangles;
if (uv != null) {
newMesh.uv = uv;
}
if (rawBounds == null) {
if (vertices.Count > 0) {

}
}
this.rawBounds = rawBounds;
newMesh.rawBounds = rawBounds;
return newMesh;
return new uiMeshMesh(matrix, this.vertices, this.triangles, this.uv, this.rawBounds);
var vertices = ItemPoolManager.alloc<uiList<Vector3>>();
vertices.SetCapacity(this.vertices.Count);
vertices.AddRange(this.vertices.data);
var triangles = ItemPoolManager.alloc<uiList<int>>();
triangles.SetCapacity(this.triangles.Count);
triangles.AddRange(this.triangles.data);
uiList<Vector2> uv = null;
if (this.uv != null) {
uv = ItemPoolManager.alloc<uiList<Vector2>>();
uv.SetCapacity(this.uv.Count);
uv.AddRange(this.uv.data);
}
var ret = uiMeshMesh.create(matrix, vertices, triangles, uv, this.rawBounds);
return ret;
}
}
}
正在加载...
取消
保存