flutter_flutter/engine/src/flutter/shell/common/pointer_data_dispatcher.cc
liyuqian ffca51fcdd Reland "Smooth out iOS irregular input events delivery (#12280)" (flutter/engine#12385)
This reverts commit 56bb40c0179628e37ba3614534552441642c0492.

Additionally, we fix https://github.com/flutter/flutter/issues/40863 by adding a secondary VSYNC callback.

Unit tests are updated to provide VSYNC mocking and check the fix of https://github.com/flutter/flutter/issues/40863.

The root cause of having https://github.com/flutter/flutter/issues/40863 is the false assumption that each input event must trigger a new frame. That was true in the framework PR https://github.com/flutter/flutter/pull/36616 because the input events there are all scrolling move events. When the PR was ported to the engine, we can no longer distinguish different types of events, and tap events may no longer trigger a new frame.

Therefore, this PR directly hooks into the `VsyncWaiter` and uses its (newly added) secondary callback to dispatch the pending input event.
2019-09-30 11:25:50 -07:00

64 lines
2.2 KiB
C++

// 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/shell/common/pointer_data_dispatcher.h"
namespace flutter {
PointerDataDispatcher::~PointerDataDispatcher() = default;
DefaultPointerDataDispatcher::~DefaultPointerDataDispatcher() = default;
SmoothPointerDataDispatcher::SmoothPointerDataDispatcher(Delegate& delegate)
: DefaultPointerDataDispatcher(delegate), weak_factory_(this) {}
SmoothPointerDataDispatcher::~SmoothPointerDataDispatcher() = default;
void DefaultPointerDataDispatcher::DispatchPacket(
std::unique_ptr<PointerDataPacket> packet,
uint64_t trace_flow_id) {
delegate_.DoDispatchPacket(std::move(packet), trace_flow_id);
}
void SmoothPointerDataDispatcher::DispatchPacket(
std::unique_ptr<PointerDataPacket> packet,
uint64_t trace_flow_id) {
if (is_pointer_data_in_progress_) {
if (pending_packet_ != nullptr) {
DispatchPendingPacket();
}
pending_packet_ = std::move(packet);
pending_trace_flow_id_ = trace_flow_id;
} else {
FML_DCHECK(pending_packet_ == nullptr);
DefaultPointerDataDispatcher::DispatchPacket(std::move(packet),
trace_flow_id);
}
is_pointer_data_in_progress_ = true;
ScheduleSecondaryVsyncCallback();
}
void SmoothPointerDataDispatcher::ScheduleSecondaryVsyncCallback() {
delegate_.ScheduleSecondaryVsyncCallback(
[dispatcher = weak_factory_.GetWeakPtr()]() {
if (dispatcher && dispatcher->is_pointer_data_in_progress_) {
if (dispatcher->pending_packet_ != nullptr) {
dispatcher->DispatchPendingPacket();
} else {
dispatcher->is_pointer_data_in_progress_ = false;
}
}
});
}
void SmoothPointerDataDispatcher::DispatchPendingPacket() {
FML_DCHECK(pending_packet_ != nullptr);
FML_DCHECK(is_pointer_data_in_progress_);
DefaultPointerDataDispatcher::DispatchPacket(std::move(pending_packet_),
pending_trace_flow_id_);
pending_packet_ = nullptr;
pending_trace_flow_id_ = -1;
ScheduleSecondaryVsyncCallback();
}
} // namespace flutter