blob: 80d8e72a87e26c12be8f4c848336aad5520a61e3 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
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 Brownb4ff35d2011-01-02 16:37:43 -080018//#define LOG_NDEBUG 0
19
20// Log debug messages about pointer updates
21#define DEBUG_POINTER_UPDATES 0
22
23#include "PointerController.h"
24
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070025#include <log/log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080026
27#include <SkBitmap.h>
28#include <SkCanvas.h>
29#include <SkColor.h>
30#include <SkPaint.h>
Mike Reedc2f31df2016-10-28 17:21:45 -040031#include <SkBlendMode.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032
33namespace android {
34
Vladislav Kaznacheev33c59032016-09-09 10:03:31 -070035// --- WeakLooperCallback ---
36
37class WeakLooperCallback: public LooperCallback {
38protected:
39 virtual ~WeakLooperCallback() { }
40
41public:
42 WeakLooperCallback(const wp<LooperCallback>& callback) :
43 mCallback(callback) {
44 }
45
46 virtual int handleEvent(int fd, int events, void* data) {
47 sp<LooperCallback> callback = mCallback.promote();
48 if (callback != NULL) {
49 return callback->handleEvent(fd, events, data);
50 }
51 return 0; // the client is gone, remove the callback
52 }
53
54private:
55 wp<LooperCallback> mCallback;
56};
57
Jeff Brownb4ff35d2011-01-02 16:37:43 -080058// --- PointerController ---
59
Jeff Brown05dc66a2011-03-02 14:41:58 -080060// Time to wait before starting the fade when the pointer is inactive.
Jeff Brown2352b972011-04-12 22:39:53 -070061static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
62static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
63
Jeff Brown2352b972011-04-12 22:39:53 -070064// Time to spend fading out the spot completely.
65static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080066
67// Time to spend fading out the pointer completely.
Jeff Brown2352b972011-04-12 22:39:53 -070068static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080069
Jun Mukaic0c0ac32015-10-27 10:09:21 -070070// The number of events to be read at once for DisplayEventReceiver.
71static const int EVENT_BUFFER_SIZE = 100;
Jeff Brown05dc66a2011-03-02 14:41:58 -080072
Jeff Brown2352b972011-04-12 22:39:53 -070073// --- PointerController ---
74
75PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
76 const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
77 mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
Jeff Brown5541de92011-04-11 11:54:25 -070078 mHandler = new WeakMessageHandler(this);
Vladislav Kaznacheev33c59032016-09-09 10:03:31 -070079 mCallback = new WeakLooperCallback(this);
Jeff Brown5541de92011-04-11 11:54:25 -070080
Jun Mukaic0c0ac32015-10-27 10:09:21 -070081 if (mDisplayEventReceiver.initCheck() == NO_ERROR) {
82 mLooper->addFd(mDisplayEventReceiver.getFd(), Looper::POLL_CALLBACK,
Vladislav Kaznacheev33c59032016-09-09 10:03:31 -070083 Looper::EVENT_INPUT, mCallback, nullptr);
Jun Mukaic0c0ac32015-10-27 10:09:21 -070084 } else {
85 ALOGE("Failed to initialize DisplayEventReceiver.");
86 }
87
Jeff Brownb4ff35d2011-01-02 16:37:43 -080088 AutoMutex _l(mLock);
89
Jeff Brown2352b972011-04-12 22:39:53 -070090 mLocked.animationPending = false;
91
Jeff Brown2352b972011-04-12 22:39:53 -070092 mLocked.presentation = PRESENTATION_POINTER;
93 mLocked.presentationChanged = false;
94
95 mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
96
Jeff Brown538881e2011-05-25 18:23:38 -070097 mLocked.pointerFadeDirection = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080098 mLocked.pointerX = 0;
99 mLocked.pointerY = 0;
Jeff Brown538881e2011-05-25 18:23:38 -0700100 mLocked.pointerAlpha = 0.0f; // pointer is initially faded
Jeff Brown2352b972011-04-12 22:39:53 -0700101 mLocked.pointerSprite = mSpriteController->createSprite();
102 mLocked.pointerIconChanged = false;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100103 mLocked.requestedPointerType = mPolicy->getDefaultPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700104
Jun Mukai808196f2015-10-28 16:46:44 -0700105 mLocked.animationFrameIndex = 0;
106 mLocked.lastFrameUpdatedTime = 0;
107
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800108 mLocked.buttonState = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800109}
110
111PointerController::~PointerController() {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800112 mLooper->removeMessages(mHandler);
113
Jeff Brown5541de92011-04-11 11:54:25 -0700114 AutoMutex _l(mLock);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800115
Jeff Brown2352b972011-04-12 22:39:53 -0700116 mLocked.pointerSprite.clear();
117
118 for (size_t i = 0; i < mLocked.spots.size(); i++) {
119 delete mLocked.spots.itemAt(i);
120 }
121 mLocked.spots.clear();
122 mLocked.recycledSprites.clear();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800123}
124
125bool PointerController::getBounds(float* outMinX, float* outMinY,
126 float* outMaxX, float* outMaxY) const {
127 AutoMutex _l(mLock);
128
129 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
130}
131
132bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
133 float* outMaxX, float* outMaxY) const {
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700134
135 if (!mLocked.viewport.isValid()) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136 return false;
137 }
138
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700139 *outMinX = mLocked.viewport.logicalLeft;
140 *outMinY = mLocked.viewport.logicalTop;
141 *outMaxX = mLocked.viewport.logicalRight - 1;
142 *outMaxY = mLocked.viewport.logicalBottom - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800143 return true;
144}
145
146void PointerController::move(float deltaX, float deltaY) {
147#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000148 ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800149#endif
150 if (deltaX == 0.0f && deltaY == 0.0f) {
151 return;
152 }
153
154 AutoMutex _l(mLock);
155
156 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
157}
158
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700159void PointerController::setButtonState(int32_t buttonState) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800160#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000161 ALOGD("Set button state 0x%08x", buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800162#endif
163 AutoMutex _l(mLock);
164
165 if (mLocked.buttonState != buttonState) {
166 mLocked.buttonState = buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800167 }
168}
169
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700170int32_t PointerController::getButtonState() const {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800171 AutoMutex _l(mLock);
172
173 return mLocked.buttonState;
174}
175
176void PointerController::setPosition(float x, float y) {
177#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000178 ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800179#endif
180 AutoMutex _l(mLock);
181
182 setPositionLocked(x, y);
183}
184
185void PointerController::setPositionLocked(float x, float y) {
186 float minX, minY, maxX, maxY;
187 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
188 if (x <= minX) {
189 mLocked.pointerX = minX;
190 } else if (x >= maxX) {
191 mLocked.pointerX = maxX;
192 } else {
193 mLocked.pointerX = x;
194 }
195 if (y <= minY) {
196 mLocked.pointerY = minY;
197 } else if (y >= maxY) {
198 mLocked.pointerY = maxY;
199 } else {
200 mLocked.pointerY = y;
201 }
Jeff Brown2352b972011-04-12 22:39:53 -0700202 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800203 }
204}
205
206void PointerController::getPosition(float* outX, float* outY) const {
207 AutoMutex _l(mLock);
208
209 *outX = mLocked.pointerX;
210 *outY = mLocked.pointerY;
211}
212
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700213int32_t PointerController::getDisplayId() const {
214 AutoMutex _l(mLock);
215
216 return mLocked.viewport.displayId;
217}
218
Jeff Brown538881e2011-05-25 18:23:38 -0700219void PointerController::fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800220 AutoMutex _l(mLock);
221
Jeff Brown538881e2011-05-25 18:23:38 -0700222 // Remove the inactivity timeout, since we are fading now.
223 removeInactivityTimeoutLocked();
224
225 // Start fading.
226 if (transition == TRANSITION_IMMEDIATE) {
227 mLocked.pointerFadeDirection = 0;
228 mLocked.pointerAlpha = 0.0f;
229 updatePointerLocked();
230 } else {
231 mLocked.pointerFadeDirection = -1;
232 startAnimationLocked();
233 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800234}
235
Jeff Brown538881e2011-05-25 18:23:38 -0700236void PointerController::unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800237 AutoMutex _l(mLock);
238
Jeff Brown2352b972011-04-12 22:39:53 -0700239 // Always reset the inactivity timer.
240 resetInactivityTimeoutLocked();
241
Jeff Brown538881e2011-05-25 18:23:38 -0700242 // Start unfading.
243 if (transition == TRANSITION_IMMEDIATE) {
244 mLocked.pointerFadeDirection = 0;
Jeff Brown2352b972011-04-12 22:39:53 -0700245 mLocked.pointerAlpha = 1.0f;
246 updatePointerLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700247 } else {
248 mLocked.pointerFadeDirection = 1;
249 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800250 }
251}
252
Jeff Brown2352b972011-04-12 22:39:53 -0700253void PointerController::setPresentation(Presentation presentation) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800254 AutoMutex _l(mLock);
255
Jun Mukai1db53972015-09-11 18:08:31 -0700256 if (presentation == PRESENTATION_POINTER && mLocked.additionalMouseResources.empty()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700257 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
258 &mLocked.animationResources);
Jun Mukai1db53972015-09-11 18:08:31 -0700259 }
260
Jeff Brown2352b972011-04-12 22:39:53 -0700261 if (mLocked.presentation != presentation) {
262 mLocked.presentation = presentation;
263 mLocked.presentationChanged = true;
264
265 if (presentation != PRESENTATION_SPOT) {
266 fadeOutAndReleaseAllSpotsLocked();
267 }
268
269 updatePointerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800270 }
271}
272
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700273void PointerController::setSpots(const PointerCoords* spotCoords,
274 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
Jeff Brown2352b972011-04-12 22:39:53 -0700275#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000276 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
Jeff Brown2352b972011-04-12 22:39:53 -0700277 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
278 uint32_t id = idBits.firstMarkedBit();
279 idBits.clearBit(id);
280 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
Steve Block5baa3a62011-12-20 16:23:08 +0000281 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
Jeff Brown2352b972011-04-12 22:39:53 -0700282 c.getAxisValue(AMOTION_EVENT_AXIS_X),
283 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
284 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
285 }
286#endif
287
288 AutoMutex _l(mLock);
289
290 mSpriteController->openTransaction();
291
292 // Add or move spots for fingers that are down.
293 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700294 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -0700295 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
296 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
297 ? mResources.spotTouch : mResources.spotHover;
298 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
299 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
300
301 Spot* spot = getSpotLocked(id);
302 if (!spot) {
303 spot = createAndAddSpotLocked(id);
304 }
305
306 spot->updateSprite(&icon, x, y);
307 }
308
309 // Remove spots for fingers that went up.
310 for (size_t i = 0; i < mLocked.spots.size(); i++) {
311 Spot* spot = mLocked.spots.itemAt(i);
312 if (spot->id != Spot::INVALID_ID
313 && !spotIdBits.hasBit(spot->id)) {
314 fadeOutAndReleaseSpotLocked(spot);
315 }
316 }
317
318 mSpriteController->closeTransaction();
319}
320
321void PointerController::clearSpots() {
322#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000323 ALOGD("clearSpots");
Jeff Brown2352b972011-04-12 22:39:53 -0700324#endif
325
326 AutoMutex _l(mLock);
327
328 fadeOutAndReleaseAllSpotsLocked();
329}
330
331void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
332 AutoMutex _l(mLock);
333
334 if (mLocked.inactivityTimeout != inactivityTimeout) {
335 mLocked.inactivityTimeout = inactivityTimeout;
336 resetInactivityTimeoutLocked();
337 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800338}
339
Jun Mukai19a56012015-11-24 11:25:52 -0800340void PointerController::reloadPointerResources() {
341 AutoMutex _l(mLock);
342
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700343 loadResourcesLocked();
Jun Mukai19a56012015-11-24 11:25:52 -0800344 updatePointerLocked();
345}
346
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700347/**
348 * The viewport values for deviceHeight and deviceWidth have already been adjusted for rotation,
349 * so here we are getting the dimensions in the original, unrotated orientation (orientation 0).
350 */
351static void getNonRotatedSize(const DisplayViewport& viewport, int32_t& width, int32_t& height) {
352 if (viewport.orientation == DISPLAY_ORIENTATION_90
353 || viewport.orientation == DISPLAY_ORIENTATION_270) {
354 width = viewport.deviceHeight;
355 height = viewport.deviceWidth;
356 } else {
357 width = viewport.deviceWidth;
358 height = viewport.deviceHeight;
359 }
360}
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800361
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700362void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
363 AutoMutex _l(mLock);
364 if (viewport == mLocked.viewport) {
365 return;
Jeff Brownd728bf52012-09-08 18:05:28 -0700366 }
367
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700368 const DisplayViewport oldViewport = mLocked.viewport;
369 mLocked.viewport = viewport;
370
371 int32_t oldDisplayWidth, oldDisplayHeight;
372 getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight);
373 int32_t newDisplayWidth, newDisplayHeight;
374 getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight);
375
376 // Reset cursor position to center if size or display changed.
377 if (oldViewport.displayId != viewport.displayId
378 || oldDisplayWidth != newDisplayWidth
379 || oldDisplayHeight != newDisplayHeight) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800380
381 float minX, minY, maxX, maxY;
382 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
383 mLocked.pointerX = (minX + maxX) * 0.5f;
384 mLocked.pointerY = (minY + maxY) * 0.5f;
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700385 // Reload icon resources for density may be changed.
386 loadResourcesLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800387 } else {
388 mLocked.pointerX = 0;
389 mLocked.pointerY = 0;
390 }
391
Jeff Brown2352b972011-04-12 22:39:53 -0700392 fadeOutAndReleaseAllSpotsLocked();
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700393 } else if (oldViewport.orientation != viewport.orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800394 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
395 // This creates an invariant frame of reference that we can easily rotate when
396 // taking into account that the pointer may be located at fractional pixel offsets.
397 float x = mLocked.pointerX + 0.5f;
398 float y = mLocked.pointerY + 0.5f;
399 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800400
Jeff Brownd41cff22011-03-03 02:09:54 -0800401 // Undo the previous rotation.
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700402 switch (oldViewport.orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800403 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800404 temp = x;
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700405 x = oldViewport.deviceHeight - y;
Jeff Brownd41cff22011-03-03 02:09:54 -0800406 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800407 break;
408 case DISPLAY_ORIENTATION_180:
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700409 x = oldViewport.deviceWidth - x;
410 y = oldViewport.deviceHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800411 break;
412 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800413 temp = x;
414 x = y;
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700415 y = oldViewport.deviceWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800416 break;
417 }
418
Jeff Brownd41cff22011-03-03 02:09:54 -0800419 // Perform the new rotation.
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700420 switch (viewport.orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800421 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800422 temp = x;
423 x = y;
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700424 y = viewport.deviceHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800425 break;
426 case DISPLAY_ORIENTATION_180:
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700427 x = viewport.deviceWidth - x;
428 y = viewport.deviceHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800429 break;
430 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800431 temp = x;
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700432 x = viewport.deviceWidth - y;
Jeff Brownd41cff22011-03-03 02:09:54 -0800433 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800434 break;
435 }
436
Jeff Brownd41cff22011-03-03 02:09:54 -0800437 // Apply offsets to convert from the pixel center to the pixel top-left corner position
438 // and save the results.
439 mLocked.pointerX = x - 0.5f;
440 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800441 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700442
443 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800444}
445
Michael Wrighte051f6f2016-05-13 17:44:16 +0100446void PointerController::updatePointerIcon(int32_t iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -0700447 AutoMutex _l(mLock);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100448 if (mLocked.requestedPointerType != iconId) {
449 mLocked.requestedPointerType = iconId;
Jun Mukai1db53972015-09-11 18:08:31 -0700450 mLocked.presentationChanged = true;
451 updatePointerLocked();
452 }
453}
454
Jun Mukaid4eaef72015-10-30 15:54:33 -0700455void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
456 AutoMutex _l(mLock);
457
458 const int32_t iconId = mPolicy->getCustomPointerIconId();
459 mLocked.additionalMouseResources[iconId] = icon;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100460 mLocked.requestedPointerType = iconId;
Jun Mukaid4eaef72015-10-30 15:54:33 -0700461 mLocked.presentationChanged = true;
462
463 updatePointerLocked();
464}
465
Jeff Brown05dc66a2011-03-02 14:41:58 -0800466void PointerController::handleMessage(const Message& message) {
467 switch (message.what) {
Jeff Brown2352b972011-04-12 22:39:53 -0700468 case MSG_INACTIVITY_TIMEOUT:
469 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800470 break;
471 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800472}
473
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700474int PointerController::handleEvent(int /* fd */, int events, void* /* data */) {
475 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
476 ALOGE("Display event receiver pipe was closed or an error occurred. "
477 "events=0x%x", events);
478 return 0; // remove the callback
479 }
480
481 if (!(events & Looper::EVENT_INPUT)) {
482 ALOGW("Received spurious callback for unhandled poll event. "
483 "events=0x%x", events);
484 return 1; // keep the callback
485 }
486
487 bool gotVsync = false;
488 ssize_t n;
489 nsecs_t timestamp;
490 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
491 while ((n = mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
492 for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
493 if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
494 timestamp = buf[i].header.timestamp;
495 gotVsync = true;
496 }
497 }
498 }
499 if (gotVsync) {
500 doAnimate(timestamp);
501 }
502 return 1; // keep the callback
503}
504
505void PointerController::doAnimate(nsecs_t timestamp) {
Jeff Brown2352b972011-04-12 22:39:53 -0700506 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800507
Jeff Brown2352b972011-04-12 22:39:53 -0700508 mLocked.animationPending = false;
Jun Mukai808196f2015-10-28 16:46:44 -0700509
510 bool keepFading = doFadingAnimationLocked(timestamp);
511 bool keepBitmapFlipping = doBitmapAnimationLocked(timestamp);
512 if (keepFading || keepBitmapFlipping) {
513 startAnimationLocked();
514 }
515}
516
517bool PointerController::doFadingAnimationLocked(nsecs_t timestamp) {
518 bool keepAnimating = false;
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700519 nsecs_t frameDelay = timestamp - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800520
Jeff Brown2352b972011-04-12 22:39:53 -0700521 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700522 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown2352b972011-04-12 22:39:53 -0700523 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700524 if (mLocked.pointerAlpha <= 0.0f) {
525 mLocked.pointerAlpha = 0.0f;
526 mLocked.pointerFadeDirection = 0;
527 } else {
528 keepAnimating = true;
529 }
530 updatePointerLocked();
531 } else if (mLocked.pointerFadeDirection > 0) {
532 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
533 if (mLocked.pointerAlpha >= 1.0f) {
534 mLocked.pointerAlpha = 1.0f;
535 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800536 } else {
Jeff Brown2352b972011-04-12 22:39:53 -0700537 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800538 }
Jeff Brown2352b972011-04-12 22:39:53 -0700539 updatePointerLocked();
540 }
541
542 // Animate spots that are fading out and being removed.
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800543 for (size_t i = 0; i < mLocked.spots.size();) {
Jeff Brown2352b972011-04-12 22:39:53 -0700544 Spot* spot = mLocked.spots.itemAt(i);
545 if (spot->id == Spot::INVALID_ID) {
546 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
547 if (spot->alpha <= 0) {
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800548 mLocked.spots.removeAt(i);
Jeff Brown2352b972011-04-12 22:39:53 -0700549 releaseSpotLocked(spot);
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800550 continue;
Jeff Brown2352b972011-04-12 22:39:53 -0700551 } else {
552 spot->sprite->setAlpha(spot->alpha);
553 keepAnimating = true;
554 }
555 }
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800556 ++i;
Jeff Brown2352b972011-04-12 22:39:53 -0700557 }
Jun Mukai808196f2015-10-28 16:46:44 -0700558 return keepAnimating;
559}
Jeff Brown2352b972011-04-12 22:39:53 -0700560
Jun Mukai808196f2015-10-28 16:46:44 -0700561bool PointerController::doBitmapAnimationLocked(nsecs_t timestamp) {
562 std::map<int32_t, PointerAnimation>::const_iterator iter = mLocked.animationResources.find(
Michael Wrighte051f6f2016-05-13 17:44:16 +0100563 mLocked.requestedPointerType);
Jun Mukai808196f2015-10-28 16:46:44 -0700564 if (iter == mLocked.animationResources.end()) {
565 return false;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800566 }
Jun Mukai808196f2015-10-28 16:46:44 -0700567
568 if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) {
569 mSpriteController->openTransaction();
570
571 int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame;
572 mLocked.animationFrameIndex += incr;
573 mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr;
574 while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) {
575 mLocked.animationFrameIndex -= iter->second.animationFrames.size();
576 }
577 mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]);
578
579 mSpriteController->closeTransaction();
580 }
581
582 // Keep animating.
583 return true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800584}
585
Jeff Brown2352b972011-04-12 22:39:53 -0700586void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700587 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800588}
589
Jeff Brown2352b972011-04-12 22:39:53 -0700590void PointerController::startAnimationLocked() {
591 if (!mLocked.animationPending) {
592 mLocked.animationPending = true;
593 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700594 mDisplayEventReceiver.requestNextVsync();
Jeff Brown2352b972011-04-12 22:39:53 -0700595 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800596}
597
Jeff Brown2352b972011-04-12 22:39:53 -0700598void PointerController::resetInactivityTimeoutLocked() {
599 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
600
601 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
602 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
603 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
604}
605
Jeff Brown538881e2011-05-25 18:23:38 -0700606void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700607 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown2352b972011-04-12 22:39:53 -0700608}
609
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700610void PointerController::updatePointerLocked() REQUIRES(mLock) {
611 if (!mLocked.viewport.isValid()) {
612 return;
613 }
614
Jeff Brown2352b972011-04-12 22:39:53 -0700615 mSpriteController->openTransaction();
616
617 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
618 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700619 mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId);
Jeff Brown2352b972011-04-12 22:39:53 -0700620
621 if (mLocked.pointerAlpha > 0) {
622 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
623 mLocked.pointerSprite->setVisible(true);
624 } else {
625 mLocked.pointerSprite->setVisible(false);
626 }
627
628 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
Jun Mukai1db53972015-09-11 18:08:31 -0700629 if (mLocked.presentation == PRESENTATION_POINTER) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100630 if (mLocked.requestedPointerType == mPolicy->getDefaultPointerIconId()) {
Jun Mukai1db53972015-09-11 18:08:31 -0700631 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
632 } else {
Jun Mukai808196f2015-10-28 16:46:44 -0700633 std::map<int32_t, SpriteIcon>::const_iterator iter =
Michael Wrighte051f6f2016-05-13 17:44:16 +0100634 mLocked.additionalMouseResources.find(mLocked.requestedPointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700635 if (iter != mLocked.additionalMouseResources.end()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700636 std::map<int32_t, PointerAnimation>::const_iterator anim_iter =
Michael Wrighte051f6f2016-05-13 17:44:16 +0100637 mLocked.animationResources.find(mLocked.requestedPointerType);
Jun Mukai808196f2015-10-28 16:46:44 -0700638 if (anim_iter != mLocked.animationResources.end()) {
639 mLocked.animationFrameIndex = 0;
640 mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC);
641 startAnimationLocked();
642 }
Jun Mukai1db53972015-09-11 18:08:31 -0700643 mLocked.pointerSprite->setIcon(iter->second);
644 } else {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100645 ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700646 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
647 }
648 }
649 } else {
650 mLocked.pointerSprite->setIcon(mResources.spotAnchor);
651 }
Jeff Brown2352b972011-04-12 22:39:53 -0700652 mLocked.pointerIconChanged = false;
653 mLocked.presentationChanged = false;
654 }
655
656 mSpriteController->closeTransaction();
657}
658
659PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
660 for (size_t i = 0; i < mLocked.spots.size(); i++) {
661 Spot* spot = mLocked.spots.itemAt(i);
662 if (spot->id == id) {
663 return spot;
664 }
665 }
666 return NULL;
667}
668
669PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
670 // Remove spots until we have fewer than MAX_SPOTS remaining.
671 while (mLocked.spots.size() >= MAX_SPOTS) {
672 Spot* spot = removeFirstFadingSpotLocked();
673 if (!spot) {
674 spot = mLocked.spots.itemAt(0);
675 mLocked.spots.removeAt(0);
676 }
677 releaseSpotLocked(spot);
678 }
679
680 // Obtain a sprite from the recycled pool.
681 sp<Sprite> sprite;
682 if (! mLocked.recycledSprites.isEmpty()) {
683 sprite = mLocked.recycledSprites.top();
684 mLocked.recycledSprites.pop();
685 } else {
686 sprite = mSpriteController->createSprite();
687 }
688
689 // Return the new spot.
690 Spot* spot = new Spot(id, sprite);
691 mLocked.spots.push(spot);
692 return spot;
693}
694
695PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
696 for (size_t i = 0; i < mLocked.spots.size(); i++) {
697 Spot* spot = mLocked.spots.itemAt(i);
698 if (spot->id == Spot::INVALID_ID) {
699 mLocked.spots.removeAt(i);
700 return spot;
701 }
702 }
703 return NULL;
704}
705
706void PointerController::releaseSpotLocked(Spot* spot) {
707 spot->sprite->clearIcon();
708
709 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
710 mLocked.recycledSprites.push(spot->sprite);
711 }
712
713 delete spot;
714}
715
716void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
717 if (spot->id != Spot::INVALID_ID) {
718 spot->id = Spot::INVALID_ID;
719 startAnimationLocked();
720 }
721}
722
723void PointerController::fadeOutAndReleaseAllSpotsLocked() {
724 for (size_t i = 0; i < mLocked.spots.size(); i++) {
725 Spot* spot = mLocked.spots.itemAt(i);
726 fadeOutAndReleaseSpotLocked(spot);
727 }
728}
729
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700730void PointerController::loadResourcesLocked() REQUIRES(mLock) {
Jeff Brown2352b972011-04-12 22:39:53 -0700731 mPolicy->loadPointerResources(&mResources);
Andrii Kulian84cdf9c2018-09-14 16:48:08 -0700732
733 if (mLocked.presentation == PRESENTATION_POINTER) {
734 mLocked.additionalMouseResources.clear();
735 mLocked.animationResources.clear();
736 mPolicy->loadPointerIcon(&mLocked.pointerIcon);
737 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
738 &mLocked.animationResources);
739 }
740
741 mLocked.pointerIconChanged = true;
Jeff Brown2352b972011-04-12 22:39:53 -0700742}
743
744
745// --- PointerController::Spot ---
746
747void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
748 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
749 sprite->setAlpha(alpha);
750 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
751 sprite->setPosition(x, y);
752
753 this->x = x;
754 this->y = y;
755
756 if (icon != lastIcon) {
757 lastIcon = icon;
758 if (icon) {
759 sprite->setIcon(*icon);
760 sprite->setVisible(true);
761 } else {
762 sprite->setVisible(false);
763 }
764 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800765}
766
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800767} // namespace android