blob: b9b1cdc6cbc91feb0d578b5db7c9ef4211e56072 [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 {
Arthur Hungb9b32002018-12-18 17:39:43 +0800134
135 if (!mLocked.viewport.isValid()) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136 return false;
137 }
138
Arthur Hungb9b32002018-12-18 17:39:43 +0800139 *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
Arthur Hungb9b32002018-12-18 17:39:43 +0800213int32_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
Arthur Hungb9b32002018-12-18 17:39:43 +0800343 loadResourcesLocked();
Jun Mukai19a56012015-11-24 11:25:52 -0800344 updatePointerLocked();
345}
346
Arthur Hungb9b32002018-12-18 17:39:43 +0800347/**
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 width = viewport.deviceWidth;
353 height = viewport.deviceHeight;
Andrii Kuliand44026f2018-12-17 18:59:36 +0000354
Arthur Hungb9b32002018-12-18 17:39:43 +0800355 if (viewport.orientation == DISPLAY_ORIENTATION_90
356 || viewport.orientation == DISPLAY_ORIENTATION_270) {
357 std::swap(width, height);
358 }
359}
360
361void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
362 AutoMutex _l(mLock);
363 if (viewport == mLocked.viewport) {
364 return;
Jeff Brownd728bf52012-09-08 18:05:28 -0700365 }
366
Arthur Hungb9b32002018-12-18 17:39:43 +0800367 const DisplayViewport oldViewport = mLocked.viewport;
368 mLocked.viewport = viewport;
369
370 int32_t oldDisplayWidth, oldDisplayHeight;
371 getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight);
372 int32_t newDisplayWidth, newDisplayHeight;
373 getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight);
374
375 // Reset cursor position to center if size or display changed.
376 if (oldViewport.displayId != viewport.displayId
377 || oldDisplayWidth != newDisplayWidth
378 || oldDisplayHeight != newDisplayHeight) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800379
380 float minX, minY, maxX, maxY;
381 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
382 mLocked.pointerX = (minX + maxX) * 0.5f;
383 mLocked.pointerY = (minY + maxY) * 0.5f;
Arthur Hungb9b32002018-12-18 17:39:43 +0800384 // Reload icon resources for density may be changed.
385 loadResourcesLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800386 } else {
387 mLocked.pointerX = 0;
388 mLocked.pointerY = 0;
389 }
390
Jeff Brown2352b972011-04-12 22:39:53 -0700391 fadeOutAndReleaseAllSpotsLocked();
Arthur Hungb9b32002018-12-18 17:39:43 +0800392 } else if (oldViewport.orientation != viewport.orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800393 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
394 // This creates an invariant frame of reference that we can easily rotate when
395 // taking into account that the pointer may be located at fractional pixel offsets.
396 float x = mLocked.pointerX + 0.5f;
397 float y = mLocked.pointerY + 0.5f;
398 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800399
Jeff Brownd41cff22011-03-03 02:09:54 -0800400 // Undo the previous rotation.
Arthur Hungb9b32002018-12-18 17:39:43 +0800401 switch (oldViewport.orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800402 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800403 temp = x;
Arthur Hungb9b32002018-12-18 17:39:43 +0800404 x = oldViewport.deviceHeight - y;
Jeff Brownd41cff22011-03-03 02:09:54 -0800405 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800406 break;
407 case DISPLAY_ORIENTATION_180:
Arthur Hungb9b32002018-12-18 17:39:43 +0800408 x = oldViewport.deviceWidth - x;
409 y = oldViewport.deviceHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800410 break;
411 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800412 temp = x;
413 x = y;
Arthur Hungb9b32002018-12-18 17:39:43 +0800414 y = oldViewport.deviceWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800415 break;
416 }
417
Jeff Brownd41cff22011-03-03 02:09:54 -0800418 // Perform the new rotation.
Arthur Hungb9b32002018-12-18 17:39:43 +0800419 switch (viewport.orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800420 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800421 temp = x;
422 x = y;
Arthur Hungb9b32002018-12-18 17:39:43 +0800423 y = viewport.deviceHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800424 break;
425 case DISPLAY_ORIENTATION_180:
Arthur Hungb9b32002018-12-18 17:39:43 +0800426 x = viewport.deviceWidth - x;
427 y = viewport.deviceHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800428 break;
429 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800430 temp = x;
Arthur Hungb9b32002018-12-18 17:39:43 +0800431 x = viewport.deviceWidth - y;
Jeff Brownd41cff22011-03-03 02:09:54 -0800432 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800433 break;
434 }
435
Jeff Brownd41cff22011-03-03 02:09:54 -0800436 // Apply offsets to convert from the pixel center to the pixel top-left corner position
437 // and save the results.
438 mLocked.pointerX = x - 0.5f;
439 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800440 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700441
442 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800443}
444
Michael Wrighte051f6f2016-05-13 17:44:16 +0100445void PointerController::updatePointerIcon(int32_t iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -0700446 AutoMutex _l(mLock);
Michael Wrighte051f6f2016-05-13 17:44:16 +0100447 if (mLocked.requestedPointerType != iconId) {
448 mLocked.requestedPointerType = iconId;
Jun Mukai1db53972015-09-11 18:08:31 -0700449 mLocked.presentationChanged = true;
450 updatePointerLocked();
451 }
452}
453
Jun Mukaid4eaef72015-10-30 15:54:33 -0700454void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
455 AutoMutex _l(mLock);
456
457 const int32_t iconId = mPolicy->getCustomPointerIconId();
458 mLocked.additionalMouseResources[iconId] = icon;
Michael Wrighte051f6f2016-05-13 17:44:16 +0100459 mLocked.requestedPointerType = iconId;
Jun Mukaid4eaef72015-10-30 15:54:33 -0700460 mLocked.presentationChanged = true;
461
462 updatePointerLocked();
463}
464
Jeff Brown05dc66a2011-03-02 14:41:58 -0800465void PointerController::handleMessage(const Message& message) {
466 switch (message.what) {
Jeff Brown2352b972011-04-12 22:39:53 -0700467 case MSG_INACTIVITY_TIMEOUT:
468 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800469 break;
470 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800471}
472
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700473int PointerController::handleEvent(int /* fd */, int events, void* /* data */) {
474 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
475 ALOGE("Display event receiver pipe was closed or an error occurred. "
476 "events=0x%x", events);
477 return 0; // remove the callback
478 }
479
480 if (!(events & Looper::EVENT_INPUT)) {
481 ALOGW("Received spurious callback for unhandled poll event. "
482 "events=0x%x", events);
483 return 1; // keep the callback
484 }
485
486 bool gotVsync = false;
487 ssize_t n;
488 nsecs_t timestamp;
489 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
490 while ((n = mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
491 for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
492 if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
493 timestamp = buf[i].header.timestamp;
494 gotVsync = true;
495 }
496 }
497 }
498 if (gotVsync) {
499 doAnimate(timestamp);
500 }
501 return 1; // keep the callback
502}
503
504void PointerController::doAnimate(nsecs_t timestamp) {
Jeff Brown2352b972011-04-12 22:39:53 -0700505 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800506
Jeff Brown2352b972011-04-12 22:39:53 -0700507 mLocked.animationPending = false;
Jun Mukai808196f2015-10-28 16:46:44 -0700508
509 bool keepFading = doFadingAnimationLocked(timestamp);
510 bool keepBitmapFlipping = doBitmapAnimationLocked(timestamp);
511 if (keepFading || keepBitmapFlipping) {
512 startAnimationLocked();
513 }
514}
515
516bool PointerController::doFadingAnimationLocked(nsecs_t timestamp) {
517 bool keepAnimating = false;
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700518 nsecs_t frameDelay = timestamp - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800519
Jeff Brown2352b972011-04-12 22:39:53 -0700520 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700521 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown2352b972011-04-12 22:39:53 -0700522 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700523 if (mLocked.pointerAlpha <= 0.0f) {
524 mLocked.pointerAlpha = 0.0f;
525 mLocked.pointerFadeDirection = 0;
526 } else {
527 keepAnimating = true;
528 }
529 updatePointerLocked();
530 } else if (mLocked.pointerFadeDirection > 0) {
531 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
532 if (mLocked.pointerAlpha >= 1.0f) {
533 mLocked.pointerAlpha = 1.0f;
534 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800535 } else {
Jeff Brown2352b972011-04-12 22:39:53 -0700536 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800537 }
Jeff Brown2352b972011-04-12 22:39:53 -0700538 updatePointerLocked();
539 }
540
541 // Animate spots that are fading out and being removed.
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800542 for (size_t i = 0; i < mLocked.spots.size();) {
Jeff Brown2352b972011-04-12 22:39:53 -0700543 Spot* spot = mLocked.spots.itemAt(i);
544 if (spot->id == Spot::INVALID_ID) {
545 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
546 if (spot->alpha <= 0) {
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800547 mLocked.spots.removeAt(i);
Jeff Brown2352b972011-04-12 22:39:53 -0700548 releaseSpotLocked(spot);
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800549 continue;
Jeff Brown2352b972011-04-12 22:39:53 -0700550 } else {
551 spot->sprite->setAlpha(spot->alpha);
552 keepAnimating = true;
553 }
554 }
Ivan Lozano7ee0dba2017-12-14 12:25:36 -0800555 ++i;
Jeff Brown2352b972011-04-12 22:39:53 -0700556 }
Jun Mukai808196f2015-10-28 16:46:44 -0700557 return keepAnimating;
558}
Jeff Brown2352b972011-04-12 22:39:53 -0700559
Jun Mukai808196f2015-10-28 16:46:44 -0700560bool PointerController::doBitmapAnimationLocked(nsecs_t timestamp) {
561 std::map<int32_t, PointerAnimation>::const_iterator iter = mLocked.animationResources.find(
Michael Wrighte051f6f2016-05-13 17:44:16 +0100562 mLocked.requestedPointerType);
Jun Mukai808196f2015-10-28 16:46:44 -0700563 if (iter == mLocked.animationResources.end()) {
564 return false;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800565 }
Jun Mukai808196f2015-10-28 16:46:44 -0700566
567 if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) {
568 mSpriteController->openTransaction();
569
570 int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame;
571 mLocked.animationFrameIndex += incr;
572 mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr;
573 while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) {
574 mLocked.animationFrameIndex -= iter->second.animationFrames.size();
575 }
576 mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]);
577
578 mSpriteController->closeTransaction();
579 }
580
581 // Keep animating.
582 return true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800583}
584
Jeff Brown2352b972011-04-12 22:39:53 -0700585void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700586 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800587}
588
Jeff Brown2352b972011-04-12 22:39:53 -0700589void PointerController::startAnimationLocked() {
590 if (!mLocked.animationPending) {
591 mLocked.animationPending = true;
592 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700593 mDisplayEventReceiver.requestNextVsync();
Jeff Brown2352b972011-04-12 22:39:53 -0700594 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800595}
596
Jeff Brown2352b972011-04-12 22:39:53 -0700597void PointerController::resetInactivityTimeoutLocked() {
598 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
599
600 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
601 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
602 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
603}
604
Jeff Brown538881e2011-05-25 18:23:38 -0700605void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700606 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown2352b972011-04-12 22:39:53 -0700607}
608
Arthur Hungb9b32002018-12-18 17:39:43 +0800609void PointerController::updatePointerLocked() REQUIRES(mLock) {
610 if (!mLocked.viewport.isValid()) {
611 return;
612 }
613
Jeff Brown2352b972011-04-12 22:39:53 -0700614 mSpriteController->openTransaction();
615
616 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
617 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
Arthur Hungb9b32002018-12-18 17:39:43 +0800618 mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId);
Jeff Brown2352b972011-04-12 22:39:53 -0700619
620 if (mLocked.pointerAlpha > 0) {
621 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
622 mLocked.pointerSprite->setVisible(true);
623 } else {
624 mLocked.pointerSprite->setVisible(false);
625 }
626
627 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
Jun Mukai1db53972015-09-11 18:08:31 -0700628 if (mLocked.presentation == PRESENTATION_POINTER) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100629 if (mLocked.requestedPointerType == mPolicy->getDefaultPointerIconId()) {
Jun Mukai1db53972015-09-11 18:08:31 -0700630 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
631 } else {
Jun Mukai808196f2015-10-28 16:46:44 -0700632 std::map<int32_t, SpriteIcon>::const_iterator iter =
Michael Wrighte051f6f2016-05-13 17:44:16 +0100633 mLocked.additionalMouseResources.find(mLocked.requestedPointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700634 if (iter != mLocked.additionalMouseResources.end()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700635 std::map<int32_t, PointerAnimation>::const_iterator anim_iter =
Michael Wrighte051f6f2016-05-13 17:44:16 +0100636 mLocked.animationResources.find(mLocked.requestedPointerType);
Jun Mukai808196f2015-10-28 16:46:44 -0700637 if (anim_iter != mLocked.animationResources.end()) {
638 mLocked.animationFrameIndex = 0;
639 mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC);
640 startAnimationLocked();
641 }
Jun Mukai1db53972015-09-11 18:08:31 -0700642 mLocked.pointerSprite->setIcon(iter->second);
643 } else {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100644 ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType);
Jun Mukai1db53972015-09-11 18:08:31 -0700645 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
646 }
647 }
648 } else {
649 mLocked.pointerSprite->setIcon(mResources.spotAnchor);
650 }
Jeff Brown2352b972011-04-12 22:39:53 -0700651 mLocked.pointerIconChanged = false;
652 mLocked.presentationChanged = false;
653 }
654
655 mSpriteController->closeTransaction();
656}
657
658PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
659 for (size_t i = 0; i < mLocked.spots.size(); i++) {
660 Spot* spot = mLocked.spots.itemAt(i);
661 if (spot->id == id) {
662 return spot;
663 }
664 }
665 return NULL;
666}
667
668PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
669 // Remove spots until we have fewer than MAX_SPOTS remaining.
670 while (mLocked.spots.size() >= MAX_SPOTS) {
671 Spot* spot = removeFirstFadingSpotLocked();
672 if (!spot) {
673 spot = mLocked.spots.itemAt(0);
674 mLocked.spots.removeAt(0);
675 }
676 releaseSpotLocked(spot);
677 }
678
679 // Obtain a sprite from the recycled pool.
680 sp<Sprite> sprite;
681 if (! mLocked.recycledSprites.isEmpty()) {
682 sprite = mLocked.recycledSprites.top();
683 mLocked.recycledSprites.pop();
684 } else {
685 sprite = mSpriteController->createSprite();
686 }
687
688 // Return the new spot.
689 Spot* spot = new Spot(id, sprite);
690 mLocked.spots.push(spot);
691 return spot;
692}
693
694PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
695 for (size_t i = 0; i < mLocked.spots.size(); i++) {
696 Spot* spot = mLocked.spots.itemAt(i);
697 if (spot->id == Spot::INVALID_ID) {
698 mLocked.spots.removeAt(i);
699 return spot;
700 }
701 }
702 return NULL;
703}
704
705void PointerController::releaseSpotLocked(Spot* spot) {
706 spot->sprite->clearIcon();
707
708 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
709 mLocked.recycledSprites.push(spot->sprite);
710 }
711
712 delete spot;
713}
714
715void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
716 if (spot->id != Spot::INVALID_ID) {
717 spot->id = Spot::INVALID_ID;
718 startAnimationLocked();
719 }
720}
721
722void PointerController::fadeOutAndReleaseAllSpotsLocked() {
723 for (size_t i = 0; i < mLocked.spots.size(); i++) {
724 Spot* spot = mLocked.spots.itemAt(i);
725 fadeOutAndReleaseSpotLocked(spot);
726 }
727}
728
Arthur Hungb9b32002018-12-18 17:39:43 +0800729void PointerController::loadResourcesLocked() REQUIRES(mLock) {
Jeff Brown2352b972011-04-12 22:39:53 -0700730 mPolicy->loadPointerResources(&mResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800731
732 if (mLocked.presentation == PRESENTATION_POINTER) {
733 mLocked.additionalMouseResources.clear();
734 mLocked.animationResources.clear();
735 mPolicy->loadPointerIcon(&mLocked.pointerIcon);
736 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
737 &mLocked.animationResources);
738 }
739
740 mLocked.pointerIconChanged = true;
Jeff Brown2352b972011-04-12 22:39:53 -0700741}
742
743
744// --- PointerController::Spot ---
745
746void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
747 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
748 sprite->setAlpha(alpha);
749 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
750 sprite->setPosition(x, y);
751
752 this->x = x;
753 this->y = y;
754
755 if (icon != lastIcon) {
756 lastIcon = icon;
757 if (icon) {
758 sprite->setIcon(*icon);
759 sprite->setVisible(true);
760 } else {
761 sprite->setVisible(false);
762 }
763 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800764}
765
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800766} // namespace android