blob: bd6af788bcf742d355218501add2dfb43be36a44 [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"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages about pointer updates
22#define DEBUG_POINTER_UPDATES 0
23
24#include "PointerController.h"
25
26#include <cutils/log.h>
27
Andreas Gampe6b83b762014-11-10 15:55:11 -080028#pragma GCC diagnostic push
29#pragma GCC diagnostic ignored "-Wunused-parameter"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080030#include <SkBitmap.h>
31#include <SkCanvas.h>
32#include <SkColor.h>
33#include <SkPaint.h>
34#include <SkXfermode.h>
Andreas Gampe6b83b762014-11-10 15:55:11 -080035#pragma GCC diagnostic pop
Jeff Brownb4ff35d2011-01-02 16:37:43 -080036
37namespace android {
38
39// --- PointerController ---
40
Jeff Brown05dc66a2011-03-02 14:41:58 -080041// Time to wait before starting the fade when the pointer is inactive.
Jeff Brown2352b972011-04-12 22:39:53 -070042static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
43static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
44
Jeff Brown2352b972011-04-12 22:39:53 -070045// Time to spend fading out the spot completely.
46static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080047
48// Time to spend fading out the pointer completely.
Jeff Brown2352b972011-04-12 22:39:53 -070049static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080050
Jun Mukaic0c0ac32015-10-27 10:09:21 -070051// The number of events to be read at once for DisplayEventReceiver.
52static const int EVENT_BUFFER_SIZE = 100;
Jeff Brown05dc66a2011-03-02 14:41:58 -080053
Jeff Brown2352b972011-04-12 22:39:53 -070054// --- PointerController ---
55
56PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
57 const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
58 mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
Jeff Brown5541de92011-04-11 11:54:25 -070059 mHandler = new WeakMessageHandler(this);
60
Jun Mukaic0c0ac32015-10-27 10:09:21 -070061 if (mDisplayEventReceiver.initCheck() == NO_ERROR) {
62 mLooper->addFd(mDisplayEventReceiver.getFd(), Looper::POLL_CALLBACK,
63 Looper::EVENT_INPUT, this, nullptr);
64 } else {
65 ALOGE("Failed to initialize DisplayEventReceiver.");
66 }
67
Jeff Brownb4ff35d2011-01-02 16:37:43 -080068 AutoMutex _l(mLock);
69
Jeff Brown2352b972011-04-12 22:39:53 -070070 mLocked.animationPending = false;
71
Jeff Brownb4ff35d2011-01-02 16:37:43 -080072 mLocked.displayWidth = -1;
73 mLocked.displayHeight = -1;
74 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
75
Jeff Brown2352b972011-04-12 22:39:53 -070076 mLocked.presentation = PRESENTATION_POINTER;
77 mLocked.presentationChanged = false;
78
79 mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
80
Jeff Brown538881e2011-05-25 18:23:38 -070081 mLocked.pointerFadeDirection = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080082 mLocked.pointerX = 0;
83 mLocked.pointerY = 0;
Jeff Brown538881e2011-05-25 18:23:38 -070084 mLocked.pointerAlpha = 0.0f; // pointer is initially faded
Jeff Brown2352b972011-04-12 22:39:53 -070085 mLocked.pointerSprite = mSpriteController->createSprite();
86 mLocked.pointerIconChanged = false;
Jun Mukai5ec74202015-10-07 16:58:09 +090087 mLocked.requestedPointerShape = mPolicy->getDefaultPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -070088
Jun Mukai808196f2015-10-28 16:46:44 -070089 mLocked.animationFrameIndex = 0;
90 mLocked.lastFrameUpdatedTime = 0;
91
Jeff Brownb4ff35d2011-01-02 16:37:43 -080092 mLocked.buttonState = 0;
93
Jeff Brown2352b972011-04-12 22:39:53 -070094 loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080095}
96
97PointerController::~PointerController() {
Jeff Brown05dc66a2011-03-02 14:41:58 -080098 mLooper->removeMessages(mHandler);
99
Jeff Brown5541de92011-04-11 11:54:25 -0700100 AutoMutex _l(mLock);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800101
Jeff Brown2352b972011-04-12 22:39:53 -0700102 mLocked.pointerSprite.clear();
103
104 for (size_t i = 0; i < mLocked.spots.size(); i++) {
105 delete mLocked.spots.itemAt(i);
106 }
107 mLocked.spots.clear();
108 mLocked.recycledSprites.clear();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800109}
110
111bool PointerController::getBounds(float* outMinX, float* outMinY,
112 float* outMaxX, float* outMaxY) const {
113 AutoMutex _l(mLock);
114
115 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
116}
117
118bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
119 float* outMaxX, float* outMaxY) const {
120 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
121 return false;
122 }
123
124 *outMinX = 0;
125 *outMinY = 0;
126 switch (mLocked.displayOrientation) {
127 case DISPLAY_ORIENTATION_90:
128 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800129 *outMaxX = mLocked.displayHeight - 1;
130 *outMaxY = mLocked.displayWidth - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800131 break;
132 default:
Jeff Brownd41cff22011-03-03 02:09:54 -0800133 *outMaxX = mLocked.displayWidth - 1;
134 *outMaxY = mLocked.displayHeight - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800135 break;
136 }
137 return true;
138}
139
140void PointerController::move(float deltaX, float deltaY) {
141#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000142 ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800143#endif
144 if (deltaX == 0.0f && deltaY == 0.0f) {
145 return;
146 }
147
148 AutoMutex _l(mLock);
149
150 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
151}
152
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700153void PointerController::setButtonState(int32_t buttonState) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800154#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000155 ALOGD("Set button state 0x%08x", buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800156#endif
157 AutoMutex _l(mLock);
158
159 if (mLocked.buttonState != buttonState) {
160 mLocked.buttonState = buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800161 }
162}
163
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700164int32_t PointerController::getButtonState() const {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800165 AutoMutex _l(mLock);
166
167 return mLocked.buttonState;
168}
169
170void PointerController::setPosition(float x, float y) {
171#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000172 ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800173#endif
174 AutoMutex _l(mLock);
175
176 setPositionLocked(x, y);
177}
178
179void PointerController::setPositionLocked(float x, float y) {
180 float minX, minY, maxX, maxY;
181 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
182 if (x <= minX) {
183 mLocked.pointerX = minX;
184 } else if (x >= maxX) {
185 mLocked.pointerX = maxX;
186 } else {
187 mLocked.pointerX = x;
188 }
189 if (y <= minY) {
190 mLocked.pointerY = minY;
191 } else if (y >= maxY) {
192 mLocked.pointerY = maxY;
193 } else {
194 mLocked.pointerY = y;
195 }
Jeff Brown2352b972011-04-12 22:39:53 -0700196 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800197 }
198}
199
200void PointerController::getPosition(float* outX, float* outY) const {
201 AutoMutex _l(mLock);
202
203 *outX = mLocked.pointerX;
204 *outY = mLocked.pointerY;
205}
206
Jeff Brown538881e2011-05-25 18:23:38 -0700207void PointerController::fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800208 AutoMutex _l(mLock);
209
Jeff Brown538881e2011-05-25 18:23:38 -0700210 // Remove the inactivity timeout, since we are fading now.
211 removeInactivityTimeoutLocked();
212
213 // Start fading.
214 if (transition == TRANSITION_IMMEDIATE) {
215 mLocked.pointerFadeDirection = 0;
216 mLocked.pointerAlpha = 0.0f;
217 updatePointerLocked();
218 } else {
219 mLocked.pointerFadeDirection = -1;
220 startAnimationLocked();
221 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800222}
223
Jeff Brown538881e2011-05-25 18:23:38 -0700224void PointerController::unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800225 AutoMutex _l(mLock);
226
Jeff Brown2352b972011-04-12 22:39:53 -0700227 // Always reset the inactivity timer.
228 resetInactivityTimeoutLocked();
229
Jeff Brown538881e2011-05-25 18:23:38 -0700230 // Start unfading.
231 if (transition == TRANSITION_IMMEDIATE) {
232 mLocked.pointerFadeDirection = 0;
Jeff Brown2352b972011-04-12 22:39:53 -0700233 mLocked.pointerAlpha = 1.0f;
234 updatePointerLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700235 } else {
236 mLocked.pointerFadeDirection = 1;
237 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800238 }
239}
240
Jeff Brown2352b972011-04-12 22:39:53 -0700241void PointerController::setPresentation(Presentation presentation) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800242 AutoMutex _l(mLock);
243
Jun Mukai1db53972015-09-11 18:08:31 -0700244 if (presentation == PRESENTATION_POINTER && mLocked.additionalMouseResources.empty()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700245 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
246 &mLocked.animationResources);
Jun Mukai1db53972015-09-11 18:08:31 -0700247 }
248
Jeff Brown2352b972011-04-12 22:39:53 -0700249 if (mLocked.presentation != presentation) {
250 mLocked.presentation = presentation;
251 mLocked.presentationChanged = true;
252
253 if (presentation != PRESENTATION_SPOT) {
254 fadeOutAndReleaseAllSpotsLocked();
255 }
256
257 updatePointerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800258 }
259}
260
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700261void PointerController::setSpots(const PointerCoords* spotCoords,
262 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
Jeff Brown2352b972011-04-12 22:39:53 -0700263#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000264 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
Jeff Brown2352b972011-04-12 22:39:53 -0700265 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
266 uint32_t id = idBits.firstMarkedBit();
267 idBits.clearBit(id);
268 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
Steve Block5baa3a62011-12-20 16:23:08 +0000269 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
Jeff Brown2352b972011-04-12 22:39:53 -0700270 c.getAxisValue(AMOTION_EVENT_AXIS_X),
271 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
272 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
273 }
274#endif
275
276 AutoMutex _l(mLock);
277
278 mSpriteController->openTransaction();
279
280 // Add or move spots for fingers that are down.
281 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700282 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -0700283 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
284 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
285 ? mResources.spotTouch : mResources.spotHover;
286 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
287 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
288
289 Spot* spot = getSpotLocked(id);
290 if (!spot) {
291 spot = createAndAddSpotLocked(id);
292 }
293
294 spot->updateSprite(&icon, x, y);
295 }
296
297 // Remove spots for fingers that went up.
298 for (size_t i = 0; i < mLocked.spots.size(); i++) {
299 Spot* spot = mLocked.spots.itemAt(i);
300 if (spot->id != Spot::INVALID_ID
301 && !spotIdBits.hasBit(spot->id)) {
302 fadeOutAndReleaseSpotLocked(spot);
303 }
304 }
305
306 mSpriteController->closeTransaction();
307}
308
309void PointerController::clearSpots() {
310#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000311 ALOGD("clearSpots");
Jeff Brown2352b972011-04-12 22:39:53 -0700312#endif
313
314 AutoMutex _l(mLock);
315
316 fadeOutAndReleaseAllSpotsLocked();
317}
318
319void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
320 AutoMutex _l(mLock);
321
322 if (mLocked.inactivityTimeout != inactivityTimeout) {
323 mLocked.inactivityTimeout = inactivityTimeout;
324 resetInactivityTimeoutLocked();
325 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800326}
327
Jeff Brownd728bf52012-09-08 18:05:28 -0700328void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800329 AutoMutex _l(mLock);
330
Jeff Brownd728bf52012-09-08 18:05:28 -0700331 // Adjust to use the display's unrotated coordinate frame.
332 if (orientation == DISPLAY_ORIENTATION_90
333 || orientation == DISPLAY_ORIENTATION_270) {
334 int32_t temp = height;
335 height = width;
336 width = temp;
337 }
338
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800339 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
340 mLocked.displayWidth = width;
341 mLocked.displayHeight = height;
342
343 float minX, minY, maxX, maxY;
344 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
345 mLocked.pointerX = (minX + maxX) * 0.5f;
346 mLocked.pointerY = (minY + maxY) * 0.5f;
347 } else {
348 mLocked.pointerX = 0;
349 mLocked.pointerY = 0;
350 }
351
Jeff Brown2352b972011-04-12 22:39:53 -0700352 fadeOutAndReleaseAllSpotsLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800353 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800354
355 if (mLocked.displayOrientation != orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800356 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
357 // This creates an invariant frame of reference that we can easily rotate when
358 // taking into account that the pointer may be located at fractional pixel offsets.
359 float x = mLocked.pointerX + 0.5f;
360 float y = mLocked.pointerY + 0.5f;
361 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800362
Jeff Brownd41cff22011-03-03 02:09:54 -0800363 // Undo the previous rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800364 switch (mLocked.displayOrientation) {
365 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800366 temp = x;
367 x = mLocked.displayWidth - y;
368 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800369 break;
370 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800371 x = mLocked.displayWidth - x;
372 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800373 break;
374 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800375 temp = x;
376 x = y;
377 y = mLocked.displayHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800378 break;
379 }
380
Jeff Brownd41cff22011-03-03 02:09:54 -0800381 // Perform the new rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800382 switch (orientation) {
383 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800384 temp = x;
385 x = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700386 y = mLocked.displayWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800387 break;
388 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800389 x = mLocked.displayWidth - x;
390 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800391 break;
392 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800393 temp = x;
394 x = mLocked.displayHeight - y;
395 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800396 break;
397 }
398
Jeff Brownd41cff22011-03-03 02:09:54 -0800399 // Apply offsets to convert from the pixel center to the pixel top-left corner position
400 // and save the results.
401 mLocked.pointerX = x - 0.5f;
402 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800403 mLocked.displayOrientation = orientation;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800404 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700405
406 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800407}
408
Jun Mukai808196f2015-10-28 16:46:44 -0700409void PointerController::updatePointerShape(int32_t iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -0700410 AutoMutex _l(mLock);
411 if (mLocked.requestedPointerShape != iconId) {
412 mLocked.requestedPointerShape = iconId;
413 mLocked.presentationChanged = true;
414 updatePointerLocked();
415 }
416}
417
Jeff Brown2352b972011-04-12 22:39:53 -0700418void PointerController::setPointerIcon(const SpriteIcon& icon) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800419 AutoMutex _l(mLock);
420
Jeff Brown2352b972011-04-12 22:39:53 -0700421 mLocked.pointerIcon = icon.copy();
422 mLocked.pointerIconChanged = true;
423
424 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800425}
426
Jeff Brown05dc66a2011-03-02 14:41:58 -0800427void PointerController::handleMessage(const Message& message) {
428 switch (message.what) {
Jeff Brown2352b972011-04-12 22:39:53 -0700429 case MSG_INACTIVITY_TIMEOUT:
430 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800431 break;
432 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800433}
434
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700435int PointerController::handleEvent(int /* fd */, int events, void* /* data */) {
436 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
437 ALOGE("Display event receiver pipe was closed or an error occurred. "
438 "events=0x%x", events);
439 return 0; // remove the callback
440 }
441
442 if (!(events & Looper::EVENT_INPUT)) {
443 ALOGW("Received spurious callback for unhandled poll event. "
444 "events=0x%x", events);
445 return 1; // keep the callback
446 }
447
448 bool gotVsync = false;
449 ssize_t n;
450 nsecs_t timestamp;
451 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
452 while ((n = mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
453 for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
454 if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
455 timestamp = buf[i].header.timestamp;
456 gotVsync = true;
457 }
458 }
459 }
460 if (gotVsync) {
461 doAnimate(timestamp);
462 }
463 return 1; // keep the callback
464}
465
466void PointerController::doAnimate(nsecs_t timestamp) {
Jeff Brown2352b972011-04-12 22:39:53 -0700467 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800468
Jeff Brown2352b972011-04-12 22:39:53 -0700469 mLocked.animationPending = false;
Jun Mukai808196f2015-10-28 16:46:44 -0700470
471 bool keepFading = doFadingAnimationLocked(timestamp);
472 bool keepBitmapFlipping = doBitmapAnimationLocked(timestamp);
473 if (keepFading || keepBitmapFlipping) {
474 startAnimationLocked();
475 }
476}
477
478bool PointerController::doFadingAnimationLocked(nsecs_t timestamp) {
479 bool keepAnimating = false;
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700480 nsecs_t frameDelay = timestamp - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800481
Jeff Brown2352b972011-04-12 22:39:53 -0700482 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700483 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown2352b972011-04-12 22:39:53 -0700484 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700485 if (mLocked.pointerAlpha <= 0.0f) {
486 mLocked.pointerAlpha = 0.0f;
487 mLocked.pointerFadeDirection = 0;
488 } else {
489 keepAnimating = true;
490 }
491 updatePointerLocked();
492 } else if (mLocked.pointerFadeDirection > 0) {
493 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
494 if (mLocked.pointerAlpha >= 1.0f) {
495 mLocked.pointerAlpha = 1.0f;
496 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800497 } else {
Jeff Brown2352b972011-04-12 22:39:53 -0700498 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800499 }
Jeff Brown2352b972011-04-12 22:39:53 -0700500 updatePointerLocked();
501 }
502
503 // Animate spots that are fading out and being removed.
504 for (size_t i = 0; i < mLocked.spots.size(); i++) {
505 Spot* spot = mLocked.spots.itemAt(i);
506 if (spot->id == Spot::INVALID_ID) {
507 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
508 if (spot->alpha <= 0) {
509 mLocked.spots.removeAt(i--);
510 releaseSpotLocked(spot);
511 } else {
512 spot->sprite->setAlpha(spot->alpha);
513 keepAnimating = true;
514 }
515 }
516 }
Jun Mukai808196f2015-10-28 16:46:44 -0700517 return keepAnimating;
518}
Jeff Brown2352b972011-04-12 22:39:53 -0700519
Jun Mukai808196f2015-10-28 16:46:44 -0700520bool PointerController::doBitmapAnimationLocked(nsecs_t timestamp) {
521 std::map<int32_t, PointerAnimation>::const_iterator iter = mLocked.animationResources.find(
522 mLocked.requestedPointerShape);
523 if (iter == mLocked.animationResources.end()) {
524 return false;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800525 }
Jun Mukai808196f2015-10-28 16:46:44 -0700526
527 if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) {
528 mSpriteController->openTransaction();
529
530 int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame;
531 mLocked.animationFrameIndex += incr;
532 mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr;
533 while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) {
534 mLocked.animationFrameIndex -= iter->second.animationFrames.size();
535 }
536 mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]);
537
538 mSpriteController->closeTransaction();
539 }
540
541 // Keep animating.
542 return true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800543}
544
Jeff Brown2352b972011-04-12 22:39:53 -0700545void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700546 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800547}
548
Jeff Brown2352b972011-04-12 22:39:53 -0700549void PointerController::startAnimationLocked() {
550 if (!mLocked.animationPending) {
551 mLocked.animationPending = true;
552 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700553 mDisplayEventReceiver.requestNextVsync();
Jeff Brown2352b972011-04-12 22:39:53 -0700554 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800555}
556
Jeff Brown2352b972011-04-12 22:39:53 -0700557void PointerController::resetInactivityTimeoutLocked() {
558 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
559
560 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
561 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
562 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
563}
564
Jeff Brown538881e2011-05-25 18:23:38 -0700565void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700566 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown2352b972011-04-12 22:39:53 -0700567}
568
569void PointerController::updatePointerLocked() {
570 mSpriteController->openTransaction();
571
572 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
573 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
574
575 if (mLocked.pointerAlpha > 0) {
576 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
577 mLocked.pointerSprite->setVisible(true);
578 } else {
579 mLocked.pointerSprite->setVisible(false);
580 }
581
582 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
Jun Mukai1db53972015-09-11 18:08:31 -0700583 if (mLocked.presentation == PRESENTATION_POINTER) {
Jun Mukai5ec74202015-10-07 16:58:09 +0900584 if (mLocked.requestedPointerShape == mPolicy->getDefaultPointerIconId()) {
Jun Mukai1db53972015-09-11 18:08:31 -0700585 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
586 } else {
Jun Mukai808196f2015-10-28 16:46:44 -0700587 std::map<int32_t, SpriteIcon>::const_iterator iter =
Jun Mukai1db53972015-09-11 18:08:31 -0700588 mLocked.additionalMouseResources.find(mLocked.requestedPointerShape);
589 if (iter != mLocked.additionalMouseResources.end()) {
Jun Mukai808196f2015-10-28 16:46:44 -0700590 std::map<int32_t, PointerAnimation>::const_iterator anim_iter =
591 mLocked.animationResources.find(mLocked.requestedPointerShape);
592 if (anim_iter != mLocked.animationResources.end()) {
593 mLocked.animationFrameIndex = 0;
594 mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC);
595 startAnimationLocked();
596 }
Jun Mukai1db53972015-09-11 18:08:31 -0700597 mLocked.pointerSprite->setIcon(iter->second);
598 } else {
599 ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerShape);
600 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
601 }
602 }
603 } else {
604 mLocked.pointerSprite->setIcon(mResources.spotAnchor);
605 }
Jeff Brown2352b972011-04-12 22:39:53 -0700606 mLocked.pointerIconChanged = false;
607 mLocked.presentationChanged = false;
608 }
609
610 mSpriteController->closeTransaction();
611}
612
613PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
614 for (size_t i = 0; i < mLocked.spots.size(); i++) {
615 Spot* spot = mLocked.spots.itemAt(i);
616 if (spot->id == id) {
617 return spot;
618 }
619 }
620 return NULL;
621}
622
623PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
624 // Remove spots until we have fewer than MAX_SPOTS remaining.
625 while (mLocked.spots.size() >= MAX_SPOTS) {
626 Spot* spot = removeFirstFadingSpotLocked();
627 if (!spot) {
628 spot = mLocked.spots.itemAt(0);
629 mLocked.spots.removeAt(0);
630 }
631 releaseSpotLocked(spot);
632 }
633
634 // Obtain a sprite from the recycled pool.
635 sp<Sprite> sprite;
636 if (! mLocked.recycledSprites.isEmpty()) {
637 sprite = mLocked.recycledSprites.top();
638 mLocked.recycledSprites.pop();
639 } else {
640 sprite = mSpriteController->createSprite();
641 }
642
643 // Return the new spot.
644 Spot* spot = new Spot(id, sprite);
645 mLocked.spots.push(spot);
646 return spot;
647}
648
649PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
650 for (size_t i = 0; i < mLocked.spots.size(); i++) {
651 Spot* spot = mLocked.spots.itemAt(i);
652 if (spot->id == Spot::INVALID_ID) {
653 mLocked.spots.removeAt(i);
654 return spot;
655 }
656 }
657 return NULL;
658}
659
660void PointerController::releaseSpotLocked(Spot* spot) {
661 spot->sprite->clearIcon();
662
663 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
664 mLocked.recycledSprites.push(spot->sprite);
665 }
666
667 delete spot;
668}
669
670void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
671 if (spot->id != Spot::INVALID_ID) {
672 spot->id = Spot::INVALID_ID;
673 startAnimationLocked();
674 }
675}
676
677void PointerController::fadeOutAndReleaseAllSpotsLocked() {
678 for (size_t i = 0; i < mLocked.spots.size(); i++) {
679 Spot* spot = mLocked.spots.itemAt(i);
680 fadeOutAndReleaseSpotLocked(spot);
681 }
682}
683
684void PointerController::loadResources() {
685 mPolicy->loadPointerResources(&mResources);
686}
687
688
689// --- PointerController::Spot ---
690
691void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
692 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
693 sprite->setAlpha(alpha);
694 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
695 sprite->setPosition(x, y);
696
697 this->x = x;
698 this->y = y;
699
700 if (icon != lastIcon) {
701 lastIcon = icon;
702 if (icon) {
703 sprite->setIcon(*icon);
704 sprite->setVisible(true);
705 } else {
706 sprite->setVisible(false);
707 }
708 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800709}
710
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800711} // namespace android