Skip to content

Commit

Permalink
feat: optimization matrix algorithm (#516)
Browse files Browse the repository at this point in the history
Optimize the time-consuming logic of matrix calculations, Fix jitter
problem under long list

---------

Co-authored-by: Edmond <[email protected]>
Co-authored-by: edmond.l <[email protected]>
Co-authored-by: TIANCHENG <[email protected]>
  • Loading branch information
4 people authored Nov 23, 2023
1 parent 8399d53 commit 877be25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
29 changes: 21 additions & 8 deletions webf/lib/src/rendering/box_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,31 @@ class RenderLayoutParentData extends ContainerBoxParentData<RenderBox> {
//
// ReturgetLayoutTransformTolocal layout coordinate system to the
// coordinate system of `ancestor`.
Matrix4 getLayoutTransformTo(RenderObject current, RenderObject ancestor, {bool excludeScrollOffset = false}) {
Offset getLayoutTransformTo(RenderObject current, RenderObject ancestor, {bool excludeScrollOffset = false}) {
final List<RenderObject> renderers = <RenderObject>[];
for (RenderObject renderer = current; renderer != ancestor; renderer = renderer.parent!) {
renderers.add(renderer);
assert(renderer.parent != null);
}
renderers.add(ancestor);
Offset offset = Offset.zero;

final Matrix4 transform = Matrix4.identity();
for (int index = renderers.length - 1; index > 0; index -= 1) {
RenderObject parentRenderer = renderers[index];
RenderObject childRenderer = renderers[index - 1];
// Apply the layout transform for renderBoxModel and fallback to paint transform for other renderObject type.
if (parentRenderer is RenderBoxModel) {
parentRenderer.applyLayoutTransform(childRenderer, transform, excludeScrollOffset);
} else {
parentRenderer.applyPaintTransform(childRenderer, transform);
offset += parentRenderer.obtainLayoutTransform(childRenderer, excludeScrollOffset);
} else if (parentRenderer is RenderBox) {
assert(childRenderer.parent == parentRenderer);
if (childRenderer.parentData is BoxParentData) {
offset += (childRenderer.parentData as BoxParentData).offset;
}
}
}

return transform;
return offset;
}

/// Modified from Flutter rendering/box.dart.
Expand Down Expand Up @@ -1159,6 +1163,17 @@ class RenderBoxModel extends RenderBox
transform.translate(offset.dx, offset.dy);
}

Offset obtainLayoutTransform(RenderObject child, bool excludeScrollOffset) {
assert(child.parent == this);
assert(child.parentData is BoxParentData);
final BoxParentData childParentData = child.parentData! as BoxParentData;
Offset offset = childParentData.offset;
if (excludeScrollOffset) {
offset -= Offset(scrollLeft, scrollTop);
}
return offset;
}

// The max scrollable size.
Size _maxScrollableSize = Size.zero;

Expand Down Expand Up @@ -1355,9 +1370,7 @@ class RenderBoxModel extends RenderBox
double ancestorBorderLeft = ancestor.renderStyle.borderLeftWidth?.computedValue ?? 0;
Offset ancestorBorderWidth = Offset(ancestorBorderLeft, ancestorBorderTop);

return MatrixUtils.transformPoint(
getLayoutTransformTo(this, ancestor, excludeScrollOffset: excludeScrollOffset), point) -
ancestorBorderWidth;
return getLayoutTransformTo(this, ancestor, excludeScrollOffset: excludeScrollOffset) + point - ancestorBorderWidth;
}

bool _hasLocalBackgroundImage(CSSRenderStyle renderStyle) {
Expand Down
3 changes: 1 addition & 2 deletions webf/lib/src/rendering/render_position_holder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class RenderPositionPlaceholder extends RenderPreferredSize {
// Get the layout offset of renderObject to its ancestor which does not include the paint offset
// such as scroll or transform.
Offset getOffsetToAncestor(Offset point, RenderObject ancestor, {bool excludeScrollOffset = false}) {
return MatrixUtils.transformPoint(
getLayoutTransformTo(this, ancestor, excludeScrollOffset: excludeScrollOffset), point);
return getLayoutTransformTo(this, ancestor, excludeScrollOffset: excludeScrollOffset) + point;
}
}

Expand Down

0 comments on commit 877be25

Please sign in to comment.