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