Skip to content

Commit e516485

Browse files
[web] Honor requested upscaling in instantiateImageCodecWithSize and ImageDescriptor.instantiateCodec (flutter#195836)
On the web, `instantiateImageCodecWithSize` and `ImageDescriptor.instantiateCodec` (encoded path) forwarded to the codec with a hardcoded `allowUpscaling: false`. Any requested target size larger than the intrinsic size was silently clamped back down, so `ResizeImage(allowUpscaling: true)` could never upscale on web. This PR removes the hardcoded flag so both entry points decode to the size the caller asked for, matching native `dart:ui`. The upscaling policy already lives in the framework: `ResizeImage`'s `getTargetSize` clamps to the intrinsic size when its own `allowUpscaling` is `false`, so that case behaves exactly as before. Changes: - `engine/src/flutter/lib/web_ui/lib/painting.dart`: drop `allowUpscaling: false` in `instantiateImageCodecWithSize` and in the encoded branch of `ImageDescriptor.instantiateCodec`. Every callee defaults to `allowUpscaling: true`, so this enables upscaling uniformly across the `ImageDecoder`, `createImageBitmap`, and Skia fallback paths. - `engine/src/flutter/lib/web_ui/test/ui/image_test.dart`: new tests that decode a 10x10 image to larger sizes through both entry points, including non-square, mixed up/down, and width-only (aspect-preserving) targets. The existing `Image.onCreate`/`onDispose` hook resets now use `addTearDown`, so a failing assertion can't leak hooks into later tests. - `packages/flutter/test/painting/image_provider_resize_image_test.dart`: remove `skip: isBrowser` from `group('resizing')`. Its 43 tests now run on web and pass on CanvasKit and Skwasm. Fixes flutter#195193 ## Where to focus - **Key decision: honor the caller's size instead of adding a web-only knob.** The public `dart:ui` signatures are unchanged. The web engine now trusts the target size it is given, as native does, rather than applying a second, stricter upscaling policy underneath the framework's. - **Non-obvious: the argument is removed, not set to `true`.** `true` is already the default at every hop (`engineInstantiateImageCodec` → browser/static/Skia codecs → `scaleImageIfNeeded`), and writing it explicitly trips `avoid_redundant_argument_values`. - **Non-obvious: the old clamp wasn't applied evenly.** The Skwasm Skia fallback already resized natively and ignored the flag. After this change, every decode path behaves the same. - **Primary review file:** `engine/src/flutter/lib/web_ui/lib/painting.dart` (two call sites). Then `engine/src/flutter/lib/web_ui/test/ui/image_test.dart`. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 3fe98af commit e516485

3 files changed

Lines changed: 95 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ Future<Codec> instantiateImageCodecWithSize(
763763
buffer._list!,
764764
targetWidth: targetSize.width,
765765
targetHeight: targetSize.height,
766-
allowUpscaling: false,
767766
);
768767
}
769768
} finally {
@@ -1081,12 +1080,7 @@ class ImageDescriptor {
10811080
throw StateError('Object is disposed');
10821081
}
10831082
if (_width == null) {
1084-
return instantiateImageCodec(
1085-
_data!,
1086-
targetWidth: targetWidth,
1087-
targetHeight: targetHeight,
1088-
allowUpscaling: false,
1089-
);
1083+
return instantiateImageCodec(_data!, targetWidth: targetWidth, targetHeight: targetHeight);
10901084
}
10911085

10921086
return createBmp(_data!, width, height, _rowBytes ?? width, _format!);

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

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Future<void> testMain() async {
2020
test('Image constructor invokes onCreate once', () async {
2121
var onCreateInvokedCount = 0;
2222
ui.Image? createdImage;
23+
addTearDown(() => ui.Image.onCreate = null);
2324
ui.Image.onCreate = (ui.Image image) {
2425
onCreateInvokedCount++;
2526
createdImage = image;
@@ -34,13 +35,12 @@ Future<void> testMain() async {
3435

3536
expect(onCreateInvokedCount, 2);
3637
expect(createdImage, image2);
37-
38-
ui.Image.onCreate = null;
3938
});
4039

4140
test('dispose() invokes onDispose once', () async {
4241
var onDisposeInvokedCount = 0;
4342
ui.Image? disposedImage;
43+
addTearDown(() => ui.Image.onDispose = null);
4444
ui.Image.onDispose = (ui.Image image) {
4545
onDisposeInvokedCount++;
4646
disposedImage = image;
@@ -57,8 +57,6 @@ Future<void> testMain() async {
5757

5858
expect(onDisposeInvokedCount, 2);
5959
expect(disposedImage, image2);
60-
61-
ui.Image.onDispose = null;
6260
});
6361

6462
test('scaledImageSize scales to a target width with no target height', () {
@@ -123,6 +121,10 @@ Future<void> testMain() async {
123121

124122
test('instantiateImageCodecWithSize disposes temporary image', () async {
125123
final activeImages = <ui.Image>{};
124+
addTearDown(() {
125+
ui.Image.onCreate = null;
126+
ui.Image.onDispose = null;
127+
});
126128
ui.Image.onCreate = activeImages.add;
127129
ui.Image.onDispose = activeImages.remove;
128130

@@ -146,6 +148,93 @@ Future<void> testMain() async {
146148

147149
expect(activeImages.length, 0);
148150
});
151+
152+
test('instantiateImageCodecWithSize honors upscaling when requested', () async {
153+
final ui.ImmutableBuffer imageBuffer = await _createImageBuffer();
154+
155+
final ui.Codec codec = await ui.instantiateImageCodecWithSize(
156+
imageBuffer,
157+
getTargetSize: (int w, int h) => ui.TargetImageSize(width: w * 2, height: h * 2),
158+
);
159+
final ui.FrameInfo frameInfo = await codec.getNextFrame();
160+
161+
expect(frameInfo.image.width, 20);
162+
expect(frameInfo.image.height, 20);
163+
164+
frameInfo.image.dispose();
165+
codec.dispose();
166+
});
167+
168+
test('instantiateImageCodecWithSize upscales to a non-square size', () async {
169+
final ui.ImmutableBuffer imageBuffer = await _createImageBuffer();
170+
171+
final ui.Codec codec = await ui.instantiateImageCodecWithSize(
172+
imageBuffer,
173+
getTargetSize: (int w, int h) => const ui.TargetImageSize(width: 25, height: 15),
174+
);
175+
final ui.FrameInfo frameInfo = await codec.getNextFrame();
176+
177+
expect(frameInfo.image.width, 25);
178+
expect(frameInfo.image.height, 15);
179+
180+
frameInfo.image.dispose();
181+
codec.dispose();
182+
});
183+
184+
test('instantiateImageCodecWithSize upscales one side and downscales the other', () async {
185+
final ui.ImmutableBuffer imageBuffer = await _createImageBuffer();
186+
187+
final ui.Codec codec = await ui.instantiateImageCodecWithSize(
188+
imageBuffer,
189+
getTargetSize: (int w, int h) => const ui.TargetImageSize(width: 25, height: 5),
190+
);
191+
final ui.FrameInfo frameInfo = await codec.getNextFrame();
192+
193+
expect(frameInfo.image.width, 25);
194+
expect(frameInfo.image.height, 5);
195+
196+
frameInfo.image.dispose();
197+
codec.dispose();
198+
});
199+
200+
test('ImageDescriptor.instantiateCodec honors upscaling when requested', () async {
201+
final ui.ImmutableBuffer imageBuffer = await _createImageBuffer();
202+
final ui.ImageDescriptor descriptor = await ui.ImageDescriptor.encoded(imageBuffer);
203+
204+
final ui.Codec codec = await descriptor.instantiateCodec(targetWidth: 20, targetHeight: 20);
205+
final ui.FrameInfo frameInfo = await codec.getNextFrame();
206+
207+
expect(frameInfo.image.width, 20);
208+
expect(frameInfo.image.height, 20);
209+
210+
frameInfo.image.dispose();
211+
codec.dispose();
212+
descriptor.dispose();
213+
imageBuffer.dispose();
214+
});
215+
216+
test('ImageDescriptor.instantiateCodec upscales from targetWidth alone', () async {
217+
final ui.ImmutableBuffer imageBuffer = await _createImageBuffer();
218+
final ui.ImageDescriptor descriptor = await ui.ImageDescriptor.encoded(imageBuffer);
219+
220+
final ui.Codec codec = await descriptor.instantiateCodec(targetWidth: 30);
221+
final ui.FrameInfo frameInfo = await codec.getNextFrame();
222+
223+
expect(frameInfo.image.width, 30);
224+
expect(frameInfo.image.height, 30);
225+
226+
frameInfo.image.dispose();
227+
codec.dispose();
228+
descriptor.dispose();
229+
imageBuffer.dispose();
230+
});
231+
}
232+
233+
Future<ui.ImmutableBuffer> _createImageBuffer() async {
234+
final ui.Image image = await _createImage();
235+
final ByteData? imageData = await image.toByteData(format: ui.ImageByteFormat.png);
236+
image.dispose();
237+
return ui.ImmutableBuffer.fromUint8List(imageData!.buffer.asUint8List());
149238
}
150239

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