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