Eliminates Dart->Host response copy for large buffers (flutter/engine#35468)

This commit is contained in:
gaaclarke
2022-08-24 15:56:58 -07:00
committed by GitHub
parent a645e493ec
commit f06d0b1487
8 changed files with 133 additions and 9 deletions

View File

@@ -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

View File

@@ -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",
]

View File

@@ -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) {}

View File

@@ -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.

View File

@@ -17,6 +17,11 @@ static std::atomic<uint64_t> platform_message_counter = 1;
namespace flutter {
namespace {
void MappingFinalizer(void* isolate_callback_data, void* peer) {
delete static_cast<fml::Mapping*>(peer);
}
template <typename Callback, typename TaskRunner, typename Result>
void PostCompletion(Callback&& callback,
const TaskRunner& ui_task_runner,
@@ -63,11 +68,35 @@ PlatformMessageResponseDart::~PlatformMessageResponseDart() {
}
void PlatformMessageResponseDart::Complete(std::unique_ptr<fml::Mapping> 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<std::string>().ToDart("dart:ui"));
FML_DCHECK(!(Dart_IsNull(ui_lib) || Dart_IsError(ui_lib)));
byte_buffer = Dart_Invoke(ui_lib,
tonic::DartConverter<std::string>().ToDart(
"_wrapUnmodifiableByteData"),
1, &mutable_byte_buffer);
FML_DCHECK(!(Dart_IsNull(byte_buffer) || Dart_IsError(byte_buffer)));
}
return byte_buffer;
});
}
void PlatformMessageResponseDart::CompleteEmpty() {

View File

@@ -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<fml::AutoResetWaitableEvent>();
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<tonic::DartState>();
auto response = fml::MakeRefCounted<PlatformMessageResponseDart>(
tonic::DartPersistentValue(tonic::DartState::Current(),
Dart_GetNativeArgument(args, 0)),
ui_task_runner, "foobar");
uint8_t* data = static_cast<uint8_t*>(malloc(100));
auto mapping = std::make_unique<fml::MallocMapping>(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<bool>::FromDart(Dart_GetNativeArgument(args, 0));
message_latch->Signal();
};
AddNativeCallback("FinishCallResponse",
CREATE_NATIVE_ENTRY(nativeFinishCallResponse));
Settings settings = CreateSettingsForFixture();
std::unique_ptr<Shell> 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

View File

@@ -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();

View File

@@ -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);