1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "TouchSpotController"
// Log debug messages about pointer updates
#define DEBUG_SPOT_UPDATES 0
#include "TouchSpotController.h"
#include <log/log.h>
#include <SkBitmap.h>
#include <SkBlendMode.h>
#include <SkCanvas.h>
#include <SkColor.h>
#include <SkPaint.h>
namespace {
// Time to spend fading out the spot completely.
const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
} // namespace
namespace android {
// --- Spot ---
void TouchSpotController::Spot::updateSprite(const SpriteIcon* icon, float x, float y,
int32_t displayId) {
sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
sprite->setAlpha(alpha);
sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
sprite->setPosition(x, y);
sprite->setDisplayId(displayId);
this->x = x;
this->y = y;
if (icon != mLastIcon) {
mLastIcon = icon;
if (icon) {
sprite->setIcon(*icon);
sprite->setVisible(true);
} else {
sprite->setVisible(false);
}
}
}
// --- TouchSpotController ---
TouchSpotController::TouchSpotController(int32_t displayId, PointerControllerContext& context)
: mDisplayId(displayId), mContext(context) {
mContext.getPolicy()->loadPointerResources(&mResources, mDisplayId);
}
TouchSpotController::~TouchSpotController() {
std::scoped_lock lock(mLock);
size_t numSpots = mLocked.displaySpots.size();
for (size_t i = 0; i < numSpots; i++) {
delete mLocked.displaySpots[i];
}
mLocked.displaySpots.clear();
}
void TouchSpotController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
BitSet32 spotIdBits) {
#if DEBUG_SPOT_UPDATES
ALOGD("setSpots: idBits=%08x", spotIdBits.value);
for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
uint32_t id = idBits.firstMarkedBit();
idBits.clearBit(id);
const PointerCoords& c = spotCoords[spotIdToIndex[id]];
ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f, displayId=%" PRId32 ".", id,
c.getAxisValue(AMOTION_EVENT_AXIS_X), c.getAxisValue(AMOTION_EVENT_AXIS_Y),
c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), displayId);
}
#endif
std::scoped_lock lock(mLock);
sp<SpriteController> spriteController = mContext.getSpriteController();
spriteController->openTransaction();
// Add or move spots for fingers that are down.
for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
uint32_t id = idBits.clearFirstMarkedBit();
const PointerCoords& c = spotCoords[spotIdToIndex[id]];
const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
? mResources.spotTouch
: mResources.spotHover;
float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
Spot* spot = getSpot(id, mLocked.displaySpots);
if (!spot) {
spot = createAndAddSpotLocked(id, mLocked.displaySpots);
}
spot->updateSprite(&icon, x, y, mDisplayId);
}
for (Spot* spot : mLocked.displaySpots) {
if (spot->id != Spot::INVALID_ID && !spotIdBits.hasBit(spot->id)) {
fadeOutAndReleaseSpotLocked(spot);
}
}
spriteController->closeTransaction();
}
void TouchSpotController::clearSpots() {
#if DEBUG_SPOT_UPDATES
ALOGD("clearSpots");
#endif
std::scoped_lock lock(mLock);
fadeOutAndReleaseAllSpotsLocked();
}
TouchSpotController::Spot* TouchSpotController::getSpot(uint32_t id,
const std::vector<Spot*>& spots) {
for (size_t i = 0; i < spots.size(); i++) {
Spot* spot = spots[i];
if (spot->id == id) {
return spot;
}
}
return nullptr;
}
TouchSpotController::Spot* TouchSpotController::createAndAddSpotLocked(uint32_t id,
std::vector<Spot*>& spots) {
// Remove spots until we have fewer than MAX_SPOTS remaining.
while (spots.size() >= MAX_SPOTS) {
Spot* spot = removeFirstFadingSpotLocked(spots);
if (!spot) {
spot = spots[0];
spots.erase(spots.begin());
}
releaseSpotLocked(spot);
}
// Obtain a sprite from the recycled pool.
sp<Sprite> sprite;
if (!mLocked.recycledSprites.empty()) {
sprite = mLocked.recycledSprites.back();
mLocked.recycledSprites.pop_back();
} else {
sprite = mContext.getSpriteController()->createSprite();
}
// Return the new spot.
Spot* spot = new Spot(id, sprite);
spots.push_back(spot);
return spot;
}
TouchSpotController::Spot* TouchSpotController::removeFirstFadingSpotLocked(
std::vector<Spot*>& spots) REQUIRES(mLock) {
for (size_t i = 0; i < spots.size(); i++) {
Spot* spot = spots[i];
if (spot->id == Spot::INVALID_ID) {
spots.erase(spots.begin() + i);
return spot;
}
}
return NULL;
}
void TouchSpotController::releaseSpotLocked(Spot* spot) REQUIRES(mLock) {
spot->sprite->clearIcon();
if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
mLocked.recycledSprites.push_back(spot->sprite);
}
delete spot;
}
void TouchSpotController::fadeOutAndReleaseSpotLocked(Spot* spot) REQUIRES(mLock) {
if (spot->id != Spot::INVALID_ID) {
spot->id = Spot::INVALID_ID;
mContext.startAnimation();
}
}
void TouchSpotController::fadeOutAndReleaseAllSpotsLocked() REQUIRES(mLock) {
size_t numSpots = mLocked.displaySpots.size();
for (size_t i = 0; i < numSpots; i++) {
Spot* spot = mLocked.displaySpots[i];
fadeOutAndReleaseSpotLocked(spot);
}
}
void TouchSpotController::reloadSpotResources() {
mContext.getPolicy()->loadPointerResources(&mResources, mDisplayId);
}
bool TouchSpotController::doFadingAnimation(nsecs_t timestamp, bool keepAnimating) {
std::scoped_lock lock(mLock);
nsecs_t animationTime = mContext.getAnimationTime();
nsecs_t frameDelay = timestamp - animationTime;
size_t numSpots = mLocked.displaySpots.size();
for (size_t i = 0; i < numSpots;) {
Spot* spot = mLocked.displaySpots[i];
if (spot->id == Spot::INVALID_ID) {
spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
if (spot->alpha <= 0) {
mLocked.displaySpots.erase(mLocked.displaySpots.begin() + i);
releaseSpotLocked(spot);
numSpots--;
continue;
} else {
spot->sprite->setAlpha(spot->alpha);
keepAnimating = true;
}
}
++i;
}
return keepAnimating;
}
} // namespace android
|