blob: 1f80ba9baa68f61ed6e7b4d32c50accb0635b289 [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
Michael Wright6853fe62020-07-02 00:01:38 +0100198 if (presentation == Presentation::POINTER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000199 mCursorController.getAdditionalMouseResources();
200 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800201 }
202}
203
Liam Harringtonc782be62020-07-17 19:48:24 +0000204void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
205 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800206 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000207 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
208 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
209
210 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
211 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
212
213 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
214 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
215 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
216
217 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
218 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
219 }
220
Liam Harringtonc782be62020-07-17 19:48:24 +0000221 auto it = mLocked.spotControllers.find(displayId);
222 if (it == mLocked.spotControllers.end()) {
223 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700224 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000225 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700226}
227
228void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800229 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000230 clearSpotsLocked();
231}
Jeff Brown2352b972011-04-12 22:39:53 -0700232
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800233void PointerController::clearSpotsLocked() {
Michael Wright21401ad92022-10-22 03:23:55 +0100234 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000235 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800236 }
Jeff Brown2352b972011-04-12 22:39:53 -0700237}
238
239void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000240 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800241}
242
Jun Mukai19a56012015-11-24 11:25:52 -0800243void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800244 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800245
Michael Wright21401ad92022-10-22 03:23:55 +0100246 for (auto& [displayId, spotController] : mLocked.spotControllers) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000247 spotController.reloadSpotResources();
248 }
Jun Mukai19a56012015-11-24 11:25:52 -0800249
Liam Harringtonc782be62020-07-17 19:48:24 +0000250 if (mCursorController.resourcesLoaded()) {
251 bool getAdditionalMouseResources = false;
252 if (mLocked.presentation == PointerController::Presentation::POINTER) {
253 getAdditionalMouseResources = true;
254 }
255 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800256 }
257}
258
259void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800260 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000261
262 bool getAdditionalMouseResources = false;
263 if (mLocked.presentation == PointerController::Presentation::POINTER) {
264 getAdditionalMouseResources = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700265 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000266 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000267 if (viewport.displayId != mLocked.pointerDisplayId) {
268 float xPos, yPos;
269 mCursorController.getPosition(&xPos, &yPos);
270 mContext.getPolicy()->onPointerDisplayIdChanged(viewport.displayId, xPos, yPos);
271 mLocked.pointerDisplayId = viewport.displayId;
272 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800273}
274
Brandon Pollack015f5d92022-06-02 06:59:33 +0000275void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800276 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000277 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700278}
279
Jun Mukaid4eaef72015-10-30 15:54:33 -0700280void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800281 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000282 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700283}
284
Jeff Brown2352b972011-04-12 22:39:53 -0700285void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100286 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800287}
288
Liam Harringtonc782be62020-07-17 19:48:24 +0000289void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
290 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000291 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000292 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800293 }
294
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800295 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000296 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
Michael Wright21401ad92022-10-22 03:23:55 +0100297 int32_t displayId = it->first;
298 if (!displayIdSet.count(displayId)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000299 /*
300 * Ensures that an in-progress animation won't dereference
301 * a null pointer to TouchSpotController.
302 */
Michael Wright21401ad92022-10-22 03:23:55 +0100303 mContext.removeAnimationCallback(displayId);
Liam Harringtonc782be62020-07-17 19:48:24 +0000304 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700305 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000306 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700307 }
308 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800309}
310
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800311void PointerController::onDisplayInfosChangedLocked(
312 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000313 mLocked.mDisplayInfos = displayInfo;
314}
315
316const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
317 const auto& di = mLocked.mDisplayInfos;
318 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
319 return info.displayId == displayId;
320 });
321 return it != di.end() ? it->transform : kIdentityTransform;
322}
323
Michael Wright72a89132022-10-22 03:16:31 +0100324void PointerController::dump(std::string& dump) {
325 dump += INDENT "PointerController:\n";
326 std::scoped_lock lock(getLock());
327 dump += StringPrintf(INDENT2 "Presentation: %s\n",
328 ftl::enum_string(mLocked.presentation).c_str());
329 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
Michael Wright83577752022-10-28 16:24:33 +0100330 dump += StringPrintf(INDENT2 "Viewports:\n");
331 for (const auto& info : mLocked.mDisplayInfos) {
332 info.dump(dump, INDENT3);
333 }
Michael Wright72a89132022-10-22 03:16:31 +0100334}
335
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800336} // namespace android