PointerController.cpp revision 05dc66ada6b61a6bdf806ffaa62617ac5394695d
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 17#define LOG_TAG "PointerController" 18 19//#define LOG_NDEBUG 0 20 21// Log debug messages about pointer updates 22#define DEBUG_POINTER_UPDATES 0 23 24#include "PointerController.h" 25 26#include <cutils/log.h> 27 28#include <SkBitmap.h> 29#include <SkCanvas.h> 30#include <SkColor.h> 31#include <SkPaint.h> 32#include <SkXfermode.h> 33 34namespace android { 35 36// --- PointerController --- 37 38// Time to wait before starting the fade when the pointer is inactive. 39static const nsecs_t INACTIVITY_FADE_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds 40static const nsecs_t INACTIVITY_FADE_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds 41 42// Time to spend fading out the pointer completely. 43static const nsecs_t FADE_DURATION = 500 * 1000000LL; // 500 ms 44 45// Time to wait between frames. 46static const nsecs_t FADE_FRAME_INTERVAL = 1000000000LL / 60; 47 48// Amount to subtract from alpha per frame. 49static const float FADE_DECAY_PER_FRAME = float(FADE_FRAME_INTERVAL) / FADE_DURATION; 50 51 52PointerController::PointerController(const sp<Looper>& looper, int32_t pointerLayer) : 53 mLooper(looper), mPointerLayer(pointerLayer) { 54 AutoMutex _l(mLock); 55 56 mLocked.displayWidth = -1; 57 mLocked.displayHeight = -1; 58 mLocked.displayOrientation = DISPLAY_ORIENTATION_0; 59 60 mLocked.pointerX = 0; 61 mLocked.pointerY = 0; 62 mLocked.buttonState = 0; 63 64 mLocked.iconBitmap = NULL; 65 mLocked.iconHotSpotX = 0; 66 mLocked.iconHotSpotY = 0; 67 68 mLocked.fadeAlpha = 1; 69 mLocked.inactivityFadeDelay = INACTIVITY_FADE_DELAY_NORMAL; 70 71 mLocked.wantVisible = false; 72 mLocked.visible = false; 73 mLocked.drawn = false; 74 75 mHandler = new WeakMessageHandler(this); 76} 77 78PointerController::~PointerController() { 79 mLooper->removeMessages(mHandler); 80 81 if (mSurfaceControl != NULL) { 82 mSurfaceControl->clear(); 83 mSurfaceControl.clear(); 84 } 85 86 if (mSurfaceComposerClient != NULL) { 87 mSurfaceComposerClient->dispose(); 88 mSurfaceComposerClient.clear(); 89 } 90 91 delete mLocked.iconBitmap; 92} 93 94bool PointerController::getBounds(float* outMinX, float* outMinY, 95 float* outMaxX, float* outMaxY) const { 96 AutoMutex _l(mLock); 97 98 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY); 99} 100 101bool PointerController::getBoundsLocked(float* outMinX, float* outMinY, 102 float* outMaxX, float* outMaxY) const { 103 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) { 104 return false; 105 } 106 107 *outMinX = 0; 108 *outMinY = 0; 109 switch (mLocked.displayOrientation) { 110 case DISPLAY_ORIENTATION_90: 111 case DISPLAY_ORIENTATION_270: 112 *outMaxX = mLocked.displayHeight; 113 *outMaxY = mLocked.displayWidth; 114 break; 115 default: 116 *outMaxX = mLocked.displayWidth; 117 *outMaxY = mLocked.displayHeight; 118 break; 119 } 120 return true; 121} 122 123void PointerController::move(float deltaX, float deltaY) { 124#if DEBUG_POINTER_UPDATES 125 LOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY); 126#endif 127 if (deltaX == 0.0f && deltaY == 0.0f) { 128 return; 129 } 130 131 AutoMutex _l(mLock); 132 133 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY); 134} 135 136void PointerController::setButtonState(uint32_t buttonState) { 137#if DEBUG_POINTER_UPDATES 138 LOGD("Set button state 0x%08x", buttonState); 139#endif 140 AutoMutex _l(mLock); 141 142 if (mLocked.buttonState != buttonState) { 143 mLocked.buttonState = buttonState; 144 unfadeBeforeUpdateLocked(); 145 updateLocked(); 146 } 147} 148 149uint32_t PointerController::getButtonState() const { 150 AutoMutex _l(mLock); 151 152 return mLocked.buttonState; 153} 154 155void PointerController::setPosition(float x, float y) { 156#if DEBUG_POINTER_UPDATES 157 LOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y); 158#endif 159 AutoMutex _l(mLock); 160 161 setPositionLocked(x, y); 162} 163 164void PointerController::setPositionLocked(float x, float y) { 165 float minX, minY, maxX, maxY; 166 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { 167 if (x <= minX) { 168 mLocked.pointerX = minX; 169 } else if (x >= maxX) { 170 mLocked.pointerX = maxX; 171 } else { 172 mLocked.pointerX = x; 173 } 174 if (y <= minY) { 175 mLocked.pointerY = minY; 176 } else if (y >= maxY) { 177 mLocked.pointerY = maxY; 178 } else { 179 mLocked.pointerY = y; 180 } 181 unfadeBeforeUpdateLocked(); 182 updateLocked(); 183 } 184} 185 186void PointerController::getPosition(float* outX, float* outY) const { 187 AutoMutex _l(mLock); 188 189 *outX = mLocked.pointerX; 190 *outY = mLocked.pointerY; 191} 192 193void PointerController::fade() { 194 AutoMutex _l(mLock); 195 196 startFadeLocked(); 197} 198 199void PointerController::unfade() { 200 AutoMutex _l(mLock); 201 202 if (unfadeBeforeUpdateLocked()) { 203 updateLocked(); 204 } 205} 206 207void PointerController::setInactivityFadeDelay(InactivityFadeDelay inactivityFadeDelay) { 208 AutoMutex _l(mLock); 209 210 if (mLocked.inactivityFadeDelay != inactivityFadeDelay) { 211 mLocked.inactivityFadeDelay = inactivityFadeDelay; 212 startInactivityFadeDelayLocked(); 213 } 214} 215 216void PointerController::updateLocked() { 217 bool wantVisibleAndHavePointerIcon = mLocked.wantVisible && mLocked.iconBitmap; 218 219 if (wantVisibleAndHavePointerIcon) { 220 // Want the pointer to be visible. 221 // Ensure the surface is created and drawn. 222 if (!createSurfaceIfNeededLocked() || !drawPointerIfNeededLocked()) { 223 return; 224 } 225 } else { 226 // Don't want the pointer to be visible. 227 // If it is not visible then we are done. 228 if (mSurfaceControl == NULL || !mLocked.visible) { 229 return; 230 } 231 } 232 233 status_t status = mSurfaceComposerClient->openTransaction(); 234 if (status) { 235 LOGE("Error opening surface transaction to update pointer surface."); 236 return; 237 } 238 239 if (wantVisibleAndHavePointerIcon) { 240 status = mSurfaceControl->setPosition( 241 mLocked.pointerX - mLocked.iconHotSpotX, 242 mLocked.pointerY - mLocked.iconHotSpotY); 243 if (status) { 244 LOGE("Error %d moving pointer surface.", status); 245 goto CloseTransaction; 246 } 247 248 status = mSurfaceControl->setAlpha(mLocked.fadeAlpha); 249 if (status) { 250 LOGE("Error %d setting pointer surface alpha.", status); 251 goto CloseTransaction; 252 } 253 254 if (!mLocked.visible) { 255 status = mSurfaceControl->setLayer(mPointerLayer); 256 if (status) { 257 LOGE("Error %d setting pointer surface layer.", status); 258 goto CloseTransaction; 259 } 260 261 status = mSurfaceControl->show(mPointerLayer); 262 if (status) { 263 LOGE("Error %d showing pointer surface.", status); 264 goto CloseTransaction; 265 } 266 267 mLocked.visible = true; 268 } 269 } else { 270 if (mLocked.visible) { 271 status = mSurfaceControl->hide(); 272 if (status) { 273 LOGE("Error %d hiding pointer surface.", status); 274 goto CloseTransaction; 275 } 276 277 mLocked.visible = false; 278 } 279 } 280 281CloseTransaction: 282 status = mSurfaceComposerClient->closeTransaction(); 283 if (status) { 284 LOGE("Error closing surface transaction to update pointer surface."); 285 } 286} 287 288void PointerController::setDisplaySize(int32_t width, int32_t height) { 289 AutoMutex _l(mLock); 290 291 if (mLocked.displayWidth != width || mLocked.displayHeight != height) { 292 mLocked.displayWidth = width; 293 mLocked.displayHeight = height; 294 295 float minX, minY, maxX, maxY; 296 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { 297 mLocked.pointerX = (minX + maxX) * 0.5f; 298 mLocked.pointerY = (minY + maxY) * 0.5f; 299 } else { 300 mLocked.pointerX = 0; 301 mLocked.pointerY = 0; 302 } 303 304 updateLocked(); 305 } 306} 307 308void PointerController::setDisplayOrientation(int32_t orientation) { 309 AutoMutex _l(mLock); 310 311 if (mLocked.displayOrientation != orientation) { 312 float absoluteX, absoluteY; 313 314 // Map from oriented display coordinates to absolute display coordinates. 315 switch (mLocked.displayOrientation) { 316 case DISPLAY_ORIENTATION_90: 317 absoluteX = mLocked.displayWidth - mLocked.pointerY; 318 absoluteY = mLocked.pointerX; 319 break; 320 case DISPLAY_ORIENTATION_180: 321 absoluteX = mLocked.displayWidth - mLocked.pointerX; 322 absoluteY = mLocked.displayHeight - mLocked.pointerY; 323 break; 324 case DISPLAY_ORIENTATION_270: 325 absoluteX = mLocked.pointerY; 326 absoluteY = mLocked.displayHeight - mLocked.pointerX; 327 break; 328 default: 329 absoluteX = mLocked.pointerX; 330 absoluteY = mLocked.pointerY; 331 break; 332 } 333 334 // Map from absolute display coordinates to oriented display coordinates. 335 switch (orientation) { 336 case DISPLAY_ORIENTATION_90: 337 mLocked.pointerX = absoluteY; 338 mLocked.pointerY = mLocked.displayWidth - absoluteX; 339 break; 340 case DISPLAY_ORIENTATION_180: 341 mLocked.pointerX = mLocked.displayWidth - absoluteX; 342 mLocked.pointerY = mLocked.displayHeight - absoluteY; 343 break; 344 case DISPLAY_ORIENTATION_270: 345 mLocked.pointerX = mLocked.displayHeight - absoluteY; 346 mLocked.pointerY = absoluteX; 347 break; 348 default: 349 mLocked.pointerX = absoluteX; 350 mLocked.pointerY = absoluteY; 351 break; 352 } 353 354 mLocked.displayOrientation = orientation; 355 356 updateLocked(); 357 } 358} 359 360void PointerController::setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY) { 361 AutoMutex _l(mLock); 362 363 if (mLocked.iconBitmap) { 364 delete mLocked.iconBitmap; 365 mLocked.iconBitmap = NULL; 366 } 367 368 if (bitmap) { 369 mLocked.iconBitmap = new SkBitmap(); 370 bitmap->copyTo(mLocked.iconBitmap, SkBitmap::kARGB_8888_Config); 371 } 372 373 mLocked.iconHotSpotX = hotSpotX; 374 mLocked.iconHotSpotY = hotSpotY; 375 mLocked.drawn = false; 376} 377 378bool PointerController::createSurfaceIfNeededLocked() { 379 if (!mLocked.iconBitmap) { 380 // If we don't have a pointer icon, then no point allocating a surface now. 381 return false; 382 } 383 384 if (mSurfaceComposerClient == NULL) { 385 mSurfaceComposerClient = new SurfaceComposerClient(); 386 } 387 388 if (mSurfaceControl == NULL) { 389 mSurfaceControl = mSurfaceComposerClient->createSurface(getpid(), 390 String8("Pointer Icon"), 0, 391 mLocked.iconBitmap->width(), mLocked.iconBitmap->height(), 392 PIXEL_FORMAT_RGBA_8888); 393 if (mSurfaceControl == NULL) { 394 LOGE("Error creating pointer surface."); 395 return false; 396 } 397 } 398 return true; 399} 400 401bool PointerController::drawPointerIfNeededLocked() { 402 if (!mLocked.drawn) { 403 if (!mLocked.iconBitmap) { 404 return false; 405 } 406 407 if (!resizeSurfaceLocked(mLocked.iconBitmap->width(), mLocked.iconBitmap->height())) { 408 return false; 409 } 410 411 sp<Surface> surface = mSurfaceControl->getSurface(); 412 413 Surface::SurfaceInfo surfaceInfo; 414 status_t status = surface->lock(&surfaceInfo); 415 if (status) { 416 LOGE("Error %d locking pointer surface before drawing.", status); 417 return false; 418 } 419 420 SkBitmap surfaceBitmap; 421 ssize_t bpr = surfaceInfo.s * bytesPerPixel(surfaceInfo.format); 422 surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config, surfaceInfo.w, surfaceInfo.h, bpr); 423 surfaceBitmap.setPixels(surfaceInfo.bits); 424 425 SkCanvas surfaceCanvas; 426 surfaceCanvas.setBitmapDevice(surfaceBitmap); 427 428 SkPaint paint; 429 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 430 surfaceCanvas.drawBitmap(*mLocked.iconBitmap, 0, 0, &paint); 431 432 status = surface->unlockAndPost(); 433 if (status) { 434 LOGE("Error %d unlocking pointer surface after drawing.", status); 435 return false; 436 } 437 } 438 439 mLocked.drawn = true; 440 return true; 441} 442 443bool PointerController::resizeSurfaceLocked(int32_t width, int32_t height) { 444 status_t status = mSurfaceComposerClient->openTransaction(); 445 if (status) { 446 LOGE("Error opening surface transaction to resize pointer surface."); 447 return false; 448 } 449 450 status = mSurfaceControl->setSize(width, height); 451 if (status) { 452 LOGE("Error %d setting pointer surface size.", status); 453 return false; 454 } 455 456 status = mSurfaceComposerClient->closeTransaction(); 457 if (status) { 458 LOGE("Error closing surface transaction to resize pointer surface."); 459 return false; 460 } 461 462 return true; 463} 464 465void PointerController::handleMessage(const Message& message) { 466 switch (message.what) { 467 case MSG_FADE_STEP: { 468 AutoMutex _l(mLock); 469 fadeStepLocked(); 470 break; 471 } 472 } 473} 474 475bool PointerController::unfadeBeforeUpdateLocked() { 476 sendFadeStepMessageDelayedLocked(getInactivityFadeDelayTimeLocked()); 477 478 if (isFadingLocked()) { 479 mLocked.wantVisible = true; 480 mLocked.fadeAlpha = 1; 481 return true; // update required to effect the unfade 482 } 483 return false; // update not required 484} 485 486void PointerController::startFadeLocked() { 487 if (!isFadingLocked()) { 488 sendFadeStepMessageDelayedLocked(0); 489 } 490} 491 492void PointerController::startInactivityFadeDelayLocked() { 493 if (!isFadingLocked()) { 494 sendFadeStepMessageDelayedLocked(getInactivityFadeDelayTimeLocked()); 495 } 496} 497 498void PointerController::fadeStepLocked() { 499 if (mLocked.wantVisible) { 500 mLocked.fadeAlpha -= FADE_DECAY_PER_FRAME; 501 if (mLocked.fadeAlpha < 0) { 502 mLocked.fadeAlpha = 0; 503 mLocked.wantVisible = false; 504 } else { 505 sendFadeStepMessageDelayedLocked(FADE_FRAME_INTERVAL); 506 } 507 updateLocked(); 508 } 509} 510 511bool PointerController::isFadingLocked() { 512 return !mLocked.wantVisible || mLocked.fadeAlpha != 1; 513} 514 515nsecs_t PointerController::getInactivityFadeDelayTimeLocked() { 516 return mLocked.inactivityFadeDelay == INACTIVITY_FADE_DELAY_SHORT 517 ? INACTIVITY_FADE_DELAY_TIME_SHORT : INACTIVITY_FADE_DELAY_TIME_NORMAL; 518} 519 520void PointerController::sendFadeStepMessageDelayedLocked(nsecs_t delayTime) { 521 mLooper->removeMessages(mHandler, MSG_FADE_STEP); 522 mLooper->sendMessageDelayed(delayTime, mHandler, Message(MSG_FADE_STEP)); 523} 524 525} // namespace android 526