diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 93b67b395f..aa22485f2a 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1100,6 +1100,7 @@ FILE: ../../../flutter/lib/ui/window/platform_message_response.cc FILE: ../../../flutter/lib/ui/window/platform_message_response.h FILE: ../../../flutter/lib/ui/window/platform_message_response_dart.cc FILE: ../../../flutter/lib/ui/window/platform_message_response_dart.h +FILE: ../../../flutter/lib/ui/window/platform_message_response_dart_unittests.cc FILE: ../../../flutter/lib/ui/window/pointer_data.cc FILE: ../../../flutter/lib/ui/window/pointer_data.h FILE: ../../../flutter/lib/ui/window/pointer_data_packet.cc diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index 6a2916d9cb..d9f1a893cf 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -231,6 +231,7 @@ if (enable_unittests) { "painting/vertices_unittests.cc", "semantics/semantics_update_builder_unittests.cc", "window/platform_configuration_unittests.cc", + "window/platform_message_response_dart_unittests.cc", "window/pointer_data_packet_converter_unittests.cc", ] diff --git a/engine/src/flutter/lib/ui/fixtures/ui_test.dart b/engine/src/flutter/lib/ui/fixtures/ui_test.dart index b0c7cdb697..5528172119 100644 --- a/engine/src/flutter/lib/ui/fixtures/ui_test.dart +++ b/engine/src/flutter/lib/ui/fixtures/ui_test.dart @@ -228,6 +228,21 @@ void frameCallback(_Image, int) { print('called back'); } +@pragma('vm:entry-point') +void platformMessageResponseTest() { + _callPlatformMessageResponseDart((ByteData? result) { + if (result is UnmodifiableByteDataView && + result.lengthInBytes == 100) { + _finishCallResponse(true); + } else { + _finishCallResponse(false); + } + }); +} + +void _callPlatformMessageResponseDart(void Function(ByteData? result) callback) native 'CallPlatformMessageResponseDart'; +void _finishCallResponse(bool didPass) native 'FinishCallResponse'; + @pragma('vm:entry-point') void messageCallback(dynamic data) {} diff --git a/engine/src/flutter/lib/ui/platform_dispatcher.dart b/engine/src/flutter/lib/ui/platform_dispatcher.dart index 1a86c5846b..9a3d7e9f6b 100644 --- a/engine/src/flutter/lib/ui/platform_dispatcher.dart +++ b/engine/src/flutter/lib/ui/platform_dispatcher.dart @@ -69,6 +69,10 @@ const double _kUnsetGestureSetting = -1.0; // See embedder.cc::kFlutterKeyDataChannel for more information. const String _kFlutterKeyDataChannel = 'flutter/keydata'; +@pragma('vm:entry-point') +ByteData? _wrapUnmodifiableByteData(ByteData? byteData) => + byteData == null ? null : UnmodifiableByteDataView(byteData); + /// Platform event dispatcher singleton. /// /// The most basic interface to the host operating system's interface. diff --git a/engine/src/flutter/lib/ui/window/platform_message_response_dart.cc b/engine/src/flutter/lib/ui/window/platform_message_response_dart.cc index 39d55f41d7..66c07c31c3 100644 --- a/engine/src/flutter/lib/ui/window/platform_message_response_dart.cc +++ b/engine/src/flutter/lib/ui/window/platform_message_response_dart.cc @@ -17,6 +17,11 @@ static std::atomic platform_message_counter = 1; namespace flutter { namespace { + +void MappingFinalizer(void* isolate_callback_data, void* peer) { + delete static_cast(peer); +} + template void PostCompletion(Callback&& callback, const TaskRunner& ui_task_runner, @@ -63,11 +68,35 @@ PlatformMessageResponseDart::~PlatformMessageResponseDart() { } void PlatformMessageResponseDart::Complete(std::unique_ptr data) { - PostCompletion(std::move(callback_), ui_task_runner_, &is_complete_, channel_, - [data = std::move(data)] { - return tonic::DartByteData::Create(data->GetMapping(), - data->GetSize()); - }); + PostCompletion( + std::move(callback_), ui_task_runner_, &is_complete_, channel_, + [data = std::move(data)]() mutable { + Dart_Handle byte_buffer; + intptr_t size = data->GetSize(); + if (data->GetSize() > tonic::DartByteData::kExternalSizeThreshold) { + const void* mapping = data->GetMapping(); + byte_buffer = Dart_NewUnmodifiableExternalTypedDataWithFinalizer( + /*type=*/Dart_TypedData_kByteData, + /*data=*/mapping, + /*length=*/size, + /*peer=*/data.release(), + /*external_allocation_size=*/size, + /*callback=*/MappingFinalizer); + } else { + Dart_Handle mutable_byte_buffer = + tonic::DartByteData::Create(data->GetMapping(), data->GetSize()); + Dart_Handle ui_lib = Dart_LookupLibrary( + tonic::DartConverter().ToDart("dart:ui")); + FML_DCHECK(!(Dart_IsNull(ui_lib) || Dart_IsError(ui_lib))); + byte_buffer = Dart_Invoke(ui_lib, + tonic::DartConverter().ToDart( + "_wrapUnmodifiableByteData"), + 1, &mutable_byte_buffer); + FML_DCHECK(!(Dart_IsNull(byte_buffer) || Dart_IsError(byte_buffer))); + } + + return byte_buffer; + }); } void PlatformMessageResponseDart::CompleteEmpty() { diff --git a/engine/src/flutter/lib/ui/window/platform_message_response_dart_unittests.cc b/engine/src/flutter/lib/ui/window/platform_message_response_dart_unittests.cc new file mode 100644 index 0000000000..b4a90db65a --- /dev/null +++ b/engine/src/flutter/lib/ui/window/platform_message_response_dart_unittests.cc @@ -0,0 +1,73 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/common/task_runners.h" +#include "flutter/fml/mapping.h" +#include "flutter/fml/synchronization/waitable_event.h" +#include "flutter/lib/ui/window/platform_message_response_dart.h" +#include "flutter/runtime/dart_vm.h" +#include "flutter/shell/common/shell_test.h" +#include "flutter/shell/common/thread_host.h" +#include "flutter/testing/testing.h" + +namespace flutter { +namespace testing { + +TEST_F(ShellTest, PlatformMessageResponseDart) { + bool did_pass = false; + auto message_latch = std::make_shared(); + TaskRunners task_runners("test", // label + GetCurrentTaskRunner(), // platform + CreateNewThread(), // raster + CreateNewThread(), // ui + CreateNewThread() // io + ); + + auto nativeCallPlatformMessageResponseDart = + [ui_task_runner = + task_runners.GetUITaskRunner()](Dart_NativeArguments args) { + auto dart_state = std::make_shared(); + auto response = fml::MakeRefCounted( + tonic::DartPersistentValue(tonic::DartState::Current(), + Dart_GetNativeArgument(args, 0)), + ui_task_runner, "foobar"); + uint8_t* data = static_cast(malloc(100)); + auto mapping = std::make_unique(data, 100); + response->Complete(std::move(mapping)); + }; + + AddNativeCallback("CallPlatformMessageResponseDart", + CREATE_NATIVE_ENTRY(nativeCallPlatformMessageResponseDart)); + + auto nativeFinishCallResponse = [message_latch, + &did_pass](Dart_NativeArguments args) { + did_pass = + tonic::DartConverter::FromDart(Dart_GetNativeArgument(args, 0)); + message_latch->Signal(); + }; + + AddNativeCallback("FinishCallResponse", + CREATE_NATIVE_ENTRY(nativeFinishCallResponse)); + + Settings settings = CreateSettingsForFixture(); + + std::unique_ptr shell = + CreateShell(std::move(settings), std::move(task_runners)); + + ASSERT_TRUE(shell->IsSetup()); + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("platformMessageResponseTest"); + + shell->RunEngine(std::move(configuration), [](auto result) { + ASSERT_EQ(result, Engine::RunStatus::Success); + }); + + message_latch->Wait(); + + ASSERT_TRUE(did_pass); + DestroyShell(std::move(shell), std::move(task_runners)); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.cc b/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.cc index cab038d14f..7416f12f62 100644 --- a/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.cc +++ b/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.cc @@ -12,16 +12,16 @@ namespace tonic { namespace { -// For large objects it is more efficient to use an external typed data object -// with a buffer allocated outside the Dart heap. -const int kExternalSizeThreshold = 1000; - void FreeFinalizer(void* isolate_callback_data, void* peer) { free(peer); } } // anonymous namespace +// For large objects it is more efficient to use an external typed data object +// with a buffer allocated outside the Dart heap. +const size_t DartByteData::kExternalSizeThreshold = 1000; + Dart_Handle DartByteData::Create(const void* data, size_t length) { if (length < kExternalSizeThreshold) { auto handle = DartByteData{data, length}.dart_handle(); diff --git a/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.h b/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.h index 9e14bc969f..911d0d5da0 100644 --- a/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.h +++ b/engine/src/flutter/third_party/tonic/typed_data/dart_byte_data.h @@ -14,6 +14,7 @@ namespace tonic { class DartByteData { public: + static const size_t kExternalSizeThreshold; static Dart_Handle Create(const void* data, size_t length); explicit DartByteData(Dart_Handle list);