blob: 544edc2a716f5afcd7a8f8d1ae183d206f9d45e7 [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(
48 const std::vector<android::gui::WindowInfo>&,
Prabir Pradhan5693cee2021-12-31 06:51:15 -080049 const std::vector<android::gui::DisplayInfo>& displayInfos) {
50 std::scoped_lock lock(mLock);
51 if (mPointerController == nullptr) return;
52
53 // PointerController uses DisplayInfoListener's lock.
54 base::ScopedLockAssertion assumeLocked(mPointerController->getLock());
55 mPointerController->onDisplayInfosChangedLocked(displayInfos);
56}
57
58void PointerController::DisplayInfoListener::onPointerControllerDestroyed() {
59 std::scoped_lock lock(mLock);
60 mPointerController = nullptr;
Prabir Pradhanf97fac32021-11-18 16:40:34 +000061}
62
Jeff Brownb4ff35d2011-01-02 16:37:43 -080063// --- PointerController ---
64
Michael Wrighta0bc6b12020-06-26 20:25:34 +010065std::shared_ptr<PointerController> PointerController::create(
66 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
67 const sp<SpriteController>& spriteController) {
Liam Harringtonc782be62020-07-17 19:48:24 +000068 // using 'new' to access non-public constructor
Michael Wrighta0bc6b12020-06-26 20:25:34 +010069 std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>(
70 new PointerController(policy, looper, spriteController));
Jeff Brown2352b972011-04-12 22:39:53 -070071
Michael Wrighta0bc6b12020-06-26 20:25:34 +010072 /*
73 * Now we need to hook up the constructed PointerController object to its callbacks.
74 *
75 * This must be executed after the constructor but before any other methods on PointerController
76 * in order to ensure that the fully constructed object is visible on the Looper thread, since
77 * that may be a different thread than where the PointerController is initially constructed.
78 *
79 * Unfortunately, this cannot be done as part of the constructor since we need to hand out
80 * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
81 */
Jeff Brown5541de92011-04-11 11:54:25 -070082
Liam Harringtonc782be62020-07-17 19:48:24 +000083 controller->mContext.setHandlerController(controller);
84 controller->mContext.setCallbackController(controller);
Michael Wrighta0bc6b12020-06-26 20:25:34 +010085 return controller;
86}
Jun Mukaic0c0ac32015-10-27 10:09:21 -070087
Michael Wrighta0bc6b12020-06-26 20:25:34 +010088PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
89 const sp<Looper>& looper,
90 const sp<SpriteController>& spriteController)
Prabir Pradhan5693cee2021-12-31 06:51:15 -080091 : PointerController(
92 policy, looper, spriteController,
93 [](const sp<android::gui::WindowInfosListener>& listener) {
94 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener);
95 },
96 [](const sp<android::gui::WindowInfosListener>& listener) {
97 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
98 }) {}
99
100PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
101 const sp<Looper>& looper,
102 const sp<SpriteController>& spriteController,
103 WindowListenerConsumer registerListener,
104 WindowListenerConsumer unregisterListener)
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000105 : mContext(policy, looper, spriteController, *this),
106 mCursorController(mContext),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800107 mDisplayInfoListener(new DisplayInfoListener(this)),
108 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
109 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000110 mLocked.presentation = Presentation::SPOT;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800111 registerListener(mDisplayInfoListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000112}
113
114PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800115 mDisplayInfoListener->onPointerControllerDestroyed();
116 mUnregisterWindowInfosListener(mDisplayInfoListener);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000117 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800118}
119
120std::mutex& PointerController::getLock() const {
121 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800122}
123
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000124std::optional<FloatRect> PointerController::getBounds() const {
125 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800126}
127
128void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000129 const int32_t displayId = mCursorController.getDisplayId();
130 vec2 transformed;
131 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800132 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000133 const auto& transform = getTransformForDisplayLocked(displayId);
134 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
135 }
136 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800137}
138
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700139void PointerController::setButtonState(int32_t buttonState) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000140 mCursorController.setButtonState(buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800141}
142
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700143int32_t PointerController::getButtonState() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000144 return mCursorController.getButtonState();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145}
146
147void PointerController::setPosition(float x, float y) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000148 const int32_t displayId = mCursorController.getDisplayId();
149 vec2 transformed;
150 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800151 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000152 const auto& transform = getTransformForDisplayLocked(displayId);
153 transformed = transform.transform(x, y);
154 }
155 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800156}
157
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000158FloatPoint PointerController::getPosition() const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000159 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000160 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000161 {
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);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000164 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000165 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800166}
167
Arthur Hungb9b32002018-12-18 17:39:43 +0800168int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000169 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800170}
171
Jeff Brown538881e2011-05-25 18:23:38 -0700172void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800173 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000174 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800175}
176
Jeff Brown538881e2011-05-25 18:23:38 -0700177void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800178 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000179 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800180}
181
Jeff Brown2352b972011-04-12 22:39:53 -0700182void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800183 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800184
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800185 if (mLocked.presentation == presentation) {
186 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700187 }
188
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800189 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700190
Liam Harringtonc782be62020-07-17 19:48:24 +0000191 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800192 return;
193 }
194
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900195 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
196 // For now, we support stylus hover using the mouse cursor implementation.
197 // TODO: Add proper support for stylus hover icons.
198 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
199
Liam Harringtonc782be62020-07-17 19:48:24 +0000200 mCursorController.getAdditionalMouseResources();
201 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800202 }
203}
204
Liam Harringtonc782be62020-07-17 19:48:24 +0000205void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
206 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800207 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000208 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
209 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
210
211 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
212 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
213
214 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
215 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
216 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
217
218 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
219 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
220 }
221
Liam Harringtonc782be62020-07-17 19:48:24 +0000222 auto it = mLocked.spotControllers.find(displayId);
223 if (it == mLocked.spotControllers.end()) {
224 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700225 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000226 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700227}
228
229void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800230 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000231 clearSpotsLocked();
232}
Jeff Brown2352b972011-04-12 22:39:53 -0700233
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800234void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100235 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000236 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800237 }
Jeff Brown2352b972011-04-12 22:39:53 -0700238}
239
240void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000241 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800242}
243
Jun Mukai19a56012015-11-24 11:25:52 -0800244void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800245 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800246
Michael Wright21401ad92022-10-22 03:23:55 +0100247 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000248 spotController.reloadSpotResources();
249 }
Jun Mukai19a56012015-11-24 11:25:52 -0800250
Liam Harringtonc782be62020-07-17 19:48:24 +0000251 if (mCursorController.resourcesLoaded()) {
252 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900253 if (mLocked.presentation == PointerController::Presentation::POINTER ||
254 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000255 getAdditionalMouseResources = true;
256 }
257 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800258 }
259}
260
261void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000262 struct PointerDisplayChangeArgs {
263 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000264 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000265 };
266 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000267
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000268 { // acquire lock
269 std::scoped_lock lock(getLock());
270
271 bool getAdditionalMouseResources = false;
272 if (mLocked.presentation == PointerController::Presentation::POINTER ||
273 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
274 getAdditionalMouseResources = true;
275 }
276 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
277 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000278 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000279 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000280 }
281 } // release lock
282
283 if (pointerDisplayChanged) {
284 // Notify the policy without holding the pointer controller lock.
285 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000286 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000287 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800288}
289
Brandon Pollack015f5d92022-06-02 06:59:33 +0000290void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800291 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000292 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700293}
294
Jun Mukaid4eaef72015-10-30 15:54:33 -0700295void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800296 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000297 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700298}
299
Jeff Brown2352b972011-04-12 22:39:53 -0700300void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100301 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800302}
303
Liam Harringtonc782be62020-07-17 19:48:24 +0000304void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
305 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000306 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000307 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800308 }
309
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800310 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000311 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100312 int32_t displayId = it->first;
313 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000314 /*
315 * Ensures that an in-progress animation won't dereference
316 * a null pointer to TouchSpotController.
317 */
Michael Wright21401ad92022-10-22 03:23:55 +0100318 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000319 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700320 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000321 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700322 }
323 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800324}
325
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800326void PointerController::onDisplayInfosChangedLocked(
327 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000328 mLocked.mDisplayInfos = displayInfo;
329}
330
331const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
332 const auto& di = mLocked.mDisplayInfos;
333 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
334 return info.displayId == displayId;
335 });
336 return it != di.end() ? it->transform : kIdentityTransform;
337}
338
Michael Wright72a89132022-10-22 03:16:31 +0100339void PointerController::dump(std::string& dump) {
340 dump += INDENT "PointerController:\n";
341 std::scoped_lock lock(getLock());
342 dump += StringPrintf(INDENT2 "Presentation: %s\n",
343 ftl::enum_string(mLocked.presentation).c_str());
344 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100345 dump += StringPrintf(INDENT2 "Viewports:\n");
346 for (const auto& info : mLocked.mDisplayInfos) {
347 info.dump(dump, INDENT3);
348 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100349 dump += INDENT2 "Spot Controllers:\n";
350 for (const auto& [_, spotController] : mLocked.spotControllers) {
351 spotController.dump(dump, INDENT3);
352 }
Michael Wright72a89132022-10-22 03:16:31 +0100353}
354
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800355} // namespace android