Android should use PointerDataPackets (flutter/engine#3092)
We now use JNI rather than Mojo to transport pointer data from Java to Dart. Also, fill in a few more of the pointer data fields from information in Java.
This commit is contained in:
@@ -53,6 +53,10 @@ void _onAppLifecycleStateChanged(int state) {
|
||||
window.onAppLifecycleStateChanged(AppLifecycleState.values[state]);
|
||||
}
|
||||
|
||||
// If this value changes, update the encoding code in the following files:
|
||||
//
|
||||
// * pointer_data.cc
|
||||
// * FlutterView.java
|
||||
const int _kPointerDataFieldCount = 19;
|
||||
|
||||
PointerDataPacket _unpackPointerDataPacket(ByteData packet) {
|
||||
|
||||
@@ -11,6 +11,9 @@ namespace blink {
|
||||
PointerDataPacket::PointerDataPacket(size_t count)
|
||||
: data_(count * sizeof(PointerData)) {}
|
||||
|
||||
PointerDataPacket::PointerDataPacket(char* data, size_t num_bytes)
|
||||
: data_(data, data + num_bytes) {}
|
||||
|
||||
PointerDataPacket::~PointerDataPacket() = default;
|
||||
|
||||
void PointerDataPacket::SetPointerData(size_t i, const PointerData& data) {
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace blink {
|
||||
class PointerDataPacket {
|
||||
public:
|
||||
explicit PointerDataPacket(size_t count);
|
||||
PointerDataPacket(char* data, size_t num_bytes);
|
||||
~PointerDataPacket();
|
||||
|
||||
void SetPointerData(size_t i, const PointerData& data);
|
||||
|
||||
@@ -185,13 +185,19 @@ void Engine::OnLocaleChanged(const mojo::String& language_code,
|
||||
runtime_->SetLocale(language_code_, country_code_);
|
||||
}
|
||||
|
||||
// TODO(abarth): Remove pointer::PointerPacketPtr and route
|
||||
// blink::PointerDataPacket here.
|
||||
void Engine::HandlePointerDataPacket(const PointerDataPacket& packet) {
|
||||
TRACE_EVENT0("flutter", "Engine::HandlePointerDataPacket");
|
||||
if (runtime_)
|
||||
runtime_->HandlePointerDataPacket(packet);
|
||||
}
|
||||
|
||||
// TODO(abarth): Remove pointer::PointerPacketPtr and route PointerDataPacket
|
||||
// here.
|
||||
void Engine::OnPointerPacket(pointer::PointerPacketPtr packetPtr) {
|
||||
TRACE_EVENT0("flutter", "Engine::OnPointerPacket");
|
||||
if (runtime_) {
|
||||
const size_t length = packetPtr->pointers.size();
|
||||
blink::PointerDataPacket packet(length);
|
||||
PointerDataPacket packet(length);
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
const pointer::PointerPtr& pointer = packetPtr->pointers[i];
|
||||
blink::PointerData pointer_data;
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
namespace shell {
|
||||
class Animator;
|
||||
using PointerDataPacket = blink::PointerDataPacket;
|
||||
|
||||
class Engine : public UIDelegate,
|
||||
public sky::SkyEngine,
|
||||
@@ -48,6 +49,8 @@ class Engine : public UIDelegate,
|
||||
|
||||
std::string GetUIIsolateName();
|
||||
|
||||
void HandlePointerDataPacket(const PointerDataPacket& packet);
|
||||
|
||||
private:
|
||||
// UIDelegate implementation:
|
||||
void ConnectToEngine(mojo::InterfaceRequest<SkyEngine> request) override;
|
||||
|
||||
@@ -79,7 +79,6 @@ android_library("java") {
|
||||
"//flutter/services/media:media_lib",
|
||||
"//flutter/services/platform:interfaces_java",
|
||||
"//flutter/services/platform:platform_lib",
|
||||
"//flutter/services/pointer:interfaces_java",
|
||||
"//flutter/services/raw_keyboard:interfaces_java",
|
||||
"//flutter/services/raw_keyboard:raw_keyboard_lib",
|
||||
"//flutter/services/semantics:interfaces_java",
|
||||
|
||||
@@ -44,10 +44,6 @@ import org.chromium.mojo.system.impl.CoreImpl;
|
||||
import org.chromium.mojom.editing.Keyboard;
|
||||
import org.chromium.mojom.flutter.platform.ApplicationMessages;
|
||||
import org.chromium.mojom.mojo.ServiceProvider;
|
||||
import org.chromium.mojom.pointer.Pointer;
|
||||
import org.chromium.mojom.pointer.PointerKind;
|
||||
import org.chromium.mojom.pointer.PointerPacket;
|
||||
import org.chromium.mojom.pointer.PointerType;
|
||||
import org.chromium.mojom.raw_keyboard.RawKeyboardService;
|
||||
import org.chromium.mojom.semantics.SemanticsServer;
|
||||
import org.chromium.mojom.sky.AppLifecycleState;
|
||||
@@ -55,6 +51,8 @@ import org.chromium.mojom.sky.ServicesData;
|
||||
import org.chromium.mojom.sky.SkyEngine;
|
||||
import org.chromium.mojom.sky.ViewportMetrics;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -254,71 +252,117 @@ public class FlutterView extends SurfaceView
|
||||
return mKeyboardState.createInputConnection(outAttrs);
|
||||
}
|
||||
|
||||
private Integer getPointerTypeForAction(int maskedAction) {
|
||||
// Must match the PointerChange enum in pointer.dart.
|
||||
private static final int kPointerChangeAdd = 0;
|
||||
private static final int kPointerChangeRemove = 1;
|
||||
private static final int kPointerChangeDown = 2;
|
||||
private static final int kPointerChangeMove = 3;
|
||||
private static final int kPointerChangeUp = 4;
|
||||
private static final int kPointerChangeCancel = 5;
|
||||
|
||||
// Must match the PointerDeviceKind enum in pointer.dart.
|
||||
private static final int kPointerDeviceKindTouch = 0;
|
||||
private static final int kPointerDeviceKindMouse = 1;
|
||||
private static final int kPointerDeviceKindStylus = 2;
|
||||
private static final int kPointerDeviceKindInvertedStylus = 3;
|
||||
|
||||
private int getPointerChangeForAction(int maskedAction) {
|
||||
// Primary pointer:
|
||||
if (maskedAction == MotionEvent.ACTION_DOWN) {
|
||||
return PointerType.DOWN;
|
||||
return kPointerChangeDown;
|
||||
}
|
||||
if (maskedAction == MotionEvent.ACTION_UP) {
|
||||
return PointerType.UP;
|
||||
return kPointerChangeUp;
|
||||
}
|
||||
// Secondary pointer:
|
||||
if (maskedAction == MotionEvent.ACTION_POINTER_DOWN) {
|
||||
return PointerType.DOWN;
|
||||
return kPointerChangeDown;
|
||||
}
|
||||
if (maskedAction == MotionEvent.ACTION_POINTER_UP) {
|
||||
return PointerType.UP;
|
||||
return kPointerChangeUp;
|
||||
}
|
||||
// All pointers:
|
||||
if (maskedAction == MotionEvent.ACTION_MOVE) {
|
||||
return PointerType.MOVE;
|
||||
return kPointerChangeMove;
|
||||
}
|
||||
if (maskedAction == MotionEvent.ACTION_CANCEL) {
|
||||
return PointerType.CANCEL;
|
||||
return kPointerChangeCancel;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int getPointerDeviceTypeForToolType(int toolType) {
|
||||
switch (toolType) {
|
||||
case MotionEvent.TOOL_TYPE_FINGER:
|
||||
return kPointerDeviceKindTouch;
|
||||
case MotionEvent.TOOL_TYPE_STYLUS:
|
||||
return kPointerDeviceKindStylus;
|
||||
case MotionEvent.TOOL_TYPE_MOUSE:
|
||||
return kPointerDeviceKindMouse;
|
||||
default:
|
||||
// MotionEvent.TOOL_TYPE_UNKNOWN will reach here.
|
||||
return -1;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addPointerForIndex(MotionEvent event, int pointerIndex,
|
||||
List<Pointer> result) {
|
||||
Integer pointerType = getPointerTypeForAction(event.getActionMasked());
|
||||
if (pointerType == null) {
|
||||
ByteBuffer packet) {
|
||||
int pointerChange = getPointerChangeForAction(event.getActionMasked());
|
||||
if (pointerChange == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
Pointer pointer = new Pointer();
|
||||
int pointerKind = event.getToolType(pointerIndex);
|
||||
if (pointerKind == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
pointer.timeStamp = event.getEventTime() * 1000; // Convert from milliseconds to microseconds.
|
||||
pointer.pointer = event.getPointerId(pointerIndex);
|
||||
pointer.type = pointerType;
|
||||
pointer.kind = PointerKind.TOUCH;
|
||||
pointer.x = event.getX(pointerIndex);
|
||||
pointer.y = event.getY(pointerIndex);
|
||||
long timeStamp = event.getEventTime() * 1000; // Convert from milliseconds to microseconds.
|
||||
|
||||
pointer.buttons = 0;
|
||||
pointer.down = false;
|
||||
pointer.primary = false;
|
||||
pointer.obscured = false;
|
||||
packet.putLong(timeStamp); // time_stamp
|
||||
packet.putLong(event.getPointerId(pointerIndex)); // pointer
|
||||
packet.putLong(pointerChange); // change
|
||||
packet.putLong(pointerKind); // kind
|
||||
packet.putDouble(event.getX(pointerIndex)); // physical_x
|
||||
packet.putDouble(event.getY(pointerIndex)); // physical_y
|
||||
|
||||
if (pointerKind == kPointerDeviceKindMouse) {
|
||||
packet.putLong(event.getButtonState() & 0x1F); // buttons
|
||||
} else if (pointerKind == kPointerDeviceKindStylus) {
|
||||
packet.putLong((event.getButtonState() >> 4) & 0xF); // buttons
|
||||
} else {
|
||||
packet.putLong(0); // buttons
|
||||
}
|
||||
|
||||
packet.putLong(0); // obscured
|
||||
|
||||
// TODO(eseidel): Could get the calibrated range if necessary:
|
||||
// event.getDevice().getMotionRange(MotionEvent.AXIS_PRESSURE)
|
||||
pointer.pressure = event.getPressure(pointerIndex);
|
||||
pointer.pressureMin = 0.0f;
|
||||
pointer.pressureMax = 1.0f;
|
||||
packet.putDouble(event.getPressure(pointerIndex)); // presure
|
||||
packet.putDouble(0.0); // pressure_min
|
||||
packet.putDouble(1.0); // pressure_max
|
||||
|
||||
pointer.distance = 0.0f;
|
||||
pointer.distanceMin = 0.0f;
|
||||
pointer.distanceMax = 0.0f;
|
||||
if (pointerKind == kPointerDeviceKindStylus) {
|
||||
packet.putDouble(event.getAxisValue(MotionEvent.AXIS_DISTANCE, pointerIndex)); // distance
|
||||
packet.putDouble(0.0); // distance_max
|
||||
} else {
|
||||
packet.putDouble(0.0); // distance
|
||||
packet.putDouble(0.0); // distance_max
|
||||
}
|
||||
|
||||
pointer.radiusMajor = 0.0f;
|
||||
pointer.radiusMinor = 0.0f;
|
||||
pointer.radiusMin = 0.0f;
|
||||
pointer.radiusMax = 0.0f;
|
||||
packet.putDouble(event.getToolMajor(pointerIndex)); // radius_major
|
||||
packet.putDouble(event.getToolMinor(pointerIndex)); // radius_minor
|
||||
|
||||
pointer.orientation = 0.0f;
|
||||
pointer.tilt = 0.0f;
|
||||
packet.putDouble(0.0); // radius_min
|
||||
packet.putDouble(0.0); // radius_max
|
||||
|
||||
result.add(pointer);
|
||||
packet.putDouble(event.getAxisValue(MotionEvent.AXIS_ORIENTATION, pointerIndex)); // orientation
|
||||
|
||||
if (pointerKind == kPointerDeviceKindStylus) {
|
||||
packet.putDouble(event.getAxisValue(MotionEvent.AXIS_TILT, pointerIndex)); // tilt
|
||||
} else {
|
||||
packet.putDouble(0.0); // tilt
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -332,10 +376,15 @@ public class FlutterView extends SurfaceView
|
||||
requestUnbufferedDispatch(event);
|
||||
}
|
||||
|
||||
ArrayList<Pointer> pointers = new ArrayList<Pointer>();
|
||||
// These values must match the unpacking code in hooks.dart.
|
||||
final int kPointerDataFieldCount = 19;
|
||||
final int kBytePerField = 8;
|
||||
|
||||
int pointerCount = event.getPointerCount();
|
||||
|
||||
ByteBuffer packet = ByteBuffer.allocateDirect(pointerCount * kPointerDataFieldCount * kBytePerField);
|
||||
packet.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
// TODO(abarth): Rather than unpacking these events here, we should
|
||||
// probably send them in one packet to the engine.
|
||||
int maskedAction = event.getActionMasked();
|
||||
// ACTION_UP, ACTION_POINTER_UP, ACTION_DOWN, and ACTION_POINTER_DOWN
|
||||
// only apply to a single pointer, other events apply to all pointers.
|
||||
@@ -343,20 +392,18 @@ public class FlutterView extends SurfaceView
|
||||
|| maskedAction == MotionEvent.ACTION_POINTER_UP
|
||||
|| maskedAction == MotionEvent.ACTION_DOWN
|
||||
|| maskedAction == MotionEvent.ACTION_POINTER_DOWN) {
|
||||
addPointerForIndex(event, event.getActionIndex(), pointers);
|
||||
addPointerForIndex(event, event.getActionIndex(), packet);
|
||||
} else {
|
||||
// ACTION_MOVE may not actually mean all pointers have moved
|
||||
// but it's the responsibility of a later part of the system to
|
||||
// ignore 0-deltas if desired.
|
||||
for (int p = 0; p < event.getPointerCount(); p++) {
|
||||
addPointerForIndex(event, p, pointers);
|
||||
for (int p = 0; p < pointerCount; p++) {
|
||||
addPointerForIndex(event, p, packet);
|
||||
}
|
||||
}
|
||||
|
||||
PointerPacket packet = new PointerPacket();
|
||||
packet.pointers = pointers.toArray(new Pointer[0]);
|
||||
mSkyEngine.onPointerPacket(packet);
|
||||
|
||||
assert packet.position() % (kPointerDataFieldCount * kBytePerField) == 0;
|
||||
nativeDispatchPointerDataPacket(mNativePlatformView, packet, packet.position());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -528,6 +575,8 @@ public class FlutterView extends SurfaceView
|
||||
private static native void nativeSurfaceDestroyed(long nativePlatformViewAndroid);
|
||||
private static native Bitmap nativeGetBitmap(long nativePlatformViewAndroid);
|
||||
|
||||
private static native void nativeDispatchPointerDataPacket(long nativePlatformViewAndroid, ByteBuffer buffer, int position);
|
||||
|
||||
|
||||
// ACCESSIBILITY
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "base/android/jni_android.h"
|
||||
@@ -20,8 +21,10 @@
|
||||
#include "flutter/common/threads.h"
|
||||
#include "flutter/flow/compositor_context.h"
|
||||
#include "flutter/runtime/dart_service_isolate.h"
|
||||
#include "flutter/shell/common/engine.h"
|
||||
#include "flutter/shell/common/shell.h"
|
||||
#include "jni/FlutterView_jni.h"
|
||||
#include "lib/ftl/functional/wrap_lambda.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
|
||||
namespace shell {
|
||||
@@ -422,6 +425,21 @@ void PlatformViewAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) {
|
||||
ReleaseSurface();
|
||||
}
|
||||
|
||||
void PlatformViewAndroid::DispatchPointerDataPacket(JNIEnv* env,
|
||||
jobject obj,
|
||||
jobject buffer,
|
||||
jint position) {
|
||||
char* data = static_cast<char*>(env->GetDirectBufferAddress(buffer));
|
||||
|
||||
blink::Threads::UI()->PostTask(ftl::WrapLambda([
|
||||
engine = engine_->GetWeakPtr(),
|
||||
packet = std::make_unique<PointerDataPacket>(data, position)
|
||||
] {
|
||||
if (engine.get())
|
||||
engine->HandlePointerDataPacket(*packet);
|
||||
}));
|
||||
}
|
||||
|
||||
void PlatformViewAndroid::ReleaseSurface() {
|
||||
if (context_) {
|
||||
NotifyDestroyed();
|
||||
|
||||
@@ -33,6 +33,9 @@ class PlatformViewAndroid : public PlatformView {
|
||||
|
||||
void SurfaceDestroyed(JNIEnv* env, jobject obj);
|
||||
|
||||
void DispatchPointerDataPacket(JNIEnv* env, jobject obj, jobject buffer,
|
||||
jint position);
|
||||
|
||||
base::android::ScopedJavaLocalRef<jobject> GetBitmap(JNIEnv* env,
|
||||
jobject obj);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user