浏览代码

fix some bugs

/main
xingwei.zhu 5 年前
当前提交
03bfdd56
共有 8 个文件被更改,包括 188 次插入203 次删除
  1. 4
      Runtime/foundation/debug.cs
  2. 69
      Runtime/ui/painting/canvas_impl.cs
  3. 3
      Runtime/ui/painting/txt/mesh_generator.cs
  4. 7
      Runtime/ui/utils/basic_types/path.cs
  5. 2
      Runtime/ui/utils/basic_types/pool_items.cs.meta
  6. 143
      Runtime/ui/utils/basic_types/pool_items.cs
  7. 163
      Runtime/ui/utils/basic_types/no_alloc_item.cs
  8. 0
      /Runtime/ui/utils/basic_types/pool_items.cs.meta

4
Runtime/foundation/debug.cs


}
}
[Conditional("UIWidgets_DEBUG")]
//[Conditional("UIWidgets_DEBUG")]
public static void assert(Func<bool> result, Func<string> message = null) {
if (!result()) {
throw new AssertionError(message != null ? message() : "");

[Conditional("UIWidgets_DEBUG")]
//[Conditional("UIWidgets_DEBUG")]
public static void assert(bool result, Func<string> message = null) {
if (!result) {
throw new AssertionError(message != null ? message() : "");

69
Runtime/ui/painting/canvas_impl.cs


}
void _reset() {
foreach (var layer in this._layers) {
this._clearLayer(layer);
//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);
this._currentLayer.dispose();
this._currentLayer = null;
this._lastScissor = null;
this._layers.Clear();
RenderLayer firstLayer;
if (this._layers.Count == 0) {
var width = this._renderTexture.width;
var height = this._renderTexture.height;
var width = this._renderTexture.width;
var height = this._renderTexture.height;
var bounds = Rect.fromLTWH(0, 0,
var bounds = Rect.fromLTWH(0, 0,
firstLayer = RenderLayer.create(
RenderLayer firstLayer = RenderLayer.create(
);
} else {
D.assert(this._layers.Count > 0);
firstLayer = this._layers[0];
firstLayer = RenderLayer.create(
width : firstLayer.width,
height : firstLayer.height,
layerBounds : firstLayer.layerBounds
);
}
);
foreach (var layer in this._layers) {
layer.dispose();
}
this._layers.Clear();
this._layers.Add(firstLayer);
this._currentLayer = firstLayer;
}

return;
}
//!!!!!!BUG ==> need dispose layer!!!!!!!!
this._layers.RemoveAt(this._layers.Count - 1);
var currentLayer = this._currentLayer = this._layers[this._layers.Count - 1];
var state = currentLayer.currentState;

if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
return;
}

layer.draws.Add(CanvasShader.stencilClear(layer, boundsMesh));
foreach (var maskElement in reducedClip.maskElements) {
layer.draws.Add(CanvasShader.stencil0(layer, maskElement.mesh));
layer.draws.Add(CanvasShader.stencil1(layer, boundsMesh));
layer.draws.Add(CanvasShader.stencil0(layer, maskElement.mesh.duplicate()));
layer.draws.Add(CanvasShader.stencil1(layer, boundsMesh.duplicate()));
}
}

radiusX, new Vector2(1f / textureWidth, 0), kernelX));
blurYLayer.draws.Add(CanvasShader.maskFilter(
blurYLayer, blurMesh, blurXLayer,
blurYLayer, blurMesh.duplicate(), blurXLayer,
void _drawWithMaskFilter(Rect meshBounds, Action<Paint> drawAction, Paint paint, MaskFilter maskFilter) {
void _drawWithMaskFilter(Rect meshBounds, Action<Paint> drawAction, Paint paint, MaskFilter maskFilter, Action onQuit = null) {
var layer = this._currentLayer;
var clipBounds = layer.layerBounds;

}
if (clipBounds.isEmpty) {
onQuit?.Invoke();
return;
}

onQuit?.Invoke();
return;
}

if (maskBounds.isEmpty) {
onQuit?.Invoke();
return;
}

var blurMesh = ImageMeshGenerator.imageMesh(null, Rect.one, maskBounds);
if (!this._applyClip(blurMesh.bounds)) {
blurMesh.dispose();
onQuit?.Invoke();
return;
}

Action<Paint> drawMesh = p => {
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
return;
}

};
if (paint.maskFilter != null && paint.maskFilter.sigma != 0) {
this._drawWithMaskFilter(mesh.bounds, drawMesh, paint, paint.maskFilter);
this._drawWithMaskFilter(mesh.bounds, drawMesh, paint, paint.maskFilter, () => {mesh.dispose();});
return;
}

Action<Paint> drawMesh = p => {
if (!this._applyClip(mesh.bounds)) {
mesh.dispose();
return;
}

layer.draws.Add(CanvasShader.stroke1(layer, mesh));
layer.draws.Add(CanvasShader.stroke1(layer, mesh.duplicate()));
this._drawWithMaskFilter(mesh.bounds, drawMesh, paint, paint.maskFilter);
this._drawWithMaskFilter(mesh.bounds, drawMesh, paint, paint.maskFilter, () => {mesh.dispose();});
return;
}

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

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

Action<Paint> drawMesh = (Paint p) => {
if (!this._applyClip(textBlobBounds)) {
mesh.dispose();
return;
}

if (paint.maskFilter != null && paint.maskFilter.sigma != 0) {
this._drawWithMaskFilter(textBlobBounds, drawMesh, paint, paint.maskFilter);
this._drawWithMaskFilter(textBlobBounds, drawMesh, paint, paint.maskFilter, () => {mesh.dispose();});
return;
}

Graphics.ExecuteCommandBuffer(cmdBuf);
}
this._clearLayer(layer);
}
int _lastRtID;

public override void clear() {
this.mesh?.dispose();
this.textMesh?.dispose();
}
public CmdDraw() {

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


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

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


var pt = this._points[this._points.Count - 1];
if (uiPathUtils.ptEquals(pt.x, pt.y, point.x, point.y, this._distTol)) {
pt.flags |= point.flags;
point.dispose();
return;
}
}

public uiMeshMesh boundsMesh {
get {
return uiMeshMesh.create(this.bounds);
return create(this.bounds);
}
}

newMesh.rawBounds = rawBounds;
return newMesh;
}
public uiMeshMesh duplicate() {
return this.transform(this.matrix);
}
public uiMeshMesh transform(uiMatrix3 matrix) {

2
Runtime/ui/utils/basic_types/pool_items.cs.meta


fileFormatVersion: 2
guid: de81e76ee426741728b59a1dc73b63b6
guid: dc26305d777344a62b1f9382ec6f0ce3
MonoImporter:
externalObjects: {}
serializedVersion: 2

143
Runtime/ui/utils/basic_types/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;
}
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 curItem = curPool[0];
curPool.RemoveAt(0);
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);
}
}
}

163
Runtime/ui/utils/basic_types/no_alloc_item.cs


using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.UIWidgets.foundation;
using UnityEngine;
namespace Unity.UIWidgets.ui {
public class ItemDebugInfo {
public int _watermark;
public int _size;
public string _itemKey;
public void consume() {
this._size++;
if (this._size > this._watermark) {
this._watermark = this._size;
//Debug.Log("Item watermark increases >>> " + this._itemKey + " = " + this._watermark);
}
}
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) {
info += "| " + key + " = " + debugInfo[key]._watermark + " |\n";
}
Debug.Log(info);
}
public static T alloc<T>() where T : PoolItem, new() {
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, _itemKey = typeof(T).ToString()};
}
}
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 curItem = curPool[0];
curPool.RemoveAt(0);
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 static class TestJob {
public static void testJob() {
var TestPoolItem = ItemPoolManager.alloc<TestPoolItem>();
TestPoolItem.SetLength(2f);
TestPoolItem.dispose();
}
}
public class TestPoolItem : PoolItem {
float length;
public void SetLength(float len) {
this.length = len;
}
public override void clear() {
this.length = 0;
base.dispose();
}
}
public abstract class PoolItem {
//ensure that base class has a empty constructor
bool __activated_flag = false;
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);
}
}
}

/Runtime/ui/utils/basic_types/no_alloc_item.cs.meta → /Runtime/ui/utils/basic_types/pool_items.cs.meta

正在加载...
取消
保存