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 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 25 | #include <input/Input.h> |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 26 | #include <log/log.h> |
| 27 | |
Arpit Singh | 05215af | 2024-08-25 17:49:32 +0000 | [diff] [blame] | 28 | #define INDENT " " |
| 29 | #define INDENT2 " " |
| 30 | |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 31 | namespace android { |
| 32 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 33 | namespace { |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 34 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 35 | // Time to spend fading out the pointer completely. |
| 36 | const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 37 | |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 38 | } // namespace |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 39 | |
| 40 | // --- MouseCursorController --- |
| 41 | |
| 42 | MouseCursorController::MouseCursorController(PointerControllerContext& context) |
| 43 | : mContext(context) { |
| 44 | std::scoped_lock lock(mLock); |
| 45 | |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 46 | mLocked.stylusHoverMode = false; |
| 47 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 48 | mLocked.animationFrameIndex = 0; |
| 49 | mLocked.lastFrameUpdatedTime = 0; |
| 50 | |
| 51 | mLocked.pointerFadeDirection = 0; |
| 52 | mLocked.pointerX = 0; |
| 53 | mLocked.pointerY = 0; |
| 54 | mLocked.pointerAlpha = 0.0f; // pointer is initially faded |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 55 | mLocked.pointerSprite = mContext.getSpriteController().createSprite(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 56 | mLocked.updatePointerIcon = false; |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 57 | mLocked.requestedPointerType = PointerIconStyle::TYPE_NOT_SPECIFIED; |
| 58 | mLocked.resolvedPointerType = PointerIconStyle::TYPE_NOT_SPECIFIED; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 59 | |
| 60 | mLocked.resourcesLoaded = false; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | MouseCursorController::~MouseCursorController() { |
| 64 | std::scoped_lock lock(mLock); |
| 65 | |
| 66 | mLocked.pointerSprite.clear(); |
| 67 | } |
| 68 | |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 69 | vec2 MouseCursorController::move(float deltaX, float deltaY) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 70 | #if DEBUG_MOUSE_CURSOR_UPDATES |
| 71 | ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY); |
| 72 | #endif |
| 73 | if (deltaX == 0.0f && deltaY == 0.0f) { |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 74 | return {}; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 77 | // When transition occurs, the MouseCursorController object may or may not be deleted, depending |
| 78 | // if there's another display on the other side of the transition. At this point we still need |
| 79 | // to move the cursor to the boundary. |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 80 | std::scoped_lock lock(mLock); |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 81 | const vec2 pos{mLocked.pointerX + deltaX, mLocked.pointerY + deltaY}; |
| 82 | setPositionLocked(pos.x, pos.y); |
| 83 | return vec2{pos.x - mLocked.pointerX, pos.y - mLocked.pointerY}; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 86 | void MouseCursorController::setPosition(float x, float y) { |
| 87 | #if DEBUG_MOUSE_CURSOR_UPDATES |
| 88 | ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y); |
| 89 | #endif |
| 90 | std::scoped_lock lock(mLock); |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 91 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 92 | setPositionLocked(x, y); |
| 93 | } |
| 94 | |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 95 | FloatRect MouseCursorController::getBoundsLocked() REQUIRES(mLock) { |
Prabir Pradhan | eb0c507 | 2024-09-09 19:53:06 +0000 | [diff] [blame] | 96 | // The valid bounds for a mouse cursor. Since the right and bottom edges are considered outside |
| 97 | // the display, clip the bounds by one pixel instead of letting the cursor get arbitrarily |
| 98 | // close to the outside edge. |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 99 | return FloatRect{ |
Prabir Pradhan | eb0c507 | 2024-09-09 19:53:06 +0000 | [diff] [blame] | 100 | static_cast<float>(mLocked.viewport.logicalLeft), |
| 101 | static_cast<float>(mLocked.viewport.logicalTop), |
| 102 | static_cast<float>(mLocked.viewport.logicalRight - 1), |
| 103 | static_cast<float>(mLocked.viewport.logicalBottom - 1), |
| 104 | }; |
Arpit Singh | 24c8ba6 | 2024-10-25 21:08:08 +0000 | [diff] [blame^] | 105 | } |
| 106 | void MouseCursorController::setPositionLocked(float x, float y) REQUIRES(mLock) { |
| 107 | const auto& v = mLocked.viewport; |
| 108 | if (!v.isValid()) return; |
| 109 | |
| 110 | const FloatRect bounds = getBoundsLocked(); |
Prabir Pradhan | eb0c507 | 2024-09-09 19:53:06 +0000 | [diff] [blame] | 111 | mLocked.pointerX = std::max(bounds.left, std::min(bounds.right, x)); |
| 112 | mLocked.pointerY = std::max(bounds.top, std::min(bounds.bottom, y)); |
Prabir Pradhan | b5dadec | 2023-02-28 17:43:09 +0000 | [diff] [blame] | 113 | |
| 114 | updatePointerLocked(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Prabir Pradhan | b5dadec | 2023-02-28 17:43:09 +0000 | [diff] [blame] | 117 | FloatPoint MouseCursorController::getPosition() const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 118 | std::scoped_lock lock(mLock); |
| 119 | |
Prabir Pradhan | b5dadec | 2023-02-28 17:43:09 +0000 | [diff] [blame] | 120 | return {mLocked.pointerX, mLocked.pointerY}; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 123 | ui::LogicalDisplayId MouseCursorController::getDisplayId() const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 124 | std::scoped_lock lock(mLock); |
| 125 | return mLocked.viewport.displayId; |
| 126 | } |
| 127 | |
| 128 | void MouseCursorController::fade(PointerControllerInterface::Transition transition) { |
| 129 | std::scoped_lock lock(mLock); |
| 130 | |
| 131 | // Remove the inactivity timeout, since we are fading now. |
| 132 | mContext.removeInactivityTimeout(); |
| 133 | |
| 134 | // Start fading. |
| 135 | if (transition == PointerControllerInterface::Transition::IMMEDIATE) { |
| 136 | mLocked.pointerFadeDirection = 0; |
| 137 | mLocked.pointerAlpha = 0.0f; |
| 138 | updatePointerLocked(); |
| 139 | } else { |
| 140 | mLocked.pointerFadeDirection = -1; |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 141 | startAnimationLocked(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void MouseCursorController::unfade(PointerControllerInterface::Transition transition) { |
| 146 | std::scoped_lock lock(mLock); |
| 147 | |
| 148 | // Always reset the inactivity timer. |
| 149 | mContext.resetInactivityTimeout(); |
| 150 | |
| 151 | // Start unfading. |
| 152 | if (transition == PointerControllerInterface::Transition::IMMEDIATE) { |
| 153 | mLocked.pointerFadeDirection = 0; |
| 154 | mLocked.pointerAlpha = 1.0f; |
| 155 | updatePointerLocked(); |
| 156 | } else { |
| 157 | mLocked.pointerFadeDirection = 1; |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 158 | startAnimationLocked(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 162 | void MouseCursorController::setStylusHoverMode(bool stylusHoverMode) { |
| 163 | std::scoped_lock lock(mLock); |
| 164 | |
| 165 | if (mLocked.stylusHoverMode != stylusHoverMode) { |
| 166 | mLocked.stylusHoverMode = stylusHoverMode; |
| 167 | mLocked.updatePointerIcon = true; |
| 168 | } |
| 169 | } |
| 170 | |
Arpit Singh | f4ae0ac | 2024-03-26 18:41:06 +0000 | [diff] [blame] | 171 | void MouseCursorController::setSkipScreenshot(bool skip) { |
| 172 | std::scoped_lock lock(mLock); |
| 173 | if (mLocked.skipScreenshot == skip) { |
| 174 | return; |
| 175 | } |
| 176 | mLocked.skipScreenshot = skip; |
| 177 | updatePointerLocked(); |
| 178 | } |
| 179 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 180 | void MouseCursorController::reloadPointerResources(bool getAdditionalMouseResources) { |
| 181 | std::scoped_lock lock(mLock); |
| 182 | |
| 183 | loadResourcesLocked(getAdditionalMouseResources); |
| 184 | updatePointerLocked(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * The viewport values for deviceHeight and deviceWidth have already been adjusted for rotation, |
| 189 | * so here we are getting the dimensions in the original, unrotated orientation (orientation 0). |
| 190 | */ |
| 191 | static void getNonRotatedSize(const DisplayViewport& viewport, int32_t& width, int32_t& height) { |
| 192 | width = viewport.deviceWidth; |
| 193 | height = viewport.deviceHeight; |
| 194 | |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 195 | if (viewport.orientation == ui::ROTATION_90 || viewport.orientation == ui::ROTATION_270) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 196 | std::swap(width, height); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport, |
| 201 | bool getAdditionalMouseResources) { |
| 202 | std::scoped_lock lock(mLock); |
| 203 | |
| 204 | if (viewport == mLocked.viewport) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | const DisplayViewport oldViewport = mLocked.viewport; |
| 209 | mLocked.viewport = viewport; |
| 210 | |
| 211 | int32_t oldDisplayWidth, oldDisplayHeight; |
| 212 | getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight); |
| 213 | int32_t newDisplayWidth, newDisplayHeight; |
| 214 | getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight); |
| 215 | |
| 216 | // Reset cursor position to center if size or display changed. |
| 217 | if (oldViewport.displayId != viewport.displayId || oldDisplayWidth != newDisplayWidth || |
| 218 | oldDisplayHeight != newDisplayHeight) { |
Prabir Pradhan | eb0c507 | 2024-09-09 19:53:06 +0000 | [diff] [blame] | 219 | if (viewport.isValid()) { |
| 220 | // Use integer coordinates as the starting point for the cursor location. |
| 221 | // We usually expect display sizes to be even numbers, so the flooring is precautionary. |
| 222 | mLocked.pointerX = std::floor((viewport.logicalLeft + viewport.logicalRight) / 2); |
| 223 | mLocked.pointerY = std::floor((viewport.logicalTop + viewport.logicalBottom) / 2); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 224 | // Reload icon resources for density may be changed. |
| 225 | loadResourcesLocked(getAdditionalMouseResources); |
| 226 | } else { |
| 227 | mLocked.pointerX = 0; |
| 228 | mLocked.pointerY = 0; |
| 229 | } |
| 230 | } else if (oldViewport.orientation != viewport.orientation) { |
| 231 | // Apply offsets to convert from the pixel top-left corner position to the pixel center. |
| 232 | // This creates an invariant frame of reference that we can easily rotate when |
| 233 | // taking into account that the pointer may be located at fractional pixel offsets. |
| 234 | float x = mLocked.pointerX + 0.5f; |
| 235 | float y = mLocked.pointerY + 0.5f; |
| 236 | float temp; |
| 237 | |
| 238 | // Undo the previous rotation. |
| 239 | switch (oldViewport.orientation) { |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 240 | case ui::ROTATION_90: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 241 | temp = x; |
| 242 | x = oldViewport.deviceHeight - y; |
| 243 | y = temp; |
| 244 | break; |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 245 | case ui::ROTATION_180: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 246 | x = oldViewport.deviceWidth - x; |
| 247 | y = oldViewport.deviceHeight - y; |
| 248 | break; |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 249 | case ui::ROTATION_270: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 250 | temp = x; |
| 251 | x = y; |
| 252 | y = oldViewport.deviceWidth - temp; |
| 253 | break; |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 254 | case ui::ROTATION_0: |
| 255 | break; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Perform the new rotation. |
| 259 | switch (viewport.orientation) { |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 260 | case ui::ROTATION_90: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 261 | temp = x; |
| 262 | x = y; |
| 263 | y = viewport.deviceHeight - temp; |
| 264 | break; |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 265 | case ui::ROTATION_180: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 266 | x = viewport.deviceWidth - x; |
| 267 | y = viewport.deviceHeight - y; |
| 268 | break; |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 269 | case ui::ROTATION_270: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 270 | temp = x; |
| 271 | x = viewport.deviceWidth - y; |
| 272 | y = temp; |
| 273 | break; |
Michael Wright | ecde4d0 | 2022-11-25 00:20:19 +0000 | [diff] [blame] | 274 | case ui::ROTATION_0: |
| 275 | break; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // Apply offsets to convert from the pixel center to the pixel top-left corner position |
| 279 | // and save the results. |
| 280 | mLocked.pointerX = x - 0.5f; |
| 281 | mLocked.pointerY = y - 0.5f; |
| 282 | } |
| 283 | |
| 284 | updatePointerLocked(); |
| 285 | } |
| 286 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 287 | void MouseCursorController::updatePointerIcon(PointerIconStyle iconId) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 288 | std::scoped_lock lock(mLock); |
| 289 | |
| 290 | if (mLocked.requestedPointerType != iconId) { |
| 291 | mLocked.requestedPointerType = iconId; |
| 292 | mLocked.updatePointerIcon = true; |
| 293 | updatePointerLocked(); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void MouseCursorController::setCustomPointerIcon(const SpriteIcon& icon) { |
| 298 | std::scoped_lock lock(mLock); |
| 299 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 300 | const PointerIconStyle iconId = mContext.getPolicy()->getCustomPointerIconId(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 301 | mLocked.additionalMouseResources[iconId] = icon; |
| 302 | mLocked.requestedPointerType = iconId; |
| 303 | mLocked.updatePointerIcon = true; |
| 304 | updatePointerLocked(); |
| 305 | } |
| 306 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 307 | bool MouseCursorController::doFadingAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 308 | nsecs_t frameDelay = timestamp - mContext.getAnimationTime(); |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 309 | bool keepAnimating = false; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 310 | |
| 311 | // Animate pointer fade. |
| 312 | if (mLocked.pointerFadeDirection < 0) { |
| 313 | mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION; |
| 314 | if (mLocked.pointerAlpha <= 0.0f) { |
| 315 | mLocked.pointerAlpha = 0.0f; |
| 316 | mLocked.pointerFadeDirection = 0; |
| 317 | } else { |
| 318 | keepAnimating = true; |
| 319 | } |
| 320 | updatePointerLocked(); |
| 321 | } else if (mLocked.pointerFadeDirection > 0) { |
| 322 | mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION; |
| 323 | if (mLocked.pointerAlpha >= 1.0f) { |
| 324 | mLocked.pointerAlpha = 1.0f; |
| 325 | mLocked.pointerFadeDirection = 0; |
| 326 | } else { |
| 327 | keepAnimating = true; |
| 328 | } |
| 329 | updatePointerLocked(); |
| 330 | } |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 331 | return keepAnimating; |
| 332 | } |
| 333 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 334 | bool MouseCursorController::doBitmapAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) { |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 335 | std::map<PointerIconStyle, PointerAnimation>::const_iterator iter = |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 336 | mLocked.animationResources.find(mLocked.resolvedPointerType); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 337 | if (iter == mLocked.animationResources.end()) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) { |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 342 | auto& spriteController = mContext.getSpriteController(); |
| 343 | spriteController.openTransaction(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 344 | |
| 345 | int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame; |
| 346 | mLocked.animationFrameIndex += incr; |
| 347 | mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr; |
| 348 | while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) { |
| 349 | mLocked.animationFrameIndex -= iter->second.animationFrames.size(); |
| 350 | } |
| 351 | mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]); |
| 352 | |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 353 | spriteController.closeTransaction(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 354 | } |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 355 | // Keep animating. |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | void MouseCursorController::updatePointerLocked() REQUIRES(mLock) { |
| 360 | if (!mLocked.viewport.isValid()) { |
| 361 | return; |
| 362 | } |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 363 | auto& spriteController = mContext.getSpriteController(); |
| 364 | spriteController.openTransaction(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 365 | |
| 366 | mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER); |
| 367 | mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY); |
| 368 | mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId); |
Arpit Singh | f4ae0ac | 2024-03-26 18:41:06 +0000 | [diff] [blame] | 369 | mLocked.pointerSprite->setSkipScreenshot(mLocked.skipScreenshot); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 370 | |
| 371 | if (mLocked.pointerAlpha > 0) { |
| 372 | mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha); |
| 373 | mLocked.pointerSprite->setVisible(true); |
| 374 | } else { |
| 375 | mLocked.pointerSprite->setVisible(false); |
| 376 | } |
| 377 | |
| 378 | if (mLocked.updatePointerIcon) { |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 379 | mLocked.resolvedPointerType = mLocked.requestedPointerType; |
| 380 | const PointerIconStyle defaultPointerIconId = |
| 381 | mContext.getPolicy()->getDefaultPointerIconId(); |
| 382 | if (mLocked.resolvedPointerType == PointerIconStyle::TYPE_NOT_SPECIFIED) { |
| 383 | mLocked.resolvedPointerType = mLocked.stylusHoverMode |
| 384 | ? mContext.getPolicy()->getDefaultStylusIconId() |
| 385 | : defaultPointerIconId; |
| 386 | } |
| 387 | |
| 388 | if (mLocked.resolvedPointerType == defaultPointerIconId) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 389 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 390 | } else { |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 391 | std::map<PointerIconStyle, SpriteIcon>::const_iterator iter = |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 392 | mLocked.additionalMouseResources.find(mLocked.resolvedPointerType); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 393 | if (iter != mLocked.additionalMouseResources.end()) { |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 394 | std::map<PointerIconStyle, PointerAnimation>::const_iterator anim_iter = |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 395 | mLocked.animationResources.find(mLocked.resolvedPointerType); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 396 | if (anim_iter != mLocked.animationResources.end()) { |
| 397 | mLocked.animationFrameIndex = 0; |
| 398 | mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 399 | startAnimationLocked(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 400 | } |
| 401 | mLocked.pointerSprite->setIcon(iter->second); |
| 402 | } else { |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 403 | ALOGW("Can't find the resource for icon id %d", mLocked.resolvedPointerType); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 404 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 405 | } |
| 406 | } |
| 407 | mLocked.updatePointerIcon = false; |
| 408 | } |
| 409 | |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 410 | spriteController.closeTransaction(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void MouseCursorController::loadResourcesLocked(bool getAdditionalMouseResources) REQUIRES(mLock) { |
| 414 | if (!mLocked.viewport.isValid()) { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | if (!mLocked.resourcesLoaded) mLocked.resourcesLoaded = true; |
| 419 | |
| 420 | sp<PointerControllerPolicyInterface> policy = mContext.getPolicy(); |
| 421 | policy->loadPointerResources(&mResources, mLocked.viewport.displayId); |
| 422 | policy->loadPointerIcon(&mLocked.pointerIcon, mLocked.viewport.displayId); |
| 423 | |
| 424 | mLocked.additionalMouseResources.clear(); |
| 425 | mLocked.animationResources.clear(); |
| 426 | if (getAdditionalMouseResources) { |
| 427 | policy->loadAdditionalMouseResources(&mLocked.additionalMouseResources, |
| 428 | &mLocked.animationResources, |
| 429 | mLocked.viewport.displayId); |
| 430 | } |
| 431 | |
| 432 | mLocked.updatePointerIcon = true; |
| 433 | } |
| 434 | |
| 435 | bool MouseCursorController::isViewportValid() { |
| 436 | std::scoped_lock lock(mLock); |
| 437 | return mLocked.viewport.isValid(); |
| 438 | } |
| 439 | |
| 440 | void MouseCursorController::getAdditionalMouseResources() { |
| 441 | std::scoped_lock lock(mLock); |
| 442 | |
| 443 | if (mLocked.additionalMouseResources.empty()) { |
| 444 | mContext.getPolicy()->loadAdditionalMouseResources(&mLocked.additionalMouseResources, |
| 445 | &mLocked.animationResources, |
| 446 | mLocked.viewport.displayId); |
| 447 | } |
| 448 | mLocked.updatePointerIcon = true; |
| 449 | updatePointerLocked(); |
| 450 | } |
| 451 | |
| 452 | bool MouseCursorController::resourcesLoaded() { |
| 453 | std::scoped_lock lock(mLock); |
| 454 | return mLocked.resourcesLoaded; |
| 455 | } |
| 456 | |
Arpit Singh | 05215af | 2024-08-25 17:49:32 +0000 | [diff] [blame] | 457 | std::string MouseCursorController::dump() const { |
| 458 | std::string dump = INDENT "MouseCursorController:\n"; |
| 459 | std::scoped_lock lock(mLock); |
| 460 | dump += StringPrintf(INDENT2 "viewport: %s\n", mLocked.viewport.toString().c_str()); |
| 461 | dump += StringPrintf(INDENT2 "stylusHoverMode: %s\n", |
| 462 | mLocked.stylusHoverMode ? "true" : "false"); |
| 463 | dump += StringPrintf(INDENT2 "pointerFadeDirection: %d\n", mLocked.pointerFadeDirection); |
| 464 | dump += StringPrintf(INDENT2 "updatePointerIcon: %s\n", |
| 465 | mLocked.updatePointerIcon ? "true" : "false"); |
| 466 | dump += StringPrintf(INDENT2 "resourcesLoaded: %s\n", |
| 467 | mLocked.resourcesLoaded ? "true" : "false"); |
| 468 | dump += StringPrintf(INDENT2 "requestedPointerType: %d\n", mLocked.requestedPointerType); |
| 469 | dump += StringPrintf(INDENT2 "resolvedPointerType: %d\n", mLocked.resolvedPointerType); |
| 470 | dump += StringPrintf(INDENT2 "skipScreenshot: %s\n", mLocked.skipScreenshot ? "true" : "false"); |
| 471 | dump += StringPrintf(INDENT2 "animating: %s\n", mLocked.animating ? "true" : "false"); |
| 472 | return dump; |
| 473 | } |
| 474 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 475 | bool MouseCursorController::doAnimations(nsecs_t timestamp) { |
| 476 | std::scoped_lock lock(mLock); |
| 477 | bool keepFading = doFadingAnimationLocked(timestamp); |
| 478 | bool keepBitmap = doBitmapAnimationLocked(timestamp); |
| 479 | bool keepAnimating = keepFading || keepBitmap; |
| 480 | if (!keepAnimating) { |
| 481 | /* |
| 482 | * We know that this callback will be removed before another |
| 483 | * is added. mLock in PointerAnimator will not be released |
| 484 | * until after this is removed, and adding another callback |
| 485 | * requires that lock. Thus it's safe to set mLocked.animating |
| 486 | * here. |
| 487 | */ |
| 488 | mLocked.animating = false; |
| 489 | } |
| 490 | return keepAnimating; |
| 491 | } |
| 492 | |
| 493 | void MouseCursorController::startAnimationLocked() REQUIRES(mLock) { |
| 494 | using namespace std::placeholders; |
| 495 | |
| 496 | if (mLocked.animating) { |
| 497 | return; |
| 498 | } |
| 499 | mLocked.animating = true; |
| 500 | |
| 501 | std::function<bool(nsecs_t)> func = std::bind(&MouseCursorController::doAnimations, this, _1); |
| 502 | /* |
Siarhei Vishniakou | 445af18 | 2024-05-13 13:17:05 -0700 | [diff] [blame] | 503 | * Using ui::LogicalDisplayId::INVALID for displayId here to avoid removing the callback |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 504 | * if a TouchSpotController with the same display is removed. |
| 505 | */ |
Siarhei Vishniakou | 445af18 | 2024-05-13 13:17:05 -0700 | [diff] [blame] | 506 | mContext.addAnimationCallback(ui::LogicalDisplayId::INVALID, func); |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 509 | } // namespace android |