Get mouse cursor position from PointerController instead of WM

PointerController is the source of truth of the mouse cursor position.
When the VirtualDevice APIs queried the cursor position, we were
previously getting the position from WM. WM's values only update once
system_server's global monitor has processed the input event, which
leads us into a race condition.

Remove the race consition by querying the mouse cursor position directly
from PointerController.

Bug: 266687189
Test: atest VirtualMouseTest
Change-Id: Id0e0e20a0d547f969d3a27d43fdfdca34d0fa7c0
This commit is contained in:
Prabir Pradhan
2023-02-24 01:52:13 +00:00
parent 07937a009a
commit 692bbdb3a8
7 changed files with 70 additions and 32 deletions

View File

@@ -262,19 +262,34 @@ void PointerController::reloadPointerResources() {
}
void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
std::scoped_lock lock(getLock());
struct PointerDisplayChangeArgs {
int32_t displayId;
float x, y;
};
std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
bool getAdditionalMouseResources = false;
if (mLocked.presentation == PointerController::Presentation::POINTER ||
mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
getAdditionalMouseResources = true;
}
mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
if (viewport.displayId != mLocked.pointerDisplayId) {
float xPos, yPos;
mCursorController.getPosition(&xPos, &yPos);
mContext.getPolicy()->onPointerDisplayIdChanged(viewport.displayId, xPos, yPos);
mLocked.pointerDisplayId = viewport.displayId;
{ // acquire lock
std::scoped_lock lock(getLock());
bool getAdditionalMouseResources = false;
if (mLocked.presentation == PointerController::Presentation::POINTER ||
mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
getAdditionalMouseResources = true;
}
mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
if (viewport.displayId != mLocked.pointerDisplayId) {
float xPos, yPos;
mCursorController.getPosition(&xPos, &yPos);
mLocked.pointerDisplayId = viewport.displayId;
pointerDisplayChanged = {viewport.displayId, xPos, yPos};
}
} // release lock
if (pointerDisplayChanged) {
// Notify the policy without holding the pointer controller lock.
mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
pointerDisplayChanged->x,
pointerDisplayChanged->y);
}
}

View File

@@ -96,7 +96,11 @@ public abstract class InputManagerInternal {
*/
public abstract int getVirtualMousePointerDisplayId();
/** Gets the current position of the mouse cursor. */
/**
* Gets the current position of the mouse cursor.
*
* Returns NaN-s as the coordinates if the cursor is not available.
*/
public abstract PointF getCursorPosition();
/**

View File

@@ -2863,9 +2863,6 @@ public class InputManagerService extends IInputManager.Stub
int getPointerDisplayId();
/** Gets the x and y coordinates of the cursor's current position. */
PointF getCursorPosition();
/**
* Notifies window manager that a {@link android.view.MotionEvent#ACTION_DOWN} pointer event
* occurred on a window that did not have focus.
@@ -3189,7 +3186,11 @@ public class InputManagerService extends IInputManager.Stub
@Override
public PointF getCursorPosition() {
return mWindowManagerCallbacks.getCursorPosition();
final float[] p = mNative.getMouseCursorPosition();
if (p == null || p.length != 2) {
throw new IllegalStateException("Failed to get mouse cursor position");
}
return new PointF(p[0], p[1]);
}
@Override

View File

@@ -224,6 +224,16 @@ interface NativeInputManagerService {
/** Set whether stylus button reporting through motion events should be enabled. */
void setStylusButtonMotionEventsEnabled(boolean enabled);
/**
* Get the current position of the mouse cursor.
*
* If the mouse cursor is not currently shown, the coordinate values will be NaN-s.
*
* NOTE: This will grab the PointerController's lock, so we must be careful about calling this
* from the InputReader or Display threads, which may result in a deadlock.
*/
float[] getMouseCursorPosition();
/** The native implementation of InputManagerService methods. */
class NativeImpl implements NativeInputManagerService {
/** Pointer to native input manager service object, used by native code. */
@@ -465,5 +475,8 @@ interface NativeInputManagerService {
@Override
public native void setStylusButtonMotionEventsEnabled(boolean enabled);
@Override
public native float[] getMouseCursorPosition();
}
}

View File

@@ -25,7 +25,6 @@ import static com.android.server.wm.WindowManagerService.H.ON_POINTER_DOWN_OUTSI
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.PointF;
import android.os.Debug;
import android.os.IBinder;
import android.util.Slog;
@@ -220,11 +219,6 @@ final class InputManagerCallback implements InputManagerService.WindowManagerCal
}
}
@Override
public PointF getCursorPosition() {
return mService.getLatestMousePosition();
}
@Override
public void onPointerDownOutsideFocus(IBinder touchedToken) {
mService.mH.obtainMessage(ON_POINTER_DOWN_OUTSIDE_FOCUS, touchedToken).sendToTarget();

View File

@@ -186,7 +186,6 @@ import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Region;
import android.hardware.configstore.V1_0.OptionalBool;
@@ -7361,14 +7360,6 @@ public class WindowManagerService extends IWindowManager.Stub
.setPointerIconType(PointerIcon.TYPE_DEFAULT);
}
}
PointF getLatestMousePosition() {
synchronized (mMousePositionTracker) {
return new PointF(mMousePositionTracker.mLatestMouseX,
mMousePositionTracker.mLatestMouseY);
}
}
void setMousePointerDisplayId(int displayId) {
mMousePositionTracker.setPointerDisplayId(displayId);
}

View File

@@ -308,6 +308,7 @@ public:
void setMotionClassifierEnabled(bool enabled);
std::optional<std::string> getBluetoothAddress(int32_t deviceId);
void setStylusButtonMotionEventsEnabled(bool enabled);
std::array<float, 2> getMouseCursorPosition();
/* --- InputReaderPolicyInterface implementation --- */
@@ -1655,6 +1656,16 @@ bool NativeInputManager::isPerDisplayTouchModeEnabled() {
return static_cast<bool>(enabled);
}
std::array<float, 2> NativeInputManager::getMouseCursorPosition() {
AutoMutex _l(mLock);
const auto pc = mLocked.pointerController.lock();
if (!pc) return {{std::nanf(""), std::nanf("")}};
std::array<float, 2> p{};
pc->getPosition(&p[0], &p[1]);
return p;
}
// ----------------------------------------------------------------------------
static NativeInputManager* getNativeInputManager(JNIEnv* env, jobject clazz) {
@@ -2547,6 +2558,14 @@ static void nativeSetStylusButtonMotionEventsEnabled(JNIEnv* env, jobject native
im->setStylusButtonMotionEventsEnabled(enabled);
}
static jfloatArray nativeGetMouseCursorPosition(JNIEnv* env, jobject nativeImplObj) {
NativeInputManager* im = getNativeInputManager(env, nativeImplObj);
const std::array<float, 2> p = im->getMouseCursorPosition();
jfloatArray outArr = env->NewFloatArray(2);
env->SetFloatArrayRegion(outArr, 0, p.size(), p.data());
return outArr;
}
// ----------------------------------------------------------------------------
static const JNINativeMethod gInputManagerMethods[] = {
@@ -2640,6 +2659,7 @@ static const JNINativeMethod gInputManagerMethods[] = {
{"getBluetoothAddress", "(I)Ljava/lang/String;", (void*)nativeGetBluetoothAddress},
{"setStylusButtonMotionEventsEnabled", "(Z)V",
(void*)nativeSetStylusButtonMotionEventsEnabled},
{"getMouseCursorPosition", "()[F", (void*)nativeGetMouseCursorPosition},
};
#define FIND_CLASS(var, className) \