@@ -1639,22 +1639,19 @@ typedef ImageDecoderCallback = void Function(Image result);
16391639///
16401640/// To obtain an instance of the [FrameInfo] interface, see
16411641/// [Codec.getNextFrame] .
1642- @pragma ('vm:entry-point' )
1643- class FrameInfo extends NativeFieldWrapperClass2 {
1642+ class FrameInfo {
16441643 /// This class is created by the engine, and should not be instantiated
16451644 /// or extended directly.
16461645 ///
16471646 /// To obtain an instance of the [FrameInfo] interface, see
16481647 /// [Codec.getNextFrame] .
1649- @pragma ('vm:entry-point' )
1650- FrameInfo ._();
1648+ FrameInfo ._(int durationMilliseconds, this .image) : duration = Duration (milliseconds: durationMilliseconds);
16511649
16521650 /// The duration this frame should be shown.
1653- Duration get duration => Duration (milliseconds: _durationMillis);
1654- int get _durationMillis native 'FrameInfo_durationMillis' ;
1651+ final Duration duration;
16551652
16561653 /// The [Image] object for this frame.
1657- Image get image native 'FrameInfo_image' ;
1654+ final Image image;
16581655}
16591656
16601657/// A handle to an image codec.
@@ -1684,21 +1681,32 @@ class Codec extends NativeFieldWrapperClass2 {
16841681 /// * -1 for infinity repetitions.
16851682 int get repetitionCount native 'Codec_repetitionCount' ;
16861683
1684+ FrameInfo _cachedFrame;
1685+
16871686 /// Fetches the next animation frame.
16881687 ///
16891688 /// Wraps back to the first frame after returning the last frame.
16901689 ///
16911690 /// The returned future can complete with an error if the decoding has failed.
1692- Future <FrameInfo > getNextFrame () {
1693- return _futurize (_getNextFrame);
1691+ Future <FrameInfo > getNextFrame () async {
1692+ if (_cachedFrame == null || frameCount != 1 ) {
1693+ final Image image = Image ._();
1694+ final int durationMilliseconds = await _futurize ((_Callback <int > callback) => _getNextFrame (image, callback));
1695+ _cachedFrame = FrameInfo ._(durationMilliseconds, image);
1696+ }
1697+ return _cachedFrame;
16941698 }
16951699
16961700 /// Returns an error message on failure, null on success.
1697- String _getNextFrame (_Callback <FrameInfo > callback) native 'Codec_getNextFrame' ;
1701+ String _getNextFrame (Image outImage, _Callback <int > callback) native 'Codec_getNextFrame' ;
16981702
16991703 /// Release the resources used by this object. The object is no longer usable
17001704 /// after this method is called.
1701- void dispose () native 'Codec_dispose' ;
1705+ void dispose () {
1706+ _cachedFrame = null ;
1707+ _dispose ();
1708+ }
1709+ void _dispose () native 'Codec_dispose' ;
17021710}
17031711
17041712/// Instantiates an image codec [Codec] object.
@@ -1718,10 +1726,12 @@ class Codec extends NativeFieldWrapperClass2 {
17181726Future <Codec > instantiateImageCodec (Uint8List list, {
17191727 int targetWidth,
17201728 int targetHeight,
1721- }) {
1722- return _futurize (
1723- (_Callback <Codec > callback) => _instantiateImageCodec (list, callback, null , targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension)
1724- );
1729+ }) async {
1730+ final Codec codec = Codec ._();
1731+ await _futurize ((_Callback <bool > callback) {
1732+ return _instantiateImageCodec (codec, list, callback, null , targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension);
1733+ });
1734+ return codec;
17251735}
17261736
17271737/// Instantiates a [Codec] object for an image binary data.
@@ -1735,7 +1745,7 @@ Future<Codec> instantiateImageCodec(Uint8List list, {
17351745/// If both are equal to [_kDoNotResizeDimension] , then the image maintains its real size.
17361746///
17371747/// Returns an error message if the instantiation has failed, null otherwise.
1738- String _instantiateImageCodec (Uint8List list, _Callback <Codec > callback, _ImageInfo imageInfo, int targetWidth, int targetHeight)
1748+ String _instantiateImageCodec (Codec outCodec, Uint8List list, _Callback <bool > callback, _ImageInfo imageInfo, int targetWidth, int targetHeight)
17391749 native 'instantiateImageCodec' ;
17401750
17411751/// Loads a single image frame from a byte array into an [Image] object.
@@ -1776,11 +1786,12 @@ void decodeImageFromPixels(
17761786 {int rowBytes, int targetWidth, int targetHeight}
17771787) {
17781788 final _ImageInfo imageInfo = _ImageInfo (width, height, format.index, rowBytes);
1779- final Future <Codec > codecFuture = _futurize (
1780- (_Callback <Codec > callback) => _instantiateImageCodec (pixels, callback, imageInfo, targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension)
1781- );
1782- codecFuture.then ((Codec codec) => codec.getNextFrame ())
1783- .then ((FrameInfo frameInfo) => callback (frameInfo.image));
1789+ final Codec codec = Codec ._();
1790+ _futurize (
1791+ (_Callback <bool > callback) => _instantiateImageCodec (codec, pixels, callback, imageInfo, targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension)
1792+ ).then ((bool _) {
1793+ codec.getNextFrame ().then ((FrameInfo frameInfo) => callback (frameInfo.image));
1794+ });
17841795}
17851796
17861797/// Determines the winding rule that decides how the interior of a [Path] is
@@ -4125,15 +4136,17 @@ class Picture extends NativeFieldWrapperClass2 {
41254136 ///
41264137 /// Although the image is returned synchronously, the picture is actually
41274138 /// rasterized the first time the image is drawn and then cached.
4128- Future <Image > toImage (int width, int height) {
4139+ Future <Image > toImage (int width, int height) async {
41294140 if (width <= 0 || height <= 0 )
41304141 throw Exception ('Invalid image dimensions.' );
4131- return _futurize (
4132- (_Callback <Image > callback) => _toImage (width, height, callback)
4142+ final Image image = Image ._();
4143+ await _futurize (
4144+ (_Callback <bool > callback) => _toImage (image, width, height, callback)
41334145 );
4146+ return image;
41344147 }
41354148
4136- String _toImage (int width, int height, _Callback <Image > callback) native 'Picture_toImage' ;
4149+ String _toImage (Image outImage, int width, int height, _Callback <bool > callback) native 'Picture_toImage' ;
41374150
41384151 /// Release the resources used by this object. The object is no longer usable
41394152 /// after this method is called.
0 commit comments