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