Unverified Commit e5194724 authored by liyuqian's avatar liyuqian Committed by GitHub

Don't add empty OpacityLayer to the engine (#31520)

Fixes https://github.com/flutter/flutter/issues/31517
parent d4e4726a
...@@ -1242,6 +1242,9 @@ class TransformLayer extends OffsetLayer { ...@@ -1242,6 +1242,9 @@ class TransformLayer extends OffsetLayer {
/// When debugging, setting [debugDisableOpacityLayers] to true will cause this /// When debugging, setting [debugDisableOpacityLayers] to true will cause this
/// layer to be skipped (directly replaced by its children). This can be helpful /// layer to be skipped (directly replaced by its children). This can be helpful
/// to track down the cause of performance problems. /// to track down the cause of performance problems.
///
/// Try to avoid an [OpacityLayer] with no children. Remove that layer if
/// possible to save some tree walks.
class OpacityLayer extends ContainerLayer { class OpacityLayer extends ContainerLayer {
/// Creates an opacity layer. /// Creates an opacity layer.
/// ///
...@@ -1281,9 +1284,9 @@ class OpacityLayer extends ContainerLayer { ...@@ -1281,9 +1284,9 @@ class OpacityLayer extends ContainerLayer {
@override @override
ui.EngineLayer addToScene(ui.SceneBuilder builder, [ Offset layerOffset = Offset.zero ]) { ui.EngineLayer addToScene(ui.SceneBuilder builder, [ Offset layerOffset = Offset.zero ]) {
bool enabled = true; bool enabled = firstChild != null; // don't add this layer if there's no child
assert(() { assert(() {
enabled = !debugDisableOpacityLayers; enabled = enabled && !debugDisableOpacityLayers;
return true; return true;
}()); }());
if (enabled) if (enabled)
......
...@@ -183,4 +183,14 @@ void main() { ...@@ -183,4 +183,14 @@ void main() {
skip: !Platform.isLinux, skip: !Platform.isLinux,
); );
}); });
testWidgets('empty opacity does not crash', (WidgetTester tester) async {
await tester.pumpWidget(
RepaintBoundary(child: Opacity(opacity: 0.5, child: Container())),
);
final Element element = find.byType(RepaintBoundary).first.evaluate().single;
// The following line will send the layer to engine and cause crash if an
// empty opacity layer is sent.
await element.renderObject.layer.toImage(Rect.fromLTRB(0.0, 0.0, 1.0, 1.0));
});
} }
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