Skip to content

Commit

Permalink
Make sure Opacity widgets/layers do not drop the offset (#89264)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield committed Sep 13, 2021
1 parent 86c79a8 commit 0d0f7a4
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 56 deletions.
48 changes: 21 additions & 27 deletions packages/flutter/lib/src/rendering/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ class TransformLayer extends OffsetLayer {
///
/// 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 OffsetLayer {
/// Creates an opacity layer.
///
/// The [alpha] property must be non-null before the compositing phase of
Expand All @@ -1750,7 +1750,7 @@ class OpacityLayer extends ContainerLayer {
int? alpha,
Offset offset = Offset.zero,
}) : _alpha = alpha,
_offset = offset;
super(offset: offset);

/// The amount to multiply into the alpha channel.
///
Expand All @@ -1764,28 +1764,14 @@ class OpacityLayer extends ContainerLayer {
set alpha(int? value) {
assert(value != null);
if (value != _alpha) {
if (value == 255 || _alpha == 255) {
engineLayer = null;
}
_alpha = value;
markNeedsAddToScene();
}
}

/// Offset from parent in the parent's coordinate system.
Offset? get offset => _offset;
Offset? _offset;
set offset(Offset? value) {
if (value != _offset) {
_offset = value;
markNeedsAddToScene();
}
}

@override
void applyTransform(Layer? child, Matrix4 transform) {
assert(child != null);
assert(transform != null);
transform.translate(offset!.dx, offset!.dy);
}

@override
void addToScene(ui.SceneBuilder builder, [ Offset layerOffset = Offset.zero ]) {
assert(alpha != null);
Expand All @@ -1795,24 +1781,32 @@ class OpacityLayer extends ContainerLayer {
return true;
}());

if (enabled)
final int realizedAlpha = alpha!;
// The type assertions work because the [alpha] setter nulls out the
// engineLayer if it would have changed type (i.e. changed to or from 255).
if (enabled && realizedAlpha < 255) {
assert(_engineLayer is ui.OpacityEngineLayer?);
engineLayer = builder.pushOpacity(
alpha!,
offset: offset! + layerOffset,
realizedAlpha,
offset: offset + layerOffset,
oldLayer: _engineLayer as ui.OpacityEngineLayer?,
);
else
engineLayer = null;
} else {
assert(_engineLayer is ui.OffsetEngineLayer?);
engineLayer = builder.pushOffset(
layerOffset.dx + offset.dx,
layerOffset.dy + offset.dy,
oldLayer: _engineLayer as ui.OffsetEngineLayer?,
);
}
addChildrenToScene(builder);
if (enabled)
builder.pop();
builder.pop();
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(IntProperty('alpha', alpha));
properties.add(DiagnosticsProperty<Offset>('offset', offset));
}
}

Expand Down
16 changes: 2 additions & 14 deletions packages/flutter/lib/src/rendering/proxy_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ class RenderOpacity extends RenderProxyBox {
super(child);

@override
bool get alwaysNeedsCompositing => child != null && (_alpha != 0 && _alpha != 255);
bool get alwaysNeedsCompositing => child != null && (_alpha > 0);

int _alpha;

Expand Down Expand Up @@ -897,12 +897,6 @@ class RenderOpacity extends RenderProxyBox {
layer = null;
return;
}
if (_alpha == 255) {
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
context.paintChild(child!, offset);
return;
}
assert(needsCompositing);
layer = context.pushOpacity(offset, _alpha, super.paint, oldLayer: layer as OpacityLayer?);
}
Expand Down Expand Up @@ -993,7 +987,7 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil
_alpha = ui.Color.getAlphaFromOpacity(opacity.value);
if (oldAlpha != _alpha) {
final bool? didNeedCompositing = _currentlyNeedsCompositing;
_currentlyNeedsCompositing = _alpha! > 0 && _alpha! < 255;
_currentlyNeedsCompositing = _alpha! > 0;
if (child != null && didNeedCompositing != _currentlyNeedsCompositing)
markNeedsCompositingBitsUpdate();
markNeedsPaint();
Expand All @@ -1010,12 +1004,6 @@ mixin RenderAnimatedOpacityMixin<T extends RenderObject> on RenderObjectWithChil
layer = null;
return;
}
if (_alpha == 255) {
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
context.paintChild(child!, offset);
return;
}
assert(needsCompositing);
layer = context.pushOpacity(offset, _alpha!, super.paint, oldLayer: layer as OpacityLayer?);
}
Expand Down
8 changes: 1 addition & 7 deletions packages/flutter/lib/src/rendering/proxy_sliver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class RenderSliverOpacity extends RenderProxySliver {
}

@override
bool get alwaysNeedsCompositing => child != null && (_alpha != 0 && _alpha != 255);
bool get alwaysNeedsCompositing => child != null && (_alpha > 0);

int _alpha;

Expand Down Expand Up @@ -166,12 +166,6 @@ class RenderSliverOpacity extends RenderProxySliver {
layer = null;
return;
}
if (_alpha == 255) {
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
context.paintChild(child!, offset);
return;
}
assert(needsCompositing);
layer = context.pushOpacity(
offset,
Expand Down
26 changes: 26 additions & 0 deletions packages/flutter/test/rendering/debug_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,30 @@ void main() {
expect(error, isNull);
debugPaintSizeEnabled = false;
});

test('debugDisableOpacity keeps things in the right spot', () {
debugDisableOpacityLayers = true;

final RenderDecoratedBox blackBox = RenderDecoratedBox(
decoration: const BoxDecoration(color: Color(0xff000000)),
child: RenderConstrainedBox(
additionalConstraints: BoxConstraints.tight(const Size.square(20.0)),
),
);
final RenderOpacity root = RenderOpacity(
opacity: .5,
child: blackBox,
);
layout(root, phase: EnginePhase.compositingBits);

final OffsetLayer rootLayer = OffsetLayer(offset: Offset.zero);
final PaintingContext context = PaintingContext(
rootLayer,
const Rect.fromLTWH(0, 0, 500, 500),
);
root.paint(context, const Offset(40, 40));
final OpacityLayer opacityLayer = rootLayer.firstChild! as OpacityLayer;
expect(opacityLayer.offset, const Offset(40, 40));
debugDisableOpacityLayers = false;
});
}
8 changes: 4 additions & 4 deletions packages/flutter/test/rendering/proxy_box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ void main() {
expect(renderOpacity.needsCompositing, false);
});

test('RenderOpacity does not composite if it is opaque', () {
test('RenderOpacity does composite if it is opaque', () {
final RenderOpacity renderOpacity = RenderOpacity(
opacity: 1.0,
child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
);

layout(renderOpacity, phase: EnginePhase.composite);
expect(renderOpacity.needsCompositing, false);
expect(renderOpacity.needsCompositing, true);
});

test('RenderOpacity reuses its layer', () {
Expand All @@ -306,7 +306,7 @@ void main() {
expect(renderAnimatedOpacity.needsCompositing, false);
});

test('RenderAnimatedOpacity does not composite if it is opaque', () {
test('RenderAnimatedOpacity does composite if it is opaque', () {
final Animation<double> opacityAnimation = AnimationController(
vsync: FakeTickerProvider(),
)..value = 1.0;
Expand All @@ -318,7 +318,7 @@ void main() {
);

layout(renderAnimatedOpacity, phase: EnginePhase.composite);
expect(renderAnimatedOpacity.needsCompositing, false);
expect(renderAnimatedOpacity.needsCompositing, true);
});

test('RenderAnimatedOpacity reuses its layer', () {
Expand Down
8 changes: 4 additions & 4 deletions packages/flutter/test/rendering/proxy_sliver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {
expect(renderSliverOpacity.needsCompositing, false);
});

test('RenderSliverOpacity does not composite if it is opaque', () {
test('RenderSliverOpacity does composite if it is opaque', () {
final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity(
opacity: 1.0,
sliver: RenderSliverToBoxAdapter(
Expand All @@ -46,7 +46,7 @@ void main() {
);

layout(root, phase: EnginePhase.composite);
expect(renderSliverOpacity.needsCompositing, false);
expect(renderSliverOpacity.needsCompositing, true);
});
test('RenderSliverOpacity reuses its layer', () {
final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity(
Expand Down Expand Up @@ -102,7 +102,7 @@ void main() {
expect(renderSliverAnimatedOpacity.needsCompositing, false);
});

test('RenderSliverAnimatedOpacity does not composite if it is opaque', () {
test('RenderSliverAnimatedOpacity does composite if it is opaque', () {
final Animation<double> opacityAnimation = AnimationController(
vsync: FakeTickerProvider(),
)..value = 1.0;
Expand All @@ -124,7 +124,7 @@ void main() {
);

layout(root, phase: EnginePhase.composite);
expect(renderSliverAnimatedOpacity.needsCompositing, false);
expect(renderSliverAnimatedOpacity.needsCompositing, true);
});

test('RenderSliverAnimatedOpacity reuses its layer', () {
Expand Down
31 changes: 31 additions & 0 deletions packages/flutter/test/widgets/opacity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,35 @@ void main() {
final OffsetLayer offsetLayer = element.renderObject!.debugLayer! as OffsetLayer;
await offsetLayer.toImage(const Rect.fromLTRB(0.0, 0.0, 1.0, 1.0));
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/49857

testWidgets('Child shows up in the right spot when opacity is disabled', (WidgetTester tester) async {
debugDisableOpacityLayers = true;
final GlobalKey key = GlobalKey();
await tester.pumpWidget(
RepaintBoundary(
key: key,
child: Directionality(
textDirection: TextDirection.ltr,
child: Stack(
children: <Widget>[
Positioned(
top: 40,
left: 140,
child: Opacity(
opacity: .5,
child: Container(height: 100, width: 100, color: Colors.red),
),
),
],
),
),
),
);

await expectLater(
find.byKey(key),
matchesGoldenFile('opacity_disabled_with_child.png'),
);
debugDisableOpacityLayers = false;
});
}

0 comments on commit 0d0f7a4

Please sign in to comment.