blob: f84107e8792cb40e214eb0e3260ba3bcc009374b [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) {
Linnan Li37c1b992023-11-24 13:05:13 +0800114 auto initialInfo = std::make_pair(std::vector<android::gui::WindowInfo>{},
115 std::vector<android::gui::DisplayInfo>{});
116 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener,
117 &initialInfo);
Prabir Pradhanbdf93692024-01-23 18:08:28 +0000118 return initialInfo.second;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800119 },
120 [](const sp<android::gui::WindowInfosListener>& listener) {
121 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
122 }) {}
123
124PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000125 const sp<Looper>& looper, SpriteController& spriteController,
Linnan Li37c1b992023-11-24 13:05:13 +0800126 bool enabled,
127 const WindowListenerRegisterConsumer& registerListener,
128 WindowListenerUnregisterConsumer unregisterListener)
Prabir Pradhan3401d232023-06-15 23:15:32 +0000129 : mEnabled(enabled),
130 mContext(policy, looper, spriteController, *this),
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000131 mCursorController(mContext),
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000132 mDisplayInfoListener(sp<DisplayInfoListener>::make(this)),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800133 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
134 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000135 mLocked.presentation = Presentation::SPOT;
Prabir Pradhanbdf93692024-01-23 18:08:28 +0000136 const auto& initialDisplayInfos = registerListener(mDisplayInfoListener);
Linnan Li37c1b992023-11-24 13:05:13 +0800137 onDisplayInfosChangedLocked(initialDisplayInfos);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000138}
139
140PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800141 mDisplayInfoListener->onPointerControllerDestroyed();
142 mUnregisterWindowInfosListener(mDisplayInfoListener);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000143 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800144}
145
146std::mutex& PointerController::getLock() const {
147 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800148}
149
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000150std::optional<FloatRect> PointerController::getBounds() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000151 if (!mEnabled) return {};
152
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000153 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800154}
155
156void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000157 if (!mEnabled) return;
158
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000159 const int32_t displayId = mCursorController.getDisplayId();
160 vec2 transformed;
161 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800162 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000163 const auto& transform = getTransformForDisplayLocked(displayId);
164 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
165 }
166 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800167}
168
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800169void PointerController::setPosition(float x, float y) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000170 if (!mEnabled) return;
171
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000172 const int32_t displayId = mCursorController.getDisplayId();
173 vec2 transformed;
174 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800175 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000176 const auto& transform = getTransformForDisplayLocked(displayId);
177 transformed = transform.transform(x, y);
178 }
179 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800180}
181
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000182FloatPoint PointerController::getPosition() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000183 if (!mEnabled) {
Byoungho Jung49f94b52023-10-30 17:41:28 +0900184 return FloatPoint{0, 0};
Prabir Pradhan3401d232023-06-15 23:15:32 +0000185 }
186
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000187 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000188 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000189 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800190 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000191 const auto& transform = getTransformForDisplayLocked(displayId);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000192 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000193 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800194}
195
Arthur Hungb9b32002018-12-18 17:39:43 +0800196int32_t PointerController::getDisplayId() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000197 if (!mEnabled) return ADISPLAY_ID_NONE;
198
Liam Harringtonc782be62020-07-17 19:48:24 +0000199 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800200}
201
Jeff Brown538881e2011-05-25 18:23:38 -0700202void PointerController::fade(Transition transition) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000203 if (!mEnabled) return;
204
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800205 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000206 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800207}
208
Jeff Brown538881e2011-05-25 18:23:38 -0700209void PointerController::unfade(Transition transition) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000210 if (!mEnabled) return;
211
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800212 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000213 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800214}
215
Jeff Brown2352b972011-04-12 22:39:53 -0700216void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000217 if (!mEnabled) return;
218
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800219 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800220
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800221 if (mLocked.presentation == presentation) {
222 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700223 }
224
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800225 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700226
Prabir Pradhan660ad302023-11-30 20:02:11 +0000227 if (input_flags::enable_pointer_choreographer()) {
228 // When pointer choreographer is enabled, the presentation mode is only set once when the
229 // PointerController is constructed, before the display viewport is provided.
230 // TODO(b/293587049): Clean up the PointerController interface after pointer choreographer
231 // is permanently enabled. The presentation can be set in the constructor.
232 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
233 return;
234 }
235
Liam Harringtonc782be62020-07-17 19:48:24 +0000236 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800237 return;
238 }
239
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900240 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
241 // For now, we support stylus hover using the mouse cursor implementation.
242 // TODO: Add proper support for stylus hover icons.
243 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
244
Liam Harringtonc782be62020-07-17 19:48:24 +0000245 mCursorController.getAdditionalMouseResources();
246 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800247 }
248}
249
Liam Harringtonc782be62020-07-17 19:48:24 +0000250void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
251 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000252 if (!mEnabled) return;
253
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800254 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000255 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
256 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
257
258 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
259 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
260
261 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
262 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
263 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
264
265 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
266 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
267 }
268
Liam Harringtonc782be62020-07-17 19:48:24 +0000269 auto it = mLocked.spotControllers.find(displayId);
270 if (it == mLocked.spotControllers.end()) {
271 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700272 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000273 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700274}
275
276void PointerController::clearSpots() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000277 if (!mEnabled) return;
278
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800279 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000280 clearSpotsLocked();
281}
Jeff Brown2352b972011-04-12 22:39:53 -0700282
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800283void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100284 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000285 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800286 }
Jeff Brown2352b972011-04-12 22:39:53 -0700287}
288
289void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000290 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800291}
292
Jun Mukai19a56012015-11-24 11:25:52 -0800293void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800294 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800295
Michael Wright21401ad92022-10-22 03:23:55 +0100296 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000297 spotController.reloadSpotResources();
298 }
Jun Mukai19a56012015-11-24 11:25:52 -0800299
Liam Harringtonc782be62020-07-17 19:48:24 +0000300 if (mCursorController.resourcesLoaded()) {
301 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900302 if (mLocked.presentation == PointerController::Presentation::POINTER ||
303 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000304 getAdditionalMouseResources = true;
305 }
306 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800307 }
308}
309
310void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000311 struct PointerDisplayChangeArgs {
312 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000313 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000314 };
315 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000316
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000317 { // acquire lock
318 std::scoped_lock lock(getLock());
319
320 bool getAdditionalMouseResources = false;
321 if (mLocked.presentation == PointerController::Presentation::POINTER ||
322 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
323 getAdditionalMouseResources = true;
324 }
325 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
326 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000327 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000328 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000329 }
330 } // release lock
331
332 if (pointerDisplayChanged) {
333 // Notify the policy without holding the pointer controller lock.
334 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000335 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000336 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800337}
338
Brandon Pollack015f5d92022-06-02 06:59:33 +0000339void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000340 if (!mEnabled) return;
341
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800342 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000343 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700344}
345
Jun Mukaid4eaef72015-10-30 15:54:33 -0700346void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000347 if (!mEnabled) return;
348
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800349 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000350 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700351}
352
Jeff Brown2352b972011-04-12 22:39:53 -0700353void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100354 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800355}
356
Prabir Pradhanc2200182023-06-09 23:39:15 +0000357void PointerController::onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000358 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000359 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000360 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800361 }
362
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800363 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000364 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100365 int32_t displayId = it->first;
366 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000367 /*
368 * Ensures that an in-progress animation won't dereference
369 * a null pointer to TouchSpotController.
370 */
Michael Wright21401ad92022-10-22 03:23:55 +0100371 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000372 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700373 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000374 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700375 }
376 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800377}
378
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800379void PointerController::onDisplayInfosChangedLocked(
380 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000381 mLocked.mDisplayInfos = displayInfo;
382}
383
384const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
385 const auto& di = mLocked.mDisplayInfos;
386 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
387 return info.displayId == displayId;
388 });
389 return it != di.end() ? it->transform : kIdentityTransform;
390}
391
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000392std::string PointerController::dump() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000393 if (!mEnabled) {
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000394 return INDENT "PointerController: DISABLED due to ongoing PointerChoreographer refactor\n";
Prabir Pradhan3401d232023-06-15 23:15:32 +0000395 }
396
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000397 std::string dump = INDENT "PointerController:\n";
Michael Wright72a89132022-10-22 03:16:31 +0100398 std::scoped_lock lock(getLock());
399 dump += StringPrintf(INDENT2 "Presentation: %s\n",
400 ftl::enum_string(mLocked.presentation).c_str());
401 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100402 dump += StringPrintf(INDENT2 "Viewports:\n");
403 for (const auto& info : mLocked.mDisplayInfos) {
404 info.dump(dump, INDENT3);
405 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100406 dump += INDENT2 "Spot Controllers:\n";
407 for (const auto& [_, spotController] : mLocked.spotControllers) {
408 spotController.dump(dump, INDENT3);
409 }
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000410 return dump;
Michael Wright72a89132022-10-22 03:16:31 +0100411}
412
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900413// --- MousePointerController ---
414
415MousePointerController::MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
416 const sp<Looper>& looper,
417 SpriteController& spriteController, bool enabled)
418 : PointerController(policy, looper, spriteController, enabled) {
419 PointerController::setPresentation(Presentation::POINTER);
420}
421
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000422MousePointerController::~MousePointerController() {
423 MousePointerController::fade(Transition::IMMEDIATE);
424}
425
Byoungho Jung51282e82023-10-27 18:15:41 +0900426// --- TouchPointerController ---
427
428TouchPointerController::TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
429 const sp<Looper>& looper,
430 SpriteController& spriteController, bool enabled)
431 : PointerController(policy, looper, spriteController, enabled) {
432 PointerController::setPresentation(Presentation::SPOT);
433}
434
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000435TouchPointerController::~TouchPointerController() {
436 TouchPointerController::clearSpots();
437}
438
Byoungho Jung08daa252023-10-27 20:55:18 +0900439// --- StylusPointerController ---
440
441StylusPointerController::StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
442 const sp<Looper>& looper,
443 SpriteController& spriteController, bool enabled)
444 : PointerController(policy, looper, spriteController, enabled) {
445 PointerController::setPresentation(Presentation::STYLUS_HOVER);
446}
447
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000448StylusPointerController::~StylusPointerController() {
449 StylusPointerController::fade(Transition::IMMEDIATE);
450}
451
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800452} // namespace android