From 33f5ac665023ec0072be2d6bdefd63b338ed7aef Mon Sep 17 00:00:00 2001 From: ColdPaleLight <31977171+ColdPaleLight@users.noreply.github.com> Date: Thu, 19 Aug 2021 00:21:12 +0800 Subject: [PATCH] ImageInfo adds a new getter named sizeBytes to decouple ImageCache and ui.Image (#86555) --- .../flutter/lib/src/painting/image_cache.dart | 2 +- .../lib/src/painting/image_stream.dart | 3 +++ .../test/painting/image_info_test.dart | 26 +++++++++++++++++++ .../test/painting/mocks_for_image_cache.dart | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 packages/flutter/test/painting/image_info_test.dart diff --git a/packages/flutter/lib/src/painting/image_cache.dart b/packages/flutter/lib/src/painting/image_cache.dart index f1e1906915b..9cf51e21680 100644 --- a/packages/flutter/lib/src/painting/image_cache.dart +++ b/packages/flutter/lib/src/painting/image_cache.dart @@ -411,7 +411,7 @@ class ImageCache { void listener(ImageInfo? info, bool syncCall) { int? sizeBytes; if (info != null) { - sizeBytes = info.image.height * info.image.width * 4; + sizeBytes = info.sizeBytes; info.dispose(); } final _CachedImage image = _CachedImage( diff --git a/packages/flutter/lib/src/painting/image_stream.dart b/packages/flutter/lib/src/painting/image_stream.dart index d91a50f34ff..06e0face2b9 100644 --- a/packages/flutter/lib/src/painting/image_stream.dart +++ b/packages/flutter/lib/src/painting/image_stream.dart @@ -98,6 +98,9 @@ class ImageInfo { /// the image. final ui.Image image; + /// The size of raw image pixels in bytes. + int get sizeBytes => image.height * image.width * 4; + /// The linear scale factor for drawing this image at its intended size. /// /// The scale factor applies to the width and the height. diff --git a/packages/flutter/test/painting/image_info_test.dart b/packages/flutter/test/painting/image_info_test.dart new file mode 100644 index 00000000000..668e460a8a3 --- /dev/null +++ b/packages/flutter/test/painting/image_info_test.dart @@ -0,0 +1,26 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:ui' as ui show Image; + +import 'package:flutter/painting.dart'; +import 'package:flutter_test/flutter_test.dart'; + +Future main() async { + + final ui.Image smallImage = await createTestImage(width: 10, height: 20); + final ui.Image middleImage = await createTestImage(width: 20, height: 100); + final ui.Image bigImage = await createTestImage(width: 100, height: 200); + + test('ImageInfo sizeBytes', () { + ImageInfo imageInfo = ImageInfo(image: smallImage); + expect(imageInfo.sizeBytes, equals(800)); + + imageInfo = ImageInfo(image: middleImage); + expect(imageInfo.sizeBytes, equals(8000)); + + imageInfo = ImageInfo(image: bigImage); + expect(imageInfo.sizeBytes, equals(80000)); + }); +} diff --git a/packages/flutter/test/painting/mocks_for_image_cache.dart b/packages/flutter/test/painting/mocks_for_image_cache.dart index 475789e563f..facf0f5e910 100644 --- a/packages/flutter/test/painting/mocks_for_image_cache.dart +++ b/packages/flutter/test/painting/mocks_for_image_cache.dart @@ -43,6 +43,9 @@ class TestImageInfo implements ImageInfo { image.dispose(); } + @override + int get sizeBytes => image.height * image.width * 4; + @override int get hashCode => hashValues(value, image, scale, debugLabel);