blob: 88e351963148609b7a89115ae3e2a98e3004f846 [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 Brownb4ff35d2011-01-02 16:37:43 -0800139void PointerController::setPosition(float x, float y) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000140 const int32_t displayId = mCursorController.getDisplayId();
141 vec2 transformed;
142 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800143 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000144 const auto& transform = getTransformForDisplayLocked(displayId);
145 transformed = transform.transform(x, y);
146 }
147 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800148}
149
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000150FloatPoint PointerController::getPosition() const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000151 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000152 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000153 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800154 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000155 const auto& transform = getTransformForDisplayLocked(displayId);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000156 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000157 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800158}
159
Arthur Hungb9b32002018-12-18 17:39:43 +0800160int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000161 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800162}
163
Jeff Brown538881e2011-05-25 18:23:38 -0700164void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800165 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000166 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800167}
168
Jeff Brown538881e2011-05-25 18:23:38 -0700169void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800170 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000171 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800172}
173
Jeff Brown2352b972011-04-12 22:39:53 -0700174void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800175 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800176
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800177 if (mLocked.presentation == presentation) {
178 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700179 }
180
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800181 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700182
Liam Harringtonc782be62020-07-17 19:48:24 +0000183 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800184 return;
185 }
186
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900187 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
188 // For now, we support stylus hover using the mouse cursor implementation.
189 // TODO: Add proper support for stylus hover icons.
190 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
191
Liam Harringtonc782be62020-07-17 19:48:24 +0000192 mCursorController.getAdditionalMouseResources();
193 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800194 }
195}
196
Liam Harringtonc782be62020-07-17 19:48:24 +0000197void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
198 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800199 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000200 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
201 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
202
203 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
204 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
205
206 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
207 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
208 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
209
210 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
211 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
212 }
213
Liam Harringtonc782be62020-07-17 19:48:24 +0000214 auto it = mLocked.spotControllers.find(displayId);
215 if (it == mLocked.spotControllers.end()) {
216 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700217 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000218 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700219}
220
221void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800222 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000223 clearSpotsLocked();
224}
Jeff Brown2352b972011-04-12 22:39:53 -0700225
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800226void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100227 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000228 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800229 }
Jeff Brown2352b972011-04-12 22:39:53 -0700230}
231
232void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000233 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800234}
235
Jun Mukai19a56012015-11-24 11:25:52 -0800236void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800237 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800238
Michael Wright21401ad92022-10-22 03:23:55 +0100239 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000240 spotController.reloadSpotResources();
241 }
Jun Mukai19a56012015-11-24 11:25:52 -0800242
Liam Harringtonc782be62020-07-17 19:48:24 +0000243 if (mCursorController.resourcesLoaded()) {
244 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900245 if (mLocked.presentation == PointerController::Presentation::POINTER ||
246 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000247 getAdditionalMouseResources = true;
248 }
249 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800250 }
251}
252
253void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000254 struct PointerDisplayChangeArgs {
255 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000256 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000257 };
258 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000259
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000260 { // acquire lock
261 std::scoped_lock lock(getLock());
262
263 bool getAdditionalMouseResources = false;
264 if (mLocked.presentation == PointerController::Presentation::POINTER ||
265 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
266 getAdditionalMouseResources = true;
267 }
268 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
269 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000270 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000271 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000272 }
273 } // release lock
274
275 if (pointerDisplayChanged) {
276 // Notify the policy without holding the pointer controller lock.
277 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000278 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000279 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800280}
281
Brandon Pollack015f5d92022-06-02 06:59:33 +0000282void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800283 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000284 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700285}
286
Jun Mukaid4eaef72015-10-30 15:54:33 -0700287void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800288 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000289 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700290}
291
Jeff Brown2352b972011-04-12 22:39:53 -0700292void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100293 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800294}
295
Liam Harringtonc782be62020-07-17 19:48:24 +0000296void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
297 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000298 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000299 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800300 }
301
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800302 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000303 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100304 int32_t displayId = it->first;
305 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000306 /*
307 * Ensures that an in-progress animation won't dereference
308 * a null pointer to TouchSpotController.
309 */
Michael Wright21401ad92022-10-22 03:23:55 +0100310 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000311 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700312 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000313 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700314 }
315 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800316}
317
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800318void PointerController::onDisplayInfosChangedLocked(
319 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000320 mLocked.mDisplayInfos = displayInfo;
321}
322
323const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
324 const auto& di = mLocked.mDisplayInfos;
325 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
326 return info.displayId == displayId;
327 });
328 return it != di.end() ? it->transform : kIdentityTransform;
329}
330
Michael Wright72a89132022-10-22 03:16:31 +0100331void PointerController::dump(std::string& dump) {
332 dump += INDENT "PointerController:\n";
333 std::scoped_lock lock(getLock());
334 dump += StringPrintf(INDENT2 "Presentation: %s\n",
335 ftl::enum_string(mLocked.presentation).c_str());
336 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100337 dump += StringPrintf(INDENT2 "Viewports:\n");
338 for (const auto& info : mLocked.mDisplayInfos) {
339 info.dump(dump, INDENT3);
340 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100341 dump += INDENT2 "Spot Controllers:\n";
342 for (const auto& [_, spotController] : mLocked.spotControllers) {
343 spotController.dump(dump, INDENT3);
344 }
Michael Wright72a89132022-10-22 03:16:31 +0100345}
346
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800347} // namespace android