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