diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc index 06a0da74f9..4444d809b3 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc @@ -146,12 +146,14 @@ void Engine::Initialize( fidl::InterfaceHandle focuser; fidl::InterfaceHandle view_ref_focused; fidl::InterfaceHandle touch_source; + fidl::InterfaceHandle mouse_source; fuchsia::ui::composition::ViewBoundProtocols flatland_view_protocols; if (use_flatland) { flatland_view_protocols.set_view_focuser(focuser.NewRequest()); flatland_view_protocols.set_view_ref_focused(view_ref_focused.NewRequest()); flatland_view_protocols.set_touch_source(touch_source.NewRequest()); + flatland_view_protocols.set_mouse_source(mouse_source.NewRequest()); } else { gfx_protocols.set_view_focuser(focuser.NewRequest()); gfx_protocols.set_view_ref_focused(view_ref_focused.NewRequest()); @@ -370,6 +372,7 @@ void Engine::Initialize( focuser = std::move(focuser), view_ref_focused = std::move(view_ref_focused), touch_source = std::move(touch_source), + mouse_source = std::move(mouse_source), on_session_listener_error_callback = std::move(on_session_listener_error_callback), on_enable_wireframe_callback = @@ -445,7 +448,8 @@ void Engine::Initialize( shell, shell.GetTaskRunners(), std::move(view_ref), std::move(external_view_embedder), std::move(ime_service), std::move(keyboard), std::move(touch_source), - std::move(focuser), std::move(view_ref_focused), + std::move(mouse_source), std::move(focuser), + std::move(view_ref_focused), std::move(parent_viewport_watcher), std::move(on_enable_wireframe_callback), std::move(on_create_flatland_view_callback), @@ -462,7 +466,8 @@ void Engine::Initialize( shell, shell.GetTaskRunners(), std::move(view_ref), std::move(external_view_embedder), std::move(ime_service), std::move(keyboard), std::move(touch_source), - std::move(focuser), std::move(view_ref_focused), + std::move(mouse_source), std::move(focuser), + std::move(view_ref_focused), std::move(session_listener_request), std::move(on_session_listener_error_callback), std::move(on_enable_wireframe_callback), diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc index d585cd24f4..712049b1e2 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc @@ -16,6 +16,7 @@ FlatlandPlatformView::FlatlandPlatformView( fidl::InterfaceHandle ime_service, fidl::InterfaceHandle keyboard, fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source, fidl::InterfaceHandle focuser, fidl::InterfaceHandle view_ref_focused, fidl::InterfaceHandle @@ -38,6 +39,7 @@ FlatlandPlatformView::FlatlandPlatformView( std::move(ime_service), std::move(keyboard), std::move(touch_source), + std::move(mouse_source), std::move(focuser), std::move(view_ref_focused), std::move(wireframe_enabled_callback), diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.h b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.h index 0b45d8da30..0709956326 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.h @@ -30,6 +30,7 @@ class FlatlandPlatformView final : public flutter_runner::PlatformView { fidl::InterfaceHandle ime_service, fidl::InterfaceHandle keyboard, fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source, fidl::InterfaceHandle focuser, fidl::InterfaceHandle view_ref_focused, diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.cc index 26cc8d228f..9f5cf37275 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.cc @@ -16,6 +16,7 @@ GfxPlatformView::GfxPlatformView( fidl::InterfaceHandle ime_service, fidl::InterfaceHandle keyboard, fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source, fidl::InterfaceHandle focuser, fidl::InterfaceHandle view_ref_focused, fidl::InterfaceRequest @@ -39,6 +40,7 @@ GfxPlatformView::GfxPlatformView( std::move(ime_service), std::move(keyboard), std::move(touch_source), + std::move(mouse_source), std::move(focuser), std::move(view_ref_focused), std::move(wireframe_enabled_callback), diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.h b/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.h index c3aacd73ae..40130a2bf0 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/gfx_platform_view.h @@ -33,6 +33,7 @@ class GfxPlatformView final : public flutter_runner::PlatformView, fidl::InterfaceHandle ime_service, fidl::InterfaceHandle keyboard, fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source, fidl::InterfaceHandle focuser, fidl::InterfaceHandle view_ref_focused, diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc index 1871ecc284..707ab0704c 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc @@ -58,6 +58,7 @@ PlatformView::PlatformView( fidl::InterfaceHandle ime_service, fidl::InterfaceHandle keyboard, fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source, fidl::InterfaceHandle focuser, fidl::InterfaceHandle view_ref_focused, OnEnableWireframe wireframe_enabled_callback, @@ -75,7 +76,8 @@ PlatformView::PlatformView( std::make_shared(std::move(view_ref_focused), std::move(focuser))), pointer_delegate_( - std::make_shared(std::move(touch_source))), + std::make_shared(std::move(touch_source), + std::move(mouse_source))), ime_client_(this), text_sync_service_(ime_service.Bind()), keyboard_listener_binding_(this), diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h index c11caedad2..6c367fdab1 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h @@ -71,6 +71,7 @@ class PlatformView : public flutter::PlatformView, fidl::InterfaceHandle ime_service, fidl::InterfaceHandle keyboard, fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source, fidl::InterfaceHandle focuser, fidl::InterfaceHandle view_ref_focused, diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc index e69a409b8e..12b597e7f0 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -252,6 +252,12 @@ class PlatformViewBuilder { return *this; } + PlatformViewBuilder& SetMouseSource( + fidl::InterfaceHandle mouse_source) { + mouse_source_ = std::move(mouse_source); + return *this; + } + PlatformViewBuilder& SetFocuser( fidl::InterfaceHandle focuser) { focuser_ = std::move(focuser); @@ -308,7 +314,8 @@ class PlatformViewBuilder { return GfxPlatformView( delegate_, task_runners_, std::move(view_ref_pair_.view_ref), external_external_view_embedder_, std::move(ime_service_), - std::move(keyboard_), std::move(touch_source_), std::move(focuser_), + std::move(keyboard_), std::move(touch_source_), + std::move(mouse_source_), std::move(focuser_), std::move(view_ref_focused_), std::move(session_listener_request_), std::move(on_session_listener_error_callback_), std::move(wireframe_enabled_callback_), @@ -333,6 +340,7 @@ class PlatformViewBuilder { fidl::InterfaceHandle ime_service_; fidl::InterfaceHandle keyboard_; fidl::InterfaceHandle touch_source_; + fidl::InterfaceHandle mouse_source_; fidl::InterfaceHandle view_ref_focused_; fidl::InterfaceHandle focuser_; fidl::InterfaceRequest diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.cc index 72b2789bac..d28fbb927e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.cc @@ -11,6 +11,8 @@ #include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" +// TODO(fxbug.dev/87076): Add MouseSource tests. + namespace fuchsia::ui::pointer { // For using TouchInteractionId as a map key. bool operator==(const fuchsia::ui::pointer::TouchInteractionId& a, @@ -23,6 +25,8 @@ bool operator==(const fuchsia::ui::pointer::TouchInteractionId& a, namespace flutter_runner { using fup_EventPhase = fuchsia::ui::pointer::EventPhase; +using fup_MouseDeviceInfo = fuchsia::ui::pointer::MouseDeviceInfo; +using fup_MouseEvent = fuchsia::ui::pointer::MouseEvent; using fup_TouchEvent = fuchsia::ui::pointer::TouchEvent; using fup_TouchIxnStatus = fuchsia::ui::pointer::TouchInteractionStatus; using fup_TouchResponse = fuchsia::ui::pointer::TouchResponse; @@ -30,13 +34,17 @@ using fup_TouchResponseType = fuchsia::ui::pointer::TouchResponseType; using fup_ViewParameters = fuchsia::ui::pointer::ViewParameters; namespace { -void MaybeIssueTraceEvent(const fup_TouchEvent& event) { - if (event.has_trace_flow_id()) { - TRACE_FLOW_END("input", "dispatch_event_to_client", event.trace_flow_id()); - } +void IssueTouchTraceEvent(const fup_TouchEvent& event) { + FML_DCHECK(event.has_trace_flow_id()) << "API guarantee"; + TRACE_FLOW_END("input", "dispatch_event_to_client", event.trace_flow_id()); } -bool HasValidatedPointerSample(const fup_TouchEvent& event) { +void IssueMouseTraceEvent(const fup_MouseEvent& event) { + FML_DCHECK(event.has_trace_flow_id()) << "API guarantee"; + TRACE_FLOW_END("input", "dispatch_event_to_client", event.trace_flow_id()); +} + +bool HasValidatedTouchSample(const fup_TouchEvent& event) { if (!event.has_pointer_sample()) { return false; } @@ -47,6 +55,20 @@ bool HasValidatedPointerSample(const fup_TouchEvent& event) { return true; } +bool HasValidatedMouseSample(const fup_MouseEvent& event) { + if (!event.has_pointer_sample()) { + return false; + } + const auto& sample = event.pointer_sample(); + FML_DCHECK(sample.has_device_id()) << "API guarantee"; + FML_DCHECK(sample.has_position_in_viewport()) << "API guarantee"; + FML_DCHECK(!sample.has_pressed_buttons() || + sample.pressed_buttons().size() > 0) + << "API guarantee"; + + return true; +} + std::array ViewportToViewCoordinates( std::array viewport_coordinates, const std::array& viewport_to_view_transform) { @@ -73,7 +95,7 @@ std::array ViewportToViewCoordinates( } } -flutter::PointerData::Change GetChangeFromPointerEventPhase( +flutter::PointerData::Change GetChangeFromTouchEventPhase( fup_EventPhase phase) { switch (phase) { case fup_EventPhase::ADD: @@ -111,29 +133,62 @@ std::array ClampToViewSpace(const float x, return {clamped_x, clamped_y}; } +flutter::PointerData::Change ComputePhase( + bool any_button_down, + std::unordered_set& mouse_down, + uint32_t id) { + if (!mouse_down.count(id) && !any_button_down) { + return flutter::PointerData::Change::kHover; + } else if (!mouse_down.count(id) && any_button_down) { + mouse_down.insert(id); + return flutter::PointerData::Change::kDown; + } else if (mouse_down.count(id) && any_button_down) { + return flutter::PointerData::Change::kMove; + } else if (mouse_down.count(id) && !any_button_down) { + mouse_down.erase(id); + return flutter::PointerData::Change::kUp; + } + + FML_UNREACHABLE(); + return flutter::PointerData::Change::kCancel; +} + +// Flutter's PointerData.device field is 64 bits and is expected to be unique +// for each pointer. We pack Fuchsia's device ID (hi) and pointer ID (lo) into +// 64 bits to retain uniqueness across multiple touch devices. +uint64_t PackFuchsiaDeviceIdAndPointerId(uint32_t fuchsia_device_id, + uint32_t fuchsia_pointer_id) { + return (((uint64_t)fuchsia_device_id) << 32) | fuchsia_pointer_id; +} + // It returns a "draft" because the coordinates are logical. Later, view pixel // ratio is applied to obtain physical coordinates. // // The flutter pointerdata state machine has extra phases, which this function // synthesizes on the fly. Hence the return data is a flutter pointerdata, and -// optionally a second synthesized one. +// optionally a second one. // For example: , , . +// TODO(fxbug.dev/87074): Let PointerDataPacketConverter synthesize events. // // Flutter gestures expect a gesture to start within the logical view space, and // is not tolerant of floating point drift. This function coerces just the DOWN // event's coordinate to start within the logical view. std::pair> -CreatePointerDraft(const fup_TouchEvent& event, - const fup_ViewParameters& view_parameters) { - FML_DCHECK(HasValidatedPointerSample(event)) << "precondition"; +CreateTouchDraft(const fup_TouchEvent& event, + const fup_ViewParameters& view_parameters) { + FML_DCHECK(HasValidatedTouchSample(event)) << "precondition"; const auto& sample = event.pointer_sample(); + const auto& ixn = sample.interaction(); + flutter::PointerData ptr; ptr.Clear(); ptr.time_stamp = event.timestamp() / 1000; // in microseconds - ptr.change = GetChangeFromPointerEventPhase(sample.phase()); + ptr.change = GetChangeFromTouchEventPhase(sample.phase()); ptr.kind = flutter::PointerData::DeviceKind::kTouch; - ptr.device = sample.interaction().device_id; - ptr.pointer_identifier = sample.interaction().pointer_id; + // Load Fuchsia's pointer ID onto Flutter's |device| field, and not the + // |pointer_identifier| field. The latter is written by + // PointerDataPacketConverter, to track individual gesture interactions. + ptr.device = PackFuchsiaDeviceIdAndPointerId(ixn.device_id, ixn.pointer_id); // View parameters can change mid-interaction; apply transform on the fly. auto logical = ViewportToViewCoordinates(sample.position_in_viewport(), @@ -146,7 +201,7 @@ CreatePointerDraft(const fup_TouchEvent& event, flutter::PointerData down; memcpy(&down, &ptr, sizeof(flutter::PointerData)); down.change = flutter::PointerData::Change::kDown; - { // DOWN event needs to start in the logical view space. + { // Ensure gesture recognition: DOWN starts in the logical view space. auto [x, y] = ClampToViewSpace(down.physical_x, down.physical_y, view_parameters); down.physical_x = x; @@ -163,6 +218,95 @@ CreatePointerDraft(const fup_TouchEvent& event, } } +// It returns a "draft" because the coordinates are logical. Later, view pixel +// ratio is applied to obtain physical coordinates. +// +// Phase data is computed before this call; it involves state tracking based on +// button-down state. +// +// Button data, if available, gets packed into the |buttons| field, in flutter +// button order (kMousePrimaryButton, etc). The device-assigned button IDs are +// provided in priority order in MouseEvent.device_info (at the start of channel +// connection), and maps from device button ID (given in fup_MouseEvent) to +// flutter button ID (flutter::PointerData). +// +// Scroll data, if available, gets packed into the |scroll_delta_x| or +// |scroll_delta_y| fields, and the |signal_kind| field is set to kScroll. +// The PointerDataPacketConverter reads this field to synthesize events to match +// Flutter's expected pointer stream. +// TODO(fxbug.dev/87073): PointerDataPacketConverter should synthesize a +// discrete scroll event on kDown or kUp, to match engine expectations. +// +// Flutter gestures expect a gesture to start within the logical view space, and +// is not tolerant of floating point drift. This function coerces just the DOWN +// event's coordinate to start within the logical view. +flutter::PointerData CreateMouseDraft(const fup_MouseEvent& event, + const flutter::PointerData::Change phase, + const fup_ViewParameters& view_parameters, + const fup_MouseDeviceInfo& device_info) { + FML_DCHECK(HasValidatedMouseSample(event)) << "precondition"; + const auto& sample = event.pointer_sample(); + + flutter::PointerData ptr; + ptr.Clear(); + ptr.time_stamp = event.timestamp() / 1000; // in microseconds + ptr.change = phase; + ptr.kind = flutter::PointerData::DeviceKind::kMouse; + ptr.device = sample.device_id(); + + // View parameters can change mid-interaction; apply transform on the fly. + auto logical = + ViewportToViewCoordinates(sample.position_in_viewport(), + view_parameters.viewport_to_view_transform); + ptr.physical_x = logical[0]; // Not yet physical; adjusted in PlatformView. + ptr.physical_y = logical[1]; // Not yet physical; adjusted in PlatformView. + + // Ensure gesture recognition: DOWN starts in the logical view space. + if (ptr.change == flutter::PointerData::Change::kDown) { + auto [x, y] = + ClampToViewSpace(ptr.physical_x, ptr.physical_y, view_parameters); + ptr.physical_x = x; + ptr.physical_y = y; + } + + if (sample.has_pressed_buttons()) { + int64_t flutter_buttons = 0; + const auto& pressed = sample.pressed_buttons(); + for (size_t idx = 0; idx < pressed.size(); ++idx) { + const uint8_t button_id = pressed[idx]; + FML_DCHECK(device_info.has_buttons()) << "API guarantee"; + // Priority 0 maps to kPrimaryButton, and so on. + for (uint8_t prio = 0; prio < device_info.buttons().size(); ++prio) { + if (button_id == device_info.buttons()[prio]) { + flutter_buttons |= (1 << prio); + } + } + } + FML_DCHECK(flutter_buttons != 0); + ptr.buttons = flutter_buttons; + } + + // Fuchsia currently provides scroll data in "ticks", not physical pixels. + // However, Flutter expects scroll data in physical pixels. To compensate for + // lack of guidance, we make up a "reasonable amount". + // TODO(fxbug.dev/85388): Replace with physical pixel scroll. + const int kScrollOffsetMultiplier = 20; + + if (sample.has_scroll_v()) { + ptr.signal_kind = flutter::PointerData::SignalKind::kScroll; + double dy = -sample.scroll_v() * kScrollOffsetMultiplier; // logical amount + ptr.scroll_delta_y = dy; // Not yet physical; adjusted in Platform View. + } + + if (sample.has_scroll_h()) { + ptr.signal_kind = flutter::PointerData::SignalKind::kScroll; + double dx = sample.scroll_h() * kScrollOffsetMultiplier; // logical amount + ptr.scroll_delta_x = dx; // Not yet physical; adjusted in Platform View. + } + + return ptr; +} + // Helper to insert one or two events into a vector buffer. void InsertIntoBuffer( std::pair> events, @@ -176,37 +320,38 @@ void InsertIntoBuffer( } // namespace // Core logic of this class. +// Aim to keep state management in this function. void PointerDelegate::WatchLoop( std::function)> callback) { FML_LOG(INFO) << "Flutter - PointerDelegate started."; - if (watch_loop_) { + if (touch_responder_) { FML_LOG(ERROR) << "PointerDelegate::WatchLoop() must be called once."; return; } - watch_loop_ = [this, callback](std::vector events) { + touch_responder_ = [this, callback](std::vector events) { TRACE_EVENT0("flutter", "PointerDelegate::TouchHandler"); - FML_DCHECK(responses_.empty()) << "precondition"; + FML_DCHECK(touch_responses_.empty()) << "precondition"; std::vector to_client; for (const fup_TouchEvent& event : events) { - MaybeIssueTraceEvent(event); + IssueTouchTraceEvent(event); fup_TouchResponse response; // Response per event, matched on event's index. if (event.has_view_parameters()) { - view_parameters_ = std::move(event.view_parameters()); + touch_view_parameters_ = std::move(event.view_parameters()); } - if (HasValidatedPointerSample(event)) { + if (HasValidatedTouchSample(event)) { const auto& sample = event.pointer_sample(); const auto& ixn = sample.interaction(); if (sample.phase() == fup_EventPhase::ADD && !event.has_interaction_result()) { - buffer_.emplace(ixn, std::vector()); + touch_buffer_.emplace(ixn, std::vector()); } - FML_DCHECK(view_parameters_.has_value()) << "API guarantee"; - auto events = CreatePointerDraft(event, view_parameters_.value()); - if (buffer_.count(ixn) > 0) { - InsertIntoBuffer(std::move(events), &buffer_[ixn]); + FML_DCHECK(touch_view_parameters_.has_value()) << "API guarantee"; + auto events = CreateTouchDraft(event, touch_view_parameters_.value()); + if (touch_buffer_.count(ixn) > 0) { + InsertIntoBuffer(std::move(events), &touch_buffer_[ixn]); } else { InsertIntoBuffer(std::move(events), &to_client); } @@ -217,23 +362,56 @@ void PointerDelegate::WatchLoop( const auto& result = event.interaction_result(); const auto& ixn = result.interaction; if (result.status == fup_TouchIxnStatus::GRANTED && - buffer_.count(ixn) > 0) { + touch_buffer_.count(ixn) > 0) { FML_DCHECK(to_client.empty()) << "invariant"; - to_client.insert(to_client.end(), buffer_[ixn].begin(), - buffer_[ixn].end()); + to_client.insert(to_client.end(), touch_buffer_[ixn].begin(), + touch_buffer_[ixn].end()); } - buffer_.erase(ixn); // Result seen, delete the buffer. + touch_buffer_.erase(ixn); // Result seen, delete the buffer. } - responses_.push_back(std::move(response)); + touch_responses_.push_back(std::move(response)); } callback(std::move(to_client)); // Notify client of touch events, if any. - touch_source_->Watch(std::move(responses_), /*copy*/ watch_loop_); - responses_.clear(); + touch_source_->Watch(std::move(touch_responses_), + /*copy*/ touch_responder_); + touch_responses_.clear(); }; - touch_source_->Watch(std::move(responses_), /*copy*/ watch_loop_); - responses_.clear(); + mouse_responder_ = [this, callback](std::vector events) { + TRACE_EVENT0("flutter", "PointerDelegate::MouseHandler"); + std::vector to_client; + for (fup_MouseEvent& event : events) { + IssueMouseTraceEvent(event); + if (event.has_device_info()) { + const auto& id = event.device_info().id(); + mouse_device_info_[id] = std::move(*event.mutable_device_info()); + } + if (event.has_view_parameters()) { + mouse_view_parameters_ = std::move(event.view_parameters()); + } + if (HasValidatedMouseSample(event)) { + const auto& sample = event.pointer_sample(); + const auto& id = sample.device_id(); + const bool any_button_down = sample.has_pressed_buttons(); + FML_DCHECK(mouse_view_parameters_.has_value()) << "API guarantee"; + FML_DCHECK(mouse_device_info_.count(id) > 0) << "API guarantee"; + + const auto phase = ComputePhase(any_button_down, mouse_down_, id); + flutter::PointerData data = + CreateMouseDraft(event, phase, mouse_view_parameters_.value(), + mouse_device_info_[id]); + to_client.emplace_back(std::move(data)); + } + } + callback(std::move(to_client)); + mouse_source_->Watch(/*copy*/ mouse_responder_); + }; + + // Start watching both channels. + touch_source_->Watch(std::move(touch_responses_), /*copy*/ touch_responder_); + touch_responses_.clear(); + mouse_source_->Watch(/*copy*/ mouse_responder_); } } // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.h b/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.h index 6de02e8032..97f0f332d3 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate.h @@ -7,9 +7,11 @@ #include +#include #include #include #include +#include #include #include "flutter/lib/ui/window/pointer_data.h" @@ -26,28 +28,35 @@ struct IxnHasher { } }; -// Channel processor for fuchsia.ui.pointer.TouchSource protocol. It manages the -// channel state, collects touch events, and surfaces them to PlatformView as -// flutter::PointerData events for further processing and dispatch. +// Channel processors for fuchsia.ui.pointer.TouchSource and MouseSource +// protocols. It manages the channel state, collects touch and mouse events, and +// surfaces them to PlatformView as flutter::PointerData events for further +// processing and dispatch. class PointerDelegate { public: PointerDelegate( - fidl::InterfaceHandle touch_source) - : touch_source_(touch_source.Bind()) {} + fidl::InterfaceHandle touch_source, + fidl::InterfaceHandle mouse_source) + : touch_source_(touch_source.Bind()), + mouse_source_(mouse_source.Bind()) {} - // Each TouchEvent must carry a TouchPointerSample, and the supplied callback - // will translate each to a flutter::PointerData, and the vector of - // PointerData placed in a PointerDataPacket for transport to the Engine. + // This function collects Fuchsia's TouchPointerSample and MousePointerSample + // data and transforms them into flutter::PointerData structs. It then calls + // the supplied callback with a vector of flutter::PointerData, which (1) does + // last processing (applies metrics), and (2) packs these flutter::PointerData + // in a flutter::PointerDataPacket for transport to the Engine. void WatchLoop( std::function)> callback); private: + /***** TOUCH STATE *****/ + // Channel for touch events from Scenic. fuchsia::ui::pointer::TouchSourcePtr touch_source_; // Receive touch events from Scenic. Must be copyable. std::function)> - watch_loop_; + touch_responder_; // Per-interaction buffer of touch events from Scenic. When an interaction // starts with event.pointer_sample.phase == ADD, we allocate a buffer and @@ -85,20 +94,52 @@ class PointerDelegate { std::unordered_map, IxnHasher> - buffer_; + touch_buffer_; // The fuchsia.ui.pointer.TouchSource protocol allows one in-flight // hanging-get Watch() call to gather touch events, and the client is expected // to respond with consumption intent on the following hanging-get Watch() // call. Store responses here for the next call. - std::vector responses_; + std::vector touch_responses_; - // The fuchsia.ui.pointer.TouchSource protocol issues a channel-global view + // The fuchsia.ui.pointer.TouchSource protocol issues channel-global view // parameters on connection and on change. Events must apply these view // parameters to correctly map to logical view coordinates. The "nullopt" // state represents the absence of view parameters, early in the protocol // lifecycle. - std::optional view_parameters_; + std::optional touch_view_parameters_; + + /***** MOUSE STATE *****/ + + // Channel for mouse events from Scenic. + fuchsia::ui::pointer::MouseSourcePtr mouse_source_; + + // Receive mouse events from Scenic. Must be copyable. + std::function)> + mouse_responder_; + + // The set of mouse devices that are currently interacting with the UI. + // A mouse is considered flutter::PointerData::Change::kDown if any button is + // pressed. This set is used to correctly set the phase in + // flutter::PointerData.change, with this high-level algorithm: + // if !mouse_down[id] && !button then: change = kHover + // if !mouse_down[id] && button then: change = kDown; mouse_down.add(id) + // if mouse_down[id] && button then: change = kMove + // if mouse_down[id] && !button then: change = kUp; mouse_down.remove(id) + std::unordered_set mouse_down_; + + // For each mouse device, its device-specific information, such as mouse + // button priority order. + std::unordered_map + mouse_device_info_; + + // The fuchsia.ui.pointer.MouseSource protocol issues channel-global view + // parameters on connection and on change. Events must apply these view + // parameters to correctly map to logical view coordinates. The "nullopt" + // state represents the absence of view parameters, early in the protocol + // lifecycle. + std::optional mouse_view_parameters_; }; } // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate_unittests.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate_unittests.cc index daaa15e0a6..5a6bc9f533 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate_unittests.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/pointer_delegate_unittests.cc @@ -15,6 +15,7 @@ #include "flutter/fml/logging.h" #include "flutter/fml/macros.h" #include "pointer_delegate.h" +#include "tests/fakes/mouse_source.h" #include "tests/fakes/touch_source.h" #include "tests/pointer_event_utility.h" @@ -32,7 +33,7 @@ using fup_ViewParameters = fuchsia::ui::pointer::ViewParameters; constexpr std::array, 2> kRect = {{{0, 0}, {20, 20}}}; constexpr std::array kIdentity = {1, 0, 0, 0, 1, 0, 0, 0, 1}; -constexpr fup_TouchIxnId kIxnOne = {.device_id = 0u, +constexpr fup_TouchIxnId kIxnOne = {.device_id = 1u, .pointer_id = 1u, .interaction_id = 2u}; @@ -42,18 +43,22 @@ class PointerDelegateTest : public ::testing::Test { protected: PointerDelegateTest() : loop_(&kAsyncLoopConfigAttachToCurrentThread) { touch_source_ = std::make_unique(); + mouse_source_ = std::make_unique(); pointer_delegate_ = std::make_unique( - touch_source_bindings_.AddBinding(touch_source_.get())); + touch_source_bindings_.AddBinding(touch_source_.get()), + mouse_source_bindings_.AddBinding(mouse_source_.get())); } void RunLoopUntilIdle() { loop_.RunUntilIdle(); } std::unique_ptr touch_source_; + std::unique_ptr mouse_source_; std::unique_ptr pointer_delegate_; private: async::Loop loop_; fidl::BindingSet touch_source_bindings_; + fidl::BindingSet mouse_source_bindings_; FML_DISALLOW_COPY_AND_ASSIGN(PointerDelegateTest); }; @@ -611,7 +616,7 @@ TEST_F(PointerDelegateTest, Protocol_PointersAreIndependent) { RunLoopUntilIdle(); // Server gets watch call. constexpr fup_TouchIxnId kIxnTwo = { - .device_id = 0u, .pointer_id = 2u, .interaction_id = 2u}; + .device_id = 1u, .pointer_id = 2u, .interaction_id = 2u}; // Fuchsia ptr1 ADD and ptr2 ADD, no grant result for either - buffer them. std::vector events = @@ -641,11 +646,17 @@ TEST_F(PointerDelegateTest, Protocol_PointersAreIndependent) { touch_source_->ScheduleCallback(std::move(events)); RunLoopUntilIdle(); + // Note: Fuchsia's device and pointer IDs (both 32 bit) are coerced together + // to fit in Flutter's 64-bit device ID. However, Flutter's pointer_identifier + // is not set by platform runner code - PointerDataCaptureConverter (PDPC) + // sets it. ASSERT_TRUE(pointers.has_value()); ASSERT_EQ(pointers.value().size(), 2u); - EXPECT_EQ(pointers.value()[0].pointer_identifier, 2); + EXPECT_EQ(pointers.value()[0].pointer_identifier, 0); // reserved for PDPC + EXPECT_EQ(pointers.value()[0].device, (int64_t)((1ul << 32) | 2u)); EXPECT_EQ(pointers.value()[0].change, flutter::PointerData::Change::kAdd); - EXPECT_EQ(pointers.value()[1].pointer_identifier, 2); + EXPECT_EQ(pointers.value()[1].pointer_identifier, 0); // reserved for PDPC + EXPECT_EQ(pointers.value()[1].device, (int64_t)((1ul << 32) | 2u)); EXPECT_EQ(pointers.value()[1].change, flutter::PointerData::Change::kDown); pointers = {}; @@ -659,9 +670,11 @@ TEST_F(PointerDelegateTest, Protocol_PointersAreIndependent) { ASSERT_TRUE(pointers.has_value()); ASSERT_EQ(pointers.value().size(), 2u); - EXPECT_EQ(pointers.value()[0].pointer_identifier, 1); + EXPECT_EQ(pointers.value()[0].pointer_identifier, 0); // reserved for PDPC + EXPECT_EQ(pointers.value()[0].device, (int64_t)((1ul << 32) | 1u)); EXPECT_EQ(pointers.value()[0].change, flutter::PointerData::Change::kAdd); - EXPECT_EQ(pointers.value()[1].pointer_identifier, 1); + EXPECT_EQ(pointers.value()[1].pointer_identifier, 0); // reserved for PDPC + EXPECT_EQ(pointers.value()[1].device, (int64_t)((1ul << 32) | 1u)); EXPECT_EQ(pointers.value()[1].change, flutter::PointerData::Change::kDown); pointers = {}; } diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/mouse_source.h b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/mouse_source.h new file mode 100644 index 0000000000..47585e69a4 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/mouse_source.h @@ -0,0 +1,37 @@ +// 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. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_MOUSE_SOURCE_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_MOUSE_SOURCE_H_ + +#include + +#include "flutter/fml/logging.h" + +namespace flutter_runner::testing { + +// A test stub to act as the protocol server. A test can control what is sent +// back by this server implementation, via the ScheduleCallback call. +class FakeMouseSource : public fuchsia::ui::pointer::MouseSource { + public: + // |fuchsia.ui.pointer.MouseSource| + void Watch(MouseSource::WatchCallback callback) override { + callback_ = std::move(callback); + } + + // Have the server issue events to the client's hanging-get Watch call. + void ScheduleCallback(std::vector events) { + FML_CHECK(callback_) << "require a valid WatchCallback"; + callback_(std::move(events)); + } + + private: + // Client-side logic to invoke on Watch() call's return. A test triggers it + // with ScheduleCallback(). + MouseSource::WatchCallback callback_; +}; + +} // namespace flutter_runner::testing + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_MOUSE_SOURCE_H_