Track motion events for reuse post gesture disambiguation (flutter/engine#19484)

This change makes it so that we track all the motion events encountered by `FlutterView` and all of its subviews in the `MotionEventTracker` class, indexed by a unique `MotionEventId`. This identifier is then passed to the Flutter framework as seen in https://github.com/flutter/flutter/pull/60930. Once the gestures take part in gesture disambiguation and are sent back to the engine, we look-up the original motion event using the `MotionEventId` and dispatch it to the platform.

Bug: https://github.com/flutter/flutter/issues/58837
This commit is contained in:
Kaushik Iska
2020-07-06 22:22:37 -07:00
committed by GitHub
parent f9ad019129
commit 8818677dfd
11 changed files with 165 additions and 38 deletions

View File

@@ -697,6 +697,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/Flutt
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterSurfaceView.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterSurfaceView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterTextureView.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterTextureView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/RenderMode.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/RenderMode.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/SplashScreen.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/SplashScreen.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/SplashScreenProvider.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/SplashScreenProvider.java

View File

@@ -287,9 +287,9 @@ void _invoke3<A1, A2, A3>(void callback(A1 a1, A2 a2, A3 a3)?, Zone zone, A1 arg
// If this value changes, update the encoding code in the following files: // If this value changes, update the encoding code in the following files:
// //
// * pointer_data.cc // * pointer_data.cc
// * pointers.dart // * pointer.dart
// * AndroidTouchProcessor.java // * AndroidTouchProcessor.java
const int _kPointerDataFieldCount = 28; const int _kPointerDataFieldCount = 29;
PointerDataPacket _unpackPointerDataPacket(ByteData packet) { PointerDataPacket _unpackPointerDataPacket(ByteData packet) {
const int kStride = Int64List.bytesPerElement; const int kStride = Int64List.bytesPerElement;
@@ -300,6 +300,7 @@ PointerDataPacket _unpackPointerDataPacket(ByteData packet) {
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) {
int offset = i * _kPointerDataFieldCount; int offset = i * _kPointerDataFieldCount;
data.add(PointerData( data.add(PointerData(
embedderId: packet.getInt64(kStride * offset++, _kFakeHostEndian),
timeStamp: Duration(microseconds: packet.getInt64(kStride * offset++, _kFakeHostEndian)), timeStamp: Duration(microseconds: packet.getInt64(kStride * offset++, _kFakeHostEndian)),
change: PointerChange.values[packet.getInt64(kStride * offset++, _kFakeHostEndian)], change: PointerChange.values[packet.getInt64(kStride * offset++, _kFakeHostEndian)],
kind: PointerDeviceKind.values[packet.getInt64(kStride * offset++, _kFakeHostEndian)], kind: PointerDeviceKind.values[packet.getInt64(kStride * offset++, _kFakeHostEndian)],

View File

@@ -72,6 +72,7 @@ enum PointerSignalKind {
class PointerData { class PointerData {
/// Creates an object that represents the state of a pointer. /// Creates an object that represents the state of a pointer.
const PointerData({ const PointerData({
this.embedderId = 0,
this.timeStamp = Duration.zero, this.timeStamp = Duration.zero,
this.change = PointerChange.cancel, this.change = PointerChange.cancel,
this.kind = PointerDeviceKind.touch, this.kind = PointerDeviceKind.touch,
@@ -102,6 +103,13 @@ class PointerData {
this.scrollDeltaY = 0.0, this.scrollDeltaY = 0.0,
}); });
/// Unique identifier that ties the [PointerEvent] to embedder event created it.
///
/// No two pointer events can have the same [embedderId]. This is different from
/// [pointerIdentifier] - used for hit-testing, whereas [embedderId] is used to
/// identify the platform event.
final int embedderId;
/// Time of event dispatch, relative to an arbitrary timeline. /// Time of event dispatch, relative to an arbitrary timeline.
final Duration timeStamp; final Duration timeStamp;
@@ -263,6 +271,7 @@ class PointerData {
/// Returns a complete textual description of the information in this object. /// Returns a complete textual description of the information in this object.
String toStringFull() { String toStringFull() {
return '$runtimeType(' return '$runtimeType('
'embedderId: $embedderId, '
'timeStamp: $timeStamp, ' 'timeStamp: $timeStamp, '
'change: $change, ' 'change: $change, '
'kind: $kind, ' 'kind: $kind, '

View File

@@ -10,7 +10,7 @@
namespace flutter { namespace flutter {
// If this value changes, update the pointer data unpacking code in hooks.dart. // If this value changes, update the pointer data unpacking code in hooks.dart.
static constexpr int kPointerDataFieldCount = 28; static constexpr int kPointerDataFieldCount = 29;
static constexpr int kBytesPerField = sizeof(int64_t); static constexpr int kBytesPerField = sizeof(int64_t);
// Must match the button constants in events.dart. // Must match the button constants in events.dart.
enum PointerButtonMouse : int64_t { enum PointerButtonMouse : int64_t {
@@ -58,6 +58,7 @@ struct alignas(8) PointerData {
kScroll, kScroll,
}; };
int64_t embedder_id;
int64_t time_stamp; int64_t time_stamp;
Change change; Change change;
DeviceKind kind; DeviceKind kind;

View File

@@ -71,6 +71,7 @@ enum PointerSignalKind {
class PointerData { class PointerData {
/// Creates an object that represents the state of a pointer. /// Creates an object that represents the state of a pointer.
const PointerData({ const PointerData({
this.embedderId = 0,
this.timeStamp = Duration.zero, this.timeStamp = Duration.zero,
this.change = PointerChange.cancel, this.change = PointerChange.cancel,
this.kind = PointerDeviceKind.touch, this.kind = PointerDeviceKind.touch,
@@ -101,6 +102,13 @@ class PointerData {
this.scrollDeltaY = 0.0, this.scrollDeltaY = 0.0,
}); });
/// Unique identifier that ties the [PointerEvent] to embedder event created it.
///
/// No two pointer events can have the same [embedderId]. This is different from
/// [pointerIdentifier] - used for hit-testing, whereas [embedderId] is used to
/// identify the platform event.
final int embedderId;
/// Time of event dispatch, relative to an arbitrary timeline. /// Time of event dispatch, relative to an arbitrary timeline.
final Duration timeStamp; final Duration timeStamp;
@@ -257,46 +265,47 @@ class PointerData {
final double scrollDeltaY; final double scrollDeltaY;
@override @override
String toString() => '$runtimeType(x: $physicalX, y: $physicalY)'; String toString() => 'PointerData(x: $physicalX, y: $physicalY)';
/// Returns a complete textual description of the information in this object. /// Returns a complete textual description of the information in this object.
String toStringFull() { String toStringFull() {
return '$runtimeType(' return '$runtimeType('
'timeStamp: $timeStamp, ' 'embedderId: $embedderId, '
'change: $change, ' 'timeStamp: $timeStamp, '
'kind: $kind, ' 'change: $change, '
'signalKind: $signalKind, ' 'kind: $kind, '
'device: $device, ' 'signalKind: $signalKind, '
'pointerIdentifier: $pointerIdentifier, ' 'device: $device, '
'physicalX: $physicalX, ' 'pointerIdentifier: $pointerIdentifier, '
'physicalY: $physicalY, ' 'physicalX: $physicalX, '
'physicalDeltaX: $physicalDeltaX, ' 'physicalY: $physicalY, '
'physicalDeltaY: $physicalDeltaY, ' 'physicalDeltaX: $physicalDeltaX, '
'buttons: $buttons, ' 'physicalDeltaY: $physicalDeltaY, '
'synthesized: $synthesized, ' 'buttons: $buttons, '
'pressure: $pressure, ' 'synthesized: $synthesized, '
'pressureMin: $pressureMin, ' 'pressure: $pressure, '
'pressureMax: $pressureMax, ' 'pressureMin: $pressureMin, '
'distance: $distance, ' 'pressureMax: $pressureMax, '
'distanceMax: $distanceMax, ' 'distance: $distance, '
'size: $size, ' 'distanceMax: $distanceMax, '
'radiusMajor: $radiusMajor, ' 'size: $size, '
'radiusMinor: $radiusMinor, ' 'radiusMajor: $radiusMajor, '
'radiusMin: $radiusMin, ' 'radiusMinor: $radiusMinor, '
'radiusMax: $radiusMax, ' 'radiusMin: $radiusMin, '
'orientation: $orientation, ' 'radiusMax: $radiusMax, '
'tilt: $tilt, ' 'orientation: $orientation, '
'platformData: $platformData, ' 'tilt: $tilt, '
'scrollDeltaX: $scrollDeltaX, ' 'platformData: $platformData, '
'scrollDeltaY: $scrollDeltaY' 'scrollDeltaX: $scrollDeltaX, '
')'; 'scrollDeltaY: $scrollDeltaY'
')';
} }
} }
/// A sequence of reports about the state of pointers. /// A sequence of reports about the state of pointers.
class PointerDataPacket { class PointerDataPacket {
/// Creates a packet of pointer data reports. /// Creates a packet of pointer data reports.
const PointerDataPacket({this.data = const <PointerData>[]}) : assert(data != null); // ignore: unnecessary_null_comparison const PointerDataPacket({ this.data = const <PointerData>[] }) : assert(data != null); // ignore: unnecessary_null_comparison
/// Data about the individual pointers in this packet. /// Data about the individual pointers in this packet.
/// ///

View File

@@ -140,6 +140,7 @@ android_java_sources = [
"io/flutter/embedding/android/FlutterSurfaceView.java", "io/flutter/embedding/android/FlutterSurfaceView.java",
"io/flutter/embedding/android/FlutterTextureView.java", "io/flutter/embedding/android/FlutterTextureView.java",
"io/flutter/embedding/android/FlutterView.java", "io/flutter/embedding/android/FlutterView.java",
"io/flutter/embedding/android/MotionEventTracker.java",
"io/flutter/embedding/android/RenderMode.java", "io/flutter/embedding/android/RenderMode.java",
"io/flutter/embedding/android/SplashScreen.java", "io/flutter/embedding/android/SplashScreen.java",
"io/flutter/embedding/android/SplashScreenProvider.java", "io/flutter/embedding/android/SplashScreenProvider.java",

View File

@@ -57,7 +57,7 @@ public class AndroidTouchProcessor {
} }
// Must match the unpacking code in hooks.dart. // Must match the unpacking code in hooks.dart.
private static final int POINTER_DATA_FIELD_COUNT = 28; private static final int POINTER_DATA_FIELD_COUNT = 29;
private static final int BYTES_PER_FIELD = 8; private static final int BYTES_PER_FIELD = 8;
// This value must match the value in framework's platform_view.dart. // This value must match the value in framework's platform_view.dart.
@@ -65,6 +65,7 @@ public class AndroidTouchProcessor {
private static final int POINTER_DATA_FLAG_BATCHED = 1; private static final int POINTER_DATA_FLAG_BATCHED = 1;
@NonNull private final FlutterRenderer renderer; @NonNull private final FlutterRenderer renderer;
@NonNull private final MotionEventTracker motionEventTracker;
private static final int _POINTER_BUTTON_PRIMARY = 1; private static final int _POINTER_BUTTON_PRIMARY = 1;
@@ -76,6 +77,7 @@ public class AndroidTouchProcessor {
// FlutterRenderer // FlutterRenderer
public AndroidTouchProcessor(@NonNull FlutterRenderer renderer) { public AndroidTouchProcessor(@NonNull FlutterRenderer renderer) {
this.renderer = renderer; this.renderer = renderer;
this.motionEventTracker = MotionEventTracker.getInstance();
} }
/** Sends the given {@link MotionEvent} data to Flutter in a format that Flutter understands. */ /** Sends the given {@link MotionEvent} data to Flutter in a format that Flutter understands. */
@@ -174,6 +176,8 @@ public class AndroidTouchProcessor {
return; return;
} }
MotionEventTracker.MotionEventId motionEventId = motionEventTracker.track(event);
int pointerKind = getPointerDeviceTypeForToolType(event.getToolType(pointerIndex)); int pointerKind = getPointerDeviceTypeForToolType(event.getToolType(pointerIndex));
int signalKind = int signalKind =
@@ -183,6 +187,7 @@ public class AndroidTouchProcessor {
long timeStamp = event.getEventTime() * 1000; // Convert from milliseconds to microseconds. long timeStamp = event.getEventTime() * 1000; // Convert from milliseconds to microseconds.
packet.putLong(motionEventId.getId());
packet.putLong(timeStamp); // time_stamp packet.putLong(timeStamp); // time_stamp
packet.putLong(pointerChange); // change packet.putLong(pointerChange); // change
packet.putLong(pointerKind); // kind packet.putLong(pointerKind); // kind

View File

@@ -0,0 +1,79 @@
package io.flutter.embedding.android;
import android.util.LongSparseArray;
import android.view.MotionEvent;
import androidx.annotation.Nullable;
import java.util.PriorityQueue;
import java.util.concurrent.atomic.AtomicLong;
/** Tracks the motion events received by the FlutterView. */
public final class MotionEventTracker {
/** Represents a unique identifier corresponding to a motion event. */
public static class MotionEventId {
private static final AtomicLong ID_COUNTER = new AtomicLong(0);
private final long id;
private MotionEventId(long id) {
this.id = id;
}
public static MotionEventId from(long id) {
return new MotionEventId(id);
}
public static MotionEventId createUnique() {
return MotionEventId.from(ID_COUNTER.incrementAndGet());
}
public long getId() {
return id;
}
}
private final LongSparseArray<MotionEvent> eventById;
private final PriorityQueue<Long> unusedEvents;
private static MotionEventTracker INSTANCE;
public static MotionEventTracker getInstance() {
if (INSTANCE == null) {
INSTANCE = new MotionEventTracker();
}
return INSTANCE;
}
private MotionEventTracker() {
eventById = new LongSparseArray<>();
unusedEvents = new PriorityQueue<>();
}
/** Tracks the event and returns a unique MotionEventId identifying the event. */
public MotionEventId track(MotionEvent event) {
MotionEventId eventId = MotionEventId.createUnique();
eventById.put(eventId.id, event);
unusedEvents.add(eventId.id);
return eventId;
}
/**
* Returns the MotionEvent corresponding to the eventId while discarding all the motion events
* that occured prior to the event represented by the eventId. Returns null if this event was
* popped or discarded.
*/
@Nullable
public MotionEvent pop(MotionEventId eventId) {
// remove all the older events.
while (!unusedEvents.isEmpty() && unusedEvents.peek() < eventId.id) {
eventById.remove(unusedEvents.poll());
}
// remove the current event from the heap if it exists.
if (!unusedEvents.isEmpty() && unusedEvents.peek() == eventId.id) {
unusedEvents.poll();
}
MotionEvent event = eventById.get(eventId.id);
eventById.remove(eventId.id);
return event;
}
}

View File

@@ -166,7 +166,8 @@ public class PlatformViewsChannel {
(int) args.get(11), (int) args.get(11),
(int) args.get(12), (int) args.get(12),
(int) args.get(13), (int) args.get(13),
(int) args.get(14)); (int) args.get(14),
((Number) args.get(15)).longValue());
try { try {
handler.onTouch(touch); handler.onTouch(touch);
@@ -380,6 +381,8 @@ public class PlatformViewsChannel {
public final int source; public final int source;
/** TODO(mattcarroll): javadoc */ /** TODO(mattcarroll): javadoc */
public final int flags; public final int flags;
/** TODO(iskakaushik): javadoc */
public final long motionEventId;
PlatformViewTouch( PlatformViewTouch(
int viewId, int viewId,
@@ -396,7 +399,8 @@ public class PlatformViewsChannel {
int deviceId, int deviceId,
int edgeFlags, int edgeFlags,
int source, int source,
int flags) { int flags,
long motionEventId) {
this.viewId = viewId; this.viewId = viewId;
this.downTime = downTime; this.downTime = downTime;
this.eventTime = eventTime; this.eventTime = eventTime;
@@ -412,6 +416,7 @@ public class PlatformViewsChannel {
this.edgeFlags = edgeFlags; this.edgeFlags = edgeFlags;
this.source = source; this.source = source;
this.flags = flags; this.flags = flags;
this.motionEventId = motionEventId;
} }
} }
} }

View File

@@ -21,6 +21,7 @@ import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import io.flutter.embedding.android.FlutterImageView; import io.flutter.embedding.android.FlutterImageView;
import io.flutter.embedding.android.FlutterView; import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.android.MotionEventTracker;
import io.flutter.embedding.engine.FlutterOverlaySurface; import io.flutter.embedding.engine.FlutterOverlaySurface;
import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.mutatorsstack.*; import io.flutter.embedding.engine.mutatorsstack.*;
@@ -93,6 +94,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
// Platform view IDs that were displayed since the start of the current frame. // Platform view IDs that were displayed since the start of the current frame.
private HashSet<Integer> currentFrameUsedPlatformViewIds; private HashSet<Integer> currentFrameUsedPlatformViewIds;
// Used to acquire the original motion events using the motionEventIds.
private final MotionEventTracker motionEventTracker;
private final PlatformViewsChannel.PlatformViewsHandler channelHandler = private final PlatformViewsChannel.PlatformViewsHandler channelHandler =
new PlatformViewsChannel.PlatformViewsHandler() { new PlatformViewsChannel.PlatformViewsHandler() {
@@ -301,8 +305,16 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
} }
}; };
private static MotionEvent toMotionEvent( private MotionEvent toMotionEvent(float density, PlatformViewsChannel.PlatformViewTouch touch) {
float density, PlatformViewsChannel.PlatformViewTouch touch) { MotionEventTracker.MotionEventId motionEventId =
MotionEventTracker.MotionEventId.from(touch.motionEventId);
MotionEvent trackedEvent = motionEventTracker.pop(motionEventId);
if (trackedEvent != null) {
return trackedEvent;
}
// TODO (kaushikiska) : warn that we are potentially using an untracked
// event in the platform views.
PointerProperties[] pointerProperties = PointerProperties[] pointerProperties =
parsePointerPropertiesList(touch.rawPointerPropertiesList) parsePointerPropertiesList(touch.rawPointerPropertiesList)
.toArray(new PointerProperties[touch.pointerCount]); .toArray(new PointerProperties[touch.pointerCount]);
@@ -339,6 +351,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
platformViewRequests = new SparseArray<>(); platformViewRequests = new SparseArray<>();
platformViews = new SparseArray<>(); platformViews = new SparseArray<>();
mutatorViews = new SparseArray<>(); mutatorViews = new SparseArray<>();
motionEventTracker = MotionEventTracker.getInstance();
} }
/** /**

View File

@@ -1225,6 +1225,8 @@ FlutterEngineResult FlutterEngineSendPointerEvent(
for (size_t i = 0; i < events_count; ++i) { for (size_t i = 0; i < events_count; ++i) {
flutter::PointerData pointer_data; flutter::PointerData pointer_data;
pointer_data.Clear(); pointer_data.Clear();
// this is currely in use only on android embedding.
pointer_data.embedder_id = 0;
pointer_data.time_stamp = SAFE_ACCESS(current, timestamp, 0); pointer_data.time_stamp = SAFE_ACCESS(current, timestamp, 0);
pointer_data.change = ToPointerDataChange( pointer_data.change = ToPointerDataChange(
SAFE_ACCESS(current, phase, FlutterPointerPhase::kCancel)); SAFE_ACCESS(current, phase, FlutterPointerPhase::kCancel));