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 
19 //#define LOG_NDEBUG 0
20 
21 #include "SpriteController.h"
22 
23 #include <cutils/log.h>
24 #include <utils/String8.h>
25 #include <gui/Surface.h>
26 
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wunused-parameter"
29 #include <SkBitmap.h>
30 #include <SkCanvas.h>
31 #include <SkColor.h>
32 #include <SkPaint.h>
33 #include <SkXfermode.h>
34 #pragma GCC diagnostic pop
35 
36 #include <android/native_window.h>
37 
38 namespace android {
39 
40 // --- SpriteController ---
41 
SpriteController(const sp<Looper> & looper,int32_t overlayLayer)42 SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) :
43         mLooper(looper), mOverlayLayer(overlayLayer) {
44     mHandler = new WeakMessageHandler(this);
45 
46     mLocked.transactionNestingCount = 0;
47     mLocked.deferredSpriteUpdate = false;
48 }
49 
~SpriteController()50 SpriteController::~SpriteController() {
51     mLooper->removeMessages(mHandler);
52 
53     if (mSurfaceComposerClient != NULL) {
54         mSurfaceComposerClient->dispose();
55         mSurfaceComposerClient.clear();
56     }
57 }
58 
createSprite()59 sp<Sprite> SpriteController::createSprite() {
60     return new SpriteImpl(this);
61 }
62 
openTransaction()63 void SpriteController::openTransaction() {
64     AutoMutex _l(mLock);
65 
66     mLocked.transactionNestingCount += 1;
67 }
68 
closeTransaction()69 void SpriteController::closeTransaction() {
70     AutoMutex _l(mLock);
71 
72     LOG_ALWAYS_FATAL_IF(mLocked.transactionNestingCount == 0,
73             "Sprite closeTransaction() called but there is no open sprite transaction");
74 
75     mLocked.transactionNestingCount -= 1;
76     if (mLocked.transactionNestingCount == 0 && mLocked.deferredSpriteUpdate) {
77         mLocked.deferredSpriteUpdate = false;
78         mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
79     }
80 }
81 
invalidateSpriteLocked(const sp<SpriteImpl> & sprite)82 void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) {
83     bool wasEmpty = mLocked.invalidatedSprites.isEmpty();
84     mLocked.invalidatedSprites.push(sprite);
85     if (wasEmpty) {
86         if (mLocked.transactionNestingCount != 0) {
87             mLocked.deferredSpriteUpdate = true;
88         } else {
89             mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
90         }
91     }
92 }
93 
disposeSurfaceLocked(const sp<SurfaceControl> & surfaceControl)94 void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
95     bool wasEmpty = mLocked.disposedSurfaces.isEmpty();
96     mLocked.disposedSurfaces.push(surfaceControl);
97     if (wasEmpty) {
98         mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
99     }
100 }
101 
handleMessage(const Message & message)102 void SpriteController::handleMessage(const Message& message) {
103     switch (message.what) {
104     case MSG_UPDATE_SPRITES:
105         doUpdateSprites();
106         break;
107     case MSG_DISPOSE_SURFACES:
108         doDisposeSurfaces();
109         break;
110     }
111 }
112 
doUpdateSprites()113 void SpriteController::doUpdateSprites() {
114     // Collect information about sprite updates.
115     // Each sprite update record includes a reference to its associated sprite so we can
116     // be certain the sprites will not be deleted while this function runs.  Sprites
117     // may invalidate themselves again during this time but we will handle those changes
118     // in the next iteration.
119     Vector<SpriteUpdate> updates;
120     size_t numSprites;
121     { // acquire lock
122         AutoMutex _l(mLock);
123 
124         numSprites = mLocked.invalidatedSprites.size();
125         for (size_t i = 0; i < numSprites; i++) {
126             const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i);
127 
128             updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
129             sprite->resetDirtyLocked();
130         }
131         mLocked.invalidatedSprites.clear();
132     } // release lock
133 
134     // Create missing surfaces.
135     bool surfaceChanged = false;
136     for (size_t i = 0; i < numSprites; i++) {
137         SpriteUpdate& update = updates.editItemAt(i);
138 
139         if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) {
140             update.state.surfaceWidth = update.state.icon.bitmap.width();
141             update.state.surfaceHeight = update.state.icon.bitmap.height();
142             update.state.surfaceDrawn = false;
143             update.state.surfaceVisible = false;
144             update.state.surfaceControl = obtainSurface(
145                     update.state.surfaceWidth, update.state.surfaceHeight);
146             if (update.state.surfaceControl != NULL) {
147                 update.surfaceChanged = surfaceChanged = true;
148             }
149         }
150     }
151 
152     // Resize sprites if needed, inside a global transaction.
153     bool haveGlobalTransaction = false;
154     for (size_t i = 0; i < numSprites; i++) {
155         SpriteUpdate& update = updates.editItemAt(i);
156 
157         if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) {
158             int32_t desiredWidth = update.state.icon.bitmap.width();
159             int32_t desiredHeight = update.state.icon.bitmap.height();
160             if (update.state.surfaceWidth < desiredWidth
161                     || update.state.surfaceHeight < desiredHeight) {
162                 if (!haveGlobalTransaction) {
163                     SurfaceComposerClient::openGlobalTransaction();
164                     haveGlobalTransaction = true;
165                 }
166 
167                 status_t status = update.state.surfaceControl->setSize(desiredWidth, desiredHeight);
168                 if (status) {
169                     ALOGE("Error %d resizing sprite surface from %dx%d to %dx%d",
170                             status, update.state.surfaceWidth, update.state.surfaceHeight,
171                             desiredWidth, desiredHeight);
172                 } else {
173                     update.state.surfaceWidth = desiredWidth;
174                     update.state.surfaceHeight = desiredHeight;
175                     update.state.surfaceDrawn = false;
176                     update.surfaceChanged = surfaceChanged = true;
177 
178                     if (update.state.surfaceVisible) {
179                         status = update.state.surfaceControl->hide();
180                         if (status) {
181                             ALOGE("Error %d hiding sprite surface after resize.", status);
182                         } else {
183                             update.state.surfaceVisible = false;
184                         }
185                     }
186                 }
187             }
188         }
189     }
190     if (haveGlobalTransaction) {
191         SurfaceComposerClient::closeGlobalTransaction();
192     }
193 
194     // Redraw sprites if needed.
195     for (size_t i = 0; i < numSprites; i++) {
196         SpriteUpdate& update = updates.editItemAt(i);
197 
198         if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
199             update.state.surfaceDrawn = false;
200             update.surfaceChanged = surfaceChanged = true;
201         }
202 
203         if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
204                 && update.state.wantSurfaceVisible()) {
205             sp<Surface> surface = update.state.surfaceControl->getSurface();
206             ANativeWindow_Buffer outBuffer;
207             status_t status = surface->lock(&outBuffer, NULL);
208             if (status) {
209                 ALOGE("Error %d locking sprite surface before drawing.", status);
210             } else {
211                 SkBitmap surfaceBitmap;
212                 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
213                 surfaceBitmap.installPixels(SkImageInfo::MakeN32Premul(outBuffer.width, outBuffer.height),
214                                             outBuffer.bits, bpr);
215 
216                 SkCanvas surfaceCanvas(surfaceBitmap);
217 
218                 SkPaint paint;
219                 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
220                 surfaceCanvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint);
221 
222                 if (outBuffer.width > update.state.icon.bitmap.width()) {
223                     paint.setColor(0); // transparent fill color
224                     surfaceCanvas.drawRectCoords(update.state.icon.bitmap.width(), 0,
225                             outBuffer.width, update.state.icon.bitmap.height(), paint);
226                 }
227                 if (outBuffer.height > update.state.icon.bitmap.height()) {
228                     paint.setColor(0); // transparent fill color
229                     surfaceCanvas.drawRectCoords(0, update.state.icon.bitmap.height(),
230                             outBuffer.width, outBuffer.height, paint);
231                 }
232 
233                 status = surface->unlockAndPost();
234                 if (status) {
235                     ALOGE("Error %d unlocking and posting sprite surface after drawing.", status);
236                 } else {
237                     update.state.surfaceDrawn = true;
238                     update.surfaceChanged = surfaceChanged = true;
239                 }
240             }
241         }
242     }
243 
244     // Set sprite surface properties and make them visible.
245     bool haveTransaction = false;
246     for (size_t i = 0; i < numSprites; i++) {
247         SpriteUpdate& update = updates.editItemAt(i);
248 
249         bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible()
250                 && update.state.surfaceDrawn;
251         bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible;
252         bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible;
253         if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
254                 || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
255                         | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
256                         | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) {
257             status_t status;
258             if (!haveTransaction) {
259                 SurfaceComposerClient::openGlobalTransaction();
260                 haveTransaction = true;
261             }
262 
263             if (wantSurfaceVisibleAndDrawn
264                     && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
265                 status = update.state.surfaceControl->setAlpha(update.state.alpha);
266                 if (status) {
267                     ALOGE("Error %d setting sprite surface alpha.", status);
268                 }
269             }
270 
271             if (wantSurfaceVisibleAndDrawn
272                     && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
273                             | DIRTY_HOTSPOT)))) {
274                 status = update.state.surfaceControl->setPosition(
275                         update.state.positionX - update.state.icon.hotSpotX,
276                         update.state.positionY - update.state.icon.hotSpotY);
277                 if (status) {
278                     ALOGE("Error %d setting sprite surface position.", status);
279                 }
280             }
281 
282             if (wantSurfaceVisibleAndDrawn
283                     && (becomingVisible
284                             || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
285                 status = update.state.surfaceControl->setMatrix(
286                         update.state.transformationMatrix.dsdx,
287                         update.state.transformationMatrix.dtdx,
288                         update.state.transformationMatrix.dsdy,
289                         update.state.transformationMatrix.dtdy);
290                 if (status) {
291                     ALOGE("Error %d setting sprite surface transformation matrix.", status);
292                 }
293             }
294 
295             int32_t surfaceLayer = mOverlayLayer + update.state.layer;
296             if (wantSurfaceVisibleAndDrawn
297                     && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
298                 status = update.state.surfaceControl->setLayer(surfaceLayer);
299                 if (status) {
300                     ALOGE("Error %d setting sprite surface layer.", status);
301                 }
302             }
303 
304             if (becomingVisible) {
305                 status = update.state.surfaceControl->show();
306                 if (status) {
307                     ALOGE("Error %d showing sprite surface.", status);
308                 } else {
309                     update.state.surfaceVisible = true;
310                     update.surfaceChanged = surfaceChanged = true;
311                 }
312             } else if (becomingHidden) {
313                 status = update.state.surfaceControl->hide();
314                 if (status) {
315                     ALOGE("Error %d hiding sprite surface.", status);
316                 } else {
317                     update.state.surfaceVisible = false;
318                     update.surfaceChanged = surfaceChanged = true;
319                 }
320             }
321         }
322     }
323 
324     if (haveTransaction) {
325         SurfaceComposerClient::closeGlobalTransaction();
326     }
327 
328     // If any surfaces were changed, write back the new surface properties to the sprites.
329     if (surfaceChanged) { // acquire lock
330         AutoMutex _l(mLock);
331 
332         for (size_t i = 0; i < numSprites; i++) {
333             const SpriteUpdate& update = updates.itemAt(i);
334 
335             if (update.surfaceChanged) {
336                 update.sprite->setSurfaceLocked(update.state.surfaceControl,
337                         update.state.surfaceWidth, update.state.surfaceHeight,
338                         update.state.surfaceDrawn, update.state.surfaceVisible);
339             }
340         }
341     } // release lock
342 
343     // Clear the sprite update vector outside the lock.  It is very important that
344     // we do not clear sprite references inside the lock since we could be releasing
345     // the last remaining reference to the sprite here which would result in the
346     // sprite being deleted and the lock being reacquired by the sprite destructor
347     // while already held.
348     updates.clear();
349 }
350 
doDisposeSurfaces()351 void SpriteController::doDisposeSurfaces() {
352     // Collect disposed surfaces.
353     Vector<sp<SurfaceControl> > disposedSurfaces;
354     { // acquire lock
355         AutoMutex _l(mLock);
356 
357         disposedSurfaces = mLocked.disposedSurfaces;
358         mLocked.disposedSurfaces.clear();
359     } // release lock
360 
361     // Release the last reference to each surface outside of the lock.
362     // We don't want the surfaces to be deleted while we are holding our lock.
363     disposedSurfaces.clear();
364 }
365 
ensureSurfaceComposerClient()366 void SpriteController::ensureSurfaceComposerClient() {
367     if (mSurfaceComposerClient == NULL) {
368         mSurfaceComposerClient = new SurfaceComposerClient();
369     }
370 }
371 
obtainSurface(int32_t width,int32_t height)372 sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
373     ensureSurfaceComposerClient();
374 
375     sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
376             String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888,
377             ISurfaceComposerClient::eHidden |
378             ISurfaceComposerClient::eCursorWindow);
379     if (surfaceControl == NULL || !surfaceControl->isValid()) {
380         ALOGE("Error creating sprite surface.");
381         return NULL;
382     }
383     return surfaceControl;
384 }
385 
386 
387 // --- SpriteController::SpriteImpl ---
388 
SpriteImpl(const sp<SpriteController> controller)389 SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
390         mController(controller) {
391 }
392 
~SpriteImpl()393 SpriteController::SpriteImpl::~SpriteImpl() {
394     AutoMutex _m(mController->mLock);
395 
396     // Let the controller take care of deleting the last reference to sprite
397     // surfaces so that we do not block the caller on an IPC here.
398     if (mLocked.state.surfaceControl != NULL) {
399         mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
400         mLocked.state.surfaceControl.clear();
401     }
402 }
403 
setIcon(const SpriteIcon & icon)404 void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
405     AutoMutex _l(mController->mLock);
406 
407     uint32_t dirty;
408     if (icon.isValid()) {
409         icon.bitmap.copyTo(&mLocked.state.icon.bitmap, kN32_SkColorType);
410 
411         if (!mLocked.state.icon.isValid()
412                 || mLocked.state.icon.hotSpotX != icon.hotSpotX
413                 || mLocked.state.icon.hotSpotY != icon.hotSpotY) {
414             mLocked.state.icon.hotSpotX = icon.hotSpotX;
415             mLocked.state.icon.hotSpotY = icon.hotSpotY;
416             dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
417         } else {
418             dirty = DIRTY_BITMAP;
419         }
420     } else if (mLocked.state.icon.isValid()) {
421         mLocked.state.icon.bitmap.reset();
422         dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
423     } else {
424         return; // setting to invalid icon and already invalid so nothing to do
425     }
426 
427     invalidateLocked(dirty);
428 }
429 
setVisible(bool visible)430 void SpriteController::SpriteImpl::setVisible(bool visible) {
431     AutoMutex _l(mController->mLock);
432 
433     if (mLocked.state.visible != visible) {
434         mLocked.state.visible = visible;
435         invalidateLocked(DIRTY_VISIBILITY);
436     }
437 }
438 
setPosition(float x,float y)439 void SpriteController::SpriteImpl::setPosition(float x, float y) {
440     AutoMutex _l(mController->mLock);
441 
442     if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
443         mLocked.state.positionX = x;
444         mLocked.state.positionY = y;
445         invalidateLocked(DIRTY_POSITION);
446     }
447 }
448 
setLayer(int32_t layer)449 void SpriteController::SpriteImpl::setLayer(int32_t layer) {
450     AutoMutex _l(mController->mLock);
451 
452     if (mLocked.state.layer != layer) {
453         mLocked.state.layer = layer;
454         invalidateLocked(DIRTY_LAYER);
455     }
456 }
457 
setAlpha(float alpha)458 void SpriteController::SpriteImpl::setAlpha(float alpha) {
459     AutoMutex _l(mController->mLock);
460 
461     if (mLocked.state.alpha != alpha) {
462         mLocked.state.alpha = alpha;
463         invalidateLocked(DIRTY_ALPHA);
464     }
465 }
466 
setTransformationMatrix(const SpriteTransformationMatrix & matrix)467 void SpriteController::SpriteImpl::setTransformationMatrix(
468         const SpriteTransformationMatrix& matrix) {
469     AutoMutex _l(mController->mLock);
470 
471     if (mLocked.state.transformationMatrix != matrix) {
472         mLocked.state.transformationMatrix = matrix;
473         invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
474     }
475 }
476 
invalidateLocked(uint32_t dirty)477 void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
478     bool wasDirty = mLocked.state.dirty;
479     mLocked.state.dirty |= dirty;
480 
481     if (!wasDirty) {
482         mController->invalidateSpriteLocked(this);
483     }
484 }
485 
486 } // namespace android
487