From ba26f6ee8b12c5dc906a8a9fad772743ab705308 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Wed, 13 Mar 2024 17:00:17 -0700 Subject: [PATCH] Multi-view View Metrics (flutter/engine#46174) This PR adds `FlutterWindowMetricsEvent.viewId` to the embedder API. This PR only tests the ability to send metrics event for the implicit view. Once multiple views can be added via embedder API, we should test the ability to send different IDs. Part of https://github.com/flutter/flutter/issues/144806 Part of https://github.com/flutter/flutter/issues/142845 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../examples/glfw/FlutterEmbedderGLFW.cc | 3 + .../examples/glfw_drm/FlutterEmbedderGLFW.cc | 3 + .../flutter/examples/vulkan_glfw/src/main.cc | 7 ++ .../macos/framework/Source/FlutterEngine.mm | 7 +- .../shell/platform/embedder/embedder.cc | 4 +- .../shell/platform/embedder/embedder.h | 2 + .../platform/embedder/fixtures/main.dart | 36 +++++++ .../embedder/tests/embedder_unittests.cc | 99 ++++++++++++++++++- .../shell/platform/glfw/flutter_glfw.cc | 3 + .../flutter/shell/platform/linux/fl_engine.cc | 4 + .../platform/windows/flutter_windows_view.cc | 6 +- 11 files changed, 161 insertions(+), 13 deletions(-) diff --git a/engine/src/flutter/examples/glfw/FlutterEmbedderGLFW.cc b/engine/src/flutter/examples/glfw/FlutterEmbedderGLFW.cc index ae7fad5dec..a5e5e63643 100644 --- a/engine/src/flutter/examples/glfw/FlutterEmbedderGLFW.cc +++ b/engine/src/flutter/examples/glfw/FlutterEmbedderGLFW.cc @@ -81,6 +81,9 @@ void GLFWwindowSizeCallback(GLFWwindow* window, int width, int height) { event.width = width * g_pixelRatio; event.height = height * g_pixelRatio; event.pixel_ratio = g_pixelRatio; + // This example only supports a single window, therefore we assume the event + // occurred in the only view, the implicit view. + event.view_id = kImplicitViewId; FlutterEngineSendWindowMetricsEvent( reinterpret_cast(glfwGetWindowUserPointer(window)), &event); diff --git a/engine/src/flutter/examples/glfw_drm/FlutterEmbedderGLFW.cc b/engine/src/flutter/examples/glfw_drm/FlutterEmbedderGLFW.cc index f4a9ce8ead..79caba2dc2 100644 --- a/engine/src/flutter/examples/glfw_drm/FlutterEmbedderGLFW.cc +++ b/engine/src/flutter/examples/glfw_drm/FlutterEmbedderGLFW.cc @@ -104,6 +104,9 @@ void GLFWwindowSizeCallback(GLFWwindow* window, int width, int height) { event.width = width * g_pixelRatio; event.height = height * g_pixelRatio; event.pixel_ratio = g_pixelRatio; + // This example only supports a single window, therefore we assume the event + // occurred in the only view, the implicit view. + event.view_id = kImplicitViewId; FlutterEngineSendWindowMetricsEvent( reinterpret_cast(glfwGetWindowUserPointer(window)), &event); diff --git a/engine/src/flutter/examples/vulkan_glfw/src/main.cc b/engine/src/flutter/examples/vulkan_glfw/src/main.cc index c6fb9d591d..ebd55dec02 100644 --- a/engine/src/flutter/examples/vulkan_glfw/src/main.cc +++ b/engine/src/flutter/examples/vulkan_glfw/src/main.cc @@ -32,6 +32,7 @@ static const size_t kInitialWindowHeight = 600; // `VK_PRESENT_MODE_MAILBOX_KHR` for continual swap without horizontal tearing, // or `VK_PRESENT_MODE_IMMEDIATE_KHR` for no vsync. static const VkPresentModeKHR kPreferredPresentMode = VK_PRESENT_MODE_FIFO_KHR; +static constexpr FlutterViewId kImplicitViewId = 0; static_assert(FLUTTER_ENGINE_VERSION == 1, "This Flutter Embedder was authored against the stable Flutter " @@ -86,6 +87,9 @@ void GLFWcursorPositionCallbackAtPhase(GLFWwindow* window, std::chrono::duration_cast( std::chrono::high_resolution_clock::now().time_since_epoch()) .count(); + // This example only supports a single window, therefore we assume the event + // occurred in the only view, the implicit view. + event.view_id = kImplicitViewId; FlutterEngineSendPointerEvent(g_state.engine, &event, 1); } @@ -130,6 +134,9 @@ void GLFWframebufferSizeCallback(GLFWwindow* window, int width, int height) { event.width = width; event.height = height; event.pixel_ratio = g_pixelRatio; + // This example only supports a single window, therefore we assume the event + // occurred in the only view, the implicit view. + event.view_id = kImplicitViewId; FlutterEngineSendWindowMetricsEvent(g_state.engine, &event); } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index c8c1b98344..56419b18a6 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -962,12 +962,6 @@ static void SetThreadPriority(FlutterThreadPriority priority) { } - (void)updateWindowMetricsForViewController:(FlutterViewController*)viewController { - if (viewController.viewId != kFlutterImplicitViewId) { - // TODO(dkwingsmt): The embedder API only supports single-view for now. As - // embedder APIs are converted to multi-view, this method should support any - // views. - return; - } if (!_engine || !viewController || !viewController.viewLoaded) { return; } @@ -986,6 +980,7 @@ static void SetThreadPriority(FlutterThreadPriority priority) { .left = static_cast(scaledBounds.origin.x), .top = static_cast(scaledBounds.origin.y), .display_id = static_cast(displayId), + .view_id = viewController.viewId, }; _embedderAPI.SendWindowMetricsEvent(_engine, &windowMetricsEvent); } diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index f35dd805ec..8d4646eb5f 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -2141,8 +2141,8 @@ FlutterEngineResult FlutterEngineSendWindowMetricsEvent( if (engine == nullptr || flutter_metrics == nullptr) { return LOG_EMBEDDER_ERROR(kInvalidArguments, "Engine handle was invalid."); } - // TODO(dkwingsmt): Use a real view ID when multiview is supported. - int64_t view_id = kFlutterImplicitViewId; + FlutterViewId view_id = + SAFE_ACCESS(flutter_metrics, view_id, kFlutterImplicitViewId); flutter::ViewportMetrics metrics; diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index dce500fe66..f4afc9f594 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -859,6 +859,8 @@ typedef struct { double physical_view_inset_left; /// The identifier of the display the view is rendering on. FlutterEngineDisplayId display_id; + /// The view that this event is describing. + int64_t view_id; } FlutterWindowMetricsEvent; /// The phase of the pointer event. diff --git a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart index dffa430fbb..7270526b83 100644 --- a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart +++ b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart @@ -1334,6 +1334,42 @@ void pointer_data_packet_view_id() { signalNativeTest(); } +Map _getAllViewSizes() { + final Map result = {}; + for (final FlutterView view in PlatformDispatcher.instance.views) { + result[view.viewId] = view.physicalSize; + } + return result; +} + +List _findDifferences(Map a, Map b) { + final Set result = {}; + a.forEach((int viewId, Size sizeA) { + if (!b.containsKey(viewId) || b[viewId] != sizeA) { + result.add(viewId); + } + }); + b.forEach((int viewId, Size sizeB) { + if (!a.containsKey(viewId)) { + result.add(viewId); + } + }); + return result.toList()..sort(); +} + +@pragma('vm:entry-point') +void window_metrics_event_view_id() { + Map sizes = _getAllViewSizes(); + PlatformDispatcher.instance.onMetricsChanged = () { + final Map newSizes = _getAllViewSizes(); + final List differences = _findDifferences(sizes, newSizes); + sizes = newSizes; + signalNativeMessage('Changed: $differences'); + }; + + signalNativeTest(); +} + @pragma('vm:entry-point') Future channel_listener_response() async { channelBuffers.setListener('test/listen', diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index 44c7426fd7..33864fd33d 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -2727,7 +2727,7 @@ TEST_F(EmbedderTest, CanSendPointer) { /// Send a pointer event to Dart and wait until the Dart code echos with the /// view ID. -TEST_F(EmbedderTest, CanSendPointerWithViewId) { +TEST_F(EmbedderTest, CanSendPointerEventWithViewId) { auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); @@ -2767,6 +2767,103 @@ TEST_F(EmbedderTest, CanSendPointerWithViewId) { message_latch.Wait(); } +TEST_F(EmbedderTest, WindowMetricsEventDefaultsToImplicitView) { + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); + EmbedderConfigBuilder builder(context); + builder.SetSoftwareRendererConfig(); + builder.SetDartEntrypoint("window_metrics_event_view_id"); + + fml::AutoResetWaitableEvent ready_latch, message_latch; + context.AddNativeCallback( + "SignalNativeTest", + CREATE_NATIVE_ENTRY( + [&ready_latch](Dart_NativeArguments args) { ready_latch.Signal(); })); + context.AddNativeCallback( + "SignalNativeMessage", + CREATE_NATIVE_ENTRY([&message_latch](Dart_NativeArguments args) { + auto message = tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + ASSERT_EQ("Changed: [0]", message); + message_latch.Signal(); + })); + + auto engine = builder.LaunchEngine(); + ASSERT_TRUE(engine.is_valid()); + + ready_latch.Wait(); + + FlutterWindowMetricsEvent event = {}; + // Simulate an event that comes from an old version of embedder.h that doesn't + // have the view_id field. + event.struct_size = offsetof(FlutterWindowMetricsEvent, view_id); + event.width = 200; + event.height = 300; + event.pixel_ratio = 1.5; + // Skip assigning event.view_id here to test the default behavior. + + FlutterEngineResult result = + FlutterEngineSendWindowMetricsEvent(engine.get(), &event); + ASSERT_EQ(result, kSuccess); + + message_latch.Wait(); +} + +TEST_F(EmbedderTest, IgnoresWindowMetricsEventForUnknownView) { + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); + EmbedderConfigBuilder builder(context); + builder.SetSoftwareRendererConfig(); + builder.SetDartEntrypoint("window_metrics_event_view_id"); + + fml::AutoResetWaitableEvent ready_latch, message_latch; + context.AddNativeCallback( + "SignalNativeTest", + CREATE_NATIVE_ENTRY( + [&ready_latch](Dart_NativeArguments args) { ready_latch.Signal(); })); + + context.AddNativeCallback( + "SignalNativeMessage", + CREATE_NATIVE_ENTRY([&message_latch](Dart_NativeArguments args) { + auto message = tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + // Message latch should only be signaled once as the bad + // view metric should be dropped by the engine. + ASSERT_FALSE(message_latch.IsSignaledForTest()); + ASSERT_EQ("Changed: [0]", message); + message_latch.Signal(); + })); + + auto engine = builder.LaunchEngine(); + ASSERT_TRUE(engine.is_valid()); + + ready_latch.Wait(); + + // Send a window metric for a nonexistent view, which should be dropped by the + // engine. + FlutterWindowMetricsEvent bad_event = {}; + bad_event.struct_size = sizeof(FlutterWindowMetricsEvent); + bad_event.width = 200; + bad_event.height = 300; + bad_event.pixel_ratio = 1.5; + bad_event.view_id = 100; + + FlutterEngineResult result = + FlutterEngineSendWindowMetricsEvent(engine.get(), &bad_event); + ASSERT_EQ(result, kSuccess); + + // Send a window metric for a valid view. The engine notifies the Dart app. + FlutterWindowMetricsEvent event = {}; + event.struct_size = sizeof(FlutterWindowMetricsEvent); + event.width = 200; + event.height = 300; + event.pixel_ratio = 1.5; + event.view_id = 0; + + result = FlutterEngineSendWindowMetricsEvent(engine.get(), &event); + ASSERT_EQ(result, kSuccess); + + message_latch.Wait(); +} + TEST_F(EmbedderTest, RegisterChannelListener) { auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); diff --git a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc index 266a97bec5..b00bab8ced 100644 --- a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc +++ b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc @@ -297,6 +297,9 @@ static void SendWindowMetrics(FlutterDesktopWindowControllerState* controller, } else { event.pixel_ratio = controller->window_wrapper->pixel_ratio_override; } + // The GLFW embedder doesn't support multiple views. We assume all pointer + // events come from the only view, the implicit view. + event.view_id = flutter::kFlutterImplicitViewId; FlutterEngineSendWindowMetricsEvent(controller->engine->flutter_engine, &event); } diff --git a/engine/src/flutter/shell/platform/linux/fl_engine.cc b/engine/src/flutter/shell/platform/linux/fl_engine.cc index 3069cd2b39..f2c8040aa4 100644 --- a/engine/src/flutter/shell/platform/linux/fl_engine.cc +++ b/engine/src/flutter/shell/platform/linux/fl_engine.cc @@ -760,6 +760,10 @@ void fl_engine_send_window_metrics_event(FlEngine* self, event.width = width; event.height = height; event.pixel_ratio = pixel_ratio; + // TODO(dkwingsmt): Assign the correct view ID once the Linux embedder + // supports multiple views. + // https://github.com/flutter/flutter/issues/138178 + event.view_id = flutter::kFlutterImplicitViewId; self->embedder_api.SendWindowMetricsEvent(self->engine, &event); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc index 996a0da3d9..f8a87c8259 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -352,6 +352,7 @@ void FlutterWindowsView::SendWindowMetrics(size_t width, event.width = width; event.height = height; event.pixel_ratio = dpiScale; + event.view_id = view_id_; engine_->SendWindowMetricsEvent(event); } @@ -588,10 +589,7 @@ void FlutterWindowsView::SendPointerEventWithData( event.device_kind = state->device_kind; event.device = state->pointer_id; event.buttons = state->buttons; - // TODO(dkwingsmt): Use the correct view ID for pointer events once the - // Windows embedder supports multiple views. - // https://github.com/flutter/flutter/issues/138179 - event.view_id = flutter::kFlutterImplicitViewId; + event.view_id = view_id_; // Set metadata that's always the same regardless of the event. event.struct_size = sizeof(event);