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" |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 24 | #include "MouseCursorController.h" |
| 25 | #include "PointerControllerContext.h" |
| 26 | #include "TouchSpotController.h" |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 29 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 30 | #include <SkBitmap.h> |
| 31 | #include <SkBlendMode.h> |
| 32 | #include <SkCanvas.h> |
| 33 | #include <SkColor.h> |
| 34 | #include <SkPaint.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | // --- PointerController --- |
| 39 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 40 | std::shared_ptr<PointerController> PointerController::create( |
| 41 | const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
| 42 | const sp<SpriteController>& spriteController) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 43 | // using 'new' to access non-public constructor |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 44 | std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>( |
| 45 | new PointerController(policy, looper, spriteController)); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 46 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 47 | /* |
| 48 | * Now we need to hook up the constructed PointerController object to its callbacks. |
| 49 | * |
| 50 | * This must be executed after the constructor but before any other methods on PointerController |
| 51 | * in order to ensure that the fully constructed object is visible on the Looper thread, since |
| 52 | * that may be a different thread than where the PointerController is initially constructed. |
| 53 | * |
| 54 | * Unfortunately, this cannot be done as part of the constructor since we need to hand out |
| 55 | * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr. |
| 56 | */ |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 57 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 58 | controller->mContext.setHandlerController(controller); |
| 59 | controller->mContext.setCallbackController(controller); |
| 60 | controller->mContext.initializeDisplayEventReceiver(); |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 61 | return controller; |
| 62 | } |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 63 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 64 | PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 65 | const sp<Looper>& looper, |
| 66 | const sp<SpriteController>& spriteController) |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 67 | : mContext(policy, looper, spriteController, *this), mCursorController(mContext) { |
| 68 | std::scoped_lock lock(mLock); |
| 69 | mLocked.presentation = Presentation::SPOT; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 72 | bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX, |
| 73 | float* outMaxY) const { |
| 74 | return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void PointerController::move(float deltaX, float deltaY) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 78 | mCursorController.move(deltaX, deltaY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 81 | void PointerController::setButtonState(int32_t buttonState) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 82 | mCursorController.setButtonState(buttonState); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 85 | int32_t PointerController::getButtonState() const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 86 | return mCursorController.getButtonState(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void PointerController::setPosition(float x, float y) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 90 | std::scoped_lock lock(mLock); |
| 91 | mCursorController.setPosition(x, y); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void PointerController::getPosition(float* outX, float* outY) const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 95 | mCursorController.getPosition(outX, outY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 98 | int32_t PointerController::getDisplayId() const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 99 | return mCursorController.getDisplayId(); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 102 | void PointerController::fade(Transition transition) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 103 | std::scoped_lock lock(mLock); |
| 104 | mCursorController.fade(transition); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 107 | void PointerController::unfade(Transition transition) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 108 | std::scoped_lock lock(mLock); |
| 109 | mCursorController.unfade(transition); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 112 | void PointerController::setPresentation(Presentation presentation) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 113 | std::scoped_lock lock(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 114 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 115 | if (mLocked.presentation == presentation) { |
| 116 | return; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 119 | mLocked.presentation = presentation; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 120 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 121 | if (!mCursorController.isViewportValid()) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 125 | if (presentation == Presentation::POINTER) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 126 | mCursorController.getAdditionalMouseResources(); |
| 127 | clearSpotsLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 131 | void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
| 132 | BitSet32 spotIdBits, int32_t displayId) { |
| 133 | std::scoped_lock lock(mLock); |
| 134 | auto it = mLocked.spotControllers.find(displayId); |
| 135 | if (it == mLocked.spotControllers.end()) { |
| 136 | mLocked.spotControllers.try_emplace(displayId, displayId, mContext); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 137 | } |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 138 | mLocked.spotControllers.at(displayId).setSpots(spotCoords, spotIdToIndex, spotIdBits); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void PointerController::clearSpots() { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 142 | std::scoped_lock lock(mLock); |
| 143 | clearSpotsLocked(); |
| 144 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 145 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 146 | void PointerController::clearSpotsLocked() REQUIRES(mLock) { |
| 147 | for (auto& [displayID, spotController] : mLocked.spotControllers) { |
| 148 | spotController.clearSpots(); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 149 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 153 | mContext.setInactivityTimeout(inactivityTimeout); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 156 | void PointerController::reloadPointerResources() { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 157 | std::scoped_lock lock(mLock); |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 158 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 159 | for (auto& [displayID, spotController] : mLocked.spotControllers) { |
| 160 | spotController.reloadSpotResources(); |
| 161 | } |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 162 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 163 | if (mCursorController.resourcesLoaded()) { |
| 164 | bool getAdditionalMouseResources = false; |
| 165 | if (mLocked.presentation == PointerController::Presentation::POINTER) { |
| 166 | getAdditionalMouseResources = true; |
| 167 | } |
| 168 | mCursorController.reloadPointerResources(getAdditionalMouseResources); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | void PointerController::setDisplayViewport(const DisplayViewport& viewport) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 173 | std::scoped_lock lock(mLock); |
| 174 | |
| 175 | bool getAdditionalMouseResources = false; |
| 176 | if (mLocked.presentation == PointerController::Presentation::POINTER) { |
| 177 | getAdditionalMouseResources = true; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 178 | } |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 179 | mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 182 | void PointerController::updatePointerIcon(int32_t iconId) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 183 | std::scoped_lock lock(mLock); |
| 184 | mCursorController.updatePointerIcon(iconId); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 187 | void PointerController::setCustomPointerIcon(const SpriteIcon& icon) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 188 | std::scoped_lock lock(mLock); |
| 189 | mCursorController.setCustomPointerIcon(icon); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void PointerController::doAnimate(nsecs_t timestamp) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 193 | std::scoped_lock lock(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 194 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 195 | mContext.setAnimationPending(false); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 196 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 197 | bool keepFading = false; |
| 198 | keepFading = mCursorController.doFadingAnimation(timestamp, keepFading); |
| 199 | |
| 200 | for (auto& [displayID, spotController] : mLocked.spotControllers) { |
| 201 | keepFading = spotController.doFadingAnimation(timestamp, keepFading); |
| 202 | } |
| 203 | |
| 204 | bool keepBitmapFlipping = mCursorController.doBitmapAnimation(timestamp); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 205 | if (keepFading || keepBitmapFlipping) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 206 | mContext.startAnimation(); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 210 | void PointerController::doInactivityTimeout() { |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 211 | fade(Transition::GRADUAL); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 214 | void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) { |
| 215 | std::unordered_set<int32_t> displayIdSet; |
| 216 | for (DisplayViewport viewport : viewports) { |
| 217 | displayIdSet.insert(viewport.displayId); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 220 | std::scoped_lock lock(mLock); |
| 221 | for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) { |
| 222 | int32_t displayID = it->first; |
| 223 | if (!displayIdSet.count(displayID)) { |
| 224 | it = mLocked.spotControllers.erase(it); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 225 | } else { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame^] | 226 | ++it; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 231 | } // namespace android |