blob: d10e68816d2863eea01b4c2bfcb4a3d74917bdf2 [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
Prabir Pradhan62726592021-08-09 15:51:25 +0000156 update.state.surfaceControl->updateDefaultBufferSize(desiredWidth, desiredHeight);
Robert Carre13b58e2017-08-31 14:50:44 -0700157 update.state.surfaceWidth = desiredWidth;
158 update.state.surfaceHeight = desiredHeight;
159 update.state.surfaceDrawn = false;
160 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700161
Robert Carre13b58e2017-08-31 14:50:44 -0700162 if (update.state.surfaceVisible) {
163 t.hide(update.state.surfaceControl);
164 update.state.surfaceVisible = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700165 }
166 }
167 }
Arthur Hungb9b32002018-12-18 17:39:43 +0800168
169 // If surface is a new one, we have to set right layer stack.
170 if (update.surfaceChanged || update.state.dirty & DIRTY_DISPLAY_ID) {
171 t.setLayerStack(update.state.surfaceControl, update.state.displayId);
172 needApplyTransaction = true;
173 }
Jeff Brown5541de92011-04-11 11:54:25 -0700174 }
Robert Carre13b58e2017-08-31 14:50:44 -0700175 if (needApplyTransaction) {
176 t.apply();
Jeff Brown5541de92011-04-11 11:54:25 -0700177 }
178
179 // Redraw sprites if needed.
180 for (size_t i = 0; i < numSprites; i++) {
181 SpriteUpdate& update = updates.editItemAt(i);
182
183 if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
184 update.state.surfaceDrawn = false;
185 update.surfaceChanged = surfaceChanged = true;
186 }
187
188 if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
189 && update.state.wantSurfaceVisible()) {
190 sp<Surface> surface = update.state.surfaceControl->getSurface();
Garfield Tan7e3457e2020-05-28 14:31:29 -0700191 if (update.state.icon.draw(surface)) {
192 update.state.surfaceDrawn = true;
193 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700194 }
195 }
196 }
197
Robert Carre13b58e2017-08-31 14:50:44 -0700198 needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700199 for (size_t i = 0; i < numSprites; i++) {
200 SpriteUpdate& update = updates.editItemAt(i);
201
202 bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible()
203 && update.state.surfaceDrawn;
204 bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible;
205 bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible;
206 if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
207 || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
208 | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
Garfield Tan67e479a2019-08-05 16:47:40 -0700209 | DIRTY_VISIBILITY | DIRTY_HOTSPOT | DIRTY_DISPLAY_ID
210 | DIRTY_ICON_STYLE))))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700211 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700212
213 if (wantSurfaceVisibleAndDrawn
214 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700215 t.setAlpha(update.state.surfaceControl,
216 update.state.alpha);
Jeff Brown5541de92011-04-11 11:54:25 -0700217 }
218
219 if (wantSurfaceVisibleAndDrawn
220 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
221 | DIRTY_HOTSPOT)))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700222 t.setPosition(
223 update.state.surfaceControl,
Jeff Brown2352b972011-04-12 22:39:53 -0700224 update.state.positionX - update.state.icon.hotSpotX,
225 update.state.positionY - update.state.icon.hotSpotY);
Jeff Brown5541de92011-04-11 11:54:25 -0700226 }
227
228 if (wantSurfaceVisibleAndDrawn
229 && (becomingVisible
230 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700231 t.setMatrix(
232 update.state.surfaceControl,
Jeff Brown5541de92011-04-11 11:54:25 -0700233 update.state.transformationMatrix.dsdx,
234 update.state.transformationMatrix.dtdx,
235 update.state.transformationMatrix.dsdy,
236 update.state.transformationMatrix.dtdy);
Jeff Brown5541de92011-04-11 11:54:25 -0700237 }
238
Garfield Tan67e479a2019-08-05 16:47:40 -0700239 if (wantSurfaceVisibleAndDrawn
240 && (becomingVisible
241 || (update.state.dirty & (DIRTY_HOTSPOT | DIRTY_ICON_STYLE)))) {
242 Parcel p;
243 p.writeInt32(update.state.icon.style);
244 p.writeFloat(update.state.icon.hotSpotX);
245 p.writeFloat(update.state.icon.hotSpotY);
246
247 // Pass cursor metadata in the sprite surface so that when Android is running as a
248 // client OS (e.g. ARC++) the host OS can get the requested cursor metadata and
249 // update mouse cursor in the host OS.
250 t.setMetadata(
251 update.state.surfaceControl, METADATA_MOUSE_CURSOR, p);
252 }
253
Jeff Brown5541de92011-04-11 11:54:25 -0700254 int32_t surfaceLayer = mOverlayLayer + update.state.layer;
255 if (wantSurfaceVisibleAndDrawn
256 && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700257 t.setLayer(update.state.surfaceControl, surfaceLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700258 }
259
260 if (becomingVisible) {
Robert Carre13b58e2017-08-31 14:50:44 -0700261 t.show(update.state.surfaceControl);
262
263 update.state.surfaceVisible = true;
264 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700265 } else if (becomingHidden) {
Robert Carre13b58e2017-08-31 14:50:44 -0700266 t.hide(update.state.surfaceControl);
267
268 update.state.surfaceVisible = false;
269 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700270 }
271 }
272 }
273
Robert Carre13b58e2017-08-31 14:50:44 -0700274 if (needApplyTransaction) {
275 status_t status = t.apply();
276 if (status) {
277 ALOGE("Error applying Surface transaction");
278 }
Jeff Brown5541de92011-04-11 11:54:25 -0700279 }
280
281 // If any surfaces were changed, write back the new surface properties to the sprites.
282 if (surfaceChanged) { // acquire lock
283 AutoMutex _l(mLock);
284
285 for (size_t i = 0; i < numSprites; i++) {
286 const SpriteUpdate& update = updates.itemAt(i);
287
288 if (update.surfaceChanged) {
289 update.sprite->setSurfaceLocked(update.state.surfaceControl,
290 update.state.surfaceWidth, update.state.surfaceHeight,
291 update.state.surfaceDrawn, update.state.surfaceVisible);
292 }
293 }
294 } // release lock
295
296 // Clear the sprite update vector outside the lock. It is very important that
297 // we do not clear sprite references inside the lock since we could be releasing
298 // the last remaining reference to the sprite here which would result in the
299 // sprite being deleted and the lock being reacquired by the sprite destructor
300 // while already held.
301 updates.clear();
302}
303
304void SpriteController::doDisposeSurfaces() {
305 // Collect disposed surfaces.
306 Vector<sp<SurfaceControl> > disposedSurfaces;
307 { // acquire lock
Jeff Brown2352b972011-04-12 22:39:53 -0700308 AutoMutex _l(mLock);
309
310 disposedSurfaces = mLocked.disposedSurfaces;
311 mLocked.disposedSurfaces.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700312 } // release lock
313
314 // Release the last reference to each surface outside of the lock.
315 // We don't want the surfaces to be deleted while we are holding our lock.
316 disposedSurfaces.clear();
317}
318
319void SpriteController::ensureSurfaceComposerClient() {
320 if (mSurfaceComposerClient == NULL) {
321 mSurfaceComposerClient = new SurfaceComposerClient();
322 }
323}
324
325sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
326 ensureSurfaceComposerClient();
327
328 sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
Jeff Brown64a55af2012-08-26 02:47:39 -0700329 String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888,
Riley Andrews68eccda2014-07-07 11:47:35 -0700330 ISurfaceComposerClient::eHidden |
331 ISurfaceComposerClient::eCursorWindow);
Mathias Agopian52800612013-02-14 17:11:20 -0800332 if (surfaceControl == NULL || !surfaceControl->isValid()) {
Steve Block3762c312012-01-06 19:20:56 +0000333 ALOGE("Error creating sprite surface.");
Jeff Brown5541de92011-04-11 11:54:25 -0700334 return NULL;
335 }
336 return surfaceControl;
337}
338
339
340// --- SpriteController::SpriteImpl ---
341
342SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
Jeff Brown2352b972011-04-12 22:39:53 -0700343 mController(controller) {
Jeff Brown5541de92011-04-11 11:54:25 -0700344}
345
346SpriteController::SpriteImpl::~SpriteImpl() {
347 AutoMutex _m(mController->mLock);
348
349 // Let the controller take care of deleting the last reference to sprite
350 // surfaces so that we do not block the caller on an IPC here.
Jeff Brown2352b972011-04-12 22:39:53 -0700351 if (mLocked.state.surfaceControl != NULL) {
352 mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
353 mLocked.state.surfaceControl.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700354 }
355}
356
Jeff Brown2352b972011-04-12 22:39:53 -0700357void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
Jeff Brown5541de92011-04-11 11:54:25 -0700358 AutoMutex _l(mController->mLock);
359
Jeff Brown2352b972011-04-12 22:39:53 -0700360 uint32_t dirty;
361 if (icon.isValid()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400362 mLocked.state.icon.bitmap = icon.bitmap.copy(ANDROID_BITMAP_FORMAT_RGBA_8888);
Jeff Brown2352b972011-04-12 22:39:53 -0700363 if (!mLocked.state.icon.isValid()
364 || mLocked.state.icon.hotSpotX != icon.hotSpotX
365 || mLocked.state.icon.hotSpotY != icon.hotSpotY) {
366 mLocked.state.icon.hotSpotX = icon.hotSpotX;
367 mLocked.state.icon.hotSpotY = icon.hotSpotY;
368 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
369 } else {
370 dirty = DIRTY_BITMAP;
371 }
Garfield Tan67e479a2019-08-05 16:47:40 -0700372
373 if (mLocked.state.icon.style != icon.style) {
374 mLocked.state.icon.style = icon.style;
375 dirty |= DIRTY_ICON_STYLE;
376 }
Jeff Brown2352b972011-04-12 22:39:53 -0700377 } else if (mLocked.state.icon.isValid()) {
378 mLocked.state.icon.bitmap.reset();
Garfield Tan67e479a2019-08-05 16:47:40 -0700379 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT | DIRTY_ICON_STYLE;
Jeff Brown2352b972011-04-12 22:39:53 -0700380 } else {
381 return; // setting to invalid icon and already invalid so nothing to do
Jeff Brown5541de92011-04-11 11:54:25 -0700382 }
383
384 invalidateLocked(dirty);
385}
386
387void SpriteController::SpriteImpl::setVisible(bool visible) {
388 AutoMutex _l(mController->mLock);
389
Jeff Brown2352b972011-04-12 22:39:53 -0700390 if (mLocked.state.visible != visible) {
391 mLocked.state.visible = visible;
Jeff Brown5541de92011-04-11 11:54:25 -0700392 invalidateLocked(DIRTY_VISIBILITY);
393 }
394}
395
396void SpriteController::SpriteImpl::setPosition(float x, float y) {
397 AutoMutex _l(mController->mLock);
398
Jeff Brown2352b972011-04-12 22:39:53 -0700399 if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
400 mLocked.state.positionX = x;
401 mLocked.state.positionY = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700402 invalidateLocked(DIRTY_POSITION);
403 }
404}
405
406void SpriteController::SpriteImpl::setLayer(int32_t layer) {
407 AutoMutex _l(mController->mLock);
408
Jeff Brown2352b972011-04-12 22:39:53 -0700409 if (mLocked.state.layer != layer) {
410 mLocked.state.layer = layer;
Jeff Brown5541de92011-04-11 11:54:25 -0700411 invalidateLocked(DIRTY_LAYER);
412 }
413}
414
415void SpriteController::SpriteImpl::setAlpha(float alpha) {
416 AutoMutex _l(mController->mLock);
417
Jeff Brown2352b972011-04-12 22:39:53 -0700418 if (mLocked.state.alpha != alpha) {
419 mLocked.state.alpha = alpha;
Jeff Brown5541de92011-04-11 11:54:25 -0700420 invalidateLocked(DIRTY_ALPHA);
421 }
422}
423
424void SpriteController::SpriteImpl::setTransformationMatrix(
425 const SpriteTransformationMatrix& matrix) {
426 AutoMutex _l(mController->mLock);
427
Jeff Brown2352b972011-04-12 22:39:53 -0700428 if (mLocked.state.transformationMatrix != matrix) {
429 mLocked.state.transformationMatrix = matrix;
Jeff Brown5541de92011-04-11 11:54:25 -0700430 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
431 }
432}
433
Arthur Hungb9b32002018-12-18 17:39:43 +0800434void SpriteController::SpriteImpl::setDisplayId(int32_t displayId) {
435 AutoMutex _l(mController->mLock);
436
437 if (mLocked.state.displayId != displayId) {
438 mLocked.state.displayId = displayId;
439 invalidateLocked(DIRTY_DISPLAY_ID);
440 }
441}
442
Jeff Brown5541de92011-04-11 11:54:25 -0700443void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
Jeff Brown2352b972011-04-12 22:39:53 -0700444 bool wasDirty = mLocked.state.dirty;
445 mLocked.state.dirty |= dirty;
446
447 if (!wasDirty) {
448 mController->invalidateSpriteLocked(this);
Jeff Brown5541de92011-04-11 11:54:25 -0700449 }
450}
451
452} // namespace android