Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "Sprites" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "SpriteController.h" |
| 22 | |
| 23 | #include <cutils/log.h> |
| 24 | #include <utils/String8.h> |
| 25 | |
| 26 | #include <SkBitmap.h> |
| 27 | #include <SkCanvas.h> |
| 28 | #include <SkColor.h> |
| 29 | #include <SkPaint.h> |
| 30 | #include <SkXfermode.h> |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // --- SpriteController --- |
| 35 | |
| 36 | SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) : |
| 37 | mLooper(looper), mOverlayLayer(overlayLayer) { |
| 38 | mHandler = new WeakMessageHandler(this); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 39 | |
| 40 | mLocked.transactionNestingCount = 0; |
| 41 | mLocked.deferredSpriteUpdate = false; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | SpriteController::~SpriteController() { |
| 45 | mLooper->removeMessages(mHandler); |
| 46 | |
| 47 | if (mSurfaceComposerClient != NULL) { |
| 48 | mSurfaceComposerClient->dispose(); |
| 49 | mSurfaceComposerClient.clear(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | sp<Sprite> SpriteController::createSprite() { |
| 54 | return new SpriteImpl(this); |
| 55 | } |
| 56 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 57 | void SpriteController::openTransaction() { |
| 58 | AutoMutex _l(mLock); |
| 59 | |
| 60 | mLocked.transactionNestingCount += 1; |
| 61 | } |
| 62 | |
| 63 | void SpriteController::closeTransaction() { |
| 64 | AutoMutex _l(mLock); |
| 65 | |
| 66 | LOG_ALWAYS_FATAL_IF(mLocked.transactionNestingCount == 0, |
| 67 | "Sprite closeTransaction() called but there is no open sprite transaction"); |
| 68 | |
| 69 | mLocked.transactionNestingCount -= 1; |
| 70 | if (mLocked.transactionNestingCount == 0 && mLocked.deferredSpriteUpdate) { |
| 71 | mLocked.deferredSpriteUpdate = false; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 72 | mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES)); |
| 73 | } |
| 74 | } |
| 75 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 76 | void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) { |
| 77 | bool wasEmpty = mLocked.invalidatedSprites.isEmpty(); |
| 78 | mLocked.invalidatedSprites.push(sprite); |
| 79 | if (wasEmpty) { |
| 80 | if (mLocked.transactionNestingCount != 0) { |
| 81 | mLocked.deferredSpriteUpdate = true; |
| 82 | } else { |
| 83 | mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES)); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 88 | void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 89 | bool wasEmpty = mLocked.disposedSurfaces.isEmpty(); |
| 90 | mLocked.disposedSurfaces.push(surfaceControl); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 91 | if (wasEmpty) { |
| 92 | mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void SpriteController::handleMessage(const Message& message) { |
| 97 | switch (message.what) { |
| 98 | case MSG_UPDATE_SPRITES: |
| 99 | doUpdateSprites(); |
| 100 | break; |
| 101 | case MSG_DISPOSE_SURFACES: |
| 102 | doDisposeSurfaces(); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void SpriteController::doUpdateSprites() { |
| 108 | // Collect information about sprite updates. |
| 109 | // Each sprite update record includes a reference to its associated sprite so we can |
| 110 | // be certain the sprites will not be deleted while this function runs. Sprites |
| 111 | // may invalidate themselves again during this time but we will handle those changes |
| 112 | // in the next iteration. |
| 113 | Vector<SpriteUpdate> updates; |
| 114 | size_t numSprites; |
| 115 | { // acquire lock |
| 116 | AutoMutex _l(mLock); |
| 117 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 118 | numSprites = mLocked.invalidatedSprites.size(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 119 | for (size_t i = 0; i < numSprites; i++) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 120 | const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 121 | |
| 122 | updates.push(SpriteUpdate(sprite, sprite->getStateLocked())); |
| 123 | sprite->resetDirtyLocked(); |
| 124 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 125 | mLocked.invalidatedSprites.clear(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 126 | } // release lock |
| 127 | |
| 128 | // Create missing surfaces. |
| 129 | bool surfaceChanged = false; |
| 130 | for (size_t i = 0; i < numSprites; i++) { |
| 131 | SpriteUpdate& update = updates.editItemAt(i); |
| 132 | |
| 133 | if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 134 | update.state.surfaceWidth = update.state.icon.bitmap.width(); |
| 135 | update.state.surfaceHeight = update.state.icon.bitmap.height(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 136 | update.state.surfaceDrawn = false; |
| 137 | update.state.surfaceVisible = false; |
| 138 | update.state.surfaceControl = obtainSurface( |
| 139 | update.state.surfaceWidth, update.state.surfaceHeight); |
| 140 | if (update.state.surfaceControl != NULL) { |
| 141 | update.surfaceChanged = surfaceChanged = true; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Resize sprites if needed, inside a global transaction. |
| 147 | bool haveGlobalTransaction = false; |
| 148 | for (size_t i = 0; i < numSprites; i++) { |
| 149 | SpriteUpdate& update = updates.editItemAt(i); |
| 150 | |
| 151 | if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 152 | int32_t desiredWidth = update.state.icon.bitmap.width(); |
| 153 | int32_t desiredHeight = update.state.icon.bitmap.height(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 154 | if (update.state.surfaceWidth < desiredWidth |
| 155 | || update.state.surfaceHeight < desiredHeight) { |
| 156 | if (!haveGlobalTransaction) { |
| 157 | SurfaceComposerClient::openGlobalTransaction(); |
| 158 | haveGlobalTransaction = true; |
| 159 | } |
| 160 | |
| 161 | status_t status = update.state.surfaceControl->setSize(desiredWidth, desiredHeight); |
| 162 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 163 | ALOGE("Error %d resizing sprite surface from %dx%d to %dx%d", |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 164 | status, update.state.surfaceWidth, update.state.surfaceHeight, |
| 165 | desiredWidth, desiredHeight); |
| 166 | } else { |
| 167 | update.state.surfaceWidth = desiredWidth; |
| 168 | update.state.surfaceHeight = desiredHeight; |
| 169 | update.state.surfaceDrawn = false; |
| 170 | update.surfaceChanged = surfaceChanged = true; |
| 171 | |
| 172 | if (update.state.surfaceVisible) { |
| 173 | status = update.state.surfaceControl->hide(); |
| 174 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 175 | ALOGE("Error %d hiding sprite surface after resize.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 176 | } else { |
| 177 | update.state.surfaceVisible = false; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | if (haveGlobalTransaction) { |
| 185 | SurfaceComposerClient::closeGlobalTransaction(); |
| 186 | } |
| 187 | |
| 188 | // Redraw sprites if needed. |
| 189 | for (size_t i = 0; i < numSprites; i++) { |
| 190 | SpriteUpdate& update = updates.editItemAt(i); |
| 191 | |
| 192 | if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) { |
| 193 | update.state.surfaceDrawn = false; |
| 194 | update.surfaceChanged = surfaceChanged = true; |
| 195 | } |
| 196 | |
| 197 | if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn |
| 198 | && update.state.wantSurfaceVisible()) { |
| 199 | sp<Surface> surface = update.state.surfaceControl->getSurface(); |
| 200 | Surface::SurfaceInfo surfaceInfo; |
| 201 | status_t status = surface->lock(&surfaceInfo); |
| 202 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 203 | ALOGE("Error %d locking sprite surface before drawing.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 204 | } else { |
| 205 | SkBitmap surfaceBitmap; |
| 206 | ssize_t bpr = surfaceInfo.s * bytesPerPixel(surfaceInfo.format); |
| 207 | surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 208 | surfaceInfo.w, surfaceInfo.h, bpr); |
| 209 | surfaceBitmap.setPixels(surfaceInfo.bits); |
| 210 | |
| 211 | SkCanvas surfaceCanvas; |
| 212 | surfaceCanvas.setBitmapDevice(surfaceBitmap); |
| 213 | |
| 214 | SkPaint paint; |
| 215 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 216 | surfaceCanvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 217 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 218 | if (surfaceInfo.w > uint32_t(update.state.icon.bitmap.width())) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 219 | paint.setColor(0); // transparent fill color |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 220 | surfaceCanvas.drawRectCoords(update.state.icon.bitmap.width(), 0, |
| 221 | surfaceInfo.w, update.state.icon.bitmap.height(), paint); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 222 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 223 | if (surfaceInfo.h > uint32_t(update.state.icon.bitmap.height())) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 224 | paint.setColor(0); // transparent fill color |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 225 | surfaceCanvas.drawRectCoords(0, update.state.icon.bitmap.height(), |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 226 | surfaceInfo.w, surfaceInfo.h, paint); |
| 227 | } |
| 228 | |
| 229 | status = surface->unlockAndPost(); |
| 230 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 231 | ALOGE("Error %d unlocking and posting sprite surface after drawing.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 232 | } else { |
| 233 | update.state.surfaceDrawn = true; |
| 234 | update.surfaceChanged = surfaceChanged = true; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Set sprite surface properties and make them visible. |
| 241 | bool haveTransaction = false; |
| 242 | for (size_t i = 0; i < numSprites; i++) { |
| 243 | SpriteUpdate& update = updates.editItemAt(i); |
| 244 | |
| 245 | bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible() |
| 246 | && update.state.surfaceDrawn; |
| 247 | bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible; |
| 248 | bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible; |
| 249 | if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden |
| 250 | || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA |
| 251 | | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER |
| 252 | | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) { |
| 253 | status_t status; |
| 254 | if (!haveTransaction) { |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 255 | SurfaceComposerClient::openGlobalTransaction(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 256 | haveTransaction = true; |
| 257 | } |
| 258 | |
| 259 | if (wantSurfaceVisibleAndDrawn |
| 260 | && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) { |
| 261 | status = update.state.surfaceControl->setAlpha(update.state.alpha); |
| 262 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 263 | ALOGE("Error %d setting sprite surface alpha.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | if (wantSurfaceVisibleAndDrawn |
| 268 | && (becomingVisible || (update.state.dirty & (DIRTY_POSITION |
| 269 | | DIRTY_HOTSPOT)))) { |
| 270 | status = update.state.surfaceControl->setPosition( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 271 | update.state.positionX - update.state.icon.hotSpotX, |
| 272 | update.state.positionY - update.state.icon.hotSpotY); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 273 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 274 | ALOGE("Error %d setting sprite surface position.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | if (wantSurfaceVisibleAndDrawn |
| 279 | && (becomingVisible |
| 280 | || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) { |
| 281 | status = update.state.surfaceControl->setMatrix( |
| 282 | update.state.transformationMatrix.dsdx, |
| 283 | update.state.transformationMatrix.dtdx, |
| 284 | update.state.transformationMatrix.dsdy, |
| 285 | update.state.transformationMatrix.dtdy); |
| 286 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 287 | ALOGE("Error %d setting sprite surface transformation matrix.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | int32_t surfaceLayer = mOverlayLayer + update.state.layer; |
| 292 | if (wantSurfaceVisibleAndDrawn |
| 293 | && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) { |
| 294 | status = update.state.surfaceControl->setLayer(surfaceLayer); |
| 295 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 296 | ALOGE("Error %d setting sprite surface layer.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | if (becomingVisible) { |
| 301 | status = update.state.surfaceControl->show(surfaceLayer); |
| 302 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 303 | ALOGE("Error %d showing sprite surface.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 304 | } else { |
| 305 | update.state.surfaceVisible = true; |
| 306 | update.surfaceChanged = surfaceChanged = true; |
| 307 | } |
| 308 | } else if (becomingHidden) { |
| 309 | status = update.state.surfaceControl->hide(); |
| 310 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 311 | ALOGE("Error %d hiding sprite surface.", status); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 312 | } else { |
| 313 | update.state.surfaceVisible = false; |
| 314 | update.surfaceChanged = surfaceChanged = true; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (haveTransaction) { |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 321 | SurfaceComposerClient::closeGlobalTransaction(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // If any surfaces were changed, write back the new surface properties to the sprites. |
| 325 | if (surfaceChanged) { // acquire lock |
| 326 | AutoMutex _l(mLock); |
| 327 | |
| 328 | for (size_t i = 0; i < numSprites; i++) { |
| 329 | const SpriteUpdate& update = updates.itemAt(i); |
| 330 | |
| 331 | if (update.surfaceChanged) { |
| 332 | update.sprite->setSurfaceLocked(update.state.surfaceControl, |
| 333 | update.state.surfaceWidth, update.state.surfaceHeight, |
| 334 | update.state.surfaceDrawn, update.state.surfaceVisible); |
| 335 | } |
| 336 | } |
| 337 | } // release lock |
| 338 | |
| 339 | // Clear the sprite update vector outside the lock. It is very important that |
| 340 | // we do not clear sprite references inside the lock since we could be releasing |
| 341 | // the last remaining reference to the sprite here which would result in the |
| 342 | // sprite being deleted and the lock being reacquired by the sprite destructor |
| 343 | // while already held. |
| 344 | updates.clear(); |
| 345 | } |
| 346 | |
| 347 | void SpriteController::doDisposeSurfaces() { |
| 348 | // Collect disposed surfaces. |
| 349 | Vector<sp<SurfaceControl> > disposedSurfaces; |
| 350 | { // acquire lock |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 351 | AutoMutex _l(mLock); |
| 352 | |
| 353 | disposedSurfaces = mLocked.disposedSurfaces; |
| 354 | mLocked.disposedSurfaces.clear(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 355 | } // release lock |
| 356 | |
| 357 | // Release the last reference to each surface outside of the lock. |
| 358 | // We don't want the surfaces to be deleted while we are holding our lock. |
| 359 | disposedSurfaces.clear(); |
| 360 | } |
| 361 | |
| 362 | void SpriteController::ensureSurfaceComposerClient() { |
| 363 | if (mSurfaceComposerClient == NULL) { |
| 364 | mSurfaceComposerClient = new SurfaceComposerClient(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) { |
| 369 | ensureSurfaceComposerClient(); |
| 370 | |
| 371 | sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface( |
Mathias Agopian | 9638e5c | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 372 | String8("Sprite"), 0, width, height, PIXEL_FORMAT_RGBA_8888); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 373 | if (surfaceControl == NULL || !surfaceControl->isValid() |
| 374 | || !surfaceControl->getSurface()->isValid()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame^] | 375 | ALOGE("Error creating sprite surface."); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 376 | return NULL; |
| 377 | } |
| 378 | return surfaceControl; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | // --- SpriteController::SpriteImpl --- |
| 383 | |
| 384 | SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) : |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 385 | mController(controller) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | SpriteController::SpriteImpl::~SpriteImpl() { |
| 389 | AutoMutex _m(mController->mLock); |
| 390 | |
| 391 | // Let the controller take care of deleting the last reference to sprite |
| 392 | // surfaces so that we do not block the caller on an IPC here. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 393 | if (mLocked.state.surfaceControl != NULL) { |
| 394 | mController->disposeSurfaceLocked(mLocked.state.surfaceControl); |
| 395 | mLocked.state.surfaceControl.clear(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 399 | void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 400 | AutoMutex _l(mController->mLock); |
| 401 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 402 | uint32_t dirty; |
| 403 | if (icon.isValid()) { |
| 404 | icon.bitmap.copyTo(&mLocked.state.icon.bitmap, SkBitmap::kARGB_8888_Config); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 405 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 406 | if (!mLocked.state.icon.isValid() |
| 407 | || mLocked.state.icon.hotSpotX != icon.hotSpotX |
| 408 | || mLocked.state.icon.hotSpotY != icon.hotSpotY) { |
| 409 | mLocked.state.icon.hotSpotX = icon.hotSpotX; |
| 410 | mLocked.state.icon.hotSpotY = icon.hotSpotY; |
| 411 | dirty = DIRTY_BITMAP | DIRTY_HOTSPOT; |
| 412 | } else { |
| 413 | dirty = DIRTY_BITMAP; |
| 414 | } |
| 415 | } else if (mLocked.state.icon.isValid()) { |
| 416 | mLocked.state.icon.bitmap.reset(); |
| 417 | dirty = DIRTY_BITMAP | DIRTY_HOTSPOT; |
| 418 | } else { |
| 419 | return; // setting to invalid icon and already invalid so nothing to do |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | invalidateLocked(dirty); |
| 423 | } |
| 424 | |
| 425 | void SpriteController::SpriteImpl::setVisible(bool visible) { |
| 426 | AutoMutex _l(mController->mLock); |
| 427 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 428 | if (mLocked.state.visible != visible) { |
| 429 | mLocked.state.visible = visible; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 430 | invalidateLocked(DIRTY_VISIBILITY); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | void SpriteController::SpriteImpl::setPosition(float x, float y) { |
| 435 | AutoMutex _l(mController->mLock); |
| 436 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 437 | if (mLocked.state.positionX != x || mLocked.state.positionY != y) { |
| 438 | mLocked.state.positionX = x; |
| 439 | mLocked.state.positionY = y; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 440 | invalidateLocked(DIRTY_POSITION); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void SpriteController::SpriteImpl::setLayer(int32_t layer) { |
| 445 | AutoMutex _l(mController->mLock); |
| 446 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 447 | if (mLocked.state.layer != layer) { |
| 448 | mLocked.state.layer = layer; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 449 | invalidateLocked(DIRTY_LAYER); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void SpriteController::SpriteImpl::setAlpha(float alpha) { |
| 454 | AutoMutex _l(mController->mLock); |
| 455 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 456 | if (mLocked.state.alpha != alpha) { |
| 457 | mLocked.state.alpha = alpha; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 458 | invalidateLocked(DIRTY_ALPHA); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void SpriteController::SpriteImpl::setTransformationMatrix( |
| 463 | const SpriteTransformationMatrix& matrix) { |
| 464 | AutoMutex _l(mController->mLock); |
| 465 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 466 | if (mLocked.state.transformationMatrix != matrix) { |
| 467 | mLocked.state.transformationMatrix = matrix; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 468 | invalidateLocked(DIRTY_TRANSFORMATION_MATRIX); |
| 469 | } |
| 470 | } |
| 471 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 472 | void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 473 | bool wasDirty = mLocked.state.dirty; |
| 474 | mLocked.state.dirty |= dirty; |
| 475 | |
| 476 | if (!wasDirty) { |
| 477 | mController->invalidateSpriteLocked(this); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
| 481 | } // namespace android |