Skip to content

Commit cf37f8d

Browse files
[web] Honor getTargetSize when upscaling in instantiateImageCodecWithSize
Fixes flutter#195193 instantiateImageCodecWithSize already applies the caller's upscaling policy via getTargetSize (for example ResizeImage.allowUpscaling). Passing a hardcoded allowUpscaling: false then clamped the decoded image back to its intrinsic size, so ResizeImage(allowUpscaling: true) could not upscale on web. Honor the returned target size, matching dart:ui, and enable the ResizeImage resizing tests on browser. Co-authored-by: Sankalp Thakur <[email protected]>
1 parent 0081d31 commit cf37f8d

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,14 @@ 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+
// Honor the size returned by getTargetSize, which already applied the
763+
// caller's upscaling policy (for example ResizeImage.allowUpscaling).
764+
// Do not pass allowUpscaling: false; that would clamp the already-computed
765+
// target back to the intrinsic size.
762766
return await engine.engineInstantiateImageCodec(
763767
buffer._list!,
764768
targetWidth: targetSize.width,
765769
targetHeight: targetSize.height,
766-
allowUpscaling: false,
767770
);
768771
}
769772
} finally {

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,42 @@ 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+
expect(scaled.width, 20);
155+
expect(scaled.height, 20);
156+
scaled.dispose();
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+
expect(scaled.width, 5);
164+
expect(scaled.height, 5);
165+
scaled.dispose();
166+
});
167+
}
168+
169+
Future<ui.Image> _decodePngWithTargetSize(ui.TargetImageSizeCallback getTargetSize) async {
170+
final ui.Image image = await _createImage();
171+
expect(image.width, 10);
172+
expect(image.height, 10);
173+
final ByteData? imageData = await image.toByteData(format: ui.ImageByteFormat.png);
174+
image.dispose();
175+
final ui.ImmutableBuffer imageBuffer = await ui.ImmutableBuffer.fromUint8List(
176+
imageData!.buffer.asUint8List(),
177+
);
178+
final ui.Codec codec = await ui.instantiateImageCodecWithSize(
179+
imageBuffer,
180+
getTargetSize: getTargetSize,
181+
);
182+
final ui.FrameInfo frame = await codec.getNextFrame();
183+
codec.dispose();
184+
return frame.image;
149185
}
150186

151187
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)