blob: 54f893e165f7c9f096d4d49f8b5305df01cd0746 [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);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000109 mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, 0, 0);
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800110}
111
112std::mutex& PointerController::getLock() const {
113 return mDisplayInfoListener->mLock;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800114}
115
Liam Harringtonc782be62020-07-17 19:48:24 +0000116bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
117 float* outMaxY) const {
118 return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800119}
120
121void PointerController::move(float deltaX, float deltaY) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000122 const int32_t displayId = mCursorController.getDisplayId();
123 vec2 transformed;
124 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800125 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000126 const auto& transform = getTransformForDisplayLocked(displayId);
127 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
128 }
129 mCursorController.move(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800130}
131
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700132void PointerController::setButtonState(int32_t buttonState) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000133 mCursorController.setButtonState(buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800134}
135
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700136int32_t PointerController::getButtonState() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000137 return mCursorController.getButtonState();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800138}
139
140void PointerController::setPosition(float x, float y) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000141 const int32_t displayId = mCursorController.getDisplayId();
142 vec2 transformed;
143 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800144 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000145 const auto& transform = getTransformForDisplayLocked(displayId);
146 transformed = transform.transform(x, y);
147 }
148 mCursorController.setPosition(transformed.x, transformed.y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800149}
150
151void PointerController::getPosition(float* outX, float* outY) const {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000152 const int32_t displayId = mCursorController.getDisplayId();
Liam Harringtonc782be62020-07-17 19:48:24 +0000153 mCursorController.getPosition(outX, outY);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000154 {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800155 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000156 const auto& transform = getTransformForDisplayLocked(displayId);
157 const auto xy = transform.inverse().transform(*outX, *outY);
158 *outX = xy.x;
159 *outY = xy.y;
160 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800161}
162
Arthur Hungb9b32002018-12-18 17:39:43 +0800163int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +0000164 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800165}
166
Jeff Brown538881e2011-05-25 18:23:38 -0700167void PointerController::fade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800168 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000169 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800170}
171
Jeff Brown538881e2011-05-25 18:23:38 -0700172void PointerController::unfade(Transition transition) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800173 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000174 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800175}
176
Jeff Brown2352b972011-04-12 22:39:53 -0700177void PointerController::setPresentation(Presentation presentation) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800178 std::scoped_lock lock(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800179
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800180 if (mLocked.presentation == presentation) {
181 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700182 }
183
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800184 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700185
Liam Harringtonc782be62020-07-17 19:48:24 +0000186 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800187 return;
188 }
189
Michael Wright6853fe62020-07-02 00:01:38 +0100190 if (presentation == Presentation::POINTER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000191 mCursorController.getAdditionalMouseResources();
192 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800193 }
194}
195
Liam Harringtonc782be62020-07-17 19:48:24 +0000196void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
197 BitSet32 spotIdBits, int32_t displayId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800198 std::scoped_lock lock(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000199 std::array<PointerCoords, MAX_POINTERS> outSpotCoords{};
200 const ui::Transform& transform = getTransformForDisplayLocked(displayId);
201
202 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
203 const uint32_t index = spotIdToIndex[idBits.clearFirstMarkedBit()];
204
205 const vec2 xy = transform.transform(spotCoords[index].getXYValue());
206 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
207 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
208
209 float pressure = spotCoords[index].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
210 outSpotCoords[index].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
211 }
212
Liam Harringtonc782be62020-07-17 19:48:24 +0000213 auto it = mLocked.spotControllers.find(displayId);
214 if (it == mLocked.spotControllers.end()) {
215 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700216 }
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000217 mLocked.spotControllers.at(displayId).setSpots(outSpotCoords.data(), spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700218}
219
220void PointerController::clearSpots() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800221 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000222 clearSpotsLocked();
223}
Jeff Brown2352b972011-04-12 22:39:53 -0700224
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800225void PointerController::clearSpotsLocked() {
Liam Harringtonc782be62020-07-17 19:48:24 +0000226 for (auto& [displayID, spotController] : mLocked.spotControllers) {
227 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800228 }
Jeff Brown2352b972011-04-12 22:39:53 -0700229}
230
231void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000232 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800233}
234
Jun Mukai19a56012015-11-24 11:25:52 -0800235void PointerController::reloadPointerResources() {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800236 std::scoped_lock lock(getLock());
Jun Mukai19a56012015-11-24 11:25:52 -0800237
Liam Harringtonc782be62020-07-17 19:48:24 +0000238 for (auto& [displayID, spotController] : mLocked.spotControllers) {
239 spotController.reloadSpotResources();
240 }
Jun Mukai19a56012015-11-24 11:25:52 -0800241
Liam Harringtonc782be62020-07-17 19:48:24 +0000242 if (mCursorController.resourcesLoaded()) {
243 bool getAdditionalMouseResources = false;
244 if (mLocked.presentation == PointerController::Presentation::POINTER) {
245 getAdditionalMouseResources = true;
246 }
247 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800248 }
249}
250
251void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800252 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000253
254 bool getAdditionalMouseResources = false;
255 if (mLocked.presentation == PointerController::Presentation::POINTER) {
256 getAdditionalMouseResources = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700257 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000258 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000259 if (viewport.displayId != mLocked.pointerDisplayId) {
260 float xPos, yPos;
261 mCursorController.getPosition(&xPos, &yPos);
262 mContext.getPolicy()->onPointerDisplayIdChanged(viewport.displayId, xPos, yPos);
263 mLocked.pointerDisplayId = viewport.displayId;
264 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800265}
266
Brandon Pollack015f5d92022-06-02 06:59:33 +0000267void PointerController::updatePointerIcon(PointerIconStyle iconId) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800268 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000269 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700270}
271
Jun Mukaid4eaef72015-10-30 15:54:33 -0700272void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800273 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000274 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700275}
276
Jeff Brown2352b972011-04-12 22:39:53 -0700277void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100278 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800279}
280
Liam Harringtonc782be62020-07-17 19:48:24 +0000281void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
282 std::unordered_set<int32_t> displayIdSet;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000283 for (const DisplayViewport& viewport : viewports) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000284 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800285 }
286
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800287 std::scoped_lock lock(getLock());
Liam Harringtonc782be62020-07-17 19:48:24 +0000288 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
289 int32_t displayID = it->first;
290 if (!displayIdSet.count(displayID)) {
Liam Harringtonce637132020-08-14 04:00:11 +0000291 /*
292 * Ensures that an in-progress animation won't dereference
293 * a null pointer to TouchSpotController.
294 */
295 mContext.removeAnimationCallback(displayID);
Liam Harringtonc782be62020-07-17 19:48:24 +0000296 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700297 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000298 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700299 }
300 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800301}
302
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800303void PointerController::onDisplayInfosChangedLocked(
304 const std::vector<gui::DisplayInfo>& displayInfo) {
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000305 mLocked.mDisplayInfos = displayInfo;
306}
307
308const ui::Transform& PointerController::getTransformForDisplayLocked(int displayId) const {
309 const auto& di = mLocked.mDisplayInfos;
310 auto it = std::find_if(di.begin(), di.end(), [displayId](const gui::DisplayInfo& info) {
311 return info.displayId == displayId;
312 });
313 return it != di.end() ? it->transform : kIdentityTransform;
314}
315
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800316} // namespace android