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