浏览代码

review rendering code to sliver_multi_box_adaptor

/siyaoH-1.17-PlatformMessage
guanghuispark 4 年前
当前提交
145a40a1
共有 8 个文件被更改,包括 185 次插入180 次删除
  1. 2
      com.unity.uiwidgets/Runtime/gestures/hit_test.cs
  2. 7
      com.unity.uiwidgets/Runtime/rendering/shifted_box.cs
  3. 119
      com.unity.uiwidgets/Runtime/rendering/sliver.cs
  4. 95
      com.unity.uiwidgets/Runtime/rendering/sliver_fill.cs
  5. 95
      com.unity.uiwidgets/Runtime/rendering/sliver_fixed_extent_list.cs
  6. 40
      com.unity.uiwidgets/Runtime/rendering/sliver_grid.cs
  7. 2
      com.unity.uiwidgets/Runtime/rendering/sliver_list.cs
  8. 5
      com.unity.uiwidgets/Runtime/rendering/sliver_multi_box_adaptor.cs

2
com.unity.uiwidgets/Runtime/gestures/hit_test.cs


}
bool _debugVectorMoreOrLessEquals(Vector4 a, Vector4 b,
double epsilon = SliverGeometry.precisionErrorTolerance) {
double epsilon = foundation_.precisionErrorTolerance) {
bool result = true;
D.assert(() => {
Vector4 difference = a - b;

7
com.unity.uiwidgets/Runtime/rendering/shifted_box.cs


D.assert(!debugNeedsLayout);
result = child.getDistanceToActualBaseline(baseline);
var childParentData = (BoxParentData) child.parentData;
var childParentData = (BoxParentData) child.parentData;
result += childParentData.offset.dy;
}
}

protected override void performLayout() {
size = constraints.constrain(_requestedSize);
if (child != null) {
child.layout(constraints);
child.layout(constraints, parentUsesSize: true);
alignChild();
}
}

public float baseline {
get { return _baseline; }
set {
D.assert(value != null);
if (_baseline == value) {
return;
}

public TextBaseline baselineType {
get { return _baselineType; }
set {
D.assert(value != null);
if (_baselineType == value) {
return;
}

protected override void performLayout() {
if (child != null) {
BoxConstraints constraints = this.constraints;
child.layout(constraints.loosen(), parentUsesSize: true);
float? childBaseline = child.getDistanceToBaseline(baselineType);
float actualBaseline = baseline;

119
com.unity.uiwidgets/Runtime/rendering/sliver.cs


}
diagnosticInfo.Add(new DiagnosticsProperty<SliverConstraints>("The offending constraints were", this, style: DiagnosticsTreeStyle.errorProperty));
throw new UIWidgetsError(
diagnosticInfo);
throw new UIWidgetsError(diagnosticInfo);
}
return true;
});

if (ReferenceEquals(this, other)) {
return true;
}
D.assert(other.debugAssertIsValid());
return axisDirection == other.axisDirection
&& growthDirection == other.growthDirection
&& userScrollDirection == other.userScrollDirection

}
public override string ToString() {
string result = "";
result += ($"{axisDirection}") +" , ";
result +=($"{growthDirection}")+" , ";
result +=($"{userScrollDirection}")+" , ";
result +=($"scrollOffset: {scrollOffset : F1}")+" , ";
result +=($"remainingPaintExtent: {remainingPaintExtent : F1}")+" , ";
if (overlap != 0.0)
result +=($"overlap: {overlap: F1}")+" , ";
result +=($"crossAxisExtent: {crossAxisExtent : F1}")+" , ";
result +=($"crossAxisDirection: crossAxisDirection")+" , ";
result +=($"viewportMainAxisExtent: {viewportMainAxisExtent : F1}")+" , ";
result +=($"remainingCacheExtent: {remainingCacheExtent : F1}")+" , ";
result +=($"cacheOrigin: {cacheOrigin : F1}");
return $"SliverConstraints({result})";
return
$"SliverConstraints({axisDirection}, {growthDirection}, {userScrollDirection}, scrollOffset: {scrollOffset:F1}, remainingPaintExtent: {remainingCacheExtent:F1}, " +
$"{(overlap != 0.0f ? "overlap: " + overlap.ToString("F1") + ", " : "")}crossAxisExtent: {crossAxisExtent:F1}, crossAxisDirection: {crossAxisDirection}, " +
$"viewportMainAxisExtent: {viewportMainAxisExtent:F1}, remainingCacheExtent: {remainingCacheExtent:F1} " +
$"cacheOrigin: {cacheOrigin:F1})";
List<string> properties = new List<string>();
properties.Add($"{axisDirection}");
properties.Add($"{growthDirection}");
properties.Add($"{userScrollDirection}");
properties.Add($"scrollOffset: {scrollOffset : F1}");
properties.Add($"remainingPaintExtent: {remainingPaintExtent : F1}");
if (overlap != 0.0)
properties.Add($"overlap: {overlap: F1}");
properties.Add($"crossAxisExtent: {crossAxisExtent : F1}");
properties.Add($"crossAxisDirection: {crossAxisDirection}");
properties.Add($"viewportMainAxisExtent: {viewportMainAxisExtent : F1}");
properties.Add($"remainingCacheExtent: {remainingCacheExtent : F1}");
properties.Add($"cacheOrigin: {cacheOrigin : F1}");
return $"SliverConstraints({string.Join(", ",properties)})";
}
}

public readonly bool hasVisualOverflow;
public readonly float? scrollOffsetCorrection;
public readonly float cacheExtent;
public const float precisionErrorTolerance = 1e-10f;
internal static List<DiagnosticsNode> _debugCompareFloats(string labelA, float valueA, string labelB, float valueB) {
List<DiagnosticsNode> diagnosticInfo = new List<DiagnosticsNode>();

);
}
if (paintExtent - maxPaintExtent > precisionErrorTolerance) {
if (paintExtent - maxPaintExtent > foundation_.precisionErrorTolerance) {
var details = _debugCompareFloats("maxPaintExtent", maxPaintExtent, "paintExtent",
paintExtent);
details.Add(new ErrorDescription("By definition, a sliver can\"t paint more than the maximum that it can paint!"));

}
public bool addWithAxisOffset(
Offset paintOffset,
SliverHitTest hitTest
Offset paintOffset = null,
SliverHitTest hitTest = null
D.assert(mainAxisOffset != null);
D.assert(crossAxisOffset != null);
D.assert(mainAxisPosition != null);
D.assert(crossAxisPosition != null);
D.assert(hitTest != null);
if (paintOffset != null) {
pushTransform(Matrix4.translationValues(-paintOffset.dx, -paintOffset.dy, 0));

}
public class SliverHitTestEntry : HitTestEntry {
public SliverHitTestEntry(RenderSliver target,
public SliverHitTestEntry(
RenderSliver target,
float mainAxisPosition = 0.0f,
float crossAxisPosition = 0.0f
) : base(target) {

}
D.assert(!debugDoingThisResize);
string contract = "", violation = "", hint = "";
DiagnosticsNode contract, violation, hint = null;
violation = "It appears that the geometry setter was called from performLayout().";
hint = "";
violation = new ErrorDescription("It appears that the geometry setter was called from performLayout().");
violation =
"The geometry setter was called from outside layout (neither performResize() nor performLayout() were being run for this object).";
violation = new ErrorDescription("The geometry setter was called from outside layout (neither performResize() nor performLayout() were being run for this object).");
hint =
"Only the object itself can set its geometry. It is a contract violation for other objects to set it.";
hint = new ErrorDescription("Only the object itself can set its geometry. It is a contract violation for other objects to set it.");
contract =
"Because this RenderSliver has sizedByParent set to true, it must set its geometry in performResize().";
contract = new ErrorDescription("Because this RenderSliver has sizedByParent set to true, it must set its geometry in performResize().");
contract =
"Because this RenderSliver has sizedByParent set to false, it must set its geometry in performLayout().";
contract = new ErrorDescription(
"Because this RenderSliver has sizedByParent set to false, it must set its geometry in performLayout().");
throw new UIWidgetsError(
"RenderSliver geometry setter called incorrectly.\n" +
violation + "\n" +
hint + "\n" +
contract + "\n" +
"The RenderSliver in question is:\n" +
" " + this
);
List<DiagnosticsNode> information = new List<DiagnosticsNode>();
information.Add(new ErrorSummary("RenderSliver geometry setter called incorrectly."));
information.Add(violation);
if (hint != null) {
information.Add(hint);
}
information.Add(contract);
information.Add(describeForError("The RenderSliver in question is"));
throw new UIWidgetsError(information);
});
_geometry = value;

public override Rect paintBounds {
get {
D.assert(constraints.axis != null);
switch (constraints.axis) {
case Axis.horizontal:
return Rect.fromLTWH(

geometry.paintExtent
);
}
// D.assert(false);
return null;
}
}

informationCollector: infoCollector);
});
D.assert(() => {
if (geometry.paintExtent > constraints.remainingPaintExtent) {
if (geometry.paintOrigin + geometry.paintExtent > constraints.remainingPaintExtent) {
"paintExtent", geometry.paintExtent));
"paintOrigin + paintExtent", geometry.paintOrigin + geometry.paintExtent));
throw new UIWidgetsError(
diagnosticInfo
);
throw new UIWidgetsError(diagnosticInfo);
}
return true;

get { return 0.0f; }
}
public virtual bool hitTest(SliverHitTestResult result, float mainAxisPosition = 0, float crossAxisPosition = 0) {
public virtual bool hitTest(SliverHitTestResult result, float mainAxisPosition = 0.0f, float crossAxisPosition = 0.0f) {
if (mainAxisPosition >= 0.0f && mainAxisPosition < geometry.hitTestExtent &&
crossAxisPosition >= 0.0f && crossAxisPosition < constraints.crossAxisExtent) {
if (hitTestChildren(result, mainAxisPosition: mainAxisPosition,

return false;
}
protected virtual bool hitTestSelf(float mainAxisPosition = 0, float crossAxisPosition = 0) {
protected virtual bool hitTestSelf(float mainAxisPosition = 0.0f, float crossAxisPosition = 0.0f) {
protected virtual bool hitTestChildren(SliverHitTestResult result, float mainAxisPosition = 0,
float crossAxisPosition = 0) {
protected virtual bool hitTestChildren(SliverHitTestResult result, float mainAxisPosition = 0.0f,
float crossAxisPosition = 0.0f) {
return false;
}

case AxisDirection.left:
return new Size(-geometry.paintExtent, constraints.crossAxisExtent);
}
//D.assert(false);
return null;
}

});
}
public override void handleEvent(PointerEvent evt, HitTestEntry entry) {
entry = (SliverHitTestEntry) entry;
}
public override void handleEvent(PointerEvent evt, HitTestEntry entry) { }
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<SliverGeometry>("geometry", geometry));

public static class RenderSliverHelpers {
public static bool _getRightWayUp(SliverConstraints constraints) {
D.assert(constraints != null);
bool rightWayUp = true;
switch (constraints.axisDirection) {

float? absoluteCrossAxisPosition = crossAxisPosition - crossAxisDelta;
Offset paintOffset = null;
Offset transformedPosition = null;
D.assert(it.constraints.axis != null);
switch (it.constraints.axis) {
case Axis.horizontal:
if (!rightWayUp) {

new Offset(-(geometry.scrollExtent - (geometry.paintExtent + constraints.scrollOffset)),
0.0f);
break;
D.assert(childParentData.paintOffset != null);
}
protected override bool hitTestChildren(SliverHitTestResult result, float mainAxisPosition = 0.0f,

return;
}
SliverConstraints constraints = this.constraints;
child.layout(constraints.asBoxConstraints(), parentUsesSize: true);
float childExtent = 0.0f;

95
com.unity.uiwidgets/Runtime/rendering/sliver_fill.cs


using UnityEngine;
namespace Unity.UIWidgets.rendering {
public class RenderSliverFillViewport : RenderSliverFixedExtentBoxAdaptor {
public RenderSliverFillViewport(
RenderSliverBoxChildManager childManager = null,
float viewportFraction = 1.0f
) :
base(childManager: childManager) {
D.assert(viewportFraction > 0.0);
_viewportFraction = viewportFraction;
}
public override float itemExtent {
get { return constraints.viewportMainAxisExtent * viewportFraction; }
set { }
}
float _viewportFraction;
public float viewportFraction {
get { return _viewportFraction; }
set {
if (_viewportFraction == value) {
return;
}
_viewportFraction = value;
markNeedsLayout();
}
}
}
class RenderSliverFillRemainingWithScrollable : RenderSliverSingleBoxAdapter {
public RenderSliverFillRemainingWithScrollable(RenderBox child = null) : base(child: child) {

setChildParentData(child, constraints, geometry);
}
}
public class RenderSliverFillViewport : RenderSliverFixedExtentBoxAdaptor {
public RenderSliverFillViewport(
RenderSliverBoxChildManager childManager = null,
float viewportFraction = 1.0f
) :
base(childManager: childManager) {
D.assert(viewportFraction > 0.0);
_viewportFraction = viewportFraction;
}
public override float itemExtent {
get { return constraints.viewportMainAxisExtent * viewportFraction; }
set { }
}
float _viewportFraction;
public float viewportFraction {
get { return _viewportFraction; }
set {
if (_viewportFraction == value) {
return;
}
_viewportFraction = value;
markNeedsLayout();
}
}
float _padding {
get { return (1.0f - viewportFraction) * constraints.viewportMainAxisExtent * 0.5f; }
}
protected override float indexToLayoutOffset(float itemExtent, int index) {
return _padding + base.indexToLayoutOffset(itemExtent, index);
}
protected override int getMinChildIndexForScrollOffset(float scrollOffset, float itemExtent) {
return base.getMinChildIndexForScrollOffset(Mathf.Max(scrollOffset - _padding, 0.0f), itemExtent);
}
protected override int getMaxChildIndexForScrollOffset(float scrollOffset, float itemExtent) {
return base.getMaxChildIndexForScrollOffset(Mathf.Max(scrollOffset - _padding, 0.0f), itemExtent);
}
protected override float estimateMaxScrollOffset(SliverConstraints constraints,
int firstIndex = 0,
int lastIndex = 0,
float leadingScrollOffset = 0.0f,
float trailingScrollOffset = 0.0f
) {
float padding = _padding;
return childManager.estimateMaxScrollOffset(
constraints,
firstIndex: firstIndex,
lastIndex: lastIndex,
leadingScrollOffset: leadingScrollOffset - padding,
trailingScrollOffset: trailingScrollOffset - padding
) + padding + padding;
}
}
public class RenderSliverFillRemainingAndOverscroll : RenderSliverSingleBoxAdapter {

setChildParentData(child, constraints, geometry);
}
}
}

95
com.unity.uiwidgets/Runtime/rendering/sliver_fixed_extent_list.cs


}
protected virtual int getMinChildIndexForScrollOffset(float scrollOffset, float itemExtent) {
return itemExtent > 0.0 ? Mathf.Max(0, (int) (scrollOffset / itemExtent)) : 0;
if (itemExtent > 0.0) {
float actual = scrollOffset / itemExtent;
int round = actual.round();
if ((actual - round).abs() < foundation_.precisionErrorTolerance) {
return round;
}
return actual.floor();
}
return 0;
}
protected virtual int getMaxChildIndexForScrollOffset(float scrollOffset, float itemExtent) {

protected virtual float estimateMaxScrollOffset(SliverConstraints constraints,
protected virtual float estimateMaxScrollOffset(
SliverConstraints constraints,
int firstIndex = 0,
int lastIndex = 0,
float leadingScrollOffset = 0.0f,

return childManager.childCount.Value * itemExtent;
}
int _calculateLeadingGarbage(int firstIndex) {
RenderBox walker = firstChild;
int leadingGarbage = 0;
while(walker != null && indexOf(walker) < firstIndex){
leadingGarbage += 1;
walker = childAfter(walker);
}
return leadingGarbage;
}
int _calculateTrailingGarbage(int targetLastIndex) {
RenderBox walker = lastChild;
int trailingGarbage = 0;
while(walker != null && indexOf(walker) > targetLastIndex){
trailingGarbage += 1;
walker = childBefore(walker);
}
return trailingGarbage;
}
SliverConstraints constraints = this.constraints;
childManager.didStartLayout();
childManager.setDidUnderflow(false);

: (int?) null;
if (firstChild != null) {
int oldFirstIndex = indexOf(firstChild);
int oldLastIndex = indexOf(lastChild);
int leadingGarbage = (firstIndex - oldFirstIndex).clamp(0, childCount);
int trailingGarbage =
targetLastIndex == null ? 0 : (oldLastIndex - targetLastIndex.Value).clamp(0, childCount);
int leadingGarbage = _calculateLeadingGarbage(firstIndex);
int trailingGarbage = _calculateTrailingGarbage(targetLastIndex.Value);
collectGarbage(leadingGarbage, trailingGarbage);
}
else {

if (firstChild == null) {
if (!addInitialChild(index: firstIndex,
layoutOffset: indexToLayoutOffset(itemExtent, firstIndex))) {
float max = computeMaxScrollOffset(constraints, itemExtent);
float max;
if (childManager.childCount != null) {
max = computeMaxScrollOffset(constraints, itemExtent);
}
else if (firstIndex <= 0) {
max = 0.0f;
}
else {
// We will have to find it manually.
int possibleFirstIndex = firstIndex - 1;
while (
possibleFirstIndex > 0 &&
!addInitialChild(
index: possibleFirstIndex,
layoutOffset: indexToLayoutOffset(itemExtent, possibleFirstIndex)
)
) {
possibleFirstIndex -= 1;
}
max = possibleFirstIndex * itemExtent;
}
geometry = new SliverGeometry(
scrollExtent: max,
maxPaintExtent: max

trailingChildWithLayout = firstChild;
}
while (targetLastIndex == null || indexOf(trailingChildWithLayout) < targetLastIndex) {
float estimatedMaxScrollOffset = float.PositiveInfinity;
for (int index = indexOf(trailingChildWithLayout) + 1;
targetLastIndex == null || index <= targetLastIndex;
++index) {
if (child == null) {
if (child == null || indexOf(child) != index) {
estimatedMaxScrollOffset = index * itemExtent;
break;
}
}

trailingChildWithLayout = child;
D.assert(child != null);
D.assert(childParentData.index == index);
childParentData.layoutOffset = indexToLayoutOffset(itemExtent, childParentData.index);
}

D.assert(firstIndex == 0 || childScrollOffset(firstChild) <= scrollOffset);
D.assert(firstIndex == 0 ||
childScrollOffset(firstChild) - scrollOffset <= foundation_.precisionErrorTolerance);
float estimatedMaxScrollOffset = estimateMaxScrollOffset(
constraints,
firstIndex: firstIndex,
lastIndex: lastIndex,
leadingScrollOffset: leadingScrollOffset,
trailingScrollOffset: trailingScrollOffset
estimatedMaxScrollOffset = Mathf.Min(
estimatedMaxScrollOffset,
estimateMaxScrollOffset(
constraints,
firstIndex: firstIndex,
lastIndex: lastIndex,
leadingScrollOffset: leadingScrollOffset,
trailingScrollOffset: trailingScrollOffset
)
);
float paintExtent = calculatePaintOffset(

40
com.unity.uiwidgets/Runtime/rendering/sliver_grid.cs


using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;

}
public override string ToString() {
return "SliverGridGeometry(" +
"scrollOffset: $scrollOffset, " +
"crossAxisOffset: $crossAxisOffset, " +
"mainAxisExtent: $mainAxisExtent, " +
"crossAxisExtent: $crossAxisExtent" +
")";
List<string> properties = new List<string>();
properties.Add("scrollOffset: $scrollOffset");
properties.Add("crossAxisOffset: $crossAxisOffset");
properties.Add("mainAxisExtent: $mainAxisExtent");
properties.Add("crossAxisExtent: $crossAxisExtent");
return $"SliverGridGeometry({string.Join(", ",properties)})";
}
}

float? childCrossAxisExtent = null,
bool? reverseCrossAxis = null
) {
D.assert(crossAxisCount != null && crossAxisCount > 0);
D.assert(mainAxisStride != null && mainAxisStride >= 0);
D.assert(crossAxisStride != null && crossAxisStride >= 0);
D.assert(childMainAxisExtent != null && childMainAxisExtent >= 0);
D.assert(childCrossAxisExtent != null && childCrossAxisExtent >= 0);
D.assert(crossAxisCount > 0);
D.assert(mainAxisStride >= 0);
D.assert(crossAxisStride >= 0);
D.assert(childMainAxisExtent >= 0);
D.assert(childCrossAxisExtent >= 0);
D.assert(reverseCrossAxis != null);
this.crossAxisCount = crossAxisCount;
this.mainAxisStride = mainAxisStride;

}
public abstract class SliverGridDelegate {
protected SliverGridDelegate() { }
public abstract SliverGridLayout getLayout(SliverConstraints constraints);
public abstract bool shouldRelayout(SliverGridDelegate oldDelegate);

public float crossAxisOffset;
public override string ToString() {
return "crossAxisOffset=$crossAxisOffset; ${base.ToString()}";
return $"crossAxisOffset={crossAxisOffset}; {base.ToString()}";
}
}

SliverGridGeometry gridGeometry = layout.getGeometryForChildIndex(index);
BoxConstraints childConstraints = gridGeometry.getBoxConstraints(constraints);
RenderBox child = childAfter(trailingChildWithLayout);
if (child == null) {
if (child == null || indexOf(child) != index) {
child = insertAndLayoutChild(childConstraints, after: trailingChildWithLayout);
if (child == null) {
break;

}
trailingChildWithLayout = child;
D.assert(child != null);
SliverGridParentData childParentData = child.parentData as SliverGridParentData;
childParentData.layoutOffset = gridGeometry.scrollOffset ?? 0.0f;
childParentData.crossAxisOffset = gridGeometry.crossAxisOffset ?? 0.0f;

D.assert(indexOf(firstChild) == firstIndex);
D.assert(targetLastIndex == null || lastIndex <= targetLastIndex);
float estimatedTotalExtent = childManager.estimateMaxScrollOffset(constraints,
float estimatedTotalExtent = childManager.estimateMaxScrollOffset(
constraints,
firstIndex: firstIndex,
lastIndex: lastIndex,
leadingScrollOffset: leadingScrollOffset ?? 0.0f,

float paintExtent = calculatePaintOffset(constraints,
float paintExtent = calculatePaintOffset(
constraints,
float cacheExtent = calculateCacheOffset(constraints,
float cacheExtent = calculateCacheOffset(
constraints,
from: leadingScrollOffset ?? 0.0f,
to: trailingScrollOffset ?? 0.0f
);

2
com.unity.uiwidgets/Runtime/rendering/sliver_list.cs


}
else {
float firstChildScrollOffset = earliestScrollOffset - paintExtentOf(firstChild);
if (firstChildScrollOffset < -SliverGeometry.precisionErrorTolerance) {
if (firstChildScrollOffset < -foundation_.precisionErrorTolerance) {
float correction = 0.0f;
while (earliestUsefulChild != null) {
D.assert(firstChild == earliestUsefulChild);

5
com.unity.uiwidgets/Runtime/rendering/sliver_multi_box_adaptor.cs


}
public bool keptAlive { get; }
}
}
public interface RenderSliverBoxChildManager {
void createChild(int index, RenderBox after = null);

public bool debugChildIntegrityEnabled {
get { return _debugChildIntegrityEnabled;}
set {
D.assert(value != null);
D.assert(() =>{
_debugChildIntegrityEnabled = value;
return _debugVerifyChildOrder() &&

childManager.didAdoptChild(child);
D.assert(()=> {
if (_keepAliveBucket.ContainsKey(childParentData.index))
_debugDanglingKeepAlives.Add(_keepAliveBucket[childParentData.index]);
_debugDanglingKeepAlives.Add(_keepAliveBucket.getOrDefault(childParentData.index));
return true;
});
_keepAliveBucket[childParentData.index] = child;

正在加载...
取消
保存