Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 "MouseCursorController" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | // Log debug messages about pointer updates |
| 21 | #define DEBUG_MOUSE_CURSOR_UPDATES 0 |
| 22 | |
| 23 | #include "MouseCursorController.h" |
| 24 | |
| 25 | #include <log/log.h> |
| 26 | |
| 27 | #include <SkBitmap.h> |
| 28 | #include <SkBlendMode.h> |
| 29 | #include <SkCanvas.h> |
| 30 | #include <SkColor.h> |
| 31 | #include <SkPaint.h> |
| 32 | |
| 33 | namespace { |
| 34 | // Time to spend fading out the pointer completely. |
| 35 | const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms |
| 36 | } // namespace |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | // --- MouseCursorController --- |
| 41 | |
| 42 | MouseCursorController::MouseCursorController(PointerControllerContext& context) |
| 43 | : mContext(context) { |
| 44 | std::scoped_lock lock(mLock); |
| 45 | |
| 46 | mLocked.animationFrameIndex = 0; |
| 47 | mLocked.lastFrameUpdatedTime = 0; |
| 48 | |
| 49 | mLocked.pointerFadeDirection = 0; |
| 50 | mLocked.pointerX = 0; |
| 51 | mLocked.pointerY = 0; |
| 52 | mLocked.pointerAlpha = 0.0f; // pointer is initially faded |
| 53 | mLocked.pointerSprite = mContext.getSpriteController()->createSprite(); |
| 54 | mLocked.updatePointerIcon = false; |
| 55 | mLocked.requestedPointerType = mContext.getPolicy()->getDefaultPointerIconId(); |
| 56 | |
| 57 | mLocked.resourcesLoaded = false; |
| 58 | |
| 59 | mLocked.buttonState = 0; |
| 60 | } |
| 61 | |
| 62 | MouseCursorController::~MouseCursorController() { |
| 63 | std::scoped_lock lock(mLock); |
| 64 | |
| 65 | mLocked.pointerSprite.clear(); |
| 66 | } |
| 67 | |
| 68 | bool MouseCursorController::getBounds(float* outMinX, float* outMinY, float* outMaxX, |
| 69 | float* outMaxY) const { |
| 70 | std::scoped_lock lock(mLock); |
| 71 | |
| 72 | return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY); |
| 73 | } |
| 74 | |
| 75 | bool MouseCursorController::getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, |
| 76 | float* outMaxY) const REQUIRES(mLock) { |
| 77 | if (!mLocked.viewport.isValid()) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | *outMinX = mLocked.viewport.logicalLeft; |
| 82 | *outMinY = mLocked.viewport.logicalTop; |
| 83 | *outMaxX = mLocked.viewport.logicalRight - 1; |
| 84 | *outMaxY = mLocked.viewport.logicalBottom - 1; |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void MouseCursorController::move(float deltaX, float deltaY) { |
| 89 | #if DEBUG_MOUSE_CURSOR_UPDATES |
| 90 | ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY); |
| 91 | #endif |
| 92 | if (deltaX == 0.0f && deltaY == 0.0f) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | std::scoped_lock lock(mLock); |
| 97 | |
| 98 | setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY); |
| 99 | } |
| 100 | |
| 101 | void MouseCursorController::setButtonState(int32_t buttonState) { |
| 102 | #if DEBUG_MOUSE_CURSOR_UPDATES |
| 103 | ALOGD("Set button state 0x%08x", buttonState); |
| 104 | #endif |
| 105 | std::scoped_lock lock(mLock); |
| 106 | |
| 107 | if (mLocked.buttonState != buttonState) { |
| 108 | mLocked.buttonState = buttonState; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int32_t MouseCursorController::getButtonState() const { |
| 113 | std::scoped_lock lock(mLock); |
| 114 | return mLocked.buttonState; |
| 115 | } |
| 116 | |
| 117 | void MouseCursorController::setPosition(float x, float y) { |
| 118 | #if DEBUG_MOUSE_CURSOR_UPDATES |
| 119 | ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y); |
| 120 | #endif |
| 121 | std::scoped_lock lock(mLock); |
| 122 | setPositionLocked(x, y); |
| 123 | } |
| 124 | |
| 125 | void MouseCursorController::setPositionLocked(float x, float y) REQUIRES(mLock) { |
| 126 | float minX, minY, maxX, maxY; |
| 127 | if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { |
| 128 | if (x <= minX) { |
| 129 | mLocked.pointerX = minX; |
| 130 | } else if (x >= maxX) { |
| 131 | mLocked.pointerX = maxX; |
| 132 | } else { |
| 133 | mLocked.pointerX = x; |
| 134 | } |
| 135 | if (y <= minY) { |
| 136 | mLocked.pointerY = minY; |
| 137 | } else if (y >= maxY) { |
| 138 | mLocked.pointerY = maxY; |
| 139 | } else { |
| 140 | mLocked.pointerY = y; |
| 141 | } |
| 142 | updatePointerLocked(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void MouseCursorController::getPosition(float* outX, float* outY) const { |
| 147 | std::scoped_lock lock(mLock); |
| 148 | |
| 149 | *outX = mLocked.pointerX; |
| 150 | *outY = mLocked.pointerY; |
| 151 | } |
| 152 | |
| 153 | int32_t MouseCursorController::getDisplayId() const { |
| 154 | std::scoped_lock lock(mLock); |
| 155 | return mLocked.viewport.displayId; |
| 156 | } |
| 157 | |
| 158 | void MouseCursorController::fade(PointerControllerInterface::Transition transition) { |
| 159 | std::scoped_lock lock(mLock); |
| 160 | |
| 161 | // Remove the inactivity timeout, since we are fading now. |
| 162 | mContext.removeInactivityTimeout(); |
| 163 | |
| 164 | // Start fading. |
| 165 | if (transition == PointerControllerInterface::Transition::IMMEDIATE) { |
| 166 | mLocked.pointerFadeDirection = 0; |
| 167 | mLocked.pointerAlpha = 0.0f; |
| 168 | updatePointerLocked(); |
| 169 | } else { |
| 170 | mLocked.pointerFadeDirection = -1; |
| 171 | mContext.startAnimation(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void MouseCursorController::unfade(PointerControllerInterface::Transition transition) { |
| 176 | std::scoped_lock lock(mLock); |
| 177 | |
| 178 | // Always reset the inactivity timer. |
| 179 | mContext.resetInactivityTimeout(); |
| 180 | |
| 181 | // Start unfading. |
| 182 | if (transition == PointerControllerInterface::Transition::IMMEDIATE) { |
| 183 | mLocked.pointerFadeDirection = 0; |
| 184 | mLocked.pointerAlpha = 1.0f; |
| 185 | updatePointerLocked(); |
| 186 | } else { |
| 187 | mLocked.pointerFadeDirection = 1; |
| 188 | mContext.startAnimation(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void MouseCursorController::reloadPointerResources(bool getAdditionalMouseResources) { |
| 193 | std::scoped_lock lock(mLock); |
| 194 | |
| 195 | loadResourcesLocked(getAdditionalMouseResources); |
| 196 | updatePointerLocked(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * The viewport values for deviceHeight and deviceWidth have already been adjusted for rotation, |
| 201 | * so here we are getting the dimensions in the original, unrotated orientation (orientation 0). |
| 202 | */ |
| 203 | static void getNonRotatedSize(const DisplayViewport& viewport, int32_t& width, int32_t& height) { |
| 204 | width = viewport.deviceWidth; |
| 205 | height = viewport.deviceHeight; |
| 206 | |
| 207 | if (viewport.orientation == DISPLAY_ORIENTATION_90 || |
| 208 | viewport.orientation == DISPLAY_ORIENTATION_270) { |
| 209 | std::swap(width, height); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport, |
| 214 | bool getAdditionalMouseResources) { |
| 215 | std::scoped_lock lock(mLock); |
| 216 | |
| 217 | if (viewport == mLocked.viewport) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | const DisplayViewport oldViewport = mLocked.viewport; |
| 222 | mLocked.viewport = viewport; |
| 223 | |
| 224 | int32_t oldDisplayWidth, oldDisplayHeight; |
| 225 | getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight); |
| 226 | int32_t newDisplayWidth, newDisplayHeight; |
| 227 | getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight); |
| 228 | |
| 229 | // Reset cursor position to center if size or display changed. |
| 230 | if (oldViewport.displayId != viewport.displayId || oldDisplayWidth != newDisplayWidth || |
| 231 | oldDisplayHeight != newDisplayHeight) { |
| 232 | float minX, minY, maxX, maxY; |
| 233 | if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { |
| 234 | mLocked.pointerX = (minX + maxX) * 0.5f; |
| 235 | mLocked.pointerY = (minY + maxY) * 0.5f; |
| 236 | // Reload icon resources for density may be changed. |
| 237 | loadResourcesLocked(getAdditionalMouseResources); |
| 238 | } else { |
| 239 | mLocked.pointerX = 0; |
| 240 | mLocked.pointerY = 0; |
| 241 | } |
| 242 | } else if (oldViewport.orientation != viewport.orientation) { |
| 243 | // Apply offsets to convert from the pixel top-left corner position to the pixel center. |
| 244 | // This creates an invariant frame of reference that we can easily rotate when |
| 245 | // taking into account that the pointer may be located at fractional pixel offsets. |
| 246 | float x = mLocked.pointerX + 0.5f; |
| 247 | float y = mLocked.pointerY + 0.5f; |
| 248 | float temp; |
| 249 | |
| 250 | // Undo the previous rotation. |
| 251 | switch (oldViewport.orientation) { |
| 252 | case DISPLAY_ORIENTATION_90: |
| 253 | temp = x; |
| 254 | x = oldViewport.deviceHeight - y; |
| 255 | y = temp; |
| 256 | break; |
| 257 | case DISPLAY_ORIENTATION_180: |
| 258 | x = oldViewport.deviceWidth - x; |
| 259 | y = oldViewport.deviceHeight - y; |
| 260 | break; |
| 261 | case DISPLAY_ORIENTATION_270: |
| 262 | temp = x; |
| 263 | x = y; |
| 264 | y = oldViewport.deviceWidth - temp; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | // Perform the new rotation. |
| 269 | switch (viewport.orientation) { |
| 270 | case DISPLAY_ORIENTATION_90: |
| 271 | temp = x; |
| 272 | x = y; |
| 273 | y = viewport.deviceHeight - temp; |
| 274 | break; |
| 275 | case DISPLAY_ORIENTATION_180: |
| 276 | x = viewport.deviceWidth - x; |
| 277 | y = viewport.deviceHeight - y; |
| 278 | break; |
| 279 | case DISPLAY_ORIENTATION_270: |
| 280 | temp = x; |
| 281 | x = viewport.deviceWidth - y; |
| 282 | y = temp; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | // Apply offsets to convert from the pixel center to the pixel top-left corner position |
| 287 | // and save the results. |
| 288 | mLocked.pointerX = x - 0.5f; |
| 289 | mLocked.pointerY = y - 0.5f; |
| 290 | } |
| 291 | |
| 292 | updatePointerLocked(); |
| 293 | } |
| 294 | |
| 295 | void MouseCursorController::updatePointerIcon(int32_t iconId) { |
| 296 | std::scoped_lock lock(mLock); |
| 297 | |
| 298 | if (mLocked.requestedPointerType != iconId) { |
| 299 | mLocked.requestedPointerType = iconId; |
| 300 | mLocked.updatePointerIcon = true; |
| 301 | updatePointerLocked(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void MouseCursorController::setCustomPointerIcon(const SpriteIcon& icon) { |
| 306 | std::scoped_lock lock(mLock); |
| 307 | |
| 308 | const int32_t iconId = mContext.getPolicy()->getCustomPointerIconId(); |
| 309 | mLocked.additionalMouseResources[iconId] = icon; |
| 310 | mLocked.requestedPointerType = iconId; |
| 311 | mLocked.updatePointerIcon = true; |
| 312 | updatePointerLocked(); |
| 313 | } |
| 314 | |
| 315 | bool MouseCursorController::doFadingAnimation(nsecs_t timestamp, bool keepAnimating) { |
| 316 | nsecs_t frameDelay = timestamp - mContext.getAnimationTime(); |
| 317 | |
| 318 | std::scoped_lock lock(mLock); |
| 319 | |
| 320 | // Animate pointer fade. |
| 321 | if (mLocked.pointerFadeDirection < 0) { |
| 322 | mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION; |
| 323 | if (mLocked.pointerAlpha <= 0.0f) { |
| 324 | mLocked.pointerAlpha = 0.0f; |
| 325 | mLocked.pointerFadeDirection = 0; |
| 326 | } else { |
| 327 | keepAnimating = true; |
| 328 | } |
| 329 | updatePointerLocked(); |
| 330 | } else if (mLocked.pointerFadeDirection > 0) { |
| 331 | mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION; |
| 332 | if (mLocked.pointerAlpha >= 1.0f) { |
| 333 | mLocked.pointerAlpha = 1.0f; |
| 334 | mLocked.pointerFadeDirection = 0; |
| 335 | } else { |
| 336 | keepAnimating = true; |
| 337 | } |
| 338 | updatePointerLocked(); |
| 339 | } |
| 340 | |
| 341 | return keepAnimating; |
| 342 | } |
| 343 | |
| 344 | bool MouseCursorController::doBitmapAnimation(nsecs_t timestamp) { |
| 345 | std::scoped_lock lock(mLock); |
| 346 | |
| 347 | std::map<int32_t, PointerAnimation>::const_iterator iter = |
| 348 | mLocked.animationResources.find(mLocked.requestedPointerType); |
| 349 | if (iter == mLocked.animationResources.end()) { |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) { |
| 354 | sp<SpriteController> spriteController = mContext.getSpriteController(); |
| 355 | spriteController->openTransaction(); |
| 356 | |
| 357 | int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame; |
| 358 | mLocked.animationFrameIndex += incr; |
| 359 | mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr; |
| 360 | while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) { |
| 361 | mLocked.animationFrameIndex -= iter->second.animationFrames.size(); |
| 362 | } |
| 363 | mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]); |
| 364 | |
| 365 | spriteController->closeTransaction(); |
| 366 | } |
| 367 | |
| 368 | // Keep animating. |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | void MouseCursorController::updatePointerLocked() REQUIRES(mLock) { |
| 373 | if (!mLocked.viewport.isValid()) { |
| 374 | return; |
| 375 | } |
| 376 | sp<SpriteController> spriteController = mContext.getSpriteController(); |
| 377 | spriteController->openTransaction(); |
| 378 | |
| 379 | mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER); |
| 380 | mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY); |
| 381 | mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId); |
| 382 | |
| 383 | if (mLocked.pointerAlpha > 0) { |
| 384 | mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha); |
| 385 | mLocked.pointerSprite->setVisible(true); |
| 386 | } else { |
| 387 | mLocked.pointerSprite->setVisible(false); |
| 388 | } |
| 389 | |
| 390 | if (mLocked.updatePointerIcon) { |
| 391 | if (mLocked.requestedPointerType == mContext.getPolicy()->getDefaultPointerIconId()) { |
| 392 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 393 | } else { |
| 394 | std::map<int32_t, SpriteIcon>::const_iterator iter = |
| 395 | mLocked.additionalMouseResources.find(mLocked.requestedPointerType); |
| 396 | if (iter != mLocked.additionalMouseResources.end()) { |
| 397 | std::map<int32_t, PointerAnimation>::const_iterator anim_iter = |
| 398 | mLocked.animationResources.find(mLocked.requestedPointerType); |
| 399 | if (anim_iter != mLocked.animationResources.end()) { |
| 400 | mLocked.animationFrameIndex = 0; |
| 401 | mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 402 | mContext.startAnimation(); |
| 403 | } |
| 404 | mLocked.pointerSprite->setIcon(iter->second); |
| 405 | } else { |
| 406 | ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType); |
| 407 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 408 | } |
| 409 | } |
| 410 | mLocked.updatePointerIcon = false; |
| 411 | } |
| 412 | |
| 413 | spriteController->closeTransaction(); |
| 414 | } |
| 415 | |
| 416 | void MouseCursorController::loadResourcesLocked(bool getAdditionalMouseResources) REQUIRES(mLock) { |
| 417 | if (!mLocked.viewport.isValid()) { |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | if (!mLocked.resourcesLoaded) mLocked.resourcesLoaded = true; |
| 422 | |
| 423 | sp<PointerControllerPolicyInterface> policy = mContext.getPolicy(); |
| 424 | policy->loadPointerResources(&mResources, mLocked.viewport.displayId); |
| 425 | policy->loadPointerIcon(&mLocked.pointerIcon, mLocked.viewport.displayId); |
| 426 | |
| 427 | mLocked.additionalMouseResources.clear(); |
| 428 | mLocked.animationResources.clear(); |
| 429 | if (getAdditionalMouseResources) { |
| 430 | policy->loadAdditionalMouseResources(&mLocked.additionalMouseResources, |
| 431 | &mLocked.animationResources, |
| 432 | mLocked.viewport.displayId); |
| 433 | } |
| 434 | |
| 435 | mLocked.updatePointerIcon = true; |
| 436 | } |
| 437 | |
| 438 | bool MouseCursorController::isViewportValid() { |
| 439 | std::scoped_lock lock(mLock); |
| 440 | return mLocked.viewport.isValid(); |
| 441 | } |
| 442 | |
| 443 | void MouseCursorController::getAdditionalMouseResources() { |
| 444 | std::scoped_lock lock(mLock); |
| 445 | |
| 446 | if (mLocked.additionalMouseResources.empty()) { |
| 447 | mContext.getPolicy()->loadAdditionalMouseResources(&mLocked.additionalMouseResources, |
| 448 | &mLocked.animationResources, |
| 449 | mLocked.viewport.displayId); |
| 450 | } |
| 451 | mLocked.updatePointerIcon = true; |
| 452 | updatePointerLocked(); |
| 453 | } |
| 454 | |
| 455 | bool MouseCursorController::resourcesLoaded() { |
| 456 | std::scoped_lock lock(mLock); |
| 457 | return mLocked.resourcesLoaded; |
| 458 | } |
| 459 | |
| 460 | } // namespace android |