Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 72d0c36

Browse files
authored
Revert "Avoid a copy in EncodeImage (#19504)" (#19739)
This reverts commit f9acd08.
1 parent da8defd commit 72d0c36

File tree

6 files changed

+10
-123
lines changed

6 files changed

+10
-123
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ FILE: ../../../flutter/lib/ui/painting/image_decoder.h
337337
FILE: ../../../flutter/lib/ui/painting/image_decoder_unittests.cc
338338
FILE: ../../../flutter/lib/ui/painting/image_encoding.cc
339339
FILE: ../../../flutter/lib/ui/painting/image_encoding.h
340-
FILE: ../../../flutter/lib/ui/painting/image_encoding_unittests.cc
341340
FILE: ../../../flutter/lib/ui/painting/image_filter.cc
342341
FILE: ../../../flutter/lib/ui/painting/image_filter.h
343342
FILE: ../../../flutter/lib/ui/painting/image_shader.cc

lib/ui/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ if (enable_unittests) {
178178
public_configs = [ "//flutter:export_dynamic_symbols" ]
179179

180180
sources = [
181-
"painting/image_encoding_unittests.cc",
182181
"painting/vertices_unittests.cc",
183182
"window/pointer_data_packet_converter_unittests.cc",
184183
]

lib/ui/fixtures/ui_test.dart

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,3 @@ void frameCallback(FrameInfo info) {
4444
@pragma('vm:entry-point')
4545
void messageCallback(dynamic data) {
4646
}
47-
48-
49-
// Draw a circle on a Canvas that has a PictureRecorder. Take the image from
50-
// the PictureRecorder, and encode it as png. Check that the png data is
51-
// backed by an external Uint8List.
52-
@pragma('vm:entry-point')
53-
Future<void> encodeImageProducesExternalUint8List() async {
54-
final PictureRecorder pictureRecorder = PictureRecorder();
55-
final Canvas canvas = Canvas(pictureRecorder);
56-
final Paint paint = Paint()
57-
..color = Color.fromRGBO(255, 255, 255, 1.0)
58-
..style = PaintingStyle.fill;
59-
final Offset c = Offset(50.0, 50.0);
60-
canvas.drawCircle(c, 25.0, paint);
61-
final Picture picture = pictureRecorder.endRecording();
62-
final Image image = await picture.toImage(100, 100);
63-
_encodeImage(image, ImageByteFormat.png.index, _validateExternal);
64-
}
65-
void _encodeImage(Image i, int format, void Function(Uint8List result))
66-
native 'EncodeImage';
67-
void _validateExternal(Uint8List result) native 'ValidateExternal';

lib/ui/painting.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,20 +1600,17 @@ class Image extends NativeFieldWrapperClass2 {
16001600
/// The number of image pixels along the image's vertical axis.
16011601
int get height native 'Image_height';
16021602

1603-
/// Converts the [Image] object into a read-only byte array.
1603+
/// Converts the [Image] object into a byte array.
16041604
///
16051605
/// The [format] argument specifies the format in which the bytes will be
16061606
/// returned.
16071607
///
16081608
/// Returns a future that completes with the binary image data or an error
1609-
/// if encoding fails. Note that attempting to write to the returned
1610-
/// [ByteData] will result in an [UnsupportedError] exception.
1609+
/// if encoding fails.
16111610
Future<ByteData?> toByteData({ImageByteFormat format = ImageByteFormat.rawRgba}) {
16121611
return _futurize((_Callback<ByteData> callback) {
16131612
return _toByteData(format.index, (Uint8List? encoded) {
1614-
// [encoded] wraps a read-only SkData buffer, so we wrap it here in
1615-
// an [UnmodifiableByteDataView].
1616-
callback(UnmodifiableByteDataView(encoded!.buffer.asByteData()));
1613+
callback(encoded!.buffer.asByteData());
16171614
});
16181615
});
16191616
}

lib/ui/painting/image_encoding.cc

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ enum ImageByteFormat {
3535
kPNG,
3636
};
3737

38-
void FinalizeSkData(void* isolate_callback_data,
39-
Dart_WeakPersistentHandle handle,
40-
void* peer) {
41-
SkData* buffer = reinterpret_cast<SkData*>(peer);
42-
buffer->unref();
43-
}
44-
4538
void InvokeDataCallback(std::unique_ptr<DartPersistentValue> callback,
4639
sk_sp<SkData> buffer) {
4740
std::shared_ptr<tonic::DartState> dart_state = callback->dart_state().lock();
@@ -51,15 +44,11 @@ void InvokeDataCallback(std::unique_ptr<DartPersistentValue> callback,
5144
tonic::DartState::Scope scope(dart_state);
5245
if (!buffer) {
5346
DartInvoke(callback->value(), {Dart_Null()});
54-
return;
47+
} else {
48+
Dart_Handle dart_data = tonic::DartConverter<tonic::Uint8List>::ToDart(
49+
buffer->bytes(), buffer->size());
50+
DartInvoke(callback->value(), {dart_data});
5551
}
56-
// SkData are generally read-only.
57-
void* bytes = const_cast<void*>(buffer->data());
58-
const intptr_t length = buffer->size();
59-
void* peer = reinterpret_cast<void*>(buffer.release());
60-
Dart_Handle dart_data = Dart_NewExternalTypedDataWithFinalizer(
61-
Dart_TypedData_kUint8, bytes, length, peer, length, FinalizeSkData);
62-
DartInvoke(callback->value(), {dart_data});
6352
}
6453

6554
sk_sp<SkImage> ConvertToRasterUsingResourceContext(
@@ -233,10 +222,9 @@ void EncodeImageAndInvokeDataCallback(
233222
auto encode_task = [callback_task = std::move(callback_task), format,
234223
ui_task_runner](sk_sp<SkImage> raster_image) {
235224
sk_sp<SkData> encoded = EncodeImage(std::move(raster_image), format);
236-
ui_task_runner->PostTask([callback_task = std::move(callback_task),
237-
encoded = std::move(encoded)]() mutable {
238-
callback_task(std::move(encoded));
239-
});
225+
ui_task_runner->PostTask(
226+
[callback_task = std::move(callback_task),
227+
encoded = std::move(encoded)] { callback_task(encoded); });
240228
};
241229

242230
ConvertImageToRaster(std::move(image), encode_task, raster_task_runner,

lib/ui/painting/image_encoding_unittests.cc

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

0 commit comments

Comments
 (0)