Skip to content

Commit 7772f6b

Browse files
Revert "Remove ImageProvider.load, DecoderCallback and `PaintingB… (#133482)
…inding.instantiateImageCodec` (#132679)" This reverts commit b4f4ece. Trial revert for flutter/flutter#133398 Co-authored-by: LongCatIsLooong <[email protected]>
1 parent ca33836 commit 7772f6b

18 files changed

+345
-75
lines changed

packages/flutter/lib/src/painting/_network_image_io.dart

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import 'debug.dart';
1313
import 'image_provider.dart' as image_provider;
1414
import 'image_stream.dart';
1515

16-
// Method signature for _loadAsync decode callbacks.
17-
typedef _SimpleDecoderCallback = Future<ui.Codec> Function(ui.ImmutableBuffer buffer);
18-
1916
/// The dart:io implementation of [image_provider.NetworkImage].
2017
@immutable
2118
class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkImage> implements image_provider.NetworkImage {
@@ -38,6 +35,25 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
3835
return SynchronousFuture<NetworkImage>(this);
3936
}
4037

38+
@override
39+
ImageStreamCompleter load(image_provider.NetworkImage key, image_provider.DecoderCallback decode) {
40+
// Ownership of this controller is handed off to [_loadAsync]; it is that
41+
// method's responsibility to close the controller's stream when the image
42+
// has been loaded or an error is thrown.
43+
final StreamController<ImageChunkEvent> chunkEvents = StreamController<ImageChunkEvent>();
44+
45+
return MultiFrameImageStreamCompleter(
46+
codec: _loadAsync(key as NetworkImage, chunkEvents, decodeDeprecated: decode),
47+
chunkEvents: chunkEvents.stream,
48+
scale: key.scale,
49+
debugLabel: key.url,
50+
informationCollector: () => <DiagnosticsNode>[
51+
DiagnosticsProperty<image_provider.ImageProvider>('Image provider', this),
52+
DiagnosticsProperty<image_provider.NetworkImage>('Image key', key),
53+
],
54+
);
55+
}
56+
4157
@override
4258
ImageStreamCompleter loadBuffer(image_provider.NetworkImage key, image_provider.DecoderBufferCallback decode) {
4359
// Ownership of this controller is handed off to [_loadAsync]; it is that
@@ -46,7 +62,7 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
4662
final StreamController<ImageChunkEvent> chunkEvents = StreamController<ImageChunkEvent>();
4763

4864
return MultiFrameImageStreamCompleter(
49-
codec: _loadAsync(key as NetworkImage, chunkEvents, decode: decode),
65+
codec: _loadAsync(key as NetworkImage, chunkEvents, decodeBufferDeprecated: decode),
5066
chunkEvents: chunkEvents.stream,
5167
scale: key.scale,
5268
debugLabel: key.url,
@@ -96,7 +112,9 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
96112
Future<ui.Codec> _loadAsync(
97113
NetworkImage key,
98114
StreamController<ImageChunkEvent> chunkEvents, {
99-
required _SimpleDecoderCallback decode,
115+
image_provider.ImageDecoderCallback? decode,
116+
image_provider.DecoderBufferCallback? decodeBufferDeprecated,
117+
image_provider.DecoderCallback? decodeDeprecated,
100118
}) async {
101119
try {
102120
assert(key == this);
@@ -130,7 +148,16 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
130148
throw Exception('NetworkImage is an empty file: $resolved');
131149
}
132150

133-
return decode(await ui.ImmutableBuffer.fromUint8List(bytes));
151+
if (decode != null) {
152+
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
153+
return decode(buffer);
154+
} else if (decodeBufferDeprecated != null) {
155+
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
156+
return decodeBufferDeprecated(buffer);
157+
} else {
158+
assert(decodeDeprecated != null);
159+
return decodeDeprecated!(bytes);
160+
}
134161
} catch (e) {
135162
// Depending on where the exception was thrown, the image cache may not
136163
// have had a chance to track the key in the cache at all.

packages/flutter/lib/src/painting/_network_image_web.dart

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import 'image_stream.dart';
1515
/// Creates a type for an overridable factory function for testing purposes.
1616
typedef HttpRequestFactory = web.XMLHttpRequest Function();
1717

18-
// Method signature for _loadAsync decode callbacks.
19-
typedef _SimpleDecoderCallback = Future<ui.Codec> Function(ui.ImmutableBuffer buffer);
20-
2118
/// Default HTTP client.
2219
web.XMLHttpRequest _httpClient() {
2320
return web.XMLHttpRequest();
@@ -57,6 +54,23 @@ class NetworkImage
5754
return SynchronousFuture<NetworkImage>(this);
5855
}
5956

57+
@override
58+
ImageStreamCompleter load(image_provider.NetworkImage key, image_provider.DecoderCallback decode) {
59+
// Ownership of this controller is handed off to [_loadAsync]; it is that
60+
// method's responsibility to close the controller's stream when the image
61+
// has been loaded or an error is thrown.
62+
final StreamController<ImageChunkEvent> chunkEvents =
63+
StreamController<ImageChunkEvent>();
64+
65+
return MultiFrameImageStreamCompleter(
66+
chunkEvents: chunkEvents.stream,
67+
codec: _loadAsync(key as NetworkImage, null, null, decode, chunkEvents),
68+
scale: key.scale,
69+
debugLabel: key.url,
70+
informationCollector: _imageStreamInformationCollector(key),
71+
);
72+
}
73+
6074
@override
6175
ImageStreamCompleter loadBuffer(image_provider.NetworkImage key, image_provider.DecoderBufferCallback decode) {
6276
// Ownership of this controller is handed off to [_loadAsync]; it is that
@@ -67,7 +81,7 @@ class NetworkImage
6781

6882
return MultiFrameImageStreamCompleter(
6983
chunkEvents: chunkEvents.stream,
70-
codec: _loadAsync(key as NetworkImage, decode, chunkEvents),
84+
codec: _loadAsync(key as NetworkImage, null, decode, null, chunkEvents),
7185
scale: key.scale,
7286
debugLabel: key.url,
7387
informationCollector: _imageStreamInformationCollector(key),
@@ -83,7 +97,7 @@ class NetworkImage
8397

8498
return MultiFrameImageStreamCompleter(
8599
chunkEvents: chunkEvents.stream,
86-
codec: _loadAsync(key as NetworkImage, decode, chunkEvents),
100+
codec: _loadAsync(key as NetworkImage, decode, null, null, chunkEvents),
87101
scale: key.scale,
88102
debugLabel: key.url,
89103
informationCollector: _imageStreamInformationCollector(key),
@@ -107,7 +121,9 @@ class NetworkImage
107121
// directly in place of the typical `instantiateImageCodec` method.
108122
Future<ui.Codec> _loadAsync(
109123
NetworkImage key,
110-
_SimpleDecoderCallback decode,
124+
image_provider.ImageDecoderCallback? decode,
125+
image_provider.DecoderBufferCallback? decodeBufferDeprecated,
126+
image_provider.DecoderCallback? decodeDeprecated,
111127
StreamController<ImageChunkEvent> chunkEvents,
112128
) async {
113129
assert(key == this);
@@ -162,7 +178,17 @@ class NetworkImage
162178
throw image_provider.NetworkImageLoadException(
163179
statusCode: request.status, uri: resolved);
164180
}
165-
return decode(await ui.ImmutableBuffer.fromUint8List(bytes));
181+
182+
if (decode != null) {
183+
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
184+
return decode(buffer);
185+
} else if (decodeBufferDeprecated != null) {
186+
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
187+
return decodeBufferDeprecated(buffer);
188+
} else {
189+
assert(decodeDeprecated != null);
190+
return decodeDeprecated!(bytes);
191+
}
166192
} else {
167193
// This API only exists in the web engine implementation and is not
168194
// contained in the analyzer summary for Flutter.

packages/flutter/lib/src/painting/binding.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,47 @@ mixin PaintingBinding on BindingBase, ServicesBinding {
7878
@protected
7979
ImageCache createImageCache() => ImageCache();
8080

81+
/// Calls through to [dart:ui.instantiateImageCodec] from [ImageCache].
82+
///
83+
/// This method is deprecated. use [instantiateImageCodecFromBuffer] with an
84+
/// [ImmutableBuffer] instance instead of this method.
85+
///
86+
/// The `cacheWidth` and `cacheHeight` parameters, when specified, indicate
87+
/// the size to decode the image to.
88+
///
89+
/// Both `cacheWidth` and `cacheHeight` must be positive values greater than
90+
/// or equal to 1, or null. It is valid to specify only one of `cacheWidth`
91+
/// and `cacheHeight` with the other remaining null, in which case the omitted
92+
/// dimension will be scaled to maintain the aspect ratio of the original
93+
/// dimensions. When both are null or omitted, the image will be decoded at
94+
/// its native resolution.
95+
///
96+
/// The `allowUpscaling` parameter determines whether the `cacheWidth` or
97+
/// `cacheHeight` parameters are clamped to the intrinsic width and height of
98+
/// the original image. By default, the dimensions are clamped to avoid
99+
/// unnecessary memory usage for images. Callers that wish to display an image
100+
/// above its native resolution should prefer scaling the canvas the image is
101+
/// drawn into.
102+
@Deprecated(
103+
'Use instantiateImageCodecWithSize with an ImmutableBuffer instance instead. '
104+
'This feature was deprecated after v2.13.0-1.0.pre.',
105+
)
106+
Future<ui.Codec> instantiateImageCodec(
107+
Uint8List bytes, {
108+
int? cacheWidth,
109+
int? cacheHeight,
110+
bool allowUpscaling = false,
111+
}) {
112+
assert(cacheWidth == null || cacheWidth > 0);
113+
assert(cacheHeight == null || cacheHeight > 0);
114+
return ui.instantiateImageCodec(
115+
bytes,
116+
targetWidth: cacheWidth,
117+
targetHeight: cacheHeight,
118+
allowUpscaling: allowUpscaling,
119+
);
120+
}
121+
81122
/// Calls through to [dart:ui.instantiateImageCodecFromBuffer] from [ImageCache].
82123
///
83124
/// The [buffer] parameter should be an [ui.ImmutableBuffer] instance which can

packages/flutter/lib/src/painting/image_decoder.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:typed_data';
6-
import 'dart:ui' as ui show Codec, FrameInfo, Image, ImmutableBuffer;
6+
import 'dart:ui' as ui show Codec, FrameInfo, Image;
77

88
import 'binding.dart';
99

@@ -17,11 +17,10 @@ import 'binding.dart';
1717
/// [instantiateImageCodec] if support for animated images is necessary.
1818
///
1919
/// This function differs from [ui.decodeImageFromList] in that it defers to
20-
/// [PaintingBinding.instantiateImageCodecWithSize], and therefore can be mocked
21-
/// in tests.
20+
/// [PaintingBinding.instantiateImageCodec], and therefore can be mocked in
21+
/// tests.
2222
Future<ui.Image> decodeImageFromList(Uint8List bytes) async {
23-
final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
24-
final ui.Codec codec = await PaintingBinding.instance.instantiateImageCodecWithSize(buffer);
23+
final ui.Codec codec = await PaintingBinding.instance.instantiateImageCodec(bytes);
2524
final ui.FrameInfo frameInfo = await codec.getNextFrame();
2625
return frameInfo.image;
2726
}

0 commit comments

Comments
 (0)