Skip to content

Commit d36ecec

Browse files
author
Jonah Williams
authored
[framework] partial removal of forced compositing from opacity (#106989)
1 parent efd006e commit d36ecec

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

packages/flutter/lib/src/rendering/proxy_box.dart

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -883,15 +883,7 @@ class RenderOpacity extends RenderProxyBox {
883883
super(child);
884884

885885
@override
886-
bool get alwaysNeedsCompositing => child != null && (_alpha > 0);
887-
888-
@override
889-
OffsetLayer updateCompositedLayer({required covariant OpacityLayer? oldLayer}) {
890-
assert(_alpha != 255);
891-
final OpacityLayer updatedLayer = oldLayer ?? OpacityLayer();
892-
updatedLayer.alpha = _alpha;
893-
return updatedLayer;
894-
}
886+
bool get alwaysNeedsCompositing => child != null && (_alpha > 0 && _alpha < 255);
895887

896888
int _alpha;
897889

@@ -949,19 +941,26 @@ class RenderOpacity extends RenderProxyBox {
949941

950942
@override
951943
void paint(PaintingContext context, Offset offset) {
952-
if (child != null) {
953-
if (_alpha == 0) {
954-
// No need to keep the layer. We'll create a new one if necessary.
955-
layer = null;
956-
return;
957-
}
958-
assert(needsCompositing);
959-
layer = context.pushOpacity(offset, _alpha, super.paint, oldLayer: layer as OpacityLayer?);
960-
assert(() {
961-
layer!.debugCreator = debugCreator;
962-
return true;
963-
}());
944+
if (child == null) {
945+
return;
946+
}
947+
if (_alpha == 0) {
948+
// No need to keep the layer. We'll create a new one if necessary.
949+
layer = null;
950+
return;
964951
}
952+
if (_alpha == 255) {
953+
// No need to keep the layer. We'll create a new one if necessary.
954+
layer = null;
955+
return super.paint(context, offset);
956+
}
957+
958+
assert(needsCompositing);
959+
layer = context.pushOpacity(offset, _alpha, super.paint, oldLayer: layer as OpacityLayer?);
960+
assert(() {
961+
layer!.debugCreator = debugCreator;
962+
return true;
963+
}());
965964
}
966965

967966
@override

packages/flutter/test/rendering/proxy_box_test.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,21 @@ void main() {
240240
expect(renderOpacity.needsCompositing, false);
241241
});
242242

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

248+
layout(renderOpacity, phase: EnginePhase.composite);
249+
expect(renderOpacity.needsCompositing, false);
250+
});
251+
252+
test('RenderOpacity does composite if it is partially opaque', () {
253+
final RenderOpacity renderOpacity = RenderOpacity(
254+
opacity: 0.1,
255+
child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
256+
);
257+
248258
layout(renderOpacity, phase: EnginePhase.composite);
249259
expect(renderOpacity.needsCompositing, true);
250260
});

packages/flutter/test/widgets/debug_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ void main() {
285285
child: Placeholder(),
286286
),
287287
const Opacity(
288-
opacity: 1.0,
288+
opacity: 0.9,
289289
child: Placeholder(),
290290
),
291291
ImageFiltered(

0 commit comments

Comments
 (0)