• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 "MouseCursorController"
18 //#define LOG_NDEBUG 0
19 
20 // Log debug messages about pointer updates
21 #define DEBUG_MOUSE_CURSOR_UPDATES 0
22 
23 #include "MouseCursorController.h"
24 
25 #include <log/log.h>
26 
27 #include <SkBitmap.h>
28 #include <SkBlendMode.h>
29 #include <SkCanvas.h>
30 #include <SkColor.h>
31 #include <SkPaint.h>
32 
33 namespace {
34 // Time to spend fading out the pointer completely.
35 const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
36 } // namespace
37 
38 namespace android {
39 
40 // --- MouseCursorController ---
41 
MouseCursorController(PointerControllerContext & context)42 MouseCursorController::MouseCursorController(PointerControllerContext& context)
43       : mContext(context) {
44     std::scoped_lock lock(mLock);
45 
46     mLocked.animationFrameIndex = 0;
47     mLocked.lastFrameUpdatedTime = 0;
48 
49     mLocked.pointerFadeDirection = 0;
50     mLocked.pointerX = 0;
51     mLocked.pointerY = 0;
52     mLocked.pointerAlpha = 0.0f; // pointer is initially faded
53     mLocked.pointerSprite = mContext.getSpriteController()->createSprite();
54     mLocked.updatePointerIcon = false;
55     mLocked.requestedPointerType = mContext.getPolicy()->getDefaultPointerIconId();
56 
57     mLocked.resourcesLoaded = false;
58 
59     mLocked.buttonState = 0;
60 }
61 
~MouseCursorController()62 MouseCursorController::~MouseCursorController() {
63     std::scoped_lock lock(mLock);
64 
65     mLocked.pointerSprite.clear();
66 }
67 
getBounds(float * outMinX,float * outMinY,float * outMaxX,float * outMaxY) const68 bool MouseCursorController::getBounds(float* outMinX, float* outMinY, float* outMaxX,
69                                       float* outMaxY) const {
70     std::scoped_lock lock(mLock);
71 
72     return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
73 }
74 
getBoundsLocked(float * outMinX,float * outMinY,float * outMaxX,float * outMaxY) const75 bool MouseCursorController::getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX,
76                                             float* outMaxY) const REQUIRES(mLock) {
77     if (!mLocked.viewport.isValid()) {
78         return false;
79     }
80 
81     *outMinX = mLocked.viewport.logicalLeft;
82     *outMinY = mLocked.viewport.logicalTop;
83     *outMaxX = mLocked.viewport.logicalRight - 1;
84     *outMaxY = mLocked.viewport.logicalBottom - 1;
85     return true;
86 }
87 
move(float deltaX,float deltaY)88 void MouseCursorController::move(float deltaX, float deltaY) {
89 #if DEBUG_MOUSE_CURSOR_UPDATES
90     ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
91 #endif
92     if (deltaX == 0.0f && deltaY == 0.0f) {
93         return;
94     }
95 
96     std::scoped_lock lock(mLock);
97 
98     setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
99 }
100 
setButtonState(int32_t buttonState)101 void MouseCursorController::setButtonState(int32_t buttonState) {
102 #if DEBUG_MOUSE_CURSOR_UPDATES
103     ALOGD("Set button state 0x%08x", buttonState);
104 #endif
105     std::scoped_lock lock(mLock);
106 
107     if (mLocked.buttonState != buttonState) {
108         mLocked.buttonState = buttonState;
109     }
110 }
111 
getButtonState() const112 int32_t MouseCursorController::getButtonState() const {
113     std::scoped_lock lock(mLock);
114     return mLocked.buttonState;
115 }
116 
setPosition(float x,float y)117 void MouseCursorController::setPosition(float x, float y) {
118 #if DEBUG_MOUSE_CURSOR_UPDATES
119     ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
120 #endif
121     std::scoped_lock lock(mLock);
122     setPositionLocked(x, y);
123 }
124 
setPositionLocked(float x,float y)125 void MouseCursorController::setPositionLocked(float x, float y) REQUIRES(mLock) {
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 
getPosition(float * outX,float * outY) const146 void MouseCursorController::getPosition(float* outX, float* outY) const {
147     std::scoped_lock lock(mLock);
148 
149     *outX = mLocked.pointerX;
150     *outY = mLocked.pointerY;
151 }
152 
getDisplayId() const153 int32_t MouseCursorController::getDisplayId() const {
154     std::scoped_lock lock(mLock);
155     return mLocked.viewport.displayId;
156 }
157 
fade(PointerControllerInterface::Transition transition)158 void MouseCursorController::fade(PointerControllerInterface::Transition transition) {
159     std::scoped_lock lock(mLock);
160 
161     // Remove the inactivity timeout, since we are fading now.
162     mContext.removeInactivityTimeout();
163 
164     // Start fading.
165     if (transition == PointerControllerInterface::Transition::IMMEDIATE) {
166         mLocked.pointerFadeDirection = 0;
167         mLocked.pointerAlpha = 0.0f;
168         updatePointerLocked();
169     } else {
170         mLocked.pointerFadeDirection = -1;
171         startAnimationLocked();
172     }
173 }
174 
unfade(PointerControllerInterface::Transition transition)175 void MouseCursorController::unfade(PointerControllerInterface::Transition transition) {
176     std::scoped_lock lock(mLock);
177 
178     // Always reset the inactivity timer.
179     mContext.resetInactivityTimeout();
180 
181     // Start unfading.
182     if (transition == PointerControllerInterface::Transition::IMMEDIATE) {
183         mLocked.pointerFadeDirection = 0;
184         mLocked.pointerAlpha = 1.0f;
185         updatePointerLocked();
186     } else {
187         mLocked.pointerFadeDirection = 1;
188         startAnimationLocked();
189     }
190 }
191 
reloadPointerResources(bool getAdditionalMouseResources)192 void MouseCursorController::reloadPointerResources(bool getAdditionalMouseResources) {
193     std::scoped_lock lock(mLock);
194 
195     loadResourcesLocked(getAdditionalMouseResources);
196     updatePointerLocked();
197 }
198 
199 /**
200  * The viewport values for deviceHeight and deviceWidth have already been adjusted for rotation,
201  * so here we are getting the dimensions in the original, unrotated orientation (orientation 0).
202  */
getNonRotatedSize(const DisplayViewport & viewport,int32_t & width,int32_t & height)203 static void getNonRotatedSize(const DisplayViewport& viewport, int32_t& width, int32_t& height) {
204     width = viewport.deviceWidth;
205     height = viewport.deviceHeight;
206 
207     if (viewport.orientation == DISPLAY_ORIENTATION_90 ||
208         viewport.orientation == DISPLAY_ORIENTATION_270) {
209         std::swap(width, height);
210     }
211 }
212 
setDisplayViewport(const DisplayViewport & viewport,bool getAdditionalMouseResources)213 void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport,
214                                                bool getAdditionalMouseResources) {
215     std::scoped_lock lock(mLock);
216 
217     if (viewport == mLocked.viewport) {
218         return;
219     }
220 
221     const DisplayViewport oldViewport = mLocked.viewport;
222     mLocked.viewport = viewport;
223 
224     int32_t oldDisplayWidth, oldDisplayHeight;
225     getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight);
226     int32_t newDisplayWidth, newDisplayHeight;
227     getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight);
228 
229     // Reset cursor position to center if size or display changed.
230     if (oldViewport.displayId != viewport.displayId || oldDisplayWidth != newDisplayWidth ||
231         oldDisplayHeight != newDisplayHeight) {
232         float minX, minY, maxX, maxY;
233         if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
234             mLocked.pointerX = (minX + maxX) * 0.5f;
235             mLocked.pointerY = (minY + maxY) * 0.5f;
236             // Reload icon resources for density may be changed.
237             loadResourcesLocked(getAdditionalMouseResources);
238         } else {
239             mLocked.pointerX = 0;
240             mLocked.pointerY = 0;
241         }
242     } else if (oldViewport.orientation != viewport.orientation) {
243         // Apply offsets to convert from the pixel top-left corner position to the pixel center.
244         // This creates an invariant frame of reference that we can easily rotate when
245         // taking into account that the pointer may be located at fractional pixel offsets.
246         float x = mLocked.pointerX + 0.5f;
247         float y = mLocked.pointerY + 0.5f;
248         float temp;
249 
250         // Undo the previous rotation.
251         switch (oldViewport.orientation) {
252             case DISPLAY_ORIENTATION_90:
253                 temp = x;
254                 x = oldViewport.deviceHeight - y;
255                 y = temp;
256                 break;
257             case DISPLAY_ORIENTATION_180:
258                 x = oldViewport.deviceWidth - x;
259                 y = oldViewport.deviceHeight - y;
260                 break;
261             case DISPLAY_ORIENTATION_270:
262                 temp = x;
263                 x = y;
264                 y = oldViewport.deviceWidth - temp;
265                 break;
266         }
267 
268         // Perform the new rotation.
269         switch (viewport.orientation) {
270             case DISPLAY_ORIENTATION_90:
271                 temp = x;
272                 x = y;
273                 y = viewport.deviceHeight - temp;
274                 break;
275             case DISPLAY_ORIENTATION_180:
276                 x = viewport.deviceWidth - x;
277                 y = viewport.deviceHeight - y;
278                 break;
279             case DISPLAY_ORIENTATION_270:
280                 temp = x;
281                 x = viewport.deviceWidth - y;
282                 y = temp;
283                 break;
284         }
285 
286         // Apply offsets to convert from the pixel center to the pixel top-left corner position
287         // and save the results.
288         mLocked.pointerX = x - 0.5f;
289         mLocked.pointerY = y - 0.5f;
290     }
291 
292     updatePointerLocked();
293 }
294 
updatePointerIcon(int32_t iconId)295 void MouseCursorController::updatePointerIcon(int32_t iconId) {
296     std::scoped_lock lock(mLock);
297 
298     if (mLocked.requestedPointerType != iconId) {
299         mLocked.requestedPointerType = iconId;
300         mLocked.updatePointerIcon = true;
301         updatePointerLocked();
302     }
303 }
304 
setCustomPointerIcon(const SpriteIcon & icon)305 void MouseCursorController::setCustomPointerIcon(const SpriteIcon& icon) {
306     std::scoped_lock lock(mLock);
307 
308     const int32_t iconId = mContext.getPolicy()->getCustomPointerIconId();
309     mLocked.additionalMouseResources[iconId] = icon;
310     mLocked.requestedPointerType = iconId;
311     mLocked.updatePointerIcon = true;
312     updatePointerLocked();
313 }
314 
doFadingAnimationLocked(nsecs_t timestamp)315 bool MouseCursorController::doFadingAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) {
316     nsecs_t frameDelay = timestamp - mContext.getAnimationTime();
317     bool keepAnimating = false;
318 
319     // Animate pointer fade.
320     if (mLocked.pointerFadeDirection < 0) {
321         mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
322         if (mLocked.pointerAlpha <= 0.0f) {
323             mLocked.pointerAlpha = 0.0f;
324             mLocked.pointerFadeDirection = 0;
325         } else {
326             keepAnimating = true;
327         }
328         updatePointerLocked();
329     } else if (mLocked.pointerFadeDirection > 0) {
330         mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
331         if (mLocked.pointerAlpha >= 1.0f) {
332             mLocked.pointerAlpha = 1.0f;
333             mLocked.pointerFadeDirection = 0;
334         } else {
335             keepAnimating = true;
336         }
337         updatePointerLocked();
338     }
339     return keepAnimating;
340 }
341 
doBitmapAnimationLocked(nsecs_t timestamp)342 bool MouseCursorController::doBitmapAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) {
343     std::map<int32_t, PointerAnimation>::const_iterator iter =
344             mLocked.animationResources.find(mLocked.requestedPointerType);
345     if (iter == mLocked.animationResources.end()) {
346         return false;
347     }
348 
349     if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) {
350         sp<SpriteController> spriteController = mContext.getSpriteController();
351         spriteController->openTransaction();
352 
353         int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame;
354         mLocked.animationFrameIndex += incr;
355         mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr;
356         while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) {
357             mLocked.animationFrameIndex -= iter->second.animationFrames.size();
358         }
359         mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]);
360 
361         spriteController->closeTransaction();
362     }
363     // Keep animating.
364     return true;
365 }
366 
updatePointerLocked()367 void MouseCursorController::updatePointerLocked() REQUIRES(mLock) {
368     if (!mLocked.viewport.isValid()) {
369         return;
370     }
371     sp<SpriteController> spriteController = mContext.getSpriteController();
372     spriteController->openTransaction();
373 
374     mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
375     mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
376     mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId);
377 
378     if (mLocked.pointerAlpha > 0) {
379         mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
380         mLocked.pointerSprite->setVisible(true);
381     } else {
382         mLocked.pointerSprite->setVisible(false);
383     }
384 
385     if (mLocked.updatePointerIcon) {
386         if (mLocked.requestedPointerType == mContext.getPolicy()->getDefaultPointerIconId()) {
387             mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
388         } else {
389             std::map<int32_t, SpriteIcon>::const_iterator iter =
390                     mLocked.additionalMouseResources.find(mLocked.requestedPointerType);
391             if (iter != mLocked.additionalMouseResources.end()) {
392                 std::map<int32_t, PointerAnimation>::const_iterator anim_iter =
393                         mLocked.animationResources.find(mLocked.requestedPointerType);
394                 if (anim_iter != mLocked.animationResources.end()) {
395                     mLocked.animationFrameIndex = 0;
396                     mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC);
397                     startAnimationLocked();
398                 }
399                 mLocked.pointerSprite->setIcon(iter->second);
400             } else {
401                 ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType);
402                 mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
403             }
404         }
405         mLocked.updatePointerIcon = false;
406     }
407 
408     spriteController->closeTransaction();
409 }
410 
loadResourcesLocked(bool getAdditionalMouseResources)411 void MouseCursorController::loadResourcesLocked(bool getAdditionalMouseResources) REQUIRES(mLock) {
412     if (!mLocked.viewport.isValid()) {
413         return;
414     }
415 
416     if (!mLocked.resourcesLoaded) mLocked.resourcesLoaded = true;
417 
418     sp<PointerControllerPolicyInterface> policy = mContext.getPolicy();
419     policy->loadPointerResources(&mResources, mLocked.viewport.displayId);
420     policy->loadPointerIcon(&mLocked.pointerIcon, mLocked.viewport.displayId);
421 
422     mLocked.additionalMouseResources.clear();
423     mLocked.animationResources.clear();
424     if (getAdditionalMouseResources) {
425         policy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
426                                              &mLocked.animationResources,
427                                              mLocked.viewport.displayId);
428     }
429 
430     mLocked.updatePointerIcon = true;
431 }
432 
isViewportValid()433 bool MouseCursorController::isViewportValid() {
434     std::scoped_lock lock(mLock);
435     return mLocked.viewport.isValid();
436 }
437 
getAdditionalMouseResources()438 void MouseCursorController::getAdditionalMouseResources() {
439     std::scoped_lock lock(mLock);
440 
441     if (mLocked.additionalMouseResources.empty()) {
442         mContext.getPolicy()->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
443                                                            &mLocked.animationResources,
444                                                            mLocked.viewport.displayId);
445     }
446     mLocked.updatePointerIcon = true;
447     updatePointerLocked();
448 }
449 
resourcesLoaded()450 bool MouseCursorController::resourcesLoaded() {
451     std::scoped_lock lock(mLock);
452     return mLocked.resourcesLoaded;
453 }
454 
doAnimations(nsecs_t timestamp)455 bool MouseCursorController::doAnimations(nsecs_t timestamp) {
456     std::scoped_lock lock(mLock);
457     bool keepFading = doFadingAnimationLocked(timestamp);
458     bool keepBitmap = doBitmapAnimationLocked(timestamp);
459     bool keepAnimating = keepFading || keepBitmap;
460     if (!keepAnimating) {
461         /*
462          * We know that this callback will be removed before another
463          * is added. mLock in PointerAnimator will not be released
464          * until after this is removed, and adding another callback
465          * requires that lock. Thus it's safe to set mLocked.animating
466          * here.
467          */
468         mLocked.animating = false;
469     }
470     return keepAnimating;
471 }
472 
startAnimationLocked()473 void MouseCursorController::startAnimationLocked() REQUIRES(mLock) {
474     using namespace std::placeholders;
475 
476     if (mLocked.animating) {
477         return;
478     }
479     mLocked.animating = true;
480 
481     std::function<bool(nsecs_t)> func = std::bind(&MouseCursorController::doAnimations, this, _1);
482     /*
483      * Using -1 for displayId here to avoid removing the callback
484      * if a TouchSpotController with the same display is removed.
485      */
486     mContext.addAnimationCallback(-1, func);
487 }
488 
489 } // namespace android
490