Skip to content

Commit 8df7006

Browse files
[web] Honor getTargetSize when upscaling in instantiateImageCodecWithSize
Fixes flutter#195193
1 parent 0081d31 commit 8df7006

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/painting.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,12 @@ Future<Codec> instantiateImageCodecWithSize(
759759
final int width = info.image.width;
760760
final int height = info.image.height;
761761
final TargetImageSize targetSize = getTargetSize(width, height);
762+
// getTargetSize already applies the caller's upscaling policy (for
763+
// example ResizeImage.allowUpscaling), so decode at exactly this size.
762764
return await engine.engineInstantiateImageCodec(
763765
buffer._list!,
764766
targetWidth: targetSize.width,
765767
targetHeight: targetSize.height,
766-
allowUpscaling: false,
767768
);
768769
}
769770
} finally {

‎engine/src/flutter/lib/web_ui/test/ui/image_test.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,40 @@ Future<void> testMain() async {
146146

147147
expect(activeImages.length, 0);
148148
});
149+
150+
test('instantiateImageCodecWithSize scales to getTargetSize larger than intrinsic', () async {
151+
final ui.Image scaled = await _decodePngWithTargetSize(
152+
(int width, int height) => ui.TargetImageSize(width: width * 2, height: height * 2),
153+
);
154+
addTearDown(scaled.dispose);
155+
expect(scaled.width, 20);
156+
expect(scaled.height, 20);
157+
});
158+
159+
test('instantiateImageCodecWithSize scales to getTargetSize smaller than intrinsic', () async {
160+
final ui.Image scaled = await _decodePngWithTargetSize(
161+
(int width, int height) => ui.TargetImageSize(width: width ~/ 2, height: height ~/ 2),
162+
);
163+
addTearDown(scaled.dispose);
164+
expect(scaled.width, 5);
165+
expect(scaled.height, 5);
166+
});
167+
}
168+
169+
Future<ui.Image> _decodePngWithTargetSize(ui.TargetImageSizeCallback getTargetSize) async {
170+
final ui.Image image = await _createImage();
171+
final ByteData? imageData = await image.toByteData(format: ui.ImageByteFormat.png);
172+
image.dispose();
173+
final ui.ImmutableBuffer imageBuffer = await ui.ImmutableBuffer.fromUint8List(
174+
imageData!.buffer.asUint8List(),
175+
);
176+
final ui.Codec codec = await ui.instantiateImageCodecWithSize(
177+
imageBuffer,
178+
getTargetSize: getTargetSize,
179+
);
180+
final ui.FrameInfo frame = await codec.getNextFrame();
181+
codec.dispose();
182+
return frame.image;
149183
}
150184

151185
Future<ui.Image> _createImage() => _createPicture().toImage(10, 10);

‎packages/flutter/test/painting/image_provider_resize_image_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ void main() {
619619
await _expectImageSize(resizedImage, const Size(25, 25));
620620
});
621621
});
622-
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/73120);
622+
});
623623

624624
test('does not resize when no size is passed', () async {
625625
final bytes = Uint8List.fromList(kTransparentImage);

0 commit comments

Comments
 (0)