Skip to content

Commit 683426a

Browse files
committed
Always scale by devicePixelRatio in ResizeImage
1 parent 00a4e2f commit 683426a

3 files changed

Lines changed: 65 additions & 108 deletions

File tree

‎packages/flutter/lib/src/painting/image_provider.dart‎

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -802,15 +802,13 @@ class ResizeImageKey {
802802
this._width,
803803
this._height,
804804
this._allowUpscaling,
805-
this._devicePixelRatio,
806805
);
807806

808807
final Object _providerCacheKey;
809808
final ResizeImagePolicy _policy;
810809
final int? _width;
811810
final int? _height;
812811
final bool _allowUpscaling;
813-
final double _devicePixelRatio;
814812

815813
@override
816814
bool operator ==(Object other) {
@@ -822,13 +820,11 @@ class ResizeImageKey {
822820
other._policy == _policy &&
823821
other._width == _width &&
824822
other._height == _height &&
825-
other._allowUpscaling == _allowUpscaling &&
826-
other._devicePixelRatio == _devicePixelRatio;
823+
other._allowUpscaling == _allowUpscaling;
827824
}
828825

829826
@override
830-
int get hashCode =>
831-
Object.hash(_providerCacheKey, _policy, _width, _height, _allowUpscaling, _devicePixelRatio);
827+
int get hashCode => Object.hash(_providerCacheKey, _policy, _width, _height, _allowUpscaling);
832828
}
833829

834830
/// Configures the behavior for [ResizeImage].
@@ -1237,8 +1233,8 @@ enum ResizeImagePolicy {
12371233
/// These values also set the image's width & height in logical pixels
12381234
/// if it is unconstrained.
12391235
///
1240-
/// If [useLogicalPixels] is true, [width] and [height] are multiplied by
1241-
/// the device's pixel ratio at decode time.
1236+
/// [width] and [height] are specified in logical pixels and multiplied by
1237+
/// [ImageConfiguration.devicePixelRatio] at decode time.
12421238
///
12431239
/// {@tool snippet}
12441240
/// This example shows how to size the image to half of the screen's width.
@@ -1272,7 +1268,6 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
12721268
this.height,
12731269
this.policy = ResizeImagePolicy.exact,
12741270
this.allowUpscaling = false,
1275-
this.useLogicalPixels = false,
12761271
}) : assert(width != null || height != null);
12771272

12781273
/// The [ImageProvider] that this class wraps.
@@ -1281,11 +1276,17 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
12811276
/// The width the image should decode to and cache.
12821277
///
12831278
/// At least one of this and [height] must be non-null.
1279+
///
1280+
/// This value is in logical pixels and is multiplied by
1281+
/// [ImageConfiguration.devicePixelRatio] at decode time.
12841282
final int? width;
12851283

12861284
/// The height the image should decode to and cache.
12871285
///
12881286
/// At least one of this and [width] must be non-null.
1287+
///
1288+
/// This value is in logical pixels and is multiplied by
1289+
/// [ImageConfiguration.devicePixelRatio] at decode time.
12891290
final int? height;
12901291

12911292
/// The policy that determines how [width] and [height] are interpreted.
@@ -1302,31 +1303,18 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
13021303
/// to use an appropriate [Image.fit].
13031304
final bool allowUpscaling;
13041305

1305-
/// Whether to treat [width] and [height] as logical pixels.
1306-
///
1307-
/// When true, [width] and [height] are multiplied by
1308-
/// [ImageConfiguration.devicePixelRatio] at decode time.
1309-
final bool useLogicalPixels;
1310-
13111306
/// Composes the `provider` in a [ResizeImage] only when `cacheWidth` and
13121307
/// `cacheHeight` are not both null.
13131308
///
13141309
/// When `cacheWidth` and `cacheHeight` are both null, this will return the
1315-
/// `provider` directly. If `useLogicalPixels` is true, the dimensions are
1316-
/// multiplied by the device's pixel ratio at decode time.
1310+
/// `provider` directly.
13171311
static ImageProvider<Object> resizeIfNeeded(
13181312
int? cacheWidth,
13191313
int? cacheHeight,
1320-
ImageProvider<Object> provider, {
1321-
bool useLogicalPixels = false,
1322-
}) {
1314+
ImageProvider<Object> provider,
1315+
) {
13231316
if (cacheWidth != null || cacheHeight != null) {
1324-
return ResizeImage(
1325-
provider,
1326-
width: cacheWidth,
1327-
height: cacheHeight,
1328-
useLogicalPixels: useLogicalPixels,
1329-
);
1317+
return ResizeImage(provider, width: cacheWidth, height: cacheHeight);
13301318
}
13311319
return provider;
13321320
}
@@ -1337,9 +1325,8 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
13371325
'This feature was deprecated after v3.7.0-1.4.pre.',
13381326
)
13391327
ImageStreamCompleter loadBuffer(ResizeImageKey key, DecoderBufferCallback decode) {
1340-
final double devicePixelRatio = useLogicalPixels ? key._devicePixelRatio : 1.0;
1341-
final int? effectiveWidth = width != null ? (width! * devicePixelRatio).ceil() : null;
1342-
final int? effectiveHeight = height != null ? (height! * devicePixelRatio).ceil() : null;
1328+
final int? effectiveWidth = key._width;
1329+
final int? effectiveHeight = key._height;
13431330

13441331
Future<ui.Codec> decodeResize(
13451332
ui.ImmutableBuffer buffer, {
@@ -1373,9 +1360,8 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
13731360

13741361
@override
13751362
ImageStreamCompleter loadImage(ResizeImageKey key, ImageDecoderCallback decode) {
1376-
final double devicePixelRatio = useLogicalPixels ? key._devicePixelRatio : 1.0;
1377-
final int? effectiveWidth = width != null ? (width! * devicePixelRatio).ceil() : null;
1378-
final int? effectiveHeight = height != null ? (height! * devicePixelRatio).ceil() : null;
1363+
final int? effectiveWidth = key._width;
1364+
final int? effectiveHeight = key._height;
13791365

13801366
Future<ui.Codec> decodeResize(
13811367
ui.ImmutableBuffer buffer, {
@@ -1468,9 +1454,9 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
14681454

14691455
@override
14701456
Future<ResizeImageKey> obtainKey(ImageConfiguration configuration) {
1471-
final double devicePixelRatio = useLogicalPixels
1472-
? (configuration.devicePixelRatio ?? 1.0)
1473-
: 1.0;
1457+
final double devicePixelRatio = configuration.devicePixelRatio ?? 1.0;
1458+
final int? effectiveWidth = width != null ? (width! * devicePixelRatio).ceil() : null;
1459+
final int? effectiveHeight = height != null ? (height! * devicePixelRatio).ceil() : null;
14741460
Completer<ResizeImageKey>? completer;
14751461
// If the imageProvider.obtainKey future is synchronous, then we will be able to fill in result with
14761462
// a value before completer is initialized below.
@@ -1480,12 +1466,12 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
14801466
// This future has completed synchronously (completer was never assigned),
14811467
// so we can directly create the synchronous result to return.
14821468
result = SynchronousFuture<ResizeImageKey>(
1483-
ResizeImageKey._(key, policy, width, height, allowUpscaling, devicePixelRatio),
1469+
ResizeImageKey._(key, policy, effectiveWidth, effectiveHeight, allowUpscaling),
14841470
);
14851471
} else {
14861472
// This future did not synchronously complete.
14871473
completer.complete(
1488-
ResizeImageKey._(key, policy, width, height, allowUpscaling, devicePixelRatio),
1474+
ResizeImageKey._(key, policy, effectiveWidth, effectiveHeight, allowUpscaling),
14891475
);
14901476
}
14911477
});
@@ -1511,13 +1497,11 @@ class ResizeImage extends ImageProvider<ResizeImageKey> {
15111497
width == other.width &&
15121498
height == other.height &&
15131499
policy == other.policy &&
1514-
allowUpscaling == other.allowUpscaling &&
1515-
useLogicalPixels == other.useLogicalPixels;
1500+
allowUpscaling == other.allowUpscaling;
15161501
}
15171502

15181503
@override
1519-
int get hashCode =>
1520-
Object.hash(imageProvider, width, height, policy, allowUpscaling, useLogicalPixels);
1504+
int get hashCode => Object.hash(imageProvider, width, height, policy, allowUpscaling);
15211505
}
15221506

15231507
/// The strategy for [Image.network] and [NetworkImage] to decide whether to

‎packages/flutter/lib/src/widgets/image.dart‎

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,10 @@ class Image extends StatefulWidget {
404404
/// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
405405
///
406406
/// If `cacheWidth` or `cacheHeight` are provided, they indicate to the
407-
/// engine that the image should be decoded at the specified size, in
408-
/// physical pixels. The image will be rendered to the constraints of
409-
/// the layout or [width] and [height] regardless of these parameters.
410-
/// These parameters are primarily intended to reduce the memory usage of
411-
/// [ImageCache].
412-
///
413-
/// If `useLogicalCacheSize` is true, `cacheWidth` and `cacheHeight` are
414-
/// treated as logical pixels and multiplied by the device's pixel ratio
415-
/// at decode time.
407+
/// engine that the image should be decoded at the specified size. The image
408+
/// will be rendered to the constraints of the layout or [width] and [height]
409+
/// regardless of these parameters. These parameters are primarily intended
410+
/// to reduce the memory usage of [ImageCache].
416411
///
417412
/// In the case where the network image is on the Web platform, the [cacheWidth]
418413
/// and [cacheHeight] parameters are ignored as the web engine delegates
@@ -476,7 +471,6 @@ class Image extends StatefulWidget {
476471
Map<String, String>? headers,
477472
int? cacheWidth,
478473
int? cacheHeight,
479-
bool useLogicalCacheSize = false,
480474
WebHtmlElementStrategy webHtmlElementStrategy = WebHtmlElementStrategy.never,
481475
}) : image = ResizeImage.resizeIfNeeded(
482476
cacheWidth,
@@ -487,7 +481,6 @@ class Image extends StatefulWidget {
487481
headers: headers,
488482
webHtmlElementStrategy: webHtmlElementStrategy,
489483
),
490-
useLogicalPixels: useLogicalCacheSize,
491484
),
492485
assert(cacheWidth == null || cacheWidth > 0),
493486
assert(cacheHeight == null || cacheHeight > 0);
@@ -511,15 +504,10 @@ class Image extends StatefulWidget {
511504
/// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
512505
///
513506
/// If `cacheWidth` or `cacheHeight` are provided, they indicate to the
514-
/// engine that the image must be decoded at the specified size, in
515-
/// physical pixels. The image will be rendered to the constraints of
516-
/// the layout or [width] and [height] regardless of these parameters.
517-
/// These parameters are primarily intended to reduce the memory usage of
518-
/// [ImageCache].
519-
///
520-
/// If `useLogicalCacheSize` is true, `cacheWidth` and `cacheHeight` are
521-
/// treated as logical pixels and multiplied by the device's pixel ratio
522-
/// at decode time.
507+
/// engine that the image must be decoded at the specified size. The image
508+
/// will be rendered to the constraints of the layout or [width] and [height]
509+
/// regardless of these parameters. These parameters are primarily intended
510+
/// to reduce the memory usage of [ImageCache].
523511
///
524512
/// Loading an image from a file creates an in memory copy of the file,
525513
/// which is retained in the [ImageCache]. The underlying file is not
@@ -552,19 +540,13 @@ class Image extends StatefulWidget {
552540
this.filterQuality = FilterQuality.medium,
553541
int? cacheWidth,
554542
int? cacheHeight,
555-
bool useLogicalCacheSize = false,
556543
}) : // FileImage is not supported on Flutter Web therefore neither this method.
557544
assert(
558545
!kIsWeb,
559546
'Image.file is not supported on Flutter Web. '
560547
'Consider using either Image.asset or Image.network instead.',
561548
),
562-
image = ResizeImage.resizeIfNeeded(
563-
cacheWidth,
564-
cacheHeight,
565-
FileImage(file, scale: scale),
566-
useLogicalPixels: useLogicalCacheSize,
567-
),
549+
image = ResizeImage.resizeIfNeeded(cacheWidth, cacheHeight, FileImage(file, scale: scale)),
568550
loadingBuilder = null,
569551
assert(cacheWidth == null || cacheWidth > 0),
570552
assert(cacheHeight == null || cacheHeight > 0);
@@ -602,15 +584,10 @@ class Image extends StatefulWidget {
602584
/// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
603585
///
604586
/// If `cacheWidth` or `cacheHeight` are provided, they indicate to the
605-
/// engine that the image must be decoded at the specified size, in
606-
/// physical pixels. The image will be rendered to the constraints of
607-
/// the layout or [width] and [height] regardless of these parameters.
608-
/// These parameters are primarily intended to reduce the memory usage of
609-
/// [ImageCache].
610-
///
611-
/// If `useLogicalCacheSize` is true, `cacheWidth` and `cacheHeight` are
612-
/// treated as logical pixels and multiplied by the device's pixel ratio
613-
/// at decode time.
587+
/// engine that the image must be decoded at the specified size. The image
588+
/// will be rendered to the constraints of the layout or [width] and [height]
589+
/// regardless of these parameters. These parameters are primarily intended
590+
/// to reduce the memory usage of [ImageCache].
614591
///
615592
/// Either the [width] and [height] arguments should be specified, or the
616593
/// widget should be placed in a context that sets tight layout constraints.
@@ -726,14 +703,12 @@ class Image extends StatefulWidget {
726703
this.filterQuality = FilterQuality.medium,
727704
int? cacheWidth,
728705
int? cacheHeight,
729-
bool useLogicalCacheSize = false,
730706
}) : image = ResizeImage.resizeIfNeeded(
731707
cacheWidth,
732708
cacheHeight,
733709
scale != null
734710
? ExactAssetImage(name, bundle: bundle, scale: scale, package: package)
735711
: AssetImage(name, bundle: bundle, package: package),
736-
useLogicalPixels: useLogicalCacheSize,
737712
),
738713
loadingBuilder = null,
739714
assert(cacheWidth == null || cacheWidth > 0),
@@ -763,15 +738,10 @@ class Image extends StatefulWidget {
763738
/// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
764739
///
765740
/// If `cacheWidth` or `cacheHeight` are provided, they indicate to the
766-
/// engine that the image must be decoded at the specified size, in
767-
/// physical pixels. The image will be rendered to the constraints of
768-
/// the layout or [width] and [height] regardless of these parameters.
769-
/// These parameters are primarily intended to reduce the memory usage of
770-
/// [ImageCache].
771-
///
772-
/// If `useLogicalCacheSize` is true, `cacheWidth` and `cacheHeight` are
773-
/// treated as logical pixels and multiplied by the device's pixel ratio
774-
/// at decode time.
741+
/// engine that the image must be decoded at the specified size. The image
742+
/// will be rendered to the constraints of the layout or [width] and [height]
743+
/// regardless of these parameters. These parameters are primarily intended
744+
/// to reduce the memory usage of [ImageCache].
775745
Image.memory(
776746
Uint8List bytes, {
777747
super.key,
@@ -795,12 +765,10 @@ class Image extends StatefulWidget {
795765
this.filterQuality = FilterQuality.medium,
796766
int? cacheWidth,
797767
int? cacheHeight,
798-
bool useLogicalCacheSize = false,
799768
}) : image = ResizeImage.resizeIfNeeded(
800769
cacheWidth,
801770
cacheHeight,
802771
MemoryImage(bytes, scale: scale),
803-
useLogicalPixels: useLogicalCacheSize,
804772
),
805773
loadingBuilder = null,
806774
assert(cacheWidth == null || cacheWidth > 0),

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,49 +103,54 @@ void main() {
103103
});
104104

105105
// Regression test for https://github.com/flutter/flutter/issues/56239
106-
test('useLogicalPixels accounts for devicePixelRatio when decoding', () async {
106+
test('scales by devicePixelRatio when decoding', () async {
107107
// Source image is 50x50.
108108
final bytes = Uint8List.fromList(kBlueSquarePng);
109109
await _expectImageSize(MemoryImage(bytes), const Size(50, 50));
110110

111-
// Request 25x25 logical pixels on a 2x device with devicePixelRatio scaling.
111+
// Request 25x25 logical pixels on a 2x device.
112112
// Should decode at 50x50 physical pixels (25 * 2).
113113
final resizedImage = ResizeImage(
114114
MemoryImage(bytes),
115115
width: 25,
116116
height: 25,
117117
allowUpscaling: true,
118-
useLogicalPixels: true,
119118
);
120119
const config = ImageConfiguration(devicePixelRatio: 2.0);
121120
final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: config);
122121
expect(resizedImageSize, const Size(50, 50));
123122
});
124123

125-
// Regression test for https://github.com/flutter/flutter/issues/56239
126-
test('useLogicalPixels does not upscale beyond intrinsic size by default', () async {
127-
// Source image is 50x50, request 25x25 on a 3x device with devicePixelRatio scaling.
124+
test('does not upscale beyond intrinsic size when devicePixelRatio is large', () async {
125+
// Source image is 50x50, request 25x25 on a 3x device.
128126
// Effective = 75x75, but allowUpscaling=false (default), so clamped to 50x50.
129127
final bytes = Uint8List.fromList(kBlueSquarePng);
130-
final resizedImage = ResizeImage(
131-
MemoryImage(bytes),
132-
width: 25,
133-
height: 25,
134-
useLogicalPixels: true,
135-
);
128+
final resizedImage = ResizeImage(MemoryImage(bytes), width: 25, height: 25);
136129
const config = ImageConfiguration(devicePixelRatio: 3.0);
137130
final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: config);
138131
expect(resizedImageSize, const Size(50, 50));
139132
});
140133

141-
test('without useLogicalPixels ignores devicePixelRatio', () async {
142-
// Source image is 50x50, request 25x25 on a 2x device WITHOUT devicePixelRatio scaling.
143-
// Should decode at exactly 25x25 (not 50x50).
134+
test('obtainKey produces distinct keys for different devicePixelRatio', () async {
144135
final bytes = Uint8List.fromList(kBlueSquarePng);
145136
final resizedImage = ResizeImage(MemoryImage(bytes), width: 25, height: 25);
146-
const config = ImageConfiguration(devicePixelRatio: 2.0);
147-
final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: config);
148-
expect(resizedImageSize, const Size(25, 25));
137+
final ResizeImageKey key1x = await resizedImage.obtainKey(
138+
const ImageConfiguration(devicePixelRatio: 1.0),
139+
);
140+
final ResizeImageKey key2x = await resizedImage.obtainKey(
141+
const ImageConfiguration(devicePixelRatio: 2.0),
142+
);
143+
expect(key1x, isNot(equals(key2x)));
144+
});
145+
146+
test('obtainKey treats null devicePixelRatio as 1.0', () async {
147+
final bytes = Uint8List.fromList(kBlueSquarePng);
148+
final resizedImage = ResizeImage(MemoryImage(bytes), width: 25, height: 25);
149+
final ResizeImageKey keyEmpty = await resizedImage.obtainKey(ImageConfiguration.empty);
150+
final ResizeImageKey key1x = await resizedImage.obtainKey(
151+
const ImageConfiguration(devicePixelRatio: 1.0),
152+
);
153+
expect(keyEmpty, equals(key1x));
149154
});
150155

151156
test('refuses upscaling when allowUpscaling=false', () async {

0 commit comments

Comments
 (0)