diff --git a/packages/flutter/lib/src/painting/decoration_image.dart b/packages/flutter/lib/src/painting/decoration_image.dart index 912eaff5f4..636d3f4315 100644 --- a/packages/flutter/lib/src/painting/decoration_image.dart +++ b/packages/flutter/lib/src/painting/decoration_image.dart @@ -3,7 +3,8 @@ // found in the LICENSE file. import 'dart:developer' as developer; -import 'dart:ui' as ui show Image; +import 'dart:math' as math; +import 'dart:ui' as ui show FlutterView, Image; import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; @@ -558,13 +559,22 @@ void paintImage({ bool invertedCanvas = false; // Output size and destination rect are fully calculated. if (!kReleaseMode) { + // We can use the devicePixelRatio of the views directly here (instead of + // going through a MediaQuery) because if it changes, whatever is aware of + // the MediaQuery will be repainting the image anyways. + // Furthermore, for the memory check below we just assume that all images + // are decoded for the view with the highest device pixel ratio and use that + // as an upper bound for the display size of the image. + final double maxDevicePixelRatio = PaintingBinding.instance.platformDispatcher.views.fold( + 0.0, + (double previousValue, ui.FlutterView view) => math.max(previousValue, view.devicePixelRatio), + ); + final ImageSizeInfo sizeInfo = ImageSizeInfo( // Some ImageProvider implementations may not have given this. source: debugImageLabel ?? '', imageSize: Size(image.width.toDouble(), image.height.toDouble()), - // It's ok to use this instead of a MediaQuery because if this changes, - // whatever is aware of the MediaQuery will be repainting the image anyway. - displaySize: outputSize * PaintingBinding.instance.window.devicePixelRatio, + displaySize: outputSize * maxDevicePixelRatio, ); assert(() { if (debugInvertOversizedImages && @@ -576,7 +586,8 @@ void paintImage({ exception: 'Image $debugImageLabel has a display size of ' '$outputWidth×$outputHeight but a decode size of ' '${image.width}×${image.height}, which uses an additional ' - '${overheadInKilobytes}KB.\n\n' + '${overheadInKilobytes}KB (assuming a device pixel ratio of ' + '$maxDevicePixelRatio).\n\n' 'Consider resizing the asset ahead of time, supplying a cacheWidth ' 'parameter of $outputWidth, a cacheHeight parameter of ' '$outputHeight, or using a ResizeImage.', diff --git a/packages/flutter/test/painting/paint_image_test.dart b/packages/flutter/test/painting/paint_image_test.dart index ae6fd4a1e3..f4787d8eb6 100644 --- a/packages/flutter/test/painting/paint_image_test.dart +++ b/packages/flutter/test/painting/paint_image_test.dart @@ -102,7 +102,7 @@ void main() { expect( messages.single, - 'Image TestImage has a display size of 300×150 but a decode size of 300×300, which uses an additional 234KB.\n\n' + 'Image TestImage has a display size of 300×150 but a decode size of 300×300, which uses an additional 234KB (assuming a device pixel ratio of ${3.0}).\n\n' 'Consider resizing the asset ahead of time, supplying a cacheWidth parameter of 300, a cacheHeight parameter of 150, or using a ResizeImage.', );