Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | // Log debug messages about pointer updates |
| 21 | #define DEBUG_POINTER_UPDATES 0 |
| 22 | |
| 23 | #include "PointerController.h" |
| 24 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 25 | #include <log/log.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 26 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 27 | #include <memory> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // --- PointerController --- |
| 32 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 33 | // Time to wait before starting the fade when the pointer is inactive. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 34 | static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds |
| 35 | static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds |
| 36 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 37 | // Time to spend fading out the spot completely. |
| 38 | static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 39 | |
| 40 | // Time to spend fading out the pointer completely. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 41 | static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 42 | |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 43 | // The number of events to be read at once for DisplayEventReceiver. |
| 44 | static const int EVENT_BUFFER_SIZE = 100; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 45 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 46 | std::shared_ptr<PointerController> PointerController::create( |
| 47 | const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
| 48 | const sp<SpriteController>& spriteController) { |
| 49 | std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>( |
| 50 | new PointerController(policy, looper, spriteController)); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 51 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 52 | /* |
| 53 | * Now we need to hook up the constructed PointerController object to its callbacks. |
| 54 | * |
| 55 | * This must be executed after the constructor but before any other methods on PointerController |
| 56 | * in order to ensure that the fully constructed object is visible on the Looper thread, since |
| 57 | * that may be a different thread than where the PointerController is initially constructed. |
| 58 | * |
| 59 | * Unfortunately, this cannot be done as part of the constructor since we need to hand out |
| 60 | * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr. |
| 61 | */ |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 62 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 63 | controller->mHandler->pointerController = controller; |
| 64 | controller->mCallback->pointerController = controller; |
| 65 | if (controller->mDisplayEventReceiver.initCheck() == NO_ERROR) { |
| 66 | controller->mLooper->addFd(controller->mDisplayEventReceiver.getFd(), Looper::POLL_CALLBACK, |
| 67 | Looper::EVENT_INPUT, controller->mCallback, nullptr); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 68 | } else { |
| 69 | ALOGE("Failed to initialize DisplayEventReceiver."); |
| 70 | } |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 71 | return controller; |
| 72 | } |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 73 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 74 | PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 75 | const sp<Looper>& looper, |
| 76 | const sp<SpriteController>& spriteController) |
| 77 | : mPolicy(policy), |
| 78 | mLooper(looper), |
| 79 | mSpriteController(spriteController), |
| 80 | mHandler(new MessageHandler()), |
| 81 | mCallback(new LooperCallback()) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 82 | AutoMutex _l(mLock); |
| 83 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 84 | mLocked.animationPending = false; |
| 85 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 86 | mLocked.presentation = PRESENTATION_POINTER; |
| 87 | mLocked.presentationChanged = false; |
| 88 | |
| 89 | mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL; |
| 90 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 91 | mLocked.pointerFadeDirection = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 92 | mLocked.pointerX = 0; |
| 93 | mLocked.pointerY = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 94 | mLocked.pointerAlpha = 0.0f; // pointer is initially faded |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 95 | mLocked.pointerSprite = mSpriteController->createSprite(); |
| 96 | mLocked.pointerIconChanged = false; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 97 | mLocked.requestedPointerType = mPolicy->getDefaultPointerIconId(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 98 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 99 | mLocked.animationFrameIndex = 0; |
| 100 | mLocked.lastFrameUpdatedTime = 0; |
| 101 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 102 | mLocked.buttonState = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | PointerController::~PointerController() { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 106 | mLooper->removeMessages(mHandler); |
| 107 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 108 | AutoMutex _l(mLock); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 109 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 110 | mLocked.pointerSprite.clear(); |
| 111 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 112 | for (auto& it : mLocked.spotsByDisplay) { |
| 113 | const std::vector<Spot*>& spots = it.second; |
| 114 | size_t numSpots = spots.size(); |
| 115 | for (size_t i = 0; i < numSpots; i++) { |
| 116 | delete spots[i]; |
| 117 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 118 | } |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 119 | mLocked.spotsByDisplay.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 120 | mLocked.recycledSprites.clear(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool PointerController::getBounds(float* outMinX, float* outMinY, |
| 124 | float* outMaxX, float* outMaxY) const { |
| 125 | AutoMutex _l(mLock); |
| 126 | |
| 127 | return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY); |
| 128 | } |
| 129 | |
| 130 | bool PointerController::getBoundsLocked(float* outMinX, float* outMinY, |
| 131 | float* outMaxX, float* outMaxY) const { |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 132 | |
| 133 | if (!mLocked.viewport.isValid()) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 137 | *outMinX = mLocked.viewport.logicalLeft; |
| 138 | *outMinY = mLocked.viewport.logicalTop; |
| 139 | *outMaxX = mLocked.viewport.logicalRight - 1; |
| 140 | *outMaxY = mLocked.viewport.logicalBottom - 1; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
| 144 | void PointerController::move(float deltaX, float deltaY) { |
| 145 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 146 | ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 147 | #endif |
| 148 | if (deltaX == 0.0f && deltaY == 0.0f) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | AutoMutex _l(mLock); |
| 153 | |
| 154 | setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY); |
| 155 | } |
| 156 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 157 | void PointerController::setButtonState(int32_t buttonState) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 158 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 159 | ALOGD("Set button state 0x%08x", buttonState); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 160 | #endif |
| 161 | AutoMutex _l(mLock); |
| 162 | |
| 163 | if (mLocked.buttonState != buttonState) { |
| 164 | mLocked.buttonState = buttonState; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 168 | int32_t PointerController::getButtonState() const { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 169 | AutoMutex _l(mLock); |
| 170 | |
| 171 | return mLocked.buttonState; |
| 172 | } |
| 173 | |
| 174 | void PointerController::setPosition(float x, float y) { |
| 175 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 176 | ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 177 | #endif |
| 178 | AutoMutex _l(mLock); |
| 179 | |
| 180 | setPositionLocked(x, y); |
| 181 | } |
| 182 | |
| 183 | void PointerController::setPositionLocked(float x, float y) { |
| 184 | float minX, minY, maxX, maxY; |
| 185 | if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { |
| 186 | if (x <= minX) { |
| 187 | mLocked.pointerX = minX; |
| 188 | } else if (x >= maxX) { |
| 189 | mLocked.pointerX = maxX; |
| 190 | } else { |
| 191 | mLocked.pointerX = x; |
| 192 | } |
| 193 | if (y <= minY) { |
| 194 | mLocked.pointerY = minY; |
| 195 | } else if (y >= maxY) { |
| 196 | mLocked.pointerY = maxY; |
| 197 | } else { |
| 198 | mLocked.pointerY = y; |
| 199 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 200 | updatePointerLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | void PointerController::getPosition(float* outX, float* outY) const { |
| 205 | AutoMutex _l(mLock); |
| 206 | |
| 207 | *outX = mLocked.pointerX; |
| 208 | *outY = mLocked.pointerY; |
| 209 | } |
| 210 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 211 | int32_t PointerController::getDisplayId() const { |
| 212 | AutoMutex _l(mLock); |
| 213 | |
| 214 | return mLocked.viewport.displayId; |
| 215 | } |
| 216 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 217 | void PointerController::fade(Transition transition) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 218 | AutoMutex _l(mLock); |
| 219 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 220 | // Remove the inactivity timeout, since we are fading now. |
| 221 | removeInactivityTimeoutLocked(); |
| 222 | |
| 223 | // Start fading. |
| 224 | if (transition == TRANSITION_IMMEDIATE) { |
| 225 | mLocked.pointerFadeDirection = 0; |
| 226 | mLocked.pointerAlpha = 0.0f; |
| 227 | updatePointerLocked(); |
| 228 | } else { |
| 229 | mLocked.pointerFadeDirection = -1; |
| 230 | startAnimationLocked(); |
| 231 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 234 | void PointerController::unfade(Transition transition) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 235 | AutoMutex _l(mLock); |
| 236 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 237 | // Always reset the inactivity timer. |
| 238 | resetInactivityTimeoutLocked(); |
| 239 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 240 | // Start unfading. |
| 241 | if (transition == TRANSITION_IMMEDIATE) { |
| 242 | mLocked.pointerFadeDirection = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 243 | mLocked.pointerAlpha = 1.0f; |
| 244 | updatePointerLocked(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 245 | } else { |
| 246 | mLocked.pointerFadeDirection = 1; |
| 247 | startAnimationLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 251 | void PointerController::setPresentation(Presentation presentation) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 252 | AutoMutex _l(mLock); |
| 253 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 254 | if (mLocked.presentation == presentation) { |
| 255 | return; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 258 | mLocked.presentation = presentation; |
| 259 | mLocked.presentationChanged = true; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 260 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 261 | if (!mLocked.viewport.isValid()) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (presentation == PRESENTATION_POINTER) { |
| 266 | if (mLocked.additionalMouseResources.empty()) { |
| 267 | mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources, |
| 268 | &mLocked.animationResources, |
| 269 | mLocked.viewport.displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 270 | } |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 271 | fadeOutAndReleaseAllSpotsLocked(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 272 | updatePointerLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 276 | void PointerController::setSpots(const PointerCoords* spotCoords, |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 277 | const uint32_t* spotIdToIndex, BitSet32 spotIdBits, int32_t displayId) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 278 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 279 | ALOGD("setSpots: idBits=%08x", spotIdBits.value); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 280 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) { |
| 281 | uint32_t id = idBits.firstMarkedBit(); |
| 282 | idBits.clearBit(id); |
| 283 | const PointerCoords& c = spotCoords[spotIdToIndex[id]]; |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 284 | ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f, displayId=%" PRId32 ".", id, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 285 | c.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 286 | c.getAxisValue(AMOTION_EVENT_AXIS_Y), |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 287 | c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 288 | displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 289 | } |
| 290 | #endif |
| 291 | |
| 292 | AutoMutex _l(mLock); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 293 | if (!mLocked.viewport.isValid()) { |
| 294 | return; |
| 295 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 296 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 297 | std::vector<Spot*> newSpots; |
| 298 | std::map<int32_t, std::vector<Spot*>>::const_iterator iter = |
| 299 | mLocked.spotsByDisplay.find(displayId); |
| 300 | if (iter != mLocked.spotsByDisplay.end()) { |
| 301 | newSpots = iter->second; |
| 302 | } |
| 303 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 304 | mSpriteController->openTransaction(); |
| 305 | |
| 306 | // Add or move spots for fingers that are down. |
| 307 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 308 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 309 | const PointerCoords& c = spotCoords[spotIdToIndex[id]]; |
| 310 | const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0 |
| 311 | ? mResources.spotTouch : mResources.spotHover; |
| 312 | float x = c.getAxisValue(AMOTION_EVENT_AXIS_X); |
| 313 | float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 314 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 315 | Spot* spot = getSpot(id, newSpots); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 316 | if (!spot) { |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 317 | spot = createAndAddSpotLocked(id, newSpots); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 320 | spot->updateSprite(&icon, x, y, displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Remove spots for fingers that went up. |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 324 | for (size_t i = 0; i < newSpots.size(); i++) { |
| 325 | Spot* spot = newSpots[i]; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 326 | if (spot->id != Spot::INVALID_ID |
| 327 | && !spotIdBits.hasBit(spot->id)) { |
| 328 | fadeOutAndReleaseSpotLocked(spot); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | mSpriteController->closeTransaction(); |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 333 | mLocked.spotsByDisplay[displayId] = newSpots; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void PointerController::clearSpots() { |
| 337 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 338 | ALOGD("clearSpots"); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 339 | #endif |
| 340 | |
| 341 | AutoMutex _l(mLock); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 342 | if (!mLocked.viewport.isValid()) { |
| 343 | return; |
| 344 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 345 | |
| 346 | fadeOutAndReleaseAllSpotsLocked(); |
| 347 | } |
| 348 | |
| 349 | void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) { |
| 350 | AutoMutex _l(mLock); |
| 351 | |
| 352 | if (mLocked.inactivityTimeout != inactivityTimeout) { |
| 353 | mLocked.inactivityTimeout = inactivityTimeout; |
| 354 | resetInactivityTimeoutLocked(); |
| 355 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 358 | void PointerController::reloadPointerResources() { |
| 359 | AutoMutex _l(mLock); |
| 360 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 361 | loadResourcesLocked(); |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 362 | updatePointerLocked(); |
| 363 | } |
| 364 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 365 | /** |
| 366 | * The viewport values for deviceHeight and deviceWidth have already been adjusted for rotation, |
| 367 | * so here we are getting the dimensions in the original, unrotated orientation (orientation 0). |
| 368 | */ |
| 369 | static void getNonRotatedSize(const DisplayViewport& viewport, int32_t& width, int32_t& height) { |
| 370 | width = viewport.deviceWidth; |
| 371 | height = viewport.deviceHeight; |
Andrii Kulian | d44026f | 2018-12-17 18:59:36 +0000 | [diff] [blame] | 372 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 373 | if (viewport.orientation == DISPLAY_ORIENTATION_90 |
| 374 | || viewport.orientation == DISPLAY_ORIENTATION_270) { |
| 375 | std::swap(width, height); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void PointerController::setDisplayViewport(const DisplayViewport& viewport) { |
| 380 | AutoMutex _l(mLock); |
| 381 | if (viewport == mLocked.viewport) { |
| 382 | return; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 385 | const DisplayViewport oldViewport = mLocked.viewport; |
| 386 | mLocked.viewport = viewport; |
| 387 | |
| 388 | int32_t oldDisplayWidth, oldDisplayHeight; |
| 389 | getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight); |
| 390 | int32_t newDisplayWidth, newDisplayHeight; |
| 391 | getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight); |
| 392 | |
| 393 | // Reset cursor position to center if size or display changed. |
| 394 | if (oldViewport.displayId != viewport.displayId |
| 395 | || oldDisplayWidth != newDisplayWidth |
| 396 | || oldDisplayHeight != newDisplayHeight) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 397 | |
| 398 | float minX, minY, maxX, maxY; |
| 399 | if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { |
| 400 | mLocked.pointerX = (minX + maxX) * 0.5f; |
| 401 | mLocked.pointerY = (minY + maxY) * 0.5f; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 402 | // Reload icon resources for density may be changed. |
| 403 | loadResourcesLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 404 | } else { |
| 405 | mLocked.pointerX = 0; |
| 406 | mLocked.pointerY = 0; |
| 407 | } |
| 408 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 409 | fadeOutAndReleaseAllSpotsLocked(); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 410 | } else if (oldViewport.orientation != viewport.orientation) { |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 411 | // Apply offsets to convert from the pixel top-left corner position to the pixel center. |
| 412 | // This creates an invariant frame of reference that we can easily rotate when |
| 413 | // taking into account that the pointer may be located at fractional pixel offsets. |
| 414 | float x = mLocked.pointerX + 0.5f; |
| 415 | float y = mLocked.pointerY + 0.5f; |
| 416 | float temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 417 | |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 418 | // Undo the previous rotation. |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 419 | switch (oldViewport.orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 420 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 421 | temp = x; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 422 | x = oldViewport.deviceHeight - y; |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 423 | y = temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 424 | break; |
| 425 | case DISPLAY_ORIENTATION_180: |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 426 | x = oldViewport.deviceWidth - x; |
| 427 | y = oldViewport.deviceHeight - y; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 428 | break; |
| 429 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 430 | temp = x; |
| 431 | x = y; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 432 | y = oldViewport.deviceWidth - temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 436 | // Perform the new rotation. |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 437 | switch (viewport.orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 438 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 439 | temp = x; |
| 440 | x = y; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 441 | y = viewport.deviceHeight - temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 442 | break; |
| 443 | case DISPLAY_ORIENTATION_180: |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 444 | x = viewport.deviceWidth - x; |
| 445 | y = viewport.deviceHeight - y; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 446 | break; |
| 447 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 448 | temp = x; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 449 | x = viewport.deviceWidth - y; |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 450 | y = temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 454 | // Apply offsets to convert from the pixel center to the pixel top-left corner position |
| 455 | // and save the results. |
| 456 | mLocked.pointerX = x - 0.5f; |
| 457 | mLocked.pointerY = y - 0.5f; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 458 | } |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 459 | |
| 460 | updatePointerLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 463 | void PointerController::updatePointerIcon(int32_t iconId) { |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 464 | AutoMutex _l(mLock); |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 465 | if (mLocked.requestedPointerType != iconId) { |
| 466 | mLocked.requestedPointerType = iconId; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 467 | mLocked.presentationChanged = true; |
| 468 | updatePointerLocked(); |
| 469 | } |
| 470 | } |
| 471 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 472 | void PointerController::setCustomPointerIcon(const SpriteIcon& icon) { |
| 473 | AutoMutex _l(mLock); |
| 474 | |
| 475 | const int32_t iconId = mPolicy->getCustomPointerIconId(); |
| 476 | mLocked.additionalMouseResources[iconId] = icon; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 477 | mLocked.requestedPointerType = iconId; |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 478 | mLocked.presentationChanged = true; |
| 479 | |
| 480 | updatePointerLocked(); |
| 481 | } |
| 482 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 483 | void PointerController::MessageHandler::handleMessage(const Message& message) { |
| 484 | std::shared_ptr<PointerController> controller = pointerController.lock(); |
| 485 | |
| 486 | if (controller == nullptr) { |
| 487 | ALOGE("PointerController instance was released before processing message: what=%d", |
| 488 | message.what); |
| 489 | return; |
| 490 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 491 | switch (message.what) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 492 | case MSG_INACTIVITY_TIMEOUT: |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 493 | controller->doInactivityTimeout(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 494 | break; |
| 495 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 498 | int PointerController::LooperCallback::handleEvent(int /* fd */, int events, void* /* data */) { |
| 499 | std::shared_ptr<PointerController> controller = pointerController.lock(); |
| 500 | if (controller == nullptr) { |
| 501 | ALOGW("PointerController instance was released with pending callbacks. events=0x%x", |
| 502 | events); |
| 503 | return 0; // Remove the callback, the PointerController is gone anyways |
| 504 | } |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 505 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 506 | ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x", events); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 507 | return 0; // remove the callback |
| 508 | } |
| 509 | |
| 510 | if (!(events & Looper::EVENT_INPUT)) { |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 511 | ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 512 | return 1; // keep the callback |
| 513 | } |
| 514 | |
| 515 | bool gotVsync = false; |
| 516 | ssize_t n; |
| 517 | nsecs_t timestamp; |
| 518 | DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 519 | while ((n = controller->mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 520 | for (size_t i = 0; i < static_cast<size_t>(n); ++i) { |
| 521 | if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) { |
| 522 | timestamp = buf[i].header.timestamp; |
| 523 | gotVsync = true; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | if (gotVsync) { |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 528 | controller->doAnimate(timestamp); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 529 | } |
| 530 | return 1; // keep the callback |
| 531 | } |
| 532 | |
| 533 | void PointerController::doAnimate(nsecs_t timestamp) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 534 | AutoMutex _l(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 535 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 536 | mLocked.animationPending = false; |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 537 | |
| 538 | bool keepFading = doFadingAnimationLocked(timestamp); |
| 539 | bool keepBitmapFlipping = doBitmapAnimationLocked(timestamp); |
| 540 | if (keepFading || keepBitmapFlipping) { |
| 541 | startAnimationLocked(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | bool PointerController::doFadingAnimationLocked(nsecs_t timestamp) { |
| 546 | bool keepAnimating = false; |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 547 | nsecs_t frameDelay = timestamp - mLocked.animationTime; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 548 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 549 | // Animate pointer fade. |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 550 | if (mLocked.pointerFadeDirection < 0) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 551 | mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 552 | if (mLocked.pointerAlpha <= 0.0f) { |
| 553 | mLocked.pointerAlpha = 0.0f; |
| 554 | mLocked.pointerFadeDirection = 0; |
| 555 | } else { |
| 556 | keepAnimating = true; |
| 557 | } |
| 558 | updatePointerLocked(); |
| 559 | } else if (mLocked.pointerFadeDirection > 0) { |
| 560 | mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION; |
| 561 | if (mLocked.pointerAlpha >= 1.0f) { |
| 562 | mLocked.pointerAlpha = 1.0f; |
| 563 | mLocked.pointerFadeDirection = 0; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 564 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 565 | keepAnimating = true; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 566 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 567 | updatePointerLocked(); |
| 568 | } |
| 569 | |
| 570 | // Animate spots that are fading out and being removed. |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 571 | for(auto it = mLocked.spotsByDisplay.begin(); it != mLocked.spotsByDisplay.end();) { |
| 572 | std::vector<Spot*>& spots = it->second; |
| 573 | size_t numSpots = spots.size(); |
| 574 | for (size_t i = 0; i < numSpots;) { |
| 575 | Spot* spot = spots[i]; |
| 576 | if (spot->id == Spot::INVALID_ID) { |
| 577 | spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION; |
| 578 | if (spot->alpha <= 0) { |
| 579 | spots.erase(spots.begin() + i); |
| 580 | releaseSpotLocked(spot); |
| 581 | numSpots--; |
| 582 | continue; |
| 583 | } else { |
| 584 | spot->sprite->setAlpha(spot->alpha); |
| 585 | keepAnimating = true; |
| 586 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 587 | } |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 588 | ++i; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 589 | } |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 590 | |
| 591 | if (spots.size() == 0) { |
| 592 | it = mLocked.spotsByDisplay.erase(it); |
| 593 | } else { |
| 594 | ++it; |
| 595 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 596 | } |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 597 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 598 | return keepAnimating; |
| 599 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 600 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 601 | bool PointerController::doBitmapAnimationLocked(nsecs_t timestamp) { |
| 602 | std::map<int32_t, PointerAnimation>::const_iterator iter = mLocked.animationResources.find( |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 603 | mLocked.requestedPointerType); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 604 | if (iter == mLocked.animationResources.end()) { |
| 605 | return false; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 606 | } |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 607 | |
| 608 | if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) { |
| 609 | mSpriteController->openTransaction(); |
| 610 | |
| 611 | int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame; |
| 612 | mLocked.animationFrameIndex += incr; |
| 613 | mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr; |
| 614 | while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) { |
| 615 | mLocked.animationFrameIndex -= iter->second.animationFrames.size(); |
| 616 | } |
| 617 | mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]); |
| 618 | |
| 619 | mSpriteController->closeTransaction(); |
| 620 | } |
| 621 | |
| 622 | // Keep animating. |
| 623 | return true; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 626 | void PointerController::doInactivityTimeout() { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 627 | fade(TRANSITION_GRADUAL); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 630 | void PointerController::startAnimationLocked() { |
| 631 | if (!mLocked.animationPending) { |
| 632 | mLocked.animationPending = true; |
| 633 | mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 634 | mDisplayEventReceiver.requestNextVsync(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 635 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 638 | void PointerController::resetInactivityTimeoutLocked() { |
| 639 | mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT); |
| 640 | |
| 641 | nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT |
| 642 | ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL; |
| 643 | mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT); |
| 644 | } |
| 645 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 646 | void PointerController::removeInactivityTimeoutLocked() { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 647 | mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 650 | void PointerController::updatePointerLocked() REQUIRES(mLock) { |
| 651 | if (!mLocked.viewport.isValid()) { |
| 652 | return; |
| 653 | } |
| 654 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 655 | mSpriteController->openTransaction(); |
| 656 | |
| 657 | mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER); |
| 658 | mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 659 | mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 660 | |
| 661 | if (mLocked.pointerAlpha > 0) { |
| 662 | mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha); |
| 663 | mLocked.pointerSprite->setVisible(true); |
| 664 | } else { |
| 665 | mLocked.pointerSprite->setVisible(false); |
| 666 | } |
| 667 | |
| 668 | if (mLocked.pointerIconChanged || mLocked.presentationChanged) { |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 669 | if (mLocked.presentation == PRESENTATION_POINTER) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 670 | if (mLocked.requestedPointerType == mPolicy->getDefaultPointerIconId()) { |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 671 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 672 | } else { |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 673 | std::map<int32_t, SpriteIcon>::const_iterator iter = |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 674 | mLocked.additionalMouseResources.find(mLocked.requestedPointerType); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 675 | if (iter != mLocked.additionalMouseResources.end()) { |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 676 | std::map<int32_t, PointerAnimation>::const_iterator anim_iter = |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 677 | mLocked.animationResources.find(mLocked.requestedPointerType); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 678 | if (anim_iter != mLocked.animationResources.end()) { |
| 679 | mLocked.animationFrameIndex = 0; |
| 680 | mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 681 | startAnimationLocked(); |
| 682 | } |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 683 | mLocked.pointerSprite->setIcon(iter->second); |
| 684 | } else { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 685 | ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 686 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 687 | } |
| 688 | } |
| 689 | } else { |
| 690 | mLocked.pointerSprite->setIcon(mResources.spotAnchor); |
| 691 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 692 | mLocked.pointerIconChanged = false; |
| 693 | mLocked.presentationChanged = false; |
| 694 | } |
| 695 | |
| 696 | mSpriteController->closeTransaction(); |
| 697 | } |
| 698 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 699 | PointerController::Spot* PointerController::getSpot(uint32_t id, const std::vector<Spot*>& spots) { |
| 700 | for (size_t i = 0; i < spots.size(); i++) { |
| 701 | Spot* spot = spots[i]; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 702 | if (spot->id == id) { |
| 703 | return spot; |
| 704 | } |
| 705 | } |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 706 | |
| 707 | return nullptr; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 710 | PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id, |
| 711 | std::vector<Spot*>& spots) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 712 | // Remove spots until we have fewer than MAX_SPOTS remaining. |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 713 | while (spots.size() >= MAX_SPOTS) { |
| 714 | Spot* spot = removeFirstFadingSpotLocked(spots); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 715 | if (!spot) { |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 716 | spot = spots[0]; |
| 717 | spots.erase(spots.begin()); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 718 | } |
| 719 | releaseSpotLocked(spot); |
| 720 | } |
| 721 | |
| 722 | // Obtain a sprite from the recycled pool. |
| 723 | sp<Sprite> sprite; |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 724 | if (! mLocked.recycledSprites.empty()) { |
| 725 | sprite = mLocked.recycledSprites.back(); |
| 726 | mLocked.recycledSprites.pop_back(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 727 | } else { |
| 728 | sprite = mSpriteController->createSprite(); |
| 729 | } |
| 730 | |
| 731 | // Return the new spot. |
| 732 | Spot* spot = new Spot(id, sprite); |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 733 | spots.push_back(spot); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 734 | return spot; |
| 735 | } |
| 736 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 737 | PointerController::Spot* PointerController::removeFirstFadingSpotLocked(std::vector<Spot*>& spots) { |
| 738 | for (size_t i = 0; i < spots.size(); i++) { |
| 739 | Spot* spot = spots[i]; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 740 | if (spot->id == Spot::INVALID_ID) { |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 741 | spots.erase(spots.begin() + i); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 742 | return spot; |
| 743 | } |
| 744 | } |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame^] | 745 | return nullptr; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void PointerController::releaseSpotLocked(Spot* spot) { |
| 749 | spot->sprite->clearIcon(); |
| 750 | |
| 751 | if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) { |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 752 | mLocked.recycledSprites.push_back(spot->sprite); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | delete spot; |
| 756 | } |
| 757 | |
| 758 | void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) { |
| 759 | if (spot->id != Spot::INVALID_ID) { |
| 760 | spot->id = Spot::INVALID_ID; |
| 761 | startAnimationLocked(); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | void PointerController::fadeOutAndReleaseAllSpotsLocked() { |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 766 | for (auto& it : mLocked.spotsByDisplay) { |
| 767 | const std::vector<Spot*>& spots = it.second; |
| 768 | size_t numSpots = spots.size(); |
| 769 | for (size_t i = 0; i < numSpots; i++) { |
| 770 | Spot* spot = spots[i]; |
| 771 | fadeOutAndReleaseSpotLocked(spot); |
| 772 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 776 | void PointerController::loadResourcesLocked() REQUIRES(mLock) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 777 | if (!mLocked.viewport.isValid()) { |
| 778 | return; |
| 779 | } |
| 780 | |
Andrii Kulian | fd8666d | 2018-10-05 16:58:39 -0700 | [diff] [blame] | 781 | mPolicy->loadPointerResources(&mResources, mLocked.viewport.displayId); |
Arthur Hung | 350896b | 2019-02-26 16:35:01 +0800 | [diff] [blame] | 782 | mPolicy->loadPointerIcon(&mLocked.pointerIcon, mLocked.viewport.displayId); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 783 | |
Arthur Hung | 350896b | 2019-02-26 16:35:01 +0800 | [diff] [blame] | 784 | mLocked.additionalMouseResources.clear(); |
| 785 | mLocked.animationResources.clear(); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 786 | if (mLocked.presentation == PRESENTATION_POINTER) { |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 787 | mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources, |
Andrii Kulian | fd8666d | 2018-10-05 16:58:39 -0700 | [diff] [blame] | 788 | &mLocked.animationResources, mLocked.viewport.displayId); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | mLocked.pointerIconChanged = true; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | |
| 795 | // --- PointerController::Spot --- |
| 796 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 797 | void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y, |
| 798 | int32_t displayId) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 799 | sprite->setLayer(Sprite::BASE_LAYER_SPOT + id); |
| 800 | sprite->setAlpha(alpha); |
| 801 | sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale)); |
| 802 | sprite->setPosition(x, y); |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 803 | sprite->setDisplayId(displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 804 | this->x = x; |
| 805 | this->y = y; |
| 806 | |
| 807 | if (icon != lastIcon) { |
| 808 | lastIcon = icon; |
| 809 | if (icon) { |
| 810 | sprite->setIcon(*icon); |
| 811 | sprite->setVisible(true); |
| 812 | } else { |
| 813 | sprite->setVisible(false); |
| 814 | } |
| 815 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 818 | } // namespace android |