blob: 42606e3ee4dfce6675349515aa748ecdd5b3057e [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 " "
35
Jeff Brownb4ff35d2011-01-02 16:37:43 -080036namespace android {
37
Prabir Pradhanf97fac32021-11-18 16:40:34 +000038namespace {
39
40const ui::Transform kIdentityTransform;
41
42} // namespace
43
44// --- PointerController::DisplayInfoListener ---
45
46void PointerController::DisplayInfoListener::onWindowInfosChanged(
47 const std::vector<android::gui::WindowInfo>&,
Prabir Pradhan5693cee2021-12-31 06:51:15 -080048 const std::vector<android::gui::DisplayInfo>& displayInfos) {
49 std::scoped_lock lock(mLock);
50 if (mPointerController == nullptr) return;
51
52 // PointerController uses DisplayInfoListener's lock.
53 base::ScopedLockAssertion assumeLocked(mPointerController->getLock());
54 mPointerController->onDisplayInfosChangedLocked(displayInfos);
55}
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,
66 const sp<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,
88 const sp<Looper>& looper,
89 const sp<SpriteController>& spriteController)
Prabir Pradhan5693cee2021-12-31 06:51:15 -080090 : PointerController(
91 policy, looper, spriteController,
92 [](const sp<android::gui::WindowInfosListener>& listener) {
93 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener);
94 },
95 [](const sp<android::gui::WindowInfosListener>& listener) {
96 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
97 }) {}
98
99PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
100 const sp<Looper>& looper,
101 const sp<SpriteController>& spriteController,
102 WindowListenerConsumer registerListener,
103 WindowListenerConsumer unregisterListener)
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000104 : mContext(policy, looper, spriteController, *this),
105 mCursorController(mContext),
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800106 mDisplayInfoListener(new DisplayInfoListener(this)),
107 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
108 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000109 mLocked.presentation = Presentation::SPOT;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800110 registerListener(mDisplayInfoListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000111}
112
113PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800114 mDisplayInfoListener->onPointerControllerDestroyed();
115 mUnregisterWindowInfosListener(mDisplayInfoListener);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000116 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, 0, 0);
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800117}
118
119std::mutex& PointerController::getLock() const {
120 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800121}
122
Liam Harringtonc782be62020-07-17 19:48:24 +0000123bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
124 float* outMaxY) const {
125 return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
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
158void PointerController::getPosition(float* outX, float* outY) const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000159 const int32_t displayId = mCursorController.getDisplayId();
Liam Harringtonc782be62020-07-17 19:48:24 +0000160 mCursorController.getPosition(outX, outY);
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);
164 const auto xy = transform.inverse().transform(*outX, *outY);
165 *outX = xy.x;
166 *outY = xy.y;
167 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800168}
169
Arthur Hungb9b32002018-12-18 17:39:43 +0800170int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000171 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800172}
173
Jeff Brown538881e2011-05-25 18:23:38 -0700174void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800175 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000176 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800177}
178
Jeff Brown538881e2011-05-25 18:23:38 -0700179void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800180 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000181 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800182}
183
Jeff Brown2352b972011-04-12 22:39:53 -0700184void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800185 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800186
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800187 if (mLocked.presentation == presentation) {
188 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700189 }
190
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800191 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700192
Liam Harringtonc782be62020-07-17 19:48:24 +0000193 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800194 return;
195 }
196
Michael Wright6853fe62020-07-02 00:01:38 +0100197 if (presentation == Presentation::POINTER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000198 mCursorController.getAdditionalMouseResources();
199 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800200 }
201}
202
Liam Harringtonc782be62020-07-17 19:48:24 +0000203void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
204 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800205 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000206 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
207 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
208
209 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
210 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
211
212 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
213 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
214 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
215
216 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
217 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
218 }
219
Liam Harringtonc782be62020-07-17 19:48:24 +0000220 auto it = mLocked.spotControllers.find(displayId);
221 if (it == mLocked.spotControllers.end()) {
222 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700223 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000224 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700225}
226
227void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800228 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000229 clearSpotsLocked();
230}
Jeff Brown2352b972011-04-12 22:39:53 -0700231
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800232void PointerController::clearSpotsLocked() {
Liam Harringtonc782be62020-07-17 19:48:24 +0000233 for (auto& [displayID, spotController] : mLocked.spotControllers) {
234 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800235 }
Jeff Brown2352b972011-04-12 22:39:53 -0700236}
237
238void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000239 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800240}
241
Jun Mukai19a56012015-11-24 11:25:52 -0800242void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800243 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800244
Liam Harringtonc782be62020-07-17 19:48:24 +0000245 for (auto& [displayID, spotController] : mLocked.spotControllers) {
246 spotController.reloadSpotResources();
247 }
Jun Mukai19a56012015-11-24 11:25:52 -0800248
Liam Harringtonc782be62020-07-17 19:48:24 +0000249 if (mCursorController.resourcesLoaded()) {
250 bool getAdditionalMouseResources = false;
251 if (mLocked.presentation == PointerController::Presentation::POINTER) {
252 getAdditionalMouseResources = true;
253 }
254 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800255 }
256}
257
258void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800259 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000260
261 bool getAdditionalMouseResources = false;
262 if (mLocked.presentation == PointerController::Presentation::POINTER) {
263 getAdditionalMouseResources = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700264 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000265 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000266 if (viewport.displayId != mLocked.pointerDisplayId) {
267 float xPos, yPos;
268 mCursorController.getPosition(&xPos, &yPos);
269 mContext.getPolicy()->onPointerDisplayIdChanged(viewport.displayId, xPos, yPos);
270 mLocked.pointerDisplayId = viewport.displayId;
271 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800272}
273
Brandon Pollack015f5d92022-06-02 06:59:33 +0000274void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800275 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000276 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700277}
278
Jun Mukaid4eaef72015-10-30 15:54:33 -0700279void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800280 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000281 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700282}
283
Jeff Brown2352b972011-04-12 22:39:53 -0700284void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100285 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800286}
287
Liam Harringtonc782be62020-07-17 19:48:24 +0000288void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
289 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000290 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000291 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800292 }
293
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800294 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000295 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
296 int32_t displayID = it->first;
297 if (!displayIdSet.count(displayID)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000298 /*
299 * Ensures that an in-progress animation won't dereference
300 * a null pointer to TouchSpotController.
301 */
302 mContext.removeAnimationCallback(displayID);
Liam Harringtonc782be62020-07-17 19:48:24 +0000303 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700304 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000305 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700306 }
307 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800308}
309
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800310void PointerController::onDisplayInfosChangedLocked(
311 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000312 mLocked.mDisplayInfos = displayInfo;
313}
314
315const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
316 const auto& di = mLocked.mDisplayInfos;
317 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
318 return info.displayId == displayId;
319 });
320 return it != di.end() ? it->transform : kIdentityTransform;
321}
322
Michael Wright72a89132022-10-22 03:16:31 +0100323void PointerController::dump(std::string& dump) {
324 dump += INDENT "PointerController:\n";
325 std::scoped_lock lock(getLock());
326 dump += StringPrintf(INDENT2 "Presentation: %s\n",
327 ftl::enum_string(mLocked.presentation).c_str());
328 dump += StringPrintf(INDENT2 "Pointer Display ID: %" PRIu32 "\n", mLocked.pointerDisplayId);
329}
330
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800331} // namespace android