@@ -1625,6 +1625,9 @@ enum PixelFormat {
16251625 bgra8888,
16261626}
16271627
1628+ /// Signature for [Image] lifecycle events.
1629+ typedef ImageEventCallback = void Function (Image image);
1630+
16281631/// Opaque handle to raw decoded image data (pixels).
16291632///
16301633/// To obtain an [Image] object, use the [ImageDescriptor] API.
@@ -1656,12 +1659,27 @@ class Image {
16561659 return true ;
16571660 }());
16581661 _image._handles.add (this );
1662+ onCreate? .call (this );
16591663 }
16601664
16611665 // C++ unit tests access this.
16621666 @pragma ('vm:entry-point' )
16631667 final _Image _image;
16641668
1669+ /// A callback that is invoked to report an image creation.
1670+ ///
1671+ /// It's preferred to use [MemoryAllocations] in flutter/foundation.dart
1672+ /// than to use [onCreate] directly because [MemoryAllocations]
1673+ /// allows multiple callbacks.
1674+ static ImageEventCallback ? onCreate;
1675+
1676+ /// A callback that is invoked to report the image disposal.
1677+ ///
1678+ /// It's preferred to use [MemoryAllocations] in flutter/foundation.dart
1679+ /// than to use [onDispose] directly because [MemoryAllocations]
1680+ /// allows multiple callbacks.
1681+ static ImageEventCallback ? onDispose;
1682+
16651683 StackTrace ? _debugStack;
16661684
16671685 /// The number of image pixels along the image's horizontal axis.
@@ -1682,6 +1700,7 @@ class Image {
16821700 /// useful when trying to determine what parts of the program are keeping an
16831701 /// image resident in memory.
16841702 void dispose () {
1703+ onDispose? .call (this );
16851704 assert (! _disposed && ! _image._disposed);
16861705 assert (_image._handles.contains (this ));
16871706 _disposed = true ;
@@ -5745,6 +5764,9 @@ class Canvas extends NativeFieldWrapperClass1 {
57455764 external void _drawShadow (Path path, int color, double elevation, bool transparentOccluder);
57465765}
57475766
5767+ /// Signature for [Picture] lifecycle events.
5768+ typedef PictureEventCallback = void Function (Picture picture);
5769+
57485770/// An object representing a sequence of recorded graphical operations.
57495771///
57505772/// To create a [Picture] , use a [PictureRecorder] .
@@ -5761,6 +5783,20 @@ class Picture extends NativeFieldWrapperClass1 {
57615783 @pragma ('vm:entry-point' )
57625784 Picture ._();
57635785
5786+ /// A callback that is invoked to report a picture creation.
5787+ ///
5788+ /// It's preferred to use [MemoryAllocations] in flutter/foundation.dart
5789+ /// than to use [onCreate] directly because [MemoryAllocations]
5790+ /// allows multiple callbacks.
5791+ static PictureEventCallback ? onCreate;
5792+
5793+ /// A callback that is invoked to report the picture disposal.
5794+ ///
5795+ /// It's preferred to use [MemoryAllocations] in flutter/foundation.dart
5796+ /// than to use [onDispose] directly because [MemoryAllocations]
5797+ /// allows multiple callbacks.
5798+ static PictureEventCallback ? onDispose;
5799+
57645800 /// Creates an image from this picture.
57655801 ///
57665802 /// The returned image will be `width` pixels wide and `height` pixels high.
@@ -5824,6 +5860,7 @@ class Picture extends NativeFieldWrapperClass1 {
58245860 _disposed = true ;
58255861 return true ;
58265862 }());
5863+ onDispose? .call (this );
58275864 _dispose ();
58285865 }
58295866
@@ -5890,6 +5927,9 @@ class PictureRecorder extends NativeFieldWrapperClass1 {
58905927 _endRecording (picture);
58915928 _canvas! ._recorder = null ;
58925929 _canvas = null ;
5930+ // We invoke the handler here, not in the Picture constructor, because we want
5931+ // [picture.approximateBytesUsed] to be available for the handler.
5932+ Picture .onCreate? .call (picture);
58935933 return picture;
58945934 }
58955935
0 commit comments