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

Commit 4ba0fa5

Browse files
author
Yuncong Zhang
committed
[1.5.4] Upgrade rendering objects.
1 parent 4925071 commit 4ba0fa5

File tree

10 files changed

+373
-149
lines changed

10 files changed

+373
-149
lines changed

Runtime/rendering/debug.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Unity.UIWidgets.rendering {
2+
public static class RenderingDebugUtils {
3+
public static bool debugCheckElevationsEnabled = false;
4+
}
5+
}

Runtime/rendering/debug.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/rendering/editable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ void _paintCaret(Canvas canvas, Offset effectiveOffset, TextPosition textPositio
12451245
D.assert(this._textLayoutLastWidth == this.constraints.maxWidth);
12461246
var paint = new Paint() {color = this._floatingCursorOn ? this.backgroundCursorColor : this._cursorColor};
12471247
var caretOffset = this._textPainter.getOffsetForCaret(textPosition, this._caretPrototype) + effectiveOffset;
1248-
Rect caretRect = this._caretPrototype.shift(caretOffset + effectiveOffset);
1248+
Rect caretRect = this._caretPrototype.shift(caretOffset);
12491249
if (this._cursorOffset != null) {
12501250
caretRect = caretRect.shift(this._cursorOffset);
12511251
}

Runtime/rendering/error.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public RenderErrorBox(string message = "") {
2929
);
3030

3131
static ParagraphStyle paragraphStyle = new ParagraphStyle(
32-
lineHeight: 1.0f
32+
height: 1.0f
3333
);
3434
}
3535
}

Runtime/rendering/flex.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ protected override void performLayout() {
385385
if (totalFlex > 0 || this.crossAxisAlignment == CrossAxisAlignment.baseline) {
386386
float spacePerFlex = canFlex && totalFlex > 0 ? (freeSpace / totalFlex) : float.NaN;
387387
child = this.firstChild;
388+
float maxSizeAboveBaseline = 0;
389+
float maxSizeBelowBaseline = 0;
388390
while (child != null) {
389391
int flex = this._getFlex(child);
390392
if (flex > 0) {
@@ -448,6 +450,9 @@ protected override void performLayout() {
448450
float? distance = child.getDistanceToBaseline(this.textBaseline, onlyReal: true);
449451
if (distance != null) {
450452
maxBaselineDistance = Mathf.Max(maxBaselineDistance, distance.Value);
453+
maxSizeAboveBaseline = Mathf.Max(distance.Value, maxSizeAboveBaseline);
454+
maxSizeBelowBaseline = Mathf.Max(child.size.height - distance.Value, maxSizeBelowBaseline);
455+
crossSize = maxSizeAboveBaseline + maxSizeBelowBaseline;
451456
}
452457
}
453458

Runtime/rendering/layer.cs

Lines changed: 155 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using Unity.UIWidgets.external.simplejson;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Text;
44
using Unity.UIWidgets.foundation;
55
using Unity.UIWidgets.painting;
66
using Unity.UIWidgets.ui;
@@ -243,7 +243,7 @@ public Layer firstChild {
243243
public Layer lastChild {
244244
get { return this._lastChild; }
245245
}
246-
246+
247247
internal override S find<S>(Offset regionOffset) {
248248
Layer current = this.lastChild;
249249
while (current != null) {
@@ -282,6 +282,95 @@ bool _debugUltimateNextSiblingOf(Layer child, Layer equals = null) {
282282
return child == equals;
283283
}
284284

285+
PictureLayer _highlightConflictingLayer(PhysicalModelLayer child) {
286+
PictureRecorder recorder = new PictureRecorder();
287+
var canvas = new RecorderCanvas(recorder);
288+
canvas.drawPath(child.clipPath, new Paint() {
289+
color = new Color(0xFFAA0000),
290+
style = PaintingStyle.stroke,
291+
strokeWidth = child.elevation + 10.0f,
292+
});
293+
PictureLayer pictureLayer = new PictureLayer(child.clipPath.getBounds());
294+
pictureLayer.picture = recorder.endRecording();
295+
pictureLayer.debugCreator = child;
296+
child.append(pictureLayer);
297+
return pictureLayer;
298+
}
299+
300+
List<PictureLayer> _processConflictingPhysicalLayers(PhysicalModelLayer predecessor, PhysicalModelLayer child) {
301+
UIWidgetsError.reportError(new UIWidgetsErrorDetails(
302+
exception: new UIWidgetsError("Painting order is out of order with respect to elevation.\n" +
303+
"See https://api.flutter.dev/flutter/rendering/debugCheckElevations.html " +
304+
"for more details."),
305+
context: "during compositing",
306+
informationCollector: (StringBuilder builder) => {
307+
builder.AppendLine("Attempted to composite layer");
308+
builder.AppendLine(child.ToString());
309+
builder.AppendLine("after layer");
310+
builder.AppendLine(predecessor.ToString());
311+
builder.AppendLine("which occupies the same area at a higher elevation.");
312+
}
313+
));
314+
return new List<PictureLayer> {
315+
this._highlightConflictingLayer(predecessor),
316+
this._highlightConflictingLayer(child)
317+
};
318+
}
319+
320+
protected List<PictureLayer> _debugCheckElevations() {
321+
List<PhysicalModelLayer> physicalModelLayers =
322+
this.depthFirstIterateChildren().OfType<PhysicalModelLayer>().ToList();
323+
List<PictureLayer> addedLayers = new List<PictureLayer>();
324+
325+
for (int i = 0; i < physicalModelLayers.Count; i++) {
326+
PhysicalModelLayer physicalModelLayer = physicalModelLayers[i];
327+
D.assert(physicalModelLayer.lastChild?.debugCreator != physicalModelLayer,
328+
() => "debugCheckElevations has either already visited this layer or failed to remove the" +
329+
" added picture from it.");
330+
float accumulatedElevation = physicalModelLayer.elevation;
331+
Layer ancestor = physicalModelLayer.parent;
332+
while (ancestor != null) {
333+
if (ancestor is PhysicalModelLayer modelLayer) {
334+
accumulatedElevation += modelLayer.elevation;
335+
}
336+
337+
ancestor = ancestor.parent;
338+
}
339+
340+
for (int j = 0; j <= i; j++) {
341+
PhysicalModelLayer predecessor = physicalModelLayers[j];
342+
float predecessorAccumulatedElevation = predecessor.elevation;
343+
ancestor = predecessor.parent;
344+
while (ancestor != null) {
345+
if (ancestor == predecessor) {
346+
continue;
347+
}
348+
349+
if (ancestor is PhysicalModelLayer modelLayer) {
350+
predecessorAccumulatedElevation += modelLayer.elevation;
351+
}
352+
353+
ancestor = ancestor.parent;
354+
}
355+
356+
if (predecessorAccumulatedElevation <= accumulatedElevation) {
357+
continue;
358+
}
359+
360+
Path intersection = Path.combine(
361+
PathOperation.intersect,
362+
predecessor._debugTransformedClipPath,
363+
physicalModelLayer._debugTransformedClipPath);
364+
365+
if (intersection != null && intersection.computeMetrics().Any((metric) => metric.length > 0)) {
366+
addedLayers.AddRange(this._processConflictingPhysicalLayers(predecessor, physicalModelLayer));
367+
}
368+
}
369+
}
370+
371+
return addedLayers;
372+
}
373+
285374
internal override void updateSubtreeNeedsAddToScene() {
286375
base.updateSubtreeNeedsAddToScene();
287376
Layer child = this.firstChild;
@@ -419,6 +508,25 @@ public virtual void applyTransform(Layer child, Matrix3 transform) {
419508
D.assert(transform != null);
420509
}
421510

511+
public List<Layer> depthFirstIterateChildren() {
512+
if (this.firstChild == null) {
513+
return new List<Layer>();
514+
}
515+
516+
List<Layer> children = new List<Layer>();
517+
Layer child = this.firstChild;
518+
while (child != null) {
519+
children.Add(child);
520+
if (child is ContainerLayer containerLayer) {
521+
children.AddRange(containerLayer.depthFirstIterateChildren());
522+
}
523+
524+
child = child.nextSibling;
525+
}
526+
527+
return children;
528+
}
529+
422530
public override List<DiagnosticsNode> debugDescribeChildren() {
423531
var children = new List<DiagnosticsNode>();
424532
if (this.firstChild == null) {
@@ -470,9 +578,27 @@ public override void applyTransform(Layer child, Matrix3 transform) {
470578
}
471579

472580
public Scene buildScene(SceneBuilder builder) {
581+
List<PictureLayer> temporaryLayers = null;
582+
D.assert(() => {
583+
if (RenderingDebugUtils.debugCheckElevationsEnabled) {
584+
temporaryLayers = this._debugCheckElevations();
585+
}
586+
587+
return true;
588+
});
473589
this.updateSubtreeNeedsAddToScene();
474590
this.addToScene(builder);
475-
return builder.build();
591+
Scene scene = builder.build();
592+
D.assert(() => {
593+
if (temporaryLayers != null) {
594+
foreach (PictureLayer temporaryLayer in temporaryLayers) {
595+
temporaryLayer.remove();
596+
}
597+
}
598+
599+
return true;
600+
});
601+
return scene;
476602
}
477603

478604
internal override flow.Layer addToScene(SceneBuilder builder, Offset layerOffset = null) {
@@ -731,7 +857,7 @@ internal override S find<S>(Offset regionOffset) {
731857
if (this._invertedTransform == null) {
732858
return null;
733859
}
734-
860+
735861
Offset transform = this._invertedTransform.mapXY(regionOffset.dx, regionOffset.dy);
736862
return base.find<S>(transform);
737863
}
@@ -756,7 +882,13 @@ internal override flow.Layer addToScene(SceneBuilder builder, Offset layerOffset
756882
public override void applyTransform(Layer child, Matrix3 transform) {
757883
D.assert(child != null);
758884
D.assert(transform != null);
759-
transform.preConcat(this._lastEffectiveTransform);
885+
D.assert(this._lastEffectiveTransform != null || this.transform != null);
886+
if (this._lastEffectiveTransform == null) {
887+
transform.preConcat(this.transform);
888+
}
889+
else {
890+
transform.preConcat(this._lastEffectiveTransform);
891+
}
760892
}
761893

762894
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
@@ -822,7 +954,7 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
822954
properties.add(new DiagnosticsProperty<Offset>("offset", this.offset));
823955
}
824956
}
825-
957+
826958
public class BackdropFilterLayer : ContainerLayer {
827959
public BackdropFilterLayer(ImageFilter filter = null) {
828960
D.assert(filter != null);
@@ -1043,7 +1175,7 @@ void _establishTransform() {
10431175
protected override bool alwaysNeedsAddToScene {
10441176
get { return true; }
10451177
}
1046-
1178+
10471179

10481180
internal override flow.Layer addToScene(SceneBuilder builder, Offset layerOffset = null) {
10491181
layerOffset = layerOffset ?? Offset.zero;
@@ -1217,6 +1349,20 @@ public Clip clipBehavior {
12171349
}
12181350
}
12191351

1352+
internal Path _debugTransformedClipPath {
1353+
get {
1354+
ContainerLayer ancestor = this.parent;
1355+
Matrix3 matrix = Matrix3.I();
1356+
while (ancestor != null && ancestor.parent != null) {
1357+
ancestor.applyTransform(this, matrix);
1358+
ancestor = ancestor.parent;
1359+
}
1360+
1361+
return this.clipPath.transform(matrix);
1362+
}
1363+
}
1364+
1365+
12201366
Clip _clipBehavior;
12211367

12221368
public float elevation {

Runtime/rendering/object.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Unity.UIWidgets.ui;
88
using UnityEngine;
99
using Canvas = Unity.UIWidgets.ui.Canvas;
10+
using Color = Unity.UIWidgets.ui.Color;
1011
using Rect = Unity.UIWidgets.ui.Rect;
1112

1213
namespace Unity.UIWidgets.rendering {
@@ -196,7 +197,7 @@ protected virtual void stopRecordingIfNeeded() {
196197
Paint paint = new Paint {
197198
style = PaintingStyle.stroke,
198199
strokeWidth = 1.0f,
199-
color = new ui.Color(0xFFFF9800),
200+
color = new Color(0xFFFF9800),
200201
};
201202
this.canvas.drawRect(this.estimatedBounds, paint);
202203
}
@@ -288,7 +289,8 @@ public void pushTransform(bool needsCompositing, Offset offset, Matrix3 transfor
288289
Matrix3 effectiveTransform;
289290
if (offset == null || offset == Offset.zero) {
290291
effectiveTransform = transform;
291-
} else {
292+
}
293+
else {
292294
effectiveTransform = Matrix3.makeTrans(offset.dx, offset.dy);
293295
effectiveTransform.preConcat(transform);
294296
effectiveTransform.preTranslate(-offset.dx, -offset.dy);
@@ -297,7 +299,7 @@ public void pushTransform(bool needsCompositing, Offset offset, Matrix3 transfor
297299
if (needsCompositing) {
298300
var inverse = Matrix3.I();
299301
var invertible = effectiveTransform.invert(inverse);
300-
302+
301303
// it could just be "scale == 0", ignore the assertion.
302304
// D.assert(invertible);
303305

@@ -1377,6 +1379,7 @@ public override string toStringShallow(
13771379
}
13781380

13791381
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
1382+
base.debugFillProperties(properties);
13801383
properties.add(new DiagnosticsProperty<object>(
13811384
"creator", this.debugCreator, defaultValue: Diagnostics.kNullDefaultValue,
13821385
level: DiagnosticLevel.debug));

0 commit comments

Comments
 (0)