blob: 435452c1137038122649b6608d45b1ab893e6585 [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,
Prabir Pradhan27c6d992023-08-18 19:44:55 +000066 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,
Prabir Pradhan27c6d992023-08-18 19:44:55 +000088 const sp<Looper>& looper, SpriteController& spriteController)
Prabir Pradhan5693cee2021-12-31 06:51:15 -080089 : PointerController(
90 policy, looper, spriteController,
91 [](const sp<android::gui::WindowInfosListener>& listener) {
92 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener);
93 },
94 [](const sp<android::gui::WindowInfosListener>& listener) {
95 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
96 }) {}
97
98PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan27c6d992023-08-18 19:44:55 +000099 const sp<Looper>& looper, SpriteController& spriteController,
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800100 WindowListenerConsumer registerListener,
101 WindowListenerConsumer unregisterListener)
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000102 : mContext(policy, looper, spriteController, *this),
103 mCursorController(mContext),
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000104 mDisplayInfoListener(sp<DisplayInfoListener>::make(this)),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800105 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
106 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000107 mLocked.presentation = Presentation::SPOT;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800108 registerListener(mDisplayInfoListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000109}
110
111PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800112 mDisplayInfoListener->onPointerControllerDestroyed();
113 mUnregisterWindowInfosListener(mDisplayInfoListener);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000114 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800115}
116
117std::mutex& PointerController::getLock() const {
118 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800119}
120
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000121std::optional<FloatRect> PointerController::getBounds() const {
122 return mCursorController.getBounds();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800123}
124
125void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000126 const int32_t displayId = mCursorController.getDisplayId();
127 vec2 transformed;
128 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800129 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000130 const auto& transform = getTransformForDisplayLocked(displayId);
131 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
132 }
133 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800134}
135
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136void PointerController::setPosition(float x, float y) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000137 const int32_t displayId = mCursorController.getDisplayId();
138 vec2 transformed;
139 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800140 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000141 const auto& transform = getTransformForDisplayLocked(displayId);
142 transformed = transform.transform(x, y);
143 }
144 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145}
146
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000147FloatPoint PointerController::getPosition() const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000148 const int32_t displayId = mCursorController.getDisplayId();
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000149 const auto p = mCursorController.getPosition();
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000150 {
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);
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000153 return FloatPoint{transform.inverse().transform(p.x, p.y)};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000154 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800155}
156
Arthur Hungb9b32002018-12-18 17:39:43 +0800157int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000158 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800159}
160
Jeff Brown538881e2011-05-25 18:23:38 -0700161void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800162 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000163 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800164}
165
Jeff Brown538881e2011-05-25 18:23:38 -0700166void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800167 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000168 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800169}
170
Jeff Brown2352b972011-04-12 22:39:53 -0700171void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800172 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800173
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800174 if (mLocked.presentation == presentation) {
175 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700176 }
177
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800178 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700179
Liam Harringtonc782be62020-07-17 19:48:24 +0000180 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800181 return;
182 }
183
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900184 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
185 // For now, we support stylus hover using the mouse cursor implementation.
186 // TODO: Add proper support for stylus hover icons.
187 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
188
Liam Harringtonc782be62020-07-17 19:48:24 +0000189 mCursorController.getAdditionalMouseResources();
190 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800191 }
192}
193
Liam Harringtonc782be62020-07-17 19:48:24 +0000194void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
195 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800196 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000197 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
198 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
199
200 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
201 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
202
203 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
204 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
205 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
206
207 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
208 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
209 }
210
Liam Harringtonc782be62020-07-17 19:48:24 +0000211 auto it = mLocked.spotControllers.find(displayId);
212 if (it == mLocked.spotControllers.end()) {
213 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700214 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000215 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700216}
217
218void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800219 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000220 clearSpotsLocked();
221}
Jeff Brown2352b972011-04-12 22:39:53 -0700222
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800223void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100224 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000225 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800226 }
Jeff Brown2352b972011-04-12 22:39:53 -0700227}
228
229void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000230 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800231}
232
Jun Mukai19a56012015-11-24 11:25:52 -0800233void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800234 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800235
Michael Wright21401ad92022-10-22 03:23:55 +0100236 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000237 spotController.reloadSpotResources();
238 }
Jun Mukai19a56012015-11-24 11:25:52 -0800239
Liam Harringtonc782be62020-07-17 19:48:24 +0000240 if (mCursorController.resourcesLoaded()) {
241 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900242 if (mLocked.presentation == PointerController::Presentation::POINTER ||
243 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000244 getAdditionalMouseResources = true;
245 }
246 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800247 }
248}
249
250void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000251 struct PointerDisplayChangeArgs {
252 int32_t displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000253 FloatPoint cursorPosition;
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000254 };
255 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000256
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000257 { // acquire lock
258 std::scoped_lock lock(getLock());
259
260 bool getAdditionalMouseResources = false;
261 if (mLocked.presentation == PointerController::Presentation::POINTER ||
262 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
263 getAdditionalMouseResources = true;
264 }
265 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
266 if (viewport.displayId != mLocked.pointerDisplayId) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000267 mLocked.pointerDisplayId = viewport.displayId;
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000268 pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000269 }
270 } // release lock
271
272 if (pointerDisplayChanged) {
273 // Notify the policy without holding the pointer controller lock.
274 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
Prabir Pradhanb5dadec2023-02-28 17:43:09 +0000275 pointerDisplayChanged->cursorPosition);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000276 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800277}
278
Brandon Pollack015f5d92022-06-02 06:59:33 +0000279void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800280 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000281 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700282}
283
Jun Mukaid4eaef72015-10-30 15:54:33 -0700284void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800285 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000286 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700287}
288
Jeff Brown2352b972011-04-12 22:39:53 -0700289void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100290 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800291}
292
Liam Harringtonc782be62020-07-17 19:48:24 +0000293void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
294 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000295 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000296 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800297 }
298
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800299 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000300 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100301 int32_t displayId = it->first;
302 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000303 /*
304 * Ensures that an in-progress animation won't dereference
305 * a null pointer to TouchSpotController.
306 */
Michael Wright21401ad92022-10-22 03:23:55 +0100307 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000308 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700309 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000310 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700311 }
312 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800313}
314
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800315void PointerController::onDisplayInfosChangedLocked(
316 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000317 mLocked.mDisplayInfos = displayInfo;
318}
319
320const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
321 const auto& di = mLocked.mDisplayInfos;
322 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
323 return info.displayId == displayId;
324 });
325 return it != di.end() ? it->transform : kIdentityTransform;
326}
327
Michael Wright72a89132022-10-22 03:16:31 +0100328void PointerController::dump(std::string& dump) {
329 dump += INDENT "PointerController:\n";
330 std::scoped_lock lock(getLock());
331 dump += StringPrintf(INDENT2 "Presentation: %s\n",
332 ftl::enum_string(mLocked.presentation).c_str());
333 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100334 dump += StringPrintf(INDENT2 "Viewports:\n");
335 for (const auto& info : mLocked.mDisplayInfos) {
336 info.dump(dump, INDENT3);
337 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100338 dump += INDENT2 "Spot Controllers:\n";
339 for (const auto& [_, spotController] : mLocked.spotControllers) {
340 spotController.dump(dump, INDENT3);
341 }
Michael Wright72a89132022-10-22 03:16:31 +0100342}
343
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800344} // namespace android