Skip to content

Commit

Permalink
[framework] partial removal of forced compositing from opacity (#106989)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored Jul 2, 2022
1 parent efd006e commit d36ecec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
41 changes: 20 additions & 21 deletions packages/flutter/lib/src/rendering/proxy_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -883,15 +883,7 @@ class RenderOpacity extends RenderProxyBox {
super(child);

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

@override
OffsetLayer updateCompositedLayer({required covariant OpacityLayer? oldLayer}) {
assert(_alpha != 255);
final OpacityLayer updatedLayer = oldLayer ?? OpacityLayer();
updatedLayer.alpha = _alpha;
return updatedLayer;
}
bool get alwaysNeedsCompositing => child != null && (_alpha > 0 && _alpha < 255);

int _alpha;

Expand Down Expand Up @@ -949,19 +941,26 @@ class RenderOpacity extends RenderProxyBox {

@override
void paint(PaintingContext context, Offset offset) {
if (child != null) {
if (_alpha == 0) {
// 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;
}());
if (child == null) {
return;
}
if (_alpha == 0) {
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
return;
}
if (_alpha == 255) {
// No need to keep the layer. We'll create a new one if necessary.
layer = null;
return super.paint(context, offset);
}

assert(needsCompositing);
layer = context.pushOpacity(offset, _alpha, super.paint, oldLayer: layer as OpacityLayer?);
assert(() {
layer!.debugCreator = debugCreator;
return true;
}());
}

@override
Expand Down
12 changes: 11 additions & 1 deletion packages/flutter/test/rendering/proxy_box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,21 @@ void main() {
expect(renderOpacity.needsCompositing, false);
});

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

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

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

layout(renderOpacity, phase: EnginePhase.composite);
expect(renderOpacity.needsCompositing, true);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/widgets/debug_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void main() {
child: Placeholder(),
),
const Opacity(
opacity: 1.0,
opacity: 0.9,
child: Placeholder(),
),
ImageFiltered(
Expand Down

0 comments on commit d36ecec

Please sign in to comment.