PointerController.cpp revision f5a2ff6f2e5c5ba9dc7ab16f3b7f8f05daf3c159
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 38PointerController::PointerController(int32_t pointerLayer) : 39 mPointerLayer(pointerLayer) { 40 AutoMutex _l(mLock); 41 42 mLocked.displayWidth = -1; 43 mLocked.displayHeight = -1; 44 mLocked.displayOrientation = DISPLAY_ORIENTATION_0; 45 46 mLocked.pointerX = 0; 47 mLocked.pointerY = 0; 48 mLocked.buttonState = 0; 49 50 mLocked.iconBitmap = NULL; 51 mLocked.iconHotSpotX = 0; 52 mLocked.iconHotSpotY = 0; 53 54 mLocked.wantVisible = false; 55 mLocked.visible = false; 56 mLocked.drawn = false; 57} 58 59PointerController::~PointerController() { 60 if (mSurfaceControl != NULL) { 61 mSurfaceControl->clear(); 62 mSurfaceControl.clear(); 63 } 64 65 if (mSurfaceComposerClient != NULL) { 66 mSurfaceComposerClient->dispose(); 67 mSurfaceComposerClient.clear(); 68 } 69 70 delete mLocked.iconBitmap; 71} 72 73bool PointerController::getBounds(float* outMinX, float* outMinY, 74 float* outMaxX, float* outMaxY) const { 75 AutoMutex _l(mLock); 76 77 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY); 78} 79 80bool PointerController::getBoundsLocked(float* outMinX, float* outMinY, 81 float* outMaxX, float* outMaxY) const { 82 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) { 83 return false; 84 } 85 86 *outMinX = 0; 87 *outMinY = 0; 88 switch (mLocked.displayOrientation) { 89 case DISPLAY_ORIENTATION_90: 90 case DISPLAY_ORIENTATION_270: 91 *outMaxX = mLocked.displayHeight; 92 *outMaxY = mLocked.displayWidth; 93 break; 94 default: 95 *outMaxX = mLocked.displayWidth; 96 *outMaxY = mLocked.displayHeight; 97 break; 98 } 99 return true; 100} 101 102void PointerController::move(float deltaX, float deltaY) { 103#if DEBUG_POINTER_UPDATES 104 LOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY); 105#endif 106 if (deltaX == 0.0f && deltaY == 0.0f) { 107 return; 108 } 109 110 AutoMutex _l(mLock); 111 112 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY); 113} 114 115void PointerController::setButtonState(uint32_t buttonState) { 116#if DEBUG_POINTER_UPDATES 117 LOGD("Set button state 0x%08x", buttonState); 118#endif 119 AutoMutex _l(mLock); 120 121 if (mLocked.buttonState != buttonState) { 122 mLocked.buttonState = buttonState; 123 mLocked.wantVisible = true; 124 updateLocked(); 125 } 126} 127 128uint32_t PointerController::getButtonState() const { 129 AutoMutex _l(mLock); 130 131 return mLocked.buttonState; 132} 133 134void PointerController::setPosition(float x, float y) { 135#if DEBUG_POINTER_UPDATES 136 LOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y); 137#endif 138 AutoMutex _l(mLock); 139 140 setPositionLocked(x, y); 141} 142 143void PointerController::setPositionLocked(float x, float y) { 144 float minX, minY, maxX, maxY; 145 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { 146 if (x <= minX) { 147 mLocked.pointerX = minX; 148 } else if (x >= maxX) { 149 mLocked.pointerX = maxX; 150 } else { 151 mLocked.pointerX = x; 152 } 153 if (y <= minY) { 154 mLocked.pointerY = minY; 155 } else if (y >= maxY) { 156 mLocked.pointerY = maxY; 157 } else { 158 mLocked.pointerY = y; 159 } 160 mLocked.wantVisible = true; 161 updateLocked(); 162 } 163} 164 165void PointerController::getPosition(float* outX, float* outY) const { 166 AutoMutex _l(mLock); 167 168 *outX = mLocked.pointerX; 169 *outY = mLocked.pointerY; 170} 171 172void PointerController::updateLocked() { 173 bool wantVisibleAndHavePointerIcon = mLocked.wantVisible && mLocked.iconBitmap; 174 175 if (wantVisibleAndHavePointerIcon) { 176 // Want the pointer to be visible. 177 // Ensure the surface is created and drawn. 178 if (!createSurfaceIfNeededLocked() || !drawPointerIfNeededLocked()) { 179 return; 180 } 181 } else { 182 // Don't want the pointer to be visible. 183 // If it is not visible then we are done. 184 if (mSurfaceControl == NULL || !mLocked.visible) { 185 return; 186 } 187 } 188 189 status_t status = mSurfaceComposerClient->openTransaction(); 190 if (status) { 191 LOGE("Error opening surface transaction to update pointer surface."); 192 return; 193 } 194 195 if (wantVisibleAndHavePointerIcon) { 196 status = mSurfaceControl->setPosition( 197 mLocked.pointerX - mLocked.iconHotSpotX, 198 mLocked.pointerY - mLocked.iconHotSpotY); 199 if (status) { 200 LOGE("Error %d moving pointer surface.", status); 201 goto CloseTransaction; 202 } 203 204 if (!mLocked.visible) { 205 status = mSurfaceControl->setLayer(mPointerLayer); 206 if (status) { 207 LOGE("Error %d setting pointer surface layer.", status); 208 goto CloseTransaction; 209 } 210 211 status = mSurfaceControl->show(mPointerLayer); 212 if (status) { 213 LOGE("Error %d showing pointer surface.", status); 214 goto CloseTransaction; 215 } 216 217 mLocked.visible = true; 218 } 219 } else { 220 if (mLocked.visible) { 221 status = mSurfaceControl->hide(); 222 if (status) { 223 LOGE("Error %d hiding pointer surface.", status); 224 goto CloseTransaction; 225 } 226 227 mLocked.visible = false; 228 } 229 } 230 231CloseTransaction: 232 status = mSurfaceComposerClient->closeTransaction(); 233 if (status) { 234 LOGE("Error closing surface transaction to update pointer surface."); 235 } 236} 237 238void PointerController::setDisplaySize(int32_t width, int32_t height) { 239 AutoMutex _l(mLock); 240 241 if (mLocked.displayWidth != width || mLocked.displayHeight != height) { 242 mLocked.displayWidth = width; 243 mLocked.displayHeight = height; 244 245 float minX, minY, maxX, maxY; 246 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { 247 mLocked.pointerX = (minX + maxX) * 0.5f; 248 mLocked.pointerY = (minY + maxY) * 0.5f; 249 } else { 250 mLocked.pointerX = 0; 251 mLocked.pointerY = 0; 252 } 253 254 updateLocked(); 255 } 256} 257 258void PointerController::setDisplayOrientation(int32_t orientation) { 259 AutoMutex _l(mLock); 260 261 if (mLocked.displayOrientation != orientation) { 262 float absoluteX, absoluteY; 263 264 // Map from oriented display coordinates to absolute display coordinates. 265 switch (mLocked.displayOrientation) { 266 case DISPLAY_ORIENTATION_90: 267 absoluteX = mLocked.displayWidth - mLocked.pointerY; 268 absoluteY = mLocked.pointerX; 269 break; 270 case DISPLAY_ORIENTATION_180: 271 absoluteX = mLocked.displayWidth - mLocked.pointerX; 272 absoluteY = mLocked.displayHeight - mLocked.pointerY; 273 break; 274 case DISPLAY_ORIENTATION_270: 275 absoluteX = mLocked.pointerY; 276 absoluteY = mLocked.displayHeight - mLocked.pointerX; 277 break; 278 default: 279 absoluteX = mLocked.pointerX; 280 absoluteY = mLocked.pointerY; 281 break; 282 } 283 284 // Map from absolute display coordinates to oriented display coordinates. 285 switch (orientation) { 286 case DISPLAY_ORIENTATION_90: 287 mLocked.pointerX = absoluteY; 288 mLocked.pointerY = mLocked.displayWidth - absoluteX; 289 break; 290 case DISPLAY_ORIENTATION_180: 291 mLocked.pointerX = mLocked.displayWidth - absoluteX; 292 mLocked.pointerY = mLocked.displayHeight - absoluteY; 293 break; 294 case DISPLAY_ORIENTATION_270: 295 mLocked.pointerX = mLocked.displayHeight - absoluteY; 296 mLocked.pointerY = absoluteX; 297 break; 298 default: 299 mLocked.pointerX = absoluteX; 300 mLocked.pointerY = absoluteY; 301 break; 302 } 303 304 mLocked.displayOrientation = orientation; 305 306 updateLocked(); 307 } 308} 309 310void PointerController::setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY) { 311 AutoMutex _l(mLock); 312 313 if (mLocked.iconBitmap) { 314 delete mLocked.iconBitmap; 315 mLocked.iconBitmap = NULL; 316 } 317 318 if (bitmap) { 319 mLocked.iconBitmap = new SkBitmap(); 320 bitmap->copyTo(mLocked.iconBitmap, SkBitmap::kARGB_8888_Config); 321 } 322 323 mLocked.iconHotSpotX = hotSpotX; 324 mLocked.iconHotSpotY = hotSpotY; 325 mLocked.drawn = false; 326} 327 328bool PointerController::createSurfaceIfNeededLocked() { 329 if (!mLocked.iconBitmap) { 330 // If we don't have a pointer icon, then no point allocating a surface now. 331 return false; 332 } 333 334 if (mSurfaceComposerClient == NULL) { 335 mSurfaceComposerClient = new SurfaceComposerClient(); 336 } 337 338 if (mSurfaceControl == NULL) { 339 mSurfaceControl = mSurfaceComposerClient->createSurface(getpid(), 340 String8("Pointer Icon"), 0, 341 mLocked.iconBitmap->width(), mLocked.iconBitmap->height(), 342 PIXEL_FORMAT_RGBA_8888); 343 if (mSurfaceControl == NULL) { 344 LOGE("Error creating pointer surface."); 345 return false; 346 } 347 } 348 return true; 349} 350 351bool PointerController::drawPointerIfNeededLocked() { 352 if (!mLocked.drawn) { 353 if (!mLocked.iconBitmap) { 354 return false; 355 } 356 357 if (!resizeSurfaceLocked(mLocked.iconBitmap->width(), mLocked.iconBitmap->height())) { 358 return false; 359 } 360 361 sp<Surface> surface = mSurfaceControl->getSurface(); 362 363 Surface::SurfaceInfo surfaceInfo; 364 status_t status = surface->lock(&surfaceInfo); 365 if (status) { 366 LOGE("Error %d locking pointer surface before drawing.", status); 367 return false; 368 } 369 370 SkBitmap surfaceBitmap; 371 ssize_t bpr = surfaceInfo.s * bytesPerPixel(surfaceInfo.format); 372 surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config, surfaceInfo.w, surfaceInfo.h, bpr); 373 surfaceBitmap.setPixels(surfaceInfo.bits); 374 375 SkCanvas surfaceCanvas; 376 surfaceCanvas.setBitmapDevice(surfaceBitmap); 377 378 SkPaint paint; 379 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 380 surfaceCanvas.drawBitmap(*mLocked.iconBitmap, 0, 0, &paint); 381 382 status = surface->unlockAndPost(); 383 if (status) { 384 LOGE("Error %d unlocking pointer surface after drawing.", status); 385 return false; 386 } 387 } 388 389 mLocked.drawn = true; 390 return true; 391} 392 393bool PointerController::resizeSurfaceLocked(int32_t width, int32_t height) { 394 status_t status = mSurfaceComposerClient->openTransaction(); 395 if (status) { 396 LOGE("Error opening surface transaction to resize pointer surface."); 397 return false; 398 } 399 400 status = mSurfaceControl->setSize(width, height); 401 if (status) { 402 LOGE("Error %d setting pointer surface size.", status); 403 return false; 404 } 405 406 status = mSurfaceComposerClient->closeTransaction(); 407 if (status) { 408 LOGE("Error closing surface transaction to resize pointer surface."); 409 return false; 410 } 411 412 return true; 413} 414 415} // namespace android 416