blob: 6a32c5a71999ffa40424d5da9d7deef7457f2376 [file] [log] [blame]
Jeff Brown5541de92011-04-11 11:54:25 -07001/*
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 Brown5541de92011-04-11 11:54:25 -070018//#define LOG_NDEBUG 0
19
20#include "SpriteController.h"
21
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070022#include <log/log.h>
Jeff Brown5541de92011-04-11 11:54:25 -070023#include <utils/String8.h>
Mathias Agopian52800612013-02-14 17:11:20 -080024#include <gui/Surface.h>
Jeff Brown5541de92011-04-11 11:54:25 -070025
Jeff Brown5541de92011-04-11 11:54:25 -070026namespace android {
27
28// --- SpriteController ---
29
Prabir Pradhanfef0c612021-11-04 14:08:50 -070030SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer,
31 ParentSurfaceProvider parentSurfaceProvider)
32 : mLooper(looper),
33 mOverlayLayer(overlayLayer),
Prabir Pradhan4cc1a632023-06-09 21:31:26 +000034 mHandler(sp<Handler>::make()),
Prabir Pradhanfef0c612021-11-04 14:08:50 -070035 mParentSurfaceProvider(std::move(parentSurfaceProvider)) {
Jeff Brown2352b972011-04-12 22:39:53 -070036 mLocked.transactionNestingCount = 0;
37 mLocked.deferredSpriteUpdate = false;
Jeff Brown5541de92011-04-11 11:54:25 -070038}
39
Prabir Pradhan27c6d992023-08-18 19:44:55 +000040void 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 Pradhan4cc1a632023-06-09 21:31:26 +000044 mHandler->spriteController = controller;
45}
46
Jeff Brown5541de92011-04-11 11:54:25 -070047SpriteController::~SpriteController() {
48 mLooper->removeMessages(mHandler);
49
50 if (mSurfaceComposerClient != NULL) {
51 mSurfaceComposerClient->dispose();
52 mSurfaceComposerClient.clear();
53 }
54}
55
56sp<Sprite> SpriteController::createSprite() {
Prabir Pradhan27c6d992023-08-18 19:44:55 +000057 return sp<SpriteImpl>::make(*this);
Jeff Brown5541de92011-04-11 11:54:25 -070058}
59
Jeff Brown2352b972011-04-12 22:39:53 -070060void SpriteController::openTransaction() {
61 AutoMutex _l(mLock);
62
63 mLocked.transactionNestingCount += 1;
64}
65
66void 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 Pradhan4cc1a632023-06-09 21:31:26 +000075 mLooper->sendMessage(mHandler, Message(Handler::MSG_UPDATE_SPRITES));
Jeff Brown5541de92011-04-11 11:54:25 -070076 }
77}
78
Jeff Brown2352b972011-04-12 22:39:53 -070079void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) {
Prabir Pradhan3f8b2892021-11-18 08:55:02 -080080 bool wasEmpty = mLocked.invalidatedSprites.empty();
81 mLocked.invalidatedSprites.push_back(sprite);
Jeff Brown2352b972011-04-12 22:39:53 -070082 if (wasEmpty) {
83 if (mLocked.transactionNestingCount != 0) {
84 mLocked.deferredSpriteUpdate = true;
85 } else {
Prabir Pradhan4cc1a632023-06-09 21:31:26 +000086 mLooper->sendMessage(mHandler, Message(Handler::MSG_UPDATE_SPRITES));
Jeff Brown2352b972011-04-12 22:39:53 -070087 }
88 }
89}
90
Jeff Brown5541de92011-04-11 11:54:25 -070091void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
Prabir Pradhan3f8b2892021-11-18 08:55:02 -080092 bool wasEmpty = mLocked.disposedSurfaces.empty();
93 mLocked.disposedSurfaces.push_back(surfaceControl);
Jeff Brown5541de92011-04-11 11:54:25 -070094 if (wasEmpty) {
Prabir Pradhan4cc1a632023-06-09 21:31:26 +000095 mLooper->sendMessage(mHandler, Message(Handler::MSG_DISPOSE_SURFACES));
Jeff Brown5541de92011-04-11 11:54:25 -070096 }
97}
98
99void 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 Brown2352b972011-04-12 22:39:53 -0700110 numSprites = mLocked.invalidatedSprites.size();
Jeff Brown5541de92011-04-11 11:54:25 -0700111 for (size_t i = 0; i < numSprites; i++) {
Prabir Pradhan3f8b2892021-11-18 08:55:02 -0800112 const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites[i];
Jeff Brown5541de92011-04-11 11:54:25 -0700113
114 updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
115 sprite->resetDirtyLocked();
116 }
Jeff Brown2352b972011-04-12 22:39:53 -0700117 mLocked.invalidatedSprites.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700118 } // 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 Tan7e3457e2020-05-28 14:31:29 -0700126 update.state.surfaceWidth = update.state.icon.width();
127 update.state.surfaceHeight = update.state.icon.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700128 update.state.surfaceDrawn = false;
129 update.state.surfaceVisible = false;
Arthur Hung2b5cd172022-05-30 12:54:27 +0000130 update.state.surfaceControl =
131 obtainSurface(update.state.surfaceWidth, update.state.surfaceHeight,
132 update.state.displayId);
Jeff Brown5541de92011-04-11 11:54:25 -0700133 if (update.state.surfaceControl != NULL) {
134 update.surfaceChanged = surfaceChanged = true;
135 }
136 }
137 }
138
Arthur Hungb9b32002018-12-18 17:39:43 +0800139 // Resize and/or reparent sprites if needed.
Robert Carre13b58e2017-08-31 14:50:44 -0700140 SurfaceComposerClient::Transaction t;
141 bool needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700142 for (size_t i = 0; i < numSprites; i++) {
143 SpriteUpdate& update = updates.editItemAt(i);
Arthur Hungb9b32002018-12-18 17:39:43 +0800144 if (update.state.surfaceControl == nullptr) {
145 continue;
146 }
Jeff Brown5541de92011-04-11 11:54:25 -0700147
Arthur Hungb9b32002018-12-18 17:39:43 +0800148 if (update.state.wantSurfaceVisible()) {
Garfield Tan7e3457e2020-05-28 14:31:29 -0700149 int32_t desiredWidth = update.state.icon.width();
150 int32_t desiredHeight = update.state.icon.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700151 if (update.state.surfaceWidth < desiredWidth
152 || update.state.surfaceHeight < desiredHeight) {
Robert Carre13b58e2017-08-31 14:50:44 -0700153 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700154
Prabir Pradhan62726592021-08-09 15:51:25 +0000155 update.state.surfaceControl->updateDefaultBufferSize(desiredWidth, desiredHeight);
Robert Carre13b58e2017-08-31 14:50:44 -0700156 update.state.surfaceWidth = desiredWidth;
157 update.state.surfaceHeight = desiredHeight;
158 update.state.surfaceDrawn = false;
159 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700160
Robert Carre13b58e2017-08-31 14:50:44 -0700161 if (update.state.surfaceVisible) {
162 t.hide(update.state.surfaceControl);
163 update.state.surfaceVisible = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700164 }
165 }
166 }
Arthur Hungb9b32002018-12-18 17:39:43 +0800167
Arthur Hung2b5cd172022-05-30 12:54:27 +0000168 // If surface has changed to a new display, we have to reparent it.
169 if (update.state.dirty & DIRTY_DISPLAY_ID) {
Prabir Pradhanfef0c612021-11-04 14:08:50 -0700170 t.reparent(update.state.surfaceControl, mParentSurfaceProvider(update.state.displayId));
Arthur Hungb9b32002018-12-18 17:39:43 +0800171 needApplyTransaction = true;
172 }
Jeff Brown5541de92011-04-11 11:54:25 -0700173 }
Robert Carre13b58e2017-08-31 14:50:44 -0700174 if (needApplyTransaction) {
175 t.apply();
Jeff Brown5541de92011-04-11 11:54:25 -0700176 }
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 Tan7e3457e2020-05-28 14:31:29 -0700190 if (update.state.icon.draw(surface)) {
191 update.state.surfaceDrawn = true;
192 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700193 }
194 }
195 }
196
Robert Carre13b58e2017-08-31 14:50:44 -0700197 needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700198 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;
Pat Manningacbe18d2024-03-05 17:54:30 +0000205 if (update.state.surfaceControl != NULL &&
206 (becomingVisible || becomingHidden ||
207 (wantSurfaceVisibleAndDrawn &&
208 (update.state.dirty &
209 (DIRTY_ALPHA | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER |
210 DIRTY_VISIBILITY | DIRTY_HOTSPOT | DIRTY_DISPLAY_ID | DIRTY_ICON_STYLE |
211 DIRTY_DRAW_DROP_SHADOW))))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700212 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700213
214 if (wantSurfaceVisibleAndDrawn
215 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700216 t.setAlpha(update.state.surfaceControl,
217 update.state.alpha);
Jeff Brown5541de92011-04-11 11:54:25 -0700218 }
219
220 if (wantSurfaceVisibleAndDrawn
221 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
222 | DIRTY_HOTSPOT)))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700223 t.setPosition(
224 update.state.surfaceControl,
Jeff Brown2352b972011-04-12 22:39:53 -0700225 update.state.positionX - update.state.icon.hotSpotX,
226 update.state.positionY - update.state.icon.hotSpotY);
Jeff Brown5541de92011-04-11 11:54:25 -0700227 }
228
229 if (wantSurfaceVisibleAndDrawn
230 && (becomingVisible
231 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700232 t.setMatrix(
233 update.state.surfaceControl,
Jeff Brown5541de92011-04-11 11:54:25 -0700234 update.state.transformationMatrix.dsdx,
235 update.state.transformationMatrix.dtdx,
236 update.state.transformationMatrix.dsdy,
237 update.state.transformationMatrix.dtdy);
Jeff Brown5541de92011-04-11 11:54:25 -0700238 }
239
Pat Manningacbe18d2024-03-05 17:54:30 +0000240 if (wantSurfaceVisibleAndDrawn &&
241 (becomingVisible ||
242 (update.state.dirty &
243 (DIRTY_HOTSPOT | DIRTY_ICON_STYLE | DIRTY_DRAW_DROP_SHADOW)))) {
Garfield Tan67e479a2019-08-05 16:47:40 -0700244 Parcel p;
Brandon Pollack015f5d92022-06-02 06:59:33 +0000245 p.writeInt32(static_cast<int32_t>(update.state.icon.style));
Garfield Tan67e479a2019-08-05 16:47:40 -0700246 p.writeFloat(update.state.icon.hotSpotX);
247 p.writeFloat(update.state.icon.hotSpotY);
Pat Manningacbe18d2024-03-05 17:54:30 +0000248 p.writeBool(update.state.icon.drawNativeDropShadow);
Garfield Tan67e479a2019-08-05 16:47:40 -0700249
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 Luo36b55bc2022-03-08 14:50:45 -0800253 t.setMetadata(update.state.surfaceControl, gui::METADATA_MOUSE_CURSOR, p);
Garfield Tan67e479a2019-08-05 16:47:40 -0700254 }
255
Jeff Brown5541de92011-04-11 11:54:25 -0700256 int32_t surfaceLayer = mOverlayLayer + update.state.layer;
257 if (wantSurfaceVisibleAndDrawn
258 && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700259 t.setLayer(update.state.surfaceControl, surfaceLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700260 }
261
262 if (becomingVisible) {
Robert Carre13b58e2017-08-31 14:50:44 -0700263 t.show(update.state.surfaceControl);
264
265 update.state.surfaceVisible = true;
266 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700267 } else if (becomingHidden) {
Robert Carre13b58e2017-08-31 14:50:44 -0700268 t.hide(update.state.surfaceControl);
269
270 update.state.surfaceVisible = false;
271 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700272 }
273 }
274 }
275
Robert Carre13b58e2017-08-31 14:50:44 -0700276 if (needApplyTransaction) {
277 status_t status = t.apply();
278 if (status) {
279 ALOGE("Error applying Surface transaction");
280 }
Jeff Brown5541de92011-04-11 11:54:25 -0700281 }
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
306void SpriteController::doDisposeSurfaces() {
307 // Collect disposed surfaces.
Prabir Pradhan3f8b2892021-11-18 08:55:02 -0800308 std::vector<sp<SurfaceControl>> disposedSurfaces;
Jeff Brown5541de92011-04-11 11:54:25 -0700309 { // acquire lock
Jeff Brown2352b972011-04-12 22:39:53 -0700310 AutoMutex _l(mLock);
311
312 disposedSurfaces = mLocked.disposedSurfaces;
313 mLocked.disposedSurfaces.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700314 } // release lock
315
Prabir Pradhan3f8b2892021-11-18 08:55:02 -0800316 // 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 Brown5541de92011-04-11 11:54:25 -0700323 // 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
328void SpriteController::ensureSurfaceComposerClient() {
329 if (mSurfaceComposerClient == NULL) {
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000330 mSurfaceComposerClient = sp<SurfaceComposerClient>::make();
Jeff Brown5541de92011-04-11 11:54:25 -0700331 }
332}
333
Arthur Hung2b5cd172022-05-30 12:54:27 +0000334sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height,
335 int32_t displayId) {
Jeff Brown5541de92011-04-11 11:54:25 -0700336 ensureSurfaceComposerClient();
337
Arthur Hung2b5cd172022-05-30 12:54:27 +0000338 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 Block3762c312012-01-06 19:20:56 +0000350 ALOGE("Error creating sprite surface.");
Arthur Hung2b5cd172022-05-30 12:54:27 +0000351 return nullptr;
Jeff Brown5541de92011-04-11 11:54:25 -0700352 }
353 return surfaceControl;
354}
355
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000356// --- SpriteController::Handler ---
357
358void SpriteController::Handler::handleMessage(const android::Message& message) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000359 auto controller = spriteController.lock();
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000360 if (!controller) {
361 return;
362 }
363
364 switch (message.what) {
365 case MSG_UPDATE_SPRITES:
366 controller->doUpdateSprites();
367 break;
368 case MSG_DISPOSE_SURFACES:
369 controller->doDisposeSurfaces();
370 break;
371 }
372}
373
Jeff Brown5541de92011-04-11 11:54:25 -0700374// --- SpriteController::SpriteImpl ---
375
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000376SpriteController::SpriteImpl::SpriteImpl(SpriteController& controller) : mController(controller) {}
Jeff Brown5541de92011-04-11 11:54:25 -0700377
378SpriteController::SpriteImpl::~SpriteImpl() {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000379 AutoMutex _m(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700380
381 // Let the controller take care of deleting the last reference to sprite
382 // surfaces so that we do not block the caller on an IPC here.
Jeff Brown2352b972011-04-12 22:39:53 -0700383 if (mLocked.state.surfaceControl != NULL) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000384 mController.disposeSurfaceLocked(mLocked.state.surfaceControl);
Jeff Brown2352b972011-04-12 22:39:53 -0700385 mLocked.state.surfaceControl.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700386 }
387}
388
Jeff Brown2352b972011-04-12 22:39:53 -0700389void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000390 AutoMutex _l(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700391
Jeff Brown2352b972011-04-12 22:39:53 -0700392 uint32_t dirty;
393 if (icon.isValid()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400394 mLocked.state.icon.bitmap = icon.bitmap.copy(ANDROID_BITMAP_FORMAT_RGBA_8888);
Pat Manningacbe18d2024-03-05 17:54:30 +0000395 if (!mLocked.state.icon.isValid() || mLocked.state.icon.hotSpotX != icon.hotSpotX ||
396 mLocked.state.icon.hotSpotY != icon.hotSpotY ||
397 mLocked.state.icon.drawNativeDropShadow != icon.drawNativeDropShadow) {
Jeff Brown2352b972011-04-12 22:39:53 -0700398 mLocked.state.icon.hotSpotX = icon.hotSpotX;
399 mLocked.state.icon.hotSpotY = icon.hotSpotY;
Pat Manningacbe18d2024-03-05 17:54:30 +0000400 mLocked.state.icon.drawNativeDropShadow = icon.drawNativeDropShadow;
401 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT | DIRTY_DRAW_DROP_SHADOW;
Jeff Brown2352b972011-04-12 22:39:53 -0700402 } else {
403 dirty = DIRTY_BITMAP;
404 }
Garfield Tan67e479a2019-08-05 16:47:40 -0700405
406 if (mLocked.state.icon.style != icon.style) {
407 mLocked.state.icon.style = icon.style;
408 dirty |= DIRTY_ICON_STYLE;
409 }
Jeff Brown2352b972011-04-12 22:39:53 -0700410 } else if (mLocked.state.icon.isValid()) {
411 mLocked.state.icon.bitmap.reset();
Pat Manningacbe18d2024-03-05 17:54:30 +0000412 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT | DIRTY_ICON_STYLE | DIRTY_DRAW_DROP_SHADOW;
Jeff Brown2352b972011-04-12 22:39:53 -0700413 } else {
414 return; // setting to invalid icon and already invalid so nothing to do
Jeff Brown5541de92011-04-11 11:54:25 -0700415 }
416
417 invalidateLocked(dirty);
418}
419
420void SpriteController::SpriteImpl::setVisible(bool visible) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000421 AutoMutex _l(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700422
Jeff Brown2352b972011-04-12 22:39:53 -0700423 if (mLocked.state.visible != visible) {
424 mLocked.state.visible = visible;
Jeff Brown5541de92011-04-11 11:54:25 -0700425 invalidateLocked(DIRTY_VISIBILITY);
426 }
427}
428
429void SpriteController::SpriteImpl::setPosition(float x, float y) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000430 AutoMutex _l(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700431
Jeff Brown2352b972011-04-12 22:39:53 -0700432 if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
433 mLocked.state.positionX = x;
434 mLocked.state.positionY = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700435 invalidateLocked(DIRTY_POSITION);
436 }
437}
438
439void SpriteController::SpriteImpl::setLayer(int32_t layer) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000440 AutoMutex _l(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700441
Jeff Brown2352b972011-04-12 22:39:53 -0700442 if (mLocked.state.layer != layer) {
443 mLocked.state.layer = layer;
Jeff Brown5541de92011-04-11 11:54:25 -0700444 invalidateLocked(DIRTY_LAYER);
445 }
446}
447
448void SpriteController::SpriteImpl::setAlpha(float alpha) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000449 AutoMutex _l(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700450
Jeff Brown2352b972011-04-12 22:39:53 -0700451 if (mLocked.state.alpha != alpha) {
452 mLocked.state.alpha = alpha;
Jeff Brown5541de92011-04-11 11:54:25 -0700453 invalidateLocked(DIRTY_ALPHA);
454 }
455}
456
457void SpriteController::SpriteImpl::setTransformationMatrix(
458 const SpriteTransformationMatrix& matrix) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000459 AutoMutex _l(mController.mLock);
Jeff Brown5541de92011-04-11 11:54:25 -0700460
Jeff Brown2352b972011-04-12 22:39:53 -0700461 if (mLocked.state.transformationMatrix != matrix) {
462 mLocked.state.transformationMatrix = matrix;
Jeff Brown5541de92011-04-11 11:54:25 -0700463 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
464 }
465}
466
Arthur Hungb9b32002018-12-18 17:39:43 +0800467void SpriteController::SpriteImpl::setDisplayId(int32_t displayId) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000468 AutoMutex _l(mController.mLock);
Arthur Hungb9b32002018-12-18 17:39:43 +0800469
470 if (mLocked.state.displayId != displayId) {
471 mLocked.state.displayId = displayId;
472 invalidateLocked(DIRTY_DISPLAY_ID);
473 }
474}
475
Jeff Brown5541de92011-04-11 11:54:25 -0700476void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
Jeff Brown2352b972011-04-12 22:39:53 -0700477 bool wasDirty = mLocked.state.dirty;
478 mLocked.state.dirty |= dirty;
479
480 if (!wasDirty) {
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000481 mController.invalidateSpriteLocked(sp<SpriteImpl>::fromExisting(this));
Jeff Brown5541de92011-04-11 11:54:25 -0700482 }
483}
484
485} // namespace android