PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/services/input/PointerController.cpp

https://bitbucket.org/iainh/projectzzz-platform-frameworks-base
C++ | 601 lines | 447 code | 112 blank | 42 comment | 87 complexity | 14e06bcdc93bfe79e66c5dd9052be48c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC0-1.0
  1. /*
  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. #define LOG_TAG "PointerController"
  17. //#define LOG_NDEBUG 0
  18. // Log debug messages about pointer updates
  19. #define DEBUG_POINTER_UPDATES 0
  20. #include "PointerController.h"
  21. #include <cutils/log.h>
  22. #include <SkBitmap.h>
  23. #include <SkCanvas.h>
  24. #include <SkColor.h>
  25. #include <SkPaint.h>
  26. #include <SkXfermode.h>
  27. namespace android {
  28. // --- PointerController ---
  29. // Time to wait before starting the fade when the pointer is inactive.
  30. static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
  31. static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
  32. // Time to wait between animation frames.
  33. static const nsecs_t ANIMATION_FRAME_INTERVAL = 1000000000LL / 60;
  34. // Time to spend fading out the spot completely.
  35. static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
  36. // Time to spend fading out the pointer completely.
  37. static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
  38. // --- PointerController ---
  39. PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
  40. const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
  41. mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
  42. mHandler = new WeakMessageHandler(this);
  43. AutoMutex _l(mLock);
  44. mLocked.animationPending = false;
  45. mLocked.displayWidth = -1;
  46. mLocked.displayHeight = -1;
  47. mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
  48. mLocked.presentation = PRESENTATION_POINTER;
  49. mLocked.presentationChanged = false;
  50. mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
  51. mLocked.pointerFadeDirection = 0;
  52. mLocked.pointerX = 0;
  53. mLocked.pointerY = 0;
  54. mLocked.pointerAlpha = 0.0f; // pointer is initially faded
  55. mLocked.pointerSprite = mSpriteController->createSprite();
  56. mLocked.pointerIconChanged = false;
  57. mLocked.buttonState = 0;
  58. loadResources();
  59. }
  60. PointerController::~PointerController() {
  61. mLooper->removeMessages(mHandler);
  62. AutoMutex _l(mLock);
  63. mLocked.pointerSprite.clear();
  64. for (size_t i = 0; i < mLocked.spots.size(); i++) {
  65. delete mLocked.spots.itemAt(i);
  66. }
  67. mLocked.spots.clear();
  68. mLocked.recycledSprites.clear();
  69. }
  70. bool PointerController::getBounds(float* outMinX, float* outMinY,
  71. float* outMaxX, float* outMaxY) const {
  72. AutoMutex _l(mLock);
  73. return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
  74. }
  75. bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
  76. float* outMaxX, float* outMaxY) const {
  77. if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
  78. return false;
  79. }
  80. *outMinX = 0;
  81. *outMinY = 0;
  82. switch (mLocked.displayOrientation) {
  83. case DISPLAY_ORIENTATION_90:
  84. case DISPLAY_ORIENTATION_270:
  85. *outMaxX = mLocked.displayHeight - 1;
  86. *outMaxY = mLocked.displayWidth - 1;
  87. break;
  88. default:
  89. *outMaxX = mLocked.displayWidth - 1;
  90. *outMaxY = mLocked.displayHeight - 1;
  91. break;
  92. }
  93. return true;
  94. }
  95. void PointerController::move(float deltaX, float deltaY) {
  96. #if DEBUG_POINTER_UPDATES
  97. ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
  98. #endif
  99. if (deltaX == 0.0f && deltaY == 0.0f) {
  100. return;
  101. }
  102. AutoMutex _l(mLock);
  103. setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
  104. }
  105. void PointerController::setButtonState(int32_t buttonState) {
  106. #if DEBUG_POINTER_UPDATES
  107. ALOGD("Set button state 0x%08x", buttonState);
  108. #endif
  109. AutoMutex _l(mLock);
  110. if (mLocked.buttonState != buttonState) {
  111. mLocked.buttonState = buttonState;
  112. }
  113. }
  114. int32_t PointerController::getButtonState() const {
  115. AutoMutex _l(mLock);
  116. return mLocked.buttonState;
  117. }
  118. void PointerController::setPosition(float x, float y) {
  119. #if DEBUG_POINTER_UPDATES
  120. ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
  121. #endif
  122. AutoMutex _l(mLock);
  123. setPositionLocked(x, y);
  124. }
  125. void PointerController::setPositionLocked(float x, float y) {
  126. float minX, minY, maxX, maxY;
  127. if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
  128. if (x <= minX) {
  129. mLocked.pointerX = minX;
  130. } else if (x >= maxX) {
  131. mLocked.pointerX = maxX;
  132. } else {
  133. mLocked.pointerX = x;
  134. }
  135. if (y <= minY) {
  136. mLocked.pointerY = minY;
  137. } else if (y >= maxY) {
  138. mLocked.pointerY = maxY;
  139. } else {
  140. mLocked.pointerY = y;
  141. }
  142. updatePointerLocked();
  143. }
  144. }
  145. void PointerController::getPosition(float* outX, float* outY) const {
  146. AutoMutex _l(mLock);
  147. *outX = mLocked.pointerX;
  148. *outY = mLocked.pointerY;
  149. }
  150. void PointerController::fade(Transition transition) {
  151. AutoMutex _l(mLock);
  152. // Remove the inactivity timeout, since we are fading now.
  153. removeInactivityTimeoutLocked();
  154. // Start fading.
  155. if (transition == TRANSITION_IMMEDIATE) {
  156. mLocked.pointerFadeDirection = 0;
  157. mLocked.pointerAlpha = 0.0f;
  158. updatePointerLocked();
  159. } else {
  160. mLocked.pointerFadeDirection = -1;
  161. startAnimationLocked();
  162. }
  163. }
  164. void PointerController::unfade(Transition transition) {
  165. AutoMutex _l(mLock);
  166. // Always reset the inactivity timer.
  167. resetInactivityTimeoutLocked();
  168. // Start unfading.
  169. if (transition == TRANSITION_IMMEDIATE) {
  170. mLocked.pointerFadeDirection = 0;
  171. mLocked.pointerAlpha = 1.0f;
  172. updatePointerLocked();
  173. } else {
  174. mLocked.pointerFadeDirection = 1;
  175. startAnimationLocked();
  176. }
  177. }
  178. void PointerController::setPresentation(Presentation presentation) {
  179. AutoMutex _l(mLock);
  180. if (mLocked.presentation != presentation) {
  181. mLocked.presentation = presentation;
  182. mLocked.presentationChanged = true;
  183. if (presentation != PRESENTATION_SPOT) {
  184. fadeOutAndReleaseAllSpotsLocked();
  185. }
  186. updatePointerLocked();
  187. }
  188. }
  189. void PointerController::setSpots(const PointerCoords* spotCoords,
  190. const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
  191. #if DEBUG_POINTER_UPDATES
  192. ALOGD("setSpots: idBits=%08x", spotIdBits.value);
  193. for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
  194. uint32_t id = idBits.firstMarkedBit();
  195. idBits.clearBit(id);
  196. const PointerCoords& c = spotCoords[spotIdToIndex[id]];
  197. ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
  198. c.getAxisValue(AMOTION_EVENT_AXIS_X),
  199. c.getAxisValue(AMOTION_EVENT_AXIS_Y),
  200. c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
  201. }
  202. #endif
  203. AutoMutex _l(mLock);
  204. mSpriteController->openTransaction();
  205. // Add or move spots for fingers that are down.
  206. for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
  207. uint32_t id = idBits.clearFirstMarkedBit();
  208. const PointerCoords& c = spotCoords[spotIdToIndex[id]];
  209. const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
  210. ? mResources.spotTouch : mResources.spotHover;
  211. float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
  212. float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
  213. Spot* spot = getSpotLocked(id);
  214. if (!spot) {
  215. spot = createAndAddSpotLocked(id);
  216. }
  217. spot->updateSprite(&icon, x, y);
  218. }
  219. // Remove spots for fingers that went up.
  220. for (size_t i = 0; i < mLocked.spots.size(); i++) {
  221. Spot* spot = mLocked.spots.itemAt(i);
  222. if (spot->id != Spot::INVALID_ID
  223. && !spotIdBits.hasBit(spot->id)) {
  224. fadeOutAndReleaseSpotLocked(spot);
  225. }
  226. }
  227. mSpriteController->closeTransaction();
  228. }
  229. void PointerController::clearSpots() {
  230. #if DEBUG_POINTER_UPDATES
  231. ALOGD("clearSpots");
  232. #endif
  233. AutoMutex _l(mLock);
  234. fadeOutAndReleaseAllSpotsLocked();
  235. }
  236. void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
  237. AutoMutex _l(mLock);
  238. if (mLocked.inactivityTimeout != inactivityTimeout) {
  239. mLocked.inactivityTimeout = inactivityTimeout;
  240. resetInactivityTimeoutLocked();
  241. }
  242. }
  243. void PointerController::setDisplaySize(int32_t width, int32_t height) {
  244. AutoMutex _l(mLock);
  245. if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
  246. mLocked.displayWidth = width;
  247. mLocked.displayHeight = height;
  248. float minX, minY, maxX, maxY;
  249. if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
  250. mLocked.pointerX = (minX + maxX) * 0.5f;
  251. mLocked.pointerY = (minY + maxY) * 0.5f;
  252. } else {
  253. mLocked.pointerX = 0;
  254. mLocked.pointerY = 0;
  255. }
  256. fadeOutAndReleaseAllSpotsLocked();
  257. updatePointerLocked();
  258. }
  259. }
  260. void PointerController::setDisplayOrientation(int32_t orientation) {
  261. AutoMutex _l(mLock);
  262. if (mLocked.displayOrientation != orientation) {
  263. // Apply offsets to convert from the pixel top-left corner position to the pixel center.
  264. // This creates an invariant frame of reference that we can easily rotate when
  265. // taking into account that the pointer may be located at fractional pixel offsets.
  266. float x = mLocked.pointerX + 0.5f;
  267. float y = mLocked.pointerY + 0.5f;
  268. float temp;
  269. // Undo the previous rotation.
  270. switch (mLocked.displayOrientation) {
  271. case DISPLAY_ORIENTATION_90:
  272. temp = x;
  273. x = mLocked.displayWidth - y;
  274. y = temp;
  275. break;
  276. case DISPLAY_ORIENTATION_180:
  277. x = mLocked.displayWidth - x;
  278. y = mLocked.displayHeight - y;
  279. break;
  280. case DISPLAY_ORIENTATION_270:
  281. temp = x;
  282. x = y;
  283. y = mLocked.displayHeight - temp;
  284. break;
  285. }
  286. // Perform the new rotation.
  287. switch (orientation) {
  288. case DISPLAY_ORIENTATION_90:
  289. temp = x;
  290. x = y;
  291. y = mLocked.displayWidth - temp;
  292. break;
  293. case DISPLAY_ORIENTATION_180:
  294. x = mLocked.displayWidth - x;
  295. y = mLocked.displayHeight - y;
  296. break;
  297. case DISPLAY_ORIENTATION_270:
  298. temp = x;
  299. x = mLocked.displayHeight - y;
  300. y = temp;
  301. break;
  302. }
  303. // Apply offsets to convert from the pixel center to the pixel top-left corner position
  304. // and save the results.
  305. mLocked.pointerX = x - 0.5f;
  306. mLocked.pointerY = y - 0.5f;
  307. mLocked.displayOrientation = orientation;
  308. updatePointerLocked();
  309. }
  310. }
  311. void PointerController::setPointerIcon(const SpriteIcon& icon) {
  312. AutoMutex _l(mLock);
  313. mLocked.pointerIcon = icon.copy();
  314. mLocked.pointerIconChanged = true;
  315. updatePointerLocked();
  316. }
  317. void PointerController::handleMessage(const Message& message) {
  318. switch (message.what) {
  319. case MSG_ANIMATE:
  320. doAnimate();
  321. break;
  322. case MSG_INACTIVITY_TIMEOUT:
  323. doInactivityTimeout();
  324. break;
  325. }
  326. }
  327. void PointerController::doAnimate() {
  328. AutoMutex _l(mLock);
  329. bool keepAnimating = false;
  330. mLocked.animationPending = false;
  331. nsecs_t frameDelay = systemTime(SYSTEM_TIME_MONOTONIC) - mLocked.animationTime;
  332. // Animate pointer fade.
  333. if (mLocked.pointerFadeDirection < 0) {
  334. mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
  335. if (mLocked.pointerAlpha <= 0.0f) {
  336. mLocked.pointerAlpha = 0.0f;
  337. mLocked.pointerFadeDirection = 0;
  338. } else {
  339. keepAnimating = true;
  340. }
  341. updatePointerLocked();
  342. } else if (mLocked.pointerFadeDirection > 0) {
  343. mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
  344. if (mLocked.pointerAlpha >= 1.0f) {
  345. mLocked.pointerAlpha = 1.0f;
  346. mLocked.pointerFadeDirection = 0;
  347. } else {
  348. keepAnimating = true;
  349. }
  350. updatePointerLocked();
  351. }
  352. // Animate spots that are fading out and being removed.
  353. for (size_t i = 0; i < mLocked.spots.size(); i++) {
  354. Spot* spot = mLocked.spots.itemAt(i);
  355. if (spot->id == Spot::INVALID_ID) {
  356. spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
  357. if (spot->alpha <= 0) {
  358. mLocked.spots.removeAt(i--);
  359. releaseSpotLocked(spot);
  360. } else {
  361. spot->sprite->setAlpha(spot->alpha);
  362. keepAnimating = true;
  363. }
  364. }
  365. }
  366. if (keepAnimating) {
  367. startAnimationLocked();
  368. }
  369. }
  370. void PointerController::doInactivityTimeout() {
  371. fade(TRANSITION_GRADUAL);
  372. }
  373. void PointerController::startAnimationLocked() {
  374. if (!mLocked.animationPending) {
  375. mLocked.animationPending = true;
  376. mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
  377. mLooper->sendMessageDelayed(ANIMATION_FRAME_INTERVAL, mHandler, Message(MSG_ANIMATE));
  378. }
  379. }
  380. void PointerController::resetInactivityTimeoutLocked() {
  381. mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
  382. nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
  383. ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
  384. mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
  385. }
  386. void PointerController::removeInactivityTimeoutLocked() {
  387. mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
  388. }
  389. void PointerController::updatePointerLocked() {
  390. mSpriteController->openTransaction();
  391. mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
  392. mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
  393. if (mLocked.pointerAlpha > 0) {
  394. mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
  395. mLocked.pointerSprite->setVisible(true);
  396. } else {
  397. mLocked.pointerSprite->setVisible(false);
  398. }
  399. if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
  400. mLocked.pointerSprite->setIcon(mLocked.presentation == PRESENTATION_POINTER
  401. ? mLocked.pointerIcon : mResources.spotAnchor);
  402. mLocked.pointerIconChanged = false;
  403. mLocked.presentationChanged = false;
  404. }
  405. mSpriteController->closeTransaction();
  406. }
  407. PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
  408. for (size_t i = 0; i < mLocked.spots.size(); i++) {
  409. Spot* spot = mLocked.spots.itemAt(i);
  410. if (spot->id == id) {
  411. return spot;
  412. }
  413. }
  414. return NULL;
  415. }
  416. PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
  417. // Remove spots until we have fewer than MAX_SPOTS remaining.
  418. while (mLocked.spots.size() >= MAX_SPOTS) {
  419. Spot* spot = removeFirstFadingSpotLocked();
  420. if (!spot) {
  421. spot = mLocked.spots.itemAt(0);
  422. mLocked.spots.removeAt(0);
  423. }
  424. releaseSpotLocked(spot);
  425. }
  426. // Obtain a sprite from the recycled pool.
  427. sp<Sprite> sprite;
  428. if (! mLocked.recycledSprites.isEmpty()) {
  429. sprite = mLocked.recycledSprites.top();
  430. mLocked.recycledSprites.pop();
  431. } else {
  432. sprite = mSpriteController->createSprite();
  433. }
  434. // Return the new spot.
  435. Spot* spot = new Spot(id, sprite);
  436. mLocked.spots.push(spot);
  437. return spot;
  438. }
  439. PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
  440. for (size_t i = 0; i < mLocked.spots.size(); i++) {
  441. Spot* spot = mLocked.spots.itemAt(i);
  442. if (spot->id == Spot::INVALID_ID) {
  443. mLocked.spots.removeAt(i);
  444. return spot;
  445. }
  446. }
  447. return NULL;
  448. }
  449. void PointerController::releaseSpotLocked(Spot* spot) {
  450. spot->sprite->clearIcon();
  451. if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
  452. mLocked.recycledSprites.push(spot->sprite);
  453. }
  454. delete spot;
  455. }
  456. void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
  457. if (spot->id != Spot::INVALID_ID) {
  458. spot->id = Spot::INVALID_ID;
  459. startAnimationLocked();
  460. }
  461. }
  462. void PointerController::fadeOutAndReleaseAllSpotsLocked() {
  463. for (size_t i = 0; i < mLocked.spots.size(); i++) {
  464. Spot* spot = mLocked.spots.itemAt(i);
  465. fadeOutAndReleaseSpotLocked(spot);
  466. }
  467. }
  468. void PointerController::loadResources() {
  469. mPolicy->loadPointerResources(&mResources);
  470. }
  471. // --- PointerController::Spot ---
  472. void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
  473. sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
  474. sprite->setAlpha(alpha);
  475. sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
  476. sprite->setPosition(x, y);
  477. this->x = x;
  478. this->y = y;
  479. if (icon != lastIcon) {
  480. lastIcon = icon;
  481. if (icon) {
  482. sprite->setIcon(*icon);
  483. sprite->setVisible(true);
  484. } else {
  485. sprite->setVisible(false);
  486. }
  487. }
  488. }
  489. } // namespace android