blob: 45da008c3e8e02068545579a2e7721a10ea33fce [file] [log] [blame]
Liam Harringtonc782be62020-07-17 19:48:24 +00001/*
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
33namespace {
34// Time to spend fading out the pointer completely.
35const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
36} // namespace
37
38namespace android {
39
40// --- MouseCursorController ---
41
42MouseCursorController::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
62MouseCursorController::~MouseCursorController() {
63 std::scoped_lock lock(mLock);
64
65 mLocked.pointerSprite.clear();
66}
67
68bool 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
75bool 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
88void 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
101void 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
112int32_t MouseCursorController::getButtonState() const {
113 std::scoped_lock lock(mLock);
114 return mLocked.buttonState;
115}
116
117void 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
125void 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
146void MouseCursorController::getPosition(float* outX, float* outY) const {
147 std::scoped_lock lock(mLock);
148
149 *outX = mLocked.pointerX;
150 *outY = mLocked.pointerY;
151}
152
153int32_t MouseCursorController::getDisplayId() const {
154 std::scoped_lock lock(mLock);
155 return mLocked.viewport.displayId;
156}
157
158void 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;
Liam Harringtonce637132020-08-14 04:00:11 +0000171 startAnimationLocked();
Liam Harringtonc782be62020-07-17 19:48:24 +0000172 }
173}
174
175void 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;
Liam Harringtonce637132020-08-14 04:00:11 +0000188 startAnimationLocked();
Liam Harringtonc782be62020-07-17 19:48:24 +0000189 }
190}
191
192void 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 */
203static 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
213void 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
295void 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
305void 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
Liam Harringtonce637132020-08-14 04:00:11 +0000315bool MouseCursorController::doFadingAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000316 nsecs_t frameDelay = timestamp - mContext.getAnimationTime();
Liam Harringtonce637132020-08-14 04:00:11 +0000317 bool keepAnimating = false;
Liam Harringtonc782be62020-07-17 19:48:24 +0000318
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 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000339 return keepAnimating;
340}
341
Liam Harringtonce637132020-08-14 04:00:11 +0000342bool MouseCursorController::doBitmapAnimationLocked(nsecs_t timestamp) REQUIRES(mLock) {
Liam Harringtonc782be62020-07-17 19:48:24 +0000343 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 }
Liam Harringtonc782be62020-07-17 19:48:24 +0000363 // Keep animating.
364 return true;
365}
366
367void 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);
Liam Harringtonce637132020-08-14 04:00:11 +0000397 startAnimationLocked();
Liam Harringtonc782be62020-07-17 19:48:24 +0000398 }
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
411void 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
433bool MouseCursorController::isViewportValid() {
434 std::scoped_lock lock(mLock);
435 return mLocked.viewport.isValid();
436}
437
438void 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
450bool MouseCursorController::resourcesLoaded() {
451 std::scoped_lock lock(mLock);
452 return mLocked.resourcesLoaded;
453}
454
Liam Harringtonce637132020-08-14 04:00:11 +0000455bool 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
473void 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
Liam Harringtonc782be62020-07-17 19:48:24 +0000489} // namespace android