blob: 12c6a32bd0ad79945ca7d361f0034b1d4f2c527d [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
30SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) :
31 mLooper(looper), mOverlayLayer(overlayLayer) {
32 mHandler = new WeakMessageHandler(this);
Jeff Brown2352b972011-04-12 22:39:53 -070033
34 mLocked.transactionNestingCount = 0;
35 mLocked.deferredSpriteUpdate = false;
Jeff Brown5541de92011-04-11 11:54:25 -070036}
37
38SpriteController::~SpriteController() {
39 mLooper->removeMessages(mHandler);
40
41 if (mSurfaceComposerClient != NULL) {
42 mSurfaceComposerClient->dispose();
43 mSurfaceComposerClient.clear();
44 }
45}
46
47sp<Sprite> SpriteController::createSprite() {
48 return new SpriteImpl(this);
49}
50
Jeff Brown2352b972011-04-12 22:39:53 -070051void SpriteController::openTransaction() {
52 AutoMutex _l(mLock);
53
54 mLocked.transactionNestingCount += 1;
55}
56
57void 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 Brown5541de92011-04-11 11:54:25 -070066 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
67 }
68}
69
Jeff Brown2352b972011-04-12 22:39:53 -070070void 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 Brown5541de92011-04-11 11:54:25 -070082void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
Jeff Brown2352b972011-04-12 22:39:53 -070083 bool wasEmpty = mLocked.disposedSurfaces.isEmpty();
84 mLocked.disposedSurfaces.push(surfaceControl);
Jeff Brown5541de92011-04-11 11:54:25 -070085 if (wasEmpty) {
86 mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
87 }
88}
89
90void 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
101void 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 Brown2352b972011-04-12 22:39:53 -0700112 numSprites = mLocked.invalidatedSprites.size();
Jeff Brown5541de92011-04-11 11:54:25 -0700113 for (size_t i = 0; i < numSprites; i++) {
Jeff Brown2352b972011-04-12 22:39:53 -0700114 const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i);
Jeff Brown5541de92011-04-11 11:54:25 -0700115
116 updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
117 sprite->resetDirtyLocked();
118 }
Jeff Brown2352b972011-04-12 22:39:53 -0700119 mLocked.invalidatedSprites.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700120 } // 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 Tan7e3457e2020-05-28 14:31:29 -0700128 update.state.surfaceWidth = update.state.icon.width();
129 update.state.surfaceHeight = update.state.icon.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700130 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 Hungb9b32002018-12-18 17:39:43 +0800140 // Resize and/or reparent sprites if needed.
Robert Carre13b58e2017-08-31 14:50:44 -0700141 SurfaceComposerClient::Transaction t;
142 bool needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700143 for (size_t i = 0; i < numSprites; i++) {
144 SpriteUpdate& update = updates.editItemAt(i);
Arthur Hungb9b32002018-12-18 17:39:43 +0800145 if (update.state.surfaceControl == nullptr) {
146 continue;
147 }
Jeff Brown5541de92011-04-11 11:54:25 -0700148
Arthur Hungb9b32002018-12-18 17:39:43 +0800149 if (update.state.wantSurfaceVisible()) {
Garfield Tan7e3457e2020-05-28 14:31:29 -0700150 int32_t desiredWidth = update.state.icon.width();
151 int32_t desiredHeight = update.state.icon.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700152 if (update.state.surfaceWidth < desiredWidth
153 || update.state.surfaceHeight < desiredHeight) {
Robert Carre13b58e2017-08-31 14:50:44 -0700154 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700155
Robert Carre13b58e2017-08-31 14:50:44 -0700156 t.setSize(update.state.surfaceControl,
157 desiredWidth, desiredHeight);
158 update.state.surfaceWidth = desiredWidth;
159 update.state.surfaceHeight = desiredHeight;
160 update.state.surfaceDrawn = false;
161 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700162
Robert Carre13b58e2017-08-31 14:50:44 -0700163 if (update.state.surfaceVisible) {
164 t.hide(update.state.surfaceControl);
165 update.state.surfaceVisible = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700166 }
167 }
168 }
Arthur Hungb9b32002018-12-18 17:39:43 +0800169
170 // If surface is a new one, we have to set right layer stack.
171 if (update.surfaceChanged || update.state.dirty & DIRTY_DISPLAY_ID) {
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700172 t.setLayerStack(update.state.surfaceControl,
173 ui::LayerStack::fromValue(update.state.displayId));
Arthur Hungb9b32002018-12-18 17:39:43 +0800174 needApplyTransaction = true;
175 }
Jeff Brown5541de92011-04-11 11:54:25 -0700176 }
Robert Carre13b58e2017-08-31 14:50:44 -0700177 if (needApplyTransaction) {
178 t.apply();
Jeff Brown5541de92011-04-11 11:54:25 -0700179 }
180
181 // Redraw sprites if needed.
182 for (size_t i = 0; i < numSprites; i++) {
183 SpriteUpdate& update = updates.editItemAt(i);
184
185 if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
186 update.state.surfaceDrawn = false;
187 update.surfaceChanged = surfaceChanged = true;
188 }
189
190 if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
191 && update.state.wantSurfaceVisible()) {
192 sp<Surface> surface = update.state.surfaceControl->getSurface();
Garfield Tan7e3457e2020-05-28 14:31:29 -0700193 if (update.state.icon.draw(surface)) {
194 update.state.surfaceDrawn = true;
195 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700196 }
197 }
198 }
199
Robert Carre13b58e2017-08-31 14:50:44 -0700200 needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700201 for (size_t i = 0; i < numSprites; i++) {
202 SpriteUpdate& update = updates.editItemAt(i);
203
204 bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible()
205 && update.state.surfaceDrawn;
206 bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible;
207 bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible;
208 if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
209 || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
210 | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
Garfield Tan67e479a2019-08-05 16:47:40 -0700211 | DIRTY_VISIBILITY | DIRTY_HOTSPOT | DIRTY_DISPLAY_ID
212 | DIRTY_ICON_STYLE))))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700213 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700214
215 if (wantSurfaceVisibleAndDrawn
216 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700217 t.setAlpha(update.state.surfaceControl,
218 update.state.alpha);
Jeff Brown5541de92011-04-11 11:54:25 -0700219 }
220
221 if (wantSurfaceVisibleAndDrawn
222 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
223 | DIRTY_HOTSPOT)))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700224 t.setPosition(
225 update.state.surfaceControl,
Jeff Brown2352b972011-04-12 22:39:53 -0700226 update.state.positionX - update.state.icon.hotSpotX,
227 update.state.positionY - update.state.icon.hotSpotY);
Jeff Brown5541de92011-04-11 11:54:25 -0700228 }
229
230 if (wantSurfaceVisibleAndDrawn
231 && (becomingVisible
232 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700233 t.setMatrix(
234 update.state.surfaceControl,
Jeff Brown5541de92011-04-11 11:54:25 -0700235 update.state.transformationMatrix.dsdx,
236 update.state.transformationMatrix.dtdx,
237 update.state.transformationMatrix.dsdy,
238 update.state.transformationMatrix.dtdy);
Jeff Brown5541de92011-04-11 11:54:25 -0700239 }
240
Garfield Tan67e479a2019-08-05 16:47:40 -0700241 if (wantSurfaceVisibleAndDrawn
242 && (becomingVisible
243 || (update.state.dirty & (DIRTY_HOTSPOT | DIRTY_ICON_STYLE)))) {
244 Parcel p;
245 p.writeInt32(update.state.icon.style);
246 p.writeFloat(update.state.icon.hotSpotX);
247 p.writeFloat(update.state.icon.hotSpotY);
248
249 // Pass cursor metadata in the sprite surface so that when Android is running as a
250 // client OS (e.g. ARC++) the host OS can get the requested cursor metadata and
251 // update mouse cursor in the host OS.
252 t.setMetadata(
253 update.state.surfaceControl, METADATA_MOUSE_CURSOR, p);
254 }
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.
308 Vector<sp<SurfaceControl> > disposedSurfaces;
309 { // 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
316 // Release the last reference to each surface outside of the lock.
317 // We don't want the surfaces to be deleted while we are holding our lock.
318 disposedSurfaces.clear();
319}
320
321void SpriteController::ensureSurfaceComposerClient() {
322 if (mSurfaceComposerClient == NULL) {
323 mSurfaceComposerClient = new SurfaceComposerClient();
324 }
325}
326
327sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
328 ensureSurfaceComposerClient();
329
330 sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
Jeff Brown64a55af2012-08-26 02:47:39 -0700331 String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888,
Riley Andrews68eccda2014-07-07 11:47:35 -0700332 ISurfaceComposerClient::eHidden |
333 ISurfaceComposerClient::eCursorWindow);
Mathias Agopian52800612013-02-14 17:11:20 -0800334 if (surfaceControl == NULL || !surfaceControl->isValid()) {
Steve Block3762c312012-01-06 19:20:56 +0000335 ALOGE("Error creating sprite surface.");
Jeff Brown5541de92011-04-11 11:54:25 -0700336 return NULL;
337 }
338 return surfaceControl;
339}
340
341
342// --- SpriteController::SpriteImpl ---
343
344SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
Jeff Brown2352b972011-04-12 22:39:53 -0700345 mController(controller) {
Jeff Brown5541de92011-04-11 11:54:25 -0700346}
347
348SpriteController::SpriteImpl::~SpriteImpl() {
349 AutoMutex _m(mController->mLock);
350
351 // Let the controller take care of deleting the last reference to sprite
352 // surfaces so that we do not block the caller on an IPC here.
Jeff Brown2352b972011-04-12 22:39:53 -0700353 if (mLocked.state.surfaceControl != NULL) {
354 mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
355 mLocked.state.surfaceControl.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700356 }
357}
358
Jeff Brown2352b972011-04-12 22:39:53 -0700359void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
Jeff Brown5541de92011-04-11 11:54:25 -0700360 AutoMutex _l(mController->mLock);
361
Jeff Brown2352b972011-04-12 22:39:53 -0700362 uint32_t dirty;
363 if (icon.isValid()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400364 mLocked.state.icon.bitmap = icon.bitmap.copy(ANDROID_BITMAP_FORMAT_RGBA_8888);
Jeff Brown2352b972011-04-12 22:39:53 -0700365 if (!mLocked.state.icon.isValid()
366 || mLocked.state.icon.hotSpotX != icon.hotSpotX
367 || mLocked.state.icon.hotSpotY != icon.hotSpotY) {
368 mLocked.state.icon.hotSpotX = icon.hotSpotX;
369 mLocked.state.icon.hotSpotY = icon.hotSpotY;
370 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
371 } else {
372 dirty = DIRTY_BITMAP;
373 }
Garfield Tan67e479a2019-08-05 16:47:40 -0700374
375 if (mLocked.state.icon.style != icon.style) {
376 mLocked.state.icon.style = icon.style;
377 dirty |= DIRTY_ICON_STYLE;
378 }
Jeff Brown2352b972011-04-12 22:39:53 -0700379 } else if (mLocked.state.icon.isValid()) {
380 mLocked.state.icon.bitmap.reset();
Garfield Tan67e479a2019-08-05 16:47:40 -0700381 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT | DIRTY_ICON_STYLE;
Jeff Brown2352b972011-04-12 22:39:53 -0700382 } else {
383 return; // setting to invalid icon and already invalid so nothing to do
Jeff Brown5541de92011-04-11 11:54:25 -0700384 }
385
386 invalidateLocked(dirty);
387}
388
389void SpriteController::SpriteImpl::setVisible(bool visible) {
390 AutoMutex _l(mController->mLock);
391
Jeff Brown2352b972011-04-12 22:39:53 -0700392 if (mLocked.state.visible != visible) {
393 mLocked.state.visible = visible;
Jeff Brown5541de92011-04-11 11:54:25 -0700394 invalidateLocked(DIRTY_VISIBILITY);
395 }
396}
397
398void SpriteController::SpriteImpl::setPosition(float x, float y) {
399 AutoMutex _l(mController->mLock);
400
Jeff Brown2352b972011-04-12 22:39:53 -0700401 if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
402 mLocked.state.positionX = x;
403 mLocked.state.positionY = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700404 invalidateLocked(DIRTY_POSITION);
405 }
406}
407
408void SpriteController::SpriteImpl::setLayer(int32_t layer) {
409 AutoMutex _l(mController->mLock);
410
Jeff Brown2352b972011-04-12 22:39:53 -0700411 if (mLocked.state.layer != layer) {
412 mLocked.state.layer = layer;
Jeff Brown5541de92011-04-11 11:54:25 -0700413 invalidateLocked(DIRTY_LAYER);
414 }
415}
416
417void SpriteController::SpriteImpl::setAlpha(float alpha) {
418 AutoMutex _l(mController->mLock);
419
Jeff Brown2352b972011-04-12 22:39:53 -0700420 if (mLocked.state.alpha != alpha) {
421 mLocked.state.alpha = alpha;
Jeff Brown5541de92011-04-11 11:54:25 -0700422 invalidateLocked(DIRTY_ALPHA);
423 }
424}
425
426void SpriteController::SpriteImpl::setTransformationMatrix(
427 const SpriteTransformationMatrix& matrix) {
428 AutoMutex _l(mController->mLock);
429
Jeff Brown2352b972011-04-12 22:39:53 -0700430 if (mLocked.state.transformationMatrix != matrix) {
431 mLocked.state.transformationMatrix = matrix;
Jeff Brown5541de92011-04-11 11:54:25 -0700432 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
433 }
434}
435
Arthur Hungb9b32002018-12-18 17:39:43 +0800436void SpriteController::SpriteImpl::setDisplayId(int32_t displayId) {
437 AutoMutex _l(mController->mLock);
438
439 if (mLocked.state.displayId != displayId) {
440 mLocked.state.displayId = displayId;
441 invalidateLocked(DIRTY_DISPLAY_ID);
442 }
443}
444
Jeff Brown5541de92011-04-11 11:54:25 -0700445void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
Jeff Brown2352b972011-04-12 22:39:53 -0700446 bool wasDirty = mLocked.state.dirty;
447 mLocked.state.dirty |= dirty;
448
449 if (!wasDirty) {
450 mController->invalidateSpriteLocked(this);
Jeff Brown5541de92011-04-11 11:54:25 -0700451 }
452}
453
454} // namespace android