Skip to content

Commit 78a1c7e

Browse files
authored
Revert "Drop last usages of Dart_New from engine (#16838)" (#16915)
We believe this may be implicated in a Windows-specific crash during the Flutter Gallery integration test. See: #51896 This reverts commit f9b78c5.
1 parent ba8a892 commit 78a1c7e

22 files changed

+221
-155
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ 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
301303
FILE: ../../../flutter/lib/ui/painting/gradient.cc
302304
FILE: ../../../flutter/lib/ui/painting/gradient.h
303305
FILE: ../../../flutter/lib/ui/painting/image.cc

lib/ui/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ 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",
3335
"painting/gradient.cc",
3436
"painting/gradient.h",
3537
"painting/image.cc",

lib/ui/compositing.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ 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) async {
25+
Future<Image> toImage(int width, int height) {
2626
if (width <= 0 || height <= 0) {
2727
throw Exception('Invalid image dimensions.');
2828
}
29-
final Image image = Image._();
30-
await _futurize((_Callback<bool> callback) => _toImage(image, width, height, callback));
31-
return image;
29+
return _futurize((_Callback<Image> callback) => _toImage(width, height, callback));
3230
}
3331

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

3634
/// Releases the resources used by this scene.
3735
///

lib/ui/compositing/scene.cc

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

64-
Dart_Handle Scene::toImage(Dart_Handle image_handle,
65-
uint32_t width,
64+
Dart_Handle Scene::toImage(uint32_t width,
6665
uint32_t height,
6766
Dart_Handle raw_image_callback) {
6867
TRACE_EVENT0("flutter", "Scene::toImage");
@@ -76,8 +75,7 @@ Dart_Handle Scene::toImage(Dart_Handle image_handle,
7675
return tonic::ToDart("Could not flatten scene into a layer tree.");
7776
}
7877

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

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

lib/ui/compositing/scene.h

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

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

35-
Dart_Handle toImage(Dart_Handle image_handle,
36-
uint32_t width,
35+
Dart_Handle toImage(uint32_t width,
3736
uint32_t height,
3837
Dart_Handle image_callback);
3938

lib/ui/dart_ui.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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"
1617
#include "flutter/lib/ui/painting/gradient.h"
1718
#include "flutter/lib/ui/painting/image.h"
1819
#include "flutter/lib/ui/painting/image_filter.h"
@@ -79,6 +80,7 @@ void DartUI::InitForGlobal() {
7980
DartRuntimeHooks::RegisterNatives(g_natives);
8081
EngineLayer::RegisterNatives(g_natives);
8182
FontCollection::RegisterNatives(g_natives);
83+
FrameInfo::RegisterNatives(g_natives);
8284
ImageFilter::RegisterNatives(g_natives);
8385
ImageShader::RegisterNatives(g_natives);
8486
IsolateNameServerNatives::RegisterNatives(g_natives);

lib/ui/painting.dart

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,19 +1639,22 @@ typedef ImageDecoderCallback = void Function(Image result);
16391639
///
16401640
/// To obtain an instance of the [FrameInfo] interface, see
16411641
/// [Codec.getNextFrame].
1642-
class FrameInfo {
1642+
@pragma('vm:entry-point')
1643+
class FrameInfo extends NativeFieldWrapperClass2 {
16431644
/// This class is created by the engine, and should not be instantiated
16441645
/// or extended directly.
16451646
///
16461647
/// To obtain an instance of the [FrameInfo] interface, see
16471648
/// [Codec.getNextFrame].
1648-
FrameInfo._(int durationMilliseconds, this.image) : duration = Duration(milliseconds: durationMilliseconds);
1649+
@pragma('vm:entry-point')
1650+
FrameInfo._();
16491651

16501652
/// The duration this frame should be shown.
1651-
final Duration duration;
1653+
Duration get duration => Duration(milliseconds: _durationMillis);
1654+
int get _durationMillis native 'FrameInfo_durationMillis';
16521655

16531656
/// The [Image] object for this frame.
1654-
final Image image;
1657+
Image get image native 'FrameInfo_image';
16551658
}
16561659

16571660
/// A handle to an image codec.
@@ -1681,32 +1684,21 @@ class Codec extends NativeFieldWrapperClass2 {
16811684
/// * -1 for infinity repetitions.
16821685
int get repetitionCount native 'Codec_repetitionCount';
16831686

1684-
FrameInfo _cachedFrame;
1685-
16861687
/// Fetches the next animation frame.
16871688
///
16881689
/// Wraps back to the first frame after returning the last frame.
16891690
///
16901691
/// The returned future can complete with an error if the decoding has failed.
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;
1692+
Future<FrameInfo> getNextFrame() {
1693+
return _futurize(_getNextFrame);
16981694
}
16991695

17001696
/// Returns an error message on failure, null on success.
1701-
String _getNextFrame(Image outImage, _Callback<int> callback) native 'Codec_getNextFrame';
1697+
String _getNextFrame(_Callback<FrameInfo> callback) native 'Codec_getNextFrame';
17021698

17031699
/// Release the resources used by this object. The object is no longer usable
17041700
/// after this method is called.
1705-
void dispose() {
1706-
_cachedFrame = null;
1707-
_dispose();
1708-
}
1709-
void _dispose() native 'Codec_dispose';
1701+
void dispose() native 'Codec_dispose';
17101702
}
17111703

17121704
/// Instantiates an image codec [Codec] object.
@@ -1726,12 +1718,10 @@ class Codec extends NativeFieldWrapperClass2 {
17261718
Future<Codec> instantiateImageCodec(Uint8List list, {
17271719
int targetWidth,
17281720
int targetHeight,
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;
1721+
}) {
1722+
return _futurize(
1723+
(_Callback<Codec> callback) => _instantiateImageCodec(list, callback, null, targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension)
1724+
);
17351725
}
17361726

17371727
/// Instantiates a [Codec] object for an image binary data.
@@ -1745,7 +1735,7 @@ Future<Codec> instantiateImageCodec(Uint8List list, {
17451735
/// If both are equal to [_kDoNotResizeDimension], then the image maintains its real size.
17461736
///
17471737
/// Returns an error message if the instantiation has failed, null otherwise.
1748-
String _instantiateImageCodec(Codec outCodec, Uint8List list, _Callback<bool> callback, _ImageInfo imageInfo, int targetWidth, int targetHeight)
1738+
String _instantiateImageCodec(Uint8List list, _Callback<Codec> callback, _ImageInfo imageInfo, int targetWidth, int targetHeight)
17491739
native 'instantiateImageCodec';
17501740

17511741
/// Loads a single image frame from a byte array into an [Image] object.
@@ -1786,12 +1776,11 @@ void decodeImageFromPixels(
17861776
{int rowBytes, int targetWidth, int targetHeight}
17871777
) {
17881778
final _ImageInfo imageInfo = _ImageInfo(width, height, format.index, rowBytes);
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-
});
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));
17951784
}
17961785

17971786
/// Determines the winding rule that decides how the interior of a [Path] is
@@ -4136,17 +4125,15 @@ class Picture extends NativeFieldWrapperClass2 {
41364125
///
41374126
/// Although the image is returned synchronously, the picture is actually
41384127
/// rasterized the first time the image is drawn and then cached.
4139-
Future<Image> toImage(int width, int height) async {
4128+
Future<Image> toImage(int width, int height) {
41404129
if (width <= 0 || height <= 0)
41414130
throw Exception('Invalid image dimensions.');
4142-
final Image image = Image._();
4143-
await _futurize(
4144-
(_Callback<bool> callback) => _toImage(image, width, height, callback)
4131+
return _futurize(
4132+
(_Callback<Image> callback) => _toImage(width, height, callback)
41454133
);
4146-
return image;
41474134
}
41484135

4149-
String _toImage(Image outImage, int width, int height, _Callback<bool> callback) native 'Picture_toImage';
4136+
String _toImage(int width, int height, _Callback<Image> callback) native 'Picture_toImage';
41504137

41514138
/// Release the resources used by this object. The object is no longer usable
41524139
/// after this method is called.

lib/ui/painting/codec.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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"
1314
#include "flutter/lib/ui/painting/multi_frame_codec.h"
1415
#include "flutter/lib/ui/painting/single_frame_codec.h"
1516
#include "third_party/skia/include/codec/SkCodec.h"
@@ -144,14 +145,13 @@ static std::variant<ImageDecoder::ImageInfo, std::string> ConvertImageInfo(
144145
}
145146

146147
static void InstantiateImageCodec(Dart_NativeArguments args) {
147-
Dart_Handle codec_handle = Dart_GetNativeArgument(args, 0);
148-
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 2);
148+
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1);
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, 3);
154+
Dart_Handle image_info_handle = Dart_GetNativeArgument(args, 2);
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, 1,
174+
tonic::DartConverter<tonic::Uint8List>::FromArguments(args, 0,
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, 4));
194+
tonic::DartConverter<int>::FromDart(Dart_GetNativeArgument(args, 3));
195195
const int targetHeight =
196-
tonic::DartConverter<int>::FromDart(Dart_GetNativeArgument(args, 5));
196+
tonic::DartConverter<int>::FromDart(Dart_GetNativeArgument(args, 4));
197197

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

211+
fml::RefPtr<Codec> ui_codec;
212+
211213
if (single_frame) {
212214
ImageDecoder::ImageDescriptor descriptor;
213215
descriptor.decompressed_image_info = image_info;
@@ -220,13 +222,12 @@ static void InstantiateImageCodec(Dart_NativeArguments args) {
220222
}
221223
descriptor.data = std::move(buffer);
222224

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

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

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

246247
void Codec::RegisterNatives(tonic::DartLibraryNatives* natives) {
247248
natives->Register({
248-
{"instantiateImageCodec", InstantiateImageCodec, 6, true},
249+
{"instantiateImageCodec", InstantiateImageCodec, 5, true},
249250
});
250251
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
251252
}

lib/ui/painting/codec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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"
910
#include "third_party/skia/include/codec/SkCodec.h"
1011
#include "third_party/skia/include/core/SkBitmap.h"
1112
#include "third_party/skia/include/core/SkImage.h"
@@ -29,8 +30,7 @@ class Codec : public RefCountedDartWrappable<Codec> {
2930

3031
virtual int repetitionCount() const = 0;
3132

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

3535
void dispose();
3636

lib/ui/painting/frame_info.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE file.
5+
6+
#include "flutter/lib/ui/painting/frame_info.h"
7+
8+
#include "third_party/tonic/dart_binding_macros.h"
9+
#include "third_party/tonic/dart_library_natives.h"
10+
11+
namespace flutter {
12+
13+
IMPLEMENT_WRAPPERTYPEINFO(ui, FrameInfo);
14+
15+
#define FOR_EACH_BINDING(V) \
16+
V(FrameInfo, durationMillis) \
17+
V(FrameInfo, image)
18+
19+
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
20+
21+
FrameInfo::FrameInfo(fml::RefPtr<CanvasImage> image, int durationMillis)
22+
: image_(std::move(image)), durationMillis_(durationMillis) {}
23+
24+
FrameInfo::~FrameInfo(){};
25+
26+
void FrameInfo::RegisterNatives(tonic::DartLibraryNatives* natives) {
27+
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
28+
}
29+
30+
} // namespace flutter

lib/ui/painting/frame_info.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_LIB_UI_PAINTING_FRAME_INFO_H_
6+
#define FLUTTER_LIB_UI_PAINTING_FRAME_INFO_H_
7+
8+
#include "flutter/lib/ui/dart_wrapper.h"
9+
#include "flutter/lib/ui/painting/image.h"
10+
11+
namespace flutter {
12+
13+
// A single animation frame.
14+
class FrameInfo final : public RefCountedDartWrappable<FrameInfo> {
15+
DEFINE_WRAPPERTYPEINFO();
16+
17+
public:
18+
int durationMillis() { return durationMillis_; }
19+
fml::RefPtr<CanvasImage> image() { return image_; }
20+
21+
static void RegisterNatives(tonic::DartLibraryNatives* natives);
22+
23+
private:
24+
FrameInfo(fml::RefPtr<CanvasImage> image, int durationMillis);
25+
26+
~FrameInfo() override;
27+
28+
const fml::RefPtr<CanvasImage> image_;
29+
const int durationMillis_;
30+
31+
FML_FRIEND_MAKE_REF_COUNTED(FrameInfo);
32+
FML_FRIEND_REF_COUNTED_THREAD_SAFE(FrameInfo);
33+
};
34+
35+
} // namespace flutter
36+
37+
#endif // FLUTTER_LIB_UI_PAINTING_FRAME_INFO_H_

lib/ui/painting/image.h

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

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

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

0 commit comments

Comments
 (0)