blob: 1dc74e5f7740d631b2ed744836fab4a0f0aa2c6a [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>
Prabir Pradhan5693cee2021-12-31 06:51:15 -080025#include <android-base/thread_annotations.h>
26
27#include "PointerControllerContext.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080028
29namespace android {
30
Prabir Pradhanf97fac32021-11-18 16:40:34 +000031namespace {
32
33const ui::Transform kIdentityTransform;
34
35} // namespace
36
37// --- PointerController::DisplayInfoListener ---
38
39void PointerController::DisplayInfoListener::onWindowInfosChanged(
40 const std::vector<android::gui::WindowInfo>&,
Prabir Pradhan5693cee2021-12-31 06:51:15 -080041 const std::vector<android::gui::DisplayInfo>& displayInfos) {
42 std::scoped_lock lock(mLock);
43 if (mPointerController == nullptr) return;
44
45 // PointerController uses DisplayInfoListener's lock.
46 base::ScopedLockAssertion assumeLocked(mPointerController->getLock());
47 mPointerController->onDisplayInfosChangedLocked(displayInfos);
48}
49
50void PointerController::DisplayInfoListener::onPointerControllerDestroyed() {
51 std::scoped_lock lock(mLock);
52 mPointerController = nullptr;
Prabir Pradhanf97fac32021-11-18 16:40:34 +000053}
54
Jeff Brownb4ff35d2011-01-02 16:37:43 -080055// --- PointerController ---
56
Michael Wrighta0bc6b12020-06-26 20:25:34 +010057std::shared_ptr<PointerController> PointerController::create(
58 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
59 const sp<SpriteController>& spriteController) {
Liam Harringtonc782be62020-07-17 19:48:24 +000060 // using 'new' to access non-public constructor
Michael Wrighta0bc6b12020-06-26 20:25:34 +010061 std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>(
62 new PointerController(policy, looper, spriteController));
Jeff Brown2352b972011-04-12 22:39:53 -070063
Michael Wrighta0bc6b12020-06-26 20:25:34 +010064 /*
65 * Now we need to hook up the constructed PointerController object to its callbacks.
66 *
67 * This must be executed after the constructor but before any other methods on PointerController
68 * in order to ensure that the fully constructed object is visible on the Looper thread, since
69 * that may be a different thread than where the PointerController is initially constructed.
70 *
71 * Unfortunately, this cannot be done as part of the constructor since we need to hand out
72 * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
73 */
Jeff Brown5541de92011-04-11 11:54:25 -070074
Liam Harringtonc782be62020-07-17 19:48:24 +000075 controller->mContext.setHandlerController(controller);
76 controller->mContext.setCallbackController(controller);
Michael Wrighta0bc6b12020-06-26 20:25:34 +010077 return controller;
78}
Jun Mukaic0c0ac32015-10-27 10:09:21 -070079
Michael Wrighta0bc6b12020-06-26 20:25:34 +010080PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
81 const sp<Looper>& looper,
82 const sp<SpriteController>& spriteController)
Prabir Pradhan5693cee2021-12-31 06:51:15 -080083 : PointerController(
84 policy, looper, spriteController,
85 [](const sp<android::gui::WindowInfosListener>& listener) {
86 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener);
87 },
88 [](const sp<android::gui::WindowInfosListener>& listener) {
89 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
90 }) {}
91
92PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
93 const sp<Looper>& looper,
94 const sp<SpriteController>& spriteController,
95 WindowListenerConsumer registerListener,
96 WindowListenerConsumer unregisterListener)
Prabir Pradhanf97fac32021-11-18 16:40:34 +000097 : mContext(policy, looper, spriteController, *this),
98 mCursorController(mContext),
Prabir Pradhan5693cee2021-12-31 06:51:15 -080099 mDisplayInfoListener(new DisplayInfoListener(this)),
100 mUnregisterWindowInfosListener(std::move(unregisterListener)) {
101 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000102 mLocked.presentation = Presentation::SPOT;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800103 registerListener(mDisplayInfoListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000104}
105
106PointerController::~PointerController() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800107 mDisplayInfoListener->onPointerControllerDestroyed();
108 mUnregisterWindowInfosListener(mDisplayInfoListener);
109}
110
111std::mutex& PointerController::getLock() const {
112 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800113}
114
Liam Harringtonc782be62020-07-17 19:48:24 +0000115bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
116 float* outMaxY) const {
117 return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800118}
119
120void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000121 const int32_t displayId = mCursorController.getDisplayId();
122 vec2 transformed;
123 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800124 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000125 const auto& transform = getTransformForDisplayLocked(displayId);
126 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
127 }
128 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800129}
130
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700131void PointerController::setButtonState(int32_t buttonState) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000132 mCursorController.setButtonState(buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800133}
134
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700135int32_t PointerController::getButtonState() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000136 return mCursorController.getButtonState();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800137}
138
139void 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
150void PointerController::getPosition(float* outX, float* outY) const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000151 const int32_t displayId = mCursorController.getDisplayId();
Liam Harringtonc782be62020-07-17 19:48:24 +0000152 mCursorController.getPosition(outX, outY);
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);
156 const auto xy = transform.inverse().transform(*outX, *outY);
157 *outX = xy.x;
158 *outY = xy.y;
159 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800160}
161
Arthur Hungb9b32002018-12-18 17:39:43 +0800162int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000163 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800164}
165
Jeff Brown538881e2011-05-25 18:23:38 -0700166void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800167 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000168 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800169}
170
Jeff Brown538881e2011-05-25 18:23:38 -0700171void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800172 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000173 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800174}
175
Jeff Brown2352b972011-04-12 22:39:53 -0700176void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800177 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800178
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800179 if (mLocked.presentation == presentation) {
180 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700181 }
182
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800183 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700184
Liam Harringtonc782be62020-07-17 19:48:24 +0000185 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800186 return;
187 }
188
Michael Wright6853fe62020-07-02 00:01:38 +0100189 if (presentation == Presentation::POINTER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000190 mCursorController.getAdditionalMouseResources();
191 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800192 }
193}
194
Liam Harringtonc782be62020-07-17 19:48:24 +0000195void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
196 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800197 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000198 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
199 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
200
201 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
202 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
203
204 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
205 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
206 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
207
208 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
209 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
210 }
211
Liam Harringtonc782be62020-07-17 19:48:24 +0000212 auto it = mLocked.spotControllers.find(displayId);
213 if (it == mLocked.spotControllers.end()) {
214 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700215 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000216 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700217}
218
219void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800220 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000221 clearSpotsLocked();
222}
Jeff Brown2352b972011-04-12 22:39:53 -0700223
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800224void PointerController::clearSpotsLocked() {
Liam Harringtonc782be62020-07-17 19:48:24 +0000225 for (auto& [displayID, spotController] : mLocked.spotControllers) {
226 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800227 }
Jeff Brown2352b972011-04-12 22:39:53 -0700228}
229
230void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000231 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800232}
233
Jun Mukai19a56012015-11-24 11:25:52 -0800234void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800235 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800236
Liam Harringtonc782be62020-07-17 19:48:24 +0000237 for (auto& [displayID, spotController] : mLocked.spotControllers) {
238 spotController.reloadSpotResources();
239 }
Jun Mukai19a56012015-11-24 11:25:52 -0800240
Liam Harringtonc782be62020-07-17 19:48:24 +0000241 if (mCursorController.resourcesLoaded()) {
242 bool getAdditionalMouseResources = false;
243 if (mLocked.presentation == PointerController::Presentation::POINTER) {
244 getAdditionalMouseResources = true;
245 }
246 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800247 }
248}
249
250void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800251 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000252
253 bool getAdditionalMouseResources = false;
254 if (mLocked.presentation == PointerController::Presentation::POINTER) {
255 getAdditionalMouseResources = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700256 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000257 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800258}
259
Michael Wrighte051f6f2016-05-13 17:44:16 +0100260void PointerController::updatePointerIcon(int32_t iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800261 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000262 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700263}
264
Jun Mukaid4eaef72015-10-30 15:54:33 -0700265void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800266 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000267 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700268}
269
Jeff Brown2352b972011-04-12 22:39:53 -0700270void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100271 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800272}
273
Liam Harringtonc782be62020-07-17 19:48:24 +0000274void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
275 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000276 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000277 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800278 }
279
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800280 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000281 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
282 int32_t displayID = it->first;
283 if (!displayIdSet.count(displayID)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000284 /*
285 * Ensures that an in-progress animation won't dereference
286 * a null pointer to TouchSpotController.
287 */
288 mContext.removeAnimationCallback(displayID);
Liam Harringtonc782be62020-07-17 19:48:24 +0000289 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700290 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000291 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700292 }
293 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800294}
295
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800296void PointerController::onDisplayInfosChangedLocked(
297 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000298 mLocked.mDisplayInfos = displayInfo;
299}
300
301const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
302 const auto& di = mLocked.mDisplayInfos;
303 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
304 return info.displayId == displayId;
305 });
306 return it != di.end() ? it->transform : kIdentityTransform;
307}
308
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800309} // namespace android