Remove single view assumption from pointer events (flutter/engine#41602)

This change removes the assumption that that all pointer events go to the view ID `0`. This change also adds a test for pointer events.

Part of: https://github.com/flutter/flutter/issues/112205

## Background

All pointer events are sent to [`PlatformDispatcher.onPointerDataPacket`](https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/onPointerDataPacket.html). In the future, a `viewId` property will be added [`PointerData`](https://api.flutter.dev/flutter/dart-ui/PointerData-class.html), which the framework (specifically, `GestureBinding`) will use to route the event to the proper view. This `viewId` property's value will be set by the embedders.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Loïc Sharma
2023-04-28 20:47:10 -07:00
committed by GitHub
parent 256b66b63f
commit b786d923f5
7 changed files with 98 additions and 21 deletions

View File

@@ -60,6 +60,9 @@ void PlatformConfiguration::DidCreateIsolate() {
dispatch_platform_message_.Set(
tonic::DartState::Current(),
Dart_GetField(library, tonic::ToDart("_dispatchPlatformMessage")));
dispatch_pointer_data_packet_.Set(
tonic::DartState::Current(),
Dart_GetField(library, tonic::ToDart("_dispatchPointerDataPacket")));
dispatch_semantics_action_.Set(
tonic::DartState::Current(),
Dart_GetField(library, tonic::ToDart("_dispatchSemanticsAction")));
@@ -179,6 +182,26 @@ void PlatformConfiguration::DispatchPlatformMessage(
tonic::ToDart(response_id)}));
}
void PlatformConfiguration::DispatchPointerDataPacket(
const PointerDataPacket& packet) {
std::shared_ptr<tonic::DartState> dart_state =
dispatch_pointer_data_packet_.dart_state().lock();
if (!dart_state) {
return;
}
tonic::DartState::Scope scope(dart_state);
const std::vector<uint8_t>& buffer = packet.data();
Dart_Handle data_handle =
tonic::DartByteData::Create(buffer.data(), buffer.size());
if (Dart_IsError(data_handle)) {
return;
}
tonic::CheckAndHandleError(
tonic::DartInvoke(dispatch_pointer_data_packet_.Get(), {data_handle}));
}
void PlatformConfiguration::DispatchSemanticsAction(int32_t node_id,
SemanticsAction action,
fml::MallocMapping args) {

View File

@@ -329,6 +329,15 @@ class PlatformConfiguration final {
///
void DispatchPlatformMessage(std::unique_ptr<PlatformMessage> message);
//----------------------------------------------------------------------------
/// @brief Notifies the PlatformConfiguration that the client has sent
/// it pointer events. This call originates in the platform view
/// and has been forwarded through the engine to here.
///
/// @param[in] packet The pointer event(s) serialized into a packet.
///
void DispatchPointerDataPacket(const PointerDataPacket& packet);
//----------------------------------------------------------------------------
/// @brief Notifies the framework that the embedder encountered an
/// accessibility related action on the specified node. This call
@@ -440,6 +449,7 @@ class PlatformConfiguration final {
tonic::DartPersistentValue update_semantics_enabled_;
tonic::DartPersistentValue update_accessibility_features_;
tonic::DartPersistentValue dispatch_platform_message_;
tonic::DartPersistentValue dispatch_pointer_data_packet_;
tonic::DartPersistentValue dispatch_semantics_action_;
tonic::DartPersistentValue begin_frame_;
tonic::DartPersistentValue draw_frame_;

View File

@@ -21,23 +21,6 @@ Window::Window(int64_t window_id, ViewportMetrics metrics)
Window::~Window() {}
void Window::DispatchPointerDataPacket(const PointerDataPacket& packet) {
std::shared_ptr<tonic::DartState> dart_state = library_.dart_state().lock();
if (!dart_state) {
return;
}
tonic::DartState::Scope scope(dart_state);
const std::vector<uint8_t>& buffer = packet.data();
Dart_Handle data_handle =
tonic::DartByteData::Create(buffer.data(), buffer.size());
if (Dart_IsError(data_handle)) {
return;
}
tonic::CheckAndHandleError(tonic::DartInvokeField(
library_.value(), "_dispatchPointerDataPacket", {data_handle}));
}
void Window::UpdateWindowMetrics(const ViewportMetrics& metrics) {
viewport_metrics_ = metrics;

View File

@@ -28,9 +28,6 @@ class Window final {
const ViewportMetrics& viewport_metrics() const { return viewport_metrics_; }
// Dispatch a packet to the framework that indicates one or a few pointer
// events.
void DispatchPointerDataPacket(const PointerDataPacket& packet);
void UpdateWindowMetrics(const ViewportMetrics& metrics);
private:

View File

@@ -272,7 +272,7 @@ bool RuntimeController::DispatchPointerDataPacket(
const PointerDataPacket& packet) {
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {
TRACE_EVENT0("flutter", "RuntimeController::DispatchPointerDataPacket");
platform_configuration->get_window(0)->DispatchPointerDataPacket(packet);
platform_configuration->DispatchPointerDataPacket(packet);
return true;
}

View File

@@ -1206,3 +1206,17 @@ void draw_solid_green() {
void draw_solid_blue() {
drawSolidColor(const Color.fromARGB(255, 0, 0, 255));
}
@pragma('vm:entry-point')
void pointer_data_packet() {
PlatformDispatcher.instance.onPointerDataPacket =
(PointerDataPacket packet) {
signalNativeCount(packet.data.length);
for (final pointerData in packet.data) {
signalNativeMessage(pointerData.toString());
}
};
signalNativeTest();
}

View File

@@ -2510,6 +2510,56 @@ TEST_F(EmbedderTest, EmbedderThreadHostUseCustomThreadConfig) {
}
#endif
/// Send a pointer event to Dart and wait until the Dart code signals
/// it received the event.
TEST_F(EmbedderTest, CanSendPointer) {
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("pointer_data_packet");
fml::AutoResetWaitableEvent ready_latch, count_latch, message_latch;
context.AddNativeCallback(
"SignalNativeTest",
CREATE_NATIVE_ENTRY(
[&ready_latch](Dart_NativeArguments args) { ready_latch.Signal(); }));
context.AddNativeCallback(
"SignalNativeCount",
CREATE_NATIVE_ENTRY([&count_latch](Dart_NativeArguments args) {
int count = tonic::DartConverter<int>::FromDart(
Dart_GetNativeArgument(args, 0));
ASSERT_EQ(count, 1);
count_latch.Signal();
}));
context.AddNativeCallback(
"SignalNativeMessage",
CREATE_NATIVE_ENTRY([&message_latch](Dart_NativeArguments args) {
auto message = tonic::DartConverter<std::string>::FromDart(
Dart_GetNativeArgument(args, 0));
ASSERT_EQ("PointerData(x: 123.0, y: 456.0)", message);
message_latch.Signal();
}));
auto engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
ready_latch.Wait();
FlutterPointerEvent pointer_event = {};
pointer_event.struct_size = sizeof(FlutterPointerEvent);
pointer_event.phase = FlutterPointerPhase::kAdd;
pointer_event.x = 123;
pointer_event.y = 456;
pointer_event.timestamp = static_cast<size_t>(1234567890);
FlutterEngineResult result =
FlutterEngineSendPointerEvent(engine.get(), &pointer_event, 1);
ASSERT_EQ(result, kSuccess);
count_latch.Wait();
message_latch.Wait();
}
} // namespace testing
} // namespace flutter