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"
18 //#define LOG_NDEBUG 0
19 
20 #include "SpriteController.h"
21 
22 #include <log/log.h>
23 #include <utils/String8.h>
24 #include <gui/Surface.h>
25 
26 namespace android {
27 
28 // --- SpriteController ---
29 
SpriteController(const sp<Looper> & looper,int32_t overlayLayer)30 SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) :
31         mLooper(looper), mOverlayLayer(overlayLayer) {
32     mHandler = new WeakMessageHandler(this);
33 
34     mLocked.transactionNestingCount = 0;
35     mLocked.deferredSpriteUpdate = false;
36 }
37 
~SpriteController()38 SpriteController::~SpriteController() {
39     mLooper->removeMessages(mHandler);
40 
41     if (mSurfaceComposerClient != NULL) {
42         mSurfaceComposerClient->dispose();
43         mSurfaceComposerClient.clear();
44     }
45 }
46 
createSprite()47 sp<Sprite> SpriteController::createSprite() {
48     return new SpriteImpl(this);
49 }
50 
openTransaction()51 void SpriteController::openTransaction() {
52     AutoMutex _l(mLock);
53 
54     mLocked.transactionNestingCount += 1;
55 }
56 
closeTransaction()57 void 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;
66         mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
67     }
68 }
69 
invalidateSpriteLocked(const sp<SpriteImpl> & sprite)70 void 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 
disposeSurfaceLocked(const sp<SurfaceControl> & surfaceControl)82 void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
83     bool wasEmpty = mLocked.disposedSurfaces.isEmpty();
84     mLocked.disposedSurfaces.push(surfaceControl);
85     if (wasEmpty) {
86         mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
87     }
88 }
89 
handleMessage(const Message & message)90 void 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 
doUpdateSprites()101 void 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 
112         numSprites = mLocked.invalidatedSprites.size();
113         for (size_t i = 0; i < numSprites; i++) {
114             const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i);
115 
116             updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
117             sprite->resetDirtyLocked();
118         }
119         mLocked.invalidatedSprites.clear();
120     } // 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()) {
128             update.state.surfaceWidth = update.state.icon.width();
129             update.state.surfaceHeight = update.state.icon.height();
130             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 
140     // Resize and/or reparent sprites if needed.
141     SurfaceComposerClient::Transaction t;
142     bool needApplyTransaction = false;
143     for (size_t i = 0; i < numSprites; i++) {
144         SpriteUpdate& update = updates.editItemAt(i);
145         if (update.state.surfaceControl == nullptr) {
146             continue;
147         }
148 
149         if (update.state.wantSurfaceVisible()) {
150             int32_t desiredWidth = update.state.icon.width();
151             int32_t desiredHeight = update.state.icon.height();
152             if (update.state.surfaceWidth < desiredWidth
153                     || update.state.surfaceHeight < desiredHeight) {
154                 needApplyTransaction = true;
155 
156                 update.state.surfaceControl->updateDefaultBufferSize(desiredWidth, desiredHeight);
157                 update.state.surfaceWidth = desiredWidth;
158                 update.state.surfaceHeight = desiredHeight;
159                 update.state.surfaceDrawn = false;
160                 update.surfaceChanged = surfaceChanged = true;
161 
162                 if (update.state.surfaceVisible) {
163                     t.hide(update.state.surfaceControl);
164                     update.state.surfaceVisible = false;
165                 }
166             }
167         }
168 
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         }
174     }
175     if (needApplyTransaction) {
176         t.apply();
177     }
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();
191             if (update.state.icon.draw(surface)) {
192                 update.state.surfaceDrawn = true;
193                 update.surfaceChanged = surfaceChanged = true;
194             }
195         }
196     }
197 
198     needApplyTransaction = false;
199     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
209                         | DIRTY_VISIBILITY | DIRTY_HOTSPOT | DIRTY_DISPLAY_ID
210                         | DIRTY_ICON_STYLE))))) {
211             needApplyTransaction = true;
212 
213             if (wantSurfaceVisibleAndDrawn
214                     && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
215                 t.setAlpha(update.state.surfaceControl,
216                         update.state.alpha);
217             }
218 
219             if (wantSurfaceVisibleAndDrawn
220                     && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
221                             | DIRTY_HOTSPOT)))) {
222                 t.setPosition(
223                         update.state.surfaceControl,
224                         update.state.positionX - update.state.icon.hotSpotX,
225                         update.state.positionY - update.state.icon.hotSpotY);
226             }
227 
228             if (wantSurfaceVisibleAndDrawn
229                     && (becomingVisible
230                             || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
231                 t.setMatrix(
232                         update.state.surfaceControl,
233                         update.state.transformationMatrix.dsdx,
234                         update.state.transformationMatrix.dtdx,
235                         update.state.transformationMatrix.dsdy,
236                         update.state.transformationMatrix.dtdy);
237             }
238 
239             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 
254             int32_t surfaceLayer = mOverlayLayer + update.state.layer;
255             if (wantSurfaceVisibleAndDrawn
256                     && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
257                 t.setLayer(update.state.surfaceControl, surfaceLayer);
258             }
259 
260             if (becomingVisible) {
261                 t.show(update.state.surfaceControl);
262 
263                 update.state.surfaceVisible = true;
264                 update.surfaceChanged = surfaceChanged = true;
265             } else if (becomingHidden) {
266                 t.hide(update.state.surfaceControl);
267 
268                 update.state.surfaceVisible = false;
269                 update.surfaceChanged = surfaceChanged = true;
270             }
271         }
272     }
273 
274     if (needApplyTransaction) {
275         status_t status = t.apply();
276         if (status) {
277             ALOGE("Error applying Surface transaction");
278         }
279     }
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 
doDisposeSurfaces()304 void SpriteController::doDisposeSurfaces() {
305     // Collect disposed surfaces.
306     Vector<sp<SurfaceControl> > disposedSurfaces;
307     { // acquire lock
308         AutoMutex _l(mLock);
309 
310         disposedSurfaces = mLocked.disposedSurfaces;
311         mLocked.disposedSurfaces.clear();
312     } // 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 
ensureSurfaceComposerClient()319 void SpriteController::ensureSurfaceComposerClient() {
320     if (mSurfaceComposerClient == NULL) {
321         mSurfaceComposerClient = new SurfaceComposerClient();
322     }
323 }
324 
obtainSurface(int32_t width,int32_t height)325 sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
326     ensureSurfaceComposerClient();
327 
328     sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
329             String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888,
330             ISurfaceComposerClient::eHidden |
331             ISurfaceComposerClient::eCursorWindow);
332     if (surfaceControl == NULL || !surfaceControl->isValid()) {
333         ALOGE("Error creating sprite surface.");
334         return NULL;
335     }
336     return surfaceControl;
337 }
338 
339 
340 // --- SpriteController::SpriteImpl ---
341 
SpriteImpl(const sp<SpriteController> controller)342 SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
343         mController(controller) {
344 }
345 
~SpriteImpl()346 SpriteController::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.
351     if (mLocked.state.surfaceControl != NULL) {
352         mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
353         mLocked.state.surfaceControl.clear();
354     }
355 }
356 
setIcon(const SpriteIcon & icon)357 void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
358     AutoMutex _l(mController->mLock);
359 
360     uint32_t dirty;
361     if (icon.isValid()) {
362         mLocked.state.icon.bitmap = icon.bitmap.copy(ANDROID_BITMAP_FORMAT_RGBA_8888);
363         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         }
372 
373         if (mLocked.state.icon.style != icon.style) {
374             mLocked.state.icon.style = icon.style;
375             dirty |= DIRTY_ICON_STYLE;
376         }
377     } else if (mLocked.state.icon.isValid()) {
378         mLocked.state.icon.bitmap.reset();
379         dirty = DIRTY_BITMAP | DIRTY_HOTSPOT | DIRTY_ICON_STYLE;
380     } else {
381         return; // setting to invalid icon and already invalid so nothing to do
382     }
383 
384     invalidateLocked(dirty);
385 }
386 
setVisible(bool visible)387 void SpriteController::SpriteImpl::setVisible(bool visible) {
388     AutoMutex _l(mController->mLock);
389 
390     if (mLocked.state.visible != visible) {
391         mLocked.state.visible = visible;
392         invalidateLocked(DIRTY_VISIBILITY);
393     }
394 }
395 
setPosition(float x,float y)396 void SpriteController::SpriteImpl::setPosition(float x, float y) {
397     AutoMutex _l(mController->mLock);
398 
399     if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
400         mLocked.state.positionX = x;
401         mLocked.state.positionY = y;
402         invalidateLocked(DIRTY_POSITION);
403     }
404 }
405 
setLayer(int32_t layer)406 void SpriteController::SpriteImpl::setLayer(int32_t layer) {
407     AutoMutex _l(mController->mLock);
408 
409     if (mLocked.state.layer != layer) {
410         mLocked.state.layer = layer;
411         invalidateLocked(DIRTY_LAYER);
412     }
413 }
414 
setAlpha(float alpha)415 void SpriteController::SpriteImpl::setAlpha(float alpha) {
416     AutoMutex _l(mController->mLock);
417 
418     if (mLocked.state.alpha != alpha) {
419         mLocked.state.alpha = alpha;
420         invalidateLocked(DIRTY_ALPHA);
421     }
422 }
423 
setTransformationMatrix(const SpriteTransformationMatrix & matrix)424 void SpriteController::SpriteImpl::setTransformationMatrix(
425         const SpriteTransformationMatrix& matrix) {
426     AutoMutex _l(mController->mLock);
427 
428     if (mLocked.state.transformationMatrix != matrix) {
429         mLocked.state.transformationMatrix = matrix;
430         invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
431     }
432 }
433 
setDisplayId(int32_t displayId)434 void 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 
invalidateLocked(uint32_t dirty)443 void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
444     bool wasDirty = mLocked.state.dirty;
445     mLocked.state.dirty |= dirty;
446 
447     if (!wasDirty) {
448         mController->invalidateSpriteLocked(this);
449     }
450 }
451 
452 } // namespace android
453