blob: 6c5f20c1d4162eafb6563056819b0fdf71a93225 [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:
Chih-Hung Hsieh0c29f392018-12-20 13:51:01 -080042 explicit WeakLooperCallback(const wp<LooperCallback>& callback) :
Vladislav Kaznacheev33c59032016-09-09 10:03:31 -070043 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 Brownb4ff35d2011-01-02 16:37:43 -080092 mLocked.displayWidth = -1;
93 mLocked.displayHeight = -1;
94 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
95
Jeff Brown2352b972011-04-12 22:39:53 -070096 mLocked.presentation = PRESENTATION_POINTER;
97 mLocked.presentationChanged = false;
98
99 mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
100
Jeff Brown538881e2011-05-25 18:23:38 -0700101 mLocked.pointerFadeDirection = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800102 mLocked.pointerX = 0;
103 mLocked.pointerY = 0;
Jeff Brown538881e2011-05-25 18:23:38 -0700104 mLocked.pointerAlpha = 0.0f; // pointer is initially faded
Jeff Brown2352b972011-04-12 22:39:53 -0700105 mLocked.pointerSprite = mSpriteController->createSprite();
106 mLocked.pointerIconChanged = false;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100107 mLocked.requestedPointerType = mPolicy->getDefaultPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700108
Jun Mukai808196f2015-10-28 16:46:44 -0700109 mLocked.animationFrameIndex = 0;
110 mLocked.lastFrameUpdatedTime = 0;
111
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800112 mLocked.buttonState = 0;
113
Jun Mukai19a56012015-11-24 11:25:52 -0800114 mPolicy->loadPointerIcon(&mLocked.pointerIcon);
115
Jeff Brown2352b972011-04-12 22:39:53 -0700116 loadResources();
Jun Mukai19a56012015-11-24 11:25:52 -0800117
118 if (mLocked.pointerIcon.isValid()) {
119 mLocked.pointerIconChanged = true;
120 updatePointerLocked();
121 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800122}
123
124PointerController::~PointerController() {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800125 mLooper->removeMessages(mHandler);
126
Jeff Brown5541de92011-04-11 11:54:25 -0700127 AutoMutex _l(mLock);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800128
Jeff Brown2352b972011-04-12 22:39:53 -0700129 mLocked.pointerSprite.clear();
130
131 for (size_t i = 0; i < mLocked.spots.size(); i++) {
132 delete mLocked.spots.itemAt(i);
133 }
134 mLocked.spots.clear();
135 mLocked.recycledSprites.clear();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136}
137
138bool PointerController::getBounds(float* outMinX, float* outMinY,
139 float* outMaxX, float* outMaxY) const {
140 AutoMutex _l(mLock);
141
142 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
143}
144
145bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
146 float* outMaxX, float* outMaxY) const {
147 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
148 return false;
149 }
150
151 *outMinX = 0;
152 *outMinY = 0;
153 switch (mLocked.displayOrientation) {
154 case DISPLAY_ORIENTATION_90:
155 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800156 *outMaxX = mLocked.displayHeight - 1;
157 *outMaxY = mLocked.displayWidth - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800158 break;
159 default:
Jeff Brownd41cff22011-03-03 02:09:54 -0800160 *outMaxX = mLocked.displayWidth - 1;
161 *outMaxY = mLocked.displayHeight - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800162 break;
163 }
164 return true;
165}
166
167void PointerController::move(float deltaX, float deltaY) {
168#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000169 ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800170#endif
171 if (deltaX == 0.0f && deltaY == 0.0f) {
172 return;
173 }
174
175 AutoMutex _l(mLock);
176
177 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
178}
179
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700180void PointerController::setButtonState(int32_t buttonState) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800181#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000182 ALOGD("Set button state 0x%08x", buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800183#endif
184 AutoMutex _l(mLock);
185
186 if (mLocked.buttonState != buttonState) {
187 mLocked.buttonState = buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800188 }
189}
190
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700191int32_t PointerController::getButtonState() const {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800192 AutoMutex _l(mLock);
193
194 return mLocked.buttonState;
195}
196
197void PointerController::setPosition(float x, float y) {
198#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000199 ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800200#endif
201 AutoMutex _l(mLock);
202
203 setPositionLocked(x, y);
204}
205
206void PointerController::setPositionLocked(float x, float y) {
207 float minX, minY, maxX, maxY;
208 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
209 if (x <= minX) {
210 mLocked.pointerX = minX;
211 } else if (x >= maxX) {
212 mLocked.pointerX = maxX;
213 } else {
214 mLocked.pointerX = x;
215 }
216 if (y <= minY) {
217 mLocked.pointerY = minY;
218 } else if (y >= maxY) {
219 mLocked.pointerY = maxY;
220 } else {
221 mLocked.pointerY = y;
222 }
Jeff Brown2352b972011-04-12 22:39:53 -0700223 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800224 }
225}
226
227void PointerController::getPosition(float* outX, float* outY) const {
228 AutoMutex _l(mLock);
229
230 *outX = mLocked.pointerX;
231 *outY = mLocked.pointerY;
232}
233
Jeff Brown538881e2011-05-25 18:23:38 -0700234void PointerController::fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800235 AutoMutex _l(mLock);
236
Jeff Brown538881e2011-05-25 18:23:38 -0700237 // Remove the inactivity timeout, since we are fading now.
238 removeInactivityTimeoutLocked();
239
240 // Start fading.
241 if (transition == TRANSITION_IMMEDIATE) {
242 mLocked.pointerFadeDirection = 0;
243 mLocked.pointerAlpha = 0.0f;
244 updatePointerLocked();
245 } else {
246 mLocked.pointerFadeDirection = -1;
247 startAnimationLocked();
248 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800249}
250
Jeff Brown538881e2011-05-25 18:23:38 -0700251void PointerController::unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800252 AutoMutex _l(mLock);
253
Jeff Brown2352b972011-04-12 22:39:53 -0700254 // Always reset the inactivity timer.
255 resetInactivityTimeoutLocked();
256
Jeff Brown538881e2011-05-25 18:23:38 -0700257 // Start unfading.
258 if (transition == TRANSITION_IMMEDIATE) {
259 mLocked.pointerFadeDirection = 0;
Jeff Brown2352b972011-04-12 22:39:53 -0700260 mLocked.pointerAlpha = 1.0f;
261 updatePointerLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700262 } else {
263 mLocked.pointerFadeDirection = 1;
264 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800265 }
266}
267
Jeff Brown2352b972011-04-12 22:39:53 -0700268void PointerController::setPresentation(Presentation presentation) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800269 AutoMutex _l(mLock);
270
Jun Mukai1db53972015-09-11 18:08:31 -0700271 if (presentation == PRESENTATION_POINTER && mLocked.additionalMouseResources.empty()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700272 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
273 &mLocked.animationResources);
Jun Mukai1db53972015-09-11 18:08:31 -0700274 }
275
Jeff Brown2352b972011-04-12 22:39:53 -0700276 if (mLocked.presentation != presentation) {
277 mLocked.presentation = presentation;
278 mLocked.presentationChanged = true;
279
280 if (presentation != PRESENTATION_SPOT) {
281 fadeOutAndReleaseAllSpotsLocked();
282 }
283
284 updatePointerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800285 }
286}
287
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700288void PointerController::setSpots(const PointerCoords* spotCoords,
289 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
Jeff Brown2352b972011-04-12 22:39:53 -0700290#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000291 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
Jeff Brown2352b972011-04-12 22:39:53 -0700292 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
293 uint32_t id = idBits.firstMarkedBit();
294 idBits.clearBit(id);
295 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
Steve Block5baa3a62011-12-20 16:23:08 +0000296 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
Jeff Brown2352b972011-04-12 22:39:53 -0700297 c.getAxisValue(AMOTION_EVENT_AXIS_X),
298 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
299 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
300 }
301#endif
302
303 AutoMutex _l(mLock);
304
305 mSpriteController->openTransaction();
306
307 // Add or move spots for fingers that are down.
308 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700309 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -0700310 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
311 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
312 ? mResources.spotTouch : mResources.spotHover;
313 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
314 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
315
316 Spot* spot = getSpotLocked(id);
317 if (!spot) {
318 spot = createAndAddSpotLocked(id);
319 }
320
321 spot->updateSprite(&icon, x, y);
322 }
323
324 // Remove spots for fingers that went up.
325 for (size_t i = 0; i < mLocked.spots.size(); i++) {
326 Spot* spot = mLocked.spots.itemAt(i);
327 if (spot->id != Spot::INVALID_ID
328 && !spotIdBits.hasBit(spot->id)) {
329 fadeOutAndReleaseSpotLocked(spot);
330 }
331 }
332
333 mSpriteController->closeTransaction();
334}
335
336void PointerController::clearSpots() {
337#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000338 ALOGD("clearSpots");
Jeff Brown2352b972011-04-12 22:39:53 -0700339#endif
340
341 AutoMutex _l(mLock);
342
343 fadeOutAndReleaseAllSpotsLocked();
344}
345
346void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
347 AutoMutex _l(mLock);
348
349 if (mLocked.inactivityTimeout != inactivityTimeout) {
350 mLocked.inactivityTimeout = inactivityTimeout;
351 resetInactivityTimeoutLocked();
352 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800353}
354
Jun Mukai19a56012015-11-24 11:25:52 -0800355void PointerController::reloadPointerResources() {
356 AutoMutex _l(mLock);
357
358 loadResources();
359
360 if (mLocked.presentation == PRESENTATION_POINTER) {
361 mLocked.additionalMouseResources.clear();
362 mLocked.animationResources.clear();
363 mPolicy->loadPointerIcon(&mLocked.pointerIcon);
364 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
365 &mLocked.animationResources);
366 }
367
368 mLocked.presentationChanged = true;
369 updatePointerLocked();
370}
371
Jeff Brownd728bf52012-09-08 18:05:28 -0700372void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800373 AutoMutex _l(mLock);
374
Jeff Brownd728bf52012-09-08 18:05:28 -0700375 // Adjust to use the display's unrotated coordinate frame.
376 if (orientation == DISPLAY_ORIENTATION_90
377 || orientation == DISPLAY_ORIENTATION_270) {
378 int32_t temp = height;
379 height = width;
380 width = temp;
381 }
382
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800383 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
384 mLocked.displayWidth = width;
385 mLocked.displayHeight = height;
386
387 float minX, minY, maxX, maxY;
388 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
389 mLocked.pointerX = (minX + maxX) * 0.5f;
390 mLocked.pointerY = (minY + maxY) * 0.5f;
391 } else {
392 mLocked.pointerX = 0;
393 mLocked.pointerY = 0;
394 }
395
Jeff Brown2352b972011-04-12 22:39:53 -0700396 fadeOutAndReleaseAllSpotsLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800397 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800398
399 if (mLocked.displayOrientation != orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800400 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
401 // This creates an invariant frame of reference that we can easily rotate when
402 // taking into account that the pointer may be located at fractional pixel offsets.
403 float x = mLocked.pointerX + 0.5f;
404 float y = mLocked.pointerY + 0.5f;
405 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800406
Jeff Brownd41cff22011-03-03 02:09:54 -0800407 // Undo the previous rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800408 switch (mLocked.displayOrientation) {
409 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800410 temp = x;
411 x = mLocked.displayWidth - y;
412 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800413 break;
414 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800415 x = mLocked.displayWidth - x;
416 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800417 break;
418 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800419 temp = x;
420 x = y;
421 y = mLocked.displayHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800422 break;
423 }
424
Jeff Brownd41cff22011-03-03 02:09:54 -0800425 // Perform the new rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800426 switch (orientation) {
427 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800428 temp = x;
429 x = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700430 y = mLocked.displayWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800431 break;
432 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800433 x = mLocked.displayWidth - x;
434 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800435 break;
436 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800437 temp = x;
438 x = mLocked.displayHeight - y;
439 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800440 break;
441 }
442
Jeff Brownd41cff22011-03-03 02:09:54 -0800443 // Apply offsets to convert from the pixel center to the pixel top-left corner position
444 // and save the results.
445 mLocked.pointerX = x - 0.5f;
446 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800447 mLocked.displayOrientation = orientation;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800448 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700449
450 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800451}
452
Michael Wrighte051f6f2016-05-13 17:44:16 +0100453void PointerController::updatePointerIcon(int32_t iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -0700454 AutoMutex _l(mLock);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100455 if (mLocked.requestedPointerType != iconId) {
456 mLocked.requestedPointerType = iconId;
Jun Mukai1db53972015-09-11 18:08:31 -0700457 mLocked.presentationChanged = true;
458 updatePointerLocked();
459 }
460}
461
Jun Mukaid4eaef72015-10-30 15:54:33 -0700462void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
463 AutoMutex _l(mLock);
464
465 const int32_t iconId = mPolicy->getCustomPointerIconId();
466 mLocked.additionalMouseResources[iconId] = icon;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100467 mLocked.requestedPointerType = iconId;
Jun Mukaid4eaef72015-10-30 15:54:33 -0700468 mLocked.presentationChanged = true;
469
470 updatePointerLocked();
471}
472
Jeff Brown05dc66a2011-03-02 14:41:58 -0800473void PointerController::handleMessage(const Message& message) {
474 switch (message.what) {
Jeff Brown2352b972011-04-12 22:39:53 -0700475 case MSG_INACTIVITY_TIMEOUT:
476 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800477 break;
478 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800479}
480
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700481int PointerController::handleEvent(int /* fd */, int events, void* /* data */) {
482 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
483 ALOGE("Display event receiver pipe was closed or an error occurred. "
484 "events=0x%x", events);
485 return 0; // remove the callback
486 }
487
488 if (!(events & Looper::EVENT_INPUT)) {
489 ALOGW("Received spurious callback for unhandled poll event. "
490 "events=0x%x", events);
491 return 1; // keep the callback
492 }
493
494 bool gotVsync = false;
495 ssize_t n;
496 nsecs_t timestamp;
497 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
498 while ((n = mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
499 for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
500 if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
501 timestamp = buf[i].header.timestamp;
502 gotVsync = true;
503 }
504 }
505 }
506 if (gotVsync) {
507 doAnimate(timestamp);
508 }
509 return 1; // keep the callback
510}
511
512void PointerController::doAnimate(nsecs_t timestamp) {
Jeff Brown2352b972011-04-12 22:39:53 -0700513 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800514
Jeff Brown2352b972011-04-12 22:39:53 -0700515 mLocked.animationPending = false;
Jun Mukai808196f2015-10-28 16:46:44 -0700516
517 bool keepFading = doFadingAnimationLocked(timestamp);
518 bool keepBitmapFlipping = doBitmapAnimationLocked(timestamp);
519 if (keepFading || keepBitmapFlipping) {
520 startAnimationLocked();
521 }
522}
523
524bool PointerController::doFadingAnimationLocked(nsecs_t timestamp) {
525 bool keepAnimating = false;
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700526 nsecs_t frameDelay = timestamp - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800527
Jeff Brown2352b972011-04-12 22:39:53 -0700528 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700529 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown2352b972011-04-12 22:39:53 -0700530 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700531 if (mLocked.pointerAlpha <= 0.0f) {
532 mLocked.pointerAlpha = 0.0f;
533 mLocked.pointerFadeDirection = 0;
534 } else {
535 keepAnimating = true;
536 }
537 updatePointerLocked();
538 } else if (mLocked.pointerFadeDirection > 0) {
539 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
540 if (mLocked.pointerAlpha >= 1.0f) {
541 mLocked.pointerAlpha = 1.0f;
542 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800543 } else {
Jeff Brown2352b972011-04-12 22:39:53 -0700544 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800545 }
Jeff Brown2352b972011-04-12 22:39:53 -0700546 updatePointerLocked();
547 }
548
549 // Animate spots that are fading out and being removed.
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800550 for (size_t i = 0; i < mLocked.spots.size();) {
Jeff Brown2352b972011-04-12 22:39:53 -0700551 Spot* spot = mLocked.spots.itemAt(i);
552 if (spot->id == Spot::INVALID_ID) {
553 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
554 if (spot->alpha <= 0) {
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800555 mLocked.spots.removeAt(i);
Jeff Brown2352b972011-04-12 22:39:53 -0700556 releaseSpotLocked(spot);
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800557 continue;
Jeff Brown2352b972011-04-12 22:39:53 -0700558 } else {
559 spot->sprite->setAlpha(spot->alpha);
560 keepAnimating = true;
561 }
562 }
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800563 ++i;
Jeff Brown2352b972011-04-12 22:39:53 -0700564 }
Jun Mukai808196f2015-10-28 16:46:44 -0700565 return keepAnimating;
566}
Jeff Brown2352b972011-04-12 22:39:53 -0700567
Jun Mukai808196f2015-10-28 16:46:44 -0700568bool PointerController::doBitmapAnimationLocked(nsecs_t timestamp) {
569 std::map<int32_t, PointerAnimation>::const_iterator iter = mLocked.animationResources.find(
Michael Wrighte051f6f2016-05-13 17:44:16 +0100570 mLocked.requestedPointerType);
Jun Mukai808196f2015-10-28 16:46:44 -0700571 if (iter == mLocked.animationResources.end()) {
572 return false;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800573 }
Jun Mukai808196f2015-10-28 16:46:44 -0700574
575 if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) {
576 mSpriteController->openTransaction();
577
578 int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame;
579 mLocked.animationFrameIndex += incr;
580 mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr;
581 while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) {
582 mLocked.animationFrameIndex -= iter->second.animationFrames.size();
583 }
584 mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]);
585
586 mSpriteController->closeTransaction();
587 }
588
589 // Keep animating.
590 return true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800591}
592
Jeff Brown2352b972011-04-12 22:39:53 -0700593void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700594 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800595}
596
Jeff Brown2352b972011-04-12 22:39:53 -0700597void PointerController::startAnimationLocked() {
598 if (!mLocked.animationPending) {
599 mLocked.animationPending = true;
600 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700601 mDisplayEventReceiver.requestNextVsync();
Jeff Brown2352b972011-04-12 22:39:53 -0700602 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800603}
604
Jeff Brown2352b972011-04-12 22:39:53 -0700605void PointerController::resetInactivityTimeoutLocked() {
606 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
607
608 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
609 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
610 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
611}
612
Jeff Brown538881e2011-05-25 18:23:38 -0700613void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700614 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown2352b972011-04-12 22:39:53 -0700615}
616
617void PointerController::updatePointerLocked() {
618 mSpriteController->openTransaction();
619
620 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
621 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
622
623 if (mLocked.pointerAlpha > 0) {
624 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
625 mLocked.pointerSprite->setVisible(true);
626 } else {
627 mLocked.pointerSprite->setVisible(false);
628 }
629
630 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
Jun Mukai1db53972015-09-11 18:08:31 -0700631 if (mLocked.presentation == PRESENTATION_POINTER) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100632 if (mLocked.requestedPointerType == mPolicy->getDefaultPointerIconId()) {
Jun Mukai1db53972015-09-11 18:08:31 -0700633 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
634 } else {
Jun Mukai808196f2015-10-28 16:46:44 -0700635 std::map<int32_t, SpriteIcon>::const_iterator iter =
Michael Wrighte051f6f2016-05-13 17:44:16 +0100636 mLocked.additionalMouseResources.find(mLocked.requestedPointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700637 if (iter != mLocked.additionalMouseResources.end()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700638 std::map<int32_t, PointerAnimation>::const_iterator anim_iter =
Michael Wrighte051f6f2016-05-13 17:44:16 +0100639 mLocked.animationResources.find(mLocked.requestedPointerType);
Jun Mukai808196f2015-10-28 16:46:44 -0700640 if (anim_iter != mLocked.animationResources.end()) {
641 mLocked.animationFrameIndex = 0;
642 mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC);
643 startAnimationLocked();
644 }
Jun Mukai1db53972015-09-11 18:08:31 -0700645 mLocked.pointerSprite->setIcon(iter->second);
646 } else {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100647 ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700648 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
649 }
650 }
651 } else {
652 mLocked.pointerSprite->setIcon(mResources.spotAnchor);
653 }
Jeff Brown2352b972011-04-12 22:39:53 -0700654 mLocked.pointerIconChanged = false;
655 mLocked.presentationChanged = false;
656 }
657
658 mSpriteController->closeTransaction();
659}
660
661PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
662 for (size_t i = 0; i < mLocked.spots.size(); i++) {
663 Spot* spot = mLocked.spots.itemAt(i);
664 if (spot->id == id) {
665 return spot;
666 }
667 }
668 return NULL;
669}
670
671PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
672 // Remove spots until we have fewer than MAX_SPOTS remaining.
673 while (mLocked.spots.size() >= MAX_SPOTS) {
674 Spot* spot = removeFirstFadingSpotLocked();
675 if (!spot) {
676 spot = mLocked.spots.itemAt(0);
677 mLocked.spots.removeAt(0);
678 }
679 releaseSpotLocked(spot);
680 }
681
682 // Obtain a sprite from the recycled pool.
683 sp<Sprite> sprite;
684 if (! mLocked.recycledSprites.isEmpty()) {
685 sprite = mLocked.recycledSprites.top();
686 mLocked.recycledSprites.pop();
687 } else {
688 sprite = mSpriteController->createSprite();
689 }
690
691 // Return the new spot.
692 Spot* spot = new Spot(id, sprite);
693 mLocked.spots.push(spot);
694 return spot;
695}
696
697PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
698 for (size_t i = 0; i < mLocked.spots.size(); i++) {
699 Spot* spot = mLocked.spots.itemAt(i);
700 if (spot->id == Spot::INVALID_ID) {
701 mLocked.spots.removeAt(i);
702 return spot;
703 }
704 }
705 return NULL;
706}
707
708void PointerController::releaseSpotLocked(Spot* spot) {
709 spot->sprite->clearIcon();
710
711 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
712 mLocked.recycledSprites.push(spot->sprite);
713 }
714
715 delete spot;
716}
717
718void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
719 if (spot->id != Spot::INVALID_ID) {
720 spot->id = Spot::INVALID_ID;
721 startAnimationLocked();
722 }
723}
724
725void PointerController::fadeOutAndReleaseAllSpotsLocked() {
726 for (size_t i = 0; i < mLocked.spots.size(); i++) {
727 Spot* spot = mLocked.spots.itemAt(i);
728 fadeOutAndReleaseSpotLocked(spot);
729 }
730}
731
732void PointerController::loadResources() {
733 mPolicy->loadPointerResources(&mResources);
734}
735
736
737// --- PointerController::Spot ---
738
739void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
740 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
741 sprite->setAlpha(alpha);
742 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
743 sprite->setPosition(x, y);
744
745 this->x = x;
746 this->y = y;
747
748 if (icon != lastIcon) {
749 lastIcon = icon;
750 if (icon) {
751 sprite->setIcon(*icon);
752 sprite->setVisible(true);
753 } else {
754 sprite->setVisible(false);
755 }
756 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800757}
758
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800759} // namespace android