blob: e748c692743d44baab62d79c4bb6f431cdc0fa18 [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 Pradhan0e3d6652022-03-10 14:39:46 +0000117 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, 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
Liam Harringtonc782be62020-07-17 19:48:24 +0000124bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
125 float* outMaxY) const {
126 return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800127}
128
129void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000130 const int32_t displayId = mCursorController.getDisplayId();
131 vec2 transformed;
132 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800133 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000134 const auto& transform = getTransformForDisplayLocked(displayId);
135 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
136 }
137 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800138}
139
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700140void PointerController::setButtonState(int32_t buttonState) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000141 mCursorController.setButtonState(buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800142}
143
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700144int32_t PointerController::getButtonState() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000145 return mCursorController.getButtonState();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800146}
147
148void PointerController::setPosition(float x, float y) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000149 const int32_t displayId = mCursorController.getDisplayId();
150 vec2 transformed;
151 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800152 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000153 const auto& transform = getTransformForDisplayLocked(displayId);
154 transformed = transform.transform(x, y);
155 }
156 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800157}
158
159void PointerController::getPosition(float* outX, float* outY) const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000160 const int32_t displayId = mCursorController.getDisplayId();
Liam Harringtonc782be62020-07-17 19:48:24 +0000161 mCursorController.getPosition(outX, outY);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000162 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800163 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000164 const auto& transform = getTransformForDisplayLocked(displayId);
165 const auto xy = transform.inverse().transform(*outX, *outY);
166 *outX = xy.x;
167 *outY = xy.y;
168 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800169}
170
Arthur Hungb9b32002018-12-18 17:39:43 +0800171int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000172 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800173}
174
Jeff Brown538881e2011-05-25 18:23:38 -0700175void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800176 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000177 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800178}
179
Jeff Brown538881e2011-05-25 18:23:38 -0700180void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800181 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000182 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800183}
184
Jeff Brown2352b972011-04-12 22:39:53 -0700185void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800186 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800187
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800188 if (mLocked.presentation == presentation) {
189 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700190 }
191
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800192 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700193
Liam Harringtonc782be62020-07-17 19:48:24 +0000194 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800195 return;
196 }
197
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900198 if (presentation == Presentation::POINTER || presentation == Presentation::STYLUS_HOVER) {
199 // For now, we support stylus hover using the mouse cursor implementation.
200 // TODO: Add proper support for stylus hover icons.
201 mCursorController.setStylusHoverMode(presentation == Presentation::STYLUS_HOVER);
202
Liam Harringtonc782be62020-07-17 19:48:24 +0000203 mCursorController.getAdditionalMouseResources();
204 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800205 }
206}
207
Liam Harringtonc782be62020-07-17 19:48:24 +0000208void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
209 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800210 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000211 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
212 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
213
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
215 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
216
217 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
218 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
219 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
220
221 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
222 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
223 }
224
Liam Harringtonc782be62020-07-17 19:48:24 +0000225 auto it = mLocked.spotControllers.find(displayId);
226 if (it == mLocked.spotControllers.end()) {
227 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700228 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000229 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700230}
231
232void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800233 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000234 clearSpotsLocked();
235}
Jeff Brown2352b972011-04-12 22:39:53 -0700236
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800237void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100238 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000239 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800240 }
Jeff Brown2352b972011-04-12 22:39:53 -0700241}
242
243void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000244 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800245}
246
Jun Mukai19a56012015-11-24 11:25:52 -0800247void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800248 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800249
Michael Wright21401ad92022-10-22 03:23:55 +0100250 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000251 spotController.reloadSpotResources();
252 }
Jun Mukai19a56012015-11-24 11:25:52 -0800253
Liam Harringtonc782be62020-07-17 19:48:24 +0000254 if (mCursorController.resourcesLoaded()) {
255 bool getAdditionalMouseResources = false;
Seunghwan Choi670b33d2023-01-13 21:12:59 +0900256 if (mLocked.presentation == PointerController::Presentation::POINTER ||
257 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000258 getAdditionalMouseResources = true;
259 }
260 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800261 }
262}
263
264void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000265 struct PointerDisplayChangeArgs {
266 int32_t displayId;
267 float x, y;
268 };
269 std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;
Liam Harringtonc782be62020-07-17 19:48:24 +0000270
Prabir Pradhan692bbdb2023-02-24 01:52:13 +0000271 { // acquire lock
272 std::scoped_lock lock(getLock());
273
274 bool getAdditionalMouseResources = false;
275 if (mLocked.presentation == PointerController::Presentation::POINTER ||
276 mLocked.presentation == PointerController::Presentation::STYLUS_HOVER) {
277 getAdditionalMouseResources = true;
278 }
279 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
280 if (viewport.displayId != mLocked.pointerDisplayId) {
281 float xPos, yPos;
282 mCursorController.getPosition(&xPos, &yPos);
283 mLocked.pointerDisplayId = viewport.displayId;
284 pointerDisplayChanged = {viewport.displayId, xPos, yPos};
285 }
286 } // release lock
287
288 if (pointerDisplayChanged) {
289 // Notify the policy without holding the pointer controller lock.
290 mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
291 pointerDisplayChanged->x,
292 pointerDisplayChanged->y);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000293 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800294}
295
Brandon Pollack015f5d92022-06-02 06:59:33 +0000296void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800297 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000298 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700299}
300
Jun Mukaid4eaef72015-10-30 15:54:33 -0700301void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800302 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000303 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700304}
305
Jeff Brown2352b972011-04-12 22:39:53 -0700306void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100307 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800308}
309
Liam Harringtonc782be62020-07-17 19:48:24 +0000310void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
311 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000312 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000313 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800314 }
315
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800316 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000317 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100318 int32_t displayId = it->first;
319 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000320 /*
321 * Ensures that an in-progress animation won't dereference
322 * a null pointer to TouchSpotController.
323 */
Michael Wright21401ad92022-10-22 03:23:55 +0100324 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000325 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700326 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000327 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700328 }
329 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800330}
331
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800332void PointerController::onDisplayInfosChangedLocked(
333 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000334 mLocked.mDisplayInfos = displayInfo;
335}
336
337const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
338 const auto& di = mLocked.mDisplayInfos;
339 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
340 return info.displayId == displayId;
341 });
342 return it != di.end() ? it->transform : kIdentityTransform;
343}
344
Michael Wright72a89132022-10-22 03:16:31 +0100345void PointerController::dump(std::string& dump) {
346 dump += INDENT "PointerController:\n";
347 std::scoped_lock lock(getLock());
348 dump += StringPrintf(INDENT2 "Presentation: %s\n",
349 ftl::enum_string(mLocked.presentation).c_str());
350 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100351 dump += StringPrintf(INDENT2 "Viewports:\n");
352 for (const auto& info : mLocked.mDisplayInfos) {
353 info.dump(dump, INDENT3);
354 }
Michael Wright20f5fd82022-10-28 14:12:24 +0100355 dump += INDENT2 "Spot Controllers:\n";
356 for (const auto& [_, spotController] : mLocked.spotControllers) {
357 spotController.dump(dump, INDENT3);
358 }
Michael Wright72a89132022-10-22 03:16:31 +0100359}
360
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800361} // namespace android