blob: bba9c9764eee91cde14d0cea214320de11377bcb [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "PointerController"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080018//#define LOG_NDEBUG 0
19
Prabir Pradhan02b05452021-11-17 21:48:11 +000020#include "PointerController.h"
Prabir Pradhan02b05452021-11-17 21:48:11 +000021
Liam Harringtonc782be62020-07-17 19:48:24 +000022#include <SkBlendMode.h>
23#include <SkCanvas.h>
24#include <SkColor.h>
Michael Wright72a89132022-10-22 03:16:31 +010025#include <android-base/stringprintf.h>
Prabir Pradhan5693cee2021-12-31 06:51:15 -080026#include <android-base/thread_annotations.h>
Byoungho Jung6a2ce942023-10-07 16:19:19 +090027#include <com_android_input_flags.h>
Michael Wright72a89132022-10-22 03:16:31 +010028#include <ftl/enum.h>
29
30#include <mutex>
Prabir Pradhan5693cee2021-12-31 06:51:15 -080031
32#include "PointerControllerContext.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033
Michael Wright72a89132022-10-22 03:16:31 +010034#define INDENT " "
35#define INDENT2 " "
Michael Wright83577752022-10-28 16:24:33 +010036#define INDENT3 " "
Michael Wright72a89132022-10-22 03:16:31 +010037
Byoungho Jung6a2ce942023-10-07 16:19:19 +090038namespace input_flags = com::android::input::flags;
39
Jeff Brownb4ff35d2011-01-02 16:37:43 -080040namespace android {
41
Prabir Pradhanf97fac32021-11-18 16:40:34 +000042namespace {
43
44const ui::Transform kIdentityTransform;
45
46} // namespace
47
48// --- PointerController::DisplayInfoListener ---
49
50void PointerController::DisplayInfoListener::onWindowInfosChanged(
Patrick Williams8e47a672023-05-01 11:30:37 -050051 const gui::WindowInfosUpdate& update) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -080052 std::scoped_lock lock(mLock);
53 if (mPointerController == nullptr) return;
54
55 // PointerController uses DisplayInfoListener's lock.
56 base::ScopedLockAssertion assumeLocked(mPointerController->getLock());
Patrick Williams8e47a672023-05-01 11:30:37 -050057 mPointerController->onDisplayInfosChangedLocked(update.displayInfos);
Prabir Pradhan5693cee2021-12-31 06:51:15 -080058}
59
60void PointerController::DisplayInfoListener::onPointerControllerDestroyed() {
61 std::scoped_lock lock(mLock);
62 mPointerController = nullptr;
Prabir Pradhanf97fac32021-11-18 16:40:34 +000063}
64
Jeff Brownb4ff35d2011-01-02 16:37:43 -080065// --- PointerController ---
66
Michael Wrighta0bc6b12020-06-26 20:25:34 +010067std::shared_ptr<PointerController> PointerController::create(
68 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Byoungho Jung6a2ce942023-10-07 16:19:19 +090069 SpriteController& spriteController, bool enabled, ControllerType type) {
Liam Harringtonc782be62020-07-17 19:48:24 +000070 // using 'new' to access non-public constructor
Byoungho Jung6a2ce942023-10-07 16:19:19 +090071 std::shared_ptr<PointerController> controller;
72 switch (type) {
73 case ControllerType::MOUSE:
74 controller = std::shared_ptr<PointerController>(
75 new MousePointerController(policy, looper, spriteController, enabled));
76 break;
Byoungho Jung51282e82023-10-27 18:15:41 +090077 case ControllerType::TOUCH:
78 controller = std::shared_ptr<PointerController>(
79 new TouchPointerController(policy, looper, spriteController, enabled));
80 break;
Byoungho Jung08daa252023-10-27 20:55:18 +090081 case ControllerType::STYLUS:
82 controller = std::shared_ptr<PointerController>(
83 new StylusPointerController(policy, looper, spriteController, enabled));
84 break;
Byoungho Jung6a2ce942023-10-07 16:19:19 +090085 case ControllerType::LEGACY:
86 default:
87 controller = std::shared_ptr<PointerController>(
88 new PointerController(policy, looper, spriteController, enabled));
89 break;
90 }
Jeff Brown2352b972011-04-12 22:39:53 -070091
Michael Wrighta0bc6b12020-06-26 20:25:34 +010092 /*
93 * Now we need to hook up the constructed PointerController object to its callbacks.
94 *
95 * This must be executed after the constructor but before any other methods on PointerController
96 * in order to ensure that the fully constructed object is visible on the Looper thread, since
97 * that may be a different thread than where the PointerController is initially constructed.
98 *
99 * Unfortunately, this cannot be done as part of the constructor since we need to hand out
100 * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
101 */
Jeff Brown5541de92011-04-11 11:54:25 -0700102
Liam Harringtonc782be62020-07-17 19:48:24 +0000103 controller->mContext.setHandlerController(controller);
104 controller->mContext.setCallbackController(controller);
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100105 return controller;
106}
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700107
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100108PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan3401d232023-06-15 23:15:32 +0000109 const sp<Looper>& looper, SpriteController& spriteController,
110 bool enabled)
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800111 : PointerController(
Prabir Pradhan3401d232023-06-15 23:15:32 +0000112 policy, looper, spriteController, enabled,
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800113 [](const sp<android::gui::WindowInfosListener>& listener) {
114 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener);
115 },
116 [](const sp<android::gui::WindowInfosListener>& listener) {
117 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
118 }) {}
119
120PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000121 const sp<Looper>& looper, SpriteController& spriteController,
Prabir Pradhan3401d232023-06-15 23:15:32 +0000122 bool enabled, WindowListenerConsumer registerListener,
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800123 WindowListenerConsumer unregisterListener)
Prabir Pradhan3401d232023-06-15 23:15:32 +0000124 : mEnabled(enabled),
125 mContext(policy, looper, spriteController, *this),
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000126 mCursorController(mContext),
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000127 mDisplayInfoListener(sp<DisplayInfoListener>::make(this)),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800128 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
129 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000130 mLocked.presentation = Presentation::SPOT;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800131 registerListener(mDisplayInfoListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000132}
133
134PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800135 mDisplayInfoListener->onPointerControllerDestroyed();
136 mUnregisterWindowInfosListener(mDisplayInfoListener);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000137 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800138}
139
140std::mutex& PointerController::getLock() const {
141 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800142}
143
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000144std::optional<FloatRect> PointerController::getBounds() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000145 if (!mEnabled) return {};
146
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000147 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800148}
149
150void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000151 if (!mEnabled) return;
152
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000153 const int32_t displayId = mCursorController.getDisplayId();
154 vec2 transformed;
155 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800156 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000157 const auto& transform = getTransformForDisplayLocked(displayId);
158 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
159 }
160 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800161}
162
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800163void PointerController::setPosition(float x, float y) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000164 if (!mEnabled) return;
165
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000166 const int32_t displayId = mCursorController.getDisplayId();
167 vec2 transformed;
168 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800169 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000170 const auto& transform = getTransformForDisplayLocked(displayId);
171 transformed = transform.transform(x, y);
172 }
173 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800174}
175
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000176FloatPoint PointerController::getPosition() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000177 if (!mEnabled) {
Byoungho Jung49f94b52023-10-30 17:41:28 +0900178 return FloatPoint{0, 0};
Prabir Pradhan3401d232023-06-15 23:15:32 +0000179 }
180
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000181 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000182 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000183 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800184 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000185 const auto& transform = getTransformForDisplayLocked(displayId);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000186 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000187 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800188}
189
Arthur Hungb9b32002018-12-18 17:39:43 +0800190int32_t PointerController::getDisplayId() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000191 if (!mEnabled) return ADISPLAY_ID_NONE;
192
Liam Harringtonc782be62020-07-17 19:48:24 +0000193 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800194}
195
Jeff Brown538881e2011-05-25 18:23:38 -0700196void PointerController::fade(Transition transition) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000197 if (!mEnabled) return;
198
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800199 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000200 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800201}
202
Jeff Brown538881e2011-05-25 18:23:38 -0700203void PointerController::unfade(Transition transition) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000204 if (!mEnabled) return;
205
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800206 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000207 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800208}
209
Jeff Brown2352b972011-04-12 22:39:53 -0700210void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000211 if (!mEnabled) return;
212
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800213 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800214
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800215 if (mLocked.presentation == presentation) {
216 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700217 }
218
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800219 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700220
Prabir Pradhan660ad302023-11-30 20:02:11 +0000221 if (input_flags::enable_pointer_choreographer()) {
222 // When pointer choreographer is enabled, the presentation mode is only set once when the
223 // PointerController is constructed, before the display viewport is provided.
224 // TODO(b/293587049): Clean up the PointerController interface after pointer choreographer
225 // is permanently enabled. The presentation can be set in the constructor.
226 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
227 return;
228 }
229
Liam Harringtonc782be62020-07-17 19:48:24 +0000230 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800231 return;
232 }
233
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900234 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
235 // For now, we support stylus hover using the mouse cursor implementation.
236 // TODO: Add proper support for stylus hover icons.
237 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
238
Liam Harringtonc782be62020-07-17 19:48:24 +0000239 mCursorController.getAdditionalMouseResources();
240 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800241 }
242}
243
Liam Harringtonc782be62020-07-17 19:48:24 +0000244void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
245 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000246 if (!mEnabled) return;
247
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800248 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000249 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
250 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
251
252 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
253 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
254
255 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
256 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
257 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
258
259 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
260 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
261 }
262
Liam Harringtonc782be62020-07-17 19:48:24 +0000263 auto it = mLocked.spotControllers.find(displayId);
264 if (it == mLocked.spotControllers.end()) {
265 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700266 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000267 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700268}
269
270void PointerController::clearSpots() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000271 if (!mEnabled) return;
272
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800273 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000274 clearSpotsLocked();
275}
Jeff Brown2352b972011-04-12 22:39:53 -0700276
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800277void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100278 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000279 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800280 }
Jeff Brown2352b972011-04-12 22:39:53 -0700281}
282
283void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000284 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800285}
286
Jun Mukai19a56012015-11-24 11:25:52 -0800287void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800288 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800289
Michael Wright21401ad92022-10-22 03:23:55 +0100290 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000291 spotController.reloadSpotResources();
292 }
Jun Mukai19a56012015-11-24 11:25:52 -0800293
Liam Harringtonc782be62020-07-17 19:48:24 +0000294 if (mCursorController.resourcesLoaded()) {
295 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900296 if (mLocked.presentation == PointerController::Presentation::POINTER ||
297 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000298 getAdditionalMouseResources = true;
299 }
300 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800301 }
302}
303
304void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000305 struct PointerDisplayChangeArgs {
306 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000307 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000308 };
309 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000310
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000311 { // acquire lock
312 std::scoped_lock lock(getLock());
313
314 bool getAdditionalMouseResources = false;
315 if (mLocked.presentation == PointerController::Presentation::POINTER ||
316 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
317 getAdditionalMouseResources = true;
318 }
319 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
320 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000321 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000322 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000323 }
324 } // release lock
325
326 if (pointerDisplayChanged) {
327 // Notify the policy without holding the pointer controller lock.
328 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000329 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000330 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800331}
332
Brandon Pollack015f5d92022-06-02 06:59:33 +0000333void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000334 if (!mEnabled) return;
335
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800336 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000337 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700338}
339
Jun Mukaid4eaef72015-10-30 15:54:33 -0700340void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000341 if (!mEnabled) return;
342
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800343 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000344 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700345}
346
Jeff Brown2352b972011-04-12 22:39:53 -0700347void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100348 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800349}
350
Prabir Pradhanc2200182023-06-09 23:39:15 +0000351void PointerController::onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000352 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000353 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000354 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800355 }
356
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800357 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000358 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100359 int32_t displayId = it->first;
360 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000361 /*
362 * Ensures that an in-progress animation won't dereference
363 * a null pointer to TouchSpotController.
364 */
Michael Wright21401ad92022-10-22 03:23:55 +0100365 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000366 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700367 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000368 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700369 }
370 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800371}
372
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800373void PointerController::onDisplayInfosChangedLocked(
374 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000375 mLocked.mDisplayInfos = displayInfo;
376}
377
378const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
379 const auto& di = mLocked.mDisplayInfos;
380 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
381 return info.displayId == displayId;
382 });
383 return it != di.end() ? it->transform : kIdentityTransform;
384}
385
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000386std::string PointerController::dump() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000387 if (!mEnabled) {
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000388 return INDENT "PointerController: DISABLED due to ongoing PointerChoreographer refactor\n";
Prabir Pradhan3401d232023-06-15 23:15:32 +0000389 }
390
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000391 std::string dump = INDENT "PointerController:\n";
Michael Wright72a89132022-10-22 03:16:31 +0100392 std::scoped_lock lock(getLock());
393 dump += StringPrintf(INDENT2 "Presentation: %s\n",
394 ftl::enum_string(mLocked.presentation).c_str());
395 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100396 dump += StringPrintf(INDENT2 "Viewports:\n");
397 for (const auto& info : mLocked.mDisplayInfos) {
398 info.dump(dump, INDENT3);
399 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100400 dump += INDENT2 "Spot Controllers:\n";
401 for (const auto& [_, spotController] : mLocked.spotControllers) {
402 spotController.dump(dump, INDENT3);
403 }
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000404 return dump;
Michael Wright72a89132022-10-22 03:16:31 +0100405}
406
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900407// --- MousePointerController ---
408
409MousePointerController::MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
410 const sp<Looper>& looper,
411 SpriteController& spriteController, bool enabled)
412 : PointerController(policy, looper, spriteController, enabled) {
413 PointerController::setPresentation(Presentation::POINTER);
414}
415
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000416MousePointerController::~MousePointerController() {
417 MousePointerController::fade(Transition::IMMEDIATE);
418}
419
Byoungho Jung51282e82023-10-27 18:15:41 +0900420// --- TouchPointerController ---
421
422TouchPointerController::TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
423 const sp<Looper>& looper,
424 SpriteController& spriteController, bool enabled)
425 : PointerController(policy, looper, spriteController, enabled) {
426 PointerController::setPresentation(Presentation::SPOT);
427}
428
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000429TouchPointerController::~TouchPointerController() {
430 TouchPointerController::clearSpots();
431}
432
Byoungho Jung08daa252023-10-27 20:55:18 +0900433// --- StylusPointerController ---
434
435StylusPointerController::StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
436 const sp<Looper>& looper,
437 SpriteController& spriteController, bool enabled)
438 : PointerController(policy, looper, spriteController, enabled) {
439 PointerController::setPresentation(Presentation::STYLUS_HOVER);
440}
441
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000442StylusPointerController::~StylusPointerController() {
443 StylusPointerController::fade(Transition::IMMEDIATE);
444}
445
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800446} // namespace android