Skip to content

Commit f9b78c5

Browse files
authored
Drop last usages of Dart_New from engine (flutter#16838)
Image Codec FrameInfo Scene/Picture toImage
1 parent 04fa001 commit f9b78c5

22 files changed

+155
-221
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,6 @@ FILE: ../../../flutter/lib/ui/painting/color_filter.cc
298298
FILE: ../../../flutter/lib/ui/painting/color_filter.h
299299
FILE: ../../../flutter/lib/ui/painting/engine_layer.cc
300300
FILE: ../../../flutter/lib/ui/painting/engine_layer.h
301-
FILE: ../../../flutter/lib/ui/painting/frame_info.cc
302-
FILE: ../../../flutter/lib/ui/painting/frame_info.h
303301
FILE: ../../../flutter/lib/ui/painting/gradient.cc
304302
FILE: ../../../flutter/lib/ui/painting/gradient.h
305303
FILE: ../../../flutter/lib/ui/painting/image.cc

lib/ui/BUILD.gn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ source_set("ui") {
3030
"painting/color_filter.h",
3131
"painting/engine_layer.cc",
3232
"painting/engine_layer.h",
33-
"painting/frame_info.cc",
34-
"painting/frame_info.h",
3533
"painting/gradient.cc",
3634
"painting/gradient.h",
3735
"painting/image.cc",

lib/ui/compositing.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class Scene extends NativeFieldWrapperClass2 {
2222

2323
/// Creates a raster image representation of the current state of the scene.
2424
/// This is a slow operation that is performed on a background thread.
25-
Future<Image> toImage(int width, int height) {
25+
Future<Image> toImage(int width, int height) async {
2626
if (width <= 0 || height <= 0) {
2727
throw Exception('Invalid image dimensions.');
2828
}
29-
return _futurize((_Callback<Image> callback) => _toImage(width, height, callback));
29+
final Image image = Image._();
30+
await _futurize((_Callback<bool> callback) => _toImage(image, width, height, callback));
31+
return image;
3032
}
3133

32-
String _toImage(int width, int height, _Callback<Image> callback) native 'Scene_toImage';
34+
String _toImage(Image outImage, int width, int height, _Callback<bool> callback) native 'Scene_toImage';
3335

3436
/// Releases the resources used by this scene.
3537
///

lib/ui/compositing/scene.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void Scene::dispose() {
6161
ClearDartWrapper();
6262
}
6363

64-
Dart_Handle Scene::toImage(uint32_t width,
64+
Dart_Handle Scene::toImage(Dart_Handle image_handle,
65+
uint32_t width,
6566
uint32_t height,
6667
Dart_Handle raw_image_callback) {
6768
TRACE_EVENT0("flutter", "Scene::toImage");
@@ -75,7 +76,8 @@ Dart_Handle Scene::toImage(uint32_t width,
7576
return tonic::ToDart("Could not flatten scene into a layer tree.");
7677
}
7778

78-
return Picture::RasterizeToImage(picture, width, height, raw_image_callback);
79+
return Picture::RasterizeToImage(image_handle, picture, width, height,
80+
raw_image_callback);
7981
}
8082

8183
std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree() {

lib/ui/compositing/scene.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class Scene : public RefCountedDartWrappable<Scene> {
3232

3333
std::unique_ptr<flutter::LayerTree> takeLayerTree();
3434

35-
Dart_Handle toImage(uint32_t width,
35+
Dart_Handle toImage(Dart_Handle image_handle,
36+
uint32_t width,
3637
uint32_t height,
3738
Dart_Handle image_callback);
3839

lib/ui/dart_ui.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "flutter/lib/ui/painting/codec.h"
1414
#include "flutter/lib/ui/painting/color_filter.h"
1515
#include "flutter/lib/ui/painting/engine_layer.h"
16-
#include "flutter/lib/ui/painting/frame_info.h"
1716
#include "flutter/lib/ui/painting/gradient.h"
1817
#include "flutter/lib/ui/painting/image.h"
1918
#include "flutter/lib/ui/painting/image_filter.h"
@@ -80,7 +79,6 @@ void DartUI::InitForGlobal() {
8079
DartRuntimeHooks::RegisterNatives(g_natives);
8180
EngineLayer::RegisterNatives(g_natives);
8281
FontCollection::RegisterNatives(g_natives);
83-
FrameInfo::RegisterNatives(g_natives);
8482
ImageFilter::RegisterNatives(g_natives);
8583
ImageShader::RegisterNatives(g_natives);
8684
IsolateNameServerNatives::RegisterNatives(g_natives);

lib/ui/painting.dart

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
17181726
Future<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.

lib/ui/painting/codec.cc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "flutter/fml/logging.h"
1111
#include "flutter/fml/make_copyable.h"
1212
#include "flutter/fml/trace_event.h"
13-
#include "flutter/lib/ui/painting/frame_info.h"
1413
#include "flutter/lib/ui/painting/multi_frame_codec.h"
1514
#include "flutter/lib/ui/painting/single_frame_codec.h"
1615
#include "third_party/skia/include/codec/SkCodec.h"
@@ -145,13 +144,14 @@ static std::variant<ImageDecoder::ImageInfo, std::string> ConvertImageInfo(
145144
}
146145

147146
static void InstantiateImageCodec(Dart_NativeArguments args) {
148-
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1);
147+
Dart_Handle codec_handle = Dart_GetNativeArgument(args, 0);
148+
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 2);
149149
if (!Dart_IsClosure(callback_handle)) {
150150
Dart_SetReturnValue(args, tonic::ToDart("Callback must be a function"));
151151
return;
152152
}
153153

154-
Dart_Handle image_info_handle = Dart_GetNativeArgument(args, 2);
154+
Dart_Handle image_info_handle = Dart_GetNativeArgument(args, 3);
155155

156156
std::optional<ImageDecoder::ImageInfo> image_info;
157157

@@ -171,7 +171,7 @@ static void InstantiateImageCodec(Dart_NativeArguments args) {
171171
{
172172
Dart_Handle exception = nullptr;
173173
tonic::Uint8List list =
174-
tonic::DartConverter<tonic::Uint8List>::FromArguments(args, 0,
174+
tonic::DartConverter<tonic::Uint8List>::FromArguments(args, 1,
175175
exception);
176176
if (exception) {
177177
Dart_SetReturnValue(args, exception);
@@ -191,9 +191,9 @@ static void InstantiateImageCodec(Dart_NativeArguments args) {
191191
}
192192

193193
const int targetWidth =
194-
tonic::DartConverter<int>::FromDart(Dart_GetNativeArgument(args, 3));
195-
const int targetHeight =
196194
tonic::DartConverter<int>::FromDart(Dart_GetNativeArgument(args, 4));
195+
const int targetHeight =
196+
tonic::DartConverter<int>::FromDart(Dart_GetNativeArgument(args, 5));
197197

198198
std::unique_ptr<SkCodec> codec;
199199
bool single_frame;
@@ -208,8 +208,6 @@ static void InstantiateImageCodec(Dart_NativeArguments args) {
208208
single_frame = codec->getFrameCount() == 1;
209209
}
210210

211-
fml::RefPtr<Codec> ui_codec;
212-
213211
if (single_frame) {
214212
ImageDecoder::ImageDescriptor descriptor;
215213
descriptor.decompressed_image_info = image_info;
@@ -222,12 +220,13 @@ static void InstantiateImageCodec(Dart_NativeArguments args) {
222220
}
223221
descriptor.data = std::move(buffer);
224222

225-
ui_codec = fml::MakeRefCounted<SingleFrameCodec>(std::move(descriptor));
223+
SingleFrameCodec::Create(codec_handle, std::move(descriptor));
226224
} else {
227-
ui_codec = fml::MakeRefCounted<MultiFrameCodec>(std::move(codec));
225+
MultiFrameCodec::Create(codec_handle, std::move(codec));
228226
}
229227

230-
tonic::DartInvoke(callback_handle, {ToDart(ui_codec)});
228+
tonic::DartInvoke(callback_handle, {Dart_True()});
229+
Dart_SetReturnValue(args, Dart_Null());
231230
}
232231

233232
IMPLEMENT_WRAPPERTYPEINFO(ui, Codec);
@@ -246,7 +245,7 @@ void Codec::dispose() {
246245

247246
void Codec::RegisterNatives(tonic::DartLibraryNatives* natives) {
248247
natives->Register({
249-
{"instantiateImageCodec", InstantiateImageCodec, 5, true},
248+
{"instantiateImageCodec", InstantiateImageCodec, 6, true},
250249
});
251250
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
252251
}

lib/ui/painting/codec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define FLUTTER_LIB_UI_PAINTING_CODEC_H_
77

88
#include "flutter/lib/ui/dart_wrapper.h"
9-
#include "flutter/lib/ui/painting/frame_info.h"
109
#include "third_party/skia/include/codec/SkCodec.h"
1110
#include "third_party/skia/include/core/SkBitmap.h"
1211
#include "third_party/skia/include/core/SkImage.h"
@@ -30,7 +29,8 @@ class Codec : public RefCountedDartWrappable<Codec> {
3029

3130
virtual int repetitionCount() const = 0;
3231

33-
virtual Dart_Handle getNextFrame(Dart_Handle callback_handle) = 0;
32+
virtual Dart_Handle getNextFrame(Dart_Handle image_handle,
33+
Dart_Handle callback_handle) = 0;
3434

3535
void dispose();
3636

lib/ui/painting/frame_info.cc

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/ui/painting/frame_info.h

Lines changed: 0 additions & 37 deletions
This file was deleted.

lib/ui/painting/image.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class CanvasImage final : public RefCountedDartWrappable<CanvasImage> {
2222

2323
public:
2424
~CanvasImage() override;
25-
static fml::RefPtr<CanvasImage> Create() {
26-
return fml::MakeRefCounted<CanvasImage>();
25+
static fml::RefPtr<CanvasImage> Create(Dart_Handle dart_handle) {
26+
auto image = fml::MakeRefCounted<CanvasImage>();
27+
image->AssociateWithDartWrapper(dart_handle);
28+
return image;
2729
}
2830

2931
int width() { return image_.get()->width(); }

0 commit comments

Comments
 (0)