PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/input/PointerController.cpp

https://github.com/android/platform_frameworks_base
C++ | 217 lines | 144 code | 40 blank | 33 comment | 19 complexity | 3f62c6fdb72b56ec80856b1b3e031606 MD5 | raw file
Possible License(s): Apache-2.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 "MouseCursorController.h"
  22. #include "PointerControllerContext.h"
  23. #include "TouchSpotController.h"
  24. #include <log/log.h>
  25. #include <SkBitmap.h>
  26. #include <SkBlendMode.h>
  27. #include <SkCanvas.h>
  28. #include <SkColor.h>
  29. #include <SkPaint.h>
  30. namespace android {
  31. // --- PointerController ---
  32. std::shared_ptr<PointerController> PointerController::create(
  33. const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
  34. const sp<SpriteController>& spriteController) {
  35. // using 'new' to access non-public constructor
  36. std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>(
  37. new PointerController(policy, looper, spriteController));
  38. /*
  39. * Now we need to hook up the constructed PointerController object to its callbacks.
  40. *
  41. * This must be executed after the constructor but before any other methods on PointerController
  42. * in order to ensure that the fully constructed object is visible on the Looper thread, since
  43. * that may be a different thread than where the PointerController is initially constructed.
  44. *
  45. * Unfortunately, this cannot be done as part of the constructor since we need to hand out
  46. * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
  47. */
  48. controller->mContext.setHandlerController(controller);
  49. controller->mContext.setCallbackController(controller);
  50. return controller;
  51. }
  52. PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
  53. const sp<Looper>& looper,
  54. const sp<SpriteController>& spriteController)
  55. : mContext(policy, looper, spriteController, *this), mCursorController(mContext) {
  56. std::scoped_lock lock(mLock);
  57. mLocked.presentation = Presentation::SPOT;
  58. }
  59. bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
  60. float* outMaxY) const {
  61. return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
  62. }
  63. void PointerController::move(float deltaX, float deltaY) {
  64. mCursorController.move(deltaX, deltaY);
  65. }
  66. void PointerController::setButtonState(int32_t buttonState) {
  67. mCursorController.setButtonState(buttonState);
  68. }
  69. int32_t PointerController::getButtonState() const {
  70. return mCursorController.getButtonState();
  71. }
  72. void PointerController::setPosition(float x, float y) {
  73. std::scoped_lock lock(mLock);
  74. mCursorController.setPosition(x, y);
  75. }
  76. void PointerController::getPosition(float* outX, float* outY) const {
  77. mCursorController.getPosition(outX, outY);
  78. }
  79. int32_t PointerController::getDisplayId() const {
  80. return mCursorController.getDisplayId();
  81. }
  82. void PointerController::fade(Transition transition) {
  83. std::scoped_lock lock(mLock);
  84. mCursorController.fade(transition);
  85. }
  86. void PointerController::unfade(Transition transition) {
  87. std::scoped_lock lock(mLock);
  88. mCursorController.unfade(transition);
  89. }
  90. void PointerController::setPresentation(Presentation presentation) {
  91. std::scoped_lock lock(mLock);
  92. if (mLocked.presentation == presentation) {
  93. return;
  94. }
  95. mLocked.presentation = presentation;
  96. if (!mCursorController.isViewportValid()) {
  97. return;
  98. }
  99. if (presentation == Presentation::POINTER) {
  100. mCursorController.getAdditionalMouseResources();
  101. clearSpotsLocked();
  102. }
  103. }
  104. void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
  105. BitSet32 spotIdBits, int32_t displayId) {
  106. std::scoped_lock lock(mLock);
  107. auto it = mLocked.spotControllers.find(displayId);
  108. if (it == mLocked.spotControllers.end()) {
  109. mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
  110. }
  111. mLocked.spotControllers.at(displayId).setSpots(spotCoords, spotIdToIndex, spotIdBits);
  112. }
  113. void PointerController::clearSpots() {
  114. std::scoped_lock lock(mLock);
  115. clearSpotsLocked();
  116. }
  117. void PointerController::clearSpotsLocked() REQUIRES(mLock) {
  118. for (auto& [displayID, spotController] : mLocked.spotControllers) {
  119. spotController.clearSpots();
  120. }
  121. }
  122. void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
  123. mContext.setInactivityTimeout(inactivityTimeout);
  124. }
  125. void PointerController::reloadPointerResources() {
  126. std::scoped_lock lock(mLock);
  127. for (auto& [displayID, spotController] : mLocked.spotControllers) {
  128. spotController.reloadSpotResources();
  129. }
  130. if (mCursorController.resourcesLoaded()) {
  131. bool getAdditionalMouseResources = false;
  132. if (mLocked.presentation == PointerController::Presentation::POINTER) {
  133. getAdditionalMouseResources = true;
  134. }
  135. mCursorController.reloadPointerResources(getAdditionalMouseResources);
  136. }
  137. }
  138. void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
  139. std::scoped_lock lock(mLock);
  140. bool getAdditionalMouseResources = false;
  141. if (mLocked.presentation == PointerController::Presentation::POINTER) {
  142. getAdditionalMouseResources = true;
  143. }
  144. mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
  145. }
  146. void PointerController::updatePointerIcon(int32_t iconId) {
  147. std::scoped_lock lock(mLock);
  148. mCursorController.updatePointerIcon(iconId);
  149. }
  150. void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
  151. std::scoped_lock lock(mLock);
  152. mCursorController.setCustomPointerIcon(icon);
  153. }
  154. void PointerController::doInactivityTimeout() {
  155. fade(Transition::GRADUAL);
  156. }
  157. void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
  158. std::unordered_set<int32_t> displayIdSet;
  159. for (DisplayViewport viewport : viewports) {
  160. displayIdSet.insert(viewport.displayId);
  161. }
  162. std::scoped_lock lock(mLock);
  163. for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
  164. int32_t displayID = it->first;
  165. if (!displayIdSet.count(displayID)) {
  166. /*
  167. * Ensures that an in-progress animation won't dereference
  168. * a null pointer to TouchSpotController.
  169. */
  170. mContext.removeAnimationCallback(displayID);
  171. it = mLocked.spotControllers.erase(it);
  172. } else {
  173. ++it;
  174. }
  175. }
  176. }
  177. } // namespace android