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 | |
Prabir Pradhan | 6272659 | 2021-08-09 15:51:25 +0000 | [diff] [blame^] | 156 | update.state.surfaceControl->updateDefaultBufferSize(desiredWidth, desiredHeight); |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 157 | update.state.surfaceWidth = desiredWidth; |
| 158 | update.state.surfaceHeight = desiredHeight; |
| 159 | update.state.surfaceDrawn = false; |
| 160 | update.surfaceChanged = surfaceChanged = true; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 161 | |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 162 | if (update.state.surfaceVisible) { |
| 163 | t.hide(update.state.surfaceControl); |
| 164 | update.state.surfaceVisible = false; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 168 | |
| 169 | // If surface is a new one, we have to set right layer stack. |
| 170 | if (update.surfaceChanged || update.state.dirty & DIRTY_DISPLAY_ID) { |
| 171 | t.setLayerStack(update.state.surfaceControl, update.state.displayId); |
| 172 | needApplyTransaction = true; |
| 173 | } |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 174 | } |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 175 | if (needApplyTransaction) { |
| 176 | t.apply(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Redraw sprites if needed. |
| 180 | for (size_t i = 0; i < numSprites; i++) { |
| 181 | SpriteUpdate& update = updates.editItemAt(i); |
| 182 | |
| 183 | if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) { |
| 184 | update.state.surfaceDrawn = false; |
| 185 | update.surfaceChanged = surfaceChanged = true; |
| 186 | } |
| 187 | |
| 188 | if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn |
| 189 | && update.state.wantSurfaceVisible()) { |
| 190 | sp<Surface> surface = update.state.surfaceControl->getSurface(); |
Garfield Tan | 7e3457e | 2020-05-28 14:31:29 -0700 | [diff] [blame] | 191 | if (update.state.icon.draw(surface)) { |
| 192 | update.state.surfaceDrawn = true; |
| 193 | update.surfaceChanged = surfaceChanged = true; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 198 | needApplyTransaction = false; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 199 | for (size_t i = 0; i < numSprites; i++) { |
| 200 | SpriteUpdate& update = updates.editItemAt(i); |
| 201 | |
| 202 | bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible() |
| 203 | && update.state.surfaceDrawn; |
| 204 | bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible; |
| 205 | bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible; |
| 206 | if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden |
| 207 | || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA |
| 208 | | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER |
Garfield Tan | 67e479a | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 209 | | DIRTY_VISIBILITY | DIRTY_HOTSPOT | DIRTY_DISPLAY_ID |
| 210 | | DIRTY_ICON_STYLE))))) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 211 | needApplyTransaction = true; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 212 | |
| 213 | if (wantSurfaceVisibleAndDrawn |
| 214 | && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 215 | t.setAlpha(update.state.surfaceControl, |
| 216 | update.state.alpha); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (wantSurfaceVisibleAndDrawn |
| 220 | && (becomingVisible || (update.state.dirty & (DIRTY_POSITION |
| 221 | | DIRTY_HOTSPOT)))) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 222 | t.setPosition( |
| 223 | update.state.surfaceControl, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 224 | update.state.positionX - update.state.icon.hotSpotX, |
| 225 | update.state.positionY - update.state.icon.hotSpotY); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (wantSurfaceVisibleAndDrawn |
| 229 | && (becomingVisible |
| 230 | || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 231 | t.setMatrix( |
| 232 | update.state.surfaceControl, |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 233 | update.state.transformationMatrix.dsdx, |
| 234 | update.state.transformationMatrix.dtdx, |
| 235 | update.state.transformationMatrix.dsdy, |
| 236 | update.state.transformationMatrix.dtdy); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Garfield Tan | 67e479a | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 239 | if (wantSurfaceVisibleAndDrawn |
| 240 | && (becomingVisible |
| 241 | || (update.state.dirty & (DIRTY_HOTSPOT | DIRTY_ICON_STYLE)))) { |
| 242 | Parcel p; |
| 243 | p.writeInt32(update.state.icon.style); |
| 244 | p.writeFloat(update.state.icon.hotSpotX); |
| 245 | p.writeFloat(update.state.icon.hotSpotY); |
| 246 | |
| 247 | // Pass cursor metadata in the sprite surface so that when Android is running as a |
| 248 | // client OS (e.g. ARC++) the host OS can get the requested cursor metadata and |
| 249 | // update mouse cursor in the host OS. |
| 250 | t.setMetadata( |
| 251 | update.state.surfaceControl, METADATA_MOUSE_CURSOR, p); |
| 252 | } |
| 253 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 254 | int32_t surfaceLayer = mOverlayLayer + update.state.layer; |
| 255 | if (wantSurfaceVisibleAndDrawn |
| 256 | && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 257 | t.setLayer(update.state.surfaceControl, surfaceLayer); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (becomingVisible) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 261 | t.show(update.state.surfaceControl); |
| 262 | |
| 263 | update.state.surfaceVisible = true; |
| 264 | update.surfaceChanged = surfaceChanged = true; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 265 | } else if (becomingHidden) { |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 266 | t.hide(update.state.surfaceControl); |
| 267 | |
| 268 | update.state.surfaceVisible = false; |
| 269 | update.surfaceChanged = surfaceChanged = true; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 274 | if (needApplyTransaction) { |
| 275 | status_t status = t.apply(); |
| 276 | if (status) { |
| 277 | ALOGE("Error applying Surface transaction"); |
| 278 | } |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // If any surfaces were changed, write back the new surface properties to the sprites. |
| 282 | if (surfaceChanged) { // acquire lock |
| 283 | AutoMutex _l(mLock); |
| 284 | |
| 285 | for (size_t i = 0; i < numSprites; i++) { |
| 286 | const SpriteUpdate& update = updates.itemAt(i); |
| 287 | |
| 288 | if (update.surfaceChanged) { |
| 289 | update.sprite->setSurfaceLocked(update.state.surfaceControl, |
| 290 | update.state.surfaceWidth, update.state.surfaceHeight, |
| 291 | update.state.surfaceDrawn, update.state.surfaceVisible); |
| 292 | } |
| 293 | } |
| 294 | } // release lock |
| 295 | |
| 296 | // Clear the sprite update vector outside the lock. It is very important that |
| 297 | // we do not clear sprite references inside the lock since we could be releasing |
| 298 | // the last remaining reference to the sprite here which would result in the |
| 299 | // sprite being deleted and the lock being reacquired by the sprite destructor |
| 300 | // while already held. |
| 301 | updates.clear(); |
| 302 | } |
| 303 | |
| 304 | void SpriteController::doDisposeSurfaces() { |
| 305 | // Collect disposed surfaces. |
| 306 | Vector<sp<SurfaceControl> > disposedSurfaces; |
| 307 | { // acquire lock |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 308 | AutoMutex _l(mLock); |
| 309 | |
| 310 | disposedSurfaces = mLocked.disposedSurfaces; |
| 311 | mLocked.disposedSurfaces.clear(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 312 | } // release lock |
| 313 | |
| 314 | // Release the last reference to each surface outside of the lock. |
| 315 | // We don't want the surfaces to be deleted while we are holding our lock. |
| 316 | disposedSurfaces.clear(); |
| 317 | } |
| 318 | |
| 319 | void SpriteController::ensureSurfaceComposerClient() { |
| 320 | if (mSurfaceComposerClient == NULL) { |
| 321 | mSurfaceComposerClient = new SurfaceComposerClient(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) { |
| 326 | ensureSurfaceComposerClient(); |
| 327 | |
| 328 | sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface( |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 329 | String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888, |
Riley Andrews | 68eccda | 2014-07-07 11:47:35 -0700 | [diff] [blame] | 330 | ISurfaceComposerClient::eHidden | |
| 331 | ISurfaceComposerClient::eCursorWindow); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 332 | if (surfaceControl == NULL || !surfaceControl->isValid()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 333 | ALOGE("Error creating sprite surface."); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
| 336 | return surfaceControl; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // --- SpriteController::SpriteImpl --- |
| 341 | |
| 342 | SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) : |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 343 | mController(controller) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | SpriteController::SpriteImpl::~SpriteImpl() { |
| 347 | AutoMutex _m(mController->mLock); |
| 348 | |
| 349 | // Let the controller take care of deleting the last reference to sprite |
| 350 | // 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] | 351 | if (mLocked.state.surfaceControl != NULL) { |
| 352 | mController->disposeSurfaceLocked(mLocked.state.surfaceControl); |
| 353 | mLocked.state.surfaceControl.clear(); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 357 | void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 358 | AutoMutex _l(mController->mLock); |
| 359 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 360 | uint32_t dirty; |
| 361 | if (icon.isValid()) { |
Derek Sollenberger | 9ca5bbe | 2019-08-14 15:50:59 -0400 | [diff] [blame] | 362 | mLocked.state.icon.bitmap = icon.bitmap.copy(ANDROID_BITMAP_FORMAT_RGBA_8888); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 363 | if (!mLocked.state.icon.isValid() |
| 364 | || mLocked.state.icon.hotSpotX != icon.hotSpotX |
| 365 | || mLocked.state.icon.hotSpotY != icon.hotSpotY) { |
| 366 | mLocked.state.icon.hotSpotX = icon.hotSpotX; |
| 367 | mLocked.state.icon.hotSpotY = icon.hotSpotY; |
| 368 | dirty = DIRTY_BITMAP | DIRTY_HOTSPOT; |
| 369 | } else { |
| 370 | dirty = DIRTY_BITMAP; |
| 371 | } |
Garfield Tan | 67e479a | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 372 | |
| 373 | if (mLocked.state.icon.style != icon.style) { |
| 374 | mLocked.state.icon.style = icon.style; |
| 375 | dirty |= DIRTY_ICON_STYLE; |
| 376 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 377 | } else if (mLocked.state.icon.isValid()) { |
| 378 | mLocked.state.icon.bitmap.reset(); |
Garfield Tan | 67e479a | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 379 | dirty = DIRTY_BITMAP | DIRTY_HOTSPOT | DIRTY_ICON_STYLE; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 380 | } else { |
| 381 | return; // setting to invalid icon and already invalid so nothing to do |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | invalidateLocked(dirty); |
| 385 | } |
| 386 | |
| 387 | void SpriteController::SpriteImpl::setVisible(bool visible) { |
| 388 | AutoMutex _l(mController->mLock); |
| 389 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 390 | if (mLocked.state.visible != visible) { |
| 391 | mLocked.state.visible = visible; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 392 | invalidateLocked(DIRTY_VISIBILITY); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void SpriteController::SpriteImpl::setPosition(float x, float y) { |
| 397 | AutoMutex _l(mController->mLock); |
| 398 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 399 | if (mLocked.state.positionX != x || mLocked.state.positionY != y) { |
| 400 | mLocked.state.positionX = x; |
| 401 | mLocked.state.positionY = y; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 402 | invalidateLocked(DIRTY_POSITION); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void SpriteController::SpriteImpl::setLayer(int32_t layer) { |
| 407 | AutoMutex _l(mController->mLock); |
| 408 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 409 | if (mLocked.state.layer != layer) { |
| 410 | mLocked.state.layer = layer; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 411 | invalidateLocked(DIRTY_LAYER); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void SpriteController::SpriteImpl::setAlpha(float alpha) { |
| 416 | AutoMutex _l(mController->mLock); |
| 417 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 418 | if (mLocked.state.alpha != alpha) { |
| 419 | mLocked.state.alpha = alpha; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 420 | invalidateLocked(DIRTY_ALPHA); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void SpriteController::SpriteImpl::setTransformationMatrix( |
| 425 | const SpriteTransformationMatrix& matrix) { |
| 426 | AutoMutex _l(mController->mLock); |
| 427 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 428 | if (mLocked.state.transformationMatrix != matrix) { |
| 429 | mLocked.state.transformationMatrix = matrix; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 430 | invalidateLocked(DIRTY_TRANSFORMATION_MATRIX); |
| 431 | } |
| 432 | } |
| 433 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 434 | void SpriteController::SpriteImpl::setDisplayId(int32_t displayId) { |
| 435 | AutoMutex _l(mController->mLock); |
| 436 | |
| 437 | if (mLocked.state.displayId != displayId) { |
| 438 | mLocked.state.displayId = displayId; |
| 439 | invalidateLocked(DIRTY_DISPLAY_ID); |
| 440 | } |
| 441 | } |
| 442 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 443 | void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 444 | bool wasDirty = mLocked.state.dirty; |
| 445 | mLocked.state.dirty |= dirty; |
| 446 | |
| 447 | if (!wasDirty) { |
| 448 | mController->invalidateSpriteLocked(this); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | } // namespace android |