blob: 933a33eb8b0ea1244e0a68a88d127792ba5187f0 [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);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000145 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800146}
147
148std::mutex& PointerController::getLock() const {
149 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800150}
151
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000152std::optional<FloatRect> PointerController::getBounds() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000153 if (!mEnabled) return {};
154
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000155 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800156}
157
158void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000159 if (!mEnabled) return;
160
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000161 const int32_t displayId = mCursorController.getDisplayId();
162 vec2 transformed;
163 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800164 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000165 const auto& transform = getTransformForDisplayLocked(displayId);
166 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
167 }
168 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800169}
170
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800171void PointerController::setPosition(float x, float y) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000172 if (!mEnabled) return;
173
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000174 const int32_t displayId = mCursorController.getDisplayId();
175 vec2 transformed;
176 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800177 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000178 const auto& transform = getTransformForDisplayLocked(displayId);
179 transformed = transform.transform(x, y);
180 }
181 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800182}
183
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000184FloatPoint PointerController::getPosition() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000185 if (!mEnabled) {
Byoungho Jung49f94b52023-10-30 17:41:28 +0900186 return FloatPoint{0, 0};
Prabir Pradhan3401d232023-06-15 23:15:32 +0000187 }
188
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000189 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000190 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000191 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800192 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000193 const auto& transform = getTransformForDisplayLocked(displayId);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000194 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000195 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800196}
197
Arthur Hungb9b32002018-12-18 17:39:43 +0800198int32_t PointerController::getDisplayId() const {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000199 if (!mEnabled) return ADISPLAY_ID_NONE;
200
Liam Harringtonc782be62020-07-17 19:48:24 +0000201 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800202}
203
Jeff Brown538881e2011-05-25 18:23:38 -0700204void PointerController::fade(Transition transition) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000205 if (!mEnabled) return;
206
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800207 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000208 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800209}
210
Jeff Brown538881e2011-05-25 18:23:38 -0700211void PointerController::unfade(Transition transition) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000212 if (!mEnabled) return;
213
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800214 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000215 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800216}
217
Jeff Brown2352b972011-04-12 22:39:53 -0700218void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000219 if (!mEnabled) return;
220
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800221 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800222
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800223 if (mLocked.presentation == presentation) {
224 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700225 }
226
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800227 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700228
Prabir Pradhan453e1632024-02-26 21:27:31 +0000229 if (ENABLE_POINTER_CHOREOGRAPHER) {
Prabir Pradhan660ad302023-11-30 20:02:11 +0000230 // When pointer choreographer is enabled, the presentation mode is only set once when the
231 // PointerController is constructed, before the display viewport is provided.
232 // TODO(b/293587049): Clean up the PointerController interface after pointer choreographer
233 // is permanently enabled. The presentation can be set in the constructor.
234 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
235 return;
236 }
237
Liam Harringtonc782be62020-07-17 19:48:24 +0000238 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800239 return;
240 }
241
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900242 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
243 // For now, we support stylus hover using the mouse cursor implementation.
244 // TODO: Add proper support for stylus hover icons.
245 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
246
Liam Harringtonc782be62020-07-17 19:48:24 +0000247 mCursorController.getAdditionalMouseResources();
248 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800249 }
250}
251
Liam Harringtonc782be62020-07-17 19:48:24 +0000252void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
253 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000254 if (!mEnabled) return;
255
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800256 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000257 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
258 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
259
260 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
261 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
262
263 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
264 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
265 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
266
267 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
268 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
269 }
270
Liam Harringtonc782be62020-07-17 19:48:24 +0000271 auto it = mLocked.spotControllers.find(displayId);
272 if (it == mLocked.spotControllers.end()) {
273 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700274 }
Arpit Singh80fd68a2024-03-26 18:41:06 +0000275 bool skipScreenshot = mLocked.displaysToSkipScreenshot.find(displayId) !=
276 mLocked.displaysToSkipScreenshot.end();
277 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits,
278 skipScreenshot);
Jeff Brown2352b972011-04-12 22:39:53 -0700279}
280
281void PointerController::clearSpots() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000282 if (!mEnabled) return;
283
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800284 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000285 clearSpotsLocked();
286}
Jeff Brown2352b972011-04-12 22:39:53 -0700287
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800288void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100289 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000290 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800291 }
Jeff Brown2352b972011-04-12 22:39:53 -0700292}
293
294void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000295 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800296}
297
Jun Mukai19a56012015-11-24 11:25:52 -0800298void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800299 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800300
Michael Wright21401ad92022-10-22 03:23:55 +0100301 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000302 spotController.reloadSpotResources();
303 }
Jun Mukai19a56012015-11-24 11:25:52 -0800304
Liam Harringtonc782be62020-07-17 19:48:24 +0000305 if (mCursorController.resourcesLoaded()) {
306 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900307 if (mLocked.presentation == PointerController::Presentation::POINTER ||
308 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000309 getAdditionalMouseResources = true;
310 }
311 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800312 }
313}
314
315void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000316 struct PointerDisplayChangeArgs {
317 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000318 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000319 };
320 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000321
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000322 { // acquire lock
323 std::scoped_lock lock(getLock());
324
325 bool getAdditionalMouseResources = false;
326 if (mLocked.presentation == PointerController::Presentation::POINTER ||
327 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
328 getAdditionalMouseResources = true;
329 }
330 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
331 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000332 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000333 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000334 }
335 } // release lock
336
337 if (pointerDisplayChanged) {
338 // Notify the policy without holding the pointer controller lock.
339 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000340 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000341 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800342}
343
Brandon Pollack015f5d92022-06-02 06:59:33 +0000344void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000345 if (!mEnabled) return;
346
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800347 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000348 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700349}
350
Jun Mukaid4eaef72015-10-30 15:54:33 -0700351void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000352 if (!mEnabled) return;
353
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800354 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000355 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700356}
357
Arpit Singh80fd68a2024-03-26 18:41:06 +0000358void PointerController::setSkipScreenshot(int32_t displayId, bool skip) {
359 if (!mEnabled) return;
360
361 std::scoped_lock lock(getLock());
362 if (skip) {
363 mLocked.displaysToSkipScreenshot.insert(displayId);
364 } else {
365 mLocked.displaysToSkipScreenshot.erase(displayId);
366 }
367}
368
Jeff Brown2352b972011-04-12 22:39:53 -0700369void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100370 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800371}
372
Prabir Pradhanc2200182023-06-09 23:39:15 +0000373void PointerController::onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000374 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000375 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000376 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800377 }
378
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800379 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000380 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100381 int32_t displayId = it->first;
382 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000383 /*
384 * Ensures that an in-progress animation won't dereference
385 * a null pointer to TouchSpotController.
386 */
Michael Wright21401ad92022-10-22 03:23:55 +0100387 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000388 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700389 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000390 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700391 }
392 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800393}
394
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800395void PointerController::onDisplayInfosChangedLocked(
396 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000397 mLocked.mDisplayInfos = displayInfo;
398}
399
400const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
401 const auto& di = mLocked.mDisplayInfos;
402 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
403 return info.displayId == displayId;
404 });
405 return it != di.end() ? it->transform : kIdentityTransform;
406}
407
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000408std::string PointerController::dump() {
Prabir Pradhan3401d232023-06-15 23:15:32 +0000409 if (!mEnabled) {
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000410 return INDENT "PointerController: DISABLED due to ongoing PointerChoreographer refactor\n";
Prabir Pradhan3401d232023-06-15 23:15:32 +0000411 }
412
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000413 std::string dump = INDENT "PointerController:\n";
Michael Wright72a89132022-10-22 03:16:31 +0100414 std::scoped_lock lock(getLock());
415 dump += StringPrintf(INDENT2 "Presentation: %s\n",
416 ftl::enum_string(mLocked.presentation).c_str());
417 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100418 dump += StringPrintf(INDENT2 "Viewports:\n");
419 for (const auto& info : mLocked.mDisplayInfos) {
420 info.dump(dump, INDENT3);
421 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100422 dump += INDENT2 "Spot Controllers:\n";
423 for (const auto& [_, spotController] : mLocked.spotControllers) {
424 spotController.dump(dump, INDENT3);
425 }
Prabir Pradhanb41c2192023-09-06 00:48:22 +0000426 return dump;
Michael Wright72a89132022-10-22 03:16:31 +0100427}
428
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900429// --- MousePointerController ---
430
431MousePointerController::MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
432 const sp<Looper>& looper,
433 SpriteController& spriteController, bool enabled)
434 : PointerController(policy, looper, spriteController, enabled) {
435 PointerController::setPresentation(Presentation::POINTER);
436}
437
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000438MousePointerController::~MousePointerController() {
439 MousePointerController::fade(Transition::IMMEDIATE);
440}
441
Byoungho Jung51282e82023-10-27 18:15:41 +0900442// --- TouchPointerController ---
443
444TouchPointerController::TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
445 const sp<Looper>& looper,
446 SpriteController& spriteController, bool enabled)
447 : PointerController(policy, looper, spriteController, enabled) {
448 PointerController::setPresentation(Presentation::SPOT);
449}
450
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000451TouchPointerController::~TouchPointerController() {
452 TouchPointerController::clearSpots();
453}
454
Byoungho Jung08daa252023-10-27 20:55:18 +0900455// --- StylusPointerController ---
456
457StylusPointerController::StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
458 const sp<Looper>& looper,
459 SpriteController& spriteController, bool enabled)
460 : PointerController(policy, looper, spriteController, enabled) {
461 PointerController::setPresentation(Presentation::STYLUS_HOVER);
462}
463
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000464StylusPointerController::~StylusPointerController() {
465 StylusPointerController::fade(Transition::IMMEDIATE);
466}
467
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800468} // namespace android