blob: c9586e4ca81d3a260ad7ed251012216599bf0577 [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
Jeff Brownb4ff35d2011-01-02 16:37:43 -080089 mLocked.buttonState = 0;
90
Jeff Brown2352b972011-04-12 22:39:53 -070091 loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080092}
93
94PointerController::~PointerController() {
Jeff Brown05dc66a2011-03-02 14:41:58 -080095 mLooper->removeMessages(mHandler);
96
Jeff Brown5541de92011-04-11 11:54:25 -070097 AutoMutex _l(mLock);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080098
Jeff Brown2352b972011-04-12 22:39:53 -070099 mLocked.pointerSprite.clear();
100
101 for (size_t i = 0; i < mLocked.spots.size(); i++) {
102 delete mLocked.spots.itemAt(i);
103 }
104 mLocked.spots.clear();
105 mLocked.recycledSprites.clear();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800106}
107
108bool PointerController::getBounds(float* outMinX, float* outMinY,
109 float* outMaxX, float* outMaxY) const {
110 AutoMutex _l(mLock);
111
112 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
113}
114
115bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
116 float* outMaxX, float* outMaxY) const {
117 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
118 return false;
119 }
120
121 *outMinX = 0;
122 *outMinY = 0;
123 switch (mLocked.displayOrientation) {
124 case DISPLAY_ORIENTATION_90:
125 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800126 *outMaxX = mLocked.displayHeight - 1;
127 *outMaxY = mLocked.displayWidth - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800128 break;
129 default:
Jeff Brownd41cff22011-03-03 02:09:54 -0800130 *outMaxX = mLocked.displayWidth - 1;
131 *outMaxY = mLocked.displayHeight - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800132 break;
133 }
134 return true;
135}
136
137void PointerController::move(float deltaX, float deltaY) {
138#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000139 ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800140#endif
141 if (deltaX == 0.0f && deltaY == 0.0f) {
142 return;
143 }
144
145 AutoMutex _l(mLock);
146
147 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
148}
149
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700150void PointerController::setButtonState(int32_t buttonState) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800151#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000152 ALOGD("Set button state 0x%08x", buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800153#endif
154 AutoMutex _l(mLock);
155
156 if (mLocked.buttonState != buttonState) {
157 mLocked.buttonState = buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800158 }
159}
160
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700161int32_t PointerController::getButtonState() const {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800162 AutoMutex _l(mLock);
163
164 return mLocked.buttonState;
165}
166
167void PointerController::setPosition(float x, float y) {
168#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000169 ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800170#endif
171 AutoMutex _l(mLock);
172
173 setPositionLocked(x, y);
174}
175
176void PointerController::setPositionLocked(float x, float y) {
177 float minX, minY, maxX, maxY;
178 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
179 if (x <= minX) {
180 mLocked.pointerX = minX;
181 } else if (x >= maxX) {
182 mLocked.pointerX = maxX;
183 } else {
184 mLocked.pointerX = x;
185 }
186 if (y <= minY) {
187 mLocked.pointerY = minY;
188 } else if (y >= maxY) {
189 mLocked.pointerY = maxY;
190 } else {
191 mLocked.pointerY = y;
192 }
Jeff Brown2352b972011-04-12 22:39:53 -0700193 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800194 }
195}
196
197void PointerController::getPosition(float* outX, float* outY) const {
198 AutoMutex _l(mLock);
199
200 *outX = mLocked.pointerX;
201 *outY = mLocked.pointerY;
202}
203
Jeff Brown538881e2011-05-25 18:23:38 -0700204void PointerController::fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800205 AutoMutex _l(mLock);
206
Jeff Brown538881e2011-05-25 18:23:38 -0700207 // Remove the inactivity timeout, since we are fading now.
208 removeInactivityTimeoutLocked();
209
210 // Start fading.
211 if (transition == TRANSITION_IMMEDIATE) {
212 mLocked.pointerFadeDirection = 0;
213 mLocked.pointerAlpha = 0.0f;
214 updatePointerLocked();
215 } else {
216 mLocked.pointerFadeDirection = -1;
217 startAnimationLocked();
218 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800219}
220
Jeff Brown538881e2011-05-25 18:23:38 -0700221void PointerController::unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800222 AutoMutex _l(mLock);
223
Jeff Brown2352b972011-04-12 22:39:53 -0700224 // Always reset the inactivity timer.
225 resetInactivityTimeoutLocked();
226
Jeff Brown538881e2011-05-25 18:23:38 -0700227 // Start unfading.
228 if (transition == TRANSITION_IMMEDIATE) {
229 mLocked.pointerFadeDirection = 0;
Jeff Brown2352b972011-04-12 22:39:53 -0700230 mLocked.pointerAlpha = 1.0f;
231 updatePointerLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700232 } else {
233 mLocked.pointerFadeDirection = 1;
234 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800235 }
236}
237
Jeff Brown2352b972011-04-12 22:39:53 -0700238void PointerController::setPresentation(Presentation presentation) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800239 AutoMutex _l(mLock);
240
Jun Mukai1db53972015-09-11 18:08:31 -0700241 if (presentation == PRESENTATION_POINTER && mLocked.additionalMouseResources.empty()) {
242 mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources);
243 }
244
Jeff Brown2352b972011-04-12 22:39:53 -0700245 if (mLocked.presentation != presentation) {
246 mLocked.presentation = presentation;
247 mLocked.presentationChanged = true;
248
249 if (presentation != PRESENTATION_SPOT) {
250 fadeOutAndReleaseAllSpotsLocked();
251 }
252
253 updatePointerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800254 }
255}
256
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700257void PointerController::setSpots(const PointerCoords* spotCoords,
258 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
Jeff Brown2352b972011-04-12 22:39:53 -0700259#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000260 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
Jeff Brown2352b972011-04-12 22:39:53 -0700261 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
262 uint32_t id = idBits.firstMarkedBit();
263 idBits.clearBit(id);
264 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
Steve Block5baa3a62011-12-20 16:23:08 +0000265 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
Jeff Brown2352b972011-04-12 22:39:53 -0700266 c.getAxisValue(AMOTION_EVENT_AXIS_X),
267 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
268 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
269 }
270#endif
271
272 AutoMutex _l(mLock);
273
274 mSpriteController->openTransaction();
275
276 // Add or move spots for fingers that are down.
277 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -0700279 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
280 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
281 ? mResources.spotTouch : mResources.spotHover;
282 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
283 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
284
285 Spot* spot = getSpotLocked(id);
286 if (!spot) {
287 spot = createAndAddSpotLocked(id);
288 }
289
290 spot->updateSprite(&icon, x, y);
291 }
292
293 // Remove spots for fingers that went up.
294 for (size_t i = 0; i < mLocked.spots.size(); i++) {
295 Spot* spot = mLocked.spots.itemAt(i);
296 if (spot->id != Spot::INVALID_ID
297 && !spotIdBits.hasBit(spot->id)) {
298 fadeOutAndReleaseSpotLocked(spot);
299 }
300 }
301
302 mSpriteController->closeTransaction();
303}
304
305void PointerController::clearSpots() {
306#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000307 ALOGD("clearSpots");
Jeff Brown2352b972011-04-12 22:39:53 -0700308#endif
309
310 AutoMutex _l(mLock);
311
312 fadeOutAndReleaseAllSpotsLocked();
313}
314
315void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
316 AutoMutex _l(mLock);
317
318 if (mLocked.inactivityTimeout != inactivityTimeout) {
319 mLocked.inactivityTimeout = inactivityTimeout;
320 resetInactivityTimeoutLocked();
321 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800322}
323
Jeff Brownd728bf52012-09-08 18:05:28 -0700324void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800325 AutoMutex _l(mLock);
326
Jeff Brownd728bf52012-09-08 18:05:28 -0700327 // Adjust to use the display's unrotated coordinate frame.
328 if (orientation == DISPLAY_ORIENTATION_90
329 || orientation == DISPLAY_ORIENTATION_270) {
330 int32_t temp = height;
331 height = width;
332 width = temp;
333 }
334
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800335 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
336 mLocked.displayWidth = width;
337 mLocked.displayHeight = height;
338
339 float minX, minY, maxX, maxY;
340 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
341 mLocked.pointerX = (minX + maxX) * 0.5f;
342 mLocked.pointerY = (minY + maxY) * 0.5f;
343 } else {
344 mLocked.pointerX = 0;
345 mLocked.pointerY = 0;
346 }
347
Jeff Brown2352b972011-04-12 22:39:53 -0700348 fadeOutAndReleaseAllSpotsLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800349 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800350
351 if (mLocked.displayOrientation != orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800352 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
353 // This creates an invariant frame of reference that we can easily rotate when
354 // taking into account that the pointer may be located at fractional pixel offsets.
355 float x = mLocked.pointerX + 0.5f;
356 float y = mLocked.pointerY + 0.5f;
357 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800358
Jeff Brownd41cff22011-03-03 02:09:54 -0800359 // Undo the previous rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800360 switch (mLocked.displayOrientation) {
361 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800362 temp = x;
363 x = mLocked.displayWidth - y;
364 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800365 break;
366 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800367 x = mLocked.displayWidth - x;
368 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800369 break;
370 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800371 temp = x;
372 x = y;
373 y = mLocked.displayHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800374 break;
375 }
376
Jeff Brownd41cff22011-03-03 02:09:54 -0800377 // Perform the new rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800378 switch (orientation) {
379 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800380 temp = x;
381 x = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700382 y = mLocked.displayWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800383 break;
384 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800385 x = mLocked.displayWidth - x;
386 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800387 break;
388 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800389 temp = x;
390 x = mLocked.displayHeight - y;
391 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800392 break;
393 }
394
Jeff Brownd41cff22011-03-03 02:09:54 -0800395 // Apply offsets to convert from the pixel center to the pixel top-left corner position
396 // and save the results.
397 mLocked.pointerX = x - 0.5f;
398 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800399 mLocked.displayOrientation = orientation;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800400 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700401
402 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800403}
404
Jun Mukai1db53972015-09-11 18:08:31 -0700405void PointerController::updatePointerShape(int iconId) {
406 AutoMutex _l(mLock);
407 if (mLocked.requestedPointerShape != iconId) {
408 mLocked.requestedPointerShape = iconId;
409 mLocked.presentationChanged = true;
410 updatePointerLocked();
411 }
412}
413
Jeff Brown2352b972011-04-12 22:39:53 -0700414void PointerController::setPointerIcon(const SpriteIcon& icon) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800415 AutoMutex _l(mLock);
416
Jeff Brown2352b972011-04-12 22:39:53 -0700417 mLocked.pointerIcon = icon.copy();
418 mLocked.pointerIconChanged = true;
419
420 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800421}
422
Jeff Brown05dc66a2011-03-02 14:41:58 -0800423void PointerController::handleMessage(const Message& message) {
424 switch (message.what) {
Jeff Brown2352b972011-04-12 22:39:53 -0700425 case MSG_INACTIVITY_TIMEOUT:
426 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800427 break;
428 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800429}
430
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700431int PointerController::handleEvent(int /* fd */, int events, void* /* data */) {
432 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
433 ALOGE("Display event receiver pipe was closed or an error occurred. "
434 "events=0x%x", events);
435 return 0; // remove the callback
436 }
437
438 if (!(events & Looper::EVENT_INPUT)) {
439 ALOGW("Received spurious callback for unhandled poll event. "
440 "events=0x%x", events);
441 return 1; // keep the callback
442 }
443
444 bool gotVsync = false;
445 ssize_t n;
446 nsecs_t timestamp;
447 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
448 while ((n = mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
449 for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
450 if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
451 timestamp = buf[i].header.timestamp;
452 gotVsync = true;
453 }
454 }
455 }
456 if (gotVsync) {
457 doAnimate(timestamp);
458 }
459 return 1; // keep the callback
460}
461
462void PointerController::doAnimate(nsecs_t timestamp) {
Jeff Brown2352b972011-04-12 22:39:53 -0700463 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800464
Jeff Brown2352b972011-04-12 22:39:53 -0700465 bool keepAnimating = false;
466 mLocked.animationPending = false;
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700467 nsecs_t frameDelay = timestamp - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800468
Jeff Brown2352b972011-04-12 22:39:53 -0700469 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700470 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown2352b972011-04-12 22:39:53 -0700471 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700472 if (mLocked.pointerAlpha <= 0.0f) {
473 mLocked.pointerAlpha = 0.0f;
474 mLocked.pointerFadeDirection = 0;
475 } else {
476 keepAnimating = true;
477 }
478 updatePointerLocked();
479 } else if (mLocked.pointerFadeDirection > 0) {
480 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
481 if (mLocked.pointerAlpha >= 1.0f) {
482 mLocked.pointerAlpha = 1.0f;
483 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800484 } else {
Jeff Brown2352b972011-04-12 22:39:53 -0700485 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800486 }
Jeff Brown2352b972011-04-12 22:39:53 -0700487 updatePointerLocked();
488 }
489
490 // Animate spots that are fading out and being removed.
491 for (size_t i = 0; i < mLocked.spots.size(); i++) {
492 Spot* spot = mLocked.spots.itemAt(i);
493 if (spot->id == Spot::INVALID_ID) {
494 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
495 if (spot->alpha <= 0) {
496 mLocked.spots.removeAt(i--);
497 releaseSpotLocked(spot);
498 } else {
499 spot->sprite->setAlpha(spot->alpha);
500 keepAnimating = true;
501 }
502 }
503 }
504
505 if (keepAnimating) {
506 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800507 }
508}
509
Jeff Brown2352b972011-04-12 22:39:53 -0700510void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700511 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800512}
513
Jeff Brown2352b972011-04-12 22:39:53 -0700514void PointerController::startAnimationLocked() {
515 if (!mLocked.animationPending) {
516 mLocked.animationPending = true;
517 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700518 mDisplayEventReceiver.requestNextVsync();
Jeff Brown2352b972011-04-12 22:39:53 -0700519 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800520}
521
Jeff Brown2352b972011-04-12 22:39:53 -0700522void PointerController::resetInactivityTimeoutLocked() {
523 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
524
525 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
526 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
527 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
528}
529
Jeff Brown538881e2011-05-25 18:23:38 -0700530void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700531 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown2352b972011-04-12 22:39:53 -0700532}
533
534void PointerController::updatePointerLocked() {
535 mSpriteController->openTransaction();
536
537 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
538 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
539
540 if (mLocked.pointerAlpha > 0) {
541 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
542 mLocked.pointerSprite->setVisible(true);
543 } else {
544 mLocked.pointerSprite->setVisible(false);
545 }
546
547 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
Jun Mukai1db53972015-09-11 18:08:31 -0700548 if (mLocked.presentation == PRESENTATION_POINTER) {
Jun Mukai5ec74202015-10-07 16:58:09 +0900549 if (mLocked.requestedPointerShape == mPolicy->getDefaultPointerIconId()) {
Jun Mukai1db53972015-09-11 18:08:31 -0700550 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
551 } else {
552 std::map<int, SpriteIcon>::const_iterator iter =
553 mLocked.additionalMouseResources.find(mLocked.requestedPointerShape);
554 if (iter != mLocked.additionalMouseResources.end()) {
555 mLocked.pointerSprite->setIcon(iter->second);
556 } else {
557 ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerShape);
558 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
559 }
560 }
561 } else {
562 mLocked.pointerSprite->setIcon(mResources.spotAnchor);
563 }
Jeff Brown2352b972011-04-12 22:39:53 -0700564 mLocked.pointerIconChanged = false;
565 mLocked.presentationChanged = false;
566 }
567
568 mSpriteController->closeTransaction();
569}
570
571PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
572 for (size_t i = 0; i < mLocked.spots.size(); i++) {
573 Spot* spot = mLocked.spots.itemAt(i);
574 if (spot->id == id) {
575 return spot;
576 }
577 }
578 return NULL;
579}
580
581PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
582 // Remove spots until we have fewer than MAX_SPOTS remaining.
583 while (mLocked.spots.size() >= MAX_SPOTS) {
584 Spot* spot = removeFirstFadingSpotLocked();
585 if (!spot) {
586 spot = mLocked.spots.itemAt(0);
587 mLocked.spots.removeAt(0);
588 }
589 releaseSpotLocked(spot);
590 }
591
592 // Obtain a sprite from the recycled pool.
593 sp<Sprite> sprite;
594 if (! mLocked.recycledSprites.isEmpty()) {
595 sprite = mLocked.recycledSprites.top();
596 mLocked.recycledSprites.pop();
597 } else {
598 sprite = mSpriteController->createSprite();
599 }
600
601 // Return the new spot.
602 Spot* spot = new Spot(id, sprite);
603 mLocked.spots.push(spot);
604 return spot;
605}
606
607PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
608 for (size_t i = 0; i < mLocked.spots.size(); i++) {
609 Spot* spot = mLocked.spots.itemAt(i);
610 if (spot->id == Spot::INVALID_ID) {
611 mLocked.spots.removeAt(i);
612 return spot;
613 }
614 }
615 return NULL;
616}
617
618void PointerController::releaseSpotLocked(Spot* spot) {
619 spot->sprite->clearIcon();
620
621 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
622 mLocked.recycledSprites.push(spot->sprite);
623 }
624
625 delete spot;
626}
627
628void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
629 if (spot->id != Spot::INVALID_ID) {
630 spot->id = Spot::INVALID_ID;
631 startAnimationLocked();
632 }
633}
634
635void PointerController::fadeOutAndReleaseAllSpotsLocked() {
636 for (size_t i = 0; i < mLocked.spots.size(); i++) {
637 Spot* spot = mLocked.spots.itemAt(i);
638 fadeOutAndReleaseSpotLocked(spot);
639 }
640}
641
642void PointerController::loadResources() {
643 mPolicy->loadPointerResources(&mResources);
644}
645
646
647// --- PointerController::Spot ---
648
649void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
650 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
651 sprite->setAlpha(alpha);
652 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
653 sprite->setPosition(x, y);
654
655 this->x = x;
656 this->y = y;
657
658 if (icon != lastIcon) {
659 lastIcon = icon;
660 if (icon) {
661 sprite->setIcon(*icon);
662 sprite->setVisible(true);
663 } else {
664 sprite->setVisible(false);
665 }
666 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800667}
668
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800669} // namespace android