From 58c3773f74000f3f6c8537be9f6baf6e3324af27 Mon Sep 17 00:00:00 2001 From: lucasradaelli <64749873+lucasradaelli@users.noreply.github.com> Date: Fri, 22 Apr 2022 12:02:24 -0700 Subject: [PATCH] [fuchsia][a11y] Implements flow control for semantics. (flutter/engine#32796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change implements flow control mechanisms for the Accessibility Bridge that communicates with the Fuchsia semantics services. This ensures that Flutter acknowledges the pace in which the semantics manager is consuming updates and only pushes more when they are ready to be received. This change avoids entirely touching on the problem of how to break flutter semantic messages into batches that can fit in the FIDL channels. This is left for a future change. All code that is related to calculating size limits of FIDL messages is the same logic, only changing location in the file. Test: AccessibilityBridgeTest.BatchesLargeMessages Bug: fxbug.dev/97596 --- .../fuchsia/flutter/accessibility_bridge.cc | 138 +++++++++++------- .../fuchsia/flutter/accessibility_bridge.h | 44 +++++- .../flutter/accessibility_bridge_unittest.cc | 11 ++ .../fuchsia/flutter/flutter_runner_fakes.h | 19 +++ 4 files changed, 154 insertions(+), 58 deletions(-) diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.cc index cf19690764..c1f14df13c 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.cc @@ -9,6 +9,7 @@ #include #include +#include #include #include "flutter/fml/logging.h" @@ -226,6 +227,7 @@ AccessibilityBridge::AccessibilityBridge( std::move(dispatch_semantics_action_callback)), binding_(this), fuchsia_semantics_manager_(semantics_manager.Bind()), + atomic_updates_(std::make_shared>()), inspect_node_(std::move(inspect_node)) { fuchsia_semantics_manager_.set_error_handler([](zx_status_t status) { FML_LOG(ERROR) << "Flutter cannot connect to SemanticsManager with status: " @@ -487,28 +489,19 @@ static uint32_t FlutterIdToFuchsiaId(int32_t flutter_node_id) { return static_cast(flutter_node_id); } -void AccessibilityBridge::PruneUnreachableNodes() { +void AccessibilityBridge::PruneUnreachableNodes( + FuchsiaAtomicUpdate* atomic_update) { const auto& reachable_nodes = GetDescendants(kRootNodeId); - std::vector nodes_to_remove; auto iter = nodes_.begin(); while (iter != nodes_.end()) { int32_t id = iter->first; if (reachable_nodes.find(id) == reachable_nodes.end()) { - // TODO(MI4-2531): This shouldn't be strictly necessary at this level. - if (sizeof(nodes_to_remove) + (nodes_to_remove.size() * kNodeIdSize) >= - kMaxMessageSize) { - tree_ptr_->DeleteSemanticNodes(std::move(nodes_to_remove)); - nodes_to_remove.clear(); - } - nodes_to_remove.push_back(FlutterIdToFuchsiaId(id)); + atomic_update->AddNodeDeletion(FlutterIdToFuchsiaId(id)); iter = nodes_.erase(iter); } else { iter++; } } - if (!nodes_to_remove.empty()) { - tree_ptr_->DeleteSemanticNodes(std::move(nodes_to_remove)); - } } // TODO(FIDL-718) - remove this, handle the error instead in something like @@ -530,8 +523,7 @@ void AccessibilityBridge::AddSemanticsNodeUpdate( << "AccessibilityBridge received an update with out ever getting a root " "node."; - std::vector fuchsia_nodes; - size_t current_size = 0; + FuchsiaAtomicUpdate atomic_update; bool has_root_node_update = false; // TODO(MI4-2498): Actions, Roles, hit test children, additional // flags/states/attr @@ -574,27 +566,7 @@ void AccessibilityBridge::AddSemanticsNodeUpdate( this_node_size += kNodeIdSize * flutter_node.childrenInTraversalOrder.size(); - // TODO(MI4-2531, FIDL-718): Remove this - // This is defensive. If, despite our best efforts, we ended up with a node - // that is larger than the max fidl size, we send no updates. - if (this_node_size >= kMaxMessageSize) { - PrintNodeSizeError(flutter_node.id); - return; - } - current_size += this_node_size; - - // If we would exceed the max FIDL message size by appending this node, - // we should delete/update/commit now. - if (current_size >= kMaxMessageSize) { - tree_ptr_->UpdateSemanticNodes(std::move(fuchsia_nodes)); - fuchsia_nodes.clear(); - current_size = this_node_size; - } - fuchsia_nodes.push_back(std::move(fuchsia_node)); - } - - if (current_size > kMaxMessageSize) { - PrintNodeSizeError(fuchsia_nodes.back().node_id()); + atomic_update.AddNodeUpdate(std::move(fuchsia_node), this_node_size); } // Handles root node update. @@ -603,30 +575,17 @@ void AccessibilityBridge::AddSemanticsNodeUpdate( size_t root_node_size; fuchsia::accessibility::semantics::Node root_update = GetRootNodeUpdate(root_node_size); - // TODO(MI4-2531, FIDL-718): Remove this - // This is defensive. If, despite our best efforts, we ended up with a node - // that is larger than the max fidl size, we send no updates. - if (root_node_size >= kMaxMessageSize) { - PrintNodeSizeError(kRootNodeId); - return; - } - current_size += root_node_size; - // If we would exceed the max FIDL message size by appending this node, - // we should delete/update/commit now. - if (current_size >= kMaxMessageSize) { - tree_ptr_->UpdateSemanticNodes(std::move(fuchsia_nodes)); - fuchsia_nodes.clear(); - } - fuchsia_nodes.push_back(std::move(root_update)); + atomic_update.AddNodeUpdate(std::move(root_update), root_node_size); } - PruneUnreachableNodes(); + PruneUnreachableNodes(&atomic_update); UpdateScreenRects(); - tree_ptr_->UpdateSemanticNodes(std::move(fuchsia_nodes)); - // TODO(dnfield): Implement the callback here - // https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=35718. - tree_ptr_->CommitUpdates([]() {}); + atomic_updates_->push(std::move(atomic_update)); + if (atomic_updates_->size() == 1) { + // There were no commits in the queue, so send this one. + Apply(&atomic_updates_->front()); + } } fuchsia::accessibility::semantics::Node AccessibilityBridge::GetRootNodeUpdate( @@ -927,4 +886,73 @@ void AccessibilityBridge::FillInspectTree(int32_t flutter_node_id, } #endif // !FLUTTER_RELEASE +void AccessibilityBridge::Apply(FuchsiaAtomicUpdate* atomic_update) { + size_t begin = 0; + auto it = atomic_update->deletions.begin(); + + // Process up to kMaxDeletionsPerUpdate deletions at a time. + while (it != atomic_update->deletions.end()) { + std::vector to_delete; + size_t end = std::min(atomic_update->deletions.size() - begin, + kMaxDeletionsPerUpdate); + std::copy(std::make_move_iterator(it), std::make_move_iterator(it + end), + std::back_inserter(to_delete)); + tree_ptr_->DeleteSemanticNodes(std::move(to_delete)); + begin = end; + it += end; + } + + std::vector to_update; + size_t current_size = 0; + for (auto& node_and_size : atomic_update->updates) { + if (current_size + node_and_size.second > kMaxMessageSize) { + tree_ptr_->UpdateSemanticNodes(std::move(to_update)); + current_size = 0; + to_update.clear(); + } + current_size += node_and_size.second; + to_update.push_back(std::move(node_and_size.first)); + } + if (!to_update.empty()) { + tree_ptr_->UpdateSemanticNodes(std::move(to_update)); + } + + // Commit this update and subsequent ones; for flow control wait for a + // response between each commit. + tree_ptr_->CommitUpdates( + [this, atomic_updates = std::weak_ptr>( + atomic_updates_)]() { + auto atomic_updates_ptr = atomic_updates.lock(); + if (!atomic_updates_ptr) { + // The queue no longer exists, which means that is no longer + // necessary. + return; + } + // Removes the update that just went through. + atomic_updates_ptr->pop(); + if (!atomic_updates_ptr->empty()) { + Apply(&atomic_updates_ptr->front()); + } + }); + + atomic_update->deletions.clear(); + atomic_update->updates.clear(); +} + +void AccessibilityBridge::FuchsiaAtomicUpdate::AddNodeUpdate( + fuchsia::accessibility::semantics::Node node, + size_t size) { + if (size > kMaxMessageSize) { + // TODO(MI4-2531, FIDL-718): Remove this + // This is defensive. If, despite our best efforts, we ended up with a node + // that is larger than the max fidl size, we send no updates. + PrintNodeSizeError(node.node_id()); + return; + } + updates.emplace_back(std::move(node), size); +} + +void AccessibilityBridge::FuchsiaAtomicUpdate::AddNodeDeletion(uint32_t id) { + deletions.push_back(id); +} } // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.h b/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.h index 79068ef10a..8e3487c0ad 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge.h @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,7 @@ #include "flutter/lib/ui/semantics/semantics_node.h" namespace flutter_runner { + // Accessibility bridge. // // This class intermediates accessibility-related calls between Fuchsia and @@ -63,6 +65,10 @@ class AccessibilityBridge "flutter::SemanticsNode::id and " "fuchsia::accessibility::semantics::Node::node_id differ in size."); + // Maximum number of node ids to be deleted through the Semantics API. + static constexpr size_t kMaxDeletionsPerUpdate = + kMaxMessageSize / kNodeIdSize; + AccessibilityBridge( SetSemanticsEnabledCallback set_semantics_enabled_callback, DispatchSemanticsActionCallback dispatch_semantics_action_callback, @@ -108,6 +114,30 @@ class AccessibilityBridge // Fuchsia's default root semantic node id. static constexpr int32_t kRootNodeId = 0; + // Represents an atomic semantic update to Fuchsia, which can contain deletes + // and updates of semantic nodes. + // + // An atomic update is a set of operations that take a semantic tree in a + // valid state to another valid state. Please check the semantics FIDL + // documentation for details. + struct FuchsiaAtomicUpdate { + FuchsiaAtomicUpdate() = default; + ~FuchsiaAtomicUpdate() = default; + FuchsiaAtomicUpdate(FuchsiaAtomicUpdate&&) = default; + + // Adds a node to be updated. |size| contains the + // size in bytes of the node to be updated. + void AddNodeUpdate(fuchsia::accessibility::semantics::Node node, + size_t size); + + // Adds a node to be deleted. + void AddNodeDeletion(uint32_t id); + + std::vector> + updates; + std::vector deletions; + }; + // Holds a flutter semantic node and some extra info. // In particular, it adds a screen_rect field to flutter::SemanticsNode. struct SemanticsNode { @@ -157,10 +187,11 @@ class AccessibilityBridge std::unordered_set GetDescendants(int32_t node_id) const; // Removes internal references to any dangling nodes from previous - // updates, and updates the Accessibility service. + // updates, and adds the nodes to be deleted to the current |atomic_update|. // - // May result in a call to FuchsiaAccessibility::Commit(). - void PruneUnreachableNodes(); + // The node ids to be deleted are only collected at this point, and will be + // committed in the next call to |Apply()|. + void PruneUnreachableNodes(FuchsiaAtomicUpdate* atomic_update); // Updates the on-screen positions of accessibility elements, // starting from the root element with an identity matrix. @@ -194,6 +225,10 @@ class AccessibilityBridge fuchsia::accessibility::semantics::Action fuchsia_action, uint32_t node_id); + // Applies the updates and deletions in |atomic_update|, sending them via + // |tree_ptr|. + void Apply(FuchsiaAtomicUpdate* atomic_update); + // |fuchsia::accessibility::semantics::SemanticListener| void OnSemanticsModeChanged(bool enabled, OnSemanticsModeChangedCallback callback) override; @@ -221,6 +256,9 @@ class AccessibilityBridge std::unordered_map nodes_; bool semantics_enabled_; + // Queue of atomic updates to be sent to Fuchsia. + std::shared_ptr> atomic_updates_; + // Node to publish inspect data. inspect::Node inspect_node_; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge_unittest.cc index 0bd6c0acd6..0afa31ea62 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/accessibility_bridge_unittest.cc @@ -802,6 +802,11 @@ TEST_F(AccessibilityBridgeTest, BatchesLargeMessages) { } update.insert(std::make_pair(0, std::move(node0))); + + // Make the semantics manager hold answering to this commit to test the flow + // control. This means the second update will not be pushed until the first + // one has processed. + semantics_manager_.SetShouldHoldCommitResponse(true); accessibility_bridge_->AddSemanticsNodeUpdate(update, 1.f); RunLoopUntilIdle(); @@ -824,6 +829,12 @@ TEST_F(AccessibilityBridgeTest, BatchesLargeMessages) { 1.f); RunLoopUntilIdle(); + // Should still be 0, because the commit was not answered yet. + EXPECT_EQ(0, semantics_manager_.DeleteCount()); + + semantics_manager_.InvokeCommitCallback(); + RunLoopUntilIdle(); + EXPECT_EQ(1, semantics_manager_.DeleteCount()); EXPECT_EQ(next_update_count, semantics_manager_.UpdateCount()); EXPECT_EQ(2, semantics_manager_.CommitCount()); diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_fakes.h b/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_fakes.h index 531eb9341f..bbd6c3e4ce 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_fakes.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_fakes.h @@ -78,6 +78,18 @@ class MockSemanticsManager int UpdateCount() const { return update_count_; } bool UpdateOverflowed() const { return update_overflowed_; } + bool ShouldHoldCommitResponse() const { return should_hold_commit_response_; } + + void SetShouldHoldCommitResponse(bool value) { + should_hold_commit_response_ = value; + } + + void InvokeCommitCallback() { + if (commit_callback_) { + commit_callback_(); + } + } + int CommitCount() const { return commit_count_; } const std::vector& LastUpdatedNodes() @@ -87,6 +99,11 @@ class MockSemanticsManager void CommitUpdates(CommitUpdatesCallback callback) override { commit_count_++; + if (should_hold_commit_response_) { + commit_callback_ = std::move(callback); + return; + } + callback(); } void SendSemanticEvent( @@ -112,6 +129,8 @@ class MockSemanticsManager int delete_count_; bool delete_overflowed_; std::vector last_deleted_node_ids_; + bool should_hold_commit_response_ = false; + CommitUpdatesCallback commit_callback_; int commit_count_; std::vector last_events_; };