Unverified Commit 0cb5772e authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[framework] allow other RenderObjects to behave like repaint boundaries (#101952)

parent 61bbaaa4
...@@ -843,7 +843,14 @@ class RenderOpacity extends RenderProxyBox { ...@@ -843,7 +843,14 @@ class RenderOpacity extends RenderProxyBox {
super(child); super(child);
@override @override
bool get alwaysNeedsCompositing => child != null && (_alpha > 0); bool get isRepaintBoundary => child != null && (_alpha > 0);
@override
OffsetLayer updateCompositedLayer({required covariant OpacityLayer? oldLayer}) {
final OpacityLayer updatedLayer = oldLayer ?? OpacityLayer();
updatedLayer.alpha = _alpha;
return updatedLayer;
}
int _alpha; int _alpha;
...@@ -864,13 +871,13 @@ class RenderOpacity extends RenderProxyBox { ...@@ -864,13 +871,13 @@ class RenderOpacity extends RenderProxyBox {
assert(value >= 0.0 && value <= 1.0); assert(value >= 0.0 && value <= 1.0);
if (_opacity == value) if (_opacity == value)
return; return;
final bool didNeedCompositing = alwaysNeedsCompositing; final bool wasRepaintBoundary = isRepaintBoundary;
final bool wasVisible = _alpha != 0; final bool wasVisible = _alpha != 0;
_opacity = value; _opacity = value;
_alpha = ui.Color.getAlphaFromOpacity(_opacity); _alpha = ui.Color.getAlphaFromOpacity(_opacity);
if (didNeedCompositing != alwaysNeedsCompositing) if (wasRepaintBoundary != isRepaintBoundary)
markNeedsCompositingBitsUpdate(); markNeedsCompositingBitsUpdate();
markNeedsPaint(); markNeedsCompositedLayerUpdate();
if (wasVisible != (_alpha != 0) && !alwaysIncludeSemantics) if (wasVisible != (_alpha != 0) && !alwaysIncludeSemantics)
markNeedsSemanticsUpdate(); markNeedsSemanticsUpdate();
} }
...@@ -891,19 +898,10 @@ class RenderOpacity extends RenderProxyBox { ...@@ -891,19 +898,10 @@ class RenderOpacity extends RenderProxyBox {
@override @override
void paint(PaintingContext context, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (_alpha == 0) {
if (_alpha == 0) { return;
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
return;
}
assert(needsCompositing);
layer = context.pushOpacity(offset, _alpha, super.paint, oldLayer: layer as OpacityLayer?);
assert(() {
layer!.debugCreator = debugCreator;
return true;
}());
} }
super.paint(context, offset);
} }
@override @override
...@@ -929,8 +927,15 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil ...@@ -929,8 +927,15 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil
int? _alpha; int? _alpha;
@override @override
bool get alwaysNeedsCompositing => child != null && _currentlyNeedsCompositing!; bool get isRepaintBoundary => child != null && _currentlyIsRepaintBoundary!;
bool? _currentlyNeedsCompositing; bool? _currentlyIsRepaintBoundary;
@override
OffsetLayer updateCompositedLayer({required covariant OpacityLayer? oldLayer}) {
final OpacityLayer updatedLayer = oldLayer ?? OpacityLayer();
updatedLayer.alpha = _alpha;
return updatedLayer;
}
/// The animation that drives this render object's opacity. /// The animation that drives this render object's opacity.
/// ///
...@@ -990,11 +995,11 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil ...@@ -990,11 +995,11 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil
final int? oldAlpha = _alpha; final int? oldAlpha = _alpha;
_alpha = ui.Color.getAlphaFromOpacity(opacity.value); _alpha = ui.Color.getAlphaFromOpacity(opacity.value);
if (oldAlpha != _alpha) { if (oldAlpha != _alpha) {
final bool? didNeedCompositing = _currentlyNeedsCompositing; final bool? wasRepaintBoundary = _currentlyIsRepaintBoundary;
_currentlyNeedsCompositing = _alpha! > 0; _currentlyIsRepaintBoundary = _alpha! > 0;
if (child != null && didNeedCompositing != _currentlyNeedsCompositing) if (child != null && wasRepaintBoundary != _currentlyIsRepaintBoundary)
markNeedsCompositingBitsUpdate(); markNeedsCompositingBitsUpdate();
markNeedsPaint(); markNeedsCompositedLayerUpdate();
if (oldAlpha == 0 || _alpha == 0) if (oldAlpha == 0 || _alpha == 0)
markNeedsSemanticsUpdate(); markNeedsSemanticsUpdate();
} }
...@@ -1002,19 +1007,10 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil ...@@ -1002,19 +1007,10 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil
@override @override
void paint(PaintingContext context, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (_alpha == 0) {
if (_alpha == 0) { return;
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
return;
}
assert(needsCompositing);
layer = context.pushOpacity(offset, _alpha!, super.paint, oldLayer: layer as OpacityLayer?);
assert(() {
layer!.debugCreator = debugCreator;
return true;
}());
} }
super.paint(context, offset);
} }
@override @override
......
...@@ -545,11 +545,9 @@ class _HeroFlight { ...@@ -545,11 +545,9 @@ class _HeroFlight {
bottom: offsets.bottom, bottom: offsets.bottom,
left: offsets.left, left: offsets.left,
child: IgnorePointer( child: IgnorePointer(
child: RepaintBoundary( child: FadeTransition(
child: FadeTransition( opacity: _heroOpacity,
opacity: _heroOpacity, child: child,
child: child,
),
), ),
), ),
); );
......
...@@ -231,7 +231,8 @@ void main() { ...@@ -231,7 +231,8 @@ void main() {
rootLayer, rootLayer,
const Rect.fromLTWH(0, 0, 500, 500), const Rect.fromLTWH(0, 0, 500, 500),
); );
root.paint(context, const Offset(40, 40)); context.paintChild(root, const Offset(40, 40));
final OpacityLayer opacityLayer = rootLayer.firstChild! as OpacityLayer; final OpacityLayer opacityLayer = rootLayer.firstChild! as OpacityLayer;
expect(opacityLayer.offset, const Offset(40, 40)); expect(opacityLayer.offset, const Offset(40, 40));
debugDisableOpacityLayers = false; debugDisableOpacityLayers = false;
......
...@@ -561,6 +561,178 @@ void main() { ...@@ -561,6 +561,178 @@ void main() {
// The follower is still hit testable because there is a leader layer. // The follower is still hit testable because there is a leader layer.
expect(follower.hitTest(hitTestResult, position: Offset.zero), isTrue); expect(follower.hitTest(hitTestResult, position: Offset.zero), isTrue);
}); });
test('RenderObject can become a repaint boundary', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary();
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
renderBox.isRepaintBoundary = true;
renderBox.markNeedsCompositingBitsUpdate();
renderBox.markNeedsCompositedLayerUpdate();
pumpFrame(phase: EnginePhase.composite);
// The first time the render object becomes a repaint boundary
// we must repaint from the parent to allow the layer to be
// created.
expect(childBox.paintCount, 2);
expect(renderBox.paintCount, 2);
expect(renderBox.debugLayer, isA<OffsetLayer>());
renderBox.markNeedsCompositedLayerUpdate();
expect(renderBox.debugNeedsPaint, false);
expect(renderBox.debugNeedsCompositedLayerUpdate, true);
pumpFrame(phase: EnginePhase.composite);
// The second time the layer exists and we can skip paint.
expect(childBox.paintCount, 2);
expect(renderBox.paintCount, 2);
expect(renderBox.debugLayer, isA<OffsetLayer>());
renderBox.isRepaintBoundary = false;
renderBox.markNeedsCompositingBitsUpdate();
pumpFrame(phase: EnginePhase.composite);
// Once it stops being a repaint boundary we must repaint to
// remove the layer. its required that the render object
// perform this action in paint.
expect(childBox.paintCount, 3);
expect(renderBox.paintCount, 3);
expect(renderBox.debugLayer, null);
// When the render object is not a repaint boundary, calling
// markNeedsLayerPropertyUpdate is the same as calling
// markNeedsPaint.
renderBox.markNeedsCompositedLayerUpdate();
expect(renderBox.debugNeedsPaint, true);
expect(renderBox.debugNeedsCompositedLayerUpdate, true);
});
test('RenderObject with repaint boundary asserts when a composited layer is replaced during layer property update', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
// Ignore old layer.
childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
return TestOffsetLayerA();
};
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
renderBox.markNeedsCompositedLayerUpdate();
pumpFrame(phase: EnginePhase.composite, onErrors: expectAssertionError);
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102086
test('RenderObject with repaint boundary asserts when a composited layer is replaced during painting', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
// Ignore old layer.
childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
return TestOffsetLayerA();
};
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
renderBox.markNeedsPaint();
pumpFrame(phase: EnginePhase.composite, onErrors: expectAssertionError);
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102086
test('RenderObject with repaint boundary asserts when a composited layer tries to update its own offset', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
// Ignore old layer.
childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
return (oldLayer ?? TestOffsetLayerA())..offset = const Offset(2133, 4422);
};
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
renderBox.markNeedsPaint();
pumpFrame(phase: EnginePhase.composite, onErrors: expectAssertionError);
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102086
test('RenderObject markNeedsPaint while repaint boundary, and then updated to no longer be a repaint boundary with '
'calling markNeedsCompositingBitsUpdate 1', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
// Ignore old layer.
childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
return oldLayer ?? TestOffsetLayerA();
};
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
childBox.markNeedsPaint();
childBox.isRepaintBoundary = false;
childBox.markNeedsCompositingBitsUpdate();
expect(() => pumpFrame(phase: EnginePhase.composite), returnsNormally);
});
test('RenderObject markNeedsPaint while repaint boundary, and then updated to no longer be a repaint boundary with '
'calling markNeedsCompositingBitsUpdate 2', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
// Ignore old layer.
childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
return oldLayer ?? TestOffsetLayerA();
};
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
childBox.isRepaintBoundary = false;
childBox.markNeedsCompositingBitsUpdate();
childBox.markNeedsPaint();
expect(() => pumpFrame(phase: EnginePhase.composite), returnsNormally);
});
test('RenderObject markNeedsPaint while repaint boundary, and then updated to no longer be a repaint boundary with '
'calling markNeedsCompositingBitsUpdate 3', () {
final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
// Ignore old layer.
childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
return oldLayer ?? TestOffsetLayerA();
};
layout(renderBox, phase: EnginePhase.composite);
expect(childBox.paintCount, 1);
expect(renderBox.paintCount, 1);
childBox.isRepaintBoundary = false;
childBox.markNeedsCompositedLayerUpdate();
childBox.markNeedsCompositingBitsUpdate();
expect(() => pumpFrame(phase: EnginePhase.composite), returnsNormally);
});
} }
class _TestRectClipper extends CustomClipper<Rect> { class _TestRectClipper extends CustomClipper<Rect> {
...@@ -631,3 +803,38 @@ class _TestSemanticsUpdateRenderFractionalTranslation extends RenderFractionalTr ...@@ -631,3 +803,38 @@ class _TestSemanticsUpdateRenderFractionalTranslation extends RenderFractionalTr
super.markNeedsSemanticsUpdate(); super.markNeedsSemanticsUpdate();
} }
} }
class ConditionalRepaintBoundary extends RenderProxyBox {
ConditionalRepaintBoundary({this.isRepaintBoundary = false, RenderBox? child}) : super(child);
@override
bool isRepaintBoundary = false;
OffsetLayer Function(OffsetLayer?)? offsetLayerFactory;
int paintCount = 0;
@override
OffsetLayer updateCompositedLayer({required covariant OffsetLayer? oldLayer}) {
if (offsetLayerFactory != null) {
return offsetLayerFactory!.call(oldLayer);
}
return super.updateCompositedLayer(oldLayer: oldLayer);
}
@override
void paint(PaintingContext context, Offset offset) {
paintCount += 1;
super.paint(context, offset);
}
}
class TestOffsetLayerA extends OffsetLayer {}
void expectAssertionError() {
final FlutterErrorDetails errorDetails = TestRenderingFlutterBinding.instance.takeFlutterErrorDetails()!;
final bool asserted = errorDetails.toString().contains('Failed assertion');
if (!asserted) {
FlutterError.reportError(errorDetails);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('RenderAnimatedOpacityMixin avoids repainting child as it animates', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
final AnimationController controller = AnimationController(vsync: const TestVSync(), duration: const Duration(seconds: 1));
final Tween<double> opacityTween = Tween<double>(begin: 0, end: 1);
await tester.pumpWidget(
Container(
color: Colors.red,
child: FadeTransition(
opacity: controller.drive(opacityTween),
child: const TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 0);
controller.forward();
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(RenderTestObject.paintCount, 1);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(RenderTestObject.paintCount, 1);
controller.stop();
await tester.pump();
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderAnimatedOpacityMixin allows opacity layer to be disposed when animating to 0 opacity', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
final AnimationController controller = AnimationController(vsync: const TestVSync(), duration: const Duration(seconds: 1));
final Tween<double> opacityTween = Tween<double>(begin: 1, end: 0);
await tester.pumpWidget(
Container(
color: Colors.red,
child: FadeTransition(
opacity: controller.drive(opacityTween),
child: const TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
expect(tester.layers, contains(isA<OpacityLayer>()));
controller.forward();
await tester.pump();
await tester.pump(const Duration(seconds: 2));
expect(RenderTestObject.paintCount, 1);
controller.stop();
await tester.pump();
expect(tester.layers, isNot(contains(isA<OpacityLayer>())));
});
}
class TestWidget extends SingleChildRenderObjectWidget {
const TestWidget({super.key, super.child});
@override
RenderObject createRenderObject(BuildContext context) {
return RenderTestObject();
}
}
class RenderTestObject extends RenderProxyBox {
static int paintCount = 0;
@override
void paint(PaintingContext context, Offset offset) {
paintCount += 1;
super.paint(context, offset);
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('RenderOpacity acts as a repaint boundary for changes above the widget when partially opaque', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity acts as a repaint boundary for changes above the widget when fully opaque', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 1,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 1,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity can update its opacity without repainting its child - partially opaque to partially opaque', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 0.9,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity can update its opacity without repainting its child - partially opaque to fully opaque', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 1,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity can update its opacity without repainting its child - fully opaque to partially opaque', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 1,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity can update its opacity without repainting its child - fully opaque to fully transparent', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 1,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 0,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity must paint child - fully transparent to partially opaque', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 0,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 0);
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity allows child to update without updating parent', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
TestWidget(
child: Opacity(
opacity: 0.5,
child: Container(
color: Colors.red,
),
),
)
);
expect(RenderTestObject.paintCount, 1);
await tester.pumpWidget(
TestWidget(
child: Opacity(
opacity: 0.5,
child: Container(
color: Colors.blue,
),
),
)
);
expect(RenderTestObject.paintCount, 1);
});
testWidgets('RenderOpacity disposes of opacity layer when opacity is updated to 0', (WidgetTester tester) async {
RenderTestObject.paintCount = 0;
await tester.pumpWidget(
Container(
color: Colors.red,
child: const Opacity(
opacity: 0.5,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
expect(tester.layers, contains(isA<OpacityLayer>()));
await tester.pumpWidget(
Container(
color: Colors.blue,
child: const Opacity(
opacity: 0,
child: TestWidget(),
),
)
);
expect(RenderTestObject.paintCount, 1);
expect(tester.layers, isNot(contains(isA<OpacityLayer>())));
});
}
class TestWidget extends SingleChildRenderObjectWidget {
const TestWidget({super.key, super.child});
@override
RenderObject createRenderObject(BuildContext context) {
return RenderTestObject();
}
}
class RenderTestObject extends RenderProxyBox {
static int paintCount = 0;
@override
void paint(PaintingContext context, Offset offset) {
paintCount += 1;
super.paint(context, offset);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment