blob: 14c96cefd462caaa97b96a8afc11428a3e133075 [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"
Liam Harringtonc782be62020-07-17 19:48:24 +000024#include "MouseCursorController.h"
25#include "PointerControllerContext.h"
26#include "TouchSpotController.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080027
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070028#include <log/log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080029
Liam Harringtonc782be62020-07-17 19:48:24 +000030#include <SkBitmap.h>
31#include <SkBlendMode.h>
32#include <SkCanvas.h>
33#include <SkColor.h>
34#include <SkPaint.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080035
36namespace android {
37
38// --- PointerController ---
39
Michael Wrighta0bc6b12020-06-26 20:25:34 +010040std::shared_ptr<PointerController> PointerController::create(
41 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
42 const sp<SpriteController>& spriteController) {
Liam Harringtonc782be62020-07-17 19:48:24 +000043 // using 'new' to access non-public constructor
Michael Wrighta0bc6b12020-06-26 20:25:34 +010044 std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>(
45 new PointerController(policy, looper, spriteController));
Jeff Brown2352b972011-04-12 22:39:53 -070046
Michael Wrighta0bc6b12020-06-26 20:25:34 +010047 /*
48 * Now we need to hook up the constructed PointerController object to its callbacks.
49 *
50 * This must be executed after the constructor but before any other methods on PointerController
51 * in order to ensure that the fully constructed object is visible on the Looper thread, since
52 * that may be a different thread than where the PointerController is initially constructed.
53 *
54 * Unfortunately, this cannot be done as part of the constructor since we need to hand out
55 * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr.
56 */
Jeff Brown5541de92011-04-11 11:54:25 -070057
Liam Harringtonc782be62020-07-17 19:48:24 +000058 controller->mContext.setHandlerController(controller);
59 controller->mContext.setCallbackController(controller);
60 controller->mContext.initializeDisplayEventReceiver();
Michael Wrighta0bc6b12020-06-26 20:25:34 +010061 return controller;
62}
Jun Mukaic0c0ac32015-10-27 10:09:21 -070063
Michael Wrighta0bc6b12020-06-26 20:25:34 +010064PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
65 const sp<Looper>& looper,
66 const sp<SpriteController>& spriteController)
Liam Harringtonc782be62020-07-17 19:48:24 +000067 : mContext(policy, looper, spriteController, *this), mCursorController(mContext) {
68 std::scoped_lock lock(mLock);
69 mLocked.presentation = Presentation::SPOT;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080070}
71
Liam Harringtonc782be62020-07-17 19:48:24 +000072bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
73 float* outMaxY) const {
74 return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080075}
76
77void PointerController::move(float deltaX, float deltaY) {
Liam Harringtonc782be62020-07-17 19:48:24 +000078 mCursorController.move(deltaX, deltaY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080079}
80
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070081void PointerController::setButtonState(int32_t buttonState) {
Liam Harringtonc782be62020-07-17 19:48:24 +000082 mCursorController.setButtonState(buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080083}
84
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070085int32_t PointerController::getButtonState() const {
Liam Harringtonc782be62020-07-17 19:48:24 +000086 return mCursorController.getButtonState();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080087}
88
89void PointerController::setPosition(float x, float y) {
Liam Harringtonc782be62020-07-17 19:48:24 +000090 std::scoped_lock lock(mLock);
91 mCursorController.setPosition(x, y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080092}
93
94void PointerController::getPosition(float* outX, float* outY) const {
Liam Harringtonc782be62020-07-17 19:48:24 +000095 mCursorController.getPosition(outX, outY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080096}
97
Arthur Hungb9b32002018-12-18 17:39:43 +080098int32_t PointerController::getDisplayId() const {
Liam Harringtonc782be62020-07-17 19:48:24 +000099 return mCursorController.getDisplayId();
Arthur Hungb9b32002018-12-18 17:39:43 +0800100}
101
Jeff Brown538881e2011-05-25 18:23:38 -0700102void PointerController::fade(Transition transition) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000103 std::scoped_lock lock(mLock);
104 mCursorController.fade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800105}
106
Jeff Brown538881e2011-05-25 18:23:38 -0700107void PointerController::unfade(Transition transition) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000108 std::scoped_lock lock(mLock);
109 mCursorController.unfade(transition);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800110}
111
Jeff Brown2352b972011-04-12 22:39:53 -0700112void PointerController::setPresentation(Presentation presentation) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000113 std::scoped_lock lock(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800114
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800115 if (mLocked.presentation == presentation) {
116 return;
Jun Mukai1db53972015-09-11 18:08:31 -0700117 }
118
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800119 mLocked.presentation = presentation;
Jeff Brown2352b972011-04-12 22:39:53 -0700120
Liam Harringtonc782be62020-07-17 19:48:24 +0000121 if (!mCursorController.isViewportValid()) {
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800122 return;
123 }
124
Michael Wright6853fe62020-07-02 00:01:38 +0100125 if (presentation == Presentation::POINTER) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000126 mCursorController.getAdditionalMouseResources();
127 clearSpotsLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800128 }
129}
130
Liam Harringtonc782be62020-07-17 19:48:24 +0000131void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
132 BitSet32 spotIdBits, int32_t displayId) {
133 std::scoped_lock lock(mLock);
134 auto it = mLocked.spotControllers.find(displayId);
135 if (it == mLocked.spotControllers.end()) {
136 mLocked.spotControllers.try_emplace(displayId, displayId, mContext);
Jeff Brown2352b972011-04-12 22:39:53 -0700137 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000138 mLocked.spotControllers.at(displayId).setSpots(spotCoords, spotIdToIndex, spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700139}
140
141void PointerController::clearSpots() {
Liam Harringtonc782be62020-07-17 19:48:24 +0000142 std::scoped_lock lock(mLock);
143 clearSpotsLocked();
144}
Jeff Brown2352b972011-04-12 22:39:53 -0700145
Liam Harringtonc782be62020-07-17 19:48:24 +0000146void PointerController::clearSpotsLocked() REQUIRES(mLock) {
147 for (auto& [displayID, spotController] : mLocked.spotControllers) {
148 spotController.clearSpots();
Prabir Pradhanca7d7232020-01-31 17:42:34 -0800149 }
Jeff Brown2352b972011-04-12 22:39:53 -0700150}
151
152void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000153 mContext.setInactivityTimeout(inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800154}
155
Jun Mukai19a56012015-11-24 11:25:52 -0800156void PointerController::reloadPointerResources() {
Liam Harringtonc782be62020-07-17 19:48:24 +0000157 std::scoped_lock lock(mLock);
Jun Mukai19a56012015-11-24 11:25:52 -0800158
Liam Harringtonc782be62020-07-17 19:48:24 +0000159 for (auto& [displayID, spotController] : mLocked.spotControllers) {
160 spotController.reloadSpotResources();
161 }
Jun Mukai19a56012015-11-24 11:25:52 -0800162
Liam Harringtonc782be62020-07-17 19:48:24 +0000163 if (mCursorController.resourcesLoaded()) {
164 bool getAdditionalMouseResources = false;
165 if (mLocked.presentation == PointerController::Presentation::POINTER) {
166 getAdditionalMouseResources = true;
167 }
168 mCursorController.reloadPointerResources(getAdditionalMouseResources);
Arthur Hungb9b32002018-12-18 17:39:43 +0800169 }
170}
171
172void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000173 std::scoped_lock lock(mLock);
174
175 bool getAdditionalMouseResources = false;
176 if (mLocked.presentation == PointerController::Presentation::POINTER) {
177 getAdditionalMouseResources = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700178 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000179 mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800180}
181
Michael Wrighte051f6f2016-05-13 17:44:16 +0100182void PointerController::updatePointerIcon(int32_t iconId) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000183 std::scoped_lock lock(mLock);
184 mCursorController.updatePointerIcon(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700185}
186
Jun Mukaid4eaef72015-10-30 15:54:33 -0700187void PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000188 std::scoped_lock lock(mLock);
189 mCursorController.setCustomPointerIcon(icon);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700190}
191
192void PointerController::doAnimate(nsecs_t timestamp) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000193 std::scoped_lock lock(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800194
Liam Harringtonc782be62020-07-17 19:48:24 +0000195 mContext.setAnimationPending(false);
Jun Mukai808196f2015-10-28 16:46:44 -0700196
Liam Harringtonc782be62020-07-17 19:48:24 +0000197 bool keepFading = false;
198 keepFading = mCursorController.doFadingAnimation(timestamp, keepFading);
199
200 for (auto& [displayID, spotController] : mLocked.spotControllers) {
201 keepFading = spotController.doFadingAnimation(timestamp, keepFading);
202 }
203
204 bool keepBitmapFlipping = mCursorController.doBitmapAnimation(timestamp);
Jun Mukai808196f2015-10-28 16:46:44 -0700205 if (keepFading || keepBitmapFlipping) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000206 mContext.startAnimation();
Jun Mukai808196f2015-10-28 16:46:44 -0700207 }
208}
209
Jeff Brown2352b972011-04-12 22:39:53 -0700210void PointerController::doInactivityTimeout() {
Michael Wright6853fe62020-07-02 00:01:38 +0100211 fade(Transition::GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800212}
213
Liam Harringtonc782be62020-07-17 19:48:24 +0000214void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) {
215 std::unordered_set<int32_t> displayIdSet;
216 for (DisplayViewport viewport : viewports) {
217 displayIdSet.insert(viewport.displayId);
Arthur Hungb9b32002018-12-18 17:39:43 +0800218 }
219
Liam Harringtonc782be62020-07-17 19:48:24 +0000220 std::scoped_lock lock(mLock);
221 for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) {
222 int32_t displayID = it->first;
223 if (!displayIdSet.count(displayID)) {
224 it = mLocked.spotControllers.erase(it);
Jun Mukai1db53972015-09-11 18:08:31 -0700225 } else {
Liam Harringtonc782be62020-07-17 19:48:24 +0000226 ++it;
Jeff Brown2352b972011-04-12 22:39:53 -0700227 }
228 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800229}
230
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800231} // namespace android