Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 827af15

Browse files
committed
refine codes
1 parent 35a3a16 commit 827af15

File tree

8 files changed

+52
-54
lines changed

8 files changed

+52
-54
lines changed

Runtime/engine/UIWidgetsPanel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public override void OnGUI(Event evt) {
4343
this._needsPaint = true;
4444
}
4545

46-
this._needsPaint = true;
47-
4846
if (evt.type == EventType.Repaint) {
4947
if (!this._needsPaint) {
5048
return;

Runtime/ui/geometry.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,21 +1089,6 @@ public float brRadiusY {
10891089
get { return this.brRadius.y; }
10901090
}
10911091

1092-
public bool isNaiveRRect {
1093-
get {
1094-
var radius = this.tlRadiusX;
1095-
return this.tlRadiusY == radius &&
1096-
this.trRadiusX == radius &&
1097-
this.trRadiusY == radius &&
1098-
this.blRadiusX == radius &&
1099-
this.blRadiusY == radius &&
1100-
this.brRadiusX == radius &&
1101-
this.brRadiusY == radius &&
1102-
radius <= this.width / 2 &&
1103-
radius <= this.height / 2;
1104-
}
1105-
}
1106-
11071092
public static readonly RRect zero = new RRect(0, 0, 0, 0, (Radius) null);
11081093

11091094
public RRect shift(Offset offset) {

Runtime/ui/painting/path.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ public class Path {
2222

2323
uint _pathKey = 0;
2424

25-
bool _isRRect = false;
26-
public bool isRRect => this._isRRect;
25+
//shadow speeder relevant
26+
bool _isNaiveRRect = false;
27+
public bool isNaiveRRect => this._isNaiveRRect;
2728

2829
PathShapeHint _shapeHint = PathShapeHint.Other;
2930
public PathShapeHint shapeHint => this._shapeHint;
3031

31-
float _rCorner;
32-
public float rCorner => this._rCorner;
32+
float _rRectCorner;
33+
public float rRectCorner => this._rRectCorner;
3334

3435
public uint pathKey {
3536
get {
@@ -44,14 +45,14 @@ public Path(int capacity = 128) {
4445

4546
public List<float> commands => this._commands;
4647

47-
void _updateRRectFlag(bool isRRect, PathShapeHint shapeHint = PathShapeHint.Other, float corner = 0) {
48-
if (this._commands.Count > 0 && !this._isRRect) {
48+
void _updateRRectFlag(bool isNaiveRRect, PathShapeHint shapeHint = PathShapeHint.Other, float corner = 0) {
49+
if (this._commands.Count > 0 && !this._isNaiveRRect) {
4950
return;
5051
}
51-
this._isRRect = isRRect && this._hasOnlyMoveTos();
52-
if (this._isRRect) {
52+
this._isNaiveRRect = isNaiveRRect && this._hasOnlyMoveTos();
53+
if (this._isNaiveRRect) {
5354
this._shapeHint = shapeHint;
54-
this._rCorner = corner;
55+
this._rRectCorner = corner;
5556
}
5657
}
5758

@@ -134,7 +135,7 @@ void _reset() {
134135

135136
this._pathKey = pathGlobalKey++;
136137
this._cache = null;
137-
this._isRRect = false;
138+
this._isNaiveRRect = false;
138139
}
139140

140141
internal PathCache flatten(float scale) {
@@ -520,7 +521,7 @@ public void addRect(Rect rect) {
520521
}
521522

522523
public void addRRect(RRect rrect) {
523-
this._updateRRectFlag(rrect.isNaiveRRect, PathShapeHint.NaiveRRect, rrect.blRadiusX);
524+
this._updateRRectFlag(rrect.isNaiveRRect(), PathShapeHint.NaiveRRect, rrect.blRadiusX);
524525
float w = rrect.width;
525526
float h = rrect.height;
526527
float halfw = Mathf.Abs(w) * 0.5f;
@@ -767,7 +768,7 @@ public void addPath(Path path, Offset offset) {
767768
public void addPath(Path path, Matrix3 transform = null) {
768769
D.assert(path != null);
769770

770-
this._updateRRectFlag(path.isRRect, path.shapeHint, path.rCorner);
771+
this._updateRRectFlag(path.isNaiveRRect, path.shapeHint, path.rRectCorner);
771772
var i = 0;
772773
while (i < path._commands.Count) {
773774
var cmd = (PathCommand) path._commands[i];

Runtime/ui/painting/shadow_utils.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static void drawShadowFull2(Canvas canvas, Path path, Vector3 zPlaneParams, Vect
210210

211211
//debug shadow
212212
if (debugShadow) {
213-
var isRRect = path.isRRect;
213+
var isRRect = path.isNaiveRRect;
214214
if (isRRect) {
215215
ambientColor = uiColor.fromColor(Colors.red);
216216
spotColor = uiColor.fromColor(Colors.red);
@@ -246,7 +246,7 @@ static void drawShadowFull2(Canvas canvas, Path path, Vector3 zPlaneParams, Vect
246246
_shadowPaint.strokeWidth = 0;
247247
_shadowPaint.style = PaintingStyle.fill;
248248
float sigma2 = convertRadiusToSigma(radius);
249-
_shadowPaint.maskFilter = path.isRRect ? MaskFilter.fastShadow(sigma2) : MaskFilter.blur(BlurStyle.normal, sigma2);
249+
_shadowPaint.maskFilter = path.isNaiveRRect ? MaskFilter.fastShadow(sigma2) : MaskFilter.blur(BlurStyle.normal, sigma2);
250250
canvas.drawPath(path, _shadowPaint);
251251

252252
canvas.restore();
@@ -264,7 +264,6 @@ static void drawShadowFast(Canvas canvas, Path path, Vector3 zPlaneParams, Vecto
264264
float blurRadius = 0.5f * devSpaceOutset * oneOverA;
265265
float strokeWidth = 0.5f * (devSpaceOutset - blurRadius);
266266

267-
//Paint paint = new Paint {color = ambientColor, strokeWidth = strokeWidth, style = PaintingStyle.fill};
268267
_shadowPaint.color = new Color(ambientColor.value);
269268
_shadowPaint.strokeWidth = strokeWidth;
270269
_shadowPaint.style = PaintingStyle.fill;
@@ -280,13 +279,33 @@ static void drawShadowFast(Canvas canvas, Path path, Vector3 zPlaneParams, Vecto
280279

281280
canvas.save();
282281
canvas.setMatrix(_shadowMatrix);
283-
//Paint paint2 = new Paint {color = spotColor};
284282
_shadowPaint.color = new Color(spotColor.value);
285283
_shadowPaint.strokeWidth = 0;
286284
_shadowPaint.style = PaintingStyle.fill;
287285
canvas.drawPath(path, _shadowPaint);
288286

289287
canvas.restore();
290288
}
289+
290+
/*
291+
* Check whether the RRect is a naive Round-Rect, of which
292+
* (1) all the corner radius are the same
293+
* (2) the corner radius is not bigger than either half the width or the height of the Round Rect's bounding box
294+
*
295+
* Usage: The shadow of a naive Round-Rect can be easily drawn using a ShadowRBox shader, so we can use it to
296+
* find all the situations that a fast shadow can be drawn to tackle the performance issue
297+
*/
298+
public static bool isNaiveRRect(this RRect rrect) {
299+
var radius = rrect.tlRadiusX;
300+
return rrect.tlRadiusY == radius &&
301+
rrect.trRadiusX == radius &&
302+
rrect.trRadiusY == radius &&
303+
rrect.blRadiusX == radius &&
304+
rrect.blRadiusY == radius &&
305+
rrect.brRadiusX == radius &&
306+
rrect.brRadiusY == radius &&
307+
radius <= rrect.width / 2 &&
308+
radius <= rrect.height / 2;
309+
}
291310
}
292311
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,6 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
10441044

10451045
break;
10461046
case CmdDraw cmd:
1047-
if (cmd.material != CanvasShader.shadowBox) {
1048-
1049-
}
10501047
this._setRenderTarget(cmdBuf, layer.rtID, ref toClear);
10511048

10521049
if (cmd.layerId != null) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Runtime.CompilerServices;
32
using UnityEngine;
43
using UnityEngine.Rendering;
54

@@ -512,7 +511,6 @@ public static PictureFlusher.CmdDraw fastShadow(PictureFlusher.RenderLayer layer
512511
mat = _shadowRBox;
513512
}
514513

515-
//use props to set all the uniforms !!!!!
516514
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
517515
props.SetVector(_viewportId, viewport);
518516
props.SetFloat(_shadowSigmaId, sigma);

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace Unity.UIWidgets.ui {
55
public partial class PictureFlusher {
66

77
void _drawRRectShadow(uiPath path, uiPaint paint) {
8-
D.assert(path.isRRect, () => "Cannot draw Shadow for non-RRect shapes");
9-
D.assert(paint.style == PaintingStyle.fill, () => "Cannot draw Shadow for stroke lines");
8+
D.assert(path.isNaiveRRect, () => "Cannot draw fast Shadow for non-NaiveRRect shapes");
9+
D.assert(paint.style == PaintingStyle.fill, () => "Cannot draw fast Shadow for stroke lines");
1010
var bound = path.getBounds();
1111
if (!this._applyClip(bound)) {
1212
return;
@@ -33,7 +33,7 @@ void _drawRRectShadow(uiPath path, uiPaint paint) {
3333
_triangles.Add(3);
3434

3535
var mesh = uiMeshMesh.create(state.matrix, vertices, _triangles);
36-
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, sigma, path.isRect, path.isCircle, path.rCorner, new Vector4(bound.left, bound.top, bound.right, bound.bottom), paint.color));
36+
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, sigma, path.isRect, path.isCircle, path.rRectCorner, new Vector4(bound.left, bound.top, bound.right, bound.bottom), paint.color));
3737
}
3838

3939
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ public partial class uiPath : PoolObject {
1616
public uint pathKey = 0;
1717
public bool needCache = false;
1818

19-
bool _isRRect = false;
20-
public bool isRRect => this._isRRect;
19+
bool _isNaiveRRect = false;
20+
public bool isNaiveRRect => this._isNaiveRRect;
2121

2222
uiPathShapeHint _shapeHint = uiPathShapeHint.Other;
2323
public uiPathShapeHint shapeHint => this._shapeHint;
2424

25-
float _rCorner;
26-
public float rCorner => this._rCorner;
25+
float _rRectCorner;
26+
public float rRectCorner => this._rRectCorner;
2727

28-
void _updateRRectFlag(bool isRRect, uiPathShapeHint shapeHint = uiPathShapeHint.Other, float corner = 0) {
29-
if (this._commands.Count > 0 && !this._isRRect) {
28+
void _updateRRectFlag(bool isNaiveRRect, uiPathShapeHint shapeHint = uiPathShapeHint.Other, float corner = 0) {
29+
if (this._commands.Count > 0 && !this._isNaiveRRect) {
3030
return;
3131
}
32-
this._isRRect = isRRect && this._hasOnlyMoveTos();
33-
if (this._isRRect) {
32+
this._isNaiveRRect = isNaiveRRect && this._hasOnlyMoveTos();
33+
if (this._isNaiveRRect) {
3434
this._shapeHint = shapeHint;
35-
this._rCorner = corner;
35+
this._rRectCorner = corner;
3636
}
3737
}
3838

@@ -79,7 +79,7 @@ public override void clear() {
7979

8080
this.needCache = false;
8181
this.pathKey = 0;
82-
this._isRRect = false;
82+
this._isNaiveRRect = false;
8383
}
8484

8585
void _reset() {
@@ -92,7 +92,7 @@ void _reset() {
9292
this._maxY = float.MinValue;
9393
ObjectPool<uiPathCache>.release(this._cache);
9494
this._cache = null;
95-
this._isRRect = false;
95+
this._isNaiveRRect = false;
9696
}
9797

9898
internal uiPathCache flatten(float scale) {
@@ -252,7 +252,7 @@ public void addRect(Rect rect) {
252252
}
253253

254254
public void addRRect(RRect rrect) {
255-
this._updateRRectFlag(rrect.isNaiveRRect, uiPathShapeHint.NaiveRRect, rrect.blRadiusX);
255+
this._updateRRectFlag(rrect.isNaiveRRect(), uiPathShapeHint.NaiveRRect, rrect.blRadiusX);
256256
float w = rrect.width;
257257
float h = rrect.height;
258258
float halfw = Mathf.Abs(w) * 0.5f;
@@ -432,7 +432,7 @@ public static uiPath fromPath(Path path) {
432432
return uipath;
433433
}
434434

435-
uipath._updateRRectFlag(path.isRRect, (uiPathShapeHint)path.shapeHint, path.rCorner);
435+
uipath._updateRRectFlag(path.isNaiveRRect, (uiPathShapeHint)path.shapeHint, path.rRectCorner);
436436

437437
var i = 0;
438438
var _commands = path.commands;

0 commit comments

Comments
 (0)