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