blob: e21d6fb2fe14f7ec955ef23b9563b201a1125944 [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>
Michael Wright72a89132022-10-22 03:16:31 +010027#include <ftl/enum.h>
28
29#include <mutex>
Prabir Pradhan5693cee2021-12-31 06:51:15 -080030
31#include "PointerControllerContext.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032
Michael Wright72a89132022-10-22 03:16:31 +010033#define INDENT " "
34#define INDENT2 " "
Michael Wright83577752022-10-28 16:24:33 +010035#define INDENT3 " "
Michael Wright72a89132022-10-22 03:16:31 +010036
Jeff Brownb4ff35d2011-01-02 16:37:43 -080037namespace android {
38
Prabir Pradhanf97fac32021-11-18 16:40:34 +000039namespace {
40
41const ui::Transform kIdentityTransform;
42
43} // namespace
44
45// --- PointerController::DisplayInfoListener ---
46
47void PointerController::DisplayInfoListener::onWindowInfosChanged(
Patrick Williams8e47a672023-05-01 11:30:37 -050048 const gui::WindowInfosUpdate& update) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -080049 std::scoped_lock lock(mLock);
50 if (mPointerController == nullptr) return;
51
52 // PointerController uses DisplayInfoListener's lock.
53 base::ScopedLockAssertion assumeLocked(mPointerController->getLock());
Patrick Williams8e47a672023-05-01 11:30:37 -050054 mPointerController->onDisplayInfosChangedLocked(update.displayInfos);
Prabir Pradhan5693cee2021-12-31 06:51:15 -080055}
56
57void PointerController::DisplayInfoListener::onPointerControllerDestroyed() {
58 std::scoped_lock lock(mLock);
59 mPointerController = nullptr;
Prabir Pradhanf97fac32021-11-18 16:40:34 +000060}
61
Jeff Brownb4ff35d2011-01-02 16:37:43 -080062// --- PointerController ---
63
Michael Wrighta0bc6b12020-06-26 20:25:34 +010064std::shared_ptr<PointerController> PointerController::create(
65 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
66 const sp<SpriteController>& spriteController) {
Liam Harringtonc782be62020-07-17 19:48:24 +000067 // using 'new' to access non-public constructor
Michael Wrighta0bc6b12020-06-26 20:25:34 +010068 std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>(
69 new PointerController(policy, looper, spriteController));
Jeff Brown2352b972011-04-12 22:39:53 -070070
Michael Wrighta0bc6b12020-06-26 20:25:34 +010071 /*
72 * Now we need to hook up the constructed PointerController object to its callbacks.
73 *
74 * This must be executed after the constructor but before any other methods on PointerController
75 * in order to ensure that the fully constructed object is visible on the Looper thread, since
76 * that may be a different thread than where the PointerController is initially constructed.
77 *
78 * Unfortunately, this cannot be done as part of the constructor since we need to hand out
79 * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
80 */
Jeff Brown5541de92011-04-11 11:54:25 -070081
Liam Harringtonc782be62020-07-17 19:48:24 +000082 controller->mContext.setHandlerController(controller);
83 controller->mContext.setCallbackController(controller);
Michael Wrighta0bc6b12020-06-26 20:25:34 +010084 return controller;
85}
Jun Mukaic0c0ac32015-10-27 10:09:21 -070086
Michael Wrighta0bc6b12020-06-26 20:25:34 +010087PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
88 const sp<Looper>& looper,
89 const sp<SpriteController>& spriteController)
Prabir Pradhan5693cee2021-12-31 06:51:15 -080090 : PointerController(
91 policy, looper, spriteController,
92 [](const sp<android::gui::WindowInfosListener>& listener) {
93 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener);
94 },
95 [](const sp<android::gui::WindowInfosListener>& listener) {
96 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
97 }) {}
98
99PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
100 const sp<Looper>& looper,
101 const sp<SpriteController>& spriteController,
102 WindowListenerConsumer registerListener,
103 WindowListenerConsumer unregisterListener)
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000104 : mContext(policy, looper, spriteController, *this),
105 mCursorController(mContext),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800106 mDisplayInfoListener(new DisplayInfoListener(this)),
107 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
108 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000109 mLocked.presentation = Presentation::SPOT;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800110 registerListener(mDisplayInfoListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000111}
112
113PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800114 mDisplayInfoListener->onPointerControllerDestroyed();
115 mUnregisterWindowInfosListener(mDisplayInfoListener);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000116 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800117}
118
119std::mutex& PointerController::getLock() const {
120 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800121}
122
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000123std::optional<FloatRect> PointerController::getBounds() const {
124 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800125}
126
127void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000128 const int32_t displayId = mCursorController.getDisplayId();
129 vec2 transformed;
130 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800131 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000132 const auto& transform = getTransformForDisplayLocked(displayId);
133 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
134 }
135 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136}
137
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800138void PointerController::setPosition(float x, float y) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000139 const int32_t displayId = mCursorController.getDisplayId();
140 vec2 transformed;
141 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800142 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000143 const auto& transform = getTransformForDisplayLocked(displayId);
144 transformed = transform.transform(x, y);
145 }
146 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800147}
148
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000149FloatPoint PointerController::getPosition() const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000150 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000151 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000152 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800153 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000154 const auto& transform = getTransformForDisplayLocked(displayId);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000155 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000156 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800157}
158
Arthur Hungb9b32002018-12-18 17:39:43 +0800159int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000160 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800161}
162
Jeff Brown538881e2011-05-25 18:23:38 -0700163void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800164 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000165 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800166}
167
Jeff Brown538881e2011-05-25 18:23:38 -0700168void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800169 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000170 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800171}
172
Jeff Brown2352b972011-04-12 22:39:53 -0700173void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800174 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800175
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800176 if (mLocked.presentation == presentation) {
177 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700178 }
179
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800180 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700181
Liam Harringtonc782be62020-07-17 19:48:24 +0000182 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800183 return;
184 }
185
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900186 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
187 // For now, we support stylus hover using the mouse cursor implementation.
188 // TODO: Add proper support for stylus hover icons.
189 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
190
Liam Harringtonc782be62020-07-17 19:48:24 +0000191 mCursorController.getAdditionalMouseResources();
192 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800193 }
194}
195
Liam Harringtonc782be62020-07-17 19:48:24 +0000196void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
197 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800198 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000199 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
200 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
201
202 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
203 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
204
205 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
206 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
207 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
208
209 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
210 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
211 }
212
Liam Harringtonc782be62020-07-17 19:48:24 +0000213 auto it = mLocked.spotControllers.find(displayId);
214 if (it == mLocked.spotControllers.end()) {
215 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700216 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000217 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700218}
219
220void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800221 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000222 clearSpotsLocked();
223}
Jeff Brown2352b972011-04-12 22:39:53 -0700224
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800225void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100226 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000227 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800228 }
Jeff Brown2352b972011-04-12 22:39:53 -0700229}
230
231void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000232 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800233}
234
Jun Mukai19a56012015-11-24 11:25:52 -0800235void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800236 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800237
Michael Wright21401ad92022-10-22 03:23:55 +0100238 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000239 spotController.reloadSpotResources();
240 }
Jun Mukai19a56012015-11-24 11:25:52 -0800241
Liam Harringtonc782be62020-07-17 19:48:24 +0000242 if (mCursorController.resourcesLoaded()) {
243 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900244 if (mLocked.presentation == PointerController::Presentation::POINTER ||
245 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000246 getAdditionalMouseResources = true;
247 }
248 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800249 }
250}
251
252void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000253 struct PointerDisplayChangeArgs {
254 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000255 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000256 };
257 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000258
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000259 { // acquire lock
260 std::scoped_lock lock(getLock());
261
262 bool getAdditionalMouseResources = false;
263 if (mLocked.presentation == PointerController::Presentation::POINTER ||
264 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
265 getAdditionalMouseResources = true;
266 }
267 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
268 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000269 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000270 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000271 }
272 } // release lock
273
274 if (pointerDisplayChanged) {
275 // Notify the policy without holding the pointer controller lock.
276 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000277 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000278 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800279}
280
Brandon Pollack015f5d92022-06-02 06:59:33 +0000281void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800282 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000283 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700284}
285
Jun Mukaid4eaef72015-10-30 15:54:33 -0700286void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800287 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000288 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700289}
290
Jeff Brown2352b972011-04-12 22:39:53 -0700291void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100292 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800293}
294
Liam Harringtonc782be62020-07-17 19:48:24 +0000295void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
296 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000297 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000298 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800299 }
300
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800301 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000302 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100303 int32_t displayId = it->first;
304 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000305 /*
306 * Ensures that an in-progress animation won't dereference
307 * a null pointer to TouchSpotController.
308 */
Michael Wright21401ad92022-10-22 03:23:55 +0100309 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000310 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700311 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000312 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700313 }
314 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800315}
316
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800317void PointerController::onDisplayInfosChangedLocked(
318 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000319 mLocked.mDisplayInfos = displayInfo;
320}
321
322const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
323 const auto& di = mLocked.mDisplayInfos;
324 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
325 return info.displayId == displayId;
326 });
327 return it != di.end() ? it->transform : kIdentityTransform;
328}
329
Michael Wright72a89132022-10-22 03:16:31 +0100330void PointerController::dump(std::string& dump) {
331 dump += INDENT "PointerController:\n";
332 std::scoped_lock lock(getLock());
333 dump += StringPrintf(INDENT2 "Presentation: %s\n",
334 ftl::enum_string(mLocked.presentation).c_str());
335 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100336 dump += StringPrintf(INDENT2 "Viewports:\n");
337 for (const auto& info : mLocked.mDisplayInfos) {
338 info.dump(dump, INDENT3);
339 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100340 dump += INDENT2 "Spot Controllers:\n";
341 for (const auto& [_, spotController] : mLocked.spotControllers) {
342 spotController.dump(dump, INDENT3);
343 }
Michael Wright72a89132022-10-22 03:16:31 +0100344}
345
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800346} // namespace android