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