blob: 7462481f8779c5c55e93aea464f20a476817ec7e [file] [log] [blame]
Liam Harringtonc782be62020-07-17 19:48:24 +00001/*
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 "TouchSpotController"
18
19// Log debug messages about pointer updates
20#define DEBUG_SPOT_UPDATES 0
21
22#include "TouchSpotController.h"
23
Michael Wright20f5fd82022-10-28 14:12:24 +010024#include <android-base/stringprintf.h>
25#include <input/PrintTools.h>
Liam Harringtonc782be62020-07-17 19:48:24 +000026#include <log/log.h>
27
Michael Wright20f5fd82022-10-28 14:12:24 +010028#include <mutex>
29
30#define INDENT " "
31#define INDENT2 " "
32
Liam Harringtonc782be62020-07-17 19:48:24 +000033namespace {
34// Time to spend fading out the spot completely.
35const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
36} // namespace
37
38namespace android {
39
40// --- Spot ---
41
Prabir Pradhan4cc1a632023-06-09 21:31:26 +000042void TouchSpotController::Spot::updateSprite(const SpriteIcon* icon, float newX, float newY,
Linnan Li0defadf2024-05-05 19:17:05 +080043 ui::LogicalDisplayId displayId, bool skipScreenshot) {
Liam Harringtonc782be62020-07-17 19:48:24 +000044 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
45 sprite->setAlpha(alpha);
46 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
Prabir Pradhan4cc1a632023-06-09 21:31:26 +000047 sprite->setPosition(newX, newY);
Liam Harringtonc782be62020-07-17 19:48:24 +000048 sprite->setDisplayId(displayId);
Arpit Singh80fd68a2024-03-26 18:41:06 +000049 sprite->setSkipScreenshot(skipScreenshot);
Prabir Pradhan4cc1a632023-06-09 21:31:26 +000050 x = newX;
51 y = newY;
Liam Harringtonc782be62020-07-17 19:48:24 +000052
53 if (icon != mLastIcon) {
54 mLastIcon = icon;
55 if (icon) {
56 sprite->setIcon(*icon);
57 sprite->setVisible(true);
58 } else {
59 sprite->setVisible(false);
60 }
61 }
62}
63
Michael Wright20f5fd82022-10-28 14:12:24 +010064void TouchSpotController::Spot::dump(std::string& out, const char* prefix) const {
65 out += prefix;
66 base::StringAppendF(&out, "Spot{id=%" PRIx32 ", alpha=%f, scale=%f, pos=[%f, %f]}\n", id, alpha,
67 scale, x, y);
68}
69
Liam Harringtonc782be62020-07-17 19:48:24 +000070// --- TouchSpotController ---
71
Linnan Li0defadf2024-05-05 19:17:05 +080072TouchSpotController::TouchSpotController(ui::LogicalDisplayId displayId,
73 PointerControllerContext& context)
Liam Harringtonc782be62020-07-17 19:48:24 +000074 : mDisplayId(displayId), mContext(context) {
75 mContext.getPolicy()->loadPointerResources(&mResources, mDisplayId);
76}
77
78TouchSpotController::~TouchSpotController() {
79 std::scoped_lock lock(mLock);
80
81 size_t numSpots = mLocked.displaySpots.size();
82 for (size_t i = 0; i < numSpots; i++) {
83 delete mLocked.displaySpots[i];
84 }
85 mLocked.displaySpots.clear();
86}
87
88void TouchSpotController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
Arpit Singh80fd68a2024-03-26 18:41:06 +000089 BitSet32 spotIdBits, bool skipScreenshot) {
Liam Harringtonc782be62020-07-17 19:48:24 +000090#if DEBUG_SPOT_UPDATES
91 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
92 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
93 uint32_t id = idBits.firstMarkedBit();
94 idBits.clearBit(id);
95 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
96 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f, displayId=%" PRId32 ".", id,
97 c.getAxisValue(AMOTION_EVENT_AXIS_X), c.getAxisValue(AMOTION_EVENT_AXIS_Y),
Linnan Li0defadf2024-05-05 19:17:05 +080098 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), mDisplayId.id);
Liam Harringtonc782be62020-07-17 19:48:24 +000099 }
100#endif
101
102 std::scoped_lock lock(mLock);
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000103 auto& spriteController = mContext.getSpriteController();
104 spriteController.openTransaction();
Liam Harringtonc782be62020-07-17 19:48:24 +0000105
106 // Add or move spots for fingers that are down.
107 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
108 uint32_t id = idBits.clearFirstMarkedBit();
109 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
110 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
111 ? mResources.spotTouch
112 : mResources.spotHover;
113 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
114 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
115
116 Spot* spot = getSpot(id, mLocked.displaySpots);
117 if (!spot) {
118 spot = createAndAddSpotLocked(id, mLocked.displaySpots);
119 }
120
Arpit Singh80fd68a2024-03-26 18:41:06 +0000121 spot->updateSprite(&icon, x, y, mDisplayId, skipScreenshot);
Liam Harringtonc782be62020-07-17 19:48:24 +0000122 }
123
124 for (Spot* spot : mLocked.displaySpots) {
125 if (spot->id != Spot::INVALID_ID && !spotIdBits.hasBit(spot->id)) {
126 fadeOutAndReleaseSpotLocked(spot);
127 }
128 }
129
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000130 spriteController.closeTransaction();
Liam Harringtonc782be62020-07-17 19:48:24 +0000131}
132
133void TouchSpotController::clearSpots() {
134#if DEBUG_SPOT_UPDATES
135 ALOGD("clearSpots");
136#endif
137
138 std::scoped_lock lock(mLock);
139 fadeOutAndReleaseAllSpotsLocked();
140}
141
142TouchSpotController::Spot* TouchSpotController::getSpot(uint32_t id,
143 const std::vector<Spot*>& spots) {
144 for (size_t i = 0; i < spots.size(); i++) {
145 Spot* spot = spots[i];
146 if (spot->id == id) {
147 return spot;
148 }
149 }
150 return nullptr;
151}
152
153TouchSpotController::Spot* TouchSpotController::createAndAddSpotLocked(uint32_t id,
Liam Harringtonce637132020-08-14 04:00:11 +0000154 std::vector<Spot*>& spots)
155 REQUIRES(mLock) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000156 // Remove spots until we have fewer than MAX_SPOTS remaining.
157 while (spots.size() >= MAX_SPOTS) {
158 Spot* spot = removeFirstFadingSpotLocked(spots);
159 if (!spot) {
160 spot = spots[0];
161 spots.erase(spots.begin());
162 }
163 releaseSpotLocked(spot);
164 }
165
166 // Obtain a sprite from the recycled pool.
167 sp<Sprite> sprite;
168 if (!mLocked.recycledSprites.empty()) {
169 sprite = mLocked.recycledSprites.back();
170 mLocked.recycledSprites.pop_back();
171 } else {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000172 sprite = mContext.getSpriteController().createSprite();
Liam Harringtonc782be62020-07-17 19:48:24 +0000173 }
174
175 // Return the new spot.
176 Spot* spot = new Spot(id, sprite);
177 spots.push_back(spot);
178 return spot;
179}
180
181TouchSpotController::Spot* TouchSpotController::removeFirstFadingSpotLocked(
182 std::vector<Spot*>& spots) REQUIRES(mLock) {
183 for (size_t i = 0; i < spots.size(); i++) {
184 Spot* spot = spots[i];
185 if (spot->id == Spot::INVALID_ID) {
186 spots.erase(spots.begin() + i);
187 return spot;
188 }
189 }
190 return NULL;
191}
192
193void TouchSpotController::releaseSpotLocked(Spot* spot) REQUIRES(mLock) {
194 spot->sprite->clearIcon();
195
196 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
197 mLocked.recycledSprites.push_back(spot->sprite);
198 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000199 delete spot;
200}
201
202void TouchSpotController::fadeOutAndReleaseSpotLocked(Spot* spot) REQUIRES(mLock) {
203 if (spot->id != Spot::INVALID_ID) {
204 spot->id = Spot::INVALID_ID;
Liam Harringtonce637132020-08-14 04:00:11 +0000205 startAnimationLocked();
Liam Harringtonc782be62020-07-17 19:48:24 +0000206 }
207}
208
209void TouchSpotController::fadeOutAndReleaseAllSpotsLocked() REQUIRES(mLock) {
210 size_t numSpots = mLocked.displaySpots.size();
211 for (size_t i = 0; i < numSpots; i++) {
212 Spot* spot = mLocked.displaySpots[i];
213 fadeOutAndReleaseSpotLocked(spot);
214 }
215}
216
217void TouchSpotController::reloadSpotResources() {
218 mContext.getPolicy()->loadPointerResources(&mResources, mDisplayId);
219}
220
Liam Harringtonce637132020-08-14 04:00:11 +0000221bool TouchSpotController::doAnimations(nsecs_t timestamp) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000222 std::scoped_lock lock(mLock);
Liam Harringtonce637132020-08-14 04:00:11 +0000223 bool keepAnimating = doFadingAnimationLocked(timestamp);
224 if (!keepAnimating) {
225 /*
226 * We know that this callback will be removed before another
227 * is added. mLock in PointerAnimator will not be released
228 * until after this is removed, and adding another callback
229 * requires that lock. Thus it's safe to set mLocked.animating
230 * here.
231 */
232 mLocked.animating = false;
233 }
234 return keepAnimating;
235}
236
237bool TouchSpotController::doFadingAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) {
238 bool keepAnimating = false;
Liam Harringtonc782be62020-07-17 19:48:24 +0000239 nsecs_t animationTime = mContext.getAnimationTime();
240 nsecs_t frameDelay = timestamp - animationTime;
241 size_t numSpots = mLocked.displaySpots.size();
242 for (size_t i = 0; i < numSpots;) {
243 Spot* spot = mLocked.displaySpots[i];
244 if (spot->id == Spot::INVALID_ID) {
245 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
246 if (spot->alpha <= 0) {
247 mLocked.displaySpots.erase(mLocked.displaySpots.begin() + i);
248 releaseSpotLocked(spot);
249 numSpots--;
250 continue;
251 } else {
252 spot->sprite->setAlpha(spot->alpha);
253 keepAnimating = true;
254 }
255 }
256 ++i;
257 }
258 return keepAnimating;
259}
260
Liam Harringtonce637132020-08-14 04:00:11 +0000261void TouchSpotController::startAnimationLocked() REQUIRES(mLock) {
262 using namespace std::placeholders;
263
264 if (mLocked.animating) {
265 return;
266 }
267 mLocked.animating = true;
268
269 std::function<bool(nsecs_t)> func = std::bind(&TouchSpotController::doAnimations, this, _1);
270 mContext.addAnimationCallback(mDisplayId, func);
271}
272
Michael Wright20f5fd82022-10-28 14:12:24 +0100273void TouchSpotController::dump(std::string& out, const char* prefix) const {
274 using base::StringAppendF;
275 out += prefix;
276 out += "SpotController:\n";
277 out += prefix;
Linnan Li0defadf2024-05-05 19:17:05 +0800278 StringAppendF(&out, INDENT "DisplayId: %s\n", mDisplayId.toString().c_str());
Michael Wright20f5fd82022-10-28 14:12:24 +0100279 std::scoped_lock lock(mLock);
280 out += prefix;
281 StringAppendF(&out, INDENT "Animating: %s\n", toString(mLocked.animating));
282 out += prefix;
283 out += INDENT "Spots:\n";
284 std::string spotPrefix = prefix;
285 spotPrefix += INDENT2;
286 for (const auto& spot : mLocked.displaySpots) {
287 spot->dump(out, spotPrefix.c_str());
288 }
289}
290
Liam Harringtonc782be62020-07-17 19:48:24 +0000291} // namespace android