blob: 43264dbe92364a7bc4042c871c24118d34040521 [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
Prabir Pradhan453e1632024-02-26 21:27:31 +000044static const bool ENABLE_POINTER_CHOREOGRAPHER = input_flags::enable_pointer_choreographer();
45
Prabir Pradhanf97fac32021-11-18 16:40:34 +000046const ui::Transform kIdentityTransform;
47
48} // namespace
49
50// --- PointerController::DisplayInfoListener ---
51
52void PointerController::DisplayInfoListener::onWindowInfosChanged(
Patrick Williams8e47a672023-05-01 11:30:37 -050053 const gui::WindowInfosUpdate& update) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -080054 std::scoped_lock lock(mLock);
55 if (mPointerController == nullptr) return;
56
57 // PointerController uses DisplayInfoListener's lock.
58 base::ScopedLockAssertion assumeLocked(mPointerController->getLock());
Patrick Williams8e47a672023-05-01 11:30:37 -050059 mPointerController->onDisplayInfosChangedLocked(update.displayInfos);
Prabir Pradhan5693cee2021-12-31 06:51:15 -080060}
61
62void PointerController::DisplayInfoListener::onPointerControllerDestroyed() {
63 std::scoped_lock lock(mLock);
64 mPointerController = nullptr;
Prabir Pradhanf97fac32021-11-18 16:40:34 +000065}
66
Jeff Brownb4ff35d2011-01-02 16:37:43 -080067// --- PointerController ---
68
Michael Wrighta0bc6b12020-06-26 20:25:34 +010069std::shared_ptr<PointerController> PointerController::create(
70 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Byoungho Jung6a2ce942023-10-07 16:19:19 +090071 SpriteController& spriteController, bool enabled, ControllerType type) {
Liam Harringtonc782be62020-07-17 19:48:24 +000072 // using 'new' to access non-public constructor
Byoungho Jung6a2ce942023-10-07 16:19:19 +090073 std::shared_ptr<PointerController> controller;
74 switch (type) {
75 case ControllerType::MOUSE:
76 controller = std::shared_ptr<PointerController>(
77 new MousePointerController(policy, looper, spriteController, enabled));
78 break;
Byoungho Jung51282e82023-10-27 18:15:41 +090079 case ControllerType::TOUCH:
80 controller = std::shared_ptr<PointerController>(
81 new TouchPointerController(policy, looper, spriteController, enabled));
82 break;
Byoungho Jung08daa252023-10-27 20:55:18 +090083 case ControllerType::STYLUS:
84 controller = std::shared_ptr<PointerController>(
85 new StylusPointerController(policy, looper, spriteController, enabled));
86 break;
Byoungho Jung6a2ce942023-10-07 16:19:19 +090087 case ControllerType::LEGACY:
88 default:
89 controller = std::shared_ptr<PointerController>(
90 new PointerController(policy, looper, spriteController, enabled));
91 break;
92 }
Jeff Brown2352b972011-04-12 22:39:53 -070093
Michael Wrighta0bc6b12020-06-26 20:25:34 +010094 /*
95 * Now we need to hook up the constructed PointerController object to its callbacks.
96 *
97 * This must be executed after the constructor but before any other methods on PointerController
98 * in order to ensure that the fully constructed object is visible on the Looper thread, since
99 * that may be a different thread than where the PointerController is initially constructed.
100 *
101 * Unfortunately, this cannot be done as part of the constructor since we need to hand out
102 * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
103 */
Jeff Brown5541de92011-04-11 11:54:25 -0700104
Liam Harringtonc782be62020-07-17 19:48:24 +0000105 controller->mContext.setHandlerController(controller);
106 controller->mContext.setCallbackController(controller);
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100107 return controller;
108}
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700109
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100110PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan3401d232023-06-15 23:15:32 +0000111 const sp<Looper>& looper, SpriteController& spriteController,
112 bool enabled)
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800113 : PointerController(
Prabir Pradhan3401d232023-06-15 23:15:32 +0000114 policy, looper, spriteController, enabled,
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800115 [](const sp<android::gui::WindowInfosListener>& listener) {
Linnan Li37c1b992023-11-24 13:05:13 +0800116 auto initialInfo = std::make_pair(std::vector<android::gui::WindowInfo>{},
117 std::vector<android::gui::DisplayInfo>{});
118 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener,
119 &initialInfo);
Prabir Pradhanbdf93692024-01-23 18:08:28 +0000120 return initialInfo.second;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800121 },
122 [](const sp<android::gui::WindowInfosListener>& listener) {
123 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
124 }) {}
125
126PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000127 const sp<Looper>& looper, SpriteController& spriteController,
Linnan Li37c1b992023-11-24 13:05:13 +0800128 bool enabled,
129 const WindowListenerRegisterConsumer& registerListener,
130 WindowListenerUnregisterConsumer unregisterListener)
Prabir Pradhan3401d232023-06-15 23:15:32 +0000131 : mEnabled(enabled),
132 mContext(policy, looper, spriteController, *this),
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000133 mCursorController(mContext),
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000134 mDisplayInfoListener(sp<DisplayInfoListener>::make(this)),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800135 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
136 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000137 mLocked.presentation = Presentation::SPOT;
Prabir Pradhanbdf93692024-01-23 18:08:28 +0000138 const auto& initialDisplayInfos = registerListener(mDisplayInfoListener);
Linnan Li37c1b992023-11-24 13:05:13 +0800139 onDisplayInfosChangedLocked(initialDisplayInfos);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000140}
141
142PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800143 mDisplayInfoListener->onPointerControllerDestroyed();
144 mUnregisterWindowInfosListener(mDisplayInfoListener);
145}
146
147std::mutex& PointerController::getLock() const {
148 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800149}
150
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000151std::optional<FloatRect> PointerController::getBounds() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000152 if (!mEnabled) return {};
153
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000154 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800155}
156
157void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000158 if (!mEnabled) return;
159
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000160 const int32_t displayId = mCursorController.getDisplayId();
161 vec2 transformed;
162 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800163 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000164 const auto& transform = getTransformForDisplayLocked(displayId);
165 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
166 }
167 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800168}
169
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800170void PointerController::setPosition(float x, float y) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000171 if (!mEnabled) return;
172
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000173 const int32_t displayId = mCursorController.getDisplayId();
174 vec2 transformed;
175 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800176 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000177 const auto& transform = getTransformForDisplayLocked(displayId);
178 transformed = transform.transform(x, y);
179 }
180 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800181}
182
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000183FloatPoint PointerController::getPosition() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000184 if (!mEnabled) {
Byoungho Jung49f94b52023-10-30 17:41:28 +0900185 return FloatPoint{0, 0};
Prabir Pradhan3401d232023-06-15 23:15:32 +0000186 }
187
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000188 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000189 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000190 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800191 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000192 const auto& transform = getTransformForDisplayLocked(displayId);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000193 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000194 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800195}
196
Arthur Hungb9b32002018-12-18 17:39:43 +0800197int32_t PointerController::getDisplayId() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000198 if (!mEnabled) return ADISPLAY_ID_NONE;
199
Liam Harringtonc782be62020-07-17 19:48:24 +0000200 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800201}
202
Jeff Brown538881e2011-05-25 18:23:38 -0700203void PointerController::fade(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.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800208}
209
Jeff Brown538881e2011-05-25 18:23:38 -0700210void PointerController::unfade(Transition transition) {
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());
Liam Harringtonc782be62020-07-17 19:48:24 +0000214 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800215}
216
Jeff Brown2352b972011-04-12 22:39:53 -0700217void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000218 if (!mEnabled) return;
219
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800220 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800221
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800222 if (mLocked.presentation == presentation) {
223 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700224 }
225
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800226 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700227
Prabir Pradhan453e1632024-02-26 21:27:31 +0000228 if (ENABLE_POINTER_CHOREOGRAPHER) {
Prabir Pradhan660ad302023-11-30 20:02:11 +0000229 // When pointer choreographer is enabled, the presentation mode is only set once when the
230 // PointerController is constructed, before the display viewport is provided.
231 // TODO(b/293587049): Clean up the PointerController interface after pointer choreographer
232 // is permanently enabled. The presentation can be set in the constructor.
233 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
234 return;
235 }
236
Liam Harringtonc782be62020-07-17 19:48:24 +0000237 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800238 return;
239 }
240
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900241 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
242 // For now, we support stylus hover using the mouse cursor implementation.
243 // TODO: Add proper support for stylus hover icons.
244 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
245
Liam Harringtonc782be62020-07-17 19:48:24 +0000246 mCursorController.getAdditionalMouseResources();
247 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800248 }
249}
250
Liam Harringtonc782be62020-07-17 19:48:24 +0000251void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
252 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000253 if (!mEnabled) return;
254
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800255 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000256 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
257 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
258
259 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
260 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
261
262 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
263 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
264 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
265
266 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
267 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
268 }
269
Liam Harringtonc782be62020-07-17 19:48:24 +0000270 auto it = mLocked.spotControllers.find(displayId);
271 if (it == mLocked.spotControllers.end()) {
272 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700273 }
Arpit Singh80fd68a2024-03-26 18:41:06 +0000274 bool skipScreenshot = mLocked.displaysToSkipScreenshot.find(displayId) !=
275 mLocked.displaysToSkipScreenshot.end();
276 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits,
277 skipScreenshot);
Jeff Brown2352b972011-04-12 22:39:53 -0700278}
279
280void PointerController::clearSpots() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000281 if (!mEnabled) return;
282
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800283 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000284 clearSpotsLocked();
285}
Jeff Brown2352b972011-04-12 22:39:53 -0700286
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800287void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100288 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000289 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800290 }
Jeff Brown2352b972011-04-12 22:39:53 -0700291}
292
293void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000294 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800295}
296
Jun Mukai19a56012015-11-24 11:25:52 -0800297void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800298 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800299
Michael Wright21401ad92022-10-22 03:23:55 +0100300 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000301 spotController.reloadSpotResources();
302 }
Jun Mukai19a56012015-11-24 11:25:52 -0800303
Liam Harringtonc782be62020-07-17 19:48:24 +0000304 if (mCursorController.resourcesLoaded()) {
305 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900306 if (mLocked.presentation == PointerController::Presentation::POINTER ||
307 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000308 getAdditionalMouseResources = true;
309 }
310 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800311 }
312}
313
314void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000315 { // acquire lock
316 std::scoped_lock lock(getLock());
317
318 bool getAdditionalMouseResources = false;
319 if (mLocked.presentation == PointerController::Presentation::POINTER ||
320 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
321 getAdditionalMouseResources = true;
322 }
323 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
324 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000325 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000326 }
327 } // release lock
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800328}
329
Brandon Pollack015f5d92022-06-02 06:59:33 +0000330void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000331 if (!mEnabled) return;
332
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800333 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000334 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700335}
336
Jun Mukaid4eaef72015-10-30 15:54:33 -0700337void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000338 if (!mEnabled) return;
339
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800340 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000341 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700342}
343
Arpit Singh80fd68a2024-03-26 18:41:06 +0000344void PointerController::setSkipScreenshot(int32_t displayId, bool skip) {
345 if (!mEnabled) return;
346
347 std::scoped_lock lock(getLock());
348 if (skip) {
349 mLocked.displaysToSkipScreenshot.insert(displayId);
350 } else {
351 mLocked.displaysToSkipScreenshot.erase(displayId);
352 }
353}
354
Jeff Brown2352b972011-04-12 22:39:53 -0700355void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100356 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800357}
358
Prabir Pradhanc2200182023-06-09 23:39:15 +0000359void PointerController::onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000360 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000361 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000362 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800363 }
364
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800365 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000366 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100367 int32_t displayId = it->first;
368 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000369 /*
370 * Ensures that an in-progress animation won't dereference
371 * a null pointer to TouchSpotController.
372 */
Michael Wright21401ad92022-10-22 03:23:55 +0100373 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000374 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700375 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000376 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700377 }
378 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800379}
380
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800381void PointerController::onDisplayInfosChangedLocked(
382 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000383 mLocked.mDisplayInfos = displayInfo;
384}
385
386const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
387 const auto& di = mLocked.mDisplayInfos;
388 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
389 return info.displayId == displayId;
390 });
391 return it != di.end() ? it->transform : kIdentityTransform;
392}
393
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000394std::string PointerController::dump() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000395 if (!mEnabled) {
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000396 return INDENT "PointerController: DISABLED due to ongoing PointerChoreographer refactor\n";
Prabir Pradhan3401d232023-06-15 23:15:32 +0000397 }
398
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000399 std::string dump = INDENT "PointerController:\n";
Michael Wright72a89132022-10-22 03:16:31 +0100400 std::scoped_lock lock(getLock());
401 dump += StringPrintf(INDENT2 "Presentation: %s\n",
402 ftl::enum_string(mLocked.presentation).c_str());
403 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100404 dump += StringPrintf(INDENT2 "Viewports:\n");
405 for (const auto& info : mLocked.mDisplayInfos) {
406 info.dump(dump, INDENT3);
407 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100408 dump += INDENT2 "Spot Controllers:\n";
409 for (const auto& [_, spotController] : mLocked.spotControllers) {
410 spotController.dump(dump, INDENT3);
411 }
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000412 return dump;
Michael Wright72a89132022-10-22 03:16:31 +0100413}
414
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900415// --- MousePointerController ---
416
417MousePointerController::MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
418 const sp<Looper>& looper,
419 SpriteController& spriteController, bool enabled)
420 : PointerController(policy, looper, spriteController, enabled) {
421 PointerController::setPresentation(Presentation::POINTER);
422}
423
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000424MousePointerController::~MousePointerController() {
425 MousePointerController::fade(Transition::IMMEDIATE);
426}
427
Byoungho Jung51282e82023-10-27 18:15:41 +0900428// --- TouchPointerController ---
429
430TouchPointerController::TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
431 const sp<Looper>& looper,
432 SpriteController& spriteController, bool enabled)
433 : PointerController(policy, looper, spriteController, enabled) {
434 PointerController::setPresentation(Presentation::SPOT);
435}
436
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000437TouchPointerController::~TouchPointerController() {
438 TouchPointerController::clearSpots();
439}
440
Byoungho Jung08daa252023-10-27 20:55:18 +0900441// --- StylusPointerController ---
442
443StylusPointerController::StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
444 const sp<Looper>& looper,
445 SpriteController& spriteController, bool enabled)
446 : PointerController(policy, looper, spriteController, enabled) {
447 PointerController::setPresentation(Presentation::STYLUS_HOVER);
448}
449
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000450StylusPointerController::~StylusPointerController() {
451 StylusPointerController::fade(Transition::IMMEDIATE);
452}
453
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800454} // namespace android