Skip to content

Commit 21309e7

Browse files
Arpit SinghAndroid (Google) Code Review
authored andcommitted
Merge "Refactor to replace FloatPoint by vec2" into main
2 parents 4e53376 + 7f21fa3 commit 21309e7

8 files changed

Lines changed: 81 additions & 103 deletions

services/inputflinger/PointerChoreographer.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ bool isMouseOrTouchpad(uint32_t sources) {
6464
!isFromSource(sources, AINPUT_SOURCE_STYLUS));
6565
}
6666

67-
inline void notifyPointerDisplayChange(
68-
std::optional<std::tuple<ui::LogicalDisplayId, FloatPoint>> change,
69-
PointerChoreographerPolicyInterface& policy) {
67+
inline void notifyPointerDisplayChange(std::optional<std::tuple<ui::LogicalDisplayId, vec2>> change,
68+
PointerChoreographerPolicyInterface& policy) {
7069
if (!change) {
7170
return;
7271
}
@@ -245,9 +244,9 @@ NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotio
245244
if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) {
246245
// This is an absolute mouse device that knows about the location of the cursor on the
247246
// display, so set the cursor position to the specified location.
248-
const auto [x, y] = pc.getPosition();
249-
const float deltaX = args.xCursorPosition - x;
250-
const float deltaY = args.yCursorPosition - y;
247+
const auto position = pc.getPosition();
248+
const float deltaX = args.xCursorPosition - position.x;
249+
const float deltaY = args.yCursorPosition - position.y;
251250
newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
252251
newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
253252
pc.setPosition(args.xCursorPosition, args.yCursorPosition);
@@ -273,15 +272,15 @@ NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMo
273272
processPointerDeviceMotionEventLocked(/*byref*/ newArgs, /*byref*/ pc);
274273
} else {
275274
// This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
276-
const auto [x, y] = pc.getPosition();
275+
const auto position = pc.getPosition();
277276
for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
278277
newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
279-
args.pointerCoords[i].getX() + x);
278+
args.pointerCoords[i].getX() + position.x);
280279
newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
281-
args.pointerCoords[i].getY() + y);
280+
args.pointerCoords[i].getY() + position.y);
282281
}
283-
newArgs.xCursorPosition = x;
284-
newArgs.yCursorPosition = y;
282+
newArgs.xCursorPosition = position.x;
283+
newArgs.yCursorPosition = position.y;
285284
}
286285

287286
// Note displayId may have changed if the cursor moved to a different display
@@ -296,33 +295,31 @@ void PointerChoreographer::processPointerDeviceMotionEventLocked(NotifyMotionArg
296295
const float deltaX = newArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
297296
const float deltaY = newArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
298297

299-
FloatPoint unconsumedDelta = pc.move(deltaX, deltaY);
298+
vec2 unconsumedDelta = pc.move(deltaX, deltaY);
300299
if (com::android::input::flags::connected_displays_cursor() &&
301300
(std::abs(unconsumedDelta.x) > 0 || std::abs(unconsumedDelta.y) > 0)) {
302301
handleUnconsumedDeltaLocked(pc, unconsumedDelta);
303302
// pointer may have moved to a different viewport
304303
newArgs.displayId = pc.getDisplayId();
305304
}
306305

307-
const auto [x, y] = pc.getPosition();
308-
newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
309-
newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
310-
newArgs.xCursorPosition = x;
311-
newArgs.yCursorPosition = y;
306+
const auto position = pc.getPosition();
307+
newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, position.x);
308+
newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, position.y);
309+
newArgs.xCursorPosition = position.x;
310+
newArgs.yCursorPosition = position.y;
312311
}
313312

314313
void PointerChoreographer::handleUnconsumedDeltaLocked(PointerControllerInterface& pc,
315-
const FloatPoint& unconsumedDelta) {
314+
const vec2& unconsumedDelta) {
316315
// Display topology is in rotated coordinate space and Pointer controller returns and expects
317316
// values in the un-rotated coordinate space. So we need to transform delta and cursor position
318317
// back to the rotated coordinate space to lookup adjacent display in the display topology.
319318
const auto& sourceDisplayTransform = pc.getDisplayTransform();
320319
const vec2 rotatedUnconsumedDelta =
321-
transformWithoutTranslation(sourceDisplayTransform,
322-
{unconsumedDelta.x, unconsumedDelta.y});
323-
const FloatPoint cursorPosition = pc.getPosition();
324-
const vec2 rotatedCursorPosition =
325-
sourceDisplayTransform.transform(cursorPosition.x, cursorPosition.y);
320+
transformWithoutTranslation(sourceDisplayTransform, unconsumedDelta);
321+
const vec2 cursorPosition = pc.getPosition();
322+
const vec2 rotatedCursorPosition = sourceDisplayTransform.transform(cursorPosition);
326323

327324
// To find out the boundary that cursor is crossing we are checking delta in x and y direction
328325
// respectively. This prioritizes x direction over y.
@@ -769,7 +766,7 @@ PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerCo
769766
PointerChoreographer::PointerDisplayChange
770767
PointerChoreographer::calculatePointerDisplayChangeToNotify() {
771768
ui::LogicalDisplayId displayIdToNotify = ui::LogicalDisplayId::INVALID;
772-
FloatPoint cursorPosition = {0, 0};
769+
vec2 cursorPosition = {0, 0};
773770
if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
774771
it != mMousePointersByDisplay.end()) {
775772
const auto& pointerController = it->second;
@@ -840,7 +837,7 @@ std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice
840837
return std::nullopt;
841838
}
842839

843-
FloatPoint PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
840+
vec2 PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
844841
std::scoped_lock _l(getLock());
845842
const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
846843
if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);

services/inputflinger/PointerChoreographer.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PointerChoreographerInterface : public InputListenerInterface {
5959
virtual void setDisplayViewports(const std::vector<DisplayViewport>& viewports) = 0;
6060
virtual std::optional<DisplayViewport> getViewportForPointerDevice(
6161
ui::LogicalDisplayId associatedDisplayId = ui::LogicalDisplayId::INVALID) = 0;
62-
virtual FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId) = 0;
62+
virtual vec2 getMouseCursorPosition(ui::LogicalDisplayId displayId) = 0;
6363
virtual void setShowTouchesEnabled(bool enabled) = 0;
6464
virtual void setStylusPointerIconEnabled(bool enabled) = 0;
6565
/**
@@ -96,7 +96,7 @@ class PointerChoreographer : public PointerChoreographerInterface {
9696
void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override;
9797
std::optional<DisplayViewport> getViewportForPointerDevice(
9898
ui::LogicalDisplayId associatedDisplayId) override;
99-
FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId) override;
99+
vec2 getMouseCursorPosition(ui::LogicalDisplayId displayId) override;
100100
void setShowTouchesEnabled(bool enabled) override;
101101
void setStylusPointerIconEnabled(bool enabled) override;
102102
bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
@@ -134,8 +134,8 @@ class PointerChoreographer : public PointerChoreographerInterface {
134134
void dump(std::string& dump) override;
135135

136136
private:
137-
using PointerDisplayChange = std::optional<
138-
std::tuple<ui::LogicalDisplayId /*displayId*/, FloatPoint /*cursorPosition*/>>;
137+
using PointerDisplayChange =
138+
std::optional<std::tuple<ui::LogicalDisplayId /*displayId*/, vec2 /*cursorPosition*/>>;
139139

140140
// PointerChoreographer's DisplayInfoListener can outlive the PointerChoreographer because when
141141
// the listener is registered and called from display thread, a strong pointer to the listener
@@ -171,8 +171,8 @@ class PointerChoreographer : public PointerChoreographerInterface {
171171
const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays)
172172
REQUIRES(getLock());
173173

174-
void handleUnconsumedDeltaLocked(PointerControllerInterface& pc,
175-
const FloatPoint& unconsumedDelta) REQUIRES(getLock());
174+
void handleUnconsumedDeltaLocked(PointerControllerInterface& pc, const vec2& unconsumedDelta)
175+
REQUIRES(getLock());
176176

177177
void populateFakeDisplayTopologyLocked(const std::vector<gui::DisplayInfo>& displayInfos)
178178
REQUIRES(getLock());

services/inputflinger/include/PointerChoreographerPolicyInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PointerChoreographerPolicyInterface {
5454
* @param position The new position of the mouse cursor on the logical display
5555
*/
5656
virtual void notifyPointerDisplayIdChanged(ui::LogicalDisplayId displayId,
57-
const FloatPoint& position) = 0;
57+
const vec2& position) = 0;
5858

5959
/* Returns true if any InputConnection is currently active. */
6060
virtual bool isInputMethodConnectionActive() = 0;

services/inputflinger/include/PointerControllerInterface.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ namespace android {
2424

2525
struct SpriteIcon;
2626

27-
struct FloatPoint {
28-
float x;
29-
float y;
30-
31-
inline FloatPoint(float x, float y) : x(x), y(y) {}
32-
33-
inline explicit FloatPoint(vec2 p) : x(p.x), y(p.y) {}
34-
35-
template <typename T, typename U>
36-
operator std::tuple<T, U>() {
37-
return {x, y};
38-
}
39-
};
40-
4127
/**
4228
* Interface for tracking a mouse / touch pad pointer and touch pad spots.
4329
*
@@ -77,13 +63,13 @@ class PointerControllerInterface {
7763
*
7864
* Return value may be used to move pointer to corresponding adjacent display, if it exists in
7965
* the display-topology */
80-
[[nodiscard]] virtual FloatPoint move(float deltaX, float deltaY) = 0;
66+
[[nodiscard]] virtual vec2 move(float deltaX, float deltaY) = 0;
8167

8268
/* Sets the absolute location of the pointer. */
8369
virtual void setPosition(float x, float y) = 0;
8470

8571
/* Gets the absolute location of the pointer. */
86-
virtual FloatPoint getPosition() const = 0;
72+
virtual vec2 getPosition() const = 0;
8773

8874
enum class Transition {
8975
// Fade/unfade immediately.

services/inputflinger/tests/FakePointerController.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void FakePointerController::setPosition(float x, float y) {
4343
mY = y;
4444
}
4545

46-
FloatPoint FakePointerController::getPosition() const {
46+
vec2 FakePointerController::getPosition() const {
4747
if (!mEnabled) {
4848
return {0, 0};
4949
}
@@ -96,9 +96,9 @@ void FakePointerController::assertViewportNotSet() {
9696
}
9797

9898
void FakePointerController::assertPosition(float x, float y) {
99-
const auto [actualX, actualY] = getPosition();
100-
ASSERT_NEAR(x, actualX, 1);
101-
ASSERT_NEAR(y, actualY, 1);
99+
const auto actual = getPosition();
100+
ASSERT_NEAR(x, actual.x, 1);
101+
ASSERT_NEAR(y, actual.y, 1);
102102
}
103103

104104
void FakePointerController::assertSpotCount(ui::LogicalDisplayId displayId, int32_t count) {
@@ -148,13 +148,13 @@ bool FakePointerController::isPointerShown() {
148148
return mIsPointerShown;
149149
}
150150

151-
FloatPoint FakePointerController::move(float deltaX, float deltaY) {
151+
vec2 FakePointerController::move(float deltaX, float deltaY) {
152152
if (!mEnabled) return {0, 0};
153153

154154
mX += deltaX;
155155
mY += deltaY;
156156

157-
const FloatPoint position(mX, mY);
157+
const vec2 position(mX, mY);
158158

159159
if (mX < mMinX) mX = mMinX;
160160
if (mX > mMaxX) mX = mMaxX;

services/inputflinger/tests/FakePointerController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FakePointerController : public PointerControllerInterface {
4040
const std::map<ui::LogicalDisplayId, std::vector<int32_t>>& getSpots();
4141

4242
void setPosition(float x, float y) override;
43-
FloatPoint getPosition() const override;
43+
vec2 getPosition() const override;
4444
ui::LogicalDisplayId getDisplayId() const override;
4545
void setDisplayViewport(const DisplayViewport& viewport) override;
4646
void updatePointerIcon(PointerIconStyle iconId) override;
@@ -66,7 +66,7 @@ class FakePointerController : public PointerControllerInterface {
6666

6767
private:
6868
std::string dump() override { return ""; }
69-
FloatPoint move(float deltaX, float deltaY) override;
69+
vec2 move(float deltaX, float deltaY) override;
7070
void unfade(Transition) override;
7171
void setPresentation(Presentation) override {}
7272
void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,

services/inputflinger/tests/InterfaceMocks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class MockPointerChoreographerPolicyInterface : public PointerChoreographerPolic
188188
MOCK_METHOD(std::shared_ptr<PointerControllerInterface>, createPointerController,
189189
(PointerControllerInterface::ControllerType), (override));
190190
MOCK_METHOD(void, notifyPointerDisplayIdChanged,
191-
(ui::LogicalDisplayId displayId, const FloatPoint& position), (override));
191+
(ui::LogicalDisplayId displayId, const vec2& position), (override));
192192
MOCK_METHOD(bool, isInputMethodConnectionActive, (), (override));
193193
MOCK_METHOD(void, notifyMouseCursorFadedOnTyping, (), (override));
194194
};

0 commit comments

Comments
 (0)